Repository navigation
Description
Summary
A rocprofv3 kernel trace of the XLA/IREE decode path on gfx1151 (llama-3.2-1b-instruct-4bit, hip, 128 greedy tokens) shows 430 kernel dispatches and 96.5 ms of device time per decode step. 112 of those dispatches are transposes of the projection weights, about 57 ms (59% of the step): 32 x transpose_8192x2048_f16 (gate/up), 16 x transpose_2048x8192_f16 (down), 32 x transpose_2048x2048_f16 (q/o), 32 x transpose_512x2048_f16 (k/v). A further elementwise_transpose_128256x2048_f32xf16 (the tied embedding used as the LM head, still f32-resident) costs about 10 ms per step. The actual GEMV work is about 34 ms. The MLX ROCm path on the same model runs 4.1 ms of kernel time per step, 90% in qmv_wide_kernel, with zero transpose kernels.
Root cause
The emitter stores weights as the checkpoint has them, [out, in], and Builder::linear / linear_seq (src/lib/mlxcel-xla/src/emitter/builder.rs:1825-1837) contract the input feature axis with the weight's in axis (contracting_dims = [0] x [1]). IREE lowers that dot_general by materializing a transpose into [in, out] and then running a vecmat/matmul, as a separate dispatch.
Verified directly with the pinned iree-compile (3.12.0rc20260721, --iree-hal-target-device=hip --iree-rocm-target=gfx1151 --iree-hal-dump-executable-sources-to):
// current: weights [out, in]
%0 = stablehlo.dot_general %x, %w, contracting_dims = [0] x [1] : (tensor<2048xf16>, tensor<8192x2048xf16>) -> tensor<8192xf32>produces two dispatches, the first a linalg.generic transpose (affine_map -> (d1, d0)) of the whole weight, the second the vecmat.
// pre-transposed weights [in, out]
%0 = stablehlo.dot_general %x, %w, contracting_dims = [0] x [0] : (tensor<2048xf16>, tensor<2048x8192xf16>) -> tensor<8192xf32>produces a single dispatch, no transpose.
Proposed fix
Store projection weights pre-transposed as [in, out] and contract the weight's in axis (dim 0) instead of dim 1.
- Emitter: change
Builder::linear,linear_seq, andlinear_lastto expect[in, out]and emitcontracting_dims = [lhs_in] x [0]; the outputNcomes from the weight's last axis. Model emitters call only these helpers, so their call sites stay unchanged apart from the argument shapes they declare. - Loader: transpose each projection once at upload.
WeightSpec::ProjandWeightSpec::Rows(after the row slice) become[in, out]; the f16-resident packing transposes while converting. Keep embedding, norm, and other non-matmulWholetensors unchanged. - Packed path (
MLXCEL_XLA_QUANT=packed):Builder::dequant_affineshould yield[in, out](or keep its own[out, in]layout and contract dim 1) so the packed opt-in still works. Whichever way, cover it with a golden and note it in the PR. - LM head: declare the untied head as
[in, out]f16-resident under the f16 path instead of f32[V, H]. A tied checkpoint reuses the embedding, which the gather needs as[V, H]; if transposing the shared buffer per step cannot be avoided cheaply, upload one f16 transposed head copy for the tied case and document the extra memory (about 525 MB for a 128256 x 2048 vocabulary). This is the separate ~10 ms item; if it grows the change too much it may land as a follow-up, but it should be stated in the issue. - Re-freeze the committed graph goldens (
MLXCEL_FREEZE_GOLDENS=1,src/lib/mlxcel-xla/src/validation.rs) and confirm the diffs are limited to argument shapes and dot_general contracting dims. No numerical change is expected: the same f16 values are multiplied.
Expected effect
The projection transposes are 57 of 96.5 ms per step. Removing them projects to about 39 ms per step, roughly 11.5 to 25 tok/s single stream on llama-3.2-1b. The LM head item is another ~10 ms, about 33 tok/s. The remaining GEMV cost (about 34 ms versus MLX's 4.1 ms) is a separate kernel-efficiency problem and is out of scope here.
Scope
In scope: src/lib/mlxcel-xla/src/emitter/builder.rs (linear helpers, dequant), src/lib/mlxcel-xla/src/emitter/model.rs and any emitter that declares weight args, src/lib/mlxcel-xla/src/weights.rs (transposing upload), the committed assets under src/lib/mlxcel-xla/assets/, and the tests that pin argument shapes.
Out of scope: GEMV/vecmat kernel efficiency, dispatch count reduction, paged KV, non-XLA backends, graphs that are not projection matmuls.
Acceptance criteria
-
rocprofv3 --kernel-traceof a 128-token decode onllama-3.2-1b-instruct-4bit(hip) shows notranspose_*kernels for the projection weights, and the per-step device time drops from about 96.5 ms to about 40 ms or less. -
xla_batch_benchat B=4 N=8 and B=8 N=16 is still REFERENCE-EXACT PASS on hip, and the CLI output is token-identical to the pre-change binary for the same prompt and seed (--no-chat-template, greedy). -
cargo test -p mlxcel-xla --libpasses with the re-frozen goldens; the golden diff contains only argument-shape and contracting-dim changes. - The packed path (
MLXCEL_XLA_QUANT=packed) still compiles and passes its reference check, or the limitation is stated explicitly in the PR. - MLX ROCm output remains unchanged (no non-XLA files touched).
Activity
perf(xla): load single-sequence graphs with transposed weights (#2859) (
fix(xla): upload StarCoder2 c_fc in the graph's projection layout
fix(xla): expand Youtu-VL MLA latents in the transposed weight layout (…