Repository navigation
Forced float32 under tensor and pipeline parallel: use the native parallel GradScaler - #18215
Merged
Merged
Conversation
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.
Codex Review Summary: ✅ CompletedThis 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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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". |
[skip ci]
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.
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.GradScalerunder TP and PP.Under TP=2 this fails on the first gradient clip:
raised from
tp_native.clip_grad_norm_->unscale_gradients->GradScaler.unscale_. With local shard gradients there is no error, butfound_infis decided per rank, so an overflow on one rank skips the step there while the other rank applies it.Fix
tp_native._tp_scalerandpp_env.pp_trainer_setupmark the accelerator, so the forced float32 path knows a TP / PP run converted the scaler._unsloth_forced_float32_grad_scalerthen converts its scaler through the same_scaler_for_nativepath when a TP / PP / native FSDP2 run is active. The resulting scaler unscales DTensor and local gradients together and all reducesfound_infover 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_gradientsreadsaccelerator.scalerat call time so it uses the converted scaler.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.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_infreduction, 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_gloocases fail on both, because the CPU test env has novllm.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.
Gemma-4-E2B under TP has no DTensor and local gradient mix, so main does not crash there.
Not covered