Skip to content

Add variance calculation from FusedAdam optimizer states #1726

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kwyss-nvidia
Copy link
Collaborator

Description

The state in the Adam optimizer provides moving averages for the first and second moments.
These can be combined to yield a gradient variance for a parameter.

This MR adds a method to the Adam optimizer to calculate a variance for a parameter.

Fixes # (issue)

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

Add

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Comment on lines +362 to +368
"""Return the unscaled state corresponding to the input `param` and `state_name`.

Arguments:
param (torch.nn.Parameter): One of parameters in this optimizer.
state_name (string): Name of optimizer states, can be one of 'exp_avg', 'exp_avg_sq',
and 'master_param`.
"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"""Return the unscaled state corresponding to the input `param` and `state_name`.
Arguments:
param (torch.nn.Parameter): One of parameters in this optimizer.
state_name (string): Name of optimizer states, can be one of 'exp_avg', 'exp_avg_sq',
and 'master_param`.
"""
"""Estimate the gradient variance based on moment estimates."""

Comment on lines +815 to +816
first_moment = optimizer_.get_unscaled_state(param, "exp_avg")
second_moment = optimizer_.get_unscaled_state(param, "exp_avg_sq")
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could make this test more robust by manually computing exp_avg and exp_avg_sq outside of the optimizer. There's some complicated dtype-specific logic within get_unscaled_state, so I don't really trust it.

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.

2 participants