Repository navigation
Pipeline parallel: build Gemma-4 per-layer inputs in the source table's dtype (fp16 step-1 grad blowup) - #18294
Merged
Merged
Conversation
… under forced float32 The stage's per-layer table slice returned rows in the weight dtype and _ple_project cast the per-layer inputs to the projection dtype, so on float16 weights (forced float32, T4) every layer's per-layer input was summed and scaled in float16 while one GPU keeps it in float32 (~3e-4 relative per layer). On Gemma-4-E2B 4-bit PP=2 on 2x T4 that moved the step-1 gradient norm from 12.1 to 27. The slice now returns the dtype the table itself returns and the sum follows the model's own project_per_layer_inputs (no cast).
Member
Author
|
@codex review |
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. |
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
Stanley00
pushed a commit
to stanley-fork/unsloth
that referenced
this pull request
Nov 21, 2026
…nslothai#18745) ## Problem Under pipeline parallel, the frozen-prefix skip from unslothai#18442 cuts the graph at the first activation the later stage receives from the stage before it. Every stage except the last then trains nothing. On Kaggle 2xT4, Gemma-4 PP=2 losses drift from the 1 GPU run after the warmup steps. E4B 4-bit is 0.81 off at step 6 and E2B 4-bit 0.91 off at step 5. Root cause: - On stage 1, the layers that stage 0 owns are `_PPPassThrough` stand-ins with no parameters. - `_frozen_prefix._plan` therefore sees stage 1's first own layer as "the first trainable layer" and detaches its input. That input is the activation from stage 0. - The gradient never reaches stage 0, so its decoder-layer LoRA (19.17M of 34.88M trainable parameters on E4B) never updates. - 1 GPU, DDP and FSDP2 train layer 0, so the skip never engages there. That is why only PP drifted. - The step-1 gradient norm shows it directly: 2.78 under PP vs 5.12 on 1 GPU (E4B 4-bit). ## Fix - `_PPPassThrough` carries `_unsloth_pipeline_stand_in = True`. - `_plan` makes no cut, on any layer stack, while the model holds a stand-in. A later stage would otherwise cut at the activations it receives, and a non-last stage leaves its forward by raising, past the hook cleanup. This works whether the stages are split before or after `get_peft_model`. - Off the pipeline path nothing changes. ## Results Kaggle 2xT4, fp16, unsloth main 15a985f / zoo 9b3fccb, 25 steps, same harness / data / seed. Each cell is the max over steps of |PP loss - median 1 GPU loss|, all from one kernel: | | 1 GPU A/A noise | main PP | this PR PP | step-1 grad_norm, 1 GPU / main / this PR | | --- | --- | --- | --- | --- | | Gemma-4 E4B 4-bit | 0.0165 | 0.810 | 0.0085 | 5.12 / 2.78 / 4.68 | | Gemma-4 E2B 4-bit | 0.0227 | 0.914 | 0.219 (step 4) | 94.3 / 6.50 / 11.85 | - With the fix, the skip makes 0 cuts on both ranks. On main, rank 1 cut 200 forwards. - E2B still sits 0.219 off at step 4. By step 25 the gap is about 0.04. Its 1 GPU arm has a step-1 gradient spike (grad_norm 94.3), which looks like the fp16-sensitive spot from unslothai#18294 on the 1 GPU side. That is separate from this fix and was not isolated here. - Ruled out on the same kernels: - the fp16 scaler: PP with `UNSLOTH_RETURN_LOGITS=1` (no fused CE) drifted the same; - the 1F1B default: GPipe drifted the same; - the NF4 dequant cache: it never engaged. ## Tests - `tests/test_pp_frozen_prefix_gloo.py`: tiny Gemma-4, PP=2 over CPU gloo, GPipe and 1F1B, against one process. It fails on main (rank 1 cuts 8 forwards, gradient error 2.11) and passes here (0 cuts, gradients within 4.4e-4). - Existing frozen-prefix and PP suites: 107 passed / 1 skipped, the same on main and here. - On transformers 4.57.6 the new file skips (no Gemma-4 there). The other frozen-prefix suites pass. ## Not covered - The GPU proof ran the two changed files overlaid on a main install, not an install of this branch. - Only Gemma-4 E2B / E4B 4-bit, fp16 on T4, PP=2 ran on GPU. Not run: bf16, 16-bit or larger models, PP above 2, and several stages per GPU. - Under PP the skip is now off on every stage. A PP run that trains only the top layers loses the skip's saving there.
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
Pipeline parallel on Gemma 4 E2B / E4B computed the per-layer inputs (PLE) in a different precision from one GPU when the model runs in forced float32 (float16 weights, the T4 path):
_PPLayerTableSlice.forwardreturned the table rows in the weight dtype (float16), while Unsloth Zoo's forced float32 patch makes the realGemma4TextScaledWordEmbeddingreturn float32._ple_projectthen cast the per-layer inputs to the projection dtype before(projection + per_layer_inputs) * per_layer_input_scale, so the sum and the scale ran in float16. The model's ownproject_per_layer_inputshas no cast and adds in float32.Every layer's per-layer input on a PP stage was therefore rounded by about 3e-4 relative compared with one GPU. Gemma-4-E2B has positions where a small perturbation is amplified sharply on T4 (row 0, token 845 here: the hidden-state difference grew from 7.7e-3 after layer 7 to 0.73 after layer 8). The PP-only rounding pushed that position over the edge: on 2x T4 the step-1 gradient norm was 27.0 with PP=2 against 12.1 on one GPU, and the PP loss still trailed one GPU at step 3 (8.687 vs 8.647).
Fix
_embedding_output_dtype, already used for the embedding placeholder), upcasting before the scale exactly as the forced float32 embedding does. Unpatched tables return the weight dtype as before._ple_projectadds the projection and the per-layer inputs without a cast, asproject_per_layer_inputsdoes.bf16 / fp32 models are unchanged (the table output dtype equals the weight dtype and the sum dtype is the same as before).
Evidence
Kaggle 2x T4, unsloth/gemma-4-E2B-it-unsloth-bnb-4bit, LoRA r16, seq 1024, global batch 8 (bs 4, GA 2), PP=2 1F1B with 4 micro-batches, forced float32, default GradScaler, base = main 3860855 / zoo 0c64a7a0b, head = this branch's
pp_native.pyoverlaid, same kernel:Base PP=2 step 1 was 26.98 to 27.07 in 13 separate base runs, including CUDA_LAUNCH_BLOCKING=1, gloo instead of NCCL, per-rank compile caches, double buffering off, torch's own checkpointing, one visible GPU per rank and GradScaler starts of 256 and 4096, so it is not a race, a communication issue, the checkpoint offload or the loss scale. One GPU stays at 12.09 to 12.24 across the same variations.
Per-layer input parity at the decoder layer boundary (step 1, first micro-batch, same row on PP stage 0 and one GPU, relative difference):
New test
tests/test_pp_gemma4_ple_forced_float32.py(CPU, no process group, about 1 s) builds a tiny float16 Gemma 4 decoder whose per-layer table returns float32 like the forced float32 patch: 3 failed / 1 passed on main, 4 passed on this branch.tests/test_pp_gemma4_per_layer_gloo.pypasses on both (10 passed).Not covered