-
Notifications
You must be signed in to change notification settings - Fork 197
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 (scaling)!: clamp to avoid inf/nan in forward/backward #1097
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb514ba
Fix (core/scaling): clamping before restrict_val
Giuseppe5 d9fff9a
Test (core/scaling): add test for inf/nan restricted scale
Giuseppe5 c399e9b
Fix
Giuseppe5 170ef90
fix test values
Giuseppe5 863a00c
More fixes to tests
Giuseppe5 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 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
This file contains 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
This file contains 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,53 @@ | ||
import torch | ||
|
||
from brevitas.core.function_wrapper.misc import Identity | ||
from brevitas.core.restrict_val import PowerOfTwoRestrictValue | ||
from brevitas.core.scaling.runtime import RuntimeDynamicGroupStatsScaling | ||
from brevitas.core.scaling.runtime import RuntimeStatsScaling | ||
from brevitas.core.scaling.runtime import StatsFromParameterScaling | ||
from brevitas.core.stats.stats_op import AbsMax | ||
from brevitas.core.stats.stats_wrapper import SCALAR_SHAPE | ||
|
||
SCALING_MIN_VAL = 1e-6 | ||
|
||
|
||
def test_scaling_min_val_parameter(): | ||
inp = torch.zeros(1, 5, requires_grad=True) | ||
scaling_op = StatsFromParameterScaling( | ||
scaling_stats_impl=AbsMax(), | ||
scaling_stats_input_view_shape_impl=Identity(), | ||
scaling_stats_input_concat_dim=None, | ||
tracked_parameter_list=[inp], | ||
scaling_shape=SCALAR_SHAPE, | ||
restrict_scaling_impl=PowerOfTwoRestrictValue(), | ||
scaling_min_val=SCALING_MIN_VAL) | ||
pre_scale = scaling_op(inp) | ||
pre_scale.sum().backward() | ||
assert not torch.isnan(inp.grad).any() | ||
|
||
|
||
def test_scaling_min_val_runtime(): | ||
inp = torch.zeros(1, 5, requires_grad=True) | ||
scaling_op = RuntimeStatsScaling( | ||
scaling_stats_impl=AbsMax(), | ||
scaling_stats_input_view_shape_impl=Identity(), | ||
scaling_shape=SCALAR_SHAPE, | ||
restrict_scaling_impl=PowerOfTwoRestrictValue(), | ||
scaling_min_val=SCALING_MIN_VAL) | ||
pre_scale = scaling_op(inp) | ||
pre_scale.sum().backward() | ||
assert not torch.isnan(inp.grad).any() | ||
|
||
|
||
def test_scaling_min_val_dynamic_group(): | ||
inp = torch.zeros(1, 6, requires_grad=True) | ||
scaling_op = RuntimeDynamicGroupStatsScaling( | ||
group_size=3, | ||
group_dim=1, | ||
input_view_impl=Identity(), | ||
scaling_min_val=SCALING_MIN_VAL, | ||
restrict_scaling_impl=PowerOfTwoRestrictValue(), | ||
scaling_stats_impl=AbsMax()) | ||
pre_scale = scaling_op(inp) | ||
pre_scale.sum().backward() | ||
assert not torch.isnan(inp.grad).any() |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe
self.clamp_scaling
is not necessary here because we're already in log domain, andself.restrict_scaling
is responsible only for rounding and potential conversion from log to real domain (i.e., exponential).Applying clamping before restrict_val means we can't get to negative values, thus scaling values between 0 and 1.
@nickfraser
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds right to me!