Skip to content

Automatically add lightning hooks to CellariumModule as passthroughs#271

Open
sjfleming wants to merge 6 commits into
mainfrom
sf-all-lightning-hooks
Open

Automatically add lightning hooks to CellariumModule as passthroughs#271
sjfleming wants to merge 6 commits into
mainfrom
sf-all-lightning-hooks

Conversation

@sjfleming

@sjfleming sjfleming commented Nov 26, 2024

Copy link
Copy Markdown
Contributor

The idea here is to add a whole bunch of lightning hooks to CellariumModule so that CellariumModule can pass them all through to CellariumModel. Different CellariumModels may want to use different hooks.

Closes #226

@sjfleming
sjfleming marked this pull request as ready for review November 27, 2024 17:32
@sjfleming
sjfleming requested a review from ordabayevy November 27, 2024 17:32
def delegate_method(self, *args, **kwargs):
method = getattr(self.model, method_name, None)
if callable(method):
method(self.trainer)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass *args and **kwargs here as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I did try this initially:

method(self.trainer, *args, **kwargs)

but I got unexpected test failures...
then I realized that the explicitly implemented ones only used the self.trainer arg.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be passing args and kwargs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit I did not understand the test failures. It seemed like something funny was happening during "predict". I gave up on args and kwargs for that reason, thinking it must be wrong. But I never understood the test failure.

# >>> explicitly_implemented_hooks = ["on_train_epoch_start", "on_train_epoch_end"]
# >>> omit_hooks = ["on_after_batch_transfer", "on_before_batch_transfer", "on_gpu"]
# >>> passthrough_hooks = [hook for hook in all_hooks if hook not in (explicitly_implemented_hooks + omit_hooks)]
passthrough_hooks = [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice this was automated like above and robust. For example automatically adding explicitly implemented hooks as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was kind of thinking that as well, but I'm not sure. On one hand, it's nice if it's automatic, but on the other hand, the code is a bit easier to read -- easier for developers to see what is actually implemented -- if it's explicit.

What do you think?

if callable(on_train_start):
on_train_start(self.trainer)
on_train_epoch_start = getattr(self.model, "on_train_epoch_start", None)
if callable(on_train_epoch_start):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to somehow call the delegate_method here so if there are any changes in it this wouldn't need any changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if I understand this point. I was thinking that we could not re-implement the explicitly implemented methods (like on_train_epoch_start) because the passthrough/delegate would end up having the same name and they would conflict (I don't know how python handles that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement "on_predict_epoch_end" in CellariumModule

2 participants