Skip to content

Pipeline parallel: end a non-last stage without a graph break under torch.compile - #16167

Merged
danielhanchen merged 2 commits into
mainfrom
dh/fix-pp-stage-output-compile
Nov 3, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/fix-pp-stage-output-compile

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

A non-last pipeline stage ends its forward by raising _StageOutput(hidden) from the first decoder layer it does not own; _PPStageModule.forward catches it and sends the hidden states on. _StageOutput defined its own __init__ (calling super().__init__() and storing self.hidden), and Dynamo cannot trace that constructor.

Repro on main, 2 gloo ranks, a LoRA Llama split with apply_pipeline_parallel, stage module compiled with backend = "aot_eager", dynamic = True:

  • fullgraph = True: stage 0 fails on its first forward with Dynamo does not know how to trace method __init__ of class _StageOutput.
  • fullgraph = False: training runs and matches eager (loss error 2.4e-7), but stage 0 captures 5 graphs with 6 graph breaks (5 on _StageOutput.__init__, 1 on super().__init__()). The last stage is 1 graph, 0 breaks.

Change

_StageOutput no longer defines __init__; the hidden states travel as the exception's first argument (raise _StageOutput(hidden) is unchanged, the handler reads stop.args[0]). Eager behaviour is identical: it is still a BaseException, so no except Exception inside a model forward swallows it.

With the same 2-rank repro: fullgraph = True now runs on both stages, and under fullgraph = False stage 0 is 1 graph with 0 graph breaks. Loss and gradient errors against the one-process model are unchanged (2.4e-7 loss, 2.7e-7 max grad on stage 0, 1.2e-7 on stage 1).

Tests

  • tests/test_pp_native_gloo.py::test_non_last_stage_compiles_as_one_graph (a stage-0 _PPStageModule whose second layer is a pass-through, compiled with fullgraph = True; output and gradients must equal eager with 0 graph breaks): main 1 failed, this branch 1 passed.
  • tests/test_pp_native_gloo.py, tests/test_pp_native_fp16_skip_gloo.py, tests/test_pp_fsdp2_gloo.py: 47 passed on this branch.

Not covered

CPU only (gloo, eager / aot_eager backends): no GPU, NCCL or Inductor run. Unsloth does not compile the stage module itself; this matters when the user compiles it.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T16:58:40.189172Z cfc632f 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 Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-30T16:45:09.820696Z
We couldn’t complete this review. Comment @codex review to try again.
cfc632f PR opened
ℹ️ 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. Nice work!

Reviewed commit: cfc632f6a4

ℹ️ 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 self-assigned this Nov 2, 2026
@unslothai unslothai deleted a comment from danielhanchen Nov 3, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 3, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 3, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 3, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 3, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 3, 2026
@danielhanchen
danielhanchen merged commit fa2a5e7 into main Nov 3, 2026
@danielhanchen
danielhanchen deleted the dh/fix-pp-stage-output-compile branch November 3, 2026 07:34
danielhanchen added a commit that referenced this pull request Nov 4, 2026
…shards (#16634)

## Summary

`Repo tests (CPU, gloo-tp-pp)` is at its 2100 s cap. On #16618, which
only touches the Studio MLX worker, it was interrupted with the suite
almost done:

```
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
174 passed, 7 skipped, 79 warnings, 320 subtests passed in 2088.59s (0:34:48)
##[error]Process completed with exit code 124.
```

The other recent runs of that job, across the last 20 PRs, took 30.5 to
36.2 minutes end to end, setup included, so a slightly slower runner is
enough to tip it over. When this shard was created (#15951 split, #16447
comment) tensor plus pipeline parallel were about 1397 s. Pipeline
parallel has grown a lot since then: every
`torch.distributed.pipelining` schedule (#15655), per-stage sharded
loading (#15656), no graph break on a non-last stage (#16167), and
explicit-unscale overflow agreement (#16417). Nothing is wrong with
those tests. The shard just outgrew its slot, the same way `gloo` did in
#15951 and #16447.

## Change

- `.github/workflows/studio-backend-ci.yml`: `gloo-tp-pp` becomes two
shards with the same settings (`-n 4`, the 2100 s cap, the marker
filter):
  - `gloo-tp`: `tests/test_tp_n*gloo*.py` (2 files);
  - `gloo-pp`: `tests/test_pp*gloo*.py` (8 files).
- `tests/studio/test_backend_ci_parallel_isolation.py`:
`test_the_command_scan_sees_the_parallel_run_and_the_serial_steps` pins
the number of parallel pytest runs. It asks to be told when a shard is
added, so its counts go from 12 to 13 parallel runs and from 9 to 10
repo-root shards.

The selections are the old one split in two, so every file still runs
exactly once.

## Testing

- `tests/test_ci_repo_cpu_shards.py`: 15 passed. With `test_pp*gloo*`
narrowed to `test_pp_[!n]*gloo*` as a sabotage,
`test_no_file_is_dropped_or_run_twice` fails, so a dropped file is still
caught.
- `tests/studio/test_backend_ci_parallel_isolation.py`,
`tests/test_ci_repo_cpu_shards.py` and the workflow and cache-budget
guards under `tests/studio`: 164 passed.
- The two new shards' run times on this PR's own CI are the real check.
Both should finish well under the cap.
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