Repository navigation
Context parallel gather: correct gradients under torch.compile, and a guard for in-place edits of returned tensors - #15231
Merged
Conversation
…he gradients; guard the pattern
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
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. |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_GatherSequence.forward(context parallel, DPO / GRPO sequence gather) finished its batch > 1 path without.copy_(buffer.movedim(0, 1))on a tensor it had just allocated and then returned. Undertorch.compileon 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 returnsbuffer.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 anytorch.autograd.Function.forwardunderunsloth/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.compilegradient of(gather(x) * w).square().sum(), relative error:Minimal autograd.Function patterns on GPU (L4 / A100), compiled vs eager grad, relative error:
empty, returned+=/masked_fill_empty_likethencopy_new_emptythenreturn out.copy_(...)The guard test fails on
main(flagscontext_parallel.py:1046) and on the cross entropy kernel from before #14712 (flagslosses += logsumexpandlosses.masked_fill_), and passes here.tests/test_context_parallel_rl.py: 22 passed.Scan of the rest:
UnslothFusedLossin unsloth-zoo has the same shape but is already wrapped intorch.compiler.disable(unsloth-zoo#1482); the MoE grouped GEMM only writes its output through the Triton kernel.