Skip to content

Context parallel gather: correct gradients under torch.compile, and a guard for in-place edits of returned tensors - #15231

Merged
danielhanchen merged 2 commits into
mainfrom
compile-inplace-output-guard
Oct 25, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
compile-inplace-output-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What

_GatherSequence.forward (context parallel, DPO / GRPO sequence gather) finished its batch > 1 path with out.copy_(buffer.movedim(0, 1)) on a tensor it had just allocated and then returned. Under torch.compile on torch 2.11 that pattern gives the traced backward a wrong gradient for the output, the same failure #14712 fixed in the cross entropy kernel. This PR returns buffer.movedim(0, 1).contiguous() instead: same values, same one fresh allocation, nothing mutated in place.

It also adds tests/test_autograd_inplace_outputs.py, a CPU-only AST check that fails when any torch.autograd.Function.forward under unsloth/ mutates a freshly allocated tensor it returns (+=, x.op_(...)). Writes by a kernel into the buffer and in-place edits of inputs stay allowed: both were measured correct under compile.

Evidence

Two gloo ranks on CPU, eager vs torch.compile gradient of (gather(x) * w).square().sum(), relative error:

torch arm batch 1 batch 2
2.11 main 0.0 1.0 (both ranks)
2.11 this PR 0.0 0.0
2.13 main 0.0 0.0
2.13 this PR 0.0 0.0

Minimal autograd.Function patterns on GPU (L4 / A100), compiled vs eager grad, relative error:

forward ends with 2.10 2.11 2.12
kernel write into empty, returned 0 0 0
kernel write, then out-of-place op 0 0 0
kernel write, then += / masked_fill_ 0 1.0 0
empty_like then copy_ 0 1.0 0
new_empty then return out.copy_(...) 0 1.0 0
input mutated in place 0 0 0

The guard test fails on main (flags context_parallel.py:1046) and on the cross entropy kernel from before #14712 (flags losses += logsumexp and losses.masked_fill_), and passes here. tests/test_context_parallel_rl.py: 22 passed.

Scan of the rest: UnslothFusedLoss in unsloth-zoo has the same shape but is already wrapped in torch.compiler.disable (unsloth-zoo#1482); the MoE grouped GEMM only writes its output through the Triton kernel.

@danielhanchen danielhanchen self-assigned this Oct 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T08:51:42.675964Z 815ac54 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. Chef's kiss.

Reviewed commit: 815ac54c96

ℹ️ 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 327dc5d into main Oct 25, 2026
3 of 29 checks passed
@danielhanchen
danielhanchen deleted the compile-inplace-output-guard branch October 25, 2026 09:40
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