Repository navigation
Native FSDP2 float16: start the loss scale where one GPU's scaled backward fits - #15033
Conversation
…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.
|
@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. Can't wait for the next one! 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". |
…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]
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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". |
Problem
Under native FSDP2 (
UNSLOTH_FSDP2=1withtorchrun) 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:
Trainer.compute_lossmultiplies the global mean by the world size).world_sizetimes the one-GPU one, and the float32 reduce divides it back out.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_nativestarts theShardedGradScalerat the defaultinit_scaledivided by the data parallel size (read from the model's mesh, else the default process group).init_scaleset throughGradScalerKwargsis kept as given.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)
Tests
tests/test_fsdp2_native_fp16_scale_parity_gloo.py(2 ranks, gloo, realfully_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(setsinit_scalethrough the handler) passes unchanged, which covers the user-set scale being kept. The clip tests pass.Not covered