Commit 51c197c
authored
Unify CosSinRoPE and ComplexRoPE YaRN computation (#3787)
### Background
`ComplexRoPE` and `CosSinRoPE` are the two RoPE variants in torchtitan:
`ComplexRoPE` uses an interleaved complex cache (DeepSeek V3, LLaMA 3,
Qwen3.5); `CosSinRoPE` uses a concatenated cos/sin cache with
`rotate_half` application (GPT-OSS, Qwen3). Both implement YaRN
("NTK-by-parts") for context-length extension, but they drifted apart in
following ways:
1. **`beta_fast`/`beta_slow` assignment** — `CosSinRoPE` mapped `low ←
beta_slow`, `high ← beta_fast`, **the inverse of** the YaRN paper
convention and of `ComplexRoPE`. GPT-OSS's config compensated with
swapped values, hiding the inconsistency from users.
2. **Embedded rope `mscale`** — `CosSinRoPE` multiplied the YaRN
attention-magnitude factor (`0.1·log(factor)+1`) into its cos/sin cache,
coupling the rope to the attention. GPT-OSS needed this; other
`rotate_half` consumers may not. The rope should be a pure rotation,
with mscale applied where each model's attention expects it.
This PR makes the two classes share one YaRN implementation, agreeing
exactly when both enter their respective YaRN (or non-YaRN) branches.
### Commits
**1. Align `CosSinRoPE` `beta_fast`/`beta_slow` to the YaRN paper/HF
convention**
Swap `low ← beta_slow` to `low ← beta_fast` (extrapolation), `high ←
beta_fast` to `high ← beta_slow` (interpolation). Un-swap GPT-OSS config
to the official `beta_fast=32.0, beta_slow=1.0`. Numerically neutral
(the two un-swaps cancel).
**2. Extract a shared `_yarn_inv_freq` helper + `truncate` flag**
A single source-of-truth function called by both classes. Add a
`truncate` config flag (floor/ceil for DeepSeek; fractional for GPT-OSS,
which uses `truncate=False`). Always clamps to `[0, dim-1]`.
**3. Make `CosSinRoPE` a pure rotation; let GPT-OSS handle mscale in its
attention**
Remove `mscale` from `CosSinRoPE._precompute_cache` (and the unused
`mscale` field from `RoPE.Config`). GPT-OSS folds `mscale²` into its
`softmax_scale` — mathematically equivalent. The rope is now modular:
each model's attention owns its own mscale.
### References
- YaRN paper: [arXiv:2309.00071](https://arxiv.org/abs/2309.00071) (§3.2
NTK-by-parts, §3.3 attention temperature)
- DeepSeek-V3.2:
[`precompute_freqs_cis`](https://huggingface.co/deepseek-ai/DeepSeek-V3.2/blob/c69397e/inference/model.py#L324-L402)
— `low = floor(fcd(beta_fast))`, `high = ceil(fcd(beta_slow))`
- GPT-OSS official: [`ntk_alpha=1,
ntk_beta=32`](https://github.com/openai/gpt-oss/blob/d21f34e/gpt_oss/torch/model.py#L28-L29);
[`low ← ntk_beta` in
rope](https://github.com/openai/gpt-oss/blob/d21f34e/gpt_oss/torch/model.py#L85-L131)1 parent 0d7f4ab commit 51c197c
3 files changed
Lines changed: 79 additions & 78 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
42 | 85 | | |
43 | 86 | | |
44 | 87 | | |
| |||
63 | 106 | | |
64 | 107 | | |
65 | 108 | | |
66 | | - | |
| 109 | + | |
67 | 110 | | |
68 | 111 | | |
69 | 112 | | |
| |||
175 | 218 | | |
176 | 219 | | |
177 | 220 | | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
216 | 229 | | |
217 | | - | |
218 | | - | |
219 | 230 | | |
220 | 231 | | |
221 | 232 | | |
| |||
270 | 281 | | |
271 | 282 | | |
272 | 283 | | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | 284 | | |
277 | 285 | | |
278 | 286 | | |
279 | 287 | | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
295 | 296 | | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | 297 | | |
307 | | - | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
308 | 301 | | |
309 | 302 | | |
310 | 303 | | |
311 | 304 | | |
312 | 305 | | |
313 | | - | |
314 | | - | |
| 306 | + | |
| 307 | + | |
315 | 308 | | |
316 | 309 | | |
317 | 310 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
283 | 283 | | |
284 | 284 | | |
285 | 285 | | |
286 | | - | |
287 | | - | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
288 | 289 | | |
289 | 290 | | |
290 | 291 | | |
| |||
326 | 327 | | |
327 | 328 | | |
328 | 329 | | |
329 | | - | |
330 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
331 | 333 | | |
332 | 334 | | |
333 | 335 | | |
| |||
369 | 371 | | |
370 | 372 | | |
371 | 373 | | |
372 | | - | |
373 | | - | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
374 | 377 | | |
375 | 378 | | |
376 | 379 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| |||
0 commit comments