Repository navigation
Native FSDP2 fp16 scaler: agree on overflow over ranks without an explicit unscale_, and give fused optimizers one 0 / 1 flag over all devices - #15416
Conversation
…licit unscale_, and give fused optimizers one 0 / 1 flag over all devices
|
@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. You're on a roll. 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". |
…hout clipping [skip ci]
|
Added a TP Trainer test for the same defect ( @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". |
…ies of grad_scale / found_inf [skip ci]
|
Follow-up fix for mixed CPU + CUDA fused AdamW (cpu_offload shards beside CUDA ones). GradScaler's replicator and the fused optimizer move The scaler now hands the CPU side blocking host copies: New deterministic test @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]
Problem
For float16, native FSDP2 (and tensor parallel through
_tp_scaler) swaps the Trainer's GradScaler for_ShardedGradScaler(_scaler_for_nativein unsloth/models/fsdp2_native.py). Itsunscale_all-reduces the overflow flags over ranks and clamps each device's flag to 0 / 1. Two paths still go wrong with fused optimizers (adamw_torch_fused):unscale_(no gradient clipping,max_grad_norm = 0). For an optimizer with_step_supports_amp_scaling(fused Adam / AdamW),torch.amp.GradScaler.step()does not callunscale_. It calls_check_inf_per_devicewhile the stage is still READY (torch/amp/grad_scaler.py,step, v2.13.0 lines 444-445), andShardedGradScaleronly all-reduces insideunscale_(torch/distributed/fsdp/sharded_grad_scaler.py lines 235-283). On 2 gloo ranks with an inf on rank 1 only, rank 1 skips the step and backs its scale off to 27, while rank 0 steps and keeps 28. The ranks drift apart from then on.ignored_params, whichreplicate_minority_dtype(default True) keeps on the GPU. GradScaler keeps one found_inf per device, and for a fused optimizerstep()passes their SUM asoptimizer.found_inf(grad_scaler.py lines 449-464). The fused kernels skip only when found_inf == 1 (fused_adam_utils.cuhline 197 for CUDA,FusedAdam.cppline 39 for CPU) and subtract it from the step counts. With both devices overflowing the sum is 2, so the step writes non-finite weights and the counts go to -1. Stocktorch.amp.GradScalerdoes the same thing on this host (steps -1 / -1, weights non-finite), so this is inherited from PyTorch, not added by Native FSDP2 float16: fix fused AdamW crashing at the first step #14663. The scaler Unsloth installs is where it can be fixed for Unsloth runs.Fix
_ShardedGradScaler._check_inf_per_deviceruns the stock check, then all-reduces the flags over the scaler's process group the same wayShardedGradScaler.unscale_does (a CPU flag goes through the scaler device)._one_flagreplaces the per-device flags with one 0 / 1 total on the first device and zeros on the others. Every consumer (fusedstep,_maybe_opt_step,update) sums the flags, so the sum is now exactly 0 or 1.unscale_, the flags are the same as before: the old per-device clamp, 0-dim.Before / after
torch 2.13.0. Base = origin/main 0ea0473, head = this branch.
test_fsdp2_native_scaler_fused_gloo.py::test_overflow_skips_on_every_rank_without_explicit_unscale[one_rank_inf](2 gloo ranks)...::test_overflow_skips_on_every_rank_without_explicit_unscale[every_rank_inf]test_fsdp2_native_scaler_mixed_device.py::test_mixed_device_fused_adamw_skips_an_overflow_step[both-clipping](B200 + CPU, one fused AdamW)...[both-no_clipping]...[cpu-*],...[cuda-*](one device overflows)test_fsdp2_native_scaler_fused_gloo.pytests (4)-k "scale or scaler or fp16")test_fsdp2_native_scaler_fused_gloo.pyalso passes on torch 2.14.1.Not covered
_step_supports_amp_scalingand are unaffected.