openai / gym

A toolkit for developing and comparing reinforcement learning algorithms.
https://www.gymlibrary.dev
Other
34.2k stars 8.58k forks source link

[Question] AsyncVectorEnv getting hung up as `pip.recv()` not returning anything for `_check_observation_spaces` #3273

Open LeoHink opened 1 month ago

LeoHink commented 1 month ago

I'm having an issue initializing an AsyncVectorEnv for a custom environment which uses pybullet. I should specify that I am using gym==0.15.3 as this is the only compatible one for the environment I'm using.

I am using the following make_env function:

def make_env(env_id, idx, capture_video, seed):
    def thunk():
        if capture_video and idx == 0:
            env = gym.make(env_id)
            env.setup_camera(camera_eye=[0.5, -0.75, 1.5], camera_target=[-0.2, 0, 0.75], fov=60, camera_width=1920//4, camera_height=1080//4)
            # env = gym.wrappers.RecordVideo(env, f"videos/{run_name}")
            env.seed(seed)
        else:
            env = gym.make(env_id)
        env.seed(seed)
        env.action_space.seed(seed)
        env.observation_space.seed(seed)    

        return env

    return thunk

And envs = AsyncVectorEnv([make_env(env_id, i, False, seed) for i in range(num_envs)])

I've added some print statements to narrow down where the code is hanging. It seems to be in the def _check_observation_spaces(self): which is in line 307 in async_vector_env.py. Specifically after the loop:

        for pipe in self.parent_pipes:
            print(f"pipe: {pipe} /n parent pipes: {self.parent_pipes}")
            pipe.send(('_check_observation_space', self.single_observation_space))

This does seem to send the message but I do not receive anything when same_spaces, successes = zip(*[pipe.recv() for pipe in self.parent_pipes]) is called.

The code works fine when testing with cartpole, so it might be something to do with my environment and pybullet. Nonetheless wanted to post this here incase anyone is able to help. Thank you!

pseudo-rnd-thoughts commented 1 month ago

Gym 0.15 is 5 years old, I suspect that no one will be able to help without the project being updated to a more recent version

Are you able to use a sync vector env?

LeoHink commented 1 month ago

Hey, thanks for the response. I realize the gym version I'm using is outdated and that I might just have to bite the bullet and update the project going forward. But yeah, everything else seems to work fine. I can use SyncVectorEnv, which works as expected. Am trying to use AsyncVectorEnv as I'm expecting this to significantly speed up training.

pseudo-rnd-thoughts commented 1 month ago

Async will speed things up but generally not by that much, maybe 10/20% would be my guess

dan1els commented 1 month ago

Hi I'm also having same behavior when using AsyncVectorEnv. It's just hanging. Version 0.29.1

I'm creating envs this way:

cubic_envs = AsyncVectorEnv([lambda: PhysicsWrapper(
            ExportingWrapper(gym.make('GraphCubicEnv-v0', env_idx=i, exporter_ctor=lambda: ZmqExporter(args['exporter_addr']), reward_fn = reward_h, done_fn = done_h, **args)), 
            lambda: ZmqPhysicsClient(args['phys_addr']),
            reward_phs,
            **args
        ) for i in range(0,5)]
 )

UPD: I kinda figured it out. In my case it seems some of the objects, which I assume can't be easily serialized (like lambdas) can't be passed through constructor when you making async env. I just made a child class for my env and overridden all stuff like that and it start working. It's a little bit not convenient in my case, because I wanted to inject different reward functions to envs and test

In the OP case I guess the problem is in the invocation of env.setup_camera method explicitly.