Toni-SM / skrl

Modular reinforcement learning library (on PyTorch and JAX) with support for NVIDIA Isaac Gym, Omniverse Isaac Gym and Isaac Lab
https://skrl.readthedocs.io/
MIT License
551 stars 55 forks source link

Problem Running Vectorized Gymnasium Environments in PyTorch #188

Closed jpmartin42 closed 1 month ago

jpmartin42 commented 2 months ago

Description

When running examples, I was having issues running vectorized Gynasium environments.

For instance, running the following script worked fine:

import gymnasium as gym
from skrl.envs.wrappers.torch import wrap_env

env = gym.make("CartPole-v1")

env = wrap_env(env)

but the following script:

import gymnasium as gym
from skrl.envs.wrappers.torch import wrap_env

env = gym.vector.make("CartPole-v1", num_envs=4, asynchronous=False)
env = wrap_env(env)

... returned the following error:

Traceback (most recent call last):
  File "torch_gymnasium_pendulum_vector_ddpg.py", line 60, in <module>
    env = wrap_env(env)
  File "/home/josh/anaconda3/envs/raisim_python3/lib/python3.8/site-packages/skrl/envs/wrappers/torch/__init__.py", line 154, in wrap_env
    raise ValueError(f"Unknown wrapper type: {wrapper}")
ValueError: Unknown wrapper type: ['gymnasium.vector.vector_env.VectorEnv']

Note that, if I switch over to using gym.vec_make() instead of gym.vector.make(), then I get the same error as above except that the unknown wrapper type becomes ['gymnasium.experimental.vector.vector_env.VectorEnv']

I traced this issue back to __init__.py in wrappers/torch: when checking for Gymnasium environment types (line 100), it only checks for the single environment and not the vectorized environment version. Adding to the line to become the following fixed my issue:

elif _in("gymnasium.core.Env", base_classes) or _in("gymnasium.core.Wrapper", base_classes) or _in("gymnasium.vector.vector_env.VectorEnv", base_classes) or _in("gymnasium.experimental.vector.vector_env.VectorEnv"):

Following this change, I'm able to run the script above and run the training examples with vectorized Gymnasium environments.

Not sure if I just made a simple mistake to give myself this error, but the fix was quick and I thought I'd pass it along just in case!

What skrl version are you using?

1.2.0

What ML framework/library version are you using?

PyTorch

Additional system information

Ubuntu 20.04

Toni-SM commented 2 months ago

Hi @jpmartin42

This issue is already addressed in the toni/update_wrappers branch, to be merged to the develop branch soon

https://github.com/Toni-SM/skrl/blob/3abbf7672142a4c7b5d7342fb00d9ac3839842ec/skrl/envs/wrappers/torch/__init__.py#L120-L121

https://github.com/Toni-SM/skrl/blob/3abbf7672142a4c7b5d7342fb00d9ac3839842ec/skrl/envs/wrappers/torch/gymnasium_envs.py#L23

Toni-SM commented 1 month ago

Solved in https://github.com/Toni-SM/skrl/releases/tag/1.3.0