Fix flaky Lion optimizer test on HIP/AMD GPUs - #95
Open
kudomcho wants to merge 1 commit into
Open
Conversation
Scale max_error_count with tensor size instead of hardcoding 10. Lion uses sgn() on a lerp value, and when it's near zero the fused bnb kernel and separate-op PyTorch reference disagree on the sign due to different FMA contraction by HIP clang vs nvcc. Each flip causes exactly 2*lr error. Larger tensors have more boundary cases, so the tolerance should scale accordingly. For dim2=4097 x dim1=1024 (4.19M elements), the new limit is 20 instead of 10. The test was failing ~30% of runs on MI300X with 12 errors — just 2 over the old limit. Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
max_error_countintest_optimizer32bitwith tensor size instead of hardcoding 10test_optimizer32bit[dim2=4097-dim1=1024-fp32-opt=lion]on MI300X (~30% failure rate)Root Cause
Lion uses
sgn()on a lerp of momentum and gradient. When the lerp value is near zero, the fused bnb kernel and the separate-op PyTorch reference can disagree on the sign due to different FMA contraction by HIP's clang vs nvcc. Each sign flip causes exactly2 * lr = 0.0002error. Larger tensors have more boundary cases — the 4.19M element case was hitting ~12 errors against a limit of 10.New formula:
max(10, p1.numel() // 200000)— keeps the floor at 10 for small tensors, scales up for larger ones.Test plan
dim2=4097-dim1=1024-fp32-opt=lion: 10/10 passes (was 7/10 before)test_optim.py: 151 passed, 26 skipped, 0 failedtest_functional.py: 411 passed, 339 skipped, 0 failures related to this change🤖 Generated with Claude Code