Skip to content

Clear extra_jax_mappings before pickling#1263

Closed
singhharsh1708 wants to merge 1 commit into
astroautomata:masterfrom
singhharsh1708:fix-extra-jax-mappings-pickle
Closed

Clear extra_jax_mappings before pickling#1263
singhharsh1708 wants to merge 1 commit into
astroautomata:masterfrom
singhharsh1708:fix-extra-jax-mappings-pickle

Conversation

@singhharsh1708

Copy link
Copy Markdown

Summary

PySRRegressor.__getstate__ clears extra_sympy_mappings and extra_torch_mappings before pickling, since both hold live callables that generally can't be pickled (e.g. lambdas, locally-defined functions). extra_jax_mappings has 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()'s pkl.dump(self, f) fails. That failure is caught and logged at debug level 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 via PySRRegressor.from_file() then fails with a confusing EOFError: Ran out of input, with no indication of the real cause.

This:

  • adds extra_jax_mappings to the list of state keys cleared in __getstate__, matching extra_sympy_mappings/extra_torch_mappings
  • surfaces checkpoint failures via pysr_logger.warning(...) instead of debug(...), so a genuine pickling failure isn't silent

Fixes #1198

Test plan

  • Added test_getstate_clears_extra_jax_mappings: a model with an unpicklable lambda in extra_jax_mappings fails to pickle the raw dict directly, but __getstate__()'s output pickles fine and has extra_jax_mappings set to None
  • pysr/test/test_main.py -k "getstate_clears_extra_jax_mappings or pickle" passes (3/3)

__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
@MilesCranmer

Copy link
Copy Markdown
Member

Dupe of #1199

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants