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

Add pickle tests #53

Merged
merged 34 commits into from
Mar 31, 2023

Conversation

elliottower
Copy link
Contributor

@elliottower elliottower commented Mar 28, 2023

Description

This PR adds tests to ensure that each environment type can be saved and loaded via pickling. It adds EzPickle support to those environments which cannot be pickled, such as OpenSpiel.

Note: I made this as a fork of the previous PR adding dockerfile/CI support, could revert the commit and make a new branch but figured this was fine as that was merged.

Fixes # (issue), Depends on # (pull request)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Screenshots

Please attach before and after screenshots of the change if applicable.
To upload images to a PR -- simply drag and drop or copy paste.

Checklist:

  • I have run the pre-commit checks with pre-commit run --all-files (see CONTRIBUTING.md instructions to set it up)
  • I have run pytest -v and no errors are present.
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I solved any possible warnings that pytest -v has generated that are related to my code to the best of my knowledge.
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@elliottower elliottower marked this pull request as ready for review March 29, 2023 20:39
Copy link
Member

@pseudo-rnd-thoughts pseudo-rnd-thoughts left a comment

Choose a reason for hiding this comment

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

Thanks Elliot for doing this, looks impressive as far

# Without EzPickle:_register_bsuite_envs.<locals>._make_bsuite_env cannot be pickled
# With EzPickle: maximum recursion limit reached
FAILING_PICKLE_ENVS = [
"bsuite/bandit_noise-v0",

Choose a reason for hiding this comment

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

I think I have found this before. It is generally because one of the classes implemented __getattr__.
Is there a particular parameter that is missing?

I suspect the issue is that the environment only defines a variable on reset or another function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’ll go check that individual env and see. But what would we be able to do to fix it, besides submitting a PR to their repo? I guess we could in the compatibility wrapper specifically check if it’s that env or if an env has that specific variable not defined in init, and then do whatever modifications are required?

Copy link
Member

@pseudo-rnd-thoughts pseudo-rnd-thoughts Mar 30, 2023

Choose a reason for hiding this comment

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

In short, we can't fix it. The environments failing use a wrapper that contains

def __getattr__(self, attr):
    return getattr(self._env, attr)

The problem exists when _env doesn't exist in the wrapper, i.e., when a staticmethod is existed (__setstate__) then this causes an infinite loop to occur of __getattr__("static_method") -> __getattr__("_env") -> __getattr__("_env") -> ad infinitum
The second issue is that dm don't seem to be maintaining the project anymore.

The solution is simple

def __getattr__(self, attr):
    if "_env" in self.__dict__:
          return getattr(self._env, attr)
    else:
          return super().__getattribute__(attr)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you put a PR up by any chance? Even if they don't' end up merging it I feel like we might as well try, you seem to understand this stuff better than me though, I'm not sure I'd be able to explain it well or respond to any questions about it.

Choose a reason for hiding this comment

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

@@ -103,6 +104,36 @@ def test_seeding(env_id):
env_2.close()


@pytest.mark.skip(
reason="Fatal Python error: Segmentation fault (with or without EzPickle"

Choose a reason for hiding this comment

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

Do you know the source of this? Mujoco is the probable cause which we can probably get solved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like it's to do with pluggy?

/Users/elliottower/anaconda3/envs/shimmy/bin/python /Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/_jb_pytest_runner.py --target test_dm_control.py::test_pickle 
Testing started at 1:17 PM ...
Launching pytest with arguments test_dm_control.py::test_pickle --no-header --no-summary -q in /Users/elliottower/Documents/GitHub/Shimmy/tests

============================= test session starts ==============================
collecting ... collected 85 items

test_dm_control.py::test_pickle[dm_control/acrobot-swingup-v0] Fatal Python error: Segmentation fault

Current thread 0x000000011149c600 (most recent call first):
  File "/Users/elliottower/Documents/GitHub/Shimmy/tests/test_dm_control.py", line 114 in test_pickle
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/python.py", line 192 in pytest_pyfunc_call
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/python.py", line 1761 in runtest
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/runner.py", line 166 in pytest_runtest_call
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/runner.py", line 259 in <lambda>
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/runner.py", line 338 in from_call
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/runner.py", line 258 in call_runtest_hook
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/runner.py", line 219 in call_and_report
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/runner.py", line 130 in runtestprotocol
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/runner.py", line 111 in pytest_runtest_protocol
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/main.py", line 347 in pytest_runtestloop
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/main.py", line 322 in _main
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/main.py", line 268 in wrap_session
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/main.py", line 315 in pytest_cmdline_main
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_callers.py", line 39 in _multicall
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_manager.py", line 80 in _hookexec
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/pluggy/_hooks.py", line 265 in __call__
  File "/Users/elliottower/anaconda3/envs/shimmy/lib/python3.9/site-packages/_pytest/config/__init__.py", line 164 in main
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/_jb_pytest_runner.py", line 51 in <module>

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

tests/test_dm_control_multi_agent.py Outdated Show resolved Hide resolved
tests/test_dm_lab.py Show resolved Hide resolved
tests/test_dm_lab.py Outdated Show resolved Hide resolved
@elliottower elliottower mentioned this pull request Mar 30, 2023
7 tasks
@elliottower
Copy link
Contributor Author

elliottower commented Mar 30, 2023

@pseudo-rnd-thoughts any idea about these? It looks like DM lab seeding doesn't work, and there was some issue with pickling: https://github.com/Farama-Foundation/Shimmy/actions/runs/4567097924/jobs/8060464686

Also I've run the pre-commit hooks locally and it passed previously but now for some reason the CI pre-commit hooks are failing with the meltingpot compatibility file and tests? If you look up a few runs it was passing but I don't think I changed the file. I uninstalled pre-commit and re-installed it and it didn't change anything. Tried reverting to the original changes but it now won't let me commit without it re-formatting things. Not sure if this issue has happened before?

Edit: got it working by commenting out isort on the file, not ideal but the problem persisted even after removing and reinstalling pre-commit hooks.

@elliottower
Copy link
Contributor Author

About the seeding for DM lab, it seems it is possible (link), but our tests here are returning different observations and failing the seed test. I commented out the observations and it still failed with other errors. I think the problem then must be that we are not properly seeding it in the wrapper.

@elliottower
Copy link
Contributor Author

Checked over my own code to ensure there weren't any obvious typos or mistakes, tests are passing now so it should be in a good state to merge at least from what I can see.

@@ -48,6 +48,10 @@ def reset(
self._env.reset(seed=seed)

Choose a reason for hiding this comment

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

I thought we had solved the seeding issue in dm-lab @jjshoots

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure but I think this should be a separate PR with the other dm-lab fixes, this is just for adding pickle tests and turns out the pickle tests for dm lab don't seem to work and are blocked so can just update the seed/pickle stuff for dm-lab later

tests/test_meltingpot.py Outdated Show resolved Hide resolved
tests/test_dm_lab.py Outdated Show resolved Hide resolved
# Without EzPickle:_register_bsuite_envs.<locals>._make_bsuite_env cannot be pickled
# With EzPickle: maximum recursion limit reached
FAILING_PICKLE_ENVS = [
"bsuite/bandit_noise-v0",

Choose a reason for hiding this comment

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

shimmy/meltingpot_compatibility.py Show resolved Hide resolved
@pseudo-rnd-thoughts
Copy link
Member

Thanks elliot, I agree, just a couple of points about pytest.importorskip then we can merge and make a release

@pseudo-rnd-thoughts pseudo-rnd-thoughts merged commit 5dbaa9a into Farama-Foundation:main Mar 31, 2023
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.

2 participants