Repository navigation
perf(xla): load single-sequence graphs with transposed weights (#2859) - #2861
Merged
Merged
Conversation
The single-sequence decode graph contracted each projection's last axis against the checkpoint's `[out, in]` layout, and IREE lowered that to a full-weight transpose plus a vecmat on every decode step. Profiling on gfx1151 showed 112 weight transposes per step at 96.5 ms/step (57 ms of transposes, plus a 9.65 ms f32 LM-head transpose), against 4.1 ms/step for the MLX ROCm path. The graph now declares projection weights `[in, out]` and contracts the shared axis, and the loader transposes each `Proj` / `Rows` weight once at upload (`transpose_projection`, 32x32 blocked). Graph emission picks the layout through `Config::projection_layout` and the `linear_proj` / `linear_seq_proj` helpers, so a graph's weight declarations and its contractions cannot disagree. The layout is per engine, not global, because the best lowering depends on the graph shape. The ragged batch decode already lowered the old `[out, in]` contraction to one transposed-B `linalg.matmul` (fast at its row count), while the canonical matmul over `[in, out]` was about 8x slower per call; the single-row case was the opposite. `IreeLlama` therefore emits `InOut` and `IreeRaggedLlama` keeps the checkpoint's `OutIn`, and every committed golden stays byte-identical on the `OutIn` path. Phi4MM LoRA adapter tensors and the LM head stay untransposed because the loader uploads them `Whole`. Validation on gfx1151 (hip, llama-3.2-1b-instruct-4bit, release build): B=1 CLI at capacity 256, n=64 10.5-11.4 -> 18.6-21.4 tok/s and n=128 9.0-10.0 -> 16.5-18.3 tok/s; batch B=4 N=8 63.9-69.2 -> 66.8-67.7 tok/s and B=8 N=16 80.1-80.5 -> 74.8-84.1 tok/s (unchanged path, within run-to-run spread); REFERENCE-EXACT PASS on both batch sizes. Greedy output is token-identical to the previous binary on llama-3.2-1b, qwen3-1.7b, qwen2.5-0.5b and gemma-3-4b. mlxcel-xla lib tests 385 pass with unchanged goldens; clippy clean at default and `iree` features. Refs #2859
inureyes
added a commit
that referenced
this pull request
Nov 6, 2026
The single-sequence graph kept the LM head in the checkpoint layout and emitted `dot_general %x, %head, contracting_dims = [0] x [1]` against a `[vocab, hidden]` f32 tensor. On the pinned IREE that lowers to a materialized full-weight transpose fused with the f32 -> f16 convert, so every decode step streamed and rewrote the whole head: 9.64 ms of the 47.0 ms step on gfx1151 for llama-3.2-1b, with the head vecmat and argmax another 6.35 ms. `take_lm_head` now switches on the graph's `ProjectionLayout`. `InOut` (the single-sequence `IreeLlama` graphs) always declares one `[hidden, vocab]` head operand and the head sites use `linear_proj` / `linear_seq_proj`, so the contraction becomes `[0] x [0]` and the loader transposes the head once at upload, f16-resident when the f16 path is active. Tied checkpoints keep their `[vocab, hidden]` embed arg for the gather and add this second arg, read from the same tensor; untied checkpoints use `lm_head` for it, so their head goes from a 1.05 GB f32 whole tensor to a 0.53 GB f16 transposed one. `OutIn` (the ragged batch engine and every committed golden) keeps the exact previous head arg and `[V, H]` form, so the batch path and the goldens are unchanged. The tied single-sequence graph pays one extra f16 head copy (`hidden * vocab * 2` bytes, 525 MB for llama-3.2-1b). Validation on gfx1151 (hip, llama-3.2-1b-instruct-4bit, release build): the decode step drops from 47.0 to 39.6 ms and from 318 to 317 dispatches, and the head's `elementwise_transpose` dispatch is gone; the head is now one `vecmat_128256x2048` (3.49 ms) plus the argmax (3.86 ms). B=1 CLI at capacity 256, n=64 18.4-25.1 tok/s and n=128 19.1-27.8 tok/s (interleaved runs against the pre-#2861 binary: 10.5-11.3 and 10.5-11.0). Greedy output is token-identical on llama-3.2-1b and qwen3-1.7b (tied) and mistral-7b-instruct-v0.3 (untied, exercising the `lm_head` operand). Batch B=4 N=8 and B=8 N=16 still pass REFERENCE-EXACT at 65.4 and 74.3 tok/s. mlxcel-xla lib tests 387 pass with unchanged goldens; clippy clean at default and `iree` features. Refs #2863
This was referenced Nov 23, 2026
inureyes
added a commit
that referenced
this pull request
Nov 24, 2026
## Summary - OpenXLA: the Phi4MM speech/vision LoRA A/B tensors are now `WeightSpec::Whole`, not `Proj`. Since #2861 the loader transposes `Proj` specs for the single-sequence `InOut` layout, while the emitter declares the LoRA pairs in the checkpoint layout (`[rank, in]`, `[out, rank]`) and contracts them untransposed, so prefill failed with `expected 320x3072, actual 3072x320`. `Whole` is never transposed and stays f32-resident (the declared element type) in every layout, so the ragged `OutIn` path uploads exactly what it did before. I chose this over flipping the declarations because the LoRA branch is shared by single and ragged graphs. Flipping the declarations would have meant changing the LoRA branch per layout, and the f16-resident `Proj` rule would also have needed an exemption. - A new `WeightSpec::uploads_transposed` now drives the loader's `Proj` branch. The new test `phi4mm_lora_declared_shapes_match_uploaded_shapes_in_every_layout` compares the declared LoRA shapes in the prefill, embeddings-prefill, decode and ragged graphs against what the loader uploads, in both layouts. With the old `Proj` push it fails: `prefill (InOut) declares ...qkv.lora_A.speech as [3, 8] but the loader uploads [8, 3]`. - Eager CLI: `materialize_phi4mm_ordered_prompt` now passes a prompt without ordered-media markers through to `prepare_phi4mm_prompt_tokens`, as the Gemma3n path does since #922. That function inserts the audio tag at the start of the user turn, which is the same normalization the OpenXLA CLI applies, and it still rejects a placeholder count that disagrees with the request. ## Test plan Checkpoint: microsoft/Phi-4-multimodal-instruct@93f923e1. Input: the official `what_is_shown_in_this_image.wav` with "Transcribe the audio exactly." and `-n 8 --temp 0`. - [x] Eager (`MLXCEL_SDPA_DETERMINISTIC=1`), XLA cuda and XLA local-task all produce `13347 382 10147 306 495 3621` ("what is shown in this image") - [x] `xla_phi4_audio_check` passes on local-task and cuda - [x] Phi4MM vision (vision LoRA) runs on XLA cuda. It matches eager for the first 8 tokens, and XLA f16 and f32 give identical output - [x] qwen2.5-0.5b-instruct-bf16 on XLA cuda gives the same tokens as eager - [x] `cargo test -p mlxcel-xla --lib -- phi4` and `cargo test --features cuda --lib -- phi4mm_` pass - [x] `cargo check --features cuda,xla-iree --all-targets`, `cargo check --no-default-features --features xla-diagnostics --all-targets`, `insert_apache_header.py --check` and `cargo fmt --check` pass Closes #3362
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
The single-sequence XLA/IREE decode graph contracted each projection's last axis against the checkpoint's
[out, in]weight layout. IREE lowered that to a full-weight transpose plus a vecmat on every decode step: profiling on gfx1151 showed 112 projection transposes (57 ms) and a 9.65 ms f32 LM-head transpose inside a 96.5 ms step, against 4.1 ms/step for the MLX ROCm path (issue #2859).This PR makes the graph declare projection weights
[in, out]and contract the shared axis, so IREE emits one vecmat per projection, and moves the transpose to load time where it runs once per weight.Changes
ProjectionLayout(OutIn|InOut) onConfigandBuilder, withlinear_proj/linear_seq_projselecting the contraction to match the declared weight shape. Dense projection call sites moved to the layout-aware helpers; Phi4MM LoRA adapter tensors (modal,[rank, in]/[out, rank]) and the LM head (Whole) keep the untransposed helpers.transpose_projectioninweights.rs, a 32x32 blocked[out, in] -> [in, out]transpose applied once perProj/Rowsweight at upload. The packed 4-bit path keeps its in-graph dequant plus transpose, since it already trades decode throughput for upload bytes.IreeLlama(single sequence) emitsInOut;IreeRaggedLlama(continuous batching) keepsOutIn; Gemma3n's own emitters keepOutIn.Why the layout is per engine
The best lowering depends on the graph shape, not the model. The ragged batch decode already lowered the old
[out, in]contraction to a single transposed-Blinalg.matmul(about 0.3 ms per down projection at its row count), while the canonical matmul over[in, out]was about 8x slower per call (2.38 ms); a single-layout branch measured B=4 N=8 at 28.0-28.5 tok/s against 59.3-65.5 before. The single-row case is the opposite, where the old layout cost the transpose plus vecmat. KeepingOutInfor the batching engine also keeps every committed golden byte-identical, which is what the 385-testmlxcel-xlalib suite checks.Validation (gfx1151, hip, llama-3.2-1b-instruct-4bit, release build)
REFERENCE-EXACT PASSon both batch sizes.cargo test -p mlxcel-xla --lib385 passed, committed goldens unchanged.cargo clippy -p mlxcel --lib --tests -- -D warningsandcargo clippy -p mlxcel-xla --all-targets --features iree -- -D warningsclean;cargo fmt --checkclean.cargo check --features xla-iree --bin mlxcel --example xla_batch_benchclean.Closes #2859