-
Notifications
You must be signed in to change notification settings - Fork 16
Improve test coverage and report #119
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
a70ed89
1ca40c3
3b0fc7e
216bad2
2ffc244
ceeaf64
66fefd5
362c781
7425047
07659c4
695ce68
b4ea359
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 |
|---|---|---|
|
|
@@ -184,5 +184,6 @@ exclude_also = [ | |
| "if closure", | ||
| "loss = closure", | ||
| "raise .*Error", | ||
| "@abstractmethod", | ||
| ] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,19 @@ | |
| # limitations under the License. | ||
| export TORCH_COMPILE_DISABLE=1 | ||
|
|
||
| mkdir -p test-results/tests/ | ||
|
|
||
| error=0 | ||
| torchrun --nproc_per_node=8 --no-python coverage run -p tests/test_distributed_muon_utils_cpu.py -v -2 || error=1 | ||
| torchrun --nproc_per_node=4 --no-python coverage run -p tests/test_distributed_muon_utils_cpu.py -v -2 || error=1 | ||
| coverage run -p --source=emerging_optimizers tests/test_scalar_optimizers.py --device=cpu -v -2 || error=1 | ||
| coverage run -p --source=emerging_optimizers tests/test_procrustes_step.py --device=cpu -v -2 || error=1 | ||
| for n in 8 4; do | ||
| torchrun --nproc_per_node=$n --no-python coverage run -p \ | ||
| tests/test_distributed_muon_utils_cpu.py \ | ||
| --xml_output_file="test-results/tests/test_distributed_muon_utils_cpu_n${n}.xml" \ | ||
| -v -2 || error=1 | ||
| done | ||
|
|
||
| for test in "tests/test_scalar_optimizers.py" "tests/test_procrustes_step.py"; do | ||
| report_name="test-results/${test}.xml" | ||
| coverage run -p --source=emerging_optimizers $test --device=cpu -v -2 --xml_output_file="$report_name" || error=1 | ||
| done | ||
|
Comment on lines
+26
to
+29
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. New test files excluded from CI
The for-loop only covers for test in "tests/test_scalar_optimizers.py" "tests/test_procrustes_step.py" "tests/test_soap.py" "tests/test_soap_utils.py"; do
report_name="test-results/${test}.xml"
coverage run -p --source=emerging_optimizers $test --device=cpu -v -2 --xml_output_file="$report_name" || error=1
done |
||
|
|
||
| exit "${error}" | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -153,7 +153,13 @@ def test_calculate_laprop_update_with_zero_momentum_equals_rmsprop(self) -> None | |||||||||||||||||||||||||||||
| expected_param_val_after_step = initial_param_val_tensor - lr * laprop_update | ||||||||||||||||||||||||||||||
| torch.testing.assert_close(param.data, expected_param_val_after_step, atol=1e-6, rtol=1e-6) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_calculate_ademamix_update_with_alpha_zero_equals_adam(self) -> None: | ||||||||||||||||||||||||||||||
| @parameterized.parameters( | ||||||||||||||||||||||||||||||
| {"correct_bias": True, "num_beta_fast_warmup_steps": None}, | ||||||||||||||||||||||||||||||
| {"correct_bias": False, "num_beta_fast_warmup_steps": 2}, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| def test_calculate_ademamix_update_with_alpha_zero_equals_adam( | ||||||||||||||||||||||||||||||
| self, correct_bias: bool, num_beta_fast_warmup_steps: int | None | ||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||
|
Comment on lines
+156
to
+162
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. Misleading parameter name The parameterized argument is named By contrast, the sibling test
Suggested change
And update the usage on line 181 accordingly: num_beta_slow_warmup_steps=num_beta_slow_warmup_steps, |
||||||||||||||||||||||||||||||
| # AdEMAMix with alpha=0 and no beta scheduling should be equivalent to Adam. | ||||||||||||||||||||||||||||||
| exp_avg_fast_initial = torch.tensor([[1.0]], device=self.device) | ||||||||||||||||||||||||||||||
| exp_avg_slow_initial = torch.tensor([[1.0]], device=self.device) | ||||||||||||||||||||||||||||||
|
|
@@ -162,7 +168,6 @@ def test_calculate_ademamix_update_with_alpha_zero_equals_adam(self) -> None: | |||||||||||||||||||||||||||||
| betas = (0.9, 0.99, 0.999) | ||||||||||||||||||||||||||||||
| eps = 1e-8 | ||||||||||||||||||||||||||||||
| step = 10 | ||||||||||||||||||||||||||||||
| correct_bias_manual = True | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Calculate AdEMAMix update | ||||||||||||||||||||||||||||||
| exp_avg_fast_for_ademamix = exp_avg_fast_initial.clone() | ||||||||||||||||||||||||||||||
|
|
@@ -173,12 +178,12 @@ def test_calculate_ademamix_update_with_alpha_zero_equals_adam(self) -> None: | |||||||||||||||||||||||||||||
| exp_avg_fast_for_ademamix, | ||||||||||||||||||||||||||||||
| exp_avg_slow_for_ademamix, | ||||||||||||||||||||||||||||||
| exp_avg_sq_for_ademamix, | ||||||||||||||||||||||||||||||
| num_beta_slow_warmup_steps=None, | ||||||||||||||||||||||||||||||
| num_beta_slow_warmup_steps=num_beta_fast_warmup_steps, | ||||||||||||||||||||||||||||||
| num_alpha_warmup_steps=None, | ||||||||||||||||||||||||||||||
| betas=betas, | ||||||||||||||||||||||||||||||
| step=step, | ||||||||||||||||||||||||||||||
| eps=eps, | ||||||||||||||||||||||||||||||
| correct_bias=correct_bias_manual, | ||||||||||||||||||||||||||||||
| correct_bias=correct_bias, | ||||||||||||||||||||||||||||||
| alpha=0.0, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -190,7 +195,7 @@ def test_calculate_ademamix_update_with_alpha_zero_equals_adam(self) -> None: | |||||||||||||||||||||||||||||
| exp_avg_for_adam, | ||||||||||||||||||||||||||||||
| exp_avg_sq_for_adam, | ||||||||||||||||||||||||||||||
| (betas[0], betas[1]), | ||||||||||||||||||||||||||||||
| correct_bias=correct_bias_manual, | ||||||||||||||||||||||||||||||
| correct_bias=correct_bias, | ||||||||||||||||||||||||||||||
| use_nesterov=False, | ||||||||||||||||||||||||||||||
| step=step, | ||||||||||||||||||||||||||||||
| eps=eps, | ||||||||||||||||||||||||||||||
|
|
@@ -205,8 +210,8 @@ def test_calculate_sim_ademamix_update_with_zero_momentum_and_alpha_equals_rmspr | |||||||||||||||||||||||||||||
| grad = torch.tensor([[0.5]], device=self.device) | ||||||||||||||||||||||||||||||
| betas = (0.0, 0.99) # beta1=0 for momentum | ||||||||||||||||||||||||||||||
| eps = 1e-8 | ||||||||||||||||||||||||||||||
| step = 10 | ||||||||||||||||||||||||||||||
| correct_bias = False | ||||||||||||||||||||||||||||||
| step = 10 | ||||||||||||||||||||||||||||||
| lr = 0.25 | ||||||||||||||||||||||||||||||
| exp_avg_for_sim_ademamix = exp_avg_initial.clone() | ||||||||||||||||||||||||||||||
| exp_avg_sq_for_sim_ademamix = exp_avg_sq_initial.clone() | ||||||||||||||||||||||||||||||
|
|
@@ -237,7 +242,7 @@ def test_calculate_sim_ademamix_update_with_zero_momentum_and_alpha_equals_rmspr | |||||||||||||||||||||||||||||
| eps=eps, | ||||||||||||||||||||||||||||||
| weight_decay=0, | ||||||||||||||||||||||||||||||
| momentum=0, | ||||||||||||||||||||||||||||||
| centered=False, | ||||||||||||||||||||||||||||||
| centered=correct_bias, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Manually set RMSProp's internal state | ||||||||||||||||||||||||||||||
|
|
@@ -293,6 +298,41 @@ def test_calculate_signum_with_shape_scaling_returns_sign(self) -> None: | |||||||||||||||||||||||||||||
| expected_update = torch.sign(exp_avg).abs() * (2 / (shape[0] + shape[1])) | ||||||||||||||||||||||||||||||
| torch.testing.assert_close(update_abs, expected_update, atol=0, rtol=0) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_calculate_lion_update_returns_sign(self) -> None: | ||||||||||||||||||||||||||||||
| """Tests that Lion update returns sign of interpolated momentum.""" | ||||||||||||||||||||||||||||||
| shape = (8, 12) | ||||||||||||||||||||||||||||||
| momentum_beta = 0.9 | ||||||||||||||||||||||||||||||
| grad = torch.randn(shape, device=self.device) | ||||||||||||||||||||||||||||||
| exp_avg = torch.randn(shape, device=self.device) | ||||||||||||||||||||||||||||||
| exp_avg_clone = exp_avg.clone() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| update = scalar_optimizers.calculate_lion_update(grad, exp_avg, momentum_beta=momentum_beta) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Update should be sign(beta * m + (1 - beta) * g) | ||||||||||||||||||||||||||||||
| expected_update = torch.sign(momentum_beta * exp_avg_clone + (1 - momentum_beta) * grad) | ||||||||||||||||||||||||||||||
| torch.testing.assert_close(update, expected_update, atol=0, rtol=0) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # exp_avg should be updated in-place: lerp_(grad, 1 - beta) | ||||||||||||||||||||||||||||||
| expected_exp_avg = torch.lerp(exp_avg_clone, grad, 1 - momentum_beta) | ||||||||||||||||||||||||||||||
| torch.testing.assert_close(exp_avg, expected_exp_avg, atol=1e-6, rtol=1e-6) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_calculate_lion_update_with_separate_betas(self) -> None: | ||||||||||||||||||||||||||||||
| """Tests Lion with different beta1 and beta2.""" | ||||||||||||||||||||||||||||||
| shape = (4, 6) | ||||||||||||||||||||||||||||||
| beta1, beta2 = 0.9, 0.99 | ||||||||||||||||||||||||||||||
| grad = torch.randn(shape, device=self.device) | ||||||||||||||||||||||||||||||
| exp_avg = torch.randn(shape, device=self.device) | ||||||||||||||||||||||||||||||
| exp_avg_clone = exp_avg.clone() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| update = scalar_optimizers.calculate_lion_update(grad, exp_avg, momentum_beta=beta1, momentum_beta2=beta2) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expected_update = torch.sign(beta1 * exp_avg_clone + (1 - beta1) * grad) | ||||||||||||||||||||||||||||||
| torch.testing.assert_close(update, expected_update, atol=0, rtol=0) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # With separate beta2, momentum uses beta2 | ||||||||||||||||||||||||||||||
| expected_exp_avg = torch.lerp(exp_avg_clone, grad, 1 - beta2) | ||||||||||||||||||||||||||||||
| torch.testing.assert_close(exp_avg, expected_exp_avg, atol=1e-6, rtol=1e-6) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||
| testing.absltest.main() | ||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.