-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new LightningModule hook "configure_callbacks" #5621
Conversation
+1 for |
This was requested internally, see grid chat. The motivation is to have model-specific callbacks defined in the model, making it more portable. |
@awaelchli what are the implications for checkpoint loading/saving? how are callback states treated here vs callbacks attached to the trainer? Asking since it came up in #5542 |
@ananthsub The callbacks attached through this hook here will still go into the trainer as if they were added directly to the Trainer. |
@PyTorchLightning/core-contributors would you find such a hook to configure model-specific callbacks directly in the LightningModule useful and if so which API would you prefer (3 choices above)? |
option 1 looks better to me since trainer callbacks can be configured/modified using
what's grid chat?? |
I agree with @akihironitta |
While I would personally prefer 2 for maximum flexibility, I agree with Akihiro that it could be unintuitive for users. |
I think we can start with 1, and we can always add 2 later if it becomes heavily requested |
@akihironitta @teddykoker @ananthsub @rohitgr7 @s-rog |
could it be a mix of the first two such that there will be an extra argument that would specify if the model CB is added as first or as last? ideally you would like to define some kind of the insert but it is maybe overkilled atm 🔢 |
I'd agree with option 1, with 2 being the future requirement for callbacks that interfere with each other! |
trainer_callbacks=[LearningRateMonitor(), | ||
EarlyStopping(), | ||
LearningRateMonitor(), | ||
EarlyStopping()], | ||
model_callbacks=[early_stopping, lr_monitor], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akihironitta @Borda why does yapf format it like this?
is it a bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that looks strange... :/
trainer_callbacks=[ | ||
LearningRateMonitor(), progress_bar, | ||
EarlyStopping(), | ||
LearningRateMonitor(), | ||
EarlyStopping() | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, for some reason there are two elements on one line and the rest on the others. I thought it is supposed to give consistent formatting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this looks strange...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM !
What does this PR do?
Fixes #5615
Introduces a
configure_callbacks
hook in LightningModulePossible implementations:
Option 1: "appending" (CURRENT IMPLEMENTATION ON THIS PR)
Simply appens the returned callbacks. Existing callbacks cannot be modified directly.
Option 2: "replacement"
Allows to define the order and to modify existing callbacks.
Option 3: "inplace modification"
Allows to define the order and to modify existing callbacks.
Options 2 and 3 give the user more control over how callbacks are used and in which order they are executed.
However, if the LightningModule modifies the the callbacks passed to the Trainer, it is less transparent what code actually gets executed. On the other hand, a user can mutate the callbacks list in the trainer anyway through
Trainer.callbacks
, if they want to.TODOs:
Poll options