[Callbacks] FEA new hook for beginning of tasks#33494
[Callbacks] FEA new hook for beginning of tasks#33494jeremiedbb merged 20 commits intoscikit-learn:callbacksfrom
Conversation
ogrisel
left a comment
There was a problem hiding this comment.
Thanks, here is a first pass of feedback.
| assert callback.count_hooks("on_fit_task_begin") == expected_n_tasks | ||
| assert callback.count_hooks("on_fit_task_end") == expected_n_tasks | ||
| assert callback.count_hooks("on_fit_end") == 1 | ||
| assert callback.count_hooks("fit_teardown") == 1 |
There was a problem hiding this comment.
Those are great assertions but I think it would be nice to also have one about the sequence of events with estimator names and task names.
That would make a stronger test but also be helpful for SLEP reviewers to better understand the dynamics induced by the callback API design choices.
There was a problem hiding this comment.
I agree and it's in my todo list :)
but maybe can be done in a separate PR ?
There was a problem hiding this comment.
Cross-referencing our previous discussion about this, starting in #33394 (comment).
| assert callback.count_hooks("on_fit_task_begin") == expected_n_tasks | ||
| assert callback.count_hooks("on_fit_task_end") == expected_n_tasks | ||
| assert callback.count_hooks("on_fit_end") == 1 | ||
| assert callback.count_hooks("fit_teardown") == 1 |
There was a problem hiding this comment.
Cross-referencing our previous discussion about this, starting in #33394 (comment).
|
Following up a discussion we had with @ogrisel and @FrancoisPgm, I pushed a new commit where the protocol is reworked. Related to this #33494 (comment) There's now a base protocol for setup and teardown. It keeps the door open for new protocols for beginning and end of function/predict/transform tasks, while allowing a callback to just implement a minimal protocol for its usage (e.g. _BaseCallback + FunctionCallback for a function only callback). To allow callbacks to do different things during setup/teardown while not having a specific setup and teardown in each protocal, we thought that the |
lesteve
left a comment
There was a problem hiding this comment.
I think this looks good, a few comments below.
| # Only call the `on_fit_task_begin` hook of callbacks that are not | ||
| # propagated. For propagated callbacks, the hook will be called by the | ||
| # sub-estimator's root context (both represent the same task). | ||
| if callback not in getattr(self, "_propagated_callbacks", []): |
There was a problem hiding this comment.
Out of curiosity, what is the reason to change isinstance check to attribute check? Is there some subtlety involved or it for simplicity?
There was a problem hiding this comment.
It is an attr check because we want to detect the callbacks that were propagated from a parent meta-estimator. For auto-propagated callbacks, we still want to call on_fit_task_begin here for the root meta-estimator.
|
I merged #33522 into this branch to showcase how the new hooks work with the required changes from #33522. Basically we need to come back to a setup during context initialization in fit and set the context as an attribute to be able to have the teardown done in the finally block of the decorator. We should therefore merge #33522 before this one |
ogrisel
left a comment
There was a problem hiding this comment.
I gave it another pass and it looks good to me as well.
FrancoisPgm
left a comment
There was a problem hiding this comment.
A few super minor nitpicks and some docstrings to update but oterwise LGTM thx :)
|
Thanks for the reviews. I'm merging this one as well with 2 approvals to be able to update the SLEP. |
As discussed during the last meeting, this PR proposes an different version of the callback protocol with 4 callbacks
Previoulsy
on_fit_beginused to act as a setup and begin of the root task, andon_fit_endused to act as a teardown and end of the root task.Now all nodes of the context tree call the same hook at the end the task, including the root. In addition, they all call a same hook at the beginning of the task.
Side effect: setup and tear down don't need to be tied to the callback context, so we can make the decorator deal with setup and teardown it self and we don't need to store the context as an attribute of the estimator anymore which is nice.
ping @ogrisel @FrancoisPgm @StefanieSenger @lesteve