Clear extra_jax_mappings before pickling#1263
Closed
singhharsh1708 wants to merge 1 commit into
Closed
Conversation
__getstate__ already cleared extra_sympy_mappings and extra_torch_mappings (both hold live callables), but not extra_jax_mappings, even though it's the same shape (dict[Callable, str]). A model using a custom function there would silently fail to checkpoint: _checkpoint swallows the pickling exception into a debug-only log, leaving an empty/truncated pickle file that later fails with a confusing EOFError on from_file(). Also surface checkpoint failures as a warning instead of a debug-only log line, since they're otherwise invisible. Fixes astroautomata#1198
Member
|
Dupe of #1199 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PySRRegressor.__getstate__clearsextra_sympy_mappingsandextra_torch_mappingsbefore pickling, since both hold live callables that generally can't be pickled (e.g. lambdas, locally-defined functions).extra_jax_mappingshas the exact same shape (dict[Callable, str]) but was missing from that list.When a user passes a custom function there (the documented/intended use case — see
extra_jax_mappings : dict[Callable, str]in the docstring),_checkpoint()'spkl.dump(self, f)fails. That failure is caught and logged atdebuglevel only, so by default it's invisible — and since the file was already opened in"wb"mode (truncating it first), the result is an empty/truncated pickle file. Loading it later viaPySRRegressor.from_file()then fails with a confusingEOFError: Ran out of input, with no indication of the real cause.This:
extra_jax_mappingsto the list of state keys cleared in__getstate__, matchingextra_sympy_mappings/extra_torch_mappingspysr_logger.warning(...)instead ofdebug(...), so a genuine pickling failure isn't silentFixes #1198
Test plan
test_getstate_clears_extra_jax_mappings: a model with an unpicklable lambda inextra_jax_mappingsfails to pickle the raw dict directly, but__getstate__()'s output pickles fine and hasextra_jax_mappingsset toNonepysr/test/test_main.py -k "getstate_clears_extra_jax_mappings or pickle"passes (3/3)