Skip to content

perf(xla): thread the load-time weight conversions - #2981

Merged
inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:perf/xla-thread-weight-conversions
Nov 15, 2026
Merged

inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:perf/xla-thread-weight-conversions

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

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
  • 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

  • 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)
  • Validated with real checkpoints: mlx-community/Llama-3.2-1B-Instruct-4bit, mlx-community/Qwen2.5-7B-Instruct-4bit
  • 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 fix(xla): split ragged load timing, restore decode-only tok/s (#2961) #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

  • PR title uses a Conventional Commits prefix (feat:, fix:, etc.)
  • One logical change per PR (split unrelated changes)
  • Updated docs/ if user-facing behavior or supported models changed (no user-facing change)
  • Updated // Used by: ... comments on any shared function I modified (see docs/code-guidelines.md) (none carry one)
  • No secrets, credentials, or .env files committed

The f16 weight upload is host-bound. lablup#2976 split the load span and showed
it on gfx1151: of about 5 s, 1.9 s is affine dequantization and 1.9 s is
f16 packing, both single-threaded, against about 1 s of device copy.

dequantize_affine, pack_f16 and transpose_projection now fill their output
in bands of whole rows on scoped threads (at most 16, and only for tensors
of at least 2^20 elements). Each element is written by one thread with the
serial loop's arithmetic, so the result is bit-identical for every thread
count. f16_to_f32 builds its 65536-entry table once per process instead of
once per call.

On a DGX Spark GB10 (llama-3.2-1b-instruct-4bit, cuda, three interleaved
runs) the batch engine's ragged-context-and-upload goes from 5.8-5.9 s to
2.0-2.1 s and the single-sequence language-total from 9.6-9.8 s to
3.4-3.5 s. Twelve 128-token greedy trajectories are identical before and
after, and xla_batch_bench stays REFERENCE-EXACT at B=4 and B=8 with the
decode throughput unchanged.

Refs lablup#2961
@inureyes inureyes added priority:medium Medium priority status:review Under review type:performance Performance improvements labels Nov 15, 2026
@inureyes

Copy link
Copy Markdown
Member

Reviewed PR #2981. Reviewed #2961 and #2976 and the real weight-upload callers. Disjoint mutable row bands avoid data races; small tensors stay serial, workers are capped at 16, and the f16 lookup table is initialized once. cargo test -p mlxcel-xla --lib passed (410 passed, 19 ignored), including bitwise serial-versus-threaded conversions; cargo clippy -p mlxcel-xla --all-targets -- -D warnings passed. IREE/ROCm performance measurements were not repeated.

No correctness, security or performance defect found in the changed paths. No code fixes required. Hosted checks are rechecked immediately before merge.

1 similar comment
@inureyes

Copy link
Copy Markdown
Member

Reviewed PR #2981. Reviewed #2961 and #2976 and the real weight-upload callers. Disjoint mutable row bands avoid data races; small tensors stay serial, workers are capped at 16, and the f16 lookup table is initialized once. cargo test -p mlxcel-xla --lib passed (410 passed, 19 ignored), including bitwise serial-versus-threaded conversions; cargo clippy -p mlxcel-xla --all-targets -- -D warnings passed. IREE/ROCm performance measurements were not repeated.

No correctness, security or performance defect found in the changed paths. No code fixes required. Hosted checks are rechecked immediately before merge.

@inureyes
inureyes merged commit 668c9d2 into lablup:main Nov 15, 2026
31 checks passed
@inureyes inureyes added status:done Completed priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) and removed status:review Under review priority:medium Medium priority labels Nov 15, 2026
@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.

2 participants