Skip to content

Pipeline parallel: keep the frozen-prefix skip off on later stages - #18745

Merged
danielhanchen merged 3 commits into
mainfrom
pp-gemma4-drift
Nov 21, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
pp-gemma4-drift

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Problem

Under pipeline parallel, the frozen-prefix skip from #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 Pipeline parallel: build Gemma-4 per-layer inputs in the source table's dtype (fp16 step-1 grad blowup) #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.

…arlier stages got no gradient)

On a pipeline parallel stage after the first, the decoder layer list starts with
pass-through stand-ins for the layers other stages own. They have no parameters,
so the frozen-prefix skip (#18442) saw the stage's first own layer as the first
trainable one and cut there: the hidden states the stand-ins return (the previous
stage's activations) became a detached leaf, and no gradient reached any earlier
stage. Only the last stage trained.

Gemma-4 E4B 4-bit, PP=2 on 2x T4: step-1 grad norm 2.78 against 5.12 on one GPU,
losses 0.83 above one GPU by step 9 (E2B 1.05 by step 5). DDP, FSDP2 and one GPU
train layer 0, so the skip never engaged there.

The stand-in now carries `_unsloth_pipeline_stand_in`, and the skip's plan returns
None for a stack that holds one. One process and the other parallel modes are
unchanged.

tests/test_pp_frozen_prefix_gloo.py (CPU, gloo): PP=2 GPipe and 1F1B with
gradient checkpointing and the skip installed, every gradient and loss against one
process. Main: rank 1 cuts 8 forwards, gradient error 2.11; this branch: no cut,
4.4e-4.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-21T09:07:37.433590Z f252232 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. Delightful!

Reviewed commit: cd3def8228

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

…pins the skip on and checks torch pipelining first
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 94b4b7a13a

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

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

ℹ️ 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 ed74b59 into main Nov 21, 2026
@danielhanchen
danielhanchen deleted the pp-gemma4-drift branch November 21, 2026 10:24
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