-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix (scaling)!: clamp to avoid inf/nan in forward/backward (#1097)
Breaking change: new models trained after this PR, especially using QAT/gradient-based PTQ might converge to different results
- Loading branch information
Showing
4 changed files
with
108 additions
and
3 deletions.
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