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

Add log_grad_norm hook to LightningModule #7873

Merged
merged 3 commits into from
Jun 8, 2021
Merged

Conversation

carmocca
Copy link
Contributor

@carmocca carmocca commented Jun 7, 2021

What does this PR do?

Part of #7631

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • [n/a] Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

@codecov
Copy link

codecov bot commented Jun 7, 2021

Codecov Report

Merging #7873 (db7864a) into master (ce97676) will decrease coverage by 0%.
The diff coverage is 96%.

@@          Coverage Diff           @@
##           master   #7873   +/-   ##
======================================
- Coverage      93%     92%   -0%     
======================================
  Files         202     202           
  Lines       13123   13114    -9     
======================================
- Hits        12156   12111   -45     
- Misses        967    1003   +36     

Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

great, already integrated in new loop!

@carmocca carmocca force-pushed the refactor/log-grad-norm branch from 607d61c to db7864a Compare June 8, 2021 00:01
@carmocca carmocca enabled auto-merge (squash) June 8, 2021 00:01
@carmocca carmocca added the ready PRs ready to be merged label Jun 8, 2021
@@ -440,6 +440,20 @@ def __sync(
def __check_none(name: str, value: Any, _) -> Any:
raise ValueError(f'`self.log({name}, {value})` was called, but `None` values cannot be logged')

def log_grad_norm(self, grad_norm_dict: Dict[str, torch.Tensor]) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

is adding a new hook the only option here? his feels very strange that this only controls the log behavior, and not the calculation. what's the relationship between this and on_after_bacwkard?

Copy link
Contributor

@tchaton tchaton Jun 8, 2021

Choose a reason for hiding this comment

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

Hey @ananthsub. Users wanted to customise how they aggregate the grad_norm and log them.
Here, they are free to:

  • Change reduce_fx
  • Compute extra values such as standard deviation or total mean norm.
  • Perform custom aggregation.

on_after_bacwkard is independent for this function.

def log_grad_norm(self, grad_norm_dict):
self.log_dict(grad_norm_dict, on_step=False, on_epoch=True, prog_bar=False, logger=True)
"""
self.log_dict(grad_norm_dict, on_step=True, on_epoch=True, prog_bar=True, logger=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

while not BC, is prog_bar=False a better default?

Copy link
Contributor

Choose a reason for hiding this comment

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

@carmocca Any thoughts on this one ? I see value in both.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pros for true:

  • Directly visible
  • Quick experimentation

Cons for true:

  • Usually too cluttered

I think I prefer to have it as True which seems to me like the most convenient option when manually debugging your model

Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

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

LGTM !

@tchaton tchaton mentioned this pull request Jun 8, 2021
13 tasks
@carmocca carmocca merged commit 8cc55eb into master Jun 8, 2021
@carmocca carmocca deleted the refactor/log-grad-norm branch June 8, 2021 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready PRs ready to be merged refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants