-
Notifications
You must be signed in to change notification settings - Fork 34
Add option to normalize NS input in double precision #238
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
base: main
Are you sure you want to change the base?
Changes from all commits
f75800c
83032c3
0484096
ff672e3
854dfd9
451176d
3c9a69b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -121,28 +121,14 @@ def test_newtonschulz5_close_to_reference(self, dim1, dim2): | |||||
| rtol=1e-7, | ||||||
| ) | ||||||
|
|
||||||
| @parameterized.parameters(1e-2, 1e-6, 1e-9, 1e-12) | ||||||
| def test_newtonschulz_small_eps(self, scale): | ||||||
| """Orthogonalization depends only on direction, so scaling the input must not change the output. | ||||||
|
|
||||||
| Regression test for issue #229: a too-large ``eps`` in the internal ``F.normalize`` divides | ||||||
| small-norm inputs by ``eps`` instead of their norm, silently degenerating the output. The | ||||||
| orthogonalized result for ``x`` and ``scale * x`` must match for any ``scale > 0``. | ||||||
| """ | ||||||
| x = torch.randn(256, 256, device=self.device, dtype=torch.float32) | ||||||
| x = x / x.norm() # unit Frobenius norm direction | ||||||
| ref = muon_utils.newton_schulz(x, steps=5, coefficient_type="quintic") | ||||||
| out = muon_utils.newton_schulz(scale * x, steps=5, coefficient_type="quintic") | ||||||
| torch.testing.assert_close( | ||||||
| out, | ||||||
| ref, | ||||||
| atol=1e-4, | ||||||
| rtol=1e-5, | ||||||
| msg=lambda m: ( | ||||||
| f"newton_schulz not scale-invariant at input scale {scale}: " | ||||||
| f"||out||_F={out.norm().item():.4f} vs ||ref||_F={ref.norm().item():.4f}\n{m}" | ||||||
| ), | ||||||
| ) | ||||||
| def test_preserve_values_with_underflowed_norm_in_fp64(self): | ||||||
| scale = 1e-30 | ||||||
| x = torch.randn(256, 256, device=self.device, dtype=torch.float32) * scale | ||||||
| assert torch.linalg.vector_norm(x) == 0 # should underflow | ||||||
| norm_ref = torch.linalg.vector_norm(x, dtype=torch.double) | ||||||
| assert norm_ref != 0 | ||||||
| out = muon_utils.newton_schulz(x, steps=0, normalize_in_double=True) | ||||||
| torch.testing.assert_close(x / norm_ref, out, atol=0, rtol=1e-6) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| @parameterized.parameters( | ||||||
| (2, 256, 256), | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.