Repository navigation
Native FSDP2 float16: skip the step when every rank overflows under fused AdamW - #14996
Merged
Merged
Conversation
…d AdamW ShardedGradScaler all-reduces found_inf with a SUM, so it holds the number of ranks that overflowed. Fused AdamW (what adamw_8bit is swapped to under native FSDP2) skips its update only when found_inf == 1 and subtracts found_inf from its step counts, so an overflow seen by both ranks stepped on inf / NaN gradients and wrote NaN weights. Clamp found_inf to 0 / 1 after the reduce. Seen on Kaggle T4x2 (float16): Qwen2.5-VL 7B and Granite 4.0 notebooks reach a NaN loss from step 4 under UNSLOTH_FSDP2=1 while DDP skips the same step and keeps training.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Hello @danielhanchen |
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.
Under native FSDP2 with float16 (T4 / V100), a step where every rank overflows was applied with inf / NaN gradients instead of being skipped, and wrote NaN weights.
Cause
_scaler_for_nativeswaps in aShardedGradScalerwhoseunscale_all-reducesfound_infwith a sum, so its value is the number of ranks that overflowed. Native FSDP2 runs fused AdamW (it replacesadamw_8bit, which cannot step DTensor parameters), and the fused kernel skips the step only whenfound_inf == 1, and subtracts it from the step count. With 2 ranks overflowing on the same step,found_infwas 2: the step ran on inf gradients and the step count went backwards by 2.Fix
Clamp the all-reduced flag to 0 / 1 before handing it to the optimizer, keeping the 0-dim shape the previous fix introduced. Non-fused optimizers only test
found_inf > 0, so they are unchanged. DDP and one GPU do not use this scaler.Evidence
adamw_8bit: the Qwen2.5-VL 7B and Granite 4.0 notebooks went NaN from step 4 under FSDP2 on main (finite under DDP), and the Qwen3 4B GRPO notebook hit a CUDA assert at step 3. With this change all three train past those steps; Qwen2.5-VL and Granite finish on the same loss scale as DDP. olmo_hybrid LoRA on T4: main NaN from step 2, fixed trains on theadamw_torchFSDP2 and DDP loss path.tests/test_fsdp2_native_scaler_fused_gloo.py(2 gloo ranks) gains an every-rank-overflows case for fused and non-fused AdamW: both must skip that step, back the scale off once and end with the same weights. It fails on main for fused AdamW and passes with this change.