Repository navigation
fix(models): apply Llama 4's traditional llama3-scaled RoPE - #3126
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 16, 2026 07:58
This was referenced Nov 16, 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.
Summary
Llama 4 rotated Q and K with split-half pairing and plain
rope_thetafrequencies. mlx-lm usesinitialize_rope(..., traditional=True, scaling_config=rope_scaling), and Scout declares allama3block with factor 16.CxxAttentionnow holds aLlama4Rope(src/models/llama4_helpers.rs) that pairs adjacent dimensions and takes its frequency table from the sharedRopeScalingKind::resolve.TextArgsnow parsesrope_scaling.forward_llama4,forward_batched_decode_rows, and the legacy and debug forwards. The VLM text decoder and the pipeline stage build the sameCxxAttention, so they are covered too.rope_utils,--rope-*runtime overrides reach it. The family lists inrope_overrides,rope_utilsanddocs/llama-server-compat.mdnow include it.Measurements (M5 Max, llama-4-scout-17b-16e-instruct-4bit, against stock mlx-lm 0.31.3)
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
llama4_helpers_tests:Llama4Rope::applyandapply_batchedmatch a host f64 transcription of mlx-lm's traditionalLlama3RoPEat 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 thecreate_causal_maskroster testcargo 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-keysCloses #3120