Skip to content

fix(perf): speed up Phi-3.5 SuScaledRoPE decode - #42

Merged
inureyes merged 1 commit into
mainfrom
fix/phi35-rope-decode
May 20, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/phi35-rope-decode

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Speeds up Phi-3 / Phi-3.5 longrope (SuScaledRoPE) decode by collapsing the attention prologue into a single fused MLX bridge call for quantized weights.

What changed

  • New bridge op fused_qkv_project_split_su_scaled_rope (mlx_cxx_bridge.{cpp,h}, exposed via mlxcel-core lib FFI + UnifiedLinear::forward_fused_qkv_split_su_scaled_rope): runs the concatenated QKV projection, Q/K/V split, reshape + transpose to [B, H, T, D], and the SuScaledRoPE rotation in one shot. This replaces the previous chain of separate slice / reshape / transpose / multiply / fast_rope_with_freqs graph nodes, whose per-token allocation and dispatch overhead dominated decode for these small models.
  • Faithful fallback: Phi3Attention::forward takes the fused path only when SuScaledRoPE frequencies are present and the fused-QKV weights are quantized; otherwise it routes through the extracted prepare_qkv_with_rope, preserving the original step-by-step behaviour for regular (non-quantized) weights. SuScaledRoPE semantics are unchanged — both paths scale only the rotary prefix before the custom-frequency RoPE, matching mlx-lm / mlx-vlm.

Benchmarks (M5 Max)

Model Before After vs upstream
phi-3.5-mini-4bit (text) 164.03 204.63 tok/s 79% → 98% of mlx-lm
phi-3.5-vision-4bit (VLM) 123.07 168.77 tok/s 77% → 106% of mlx-vlm

phi-3-mini, phi-3.5-moe, and phi-4 are held within measurement noise as guardrails.

Verification

  • cargo fmt --all -- --check clean
  • cargo clippy --all-targets --features metal,accelerate -- -D warnings clean (rebuilds the C++ bridge)
  • Greedy generation on phi-3.5-mini-4bit produces coherent text through the new fused quantized branch

Collapse the Phi-3 / Phi-3.5 longrope (SuScaledRoPE) attention prologue into a single fused bridge call for quantized weights. The new `fused_qkv_project_split_su_scaled_rope` MLX bridge runs the concatenated QKV projection, the Q/K/V split, the reshape and transpose to `[B, H, T, D]`, and the SuScaledRoPE rotation in one shot, replacing the previous chain of separate `slice` / `reshape` / `transpose` / `multiply` / `fast_rope_with_freqs` graph nodes. The per-token allocation and dispatch overhead of that chain dominated decode for these small models.

The Rust side keeps a faithful fallback. `Phi3Attention::forward` takes the fused path only when SuScaledRoPE frequencies are present and the fused-QKV weights are quantized, and otherwise routes through the new `prepare_qkv_with_rope`, which preserves the original step-by-step behaviour for regular (non-quantized) weights. SuScaledRoPE semantics are unchanged: both paths scale only the rotary prefix before applying the custom-frequency RoPE, matching mlx-lm / mlx-vlm.

On M5 Max the change lifts `phi-3.5-mini-4bit` from 164.03 to 204.63 tok/s decode (79% to 98% of mlx-lm) and the `phi-3.5-vision-4bit` VLM path from 123.07 to 168.77 tok/s (77% to 106% of mlx-vlm), with `phi-3-mini`, `phi-3.5-moe`, and `phi-4` held within measurement noise as guardrails. A short greedy generation on `phi-3.5-mini-4bit` confirms the fused quantized branch still produces coherent text.
@inureyes inureyes added type:performance Performance improvements area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:models Model architectures, weights, loading, metadata labels May 20, 2026
@inureyes
inureyes merged commit ded74c6 into main May 20, 2026
3 checks passed
@inureyes
inureyes deleted the fix/phi35-rope-decode branch May 20, 2026 15:43
inureyes added a commit that referenced this pull request May 23, 2026
Bump version to 0.0.30. Highlights: unified pre-load memory estimator with the new mlxcel inspect subcommand and --estimate-memory preflight (#64, #65, #66, #67), Molmo v1 and InternVL VLM architectures (#41, #37), Gemma 3n / Phi-3.5 / Gemma / Jamba decode performance work (#42, #43, #44, #60, #62), and server --ctx-size as a shared parallel context budget (#57).
@inureyes inureyes added this to the 0.1 milestone Jun 21, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added the status:done Completed label Sep 6, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
Collapse the Phi-3 / Phi-3.5 longrope (SuScaledRoPE) attention prologue into a single fused bridge call for quantized weights. The new `fused_qkv_project_split_su_scaled_rope` MLX bridge runs the concatenated QKV projection, the Q/K/V split, the reshape and transpose to `[B, H, T, D]`, and the SuScaledRoPE rotation in one shot, replacing the previous chain of separate `slice` / `reshape` / `transpose` / `multiply` / `fast_rope_with_freqs` graph nodes. The per-token allocation and dispatch overhead of that chain dominated decode for these small models.

The Rust side keeps a faithful fallback. `Phi3Attention::forward` takes the fused path only when SuScaledRoPE frequencies are present and the fused-QKV weights are quantized, and otherwise routes through the new `prepare_qkv_with_rope`, which preserves the original step-by-step behaviour for regular (non-quantized) weights. SuScaledRoPE semantics are unchanged: both paths scale only the rotary prefix before applying the custom-frequency RoPE, matching mlx-lm / mlx-vlm.

On M5 Max the change lifts `phi-3.5-mini-4bit` from 164.03 to 204.63 tok/s decode (79% to 98% of mlx-lm) and the `phi-3.5-vision-4bit` VLM path from 123.07 to 168.77 tok/s (77% to 106% of mlx-vlm), with `phi-3-mini`, `phi-3.5-moe`, and `phi-4` held within measurement noise as guardrails. A short greedy generation on `phi-3.5-mini-4bit` confirms the fused quantized branch still produces coherent text.
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
Bump version to 0.0.30. Highlights: unified pre-load memory estimator with the new mlxcel inspect subcommand and --estimate-memory preflight (lablup#64, lablup#65, lablup#66, lablup#67), Molmo v1 and InternVL VLM architectures (lablup#41, lablup#37), Gemma 3n / Phi-3.5 / Gemma / Jamba decode performance work (lablup#42, lablup#43, lablup#44, lablup#60, lablup#62), and server --ctx-size as a shared parallel context budget (lablup#57).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:models Model architectures, weights, loading, metadata status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant