Skip to content
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

undo forward-hook side-effect #19

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions onmt/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ def validate(self, valid_iter, moving_average=None, task=None):
# Set model back to training mode.
valid_model.train()

# the forward hook `has_grad` was triggered, so we manually unset the flags to not fool the optim
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should work, as long as validation is never performed in the middle of an accumulation (a very reasonable assumption).

However, I think a better solution would be to modify the hook: have it check that module.training is set. If unset, then return without doing anything.

Copy link
Collaborator Author

@TimotheeMickus TimotheeMickus Sep 25, 2023

Choose a reason for hiding this comment

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

the efficient thing would be to set param.has_grad = self.training, but that's a case of clarity vs. efficiency.
Maybe a big fat explanatory comment would suffice

for p in self.model.parameters():
if hasattr(p, 'has_grad'):
p.has_grad = False

return stats

def _gradient_accumulation_over_lang_pairs(
Expand Down
Loading