Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gymnasium Integration #789

Merged
merged 29 commits into from
Feb 3, 2023
Merged

Conversation

Markus28
Copy link
Collaborator

@Markus28 Markus28 commented Dec 29, 2022

Changes:

  • Disclaimer in README
  • Replaced all occurences of Gym with Gymnasium
  • Removed code that is now dead since we no longer need to support the old step API
  • Updated type hints to only allow new step API
  • Increased required version of envpool to support Gymnasium
  • Increased required version of PettingZoo to support Gymnasium
  • Updated PettingZooEnv to only use the new step API, removed hack to also support old API
  • I had to add some # type: ignore comments, due to new type hinting in Gymnasium. I'm not that familiar with type hinting but I believe that the issue is on the Gymnasium side and we are looking into it.
  • Had to update MyTestEnv to support options kwarg
  • Skip NNI tests because they still use OpenAI Gym
  • Also allow PettingZooEnv in vector environment
  • Updated doc page about ReplayBuffer to also talk about terminated and truncated flags.

Still need to do:

  • Update the Jupyter notebooks in docs
  • Check the entire code base for more dead code (from compatibility stuff)
  • Check the reset functions of all environments/wrappers in code base to make sure they use the options kwarg
  • Someone might want to check test_env_finite.py
  • Is it okay to allow PettingZooEnv in vector environments? Might need to update docs?

@codecov-commenter
Copy link

codecov-commenter commented Jan 5, 2023

Codecov Report

Merging #789 (363250d) into master (6019406) will increase coverage by 0.20%.
The diff coverage is 89.10%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master     #789      +/-   ##
==========================================
+ Coverage   90.89%   91.10%   +0.20%     
==========================================
  Files          73       73              
  Lines        5109     5079      -30     
==========================================
- Hits         4644     4627      -17     
+ Misses        465      452      -13     
Flag Coverage Δ
unittests 91.10% <89.10%> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
tianshou/env/worker/subproc.py 57.54% <60.00%> (+0.24%) ⬆️
tianshou/env/venvs.py 90.34% <80.95%> (-3.82%) ⬇️
tianshou/env/utils.py 81.25% <83.33%> (-2.09%) ⬇️
tianshou/data/collector.py 93.30% <100.00%> (+3.00%) ⬆️
tianshou/env/gym_wrappers.py 97.72% <100.00%> (ø)
tianshou/env/pettingzoo_env.py 90.56% <100.00%> (+2.43%) ⬆️
tianshou/env/venv_wrappers.py 81.81% <100.00%> (+1.49%) ⬆️
tianshou/env/worker/base.py 65.45% <100.00%> (ø)
tianshou/env/worker/dummy.py 85.29% <100.00%> (ø)
tianshou/env/worker/ray.py 83.33% <100.00%> (+10.11%) ⬆️
... and 3 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Trinkle23897
Copy link
Collaborator

Trinkle23897 commented Jan 6, 2023

Hey @Markus28, I'm actually thinking about a non-breaking change. If a user uses gym env, tianshou's VectorEnv itself will first detect the class of the env, and if it's gym env then we adapt shimmy wrapper on top of that. This is an automated process and won't degrade user-experience.

The current changes looks fine for me but it would be nice to add the above feature request!

Also, have you added shimmy usage in docs or readme so that people can know how to convert it to a gymnasium env without seeking other resources for help?

@Markus28
Copy link
Collaborator Author

Markus28 commented Jan 7, 2023

I can do that :)
Currently, I only have a disclaimer in the README, but I can add a section to the docs

@Markus28
Copy link
Collaborator Author

So it turns out that there is unfortunately no "universal" shimmy wrapper that will take any Gym environment and give us an up-to-date Gymnasium environment. Shimmy implements different wrappers for the different API versions. I think we have the following options:

  • Just let the user do the conversion
  • Use different Shimmy wrappers depending on gym.__version__. That could lead to problems for people who are using environments with APIs that don't match their gym version
  • Implement a universal wrapper ourselves

Mark seems to believe that doing a universal wrapper would be quite complicated, but I don't see why it shouldn't be doable. I'm not sure whether it would then be part of Shimmy or Tianshou though.

@Trinkle23897
Copy link
Collaborator

I personally in favor of the second option. This will reduce most of (90%) the issue user hit and will reduce the maintenance effort quite a lot.

Comment on lines +71 to +80
gym_version = packaging.version.parse(old_gym.__version__)
if gym_version >= packaging.version.parse("0.26.0"):
return shimmy.GymV26CompatibilityV0(env=env)
elif gym_version >= packaging.version.parse("0.22.0"):
return shimmy.GymV22CompatibilityV0(env=env)
else:
raise Exception(
f"Found OpenAI Gym version {gym.__version__}. "
f"Tianshou only supports OpenAI Gym environments of version>=0.22.0"
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pseudo-rnd-thoughts Can you confirm that this is how the shimmy wrappers are intended to be used? I.e. for any environment 0.26.0>gym.__version__>=0.22.0 we use GymV22CompatibilityV0" and for gym.__version__ >= 0.26.0 we use GymV26CompatibilityV0?

@Markus28
Copy link
Collaborator Author

Markus28 commented Jan 20, 2023

I implemented the second version. I will still need to update some documentation and the tutorials :) There are currently no tests for OpenAI Gym environments, let me know if I should add some.

@nuance1979
Copy link
Collaborator

I implemented the second version. I will still need to update some documentation and the tutorials :) There are currently no tests for OpenAI Gym environments, let me know if I should add some.

Yes, I think we definitely need a test which imports gym and triggers the use of shimmy. I'd suggest making a copy of test_dqn.py to test_dqn_gym.py then only replace import gymnasium as gym with import gym.

setup.py Show resolved Hide resolved
@nuance1979
Copy link
Collaborator

@Trinkle23897 This PR is failing the doc spelling check but the error message is quite cryptic:

../../../../../../opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/tianshou/env/gym_wrappers.py:docstring of tianshou.env.gym_wrappers.ContinuousToDiscrete.action:4: : Spell check: Args: Args:.

I have no idea how to fix it. Can you take a look?

Other than that, I think this PR is ready to merge.

@Trinkle23897
Copy link
Collaborator

will take a look tonight

@Trinkle23897
Copy link
Collaborator

Trinkle23897 commented Jan 26, 2023

hmm on my local:

>>> gym.__version__, shimmy.__version__, gymnasium.__version__
('0.25.0', '0.2.0', '0.27.1')

$ python test_dqn_gym.py
Traceback (most recent call last):
  File "/Users/jiayi/github/tianshou/test/discrete/test_dqn_gym.py", line 172, in <module> 
    test_dqn(get_args())
  File "/Users/jiayi/github/tianshou/test/discrete/test_dqn_gym.py", line 64, in test_dqn
    train_envs = DummyVectorEnv(
  File "/Users/jiayi/github/tianshou/tianshou/env/venvs.py", line 428, in __init__
    super().__init__(env_fns, DummyEnvWorker, **kwargs)
  File "/Users/jiayi/github/tianshou/tianshou/env/venvs.py", line 148, in __init__
    self.workers = [worker_fn(_patch_env_generator(fn)) for fn in env_fns]
  File "/Users/jiayi/github/tianshou/tianshou/env/venvs.py", line 148, in <listcomp>
    self.workers = [worker_fn(_patch_env_generator(fn)) for fn in env_fns]
  File "/Users/jiayi/github/tianshou/tianshou/env/worker/dummy.py", line 13, in __init__
    self.env = env_fn()
  File "/Users/jiayi/github/tianshou/tianshou/env/venvs.py", line 77, in patched
    return shimmy.GymV22CompatibilityV0(env=env)
  File "/Users/jiayi/.virtualenvs/openai/lib/python3.9/site-packages/shimmy/openai_gym_compatibility.py", line 199, in __init__
    gym_env = _strip_default_wrappers(gym_env)
  File "/Users/jiayi/.virtualenvs/openai/lib/python3.9/site-packages/shimmy/openai_gym_compatibility.py", line 280, in _strip_default_wrappers
    gym.wrappers.render_collection.RenderCollection,
AttributeError: module 'gym.wrappers' has no attribute 'render_collection'

@pseudo-rnd-thoughts
Copy link

You need to update your gym version, but we will update shimmy to avoid this issue

Copy link
Collaborator

@Trinkle23897 Trinkle23897 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave render_collection as-is, will educate user to upgrade the gym version as temporary solution

@Trinkle23897 Trinkle23897 merged commit 6c6c872 into thu-ml:master Feb 3, 2023
@nuance1979 nuance1979 mentioned this pull request Feb 28, 2023
9 tasks
BFAnas pushed a commit to BFAnas/tianshou that referenced this pull request May 5, 2024
Changes:
- Disclaimer in README
- Replaced all occurences of Gym with Gymnasium
- Removed code that is now dead since we no longer need to support the
old step API
- Updated type hints to only allow new step API
- Increased required version of envpool to support Gymnasium
- Increased required version of PettingZoo to support Gymnasium
- Updated `PettingZooEnv` to only use the new step API, removed hack to
also support old API
- I had to add some `# type: ignore` comments, due to new type hinting
in Gymnasium. I'm not that familiar with type hinting but I believe that
the issue is on the Gymnasium side and we are looking into it.
- Had to update `MyTestEnv` to support `options` kwarg
- Skip NNI tests because they still use OpenAI Gym
- Also allow `PettingZooEnv` in vector environment
- Updated doc page about ReplayBuffer to also talk about terminated and
truncated flags.

Still need to do: 
- Update the Jupyter notebooks in docs
- Check the entire code base for more dead code (from compatibility
stuff)
- Check the reset functions of all environments/wrappers in code base to
make sure they use the `options` kwarg
- Someone might want to check test_env_finite.py
- Is it okay to allow `PettingZooEnv` in vector environments? Might need
to update docs?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants