Skip to content

fix(cli): report XLA prefill and decode separately (#3041) - #3042

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-3041-xla-profile-phases
Nov 14, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-3041-xla-profile-phases

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

mlxcel generate --profile on the OpenXLA backend now reports prefill and decode from the two windows the session actually runs, instead of folding prefill into the decode rate. On the same environment decode is flat in n (154.8 to 165.9 tok/s at n=64 to 512) where it previously ranged from 51.5 to 132.6 because a fixed ~590 ms of prefill sat in the denominator.

Problem

The XLA return path in generate_xla built its statistics with generation_stats_from_duration(prompt_tokens.len(), generated_len, total_time). That helper hardcodes prefill_time_ms: 0.0 and prefill_tok_per_sec: 0.0, and divides every generated token by total_time, which starts before prefill. The --profile line therefore printed:

  Prefill:          0.00 ms (0.00 tok/s)
  Decode:           ...

A rocprofv3 kernel trace of n=64 on gfx1151 with llama-3.2-1b-instruct-4bit shows 693 prefill dispatches totaling 413.5 ms of kernel time (about 590 ms wall) against a 375.6 ms decode span. Every short run was diluted by that constant: 51.5 tok/s at n=64, 75.7 at n=128, 114.0 at n=256, 132.6 at n=512.

This also means the absolute decode figures in earlier OpenXLA performance reports, including the 150.1 tok/s combined median in #3031, include prefill. That comparison remains valid because both arms paid the same prefill, but it is not a decode rate, and the report for #3031 should be read that way.

Change

  • src/lib/mlxcel-xla/src/lib.rs: new GreedyTiming { prefill_ms, decode_ms } and XlaInferenceSession::generate_streaming_greedy_timed, which times the existing prefill call and the decode loop (decode_step plus drive_greedy) separately. generate_streaming_greedy delegates to it and discards the timings, so existing callers are unchanged. The decode window starts at the first decode_step, which emits the first generated token, so all generated tokens belong in its numerator, unlike the MLX DirectEngine where the first token comes out of prefill; that difference is documented on the method.
  • src/commands/generate.rs: the plain text arm of generate_xla uses the timed entry point and fills prefill_time_ms, prefill_tok_per_sec (over the prompt prefix, one token short) and decode_tok_per_sec from it. Image, audio and Gemma3n prepared inputs do not route through this entry point and keep the previous accounting.
  • tests/xla_greedy_timing.rs (new, [[test]] with required-features = ["xla-iree", "test-utils"]): two ignored device gates. One asserts non-zero prefill and decode windows and that the timed entry point produces the same token stream as the untimed one; the other pins the zero-budget case to a prefill-only window.
  • CHANGELOG.md: Unreleased Fixed entry.

Validation

cargo check --features xla-iree                                                              # pass
cargo fmt --all -- --check                                                                   # pass
cargo test --features "xla-iree test-utils" --test xla_greedy_timing -- --ignored --test-threads=1   # 2 passed
cargo clippy --features "xla-iree test-utils" --lib --tests                                  # no new warnings

cargo clippy reports the same pre-existing private_interfaces warnings in untouched prefill_*_prepared_slot_logits methods plus one field_reassign_with_default in an untouched test module; none come from this change.

Sweep with the rebuilt binary, native quant, context capacity 1024, one warm-up run first:

n prefill prefill tok/s decode decode tok/s
64 597.04 ms 68.67 400.96 ms 159.62
128 623.11 ms 65.80 771.54 ms 165.90
256 574.99 ms 71.31 1654.14 ms 154.76
512 590.05 ms 69.49 3157.45 ms 162.16

Decode is now flat in n within 7 percent and the decode step is about 6.2 ms, against the roughly 590 ms prefill this prompt pays.

Refs #3041

`mlxcel generate --profile` on the OpenXLA backend built its statistics from the whole run through `generation_stats_from_duration`, which hardcodes `prefill_time_ms = 0` and divides every generated token by the total wall time. The line therefore printed `Prefill: 0.00 ms (0.00 tok/s)` and the decode figure was really tokens over prefill plus decode. On gfx1151 with llama-3.2-1b-instruct-4bit a 42-token prompt spends about 590 ms of prefill, so one unchanged environment read 51.5 tok/s at n=64, 75.7 at n=128, 114.0 at n=256 and 132.6 at n=512, none of which was a decode rate.

The session already runs the phases separately: `generate_streaming_greedy_timed` now times the prompt-prefix prefill and the decode loop around the existing calls and returns both as `GreedyTiming`, and the untimed `generate_streaming_greedy` delegates to it. The CLI text path fills prefill and decode from those windows. The XLA decode window starts at the first `decode_step`, which emits the first generated token, so all generated tokens belong in the decode numerator; the prefill window seeds one token fewer than the prompt. Image, audio and Gemma3n prepared inputs keep the whole-run accounting because they do not route through this entry point.

Verification, same binary, native quant, context capacity 1024, after a warm-up run: prefill 68.7, 65.8, 71.3 and 69.5 tok/s and decode 159.6, 165.9, 154.8 and 162.2 tok/s at n=64, 128, 256 and 512. Decode is now flat in n within 7 percent instead of scaling with it. Two new ignored device gates in `tests/xla_greedy_timing.rs` pin the split and the token-stream identity of the timed entry point, plus a zero-budget case.

Refs #3041
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions status:review Under review labels Nov 14, 2026
@inureyes
inureyes merged commit a9dbb30 into main Nov 14, 2026
20 checks passed
@inureyes
inureyes deleted the fix/issue-3041-xla-profile-phases branch November 14, 2026 05:13
inureyes added a commit that referenced this pull request Nov 14, 2026
A single before/after run of `mlxcel generate --profile` moves 5 to 10 percent between runs on a thermally loaded host, so an apparent win can be drift. The new harness builds or takes both arms, runs them interleaved, reports both medians and how many paired rounds the new arm won, and requires the greedy text to match. It is the harness the decode changes of #3028, #3042 and #3043 were measured with.

Three cases are refused rather than measured because each has produced a wrong number before: two arms built from the same bytes, a zero prefill window (whole-run accounting, #3042), and differing text. Text equality is delegated to scripts/ab_output_equality.sh.

The per-device toolchain env is applied from scripts/iree/setup-*.sh when it is not already exported, and GPU use is serialized through gpu-lock on CUDA and scripts/rocm_gpu_guard.sh on HIP.
@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

status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant