Repository navigation
Conversation
Nested fits keep their arrays in the namespace and device of X, so the enclosing fit can keep using them. partial_fit moves its running state to X before updating it. StandardScaler is converted as the first incremental estimator, and a new common check covers partial_fit and warm_start with array_api_strict on a non-default device. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
|
|
||
|
|
||
| class _CalibratedClassifier: | ||
| class _CalibratedClassifier(BaseEstimator): |
There was a problem hiding this comment.
Note for reviewer: _CalibratedClassifier holds the classifiers fitted in CalibratedClassifierCV.fit. As a plain class, it stopped the walk in _fitted_attrs_as_numpy (which only follows objects with get_params), so with array API inputs their coef_ stayed in the namespace of X. move_estimator_to had the same blind spot: it left them as NumPy, and the moved copy shared them with the original.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
cf62afe to
66eb066
Compare
|
I think this PR is a pretty good solution to avoid the regression |
❌ Linting issuesThere was an issue running the linter job. Please update with |
Reference Issues/PRs
Proposed solution for the first point of my comment on scikit-learn#35053 (this PR targets its branch). Context in scikit-learn#34604.
What does this implement/fix? Explain your changes.
In scikit-learn#35053, attributes are converted to NumPy each time a
_fit_context-decorated fit returns, including nested ones. This breaks code that reads fitted state back during a fit:X. For instanceCalibratedClassifierCV(LogisticRegression(), method="temperature", ensemble=False).fit(X_torch, y_torch)raisesMultiple namespaces for array inputs.partial_fitandwarm_startread back state that is now NumPy, whileXis not.This PR:
_fit_contextmarks the fit as running in a thread-local, whichsklearn.utils.parallelpropagates to joblib workers (same as the config). The conversion then walks the sub-estimators once, each one converted according to its own opt-in. Constructor parameters are left untouched.partial_fitto the namespace and device ofXbefore updating it. I convertedStandardScaleras an example: the state is(n_features,)so the extra transfers per batch are negligible compared to the batch itself.check_array_api_incremental_fit: twopartial_fitcalls, or awarm_start=Truerefit, witharray_api_stricton"device1". Forgetting to move the state then raises, while torch on CPU would silently accept some of these mixes.I used a thread-local rather than a config key: users have no reason to set this flag, and
config_contextis public and checked by numpydoc.Not covered here: calling
cross_val_predictdirectly on an estimator fitted on torch still fails, because_enforce_prediction_ordergets NumPyclasses_and torch predictions. That needs a fix in the code that readsclasses_, which I think is a separate discussion.MinMaxScalerandGaussianNBare not converted yet, but the new check already runs on them.AI usage disclosure
I used AI assistance for:
Any other comments?
I checked that the new tests catch what they are meant to catch: they fail if
partial_fitdoesn't move its state, if every fit converts (old behavior), or if the flag isn't propagated to joblib workers.Array API common checks: 771 passed, 23 xfailed, 9 xpassed (
StandardScalerandFeatureUnion([StandardScaler()])no longer need the cross-namespace xfail).🤖 Generated with Claude Code