Commit 5dcf0ed
authored
llama/rope: gate fp64 hf_precompute_freqs_cis on cos/sin scaling (pytorch#19308)
### Summary
a79521b ("Add LongRoPE support and fp64 RoPE precompute for Phi-3 /
Phi-4 family") unconditionally moved hf_precompute_freqs_cis to fp64
cos/sin precompute with a final cast to fp32. That works for the Phi-4
device validation that motivated the commit, but it broke
test_static_attention.py::test_within_transformer on the Linux unittest
runners (pull, pull-editable, trunk-release have been 100% red since the
commit landed).
The test compares mha_transformer (built with use_hf_rope=False, taking
the pure-fp32 precompute_freqs_cis path) against static_transformer
(built with use_hf_rope=True, taking hf_precompute_freqs_cis) at
rtol=1e-3, with shared weights. Before a79521b, both paths produced
bit-identical fp32 cos/sin tables (verified empirically: 0/192 entries
differed). After the commit, HF cos/sin diverge from non-HF by ~1 ULP in
38/192 entries; that drift compounds across 4 transformer layers and
tips past rtol=1e-3 on the CI runners (Python 3.10, source-built torch).
Local Python 3.12 stayed just barely within tolerance, which is why
review missed it.
Gate the fp64 precompute on the property the original commit was
actually protecting: a non-trivial cos/sin scale being applied. That is
either LongRoPE active (Phi-3 / Phi-4 set short_factor and long_factor
via config) or an explicit attention_factor != 1.0 passed through. Both
cases preserve fp64; vanilla HF RoPE (Llama family, the test config)
goes back to fp32 throughout and re-establishes bit-identical agreement
with the non-HF path.
Authored with Claude Code.
### Test plan
CI1 parent 5d07ce0 commit 5dcf0ed
1 file changed
Lines changed: 20 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
161 | 173 | | |
162 | 174 | | |
163 | 175 | | |
164 | | - | |
| 176 | + | |
165 | 177 | | |
166 | 178 | | |
167 | 179 | | |
168 | 180 | | |
169 | 181 | | |
170 | 182 | | |
171 | | - | |
172 | 183 | | |
173 | 184 | | |
174 | 185 | | |
| |||
178 | 189 | | |
179 | 190 | | |
180 | 191 | | |
181 | | - | |
| 192 | + | |
182 | 193 | | |
183 | 194 | | |
184 | 195 | | |
| |||
200 | 211 | | |
201 | 212 | | |
202 | 213 | | |
203 | | - | |
204 | | - | |
| 214 | + | |
| 215 | + | |
205 | 216 | | |
206 | 217 | | |
207 | 218 | | |
| |||
0 commit comments