Repository navigation
fix(cli): report XLA prefill and decode separately (#3041) - #3042
Merged
Merged
Conversation
`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
This was referenced Nov 14, 2026
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.
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
mlxcel generate --profileon 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 inn(154.8 to 165.9 tok/s atn=64to512) 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_xlabuilt its statistics withgeneration_stats_from_duration(prompt_tokens.len(), generated_len, total_time). That helper hardcodesprefill_time_ms: 0.0andprefill_tok_per_sec: 0.0, and divides every generated token bytotal_time, which starts before prefill. The--profileline therefore printed:A
rocprofv3kernel trace ofn=64on 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 atn=64, 75.7 atn=128, 114.0 atn=256, 132.6 atn=512.This also means the absolute decode figures in earlier OpenXLA performance reports, including the
150.1 tok/scombined 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: newGreedyTiming { prefill_ms, decode_ms }andXlaInferenceSession::generate_streaming_greedy_timed, which times the existingprefillcall and the decode loop (decode_stepplusdrive_greedy) separately.generate_streaming_greedydelegates to it and discards the timings, so existing callers are unchanged. The decode window starts at the firstdecode_step, which emits the first generated token, so all generated tokens belong in its numerator, unlike the MLXDirectEnginewhere the first token comes out of prefill; that difference is documented on the method.src/commands/generate.rs: the plain text arm ofgenerate_xlauses the timed entry point and fillsprefill_time_ms,prefill_tok_per_sec(over the prompt prefix, one token short) anddecode_tok_per_secfrom 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]]withrequired-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: UnreleasedFixedentry.Validation
cargo clippyreports the same pre-existingprivate_interfaceswarnings in untouchedprefill_*_prepared_slot_logitsmethods plus onefield_reassign_with_defaultin an untouched test module; none come from this change.Sweep with the rebuilt binary, native quant, context capacity 1024, one warm-up run first:
Decode is now flat in
nwithin 7 percent and the decode step is about 6.2 ms, against the roughly 590 ms prefill this prompt pays.Refs #3041