Repository navigation
Unify CosSinRoPE and ComplexRoPE YaRN computation - #3787
Conversation
|
|
768444b to
635cad2
Compare
|
@shuhuayu Could you please take a look at this? Thanks. |
2a7a9fb to
1379dc2
Compare
22ca5e7 to
f7ef525
Compare
tianyu-l
left a comment
There was a problem hiding this comment.
numerics CI seems broken https://github.com/pytorch/torchtitan/actions/runs/28502028306/job/84481564730?pr=3787
@shuhuayu could you make sure the main CI is healthy, and then ask for a rebase and verify this PR doesn't change numerics, and then merge?
|
I think the current main ci failed the numerics test, we can check it first and land this pr later. |
|
Got it, will wait for the main CI issue to be resolved. |
|
Seems the lint/rocm CI is not fully recovered. |
yeah, we are trying to fix it. |
CosSinRoPE's YaRN correction range assigns beta_fast/beta_slow backwards relative to the YaRN paper, HF Transformers, and torchtitan's own ComplexRoPE. gpt-oss compensated with a swapped config, hiding the bug. - CosSinRoPE._precompute_cache: swap 'low <- beta_slow' -> 'low <- beta_fast' (extrapolation), 'high <- beta_fast' -> 'high <- beta_slow' (interpolation). Follows the YaRN paper (arXiv:2309.00071, Sec 3.2: beta=32 extrapolation, alpha=1 interpolation) and HF _compute_yarn_parameters. - gpt_oss config: un-swap to the official values (beta_fast=32.0, beta_slow=1.0), matching the upstream config and passing HF's beta_fast > beta_slow validation. Numerically neutral: the two un-swaps cancel -- gpt-oss still gets low=fcd(32), high=fcd(1). qwen3/qwen3_5 use CosSinRoPE without YaRN and are unaffected.
Add a single-source-of-truth _yarn_inv_freq() function called by both ComplexRoPE and CosSinRoPE so the two cache formats compute identical frequencies. Add a 'truncate' config flag (True floors/ceils the cutoffs; False keeps fractional cutoffs). Refactor both rope classes to use it. The range is always clamped to [0,dim-1]. Numerically neutral: the helper reproduces the existing algebra exactly (same low/high, same ramp, same blend), just uses the paper-correct beta_fast/beta_slow assignment from the previous commit.
CosSinRoPE is now a pure rotation (no mscale baked into cos/sin). gpt-oss, which was the only CosSinRoPE+YaRN user, instead folds mscale^2 into its softmax_scale. Mathematically equivalent: applying mscale to cos/sin scaled q and k each by m, so the attention logit was scaled by m^2. Applying m^2 to the softmax scale produces the same logit. Verified logit-neutral to ~1e-6 on gpt-oss params.
Hi @shuhuayu — quick question on this PR. Is the main CI healthy now? Would it be possible to re-run the checks and revisit merging? Thanks! |
Merged 3d277c1 (51c197c CosSinRoPE/ComplexRoPE YaRN unify pytorch#3787 + 0d7f4ab qwen3.5 spmd block / dsv3 compile fix pytorch#3901). No replays: agpt/moe diff empty. RoPE change is numerically neutral for us -- agpt uses cos_sin with scaling=none (never hits the modified YaRN branch), never sets mscale (removed field is a no-op), and moe's mscale is on Attention.Config not RoPE.Config. sync_smoke VERDICT ok (job 12470359), all 3 configs bit-identical to 64th-67th syncs.
### 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)
Background
ComplexRoPEandCosSinRoPEare the two RoPE variants in torchtitan:ComplexRoPEuses an interleaved complex cache (DeepSeek V3, LLaMA 3, Qwen3.5);CosSinRoPEuses a concatenated cos/sin cache withrotate_halfapplication (GPT-OSS, Qwen3). Both implement YaRN ("NTK-by-parts") for context-length extension, but they drifted apart in following ways:beta_fast/beta_slowassignment —CosSinRoPEmappedlow ← beta_slow,high ← beta_fast, the inverse of the YaRN paper convention and ofComplexRoPE. GPT-OSS's config compensated with swapped values, hiding the inconsistency from users.Embedded rope
mscale—CosSinRoPEmultiplied 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; otherrotate_halfconsumers 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
CosSinRoPEbeta_fast/beta_slowto the YaRN paper/HF conventionSwap
low ← beta_slowtolow ← beta_fast(extrapolation),high ← beta_fasttohigh ← beta_slow(interpolation). Un-swap GPT-OSS config to the officialbeta_fast=32.0, beta_slow=1.0. Numerically neutral (the two un-swaps cancel).2. Extract a shared
_yarn_inv_freqhelper +truncateflagA single source-of-truth function called by both classes. Add a
truncateconfig flag (floor/ceil for DeepSeek; fractional for GPT-OSS, which usestruncate=False). Always clamps to[0, dim-1].3. Make
CosSinRoPEa pure rotation; let GPT-OSS handle mscale in its attentionRemove
mscalefromCosSinRoPE._precompute_cache(and the unusedmscalefield fromRoPE.Config). GPT-OSS foldsmscale²into itssoftmax_scale— mathematically equivalent. The rope is now modular: each model's attention owns its own mscale.References
precompute_freqs_cis—low = floor(fcd(beta_fast)),high = ceil(fcd(beta_slow))ntk_alpha=1, ntk_beta=32;low ← ntk_betain rope