Skip to content

fix(models): apply Llama 4's traditional llama3-scaled RoPE - #3126

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-3120-llama4-rope
Nov 16, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-3120-llama4-rope

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Llama 4 rotated Q and K with split-half pairing and plain rope_theta frequencies. mlx-lm uses initialize_rope(..., traditional=True, scaling_config=rope_scaling), and Scout declares a llama3 block with factor 16. CxxAttention now holds a Llama4Rope (src/models/llama4_helpers.rs) that pairs adjacent dimensions and takes its frequency table from the shared RopeScalingKind::resolve. TextArgs now parses rope_scaling.

  • Every RoPE call site goes through it: forward_llama4, forward_batched_decode_rows, and the legacy and debug forwards. The VLM text decoder and the pipeline stage build the same CxxAttention, so they are covered too.
  • Because Llama 4 now resolves its rope through rope_utils, --rope-* runtime overrides reach it. The family lists in rope_overrides, rope_utils and docs/llama-server-compat.md now include it.

Measurements (M5 Max, llama-4-scout-17b-16e-instruct-4bit, against stock mlx-lm 0.31.3)

Shape Decided main disagreement branch disagreement mlx-lm ppl main ppl branch ppl
Prefill width 1280, ctx up to 2304 1965 83.82% 0.05% 14.213 1868.2 14.217
Prefill width 4608, ctx up to 9216 3643 87.90% 0.00% 11.803 2392.9 11.800
Decode width 1, ctx 512 to 2560 750 83.33% 0.00% 16.351 1819.6 16.349
Batched decode B=2, same rows 750 83.47% 0.00% 16.351 1815.7 16.367
Decode width 1, ctx 4608 to 9216 1886 92.42% 0.00% 10.268 3059.5 10.260

On main, greedy output degenerates into repeating "understand,". On the branch it is coherent text. Cooled, order-reversed decode runs: main 46.9 tok/s, branch 45.6 to 47.5 tok/s, so there is no measurable cost. Details and CSVs are in docs/benchmark_results/llama4-rope-m5max-2026-11-16.md.

Test plan

  • New llama4_helpers_tests: Llama4Rope::apply and apply_batched match a host f64 transcription of mlx-lm's traditional Llama3RoPE at offsets 0, 7, 4093, 8191 and 20000, and per batched row. The old rotation fails the same reference.
  • models::llama4_helpers_tests, models::llama4::tests, models::rope_utils::tests, models::rope_overrides::tests, and the create_causal_mask roster test
  • cargo fmt --check, cargo clippy -p mlxcel --lib --tests --features metal,accelerate -- -D warnings, check_env_var_docs.py, check_benchmark_raw_logs.py, make verify-kernel-dtype-keys

Closes #3120

Llama 4's attention rotated Q and K with split-half pairing, plain rope_theta frequencies and unit scale. mlx-lm builds the rope with initialize_rope(..., traditional=True, scaling_config=rope_scaling), and Scout's block is llama3 with factor 16, so mlxcel disagreed with mlx-lm on most decided positions at every context length.

CxxAttention now holds a Llama4Rope (llama4_helpers.rs) that pairs adjacent dimensions and takes its table from RopeScalingKind::resolve, the shared rope_utils path, so --rope-* overrides also reach Llama 4. TextArgs parses rope_scaling. Every RoPE call site (forward_llama4, the batched decode rows, the legacy and debug forwards) goes through it; the VLM text decoder and the pipeline stage build the same CxxAttention.

On llama-4-scout-17b-16e-instruct-4bit, teacher-forced against stock mlx-lm 0.31.3 at decode and prefill widths up to 9216 tokens, decided-position disagreement drops from 83 to 92% to 0.00 to 0.05%, and perplexity from 1816 to 3060 to within 0.1% of mlx-lm. Decode throughput is unchanged. Unit tests compare the rotation against a host reference of mlx-lm's Llama3RoPE at five offsets and per batched row.

Closes #3120
@inureyes inureyes added status:done Completed type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 16, 2026
@inureyes
inureyes marked this pull request as ready for review November 16, 2026 07:58
@inureyes
inureyes merged commit 8ee49de into main Nov 16, 2026
14 checks passed
@inureyes
inureyes deleted the fix/issue-3120-llama4-rope branch November 16, 2026 07:58
@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:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:high High 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 Llama 4's traditional llama3-scaled RoPE

1 participant