Commit 09290a3
Fix fused RoPE backward pass computing wrong gradients (#2)
_FusedRoPE.backward reused the forward kernel verbatim with the same
sin, but RoPE's forward is a rotation matrix [[c,-s],[s,c]] applied to
each (x0,x1) pair, so the correct backward is that matrix's transpose
— equivalent to the same kernel with sin negated, not reapplied
unchanged. Every gradient through the fused path was wrong whenever
sin != 0 (max abs error 3.64 in the reported repro vs 0.0 after the
fix), silently corrupting training for any model using
RotaryEmbedding(use_fused=True).
test_gradient_flows only checked grad is not None, so this shipped
undetected. Added test_gradient_correctness, which compares the fused
path's gradient against the vanilla autograd-correct path via
assert_close, and fails against the old code (85% of elements
mismatched) while passing with the fix.
Reported by @tohtana during review of deepspeedai/DeepSpeed#8052.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>1 parent 4dad48e commit 09290a3
3 files changed
Lines changed: 41 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
143 | 179 | | |
144 | 180 | | |
145 | 181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
95 | 98 | | |
96 | 99 | | |
97 | 100 | | |
| |||
0 commit comments