Skip to content

fix(models): apply YaRN rope_scaling in DeepSeek-V3 and LongCat MLA - #3306

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-3295-mla-yarn-rope
Nov 23, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-3295-mla-yarn-rope

Conversation

@inureyes

@inureyes inureyes commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Closes #3295

DeepSeekV3Attention and the LongCat ngram MLA rotated q_pe / k_pe with plain base^(2i/d) frequencies whatever rope_scaling declared, so every YaRN checkpoint (DeepSeek-V3 / Kimi-K2 factor 40 over 4096, LongCat-Flash-Lite factor 10 over 32768) rotated its low-frequency pairs factor times too fast past the original context. Only the softmax-scale correction read the block.

Changes

  • New shared MlaRope (src/models/mla_rope.rs): resolves the block through RopeScalingKind::resolve (deepseek_yarn renamed to yarn, --rope-* / --yarn-* overrides honored), applies the table plus the YaRN magnitude to the rotated dims, and keeps the exact fast_rope(.., base, 1.0, offset) call when there is no block.
  • DeepSeek-V3 (deepseek_v3, kimi_k2, joyai_llm_flash, and through it Kimi-VL's text model and the V3 pipeline stage executor) and the LongCat ngram MLA resolve it once per model and duplicate it per layer. original_max_position_embeddings falls back to 4096 (no train_ctx), as upstream YarnRoPE does.
  • DsaRope (V3.2), the LongCat sparse rope and Xing4.0's opt-in table now use it, each keeping its own train_ctx (None, max_position_embeddings, max_position_embeddings). Xing4.0 still refuses linear. Youtu passes MlaRope::plain so its rotation stays plain. One plain-path change: the sparse rope's no-table rotation now follows --rope-freq-base like its table (it used rope_theta before).
  • Bilingual technical report in TECHNICAL_REPORTS/3306-mla-yarn-rope-20261123.*.md.
  • Docs: supported-models.md (V3, LongCat, Xing), the --rope-* coverage list in llama-server-compat.md, and the Used-by lists in rope_utils.rs / rope_overrides.rs, and the override error text.

Validation

Teacher-forced logit_trace against mlx-lm (references/mlx-lm 1bf8da0), with the pre-fix binary (main) as the negative arm:

Checkpoint Region Decided disagreements, this PR Pre-fix PPL this PR / ref
GigaChat3-10B-A1.8B (deepseek_v3, YaRN 64 over 4096), 8-bit 8192 positions 0 / 2774 989 / 2774 14.582 / 14.572
same past 4096 0 / 1264 971 / 1264 16.688 / 16.652
longcat-flash-lite-4bit (YaRN 10 over 32768) 36864 positions 39 / 20486 (max gap 4.5) 1709 / 20486 25.45 / 25.82
same past 32768 6 / 2446 671 / 2446 17.92 / 17.49
  • GigaChat3 is quantized locally to affine 8-bit (models/gigachat3-10b-a1.8b-mlx-8bit) because the deepseek_v3 loader does not load an unquantized MoE checkpoint; mlx-lm needed a shim to drop the MTP layer at index 26 (it hardcodes 61).
  • A single 36864-token mlx-lm forward on LongCat returns uniform logits past position 16384, so the LongCat reference is prefilled through mlx-lm's KV cache in 2048-token steps. That path agrees bit-for-bit with the single forward on the first 16384 positions, and mlxcel's one-piece and 2048-piece context prefill also agree bit-for-bit. The reference has no noise of its own (2048-step against 4096-step prefill: 0 disagreements), so the LongCat residual (gaps up to 4.5) sits above the reference's noise. It grows with position: 0 to 1 decided disagreements per 4096-position chunk below 12288, 4 to 10 from 12288 on, with no step at 32768. Its source is not identified, and the LongCat half of the first acceptance criterion is open on that point.
  • Byte-identical rows before and after: moonlight-16b-a3b-instruct-4bit (no block), xing4.0-29b-a4b-mlx-8bit, longcat-flash-lite-sparse-4bit.
  • Unit tests: tables against upstream YarnRoPE at the V3, GigaChat3 and LongCat geometries, the alias, byte-identity of the no-block path, the magnitude, a synthetic deepseek_v3 YaRN config changing q_pe past the original context, YaRN reaching every layer and batched decode, and the ngram model carrying the table. I ran the narrow selectors coverage_tests, mla_rope, deepseek_v3, deepseek_v32, xing4_0, longcat, kimi, youtu, rope_* and the stage executor, plus clippy, fmt and the CI scripts. The broad workspace gate is left to CI.
  • xing4_0_tests.rs was edited only to follow the field rename (rope_table to rope.freqs(), with_rope_table to with_rope). The assertions are unchanged.
  • Three LongCat latent tests fail only when run in parallel, with or without this change, because they assert on deltas of the shared global MLA stats counters. test(mla): make dispatch-counter assertions safe under parallel tests #3298 (now merged as test(mla): count dispatch paths per thread so tests survive parallelism #3308) addresses that.

DeepSeekV3Attention and the LongCat ngram MLA rotated q_pe / k_pe with plain frequencies whatever rope_scaling declared, so every shipped DeepSeek-V3 / Kimi-K2 / LongCat checkpoint (YaRN) rotated its low-frequency pairs factor times too fast past original_max_position_embeddings. Only the softmax scale read the block.

Promote one shared MlaRope (src/models/mla_rope.rs) that resolves the block through RopeScalingKind::resolve (deepseek_yarn renamed to yarn, --rope-* overrides honored) and applies the table with its YaRN magnitude. DeepSeek-V3 (and through it Kimi-VL, the pipeline stage executor and Xing4.0) and the LongCat ngram MLA resolve it once per model; DsaRope (V3.2), the LongCat sparse rope and Xing4.0's opt-in table move onto it, each keeping its train_ctx choice. Youtu keeps its plain rotation. A config without a block keeps the exact fast_rope call.

Validated with teacher-forced traces against mlx-lm: GigaChat3-10B-A1.8B (deepseek_v3, 8-bit) has 0 of 1264 decided disagreements past 4096 (pre-fix 971); longcat-flash-lite-4bit has 6 of 2446 past 32768 (pre-fix 671). Moonlight, Xing4.0 and LongCat-Flash-Lite-Sparse trace byte-identical before and after.

Closes #3295
The previous commit was created against a main that had moved underneath it (#3303 and others landed), so its tree reverted those changes. This commit sets the tree to current main plus only the #3295 changes; the PR's squashed diff touches only the MLA rope wiring, its tests and docs.
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata labels Nov 23, 2026
The --rope-* "reached no RoPE code path" error now lists the MLA families that honor the override, and the LongCat resolve_rope comment states upstream's initialize_rope arguments correctly. supported-models.md records that the mlx-lm LongCat reference is deterministic across prefill step sizes and that the residual decided disagreements grow with position (4 to 10 per 4096-position chunk from 12288 on) without a step at 32768. Adds the bilingual technical report for #3306.

Refs #3295
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 23, 2026
@inureyes
inureyes marked this pull request as ready for review November 23, 2026 03:52
@inureyes
inureyes merged commit 5f3562a into main Nov 23, 2026
34 checks passed
@inureyes
inureyes deleted the fix/issue-3295-mla-yarn-rope branch November 23, 2026 03:53
inureyes added a commit that referenced this pull request Nov 23, 2026
Brings in eleven commits from main. The only conflict was docs/supported-models.md, where main reworded the Xing4.0 entry next to the Hy4 one: main's Xing4.0 text and the Hy4 entry are both kept. The shared DeepSeek MLA RoPE change (#3306) and the per-thread MLA dispatch counters (#3308) merged cleanly under the Hy4 attention.

Refs #2354
inureyes added a commit that referenced this pull request Nov 23, 2026
The 39 decided disagreements left on longcat-flash-lite-4bit after #3306 are a weight difference between the two runtimes, not mlxcel drift. The checkpoint ships a fused 4-bit kv_b_proj: mlxcel splits it into dense embed_q / unembed_out, while mlx-lm quantizes both halves again (embed_q along the transposed axis). Against a reference whose split stays dense, the 36864-position trace has 4 decided disagreements, below the 5 between the bf16 and float16 references, and 2 against a float32 dense-split reference. The dtype (MLXCEL_KEEP_BF16), KV storage and attention-path arms leave the count at 39 (33 with an additive rope mask).

The mlx_lm_logit_trace.py docstring now says mlxcel converts bf16 to f16 only for unquantized checkpoints on Apple Silicon, and warns that a fused quantized kv_b_proj loads different weights in the two runtimes. supported-models.md replaces "source not identified" with the finding, and the arm tables are in docs/benchmark_results. No runtime code changes.

Closes #3318
inureyes added a commit that referenced this pull request Nov 23, 2026
The 39 decided disagreements left on longcat-flash-lite-4bit after #3306 are a weight difference between the two runtimes, not mlxcel drift. The checkpoint ships a fused 4-bit kv_b_proj: mlxcel splits it into dense embed_q / unembed_out, while mlx-lm quantizes both halves again (embed_q along the transposed axis). Against a reference whose split stays dense, the 36864-position trace has 4 decided disagreements, below the 5 between the bf16 and float16 references, and 2 against a float32 dense-split reference. The dtype (MLXCEL_KEEP_BF16), KV storage and attention-path arms leave the count at 39 (33 with an additive rope mask).

The mlx_lm_logit_trace.py docstring now says mlxcel converts bf16 to f16 only for unquantized checkpoints on Apple Silicon, and warns that a fused quantized kv_b_proj loads different weights in the two runtimes. supported-models.md replaces "source not identified" with the finding, and the arm tables are in docs/benchmark_results. No runtime code changes.

Closes #3318
inureyes added a commit that referenced this pull request Nov 23, 2026
The 39 decided disagreements left on longcat-flash-lite-4bit after #3306 are a weight difference between the two runtimes, not mlxcel drift. The checkpoint ships a fused 4-bit kv_b_proj: mlxcel splits it into dense embed_q / unembed_out, while mlx-lm quantizes both halves again (embed_q along the transposed axis). Against a reference whose split stays dense, the 36864-position trace has 4 decided disagreements, below the 5 between the bf16 and float16 references, and 2 against a float32 dense-split reference. The dtype (MLXCEL_KEEP_BF16), KV storage and attention-path arms leave the count at 39 (33 with an additive rope mask).

The mlx_lm_logit_trace.py docstring now says mlxcel converts bf16 to f16 only for unquantized checkpoints on Apple Silicon, and warns that a fused quantized kv_b_proj loads different weights in the two runtimes. supported-models.md replaces "source not identified" with the finding, and the arm tables are in docs/benchmark_results. No runtime code changes.

Closes #3318
inureyes added a commit that referenced this pull request Nov 23, 2026
…on (#3332)

Closes #3318

The 39 decided disagreements left on `longcat-flash-lite-4bit` after #3306 come from a weight difference, not from mlxcel drift. The checkpoint ships a fused 4-bit `kv_b_proj`. mlxcel splits it into dense `embed_q` / `unembed_out`. mlx-lm quantizes both halves again, and `embed_q` is quantized along the transposed axis. No runtime code changes.

## Arms (36864 positions, #3306 corpus, 2048-step cached reference prefill)

| Reference | Candidate | Per 4096-position chunk | Decided | PPL cand. / ref. |
| --- | --- | --- | --- | --- |
| mlx-lm bf16 | mlxcel main | 0 1 0 4 10 4 6 8 6 | 39 / 20486 | 25.45 / 25.82 |
| same | `MLXCEL_KEEP_BF16=1` (byte-identical rows) | 0 1 0 4 10 4 6 8 6 | 39 / 20486 | same |
| same | `MLXCEL_TRACE_KV_CACHE_MODE=fp16` (identical rows) | 0 1 0 4 10 4 6 8 6 | 39 / 20486 | same |
| same | rope term as additive mask, fused 128-wide SDPA (local experiment, not committed) | 0 0 0 1 6 5 8 8 5 | 33 / 20486 | 25.40 / 25.82 |
| mlx-lm bf16 | mlx-lm cast to float16 (dtype noise floor) | 0 0 0 1 1 0 0 1 2 | 5 / 20486 | 25.89 / 25.82 |
| mlx-lm bf16, dense split | mlxcel main | 0 0 0 2 1 0 0 0 1 | 4 / 20489 | 25.45 / 25.63 |
| mlx-lm float32, dense split | mlxcel main | 0 1 0 1 0 0 0 0 0 | 2 / 20208 | 25.45 / 25.82 |

- Arm 1 does not move because `bf16_to_f16_at_load` already keeps a quantized checkpoint's bf16 side data. The issue's premise that this checkpoint runs in f16 does not hold.
- Arm 3: `MLXCEL_TRACE_MLA_STATS=1` reports `decompressed=476` with no absorbed step, so there is no second path to force.
- The per-layer bisection (arm 4) was not needed once the dense-split reference reached the floor.

## Changes

- `scripts/mlx_lm_logit_trace.py` docstring: mlxcel converts bf16 to f16 only for unquantized checkpoints on Apple Silicon (unless `MLXCEL_KEEP_BF16`), and a fused quantized `kv_b_proj` loads different weights in the two runtimes. The `--mlx-vlm` block that #3281 edits is untouched.
- `docs/supported-models.md`: the LongCat entry states the finding instead of "source not identified".
- `docs/benchmark_results/longcat-flash-lite-trace-residual-m3ultra-2026-11-23.md`: all arm rows, plus a no-context check.
- Bilingual technical report in `TECHNICAL_REPORTS/3332-longcat-trace-residual-20261123.*.md`.

Because no runtime code changed, the sparse variant and the GigaChat3 traces are unaffected by construction. Checks: fmt, the `scripts/ci` contract scripts, the header check, and the `coverage_tests` (18 passed) and `longcat` (57 passed) lib selectors. The issue's broad `-- --test-threads=1 longcat` gate is left to the orchestrator.
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(models): apply YaRN rope_scaling in DeepSeek-V3 family and LongCat ngram MLA

1 participant