Skip to content

perf(xla): pass the decode scalars as ABI i32 arguments - #3065

Merged
inureyes merged 3 commits into
mainfrom
perf/issue-3060-abi-scalars
Nov 16, 2026
Merged

inureyes merged 3 commits into
mainfrom
perf/issue-3060-abi-scalars

Conversation

@inureyes

@inureyes inureyes commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

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 three stream.resource.loads, which on gfx1151 is a pinned hipHostMalloc/hipHostFree pair 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 no stream.resource.load.

What changed

  • emitter/model.rs: ArgDecl gains an abi_scalar flag, render_signature prints a bare i32 for a marked argument, and build_arg_schema marks token/pos/cache_len only for the 1-D single-sequence decode. The prologue rebuilds the rank-0 tensor operands with tensor.from_elements, so the op sequence after it is unchanged. The module exports a no-op decode_step.scalar_inputs marker.
  • M-RoPE keeps its tensor<3xi32> position and therefore the whole buffer-argument ABI (with its residual staging); prefill, embeddings prefill, ragged/lockstep batch decode, Gemma3n decode and the phi4 adapter_mode argument are untouched.
  • csrc/xla_iree.c: probes decode_step.scalar_inputs exactly like kv_import, and on a hit pushes token/pos/cache_len with iree_vm_list_push_value instead 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.
  • Regenerated the 15 committed assets/*/decode.mlir goldens for the signature change and the resulting SSA renumbering. A normalized comparison against the previous goldens shows the only op-level deltas are the three tensor.from_elements prologue lines, the slice and mask sites that consume their results, and the marker entry. Prefill, ragged and batched goldens are untouched.
  • Tests: an emitter unit test asserts the 1-D signature, the three rebuilds, the marker, and unchanged M-RoPE/ragged/batched/prefill signatures. tests/xla_decode_scalar_reuse.rs loads 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 stream @main tail
before (6e2e6810) 12 3 %arg146..148: !hal.buffer_view
after 0 0 %arg146: i32, %arg147: i32, %arg148: i32

Paired A/B with scripts/xla/ab_decode_pair.sh --old 6e2e6810 --new HEAD --device hip --quant native --rounds 6 --tokens 128, capacity 512:

Round before (tok/s) after (tok/s)
1 198.55 200.64
2 195.51 199.87
3 196.46 197.89
4 195.61 198.39
5 190.17 205.10
6 192.84 212.58
median 195.56 200.25

+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 onto main; 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. The test-utils variant reports four pre-existing private_interfaces lints in IreeRaggedLlama::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.
  • Device gates under scripts/rocm_gpu_guard.sh --hold -- on gfx1151: xla_decode_scalar_reuse 4/4 (scalar ABI, legacy buffer-view artifact, forced fallback, prefill-only) and xla_greedy_timing 3/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 the decode_result slot. 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

@inureyes inureyes added status:review Under review type:performance Performance improvements area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 15, 2026
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
@inureyes
inureyes force-pushed the perf/issue-3060-abi-scalars branch from 3f9279b to 9246d11 Compare November 15, 2026 23:52
@inureyes

Copy link
Copy Markdown
Member Author

Measurement and device verification on gfx1151

Streaming IR: the staging resource is gone

Compiled the real llama-3.2-1b decode graph (--compile-to=stream, --iree-hal-target-device=hip --iree-rocm-target=gfx1151, the decode graph plus the aggressive-fusion flag) for both revisions:

!stream.resource<staging> stream.resource.load stream @main tail
before (6e2e6810) 12 3 %arg146..148: !hal.buffer_view
after 0 0 %arg146: i32, %arg147: i32, %arg148: i32

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

scripts/xla/ab_decode_pair.sh --model models/llama-3.2-1b-instruct-4bit --device hip --quant native --old 6e2e6810 --new HEAD --rounds 6 --tokens 128, capacity 512, interleaved rounds:

Round before (tok/s) after (tok/s) before (ms) after (ms)
1 198.55 200.64 644.66 637.95
2 195.51 199.87 654.69 640.42
3 196.46 197.89 651.53 646.82
4 195.61 198.39 654.38 645.20
5 190.17 205.10 673.10 624.08
6 192.84 212.58 663.77 602.14
median 195.56 200.25 652.17 641.39

+2.40% median, ahead in 6 of 6 paired rounds, text EQUAL (greedy output byte-identical at 128 tokens). The step drops by about 0.11 ms, less than the ~0.35 ms of step-start idle the full 192-byte churn was costed at in #3048, so part of that idle was not the staging pair itself; the direction and the round record are unambiguous.

Device gates

Run under scripts/rocm_gpu_guard.sh --hold -- on gfx1151:

  • cargo test -p mlxcel --features xla-iree,test-utils --test xla_decode_scalar_reuse -- --ignored: 4 passed. Includes the new legacy-artifact test, which loads a buffer-view-ABI artifact and a scalar-ABI artifact in one process and compares their token streams, and the forced readback fallback, which now expects (0, 0).
  • cargo test -p mlxcel --features xla-iree,test-utils --test xla_greedy_timing -- --ignored: 3 passed.

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
@inureyes

Copy link
Copy Markdown
Member Author

PR finalization

Added the missing documentation for the new decode ABI in src/lib/mlxcel-xla/README.md: token/pos/cache_len arrive as ABI-level i32 VM arguments, the graph rebuilds tensor operands with tensor.from_elements, and the shim probes decode_step.scalar_inputs to keep the buffer-view path for older bundles and M-RoPE. No Korean counterpart exists for this document.

Coverage against the issue's acceptance criteria already holds: emitter::tests::single_decode_takes_its_scalars_as_abi_i32_arguments pins the 1-D signature and the unchanged M-RoPE, ragged, batch and prefill signatures plus the marker; the golden byte-for-byte test passes on the refreshed assets; tests/xla_decode_scalar_reuse.rs covers the two-ABI stream equality and the fallback on a device. No tests added, since those paths are already gated.

Validation: cargo fmt --all -- --check clean; cargo clippy -p mlxcel-xla --all-targets -- -D warnings clean; emitter::tests 65 passed, 0 failed.

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
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 16, 2026
@inureyes
inureyes merged commit 1bd2796 into main Nov 16, 2026
13 checks passed
@inureyes
inureyes deleted the perf/issue-3060-abi-scalars branch November 16, 2026 00:24
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(xla): pass the decode scalars as ABI i32 arguments to drop the 192-byte staging readback

1 participant