Repository navigation
ENH Make callbacks metadata routers - #34137
Conversation
…nd val in scoring monitor
…kit-learn into callback_metadatarouting
|
|
||
|
|
||
| @skip_callback_test_if_wasm | ||
| def test_search_callbacks_receive_sample_weight(): |
There was a problem hiding this comment.
This test was added when sample_weight was manually passed to all callbacks (if present in the fit_params). Now sample_weight can be requested by a callback when metadata routing is enabled. But when it is not enabled, the automatic forwarding of sample_weight to the hooks will not work, as the requested params of callback hooks are not present in the hooks' signatures.
There was a problem hiding this comment.
EDIT: Actually sample_weight in callbacks should also work with metadata routing disabled, so now the code paths when _routing_enabled() is False forward sample_weight to the callback hooks. So the test mentioned in the previous comment has been reinstated, adapted to the current version. Similar tests for LogisticRegression, StandardScaler and MaxiterEstimator have been added.
…kit-learn into callback_metadatarouting
…kit-learn into callback_metadatarouting
Co-authored-by: Jérémie du Boisberranger <[email protected]>
|
The |
jeremiedbb
left a comment
There was a problem hiding this comment.
LGTM !
I did a final pass and pushed a series of new tests, fixes, doc improvements... Almost everything looks good so I'm approving the PR and I think we can merge as is. There's still a major issue that I found during my last round of review that I'll explain below but I think that we can decide to deal with it later.
Routing does not work for auto propagated callbacks: An auto propagated callback requesting some metadata and registered on a meta estimator does not recieve that metadata by the hook calls from the sub-estimator.
The reason is that the whole routing is built statically at the beginning of fit of the meta-estimator. At that time, the sub-estimator doesn't have the propagated callback yet, so the metadata is not routed to it.
The main issue is the fundamental difference between metadata routing that is completely static and callbacks that are dynamic. There are ways to still be able to route the metadata to sub-estimators, no that complicated, but I think it's acceptable to implement that in a follow up PR. Indeed the only callback requesting metadata so far is not auto-propagated.
|
@ogrisel @jeremiedbb do you think this can get merged today ? Otherwise it won't be included in the release. |
|
Great, congratulations everyone! 🎉 |
## Summary Fixes the 10 failures in the nightly-dependency wheel tests. - Keeps the upstream inference-state check active by recording opaque GPU objects by identity in its per-attribute diagnostics. The whole-estimator comparison and production serialization stay unchanged. scikit-learn/scikit-learn#34729 - Routes extra `LogisticRegression.fit` keywords to scikit-learn for callback metadata routing and validation. Fits that only use the existing arguments stay eligible for GPU execution. - Tests metadata consumption through the public API for both `MetadataRequest` and `MetadataRouter`, while retaining the exact request checks on older versions. scikit-learn/scikit-learn#34137 ## Validation Rebuilt cuML locally and ran `test_core.py` and `test_estimator_proxy.py` on an RTX PRO 6000: 392 passed on scikit-learn `1.10.dev0`, and 388 passed on `1.9.1`. Closes #8976
Reference Issues/PRs
Potential fix for #34068
What does this implement/fix? Explain your changes.
This makes callbacks and callback-compatible estimators metadata routers.
FitCallbackis now a base class that inherits from_MetadataRequester, so a callback can request metadata withset_on_fit_task_begin_requestandset_on_fit_task_end_request. The routed values are passed to the hook as keyword arguments. Each requested name must be a keyword-only parameter of the hook.The main usecase for the routing at the moment is routing metadata to
ScoringMonitorscorers, includingsample_weight, and routingX_val/y_valso a scorer can run on validation data.ScoringMonitornow uses two scorers,scoring_trainandscoring_val. Train and validation scores are stored separately onScoringMonitorLog(train_scores/val_scores, and the corresponding*_as_pandasproperties). Validation scoring requires metadata routing.An estimator that is not a metadata router can still be used with callbacks, but with limited functionalities.
When using folds on
X, in_fit_and_score, callback parameters that are sample aligned withXare sliced with the training indices.X_valandy_valare always left unsliced since they cannot correspond to the same samples as X.Routing when metadata routing is enabled
Metadata is processed twice before a hook sees it.
ScoringMonitoradds a third processing, from the hook down to its scorers.Estimator → callback list. The estimator now holds callbacks in a
MultiCallbackobject rather than a list. When it callsprocess_routing,fitgets mapped toon_fit_task_beginandon_fit_task_end, producing a bunch of the form :routed_params = {"callback": {"on_fit_task_begin": ..., "on_fit_task_end":...}}that is shared by every callback. The estimator then callscall_on_fit_task_*withmetadata=routed_params.callback.on_fit_task_*.Callback list → one callback.
call_on_fit_task_*callsprocess_routingagain, on theMultiCallback. Its children arecallback_0,callback_1, and so on, inset_callbacksorder. So it produces a bunch of the form :metadata = {"callback_0": {"on_fit_task_*": ...}, "callback_1": {"on_fit_task_*": ...}}.Then
_call_hooksreadsmetadata["callback_i"][hook_name]and passes those keys to callbacki.ScoringMonitor→ scorers.ScoringMonitor.get_metadata_routingrequestsX_valandy_valonon_fit_task_endwhen a validation scorer is set, and routes the remaining score parameters toscorer_train.scoreandscorer_val.score.Routing when metadata routing is disabled
Only
sample_weightis forwarded, and only to hooks for which the callback overrides_accept_sample_weightto returnTrue. The same two bunchs are built by hand:_get_manual_callback_paramsbuilds the first mapping to each hook, if any callback acceptssample_weighton that hook._get_manual_routing_paramsturns that mapping intocallback_ientries for the callbacks that accept it._call_hooksthen uses the same keys as in the routing-enabled path.Any other comments?
Here's a typical example use case that this PR enables: