Skip to content

Pipeline parallel: build Gemma-4 per-layer inputs in the source table's dtype (fp16 step-1 grad blowup) - #18294

Merged
danielhanchen merged 1 commit into
mainfrom
gemma4-pp-fp16-step1
Nov 18, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
gemma4-pp-fp16-step1

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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.forward returned the table rows in the weight dtype (float16), while Unsloth Zoo's forced float32 patch makes the real Gemma4TextScaledWordEmbedding return float32.
  • _ple_project then 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 own project_per_layer_inputs has 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

  • The table slice returns the dtype the source table returns (_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_project adds the projection and the per-layer inputs without a cast, as project_per_layer_inputs does.

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.py overlaid, same kernel:

Arm Step 1 loss Step 1 grad norm Step 2 grad norm Step 3 loss
1 GPU, base 9.546 12.09 11.23 8.647
PP=2, base 9.543 27.03 11.26 8.687
1 GPU, head 9.546 12.09 11.23 8.647
PP=2, head 9.544 12.20 11.25 8.649

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):

Base Head
Per-layer input, layers 0 to 12 2.4e-4 to 3.7e-4 0 (bitwise)

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.py passes on both (10 passed).

Not covered

  • Gemma-4-E4B and the 12B / 26B variants were not run; the code path is the same table slice and projection.
  • The local B200 (sm100) with T4 numerics emulated never showed the step-1 gap (base PP 12.09 vs one GPU 12.07), so the end-to-end flip is from T4 only; on B200 the change shows as the per-layer input parity above.

… 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).
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T09:13:24.411208Z ecb5fd6 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: ecb5fd6bbd

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

@danielhanchen
danielhanchen merged commit 2628a90 into main Nov 18, 2026
@danielhanchen
danielhanchen deleted the gemma4-pp-fp16-step1 branch November 18, 2026 11:53
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.
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