Skip to content

BUG: Ensure seed sequences are restored through pickling - #26260

Merged
seberg merged 6 commits into
numpy:mainfrom
bashtage:pickle-generator-seed-seq
Apr 16, 2024
Merged

seberg merged 6 commits into
numpy:mainfrom
bashtage:pickle-generator-seed-seq

Conversation

@bashtage

Copy link
Copy Markdown
Contributor

Explicity store and restore seed sequence

closes #26234

@bashtage

Copy link
Copy Markdown
Contributor Author

The biggest concern I have with this approach is the requirement that the seed sequence uses the 3-argument version of __reduce__. This choice rules out some of the accepted behaviors of __reduce__ . Not a huge limitation, but could affect someone trying to write a custom class that inherits from ISeedSequence.

@bashtage
bashtage force-pushed the pickle-generator-seed-seq branch from f039e85 to 3ca64f5 Compare April 11, 2024 11:36
@bashtage

Copy link
Copy Markdown
Contributor Author

Should probably have a test using a file pickle generated from 1.26.x.

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(submodules are in a bad state unfortunately, although maybe you fixed it by the time I comment :))

Thanks! Preserving unpicking of old files is easy enough, yeah. If we also care about unpickling of new ones on old versions, we would need to try my solution, I think.

Breaking loading 2.0 files in 1.x should be OK, though. I am a bit unsure of backporting into a bug-fix 1.26.x. But maybe just as well.

Comment thread numpy/random/_pickle.py Outdated
# Unpickle the seed sequence
ss_ctor, ss_ctor_args, ss_state = seed_seq_info
seed_seq = ss_ctor(*ss_ctor_args)
seed_seq.__setstate__(ss_state)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should remove this here, for simplification. Which you can do if you just use self._seed_seq in the __reduce__ below (rather than seed_seq.__reduce__()).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I simplified this by removing the unnecessary call to reduce

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for having a look! LGTM, modulo the print and the tests seem good.
As I said, not sure I would like to backport this beyond 2.0, but I think backport into 2.0 would be good.

I'll leave it a bit in case someone has a comment, but basically, I think it is good modulo the print and rebasing away the submodule changes.

Comment thread numpy/random/_pickle.py Resolved
Comment thread numpy/random/_pickle.py Resolved
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Apr 11, 2024
@charris

charris commented Apr 11, 2024

Copy link
Copy Markdown
Member

I do plan on one more release of 1.26 if the release still builds. But this bug has been around since 1.17, so maybe not so important to fix.

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good for backporting to 2.0, want to wait a bit in case someone wants to make a comment or you agree that trying to simplify a bit more is worthwhile.

But, I am happy if we put it in as is, I think it is a bug fix and also an improvement, plus we got new tets.

Comment thread numpy/random/_generator.pyx Outdated
# Pickling support:
def __getstate__(self):
return self.bit_generator.state
return self.bit_generator.__getstate__()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should be good, so don't think we need to iterate more (it is nicer than before!). But thought I would comment that I would think we can just return the bit_generator here (which then gets pickled), so that Generator need not know that the bit generator even has a getstate/state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've removed this, simplified a bit more, and made sure it work with legacy pickles (<2 and <1.22, which had different __reduce__ implementations).

I think it is ready now (subject to green). Only question remaining is whether to go ahead and return type(self) instead of self.__class__.___name__?

@bashtage
bashtage force-pushed the pickle-generator-seed-seq branch from e1d0cb2 to f9cb51d Compare April 12, 2024 11:51
@bashtage

Copy link
Copy Markdown
Contributor Author

When ready to merge, I should also probably squash some or all of the commits.

@bashtage
bashtage force-pushed the pickle-generator-seed-seq branch 3 times, most recently from 550b8b8 to 3aba7a1 Compare April 12, 2024 15:31
@bashtage

Copy link
Copy Markdown
Contributor Author

Grrr mypy strict typing...

@bashtage
bashtage force-pushed the pickle-generator-seed-seq branch from b005ffa to 3a02920 Compare April 14, 2024 09:14
Explicity store and restore seed sequence

closes numpy#26234
Make more use of set and getstate to avoid changes in the pickling functions
Add test for legacy pickles
Include pickles for tests
@bashtage
bashtage force-pushed the pickle-generator-seed-seq branch 3 times, most recently from bc2d926 to 81531b6 Compare April 14, 2024 10:57
@bashtage
bashtage force-pushed the pickle-generator-seed-seq branch from 81531b6 to 0289bec Compare April 14, 2024 11:08
@bashtage
bashtage force-pushed the pickle-generator-seed-seq branch from 5aa2747 to fc57d77 Compare April 15, 2024 09:19
Explicitly return bit generator rather than ctor
@bashtage
bashtage force-pushed the pickle-generator-seed-seq branch from fc57d77 to 6cd6520 Compare April 15, 2024 09:34
@bashtage

Copy link
Copy Markdown
Contributor Author

@seberg I think this has gone as far as possible.

@seberg

seberg commented Apr 16, 2024

Copy link
Copy Markdown
Member

Thanks @bashtage looks good to me, let's put this in. It was certainly a longer journey than anticipate :), but the added tests look nice.

@seberg
seberg merged commit d567754 into numpy:main Apr 16, 2024
charris pushed a commit to charris/numpy that referenced this pull request Apr 16, 2024
Explicity store and restore seed sequence

closes numpy#26234

---

* BUG: Ensure seed sequences are restored through pickling

Explicity store and restore seed sequence

closes numpy#26234

* CLN: Simplify refactor

Make more use of set and getstate to avoid changes in the pickling functions

* BUG: Correct behavior for legacy pickles

Add test for legacy pickles
Include pickles for tests

* MAINT: Correct types for pickle related functions

* REF: Switch from string to type

* REF: Swtich to returning bit generators

Explicitly return bit generator rather than ctor
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Apr 16, 2024
charris added a commit that referenced this pull request Apr 17, 2024
BUG: Ensure seed sequences are restored through pickling (#26260)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: BitGenerator.seed_seq doesn't round-trip through pickle

3 participants