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

[fix] Check None value for Pytorch model gradients histogram #1917

Merged
merged 8 commits into from
Jul 5, 2022
Merged

[fix] Check None value for Pytorch model gradients histogram #1917

merged 8 commits into from
Jul 5, 2022

Conversation

kage08
Copy link
Contributor

@kage08 kage08 commented Jun 27, 2022

When tracking histograms of gradients, some tensors may not have gradients during specific training loops.
So check if grad is not None before converting to numpy.

This commit fixes #1925

@CLAassistant
Copy link

CLAassistant commented Jun 27, 2022

CLA assistant check
All committers have signed the CLA.

@gorarakelyan
Copy link
Contributor

@kage08 thanks a lot for the contribution! 🙌
@alberttorosyan could you please take a look at this?

@SGevorg
Copy link
Member

SGevorg commented Jun 28, 2022

@kage08 thanks a lot for the contribution. 🙏
Could you open a corresponding issue (with some usage context) and connect to this PR?
We use issues to track the changes and by adding to milestones to which version the changes got shipped with 🙌

@alberttorosyan alberttorosyan changed the title Pytorch: Check if grad is not None [fix] Check None value for Pytorch model gradients histogram Jun 28, 2022
Copy link
Member

@alberttorosyan alberttorosyan left a comment

Choose a reason for hiding this comment

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

@kage08 thanks for contribution 🙌
may I ask you to update the CHANGELOG.md to include the fix in Unreleased section?
also I've changed the PR header to match the naming conventions.

aim/sdk/adapters/pytorch.py Outdated Show resolved Hide resolved
aim/sdk/adapters/pytorch.py Outdated Show resolved Hide resolved
Copy link
Member

@alberttorosyan alberttorosyan left a comment

Choose a reason for hiding this comment

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

looks good!
the remaining steps:

  1. open a GH issue
  2. update CHANGELOG.md to include this fix

@gorarakelyan
Copy link
Contributor

Made a minor fix in the changelog :)

btw, it is worth considering getting the weight tensor only once, isn't it?

e.g:

weight = None
if hasattr(m, 'weight') and m.weight is not None:
    weight = getattr(m.weight, dt, None)

if weight is not None:
    layers[layer_name]['weight'] = get_pt_tensor(getattr(m.weight, dt)).numpy()

instead of:

if hasattr(m, 'weight') and m.weight is not None and getattr(m.weight, dt, None) is not None:
    layers[layer_name]['weight'] = get_pt_tensor(getattr(m.weight, dt)).numpy()

thoughts? @kage08 @alberttorosyan

@kage08
Copy link
Contributor Author

kage08 commented Jun 29, 2022

Made a minor fix in the changelog :)

btw, it is worth considering getting the weight tensor only once, isn't it?

e.g:

weight = None
if hasattr(m, 'weight') and m.weight is not None:
    weight = getattr(m.weight, dt, None)

if weight is not None:
    layers[layer_name]['weight'] = get_pt_tensor(getattr(m.weight, dt)).numpy()

instead of:

if hasattr(m, 'weight') and m.weight is not None and getattr(m.weight, dt, None) is not None:
    layers[layer_name]['weight'] = get_pt_tensor(getattr(m.weight, dt)).numpy()

thoughts? @kage08 @alberttorosyan

This makes sense for me. It avoids getting value twice.
Personally I would use the walrus operator, but I think that may break compatibility. So I agree with proposed change.

Copy link
Contributor

@gorarakelyan gorarakelyan left a comment

Choose a reason for hiding this comment

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

@kage08 looks awesome 🙌

Copy link
Contributor

@mihran113 mihran113 left a comment

Choose a reason for hiding this comment

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

@kage08 looks good 🙌 thanks a lot for contribution

@gorarakelyan gorarakelyan merged commit 5668bb5 into aimhubio:main Jul 5, 2022
@gorarakelyan
Copy link
Contributor

Merged the PR! @kage08 congrats on the first merged contribution! 🚀

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.

Pytorch track_gradients_dists errors out if some parameters don't have gradients
6 participants