Repository navigation
Context parallel: compiled layers follow each packed row under Ulysses - #16733
Conversation
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.
Codex Review SummaryThis 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. |
[skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
…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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
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_kwargsreturns its cached entry whenentry["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'scu_seqlensfor every later row. A minimal repro withbackend="eager"gives 4 stale calls out of 6, so this is dynamo, not Inductor.Change
ContextParallelManager._ulysses_scopebuilds this micro-batch's packed metadata entry eagerly, outside any compiled region, right after it readspacked_seq_lengths, as the fast forwards do.Testing
New
test_compiled_layers_follow_each_packed_rowintests/test_cp_ulysses_generic_gloo.py: compiled layers, two packed rows with the same shape and different documents, cp 2 vs one process.embed_tokensgrad off on documents [3, 9, 4])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):
The CP suite passes on head: 200 passed, 2 skipped.
Not covered