-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
Fix RandomNumberGenerator pickling #2639
Conversation
Can you add some tests to check that this will indeed work? |
Test added 342a0a2. Thanks for pointing it out. |
@jjyyxx can you please fix tests? |
I pushed a fix. |
@jjyyxx tests still aren't passing |
My bad. Didn't set up pre-commit check. Now I hope everything is ok. |
I don't think this actually solves the issue though? Please add a test that pickles an actual space (a Discrete and a Box - at least two tests) and then loads it in the way that causes the bug reported here. So:
With the current code, I'm still getting the same exception |
@RedTachyon Thanks for your feedback. Based on your suggestion, I write a test case like this: from gym.spaces import Box, Discrete
import tempfile
@pytest.mark.parametrize("space", [
Discrete(100),
Box(low=0.0, high=1.0, shape=(2, 2)),
])
def test_space_pickle(space):
space.sample() # To initialize the RNG, or access space.np_random
# Pickle and unpickle with a string
pickled = pickle.dumps(space)
space2 = pickle.loads(pickled)
# Pickle and unpickle with a file
with tempfile.TemporaryFile() as f:
pickle.dump(space, f)
f.seek(0)
space3 = pickle.load(f)
value = space.sample()
value2 = space2.sample()
value3 = space3.sample()
assert np.array_equal(value, value2)
assert np.array_equal(value, value3) And my fix could pass this test. But as a first-time contributor, I'm not confident where to put this test, e.g.
So I did not commit this test case yet. Besides, I'm not quite sure how to reproduce the last sentence you mentioned:
The only pitfall I found was you must |
Please put it in the second location |
I might have messed something up with my test, because now I'm rerunning the same code and works. My main concern was for the space pickle-unpickle cycle to not crash, and this seems to work now, so imo this can be merged and then we'll see if SB3 is happy |
I met this issue when working with RLlib. In my test, it works after applying this fix. |
I ran the previously failing SB3 tests again and now they pass, so this seems to fix it. |
see openai/gym#2639 for more details
Reason
gym 0.22.0 defines a
RandomNumberGenerator
class inheritingnp.random.Generator
.np.random.Generator
defines__reduce__
, but it's hard-coded to return aGenerator
instead of its subclassRandomNumberGenerator
. Thus, sampling from a Space will be broken after pickling and unpickling, due to using the deprecated methods defined inRandomNumberGenerator
. (Which further breaks some RL frameworks like RLlib relying on pickling for distributed training)Example
Before
After