Repository navigation
perf(xla): pass the decode scalars as ABI i32 arguments - #3065
Conversation
The single-sequence decode step took token, position and cache_len as rank-0 tensor<i32> buffer arguments. Because the graph uses their values as host-side slice offsets and mask bounds, IREE materialized a 192-byte !stream.resource<staging> and three stream.resource.loads per invocation, which on gfx1151 costs a pinned hipHostMalloc/hipHostFree pair per step. The decode emitter now renders those three arguments as bare i32 in the signature and rebuilds the rank-0 tensor operands with tensor.from_elements at the prologue, so the op sequence after it is unchanged. The module exports a no-op decode_step.scalar_inputs marker; the shim probes it like kv_import and pushes the values with iree_vm_list_push_value. Artifacts without the marker keep the buffer-view path, so the bundle is not versioned. M-RoPE keeps its tensor<3xi32> position and the whole buffer-argument ABI, and prefill, ragged/lockstep batch decode and Gemma3n are untouched. Refreshed the committed decode goldens for the signature change and the SSA renumbering. Added an emitter unit test for the signature, a shim counter hook, and a device gate that decodes the scalar-ABI and legacy buffer-view artifacts and compares their token streams. Refs #3060
3f9279b to
9246d11
Compare
Measurement and device verification on gfx1151Streaming IR: the staging resource is goneCompiled the real llama-3.2-1b decode graph (
The three host loads and the 192-byte pinned staging buffer IREE materialized per invocation are absent, and the ABI itself carries the scalars. Paired A/B
+2.40% median, ahead in 6 of 6 paired rounds, Device gatesRun under
|
Add the single-sequence decode ABI to the mlxcel-xla README: token, position and cache-len arrive as ABI-level i32 VM arguments rather than rank-0 tensor buffer views, which removes the host-visible staging resource IREE materialized per step. The graph rebuilds the tensor operands with tensor.from_elements, and the shim probes the decode_step.scalar_inputs marker so an artifact without it keeps the buffer-view path. This records the compatibility behavior for M-RoPE and older bundles, which is why the bundle is not versioned. Refs #3060
PR finalizationAdded the missing documentation for the new decode ABI in Coverage against the issue's acceptance criteria already holds: Validation: |
Records the staging readback removal from the rank-0 decode scalar arguments: the measured paired A/B (195.56 to 200.25 tok/s, 6/6 rounds, identical greedy output), the stream-IR evidence (no staging resource, no stream.resource.load), and why M-RoPE keeps the buffer-argument ABI. Refs #3060
Summary
The single-sequence XLA decode step passed token, position and cache_len as rank-0
tensor<i32>buffer arguments. IREE had to bring their values to the host for slice offsets and mask bounds, so every invocation materialized a 192-byte!stream.resource<staging>with threestream.resource.loads, which on gfx1151 is a pinnedhipHostMalloc/hipHostFreepair per step. This change moves those three values into the VM call itself, so the staging resource does not exist in the compiled graph.Measured on
gfx1151: decode rises from 195.56 to 200.25 tok/s (+2.40%, six of six paired rounds, greedy output byte-identical), and the compiled decode graph has no staging resource and nostream.resource.load.What changed
emitter/model.rs:ArgDeclgains anabi_scalarflag,render_signatureprints a barei32for a marked argument, andbuild_arg_schemamarks token/pos/cache_len only for the 1-D single-sequence decode. The prologue rebuilds the rank-0 tensor operands withtensor.from_elements, so the op sequence after it is unchanged. The module exports a no-opdecode_step.scalar_inputsmarker.tensor<3xi32>position and therefore the whole buffer-argument ABI (with its residual staging); prefill, embeddings prefill, ragged/lockstep batch decode, Gemma3n decode and thephi4adapter_modeargument are untouched.csrc/xla_iree.c: probesdecode_step.scalar_inputsexactly likekv_import, and on a hit pushes token/pos/cache_len withiree_vm_list_push_valueinstead of buffer views. An artifact without the marker keeps the existing buffer-view path, so the bundle is not versioned. A marked graph that ever meets an M-RoPE call is rejected instead of pushing a wrong value type. The position and cache_len bounds checks stay.src/lib/mlxcel-xla/README.md: documents the decode scalar ABI and the marker's compatibility contract.assets/*/decode.mlirgoldens for the signature change and the resulting SSA renumbering. A normalized comparison against the previous goldens shows the only op-level deltas are the threetensor.from_elementsprologue lines, the slice and mask sites that consume their results, and the marker entry. Prefill, ragged and batched goldens are untouched.tests/xla_decode_scalar_reuse.rsloads both a scalar-ABI artifact and a legacy buffer-view artifact in one process, asserts the probe result on each, and compares their token streams; the forced readback fallback expectation moves to(0, 0)because inputs no longer allocate views.Measurement
Compiled the real llama-3.2-1b decode graph to stream IR (
--iree-hal-target-device=hip --iree-rocm-target=gfx1151, decode flags including aggressive fusion):!stream.resource<staging>stream.resource.load@maintail6e2e6810)%arg146..148: !hal.buffer_view%arg146: i32, %arg147: i32, %arg148: i32Paired A/B with
scripts/xla/ab_decode_pair.sh --old 6e2e6810 --new HEAD --device hip --quant native --rounds 6 --tokens 128, capacity 512:+2.40% median, 6/6 rounds,
text EQUAL. Per step that is about 0.11 ms, less than the roughly 0.35 ms of step-start idle #3048 associated with the 192-byte churn, so part of that idle was not the staging pair itself. The A/B baseline is the #3048 merge, so its decode step already had the KV cache in module globals and no KV copy.Verification
cargo test -p mlxcel-xla --lib: 417 passed, 0 failed, 19 ignored (after rebasing ontomain; includes the byte-exact golden guard).cargo check -p mlxcel --features xla-iree,test-utils --lib --tests: clean.cargo clippy -p mlxcel --features xla-iree --lib --tests -- -D warnings: clean. Thetest-utilsvariant reports four pre-existingprivate_interfaceslints inIreeRaggedLlama::prefill_*_prepared_slot_logits, untouched here.cargo clippy -p mlxcel-xla --all-targets -- -D warnings: clean.cargo fmt --all -- --check: clean.gcc -fsyntax-only -std=gnu11 -DMLXCEL_XLA_TEST_SUPPORT=1 csrc/xla_iree.c: clean.scripts/rocm_gpu_guard.sh --hold --on gfx1151:xla_decode_scalar_reuse4/4 (scalar ABI, legacy buffer-view artifact, forced fallback, prefill-only) andxla_greedy_timing3/3.Follow-ups
The scalar path still calls
xla_step_scalars_ensure, which allocates the three input maps it no longer uses, only to obtain thedecode_resultslot. That is one allocation per context, not per step, so it is left for a later cleanup. M-RoPE keeps the buffer ABI and its staging; teaching the shim a second scalar shape would make the capability probe a two-way signal for no measured gain today.Closes #3060