Skip to content

Native FSDP2 float16: skip the step when every rank overflows under fused AdamW - #14996

Merged
danielhanchen merged 2 commits into
mainfrom
dh/s3-fsdp2-notebook-fixes
Oct 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/s3-fsdp2-notebook-fixes

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_native swaps in a ShardedGradScaler whose unscale_ all-reduces found_inf with a sum, so its value is the number of ranks that overflowed. Native FSDP2 runs fused AdamW (it replaces adamw_8bit, which cannot step DTensor parameters), and the fused kernel skips the step only when found_inf == 1, and subtracts it from the step count. With 2 ranks overflowing on the same step, found_inf was 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

  • Kaggle T4x2, fp16, default 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 the adamw_torch FSDP2 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.

…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T16:24:27.601840Z 46cdb7a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 46cdb7afab

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit 96e1778 into main Oct 23, 2026
33 of 37 checks passed
@danielhanchen
danielhanchen deleted the dh/s3-fsdp2-notebook-fixes branch October 23, 2026 20:30
@metrizable

Copy link
Copy Markdown

Hello @danielhanchen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants