Skip to content

Context parallel: compiled layers follow each packed row under Ulysses - #16733

Merged
danielhanchen merged 4 commits into
mainfrom
dh/cmp-pack-ulysses-compiled-packed
Nov 5, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
dh/cmp-pack-ulysses-compiled-packed

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Under context parallel with the generic Ulysses path, packed or padding-free batches trained the wrong thing once the decoder layers were compiled. Step 1 matched one process, then every later row attended with the first row's document boundaries: gradients at step 3 were off by 50-330% and the loss drifted from step 2.

get_packed_info_from_kwargs returns its cached entry when entry["seq_lengths"] is seq_lengths (unsloth/utils/packing.py). torch.compile evaluates that identity check once while tracing and does not guard it. The fast forwards rebuild the entry eagerly before their layers run, but generic Ulysses only ever built it inside the compiled layer, so a layer traced on one row kept that row's cu_seqlens for every later row. A minimal repro with backend="eager" gives 4 stale calls out of 6, so this is dynamo, not Inductor.

Change

ContextParallelManager._ulysses_scope builds this micro-batch's packed metadata entry eagerly, outside any compiled region, right after it reads packed_seq_lengths, as the fast forwards do.

Testing

New test_compiled_layers_follow_each_packed_row in tests/test_cp_ulysses_generic_gloo.py: compiled layers, two packed rows with the same shape and different documents, cp 2 vs one process.

case base head
qwen3, sdpa FAIL (embed_tokens grad off on documents [3, 9, 4]) PASS
gemma3, eager PASS PASS

CPU gloo composition run (2 ranks, cp 2, tiny Qwen3 / Mistral with a window / Gemma-2 with window and softcap / Gemma-3 with a replicated kv head / gpt-oss with sinks; TRL packing and padding-free; loss, gradients at steps 1 and 3 and parameters after step 3 vs one process):

base head (with the separate graph-break fix)
packed / padding-free cells, compiled 0 of 19 pass 19 of 19 pass
eager and varlen cells pass pass

The CP suite passes on head: 200 passed, 2 skipped.

Not covered

  • Unsloth's fast Llama Ulysses path and GPU block compile with packing were not run here (CPU only).
  • With this change alone, compiled packed attention still graph-breaks once per call; that is a separate fix.

Generic Ulysses reads packed document boundaries through
get_packed_info_from_kwargs inside the attention call. With the decoder
layers compiled (CPU UNSLOTH_CPU_COMPILE=1, block compile), the cache hit
in that helper compares the cached tensor to the row's by identity, which
dynamo does not guard: the graph traced on an earlier row kept serving
that row's cu_seqlens, so from the second step on every packed row was
masked with the first row's documents (CPU gloo, tiny Qwen3, cp 2: grads
at step 3 off by 130% vs one process, losses off from step 2).

The fast forwards build this entry eagerly before their layers run; the
Ulysses scope now does the same once per micro-batch, outside any
compiled region, so the compiled layers read the current row.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 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-11-05T06:51:24.122625Z 5b612b3 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: b986702fc1

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

…tadata raises

The eager packed-metadata build in _ulysses_scope ran after the manager was
installed but before the try, so a raise there (empty or 2D packed_seq_lengths)
left _ACTIVE_MANAGER set process-wide. Build it inside the try.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 5b612b3fc8

ℹ️ 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 e464a30 into main Nov 5, 2026
@danielhanchen
danielhanchen deleted the dh/cmp-pack-ulysses-compiled-packed branch November 5, 2026 08:28
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