-
Notifications
You must be signed in to change notification settings - Fork 3.5k
add unit tests for pl.utilities.grads
#9765
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
afad0d7
add tests for utilities/grads.py
awaelchli ad3e8c2
add docstring
awaelchli 2e41374
add case for None grad
awaelchli d4d41bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0555f49
handle invalid input
awaelchli 374d687
Merge branch 'master' into tests/grad-norm
awaelchli 160689b
update changelog
awaelchli 428055b
Merge branch 'master' into tests/grad-norm
carmocca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from math import isclose, sqrt | ||
|
||
import pytest | ||
import torch | ||
import torch.nn as nn | ||
|
||
from pytorch_lightning.utilities import grad_norm | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"norm_type,expected", | ||
[ | ||
# bug for L0 "norm"! total norm should be 5 here | ||
( | ||
0, | ||
{"grad_0.0_norm_param0": 3, "grad_0.0_norm_param1": 2, "grad_0.0_norm_total": 5}, | ||
), | ||
( | ||
1, | ||
{"grad_1.0_norm_param0": 1 + 2 + 3, "grad_1.0_norm_param1": 4 + 5, "grad_1.0_norm_total": 15}, | ||
), | ||
( | ||
2, | ||
{ | ||
"grad_2.0_norm_param0": pow(1 + 4 + 9, 0.5), | ||
"grad_2.0_norm_param1": pow(16 + 25, 0.5), | ||
"grad_2.0_norm_total": pow(1 + 4 + 9 + 16 + 25, 0.5), | ||
}, | ||
), | ||
( | ||
3.14, | ||
{ | ||
"grad_3.14_norm_param0": pow(1 + 2 ** 3.14 + 3 ** 3.14, 1 / 3.14), | ||
"grad_3.14_norm_param1": pow(4 ** 3.14 + 5 ** 3.14, 1 / 3.14), | ||
"grad_3.14_norm_total": pow(1 + 2 ** 3.14 + 3 ** 3.14 + 4 ** 3.14 + 5 ** 3.14, 1 / 3.14), | ||
}, | ||
), | ||
( | ||
"inf", | ||
{ | ||
"grad_inf_norm_param0": max(1, 2, 3), | ||
"grad_inf_norm_param1": max(4, 5), | ||
"grad_inf_norm_total": max(1, 2, 3, 4, 5), | ||
}, | ||
), | ||
], | ||
) | ||
def test_grad_norm(norm_type, expected): | ||
"""Test utility function for computing the p-norm of individual parameter groups and norm in total.""" | ||
|
||
class Model(nn.Module): | ||
def __init__(self): | ||
super().__init__() | ||
self.param0 = nn.Parameter(torch.rand(3)) | ||
self.param1 = nn.Parameter(torch.rand(2, 1)) | ||
self.param0.grad = torch.tensor([-1.0, 2.0, -3.0]) | ||
self.param1.grad = torch.tensor([[-4.0], [5.0]]) | ||
# param without grad should not contribute to norm | ||
self.param2 = nn.Parameter(torch.rand(1)) | ||
|
||
model = Model() | ||
norms = grad_norm(model, norm_type) | ||
expected = {k: round(v, 4) for k, v in expected.items()} | ||
assert norms == expected |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.