Repository navigation
fix(models): apply YaRN RoPE in DeepSeek V3.2 attention and indexer - #3279
Merged
Merged
Conversation
DeepSeek V3.2 rotated the MLA q_pe / k_pe and the DSA indexer queries and keys with plain RoPE, reading rope_scaling only for the softmax-scale correction. The checkpoints ship YaRN (factor 40 over 4096 original positions), so past 4096 tokens the low-frequency pairs rotated up to 40 times too fast and fp16 perplexity rose from 7.3 to over 1400. rope_scaling is now the shared RopeScalingSpec, and a per-model DsaRope resolves it through RopeScalingKind::resolve once, handing a duplicate to every MLA block and indexer, at all six call sites including batched decode. It applies upstream's YaRN magnitude multiplier to the rotated dims only (1.0 for the shipped config). An absent or default block keeps the exact plain fast_rope call. glm_moe_dsa maps its whole rope_parameters block, as upstream does, and the --rope-* overrides now reach both families. Regression tests in deepseek_v32_rope_tests.rs check the cached k_pe and indexer keys, and the indexer queries, against a yarn_rope_setup + fast_rope_with_freqs reference across original_max_position_embeddings; three of them fail with the fix reverted. Long-context perplexity on DeepSeek-V3.2-4bit is pending on an M3 Ultra. Closes #3178
The #3178 tests observed the cached k_pe and the indexer queries and keys, but not the MLA q_pe, which is never cached: reverting both q_pe sites (single-sequence forward and batched decode) to plain RoPE left every deepseek_v32 and glm_moe_dsa test green. The new test observes the query through RoPE's relative-position property. With q_pe and k_pe on one table, two tokens attend identically at positions 0, 1 and at 128, 129 (eight times the tiny config's original context); a query on a different table than its keys breaks that. It checks a two-token prefill and a batched decode with the two rows 128 positions apart. On the fix the drift is below 1e-5; with either q_pe site reverted to plain RoPE it is 1.45e-3, against a 1e-4 tolerance. Refs #3178
Member
Author
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
Checked against upstream main |
Member
Author
Security and Performance ReviewNo CRITICAL or HIGH findings, so no fix commits. Branch head stays at 6528813. Config parsing (untrusted
|
… RoPE DsaRope::from_args built the YaRN table from qk_rope_head_dim without checking it, so a zero, odd, or over-wide value in a config reached the table builder and the rope kernel. It now returns an error naming the field when the width is zero, odd, or larger than the head it rotates (the narrower of index_head_dim and qk_nope_head_dim + qk_rope_head_dim), and the model, indexer and GLM loaders propagate it. Adds a focused test for the three cases and sets index_head_dim in the checkpoint-geometry test so it stays valid. Validation: cargo test --lib (deepseek_v32 glm_moe_dsa rope_utils rope_overrides) 100 passed; clippy --lib --tests -D warnings and fmt clean. Refs #3178
…ek-v32-yarn # Conflicts: # CHANGELOG.md # src/models/rope_overrides.rs
…d tests The RoPE forward tests ran decode steps without the shared MLX test lock, so under parallel test threads they bumped the process-wide MLA decode-path counters while a_forced_int4_slot_keeps_the_indexer_key_at_eight_bits measured them as a delta, which failed it once. The five forward tests now take mlx_test_guard like the other DeepSeek V3.2 test files. Refs #3178
Records the YaRN RoPE fix for the DeepSeek V3.2 attention and indexer, the review outcomes, and the long-context perplexity re-run still pending on M3 Ultra.
inureyes
added a commit
that referenced
this pull request
Nov 23, 2026
Main gained the shared `DsaRope` for the DeepSeek-V3.2 attention and indexer (#3279), which replaced the `rope_base` field the Hy4 attention read. Hy4 now rotates through the same helper, built once per model from its own `rope_parameters`: the shipped `rope_type: default` resolves to plain RoPE with no frequency table, so V3.2's YaRN cannot leak in, and the interleaved or half-split pairing stays Hy4's choice. A new test pins that bit for bit against `fast_rope` at the config's `rope_theta`. Conflicts kept both sides: CHANGELOG.md, the latent-family notes and doc list (`hy_v4` beside `longcat_flash_sparse`), and `kv_arch`, where the LongCat sparse indexer group is appended to the Hy4-aware latent group. `MLA_LATENT_CACHE_FAMILIES` is now 19 entries. The validation script records peak memory per run with `/usr/bin/time -l`. Refs #2354
4 tasks
This was referenced Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
deepseek_v32rotated the MLAq_pe/k_pe(single-sequence and batched decode) and the DSA indexer queries and keys with plain RoPE. ItsRopeScalingparsed onlytype,factorandmscale_all_dim, which fed the softmax-scale correction and nothing else. Upstream builds both RoPEs withinitialize_rope(..., scaling_config=args.rope_scaling), which for DeepSeek-V3.2 (yarn,factor40,original_max_position_embeddings4096, betas 32 / 1) isYarnRoPE. Plain RoPE agrees with that table only on the high-frequency pairs the correction ramp leaves alone (pairs 0-10 of 32); the low-frequency pairs rotated up to 40 times too fast, which is harmless within the 4096 positions the model was pre-trained on and breaks past them, matching the 7.3 / 1403 / 2080 perplexity cliff in the issue.Change
rope_scalingis now the sharedRopeScalingSpec(the whole block), and a newDsaRope(src/models/deepseek_v32_rope.rs) resolves it once per model throughRopeScalingKind::resolve/yarn_rope_setup, then hands a duplicate to every MLA block and indexer. All six call sites use it with their existingtraditionalflag. It applies upstream's YaRN magnitude multiplier to the rotated dims only (1.0 for the shipped config, sincemscale == mscale_all_dim); the softmax-scale correction is unchanged.deepseek_yarnis accepted as an alias, as upstream does.defaultone, keeps the exact plainfast_rope(.., base, 1.0, offset)call (pinned bit-exact by a test).glm_moe_dsamaps its wholerope_parametersblock (upstream setsrope_scaling = rope_parameters), so a GLM-5 YaRN block would also take effect; adefaultone is unchanged.DsaRope::from_argsrefuses aqk_rope_head_dimthat is zero, odd, or wider than the head it rotates (min(index_head_dim, qk_nope_head_dim + qk_rope_head_dim)) before the YaRN table is built, so a malformed config fails at load with an error instead of allocating an unbounded table.--rope-*/--yarn-*overrides reach these families;docs/llama-server-compat.mdis updated accordingly.Tests
src/models/deepseek_v32_rope_tests.rs: prefill and decode acrossoriginal_max_position_embeddings, and batched decode with one row on each side of it, must leave YaRN-rotatedk_peand indexer keys in the cache, checked against a reference built fromyarn_rope_setup+fast_rope_with_freqsand against plain RoPE; indexer queries and keys likewise; the checkpoint-geometry table is pinned to values computed independently from upstream'sYarnRoPEformula in Python; the magnitude multiplier touches only the rotated dims; a YaRN model's batched decode matches sequential decode; MLA queries rotate with the key table past the original context (a two-token prefill at positions 0,1 and 128,129, and a batched decode with rows 128 positions apart, must attend identically by RoPE's relative-position property; eachq_pesite reverted alone fails it at 1.45e-3 against a 1e-4 tolerance); a malformed rope width (0, 7, 14) is refused.With the six RoPE sites reverted to main (tests kept),
prefill_and_decode_cache_yarn_rotated_keys_past_the_original_context,batched_decode_caches_yarn_rotated_keys_per_rowandindexer_queries_and_keys_rotate_with_yarnfail (cachedk_peoff by up to 2.04, indexer queries by 11.4). With the fix,cargo test --lib --profile test-fast --features rocm -- deepseek_v32 glm_moe_dsa rope_utils rope_overridespasses (99 tests, run twice), andcargo clippy --lib --tests --features rocm -- -D warnings,cargo fmt --checkandscripts/ci/check_used_by.pyare clean, on ROCm gfx1151. Workspace-wide tests and clippy were not run locally.Pending on M3 Ultra
This host (ROCm gfx1151, 30 GB) cannot load DeepSeek-V3.2-4bit, so the perplexity criteria behind 8192 and 16384, the #3163 top-k overlap re-run and the update to
docs/benchmark_results/mla-quantized-latent-dsa-m3ultra-2026-11-19.mdare not done here:Out of scope
src/models/deepseek_v3.rshas the same defect on current main:DeepSeekV3Attentioncalls plainfast_rope/fast_rope_batchedforq_pe/k_peand readsrope_scalingonly for the softmax scale, while DeepSeek-V3 checkpoints carry the same YaRN block. That needs its own issue.Closes #3178