Repository navigation
perf(xla): fuse q/k/v and gate/up into multi-output native GEMV launches - #2907
Merged
Merged
Conversation
The native 4-bit decode path now multiplies q, k and v in one launch and gate and up in another: a new pair of exports selects the weight set from the cumulative output row, so the activation row is read once per group and the step issues 65 GEMV launches instead of 113. The kernels keep both row layouts from #2891 (warp-per-row below K = 4096, block-per-row at or above it) and live in their own hal.executable.source blocks because their pipeline layouts differ in binding and constant count; the same HSACO carries all six entry points. The emitter only fuses in the single-sequence decode graph. The seq layouts keep the original operation order (gate projection, delta, bias, then up) so the committed prefill and decode goldens stay byte-identical, which the golden tests confirm; the model's q/k/v post-processing moved into a shared helper without changing the unfused sequence. On gfx1151 with llama-3.2-1b-instruct-4bit, a paired A/B on one binary at n=256 measures 122.7 tok/s fused against 117.0 per-projection (three paired rounds, all positive, about 5 percent), with per-step kernel time down from 5.46 to 5.30 ms and the 7.1 ms step otherwise unchanged because the launch gaps did not move measurably. The fused q/k/v kernel was checked against a double-precision reference through iree-run-module (max absolute error 8.6e-6 across three weight sets), greedy output stays byte-identical to the f16 path on two prompts, and xla_batch_bench stays REFERENCE-EXACT at B=8 with the switch set. Refs #2895
inureyes
force-pushed
the
perf/2895-fused-qkv-gateup
branch
from
November 10, 2026 02:50
1a95aac to
c9d97ab
Compare
This was referenced Nov 10, 2026
This was referenced Nov 10, 2026
inureyes
added a commit
that referenced
this pull request
Nov 10, 2026
The ragged continuous-batching engine still streamed f16-expanded projection weights: linear_seq_proj found no packed operand in the OutIn graph and fell through to stablehlo.dot_general. It now runs the checkpoint's packed 4-bit projections through the native HIP GEMV, reusing the cumulative-N fused ABI from #2907. qmv4.hip gains qmv4_multi_kernel and qmv4_multi_row_kernel plus the fused multi-row q/k/v and gate/up variants. Each keeps one accumulator per activation row, so a weight word is dequantized once and multiplied into all B rows and the streamed weight bytes drop about 4x. The block-per-row export reduces every row's partial through one shared-memory pass with a single barrier. native_qmv.rs adds the six exports, their hal.executable.source blocks, and the dispatch helpers with the same K >= 4096 width rule and grid rounding as the single-row form; builder.rs adds native_qmv_rows and routes a packed rank-2 linear_seq_proj through it; model.rs registers packed operands for the ragged schema, fuses q/k/v and gate/up, and keeps the f16 graph byte-identical when no native module is passed; iree.rs decides the mode once in XlaRaggedLlama::load_inner before the FFI dimensions and weight upload, so the packed tensors stay resident and the decision is shared with the prefill graphs. A ragged decode step for llama-3.2-1b drops from 113 launches to 65 (7 rank-2 projections per layer to 4), independent of B. The f16 fallback is unchanged for a missing hipcc, an unsupported arch, or a checkpoint that is not 4-bit group-64. On gfx1151 with llama-3.2-1b-instruct-4bit, the same binary through xla_batch_bench measures 74.5 tok/s at B=8 against 59.8 on the f16 path and 62.2 against 65.6 at B=4, with REFERENCE-EXACT PASS on every run; the untied Qwen2.5-7B-Instruct-4bit stays exact at B=4.
inureyes
added a commit
that referenced
this pull request
Nov 10, 2026
* perf(xla): run the ragged engine through the native 4-bit GEMV The ragged continuous-batching engine still streamed f16-expanded projection weights: linear_seq_proj found no packed operand in the OutIn graph and fell through to stablehlo.dot_general. It now runs the checkpoint's packed 4-bit projections through the native HIP GEMV, reusing the cumulative-N fused ABI from #2907. qmv4.hip gains qmv4_multi_kernel and qmv4_multi_row_kernel plus the fused multi-row q/k/v and gate/up variants. Each keeps one accumulator per activation row, so a weight word is dequantized once and multiplied into all B rows and the streamed weight bytes drop about 4x. The block-per-row export reduces every row's partial through one shared-memory pass with a single barrier. native_qmv.rs adds the six exports, their hal.executable.source blocks, and the dispatch helpers with the same K >= 4096 width rule and grid rounding as the single-row form; builder.rs adds native_qmv_rows and routes a packed rank-2 linear_seq_proj through it; model.rs registers packed operands for the ragged schema, fuses q/k/v and gate/up, and keeps the f16 graph byte-identical when no native module is passed; iree.rs decides the mode once in XlaRaggedLlama::load_inner before the FFI dimensions and weight upload, so the packed tensors stay resident and the decision is shared with the prefill graphs. A ragged decode step for llama-3.2-1b drops from 113 launches to 65 (7 rank-2 projections per layer to 4), independent of B. The f16 fallback is unchanged for a missing hipcc, an unsupported arch, or a checkpoint that is not 4-bit group-64. On gfx1151 with llama-3.2-1b-instruct-4bit, the same binary through xla_batch_bench measures 74.5 tok/s at B=8 against 59.8 on the f16 path and 62.2 against 65.6 at B=4, with REFERENCE-EXACT PASS on every run; the untied Qwen2.5-7B-Instruct-4bit stays exact at B=4. * docs(xla): correct the ragged native GEMV descriptions The MLXCEL_XLA_QUANT row in docs/environment-variables.md and the README native section implied that the LM head joins the native GEMV path on the ragged batch engine too. It does not: the ragged graph keeps the head on the f16 dot_general path regardless of packing, and only the single-sequence graph packs and dispatches it. State that split so the docs match the code, and note it in the ragged README paragraph. Also update the Builder::with_native_proj doc comment, which still said only the single-sequence decode graph sets it; the ragged decode graph sets it now, and the prefill graphs leave it off so their packed operands dequantize in the graph. * perf(xla): embed only the GEMV exports each decode graph dispatches Both decode graphs were handed all six `hal.executable.source` blocks from `native_qmv::module_parts`, so the single-sequence graph embedded the three multi-row blocks and the ragged graph embedded the three rank-1 blocks. Each block carries a full copy of the HSACO (120 KB for the current kernel), which added about 350 KB of unused object data and 700 KB of hex text to the single-sequence module, three dead executables to its iree-compile input, and the same waste in the ragged graph. Split the list: `module_parts` now carries the rank-1 blocks and `ragged_module_parts` the multi-row blocks, and `IreeRaggedLlama::load_inner` uses the latter. A test pins the split so a later export cannot silently start embedding twice. Validated with `cargo fmt --all -- --check`, `cargo test -p mlxcel-xla --lib native_qmv::` (6 passed), and `cargo clippy -p mlxcel-xla --features iree --lib --tests -- -D warnings`. * perf(xla): skip the native GEMV artifact for unsupported packed layouts `XlaRaggedLlama::load_inner` compiled and embedded the HSACO whenever the checkpoint was 4-bit group-64. `take_weight` registers a packed projection only when `supports_packed_quant()` also holds, so a checkpoint with fused q/k/v, fused gate/up, MoE, MLA or a dense MLP paid for a hipcc compile and embedded an artifact that no dispatch reached, without the fallback warning the docs promise. Gate the ragged native decision on both predicates and report the f16 fallback when either fails. The single-sequence loader keeps its own predicate, where a packed LM head can still justify the artifact. Also correct the packed-gate comment: `MLXCEL_XLA_QUANT=packed` does not set `packed_args` on the ragged path, so the check reflects the native decision there. * docs(xla): record both ragged native GEMV runs and the layout fallback The reviewed tip was re-measured end to end: native B=4 60.0 tok/s, B=8 76.7 tok/s, untied Qwen2.5-7B-Instruct-4bit B=4 14.2 tok/s, and the HIPCC-unavailable fallback 66.1 tok/s, all REFERENCE-EXACT PASS. State the two B=8 observations against the same-binary f16 reference and extend the fallback wording to the projection-layout case that the load gate now rejects. * test(xla): cover the B=4 ragged native GEMV launch structure The ragged emitter test exercised only the B=8 bundled graph, so the issue's requirement that the launch count does not scale with the batch, and that the B=4 graph carries its own push constant, was untested. Extend it to emit B=4 with the native module and assert the four multi-row dispatch counts match the B=8 graph, the batch constant is 4, and the fused outputs are shaped [4, N]. Validation: cargo fmt --all -- --check, cargo clippy -p mlxcel-xla --features iree --lib --tests -- -D warnings, and cargo check -p mlxcel-xla --features iree --lib --tests pass; the narrow selectors native_qmv::, native_ragged_decode_emits_multi_row_dispatches, and validation:: pass without the iree feature. Refs #2922 * docs: add technical report for PR #2933
9 of 18 tasks
inureyes
pushed a commit
that referenced
this pull request
Nov 15, 2026
## Summary `MLXCEL_XLA_QUANT=native` was inert on `MLXCEL_XLA_DEVICE=cuda`: the loader required the `hip` device, so the decode graph kept the f16-expanded weights. This ports the single-sequence native 4-bit GEMV to CUDA, as #2905 asks. - `src/lib/mlxcel-xla/csrc/qmv4.cu`: the six single-sequence entry points of `csrc/qmv4.hip` (`qmv4_kernel`, `qmv4_row_kernel`, and the fused q/k/v and gate/up pairs from #2895), with one change, `__shfl_down_sync(0xffffffffu, ...)` for `__shfl_down(...)`. Same MLX affine layout, same ABI (i32 constants, read-only bindings, f32 outputs), `__half` activations and scales/biases. - `src/lib/mlxcel-xla/build.rs`: compiles it with `nvcc` to `compute_80` PTX next to the Gemma3n kernel, in the same `IREE_CUDA_HOME` branch. PTX availability is now its own cfg, `xla_iree_cuda_ptx`, emitted only after both kernels compile; `xla_iree_cuda` keeps meaning "CUDA IREE runtime". Without `nvcc` the build prints a warning and carries no PTX instead of failing. A compiler that is found and rejects a source still fails the build. - `src/lib/mlxcel-xla/src/native_qmv.rs`: embeds the PTX under `cfg(xla_iree_cuda_ptx)`, and builds the CUDA target alias (`#hal.executable.target<"cuda", "cuda-nvptx-fb">`) and the three `hal.executable.source` blocks from the same export tables the hip path uses. - `src/lib/mlxcel-xla/src/iree.rs`: the switch now turns on for `cuda` as well as `hip`, and a graph that carries the PTX is compiled with `--iree-cuda-target=sm_80` (the flag the Gemma3n kernel already sets). The 4-bit group-64 gate, the signless `i32` packed operand and the LM head dtype check are the existing shared code. - The emitter is unchanged apart from comments. The CUDA decode graph is the hip graph with the target alias and the embedded object swapped. - Gemma3n: the code that needs its PTX (`gemma3n_qmv.rs`, the native SDPA gate and three test sections) moves from `cfg(xla_iree_cuda)` to `cfg(xla_iree_cuda_ptx)`. Gemma3n CUDA Q4 keeps its existing load-time error when the artifact is missing; it does not fall back to a numerically different path. - `src/lib/mlxcel-xla/csrc/qmv4_cuda_oracle.cu`: a standalone check of the six exports against a host reference. It is not part of the build; the crate README has the two commands. ## Related issues Closes #2905 ## Type of change - [ ] `feat` — new user-visible feature - [ ] `fix` — bug fix - [x] `perf` — performance improvement (include before/after numbers in the PR body) - [ ] `refactor` — internal restructuring without behavior change - [ ] `chore` — build, CI, dependencies, release infrastructure - [ ] `docs` — documentation only - [ ] `test` — tests only ## Measurements DGX Spark GB10 (sm_121, CUDA 13.0, nvcc 13.0.88, driver 580.178.04), IREE 3.12.0rc20260721 from `scripts/iree/setup-cuda.sh`, release build with `--features cuda,xla-iree`, base `955335a3`. Greedy, `-n 128`, `MLXCEL_XLA_CONTEXT_CAPACITY=256`, warm vmfb, one process per run, two timed rounds after a warm-up round. Every number in this section is from the tree in this PR. The premise first, measured before the port on `ce6855bc`: the switch does nothing on `cuda` (19.28 tok/s without it, 19.42 with it, same output, 48 tokens). | checkpoint | prompt | f16-resident (tok/s) | native (tok/s) | output | |---|---|---|---|---| | llama-3.2-1b-instruct-4bit (tied) | 1 | 20.75, 20.18 | 58.41, 62.17 | identical | | llama-3.2-1b-instruct-4bit (tied) | 2 | 20.19, 20.46 | 62.86, 69.20 | identical | | Qwen2.5-7B-Instruct-4bit (untied) | 1 | 5.85, 5.87 | 19.04, 19.56 | identical | | Qwen2.5-7B-Instruct-4bit (untied) | 2 | 5.76, 5.89 | 19.33, 19.02 | identical | About 3.1x on llama-3.2-1b and 3.3x on Qwen2.5-7B. The native rounds on llama-3.2-1b spread more than the f16 ones (58.4 to 69.2); I did not find out why. ### Trace before and after `nsys profile --trace=cuda`, llama-3.2-1b-instruct-4bit, prompt 1, 128 decode steps. Decode-step kernels are the ones whose launch count is a multiple of 128; prefill and one-off kernels are excluded (124.9 ms in the f16 run, 118.5 ms in the native run). | | f16-resident | native | |---|---|---| | tok/s under `nsys` | 20.27 | 55.53 | | launches per decode step | 309 | 309 | | GPU kernel time per decode step | 41.81 ms | 8.28 ms | | projection launches per step | 113 `vecmat` | 65 `qmv4_*` | | projection kernel time per step | 39.57 ms | 5.96 ms | | `batch_matmul` (attention), 32 per step | 1.89 ms | 1.89 ms | | everything else | 164 launches, 0.35 ms | 212 launches, 0.43 ms | Per kernel, f16 graph (name is out x in): | kernel | per step | avg | |---|---|---| | `vecmat_2048x8192` (down) | 16 | 1093.5 us | | `vecmat_8192x2048` (gate, up) | 32 | 303.0 us | | `vecmat_2048x2048` (q, o) | 32 | 274.7 us | | `vecmat_512x2048` (k, v) | 32 | 37.7 us | | `vecmat_128256x2048` (head) | 1 | 2380.2 us | Per kernel, native graph: | kernel | per step | avg | per step total | |---|---|---|---| | `qmv4_gateup_kernel` | 16 | 175.0 us | 2.80 ms | | `qmv4_kernel` (o, head) | 17 | 105.7 us | 1.80 ms | | `qmv4_row_kernel` (down) | 16 | 46.9 us | 0.75 ms | | `qmv4_qkv_kernel` | 16 | 38.5 us | 0.62 ms | Two things differ from the gfx1151 picture in #2890 and #2907. The launch count does not fall: 113 projection launches become 65, and 48 small elementwise launches appear around the external dispatches. And the down projection is the largest single change, 1.09 ms per launch as an f16 `vecmat` against 47 us, which is more than the 4x byte reduction explains; the IREE-generated K=8192 reduction is slow on this device. ### Token identity The four chat-prompt runs above produce identical text. On this base `mlxcel generate` does not print token ids on the XLA path (#2980 addresses that), so I also compared ids with the repository's own tools: `xla_traj_dump` for the f16 and the native trajectory (128 argmax steps, raw prompt, no EOS stop), and `xla_oracle_check` under `MLXCEL_XLA_QUANT=native` against the f16 dump. | checkpoint | 128-token trajectories identical id for id | |---|---| | llama-3.2-1b-instruct-4bit | 6 of 6 | | Qwen2.5-7B-Instruct-4bit | 5 of 6 | The one mismatch is Qwen2.5-7B on "Write a short poem about a lighthouse keeper and the sea.", first differing at token 67 (f16 ` whisper`, native ` story`). What I checked: - Both arms reproduce their own trajectory exactly on a second run. - The XLA f32 path picks the f16 path's token. - The MLX backend, fed the same 80-token prefix (the server's tokenization of it equals the prompt ids plus the first 67 generated ids), returns ` story` and reports the same log-probability for both candidates: -1.28125 and -1.28125, then ` challenge` at -2.328. Position 67 is a tie at the resolution the MLX backend reports, consistent with sensitivity to arithmetic differences. This observation alone does not prove exact equality of the underlying logits or exclude every kernel defect: the native kernel dequantizes each weight in f32, where the f16 graph multiplies weights already rounded to f16. The README states identity as measured, not guaranteed, with this case. ## Builds without nvcc `NVCC=/nonexistent/nvcc` with the CUDA IREE runtime installed, same host: - `cargo test --release -p mlxcel-xla --features iree --lib`: 436 passed, 20 ignored. Two existing Gemma3n emitter tests assumed that a CUDA runtime implies the PTX; their native-only sections now sit behind the artifact cfg and the shared schema assertions still run. - `cargo clippy -p mlxcel-xla --all-targets --features iree -- -D warnings` and `RUSTFLAGS="-D warnings" cargo check --features cuda,xla-iree`: clean, with the one `cargo:warning` that the PTX was omitted. - Real run, llama-3.2-1b-instruct-4bit, "Explain in three sentences why the sky is blue.", 128 tokens, `xla_traj_dump`: the no-PTX binary produces the same token ids with the switch off and on, and with it on prints the fallback warning exactly once. - A compiler that answers `--version` and then rejects the source (a fixture script exiting 23) fails the build with its diagnostic. It is not treated as an absent compiler. The host keeps its already-built MLX CUDA dependencies, so this shows that the XLA PTX step is optional, not that the whole tree builds without a CUDA toolkit. ## Standalone kernel check `csrc/qmv4_cuda_oracle.cu` launches the six exports directly, without IREE or a model, against an FP64 host reference: K from 64 to 18944 on both sides of the 4096 threshold, N of 1, 7, 8, 9 and 17, unequal fused output sizes, and guard slots after each output. On the GB10: 600 launches and 8440 outputs pass, the 300 launches with exactly representable inputs are exact, the largest absolute error on the rest is 1.19e-5 (0.019 of the FP32 error envelope), and `compute-sanitizer --tool memcheck` reports 0 errors. This checks indexing and arithmetic of the kernels. It does not make the token comparison above 12 of 12. ## Acceptance criteria | criterion from #2905 | result | |---|---| | Greedy output token-identical to the f16 path on llama-3.2-1b-instruct-4bit (tied) and one untied checkpoint, on two prompts | Met on the two chat prompts for both checkpoints (identical output, 128 tokens each). On raw-prompt id comparison: 6 of 6 on llama-3.2-1b, 5 of 6 on Qwen2.5-7B; the exception is the tie described above. | | `xla_batch_bench` stays REFERENCE-EXACT | `--device cuda --batch 8 --requests 16 --maxcap 48`: `REFERENCE-EXACT PASS` with and without the switch (420 tokens). With the switch the single-sequence references come from the native path and the batch engine from f16 weights, so this is also an id-for-id match between the two on 16 requests. Batched throughput is unchanged (31.9 tok/s without, 32.4 with), and the single-sequence baseline the harness prints goes from 18.9 to 44.2 tok/s. | | The f16 path, prefill, committed goldens and every non-CUDA device are unchanged | The f16 graph and prefill are not touched. The hip module builders now read the same export tables as CUDA, and the existing hip tests (`module_prefix_embeds_the_object_and_the_abi`, `decode_kinds_embed_only_their_own_exports`, the emitter's `native_decode_emits_the_packed_projection_dispatch`) pass unchanged; a new test holds the CUDA decode graph equal to the hip one apart from the alias and object. `cargo test -p mlxcel-xla --lib` passes with the committed goldens. Not run: any ROCm or Metal device (I have neither). | | Trace before/after pasted: dispatch count, per-kernel time, tok/s at capacity 256, n=128 | Above. | | Builds without nvcc still compile and keep the f16 path | Met. With `NVCC` pointing at a nonexistent path and the CUDA IREE runtime installed: `RUSTFLAGS="-D warnings" cargo check --features cuda,xla-iree` passes with one build warning, the XLA unit suite passes (436), and a real run with the switch on warns once, falls back, and produces the same 128 token ids as the f16 path. See "Builds without nvcc" below. | | Tests and `cargo check --features xla-iree` under `RUSTFLAGS=-D warnings` stay clean | `RUSTFLAGS="-D warnings" cargo check --features cuda,xla-iree` is clean with and without `nvcc`. | ## Test plan - [x] `cargo fmt --all -- --check` - [ ] `cargo clippy --workspace --all-targets --features metal,accelerate -- -D warnings` (not run; Linux host). `cargo clippy -p mlxcel-xla --all-targets --features iree -- -D warnings` is clean with and without `nvcc`. - [ ] `cargo test --workspace --profile test-fast --features metal,accelerate --no-fail-fast -- --test-threads=1` (not run; Linux host) - [ ] `cargo deny check` (not run; no dependency change) - [x] Validated with real checkpoints: `mlx-community/Llama-3.2-1B-Instruct-4bit`, `mlx-community/Qwen2.5-7B-Instruct-4bit` - [x] Before/after numbers and environment are in the PR body Also run on the GB10 host: - `cargo test --release -p mlxcel-xla --lib`: 412 passed, 0 failed, 19 ignored (no IREE environment). - `cargo test --release -p mlxcel-xla --features iree --lib`: 437 passed, 22 ignored (CUDA IREE build, PTX embedded); 436 passed, 20 ignored with `NVCC` pointing at a nonexistent path. - `python3 -m unittest discover -s tests -p 'test_*.py'`: 219 tests, OK. - Repository gates: env-var docs, binary assets, cross-repo refs, CUDA arch lists, kernel dtype keys, kernel port dispatch, license headers. New unit tests: - `cuda_kernels_are_the_hip_single_sequence_kernels`: `qmv4.cu` equals the corresponding `qmv4.hip` lines apart from the one intrinsic, so the two sources cannot drift. - `cuda_kernel_source_matches_the_dispatch_contract`: the entry points and block geometry the dispatch constants assume. - `cuda_module_parts_embed_the_ptx_under_the_cuda_target`: the CUDA module text equals the hip one with the object path swapped. - `cuda_ptx_carries_every_single_sequence_entry` / `cuda_ptx_is_absent_without_a_compiled_artifact`: one per build configuration. - `native_decode_graph_is_the_same_on_cuda_apart_from_the_embedded_object`: the emitted decode graph, 65 dispatches. ## Notes for reviewers - **This changes what a CUDA IREE build without nvcc does, including for Gemma3n.** On `main` such a build fails in `build.rs`. Here it builds with a warning and no PTX. Dense models then keep the f16 path; Gemma3n CUDA Q4 fails at load with its existing "built without the CUDA-IREE PTX" error instead of at build time. I did this because the issue asks for builds without nvcc to compile. If you would rather keep the hard build failure for Gemma3n, the cfg split is the only part to revert. - **Devices below sm_80.** The PTX is `compute_80` and the graph is compiled for `sm_80`, as for Gemma3n. I could not test what the opt-in switch does on an older device; I expect the module to fail to load rather than fall back. - **Ragged engine.** Out of scope per the issue. On `cuda` the batch engine keeps the f16 weights whatever the switch says, and the multi-row exports stay hip-only. - **Only the GB10 was measured.** No other NVIDIA device, and the hip path was not re-run. The kernel bodies are held line-for-line equal to the hip source by a test, but `nvcc` and `hipcc` may contract floating point differently, so I make no claim about cross-vendor bit equality. - **The untied checkpoint is `Qwen2.5-7B-Instruct-4bit`**, not the `Qwen2.5-Coder-7B-Instruct-4bit` used in #2890. Same architecture and shapes (hidden 3584, down K=18944 on the row kernel). ## Checklist - [x] PR title uses a Conventional Commits prefix (`feat:`, `fix:`, etc.) - [x] One logical change per PR (split unrelated changes) - [x] Updated `docs/` if user-facing behavior or supported models changed - [x] Updated `// Used by: ...` comments on any shared function I modified (see `docs/code-guidelines.md`) - [x] No secrets, credentials, or `.env` files committed
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
Implements #2895. The native decode path now issues one GEMV launch for q, k and v and one for gate and up, instead of one per projection: 65 launches per step against 113. A new pair of exports maps the cumulative output row to its weight set, so the activation row is read once per group, and the kernels keep both row layouts from #2891 (warp-per-row below K = 4096, block-per-row at or above it). Their pipeline layouts differ from the single-output exports, so they live in two additional
hal.executable.sourceblocks and the one HSACO carries all six entry points.The emitter restricts fusion to the single-sequence decode graph and keeps the original operation order in the seq layouts (gate projection, delta, bias, then up), so the committed goldens stay byte-identical; the golden tests confirm it. The q/k/v post-processing was factored into one helper shared by the fused and per-projection forms.
Measurements
gfx1151, llama-3.2-1b-instruct-4bit, greedy,MLXCEL_XLA_CONTEXT_CAPACITY=256, warm vmfb. Paired A/B on one binary (n=256, three interleaved rounds):The final binary measures 120.3, 120.8 and 121.1 tok/s at the same settings; the f16 path is 29.2 to 31.3. The gain is about 5 percent, below the 8 to 12 percent the issue set as the bar, but every paired round is positive.
rocprofv3per decode step: GEMV launches 113 to 65, kernel time 5.46 to 5.30 ms, dispatch gaps 1.64 to 1.81 ms (within trace noise), step 7.10 to 7.11 ms. Per-launch medians: fused q/k/v 19.4 us for 3.1 MB (was 23.9 us for three launches), fused gate/up 82.0 us for 16 MB (was 86.8), down 62.5, o_proj 14.3, head 619.5.Validation
iree-run-modulewith three different output widths (2048, 512, 512), so the cumulative row selection is exercised: max absolute error 8.6e-6, and the two 512-wide sets return distinct correct results.xla_batch_benchon hip withMLXCEL_XLA_QUANT=native:REFERENCE-EXACT PASSat B=8.cargo fmt --all -- --check;cargo check --features xla-backend,xla-ireeunderRUSTFLAGS=-D warnings.Follow-ups
Attention cost (1.3 ms per step) and the ragged batch engine's weight path are still open; the CUDA port of the kernel is tracked in #2905.
Refs #2895