Make fitted estimators picklable after predict (JAX backend)#48
Merged
weihaokong merged 2 commits intoJul 6, 2026
Merged
Conversation
The first predict memoizes nnx.jit-compiled step functions on the estimator instance (_predict_step_compiled_with_cat / _predict_step_compiled_no_cat in _batch_forward). Those closures cannot be pickled, so saving a fitted TabFMClassifier/TabFMRegressor with stdlib pickle crashes with "Can't pickle local object '..._batch_forward.<locals>._predict_step_fn'" once predict has run. That is the AutoGluon / TabArena save path (they pickle the fitted estimator), same motivation as the PyTorch-side fix in google-research#47. Drop the memoized functions from __getstate__ on both estimators: they are pure caches and are rebuilt lazily on the next predict. Restored estimators produce identical predictions.
fus3r
requested review from
abhidas,
erzel,
rajatsen91,
siriuz42,
tamannarayan and
weihaokong
as code owners
July 4, 2026 06:51
weihaokong
approved these changes
Jul 6, 2026
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
Saving a fitted estimator with stdlib pickle crashes on the JAX backend once
predicthas run:The first predict memoizes nnx.jit-compiled step functions on the estimator instance (
_predict_step_compiled_with_cat/_predict_step_compiled_no_cat, see_batch_forward). Those closures cannot be pickled. This is the same save path that motivated #47 on the PyTorch side: AutoGluon / TabArena pickle the fitted estimator, and fit then predict then save is the normal order, so this breaks the JAX backend for that workflow.The fix drops the memoized functions from
__getstate__on both estimators. They are pure caches, guarded byhasattrin_batch_forward, so they are rebuilt lazily on the next predict; a restored estimator produces identical predictions (covered by the new tests).dict(super().__getstate__())is used because sklearn'sBaseEstimator.__getstate__can hand back the live__dict__for estimators outside the sklearn namespace, and pickling should not evict the running object's cache as a side effect.Pickling a fitted estimator before the first predict already worked; only the after-predict state was broken.
Repro on main
Testing
EstimatorPickleTest): fit, predict, pickle round trip, and the restored estimator's predictions match exactly (classifier and regressor).pytest tabfm/src/passes (74 tests, both backends installed).