Skip to content

Forced float32 under tensor and pipeline parallel: use the native parallel GradScaler - #18215

Merged
danielhanchen merged 2 commits into
mainfrom
forced-fp32-parallel-scaler
Nov 17, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
forced-fp32-parallel-scaler

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Forced float32 models (float16 frozen weights with float32 LoRA, for example Gemma-4 and Qwen3.5 on T4) get a dynamic GradScaler from #18065. That scaler is built in the trainer post init, which runs after Unsloth's tensor parallel, pipeline parallel and native FSDP2 Trainer setup. The setup is what converts an fp16 run's scaler into the native parallel scaler, so the forced float32 scaler stayed a plain torch.amp.GradScaler under TP and PP.

Under TP=2 this fails on the first gradient clip:

RuntimeError: aten._amp_foreach_non_finite_check_and_unscale_.default: got mixed torch.Tensor and DTensor, need to convert all torch.Tensor to DTensor before calling distributed operators!

raised from tp_native.clip_grad_norm_ -> unscale_gradients -> GradScaler.unscale_. With local shard gradients there is no error, but found_inf is decided per rank, so an overflow on one rank skips the step there while the other rank applies it.

Fix

  • tp_native._tp_scaler and pp_env.pp_trainer_setup mark the accelerator, so the forced float32 path knows a TP / PP run converted the scaler.
  • _unsloth_forced_float32_grad_scaler then converts its scaler through the same _scaler_for_native path when a TP / PP / native FSDP2 run is active. The resulting scaler unscales DTensor and local gradients together and all reduces found_inf over the ranks. It keeps the forced float32 starting scale from Forced float32 GradScaler: start at 2**8, divided by the data parallel loss multiplier #18208 (28, already divided by the data parallel loss multiplier) instead of fp16's 216.
  • unscale_gradients reads accelerator.scaler at call time so it uses the converted scaler.
  • Plain DDP is unchanged: gradients are averaged before unscale_, so every rank already sees the same inf.

Tests

New tests/test_forced_float32_parallel_scaler_gloo.py (CPU gloo, world 2) runs the forced float32 scaler after the TP / PP setup, for TP with DTensor gradients, TP with local gradients, and PP, with no overflow, an overflow on one rank, and an overflow on every rank.

main this PR
new test 5 passed, 4 failed 9 passed

On main, the 3 TP DTensor cases fail with the mixed Tensor / DTensor error. The TP local-gradients case with an overflow on one rank fails because the ranks disagree on the step count. PP already agreed through its stage-level found_inf reduction, and it still passes.

The related scaler suites (TP / PP / FSDP2 scaler, forced float32 scaler, trainer scaler attach, PP env) give the same result before and after: 44 passed, 32 skipped. The same 5 test_pp_env_gloo cases fail on both, because the CPU test env has no vllm.sampling_params.

Kaggle 2x T4

These are fp16 runs on forced float32 models, with main at 1422db4 as the base and the same pin with this PR's three files laid over it as the head.

Model, mode main this PR
Qwen3.6-35B-A3B architecture (random weights, 4 of 40 layers), TP=2 crashes in the first clip with the mixed Tensor / DTensor error above 3 steps, grad norm 0.255 / 0.257 / 0.286
Gemma-4-E2B 4-bit, TP=2, 25 steps every step finite; tracks 1 GPU within max abs loss diff 0.038 (0.5% relative), final loss 2.900 vs 2.912

Gemma-4-E2B under TP has no DTensor and local gradient mix, so main does not crash there.

Not covered

Forced float32 (float16 frozen weights, float32 LoRA) builds its dynamic
GradScaler in the trainer post init, after Unsloth's tensor parallel,
pipeline parallel and native FSDP2 Trainer setup has already converted
an fp16 run's scaler. The forced scaler therefore stayed a plain
torch.amp.GradScaler:

- under TP, unscale_ sees DTensor and local gradients together and
  raises "aten._amp_foreach_non_finite_check_and_unscale_.default: got
  mixed torch.Tensor and DTensor" on the first clip;
- with local shard gradients it decides found_inf per rank, so one rank
  skips a step while another applies it.

The TP and PP setups now record their data parallel size on the
accelerator, and the forced scaler is converted with the same
_scaler_for_native path (DTensor aware unscale, found_inf all reduced
over the ranks) with its initial scale divided as those setups divide
fp16's. Plain DDP is unchanged since gradients are averaged before
unscale_. Adds a gloo test for TP (DTensor and local gradients) and PP
with an overflow on one rank and on every rank.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T21:15:32.425191Z 20f6b1c 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 20f6b1cf1b

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

[skip ci]
@danielhanchen
danielhanchen merged commit 51925ee into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the forced-fp32-parallel-scaler branch November 17, 2026 22:14
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.

1 participant