Skip to content

Native FSDP2 float16: start the loss scale where one GPU's scaled backward fits - #15033

Merged
danielhanchen merged 5 commits into
mainfrom
dh/s4-fsdp2-fp16-parity
Oct 24, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
dh/s4-fsdp2-fp16-parity

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Under native FSDP2 (UNSLOTH_FSDP2=1 with torchrun) in float16, the first optimizer step often overflows and is skipped where one GPU at the same global batch does not. In a 2-rank sweep on Kaggle T4x2 this hit 15 cells (smol_llama, Qwen3.5 VLMs, PaliGemma and others). Training recovers, but the FSDP2 loss curve runs one step behind one GPU.

This is not a dtype difference. Trainable weights are float32 masters on both sides, the policy reduces in float32, and gradients reach the optimizer in float32. The cause is loss normalisation:

  • Each rank's loss is its own token sum over its own share of the tokens (Trainer.compute_loss multiplies the global mean by the world size).
  • So every per-token activation gradient in the float16 backward is world_size times the one-GPU one, and the float32 reduce divides it back out.
  • At the same loss scale, a backward that fits in float16 on one GPU overflows here.

DDP has the same behaviour (on Kaggle, DDP and FSDP2 give bitwise-equal loss and grad norm on Qwen3.5 and PaliGemma, and both skip step 0).

Change

_scaler_for_native starts the ShardedGradScaler at the default init_scale divided by the data parallel size (read from the model's mesh, else the default process group).

  • The scaled backward then equals one GPU's, and the unscaled gradients are unchanged.
  • An init_scale set through GradScalerKwargs is kept as given.
  • One GPU, bfloat16, DDP and every optimizer (8-bit included) are untouched. The logged get_scale() on 2 ranks now starts at 32768 instead of 65536.

Evidence (Kaggle T4x2, fp16, 3 steps, single-GPU reference on the second T4 of the same kernel)

cell FSDP2 on main FSDP2 with this change
smol_llama LoRA skips step 0 no skip, loss within 4e-5 of one GPU
Qwen3.5 VLM QLoRA skips step 0 (DDP identical) no skip, loss within 4e-5 of one GPU
PaliGemma LoRA skips step 0 (DDP identical) no skip, loss within 1.5e-4 of one GPU (the grad norm gap equals DDP's)
Qwen3-0.6B full FT skips all 3 steps skips all 3 steps, as one GPU does on this T4

Tests

  • New tests/test_fsdp2_native_fp16_scale_parity_gloo.py (2 ranks, gloo, real fully_shard, Trainer-style loss): on main 2 of 3 fail ("native FSDP2 overflowed where one GPU did not"); all pass with the change.
  • tests/test_fsdp2_native_scaler_fused_gloo.py (sets init_scale through the handler) passes unchanged, which covers the user-set scale being kept. The clip tests pass.

Not covered

  • World size above 2, HSDP and context-parallel meshes (the mesh size is taken as the data parallel size).
  • RL and diffusion trainers' loss normalisation.
  • 10 of the 15 sweep cells were not rerun.

…e data parallel size

Each rank normalises its loss by its own share of the tokens (the Trainer multiplies the global
mean by the world size), so the per-token gradients of the float16 backward are world_size times
the one GPU ones, while the reduce divides it back out. At the same GradScaler scale a backward one
GPU fits overflows on every rank and the step is skipped (smol_llama, Qwen3-0.6B full finetuning,
Qwen3.5, PaliGemma skipped step 0 under FSDP2 only). Starting the swapped ShardedGradScaler at
init_scale / data parallel size makes the scaled backward match one GPU; unscaled gradients and
bf16 / single GPU runs are unchanged.
@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-24T02:01:46.280883Z 9d21bbe 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. Can't wait for the next one!

Reviewed commit: ce86adc10b

ℹ️ 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 added a commit that referenced this pull request Oct 23, 2026
…mage_cfg_split (#15040)

`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding`
fails on `main` again, for example in `Repo tests (CPU, rest)` on
#15033:

```
studio/backend/tests/test_diffusion_zimage_cfg_split.py:551: read_text()
studio/backend/tests/test_diffusion_zimage_cfg_split.py:748: read_text()
```

#14955 added two more reads of `diffusion.py` with the platform default
encoding to the same test file #15003 fixed earlier. Both now pass
`encoding = "utf-8"`.

## Verification

- On `main`: `-k checked_in_file_reads` gives 1 failed, 15 passed.
- On this branch: 16 passed.
- `studio/backend/tests/test_diffusion_zimage_cfg_split.py`: 35 passed.
[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 9d21bbe193

ℹ️ 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 e472a56 into main Oct 24, 2026
10 of 30 checks passed
@danielhanchen
danielhanchen deleted the dh/s4-fsdp2-fp16-parity branch October 24, 2026 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant