Repository navigation
fix: preserve shared upstream gradients in RoPE backward - #12528
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Resolve the conflict with unslothai#12171 in Fast_RoPE_Embedding.backward: keep main's _rope_rows launch and give it the private contiguous clone of dY.
|
RoPE backward no longer modifies the incoming gradient, so a gradient shared between outputs (for example What changed
On a B200 (torch 2.11 + cu128, Triton 3.6), a shared-gradient probe fails on main and passes on The cost is one extra transient gradient copy per layer in eager backward. On the same machine it was not measurable: a RoPE forward+backward benchmark over seven model shapes moved by a median of +0.1% with no change in peak memory, and LoRA training on Qwen2.5-0.5B, Llama-3.2-1B, Qwen3-0.6B, Gemma-2-2B and Mistral-7B matched main in peak memory and throughput, with losses bit-identical where training is deterministic and otherwise within run-to-run noise. Multi-GPU training and ROCm/XPU were not tested. A regression test for the shared-gradient case would be a good follow-up. Fork CI (Etherll#114) passed 20 checks. The red checks come from two failures that also happen on main: |
|
AMD confirmation at |
|
Confirmed the backward paths in rope_embedding.py rotate a contiguous upstream gradient in place, so a gradient shared between outputs comes back corrupted, and the private copy fixes it. Will get this reviewed, and a regression test for the shared-gradient case would be welcome. |
Independent review and CUDA evidenceReviewed current upstream On Linux x86_64 with an NVIDIA GeForce RTX 5060 Ti, Python 3.12.13, PyTorch 2.12.1+cu130, and Triton 3.7.1:
Full command-level evidence is recorded in the mirror evidence comment. Review convergence completed on the tested mirror head with no findings. |
mahiatlinux
left a comment
There was a problem hiding this comment.
LGTM! Ready to merge, all manually checked.
RoPE backward can silently corrupt gradients when multiple outputs receive the same contiguous upstream gradient, for example
(q + k).backward(g).contiguous()returns the original tensor in this case, and the QK path also reuses contiguous gradients directly. The in-place rotation then changes both the caller's gradient and the gradient seen by another branch.Give the three backward implementations private buffers before their in-place updates. The fast paths use contiguous clones, including for expanded or strided inputs. The slow path clones before the existing multiply/addcmul operations, preserving their dtype and rounding behavior when half-precision gradients use FP32 cos/sin. Forward's in-place behavior and the Triton arithmetic are unchanged.
This extends the stride-zero fix in #4233 to shared dense gradients. The existing dense-gradient test supplies separate
g.clone()tensors to the two outputs, which avoids this case. PyTorch's custom autograd guidance requires backward implementations to leave incoming gradients unmodified.Reproduction
With a CUDA-enabled Unsloth environment, this assertion fails on main and passes with this change:
Validation
RTX A1000 Laptop GPU, PyTorch 2.12.1+cu130, Triton 3.7.1. Real autograd and CUDA kernels were loaded through an isolated harness that bypasses package-level model initialization.
test_rope_backward_matches_referenceparameter combinations passed through the isolated harness.git diff --checkpassed. No test files are included in this PR.Already-contiguous gradients now incur a copy. Full-model training throughput, peak memory and multi-GPU execution were not measured.