Repository navigation
perf(xla): thread the load-time weight conversions - #2981
Conversation
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
|
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. 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
|
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. No correctness, security or performance defect found in the changed paths. No code fixes required. Hosted checks are rechecked immediately before merge. |
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 featurefix— bug fixperf— performance improvement (include before/after numbers in the PR body)refactor— internal restructuring without behavior changechore— build, CI, dependencies, release infrastructuredocs— documentation onlytest— tests onlyWhat changed
One file,
src/lib/mlxcel-xla/src/weights.rs:fill_rows_parallelfills an output slice in bands of whole rows, onestd::thread::scopethread per band.dequantize_affine(throughdequantize_affine_with_metadata),pack_f16andtranspose_projectionuse it.conversion_threadspicks the thread count: one below 2^20 elements, otherwiseavailable_parallelismcapped at 16.f16_to_f32builds 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_QUANTunset, release build with--features cuda,xla-iree. Base955335a3.xla_batch_benchwithMLXCEL_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):
ragged-context-and-uploadlanguage-weight-uploaddequantize_mspack_mscopy_mslanguage-module-loadSingle-sequence reference engine (147 tensors, 3,522,437,120 bytes):
language-totallanguage-weight-uploaddequantize_mspack_msThe single-sequence engine also transposes each projection; that pass is not in
pack_msbut 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):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_f16andtranspose_projectionequal 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.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_benchoncuda:REFERENCE-EXACT PASSin all twelve interleaved runs (main and patched, B=4 and B=8) and in all fourteen scaling runs.Test plan
cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features metal,accelerate -- -D warnings(not run; Linux host).cargo clippy -p mlxcel-xla --all-targets --features iree -- -D warningsis 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)mlx-community/Llama-3.2-1B-Instruct-4bit,mlx-community/Qwen2.5-7B-Instruct-4bitAlso 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), andRUSTFLAGS="-D warnings" cargo check -p mlxcel-xla --all-targets.Notes for reviewers
cuda. I could not runhip; 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.dequantize_affine_stacked(the MoE expert bank), the two BF16 carrier variants Gemma3n uses, and the unquantizedbf16_to_f32/f32_le_to_f32widening. They are not on the path measured here.Checklist
feat:,fix:, etc.)docs/if user-facing behavior or supported models changed (no user-facing change)// Used by: ...comments on any shared function I modified (seedocs/code-guidelines.md) (none carry one).envfiles committed