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

Conversation

TimotheeMickus
Copy link
Collaborator

closes #18

@TimotheeMickus TimotheeMickus self-assigned this Sep 22, 2023
@TimotheeMickus TimotheeMickus marked this pull request as ready for review September 22, 2023 15:00
Copy link
Collaborator

@Waino Waino left a comment

Choose a reason for hiding this comment

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

I proposed an alternate solution. However, feel free to merge this one if speed is of the essence.

onmt/trainer.py Outdated
@@ -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

Mickus Timothee added 2 commits September 25, 2023 10:42
Copy link
Collaborator

@Waino Waino left a comment

Choose a reason for hiding this comment

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

LGTM

@TimotheeMickus TimotheeMickus merged commit e46eed2 into main Sep 25, 2023
4 checks passed
@TimotheeMickus TimotheeMickus deleted the fix/undo_forwardhook_sideeffects branch September 25, 2023 08:29
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.

Broken gradient in mixed validation settings
2 participants