Skip to content

fix(xla): split ragged load timing, restore decode-only tok/s (#2961) - #2976

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2961-f16-ragged-load-timing
Nov 11, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2961-f16-ragged-load-timing

Conversation

@inureyes

Copy link
Copy Markdown
Member

What changed

  • iree.rs: the opaque load span is reported in parts through the existing report_load_timing gate. language-module-load covers xla_llama_create (instance, device, session, vmfb module load), language-weight-upload covers upload_weights_with_sink, and language-weight-upload-parts appends tensors, uploaded_bytes, dequantize_ms, pack_ms and copy_ms. A capacity bucket prints the parts line with zero counters because it shares the base context's views. No line is printed when MLXCEL_XLA_LOAD_TIMING is unset.
  • Test support: the shim counts xla_llama_upload_weight calls and bytes under MLXCEL_XLA_TEST_SUPPORT, and IreeRaggedLlama::weight_count() exposes the declared spec count. The ignored root test base_load_uploads_each_weight_once_and_a_bucket_uploads_none asserts one upload per spec and zero for a bucket.
  • examples/xla_batch_bench.rs: the engine is loaded before t_run, and the phase 1 and phase 2 load times print separately, so BENCH reports decode-only tok/s, the convention in docs/benchmarks.md.
  • docs/environment-variables.md and CHANGELOG.md describe the new fields and the window fix.

Root cause of the throughput drop

#2936 (311ad935) replaced the inline pump loop with drive(...) and, in that move, constructed the engine after let t_run = Instant::now();. The pre-#2936 harness started the clock after the engine load. With the f16 ragged load at 4.9 to 5.6 s, that one-time cost landed in the denominator, which is the whole difference between the 60 to 65 tok/s the harness used to report and the 20.6 to 22.5 tok/s the issue recorded. With the load excluded, the same runs decode at a median of 64.0 tok/s at B=4, and the pre-#2933 control harness, which already excluded the load, reports 63.4.

The load itself

Medians of three measured runs, capacity 256 unless stated. Load is ragged-context-and-upload on HEAD and the inner language-vmfb-and-device-upload on the control, which is the same span in both.

Arm Capacity B Load (s) Module load (s) Upload (s) Dequantize (s) Pack (s) Copy (s) Tensors Uploaded bytes
HEAD f16 256 4 5.62 0.20 5.37 1.88 1.91 1.56 146 2,997,100,544
HEAD f16 256 8 5.03 0.20 4.83 1.88 1.94 0.99 146 2,997,100,544
HEAD f16 1024 4 4.92 0.20 4.71 1.80 1.91 0.98 146 2,997,100,544
HEAD f16 1024 8 4.94 0.20 4.74 1.83 1.91 0.97 146 2,997,100,544
HEAD native 256 4 1.17 0.21 0.95 0.45 0.00 0.46 370 1,598,300,160
HEAD native 256 8 1.15 0.21 0.93 0.41 0.00 0.43 370 1,598,300,160
Control f16 256 4 4.89 not printed not printed not printed not printed not printed not printed not printed
Control f16 256 8 4.93 not printed not printed not printed not printed not printed not printed not printed
  • The f16 cost is flat in capacity: 5.62 s at 256 against 4.92 s at 1024 at B=4, and 5.03 against 4.94 at B=8. It is the 3.0 GB weight upload, split as host dequantize at 1.8 to 1.9 s, f16 pack at 1.9 s, and the device copy at 0.9 to 1.9 s.
  • The pre-perf(xla): run the ragged engine through the native 4-bit GEMV #2933 control (fd2a65ea) loads the same span in 4.89 s at B=4 and 4.93 s at B=8 on the same checkpoint, host, IREE distribution and capacity. The 0.7 s figure in the issue does not reproduce: the control is the same ~5 s, HEAD is 0.7 s slower at B=4 and 0.1 s slower at B=8, and both differences are inside the copy-time spread. There is no 7x load regression to revert.
  • Reaching 0.7 s would require not expanding the weights on the host at all, that is, uploading the packed U32 words and dequantizing on the device. That means a different ragged graph and re-validating reference exactness, and the project already records that packed decode is slower than host-dequantized f16 on hip. Since decode throughput, not load time, is the perf(xla): run the ragged engine through the native 4-bit GEMV #2933 gate and the f16-resident path is unchanged from the control, this issue keeps f16-resident weights and records the trade-off instead.
  • Both loads in a run are identified: phase 1 is the single-sequence reference engine (IreeLlama, 147 tensors, 3.52 GB, about 12.6 s upload), phase 2 is the ragged batch engine (IreeRaggedLlama, 146 tensors, 3.0 GB). ragged-context-and-upload exists only on the phase-2 ragged load, and the control prints only the inner label, which is the comparable number.
  • Upload accounting: 146 calls for 146 declared specs on the f16 ragged load, 370 for native, and zero calls and zero bytes for a capacity bucket.

#2933 absolute targets, decode-only

Arm B #2933 recorded Runs (tok/s) Median Status
f16 4 65.6 66.6, 61.2, 64.0 64.0 2.4% short, not a clean pass
f16 8 59.8 59.9, 60.4, 65.0 60.4 pass
native 4 60.0 to 62.2 59.8, 61.7, 70.7 61.7 pass, inside the recorded range
native 8 74.5 to 76.7 69.6, 75.8, 80.9 75.8 pass, inside the recorded range

Reference exactness: every native run and every f16 B=8 run prints REFERENCE-EXACT PASS. The f16 B=4 arm diverges at request 2, token 3, in all three HEAD runs and in all three runs of the pre-#2933 control with the same prompt artifact, so the divergence is a near-tie in the f16 ragged path that predates this change and is not caused by it. A repeat of the B=4 f16 target with a natural-text prompt artifact of the same shape prints REFERENCE-EXACT PASS at 63.1 tok/s, so the divergence is prompt-specific and not a path regression.

Measurement protocol

  • HEAD ce6855bc plus this change; control built from fd2a65ea (the parent of the perf(xla): run the ragged engine through the native 4-bit GEMV #2933 commit) in a separate tree from git archive, with an identical Cargo.lock and the same IREE ROCm 3.12.0rc20260721 tree.
  • gfx1151, every run under scripts/rocm_gpu_guard.sh --hold, one process per run, MLXCEL_XLA_DEVICE=hip, MLXCEL_XLA_QUANT unset for the f16 arm, MLXCEL_XLA_CONTEXT_CAPACITY 256 unless stated, warm checkpoint page cache, one warmup per arm, then three measured runs interleaved so host drift hits every arm alike; medians are reported.
  • The default spike/openxla/artifacts/prompt_ids.json is not in the checkout. I generated a 63-id natural-text artifact from the checkpoint's tokenizer; the harness derives the same request lengths (20 to 62) and the same caps as perf(xla): run the ragged engine through the native 4-bit GEMV #2933, and the runs generate 170 tokens at B=4 and 420 at B=8, which matches the perf(xla): run the ragged engine through the native 4-bit GEMV #2933 workload and its 46 and 69 pump steps.
  • Run-to-run spread on this host is 5 to 15% with a mild upward drift over the session, so the tables report all three values rather than a single run.

Verification

  • make verify-fmt: pass.
  • cargo clippy -p mlxcel-xla --features iree --lib --tests -- -D warnings: pass.
  • cargo test -p mlxcel-xla --lib: 407 passed, 19 ignored.
  • cargo test --release -p mlxcel-xla --lib --features iree load_timing_accounting_tests: 3 passed (byte accounting, detail formatting, line shape with and without detail).
  • cargo test --test llama_compat_manifest: 4 passed.
  • make verify-env-var-docs: 15 checks pass.
  • cargo test --release --features xla-iree,test-utils --test xla_ragged_hardening base_load_uploads_each_weight_once_and_a_bucket_uploads_none -- --ignored: 1 passed, asserting one upload per declared spec on the base load and zero calls and bytes for the capacity bucket.

Closes #2961.

@inureyes inureyes added status:done Completed type:performance Performance improvements priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 11, 2026
Split the ragged load span into module load and weight upload through the existing MLXCEL_XLA_LOAD_TIMING reporter. The parts line now carries the uploaded tensor count, bytes, host dequantize time, f16 pack time and device copy time. A capacity bucket reports zero upload because it shares the base context's views. No new labels are printed when the variable is unset.

Add test-support counters for xla_llama_upload_weight and an ignored root device test that asserts one upload per declared weight spec and none for a capacity bucket.

xla_batch_bench loads the engine before its timed window again. #2936 (311ad93) moved the load under t_run while replacing the pump loop with drive, which is what dropped the f16 B=4 reading from the control's 63.4 tok/s to 20.8 and made the #2933 targets look unverifiable. The pre-#2933 control (fd2a65e) loads the same span in 4.9 s on the same checkpoint, host, IREE distribution and capacity, so the 0.7 s figure in the issue does not reproduce. The f16 ragged load is the host dequantize, f16 pack and 3.0 GB device copy, unchanged from the control, and flat across capacity 256 and 1024.

Measured on gfx1151, medians of three runs: f16 ragged load 5.62 s at B=4 and 5.03 s at B=8, native 1.17 s and 1.15 s. Decode-only medians are 64.0 and 60.4 tok/s for f16 and 61.7 and 75.8 for native at B=4 and B=8.
@inureyes
inureyes force-pushed the fix/issue-2961-f16-ragged-load-timing branch from 9a6e3ab to 1ae8eb8 Compare November 11, 2026 16:35
@inureyes
inureyes marked this pull request as ready for review November 11, 2026 16:39
@inureyes
inureyes merged commit 955335a into main Nov 11, 2026
31 checks passed
@inureyes
inureyes deleted the fix/issue-2961-f16-ragged-load-timing branch November 11, 2026 16:40
inureyes pushed a commit that referenced this pull request Nov 15, 2026
## Summary

Follow-up to #2976. That change split the OpenXLA load span and showed the f16 weight upload is host-bound: on gfx1151 about 3.8 of 5 s is affine dequantization and f16 packing, both single-threaded. This threads those conversions. On a DGX Spark GB10 the batch engine's load goes from 5.8 to 5.9 s to 2.0 to 2.1 s with bit-identical weights.

## Related issues

Refs #2961, #2976

## Type of change

- [ ] `feat` — new user-visible feature
- [ ] `fix` — bug fix
- [x] `perf` — performance improvement (include before/after numbers in the PR body)
- [ ] `refactor` — internal restructuring without behavior change
- [ ] `chore` — build, CI, dependencies, release infrastructure
- [ ] `docs` — documentation only
- [ ] `test` — tests only

## What changed

One file, `src/lib/mlxcel-xla/src/weights.rs`:

- `fill_rows_parallel` fills an output slice in bands of whole rows, one `std::thread::scope` thread per band. `dequantize_affine` (through `dequantize_affine_with_metadata`), `pack_f16` and `transpose_projection` use it.
- `conversion_threads` picks the thread count: one below 2^20 elements, otherwise `available_parallelism` capped at 16.
- `f16_to_f32` builds its 65536-entry table once per process (`OnceLock`) instead of once per call. A quantized checkpoint called it twice per projection.

Each element is written by exactly one thread with the arithmetic the serial loop used, so the output does not depend on the thread count. No new dependency, no new environment variable, no change to the loader or the graphs.

## Measurements

DGX Spark GB10 (20 cores, sm_121, CUDA 13.0, driver 580.178.04), IREE 3.12.0rc20260721, `mlx-community/Llama-3.2-1B-Instruct-4bit`, `--device cuda`, `MLXCEL_XLA_QUANT` unset, release build with `--features cuda,xla-iree`. Base `955335a3`. `xla_batch_bench` with `MLXCEL_XLA_LOAD_TIMING=1`, main and patched binaries interleaved, three runs each at B=4 and at B=8. The load does not depend on B, so the ranges below cover all six runs per binary.

Batch engine (146 tensors, 2,997,100,544 bytes):

| | main | this PR |
|---|---|---|
| `ragged-context-and-upload` | 5.81 to 5.92 s | 2.01 to 2.08 s |
| `language-weight-upload` | 5.61 to 5.74 s | 1.83 to 1.90 s |
| `dequantize_ms` | 2505 to 2567 | 340 to 347 |
| `pack_ms` | 1900 to 1924 | 254 to 261 |
| `copy_ms` | 996 to 1048 | 934 to 997 |
| `language-module-load` | 0.18 to 0.20 s | 0.18 to 0.19 s |

Single-sequence reference engine (147 tensors, 3,522,437,120 bytes):

| | main | this PR |
|---|---|---|
| `language-total` | 9.56 to 9.81 s | 3.37 to 3.47 s |
| `language-weight-upload` | 9.29 to 9.52 s | 3.11 to 3.20 s |
| `dequantize_ms` | 2575 to 2628 | 356 to 360 |
| `pack_ms` | 2531 to 2557 | 317 to 324 |

The single-sequence engine also transposes each projection; that pass is not in `pack_ms` but is in the upload total, which is why its total falls by more than the two listed parts.

Decode is untouched: 22.6 to 23.1 tok/s on main against 23.0 to 23.3 at B=4, and 31.9 to 32.5 against 32.6 to 32.9 at B=8.

Scaling of the batch engine upload with the affinity mask (`taskset`, fastest cores first, two runs each):

| cores | upload | dequantize | pack |
|---|---|---|---|
| 1 | 5.70 to 5.72 s | 2582 to 2586 ms | 1955 to 1959 ms |
| 2 | 3.52 to 3.53 s | 1351 to 1368 | 990 to 999 |
| 4 | 2.47 s | 742 to 746 | 507 to 513 |
| 8 | 1.86 to 1.94 s | 423 to 427 | 282 to 287 |
| 10 | 1.84 s | 368 to 369 | 239 to 241 |
| 16 | 1.78 to 1.80 s | 334 to 342 | 249 to 257 |
| 20 | 1.82 to 1.87 s | 344 | 256 |

One core reproduces main, and the gain is spent by 8 to 10 cores. That is where the cap of 16 comes from. What remains is the device copy, about 1 s for 3 GB, which this PR does not touch.

## Correctness

- `threaded_conversions_are_bit_identical_to_the_serial_reference`: on a 1024 x 1088 tensor, which takes the threaded path, `dequantize_affine`, `pack_f16` and `transpose_projection` equal an element-by-element serial reference bit for bit.
- `fill_rows_parallel_matches_the_serial_fill_for_every_thread_count`: 1 to 64 threads over 37 rows, which none of the tested counts above 1 divides.
- `conversion_threads_is_serial_below_the_threshold_and_capped_above`.
- Twelve 128-token greedy trajectories (`xla_traj_dump`, six prompts each on llama-3.2-1b-instruct-4bit and the untied Qwen2.5-7B-Instruct-4bit) are identical id for id between the main and the patched binary.
- `xla_batch_bench` on `cuda`: `REFERENCE-EXACT PASS` in all twelve interleaved runs (main and patched, B=4 and B=8) and in all fourteen scaling runs.

## Test plan

- [x] `cargo fmt --all -- --check`
- [ ] `cargo clippy --workspace --all-targets --features metal,accelerate -- -D warnings` (not run; Linux host). `cargo clippy -p mlxcel-xla --all-targets --features iree -- -D warnings` is clean.
- [ ] `cargo test --workspace --profile test-fast --features metal,accelerate --no-fail-fast -- --test-threads=1` (not run; Linux host)
- [ ] `cargo deny check` (not run; no dependency change)
- [x] Validated with real checkpoints: `mlx-community/Llama-3.2-1B-Instruct-4bit`, `mlx-community/Qwen2.5-7B-Instruct-4bit`
- [x] Before/after numbers and environment are in the PR body

Also on the GB10 host: `cargo test --release -p mlxcel-xla --lib` (410 passed, 19 ignored, no IREE environment), `cargo test --release -p mlxcel-xla --features iree --lib` (435 passed, 22 ignored), and `RUSTFLAGS="-D warnings" cargo check -p mlxcel-xla --all-targets`.

## Notes for reviewers

- **Only one host.** The cap and the curve come from a 20-core big.LITTLE machine on `cuda`. I could not run `hip`; from the #2976 table (dequantize 1.9 s, pack 1.9 s on gfx1151) I would expect a similar saving there, but that is an expectation, not a measurement.
- **Threads are spawned per large tensor**, about 150 times per load, not kept in a pool. The one-core row above equals main to within noise, so the spawn cost is not visible.
- **Not threaded:** `dequantize_affine_stacked` (the MoE expert bank), the two BF16 carrier variants Gemma3n uses, and the unquantized `bf16_to_f32` / `f32_le_to_f32` widening. They are not on the path measured here.
- **Memory.** The conversion output buffers have the same sizes. Peak RSS was not measured; per-thread stacks and the process-lifetime conversion table add overhead.

## Checklist

- [x] PR title uses a Conventional Commits prefix (`feat:`, `fix:`, etc.)
- [x] One logical change per PR (split unrelated changes)
- [x] Updated `docs/` if user-facing behavior or supported models changed (no user-facing change)
- [x] Updated `// Used by: ...` comments on any shared function I modified (see `docs/code-guidelines.md`) (none carry one)
- [x] No secrets, credentials, or `.env` files committed
@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) priority:high High priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(xla): investigate the f16 ragged-context-and-upload regression on the load path

1 participant