Skip to content

perf(cuda): charge MLX Gather and GatherQMM for the rows they read - #3560

Merged
inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:perf/3495-cuda-gather-row-charge
Nov 29, 2026
Merged

inureyes merged 2 commits into
lablup:mainfrom
Jaeyeong-CHOI:perf/3495-cuda-gather-row-charge

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

Closes #3495.

Two MLX builtins named in #3495, Gather (take) and GatherQMM (gather_qmm), now charge the CUDA graph's byte budget only for the rows their index names, not the whole source. #3496 did the same for the fused decode-MoE kernels. On GB10 (DGX Spark), a paged KV pool slab, or one layer's expert stack, is over the 25 << 20-element budget by itself. So each gather of a few rows from one committed the graph it was encoded into.

Graph launches per decode step on GB10, main → this PR (main with MLX_MAX_MB_PER_BUFFER=1000 in parentheses, as the ceiling):

  • Pool-gather decode on Qwen3-0.6B: 58 → 28 (27) at B=1 and 281 → 90 (90) at B=4.
  • Prompt-lookup verify: 10.6 → 5.5 (5.5) per emitted token.
  • Kimi-VL-A3B: 134 → 77 (76) at B=1.
  • LFM2-8B-A1B: 66 → 60 (56) at B=4.

Greedy text is byte-identical. Decode throughput, each session three rounds against a main null arm; two sessions on the previous MLX pin fc344c98:

  • Pool-gather decode at B=4: +32.7% to +48.0%; the slowest changed run beat the fastest main run in each session.
  • Kimi-VL-A3B at B=1: +10.3% to +11.6%.
  • Prompt-lookup verify: +4.8% to +5.0%; individual run ranges overlap.
  • The remaining paths have no consistent gain. That includes LFM2-8B-A1B at B=4 (+0.2% and +1.8%), and Qwen3-30B-A3B and Kimi-VL-A3B at B=4, whose graphs this PR leaves unchanged. Qwen3-30B-A3B measured −3.3% once and +1.3% on a repeat (see Verification).

qmm_sm80 is the batched bf16/f16 gather_qmm launcher, used for MoE decode at B>1. It keeps charging its stacks whole. When it was charged by expert, its graph counts fell (Qwen3-30B-A3B at B=4 from 290 to 194), but decode showed no consistent gain. That was measured on the previous MLX pin fc344c98. Details are below.

What changed

  • src/lib/mlxcelverse/overlays/cuda/mlx/backend/cuda/device.cpp: two helpers on top of perf: Audit other pool-slab kernels for per-launch CUDA graph commits #3387's set_input_rows_read.
    • set_input_array_rows_read(encoder, arr, rows_read, rows) registers arr charged for rows_read of its rows leading slices. It does this only when arr is row-contiguous, because only then does slice r own exactly the elements [r * data_size / rows, (r + 1) * data_size / rows). Any other layout is still charged whole. A broadcast array's slices all alias the same data, and a strided view's data need not split into rows parts.
    • set_input_stack_rows_read(encoder, stack, experts_read) does the same for a GatherQMM weight, scale or bias stack [..., n, k], whose slices are the [n, k] matrices. A stack of two dims or fewer is one matrix and is charged whole.
    • The arithmetic is perf: Audit other pool-slab kernels for per-launch CUDA graph commits #3387's counted_input_elements, unchanged. An index with at least as many entries as the source has rows is charged whole, and the charge never exceeds the source.
  • src/lib/mlxcelverse/overlays/cuda/mlx/backend/cuda/indexing.cpp: new overlay. It is upstream 5a48f9cc (the MLX pin since update: advance MLX pin to 5a48f9cc #3517) plus one block in Gather::eval_gpu and the declaration it calls. A take along axis 0 (nidx == 1, axes_ == {0}, slice_sizes_[0] == 1) charges its source for indices.size() of its shape(0) rows. Every other gather, and the index itself, is still charged whole.
  • quantized/qmm/qmv.cu (gather_qmv) and quantized/qmm/qmm_naive.cu (when given rhs_indices): the weight, scale and bias stacks are charged for rhs_indices.size() experts. Still charged whole:
    • x, the indices and a global scale.
    • A plain quantized matmul (no rhs_indices).
  • src/lib/mlxcel-core/src/cuda_graph_commit_tests.rs: six launch tests, listed under Verification.
  • The CUDA overlay README, and a CHANGELOG entry under ## [Unreleased].

Why the bytes each launch reads are bounded by the index

  • Gather (device/gather.cuh). Output element out_idx reads src[elem_to_loc(out_idx % slice_size, slice_sizes, src_strides) + idx * src_strides[0]], where idx is index element out_idx / slice_size. With slice_sizes[0] == 1, the first term stays inside row 0, since every coordinate is below its slice size. The second term moves it to row idx. So a row-contiguous source has indices.size() rows read, each of data_size / shape(0) elements.
  • gather_qmv_kernel (qmv.cu). Grid slice l sets w_idx = rhs_indices[l]. qmv_kernel_impl then offsets w, scales and biases by (row + n * w_batch) * width with row < n. That stays inside expert w_idx's [n, width] block.
  • qmm_naive_kernel. Grid slice l_coord takes b_batch = rhs_indices[l_coord]. It builds mB, mS and mZ as (_, _, b_batch) of tensors with batch stride n * k (in scale groups for S and Z). The grid's z extent is rhs_indices.size().

Not changed, and why

  • qmm_sm80 with rhs_indices (bf16/f16 gather_qmm of 8 rows or more, MoE decode at B>1). Its kernel reads only the named experts too, and the first version of this change charged it that way:
    • Qwen3-30B-A3B at B=4 went from 290 to 194 graphs per step, and Kimi-VL-A3B at B=4 from 215 to 162.
    • Decode showed no consistent gain. Qwen3-30B-A3B at B=4 was +1.8%, inside main's own spread of 62.5 to 68.8 tok/s in that session. Kimi-VL-A3B at B=4 was −6.6%, then −4.4% in a second session. With qmm_sm80 charged whole, the same cell was +3.4% in one session and −2.3% in another. All of these medians are inside main's own spread on that cell across the four sessions, 73.2 to 81.3 tok/s, so neither a gain nor a loss is established.
    • The nsys traces point both ways. With the per-expert charge, qmm_sm80_kernel took 14% (Kimi-VL-A3B) and 12% (Qwen3-30B-A3B) longer per step once it shared the larger graphs. In the same traces, wall time per decode step went from 52.8 to 48.6 ms on Kimi-VL-A3B at B=4 and from 63.5 to 63.9 ms on Qwen3-30B-A3B at B=4.
    • All of this was measured on MLX fc344c98. update: advance MLX pin to 5a48f9cc #3517 moved the pin to 5a48f9cc, which also changed qmm_sm80 upstream (the 128-row tile from [CUDA] Add 128-row variant for qmm ml-explore/mlx#4851), and the per-expert variant was not re-measured there. So this decision rests on the old pin.
    • With no gain shown, it follows perf(cuda): charge MLX Gather and GatherQMM for the rows they read so pool-slab and expert-stack gathers stop committing the CUDA graph on GB10 #3495's rule for a family that does not gain: recorded, and left as it was.
  • gather_qmm_rhs_sm80 (sorted floating-point modes) and fp_gather_qmv (fp_qmv.cu, not overlaid). They read only the named experts as well, but no measured checkpoint runs a floating-point quantization mode, and no test here builds one. Their stacks are still charged whole.
  • The sorted grouped-GEMM prefill path in GatherQMM::eval_gpu. It dequantizes the whole expert stack, so it reads the stack whole and is charged whole.
  • GatherMM (gemms/gather_gemm.cu, unquantized MoE), GatherAxis (take_along_axis), Scatter, ScatterAxis and MaskedScatter. None of the paths in perf(cuda): charge MLX Gather and GatherQMM for the rows they read so pool-slab and expert-stack gathers stop committing the CUDA graph on GB10 #3495 runs them.
  • ROCm. Its encoder (overlays/rocm/mlx/backend/rocm/device.cpp) has no per-row charge and a 200 << 20-element budget. The Qwen3-0.6B pool slab (67.1M elements) and the Qwen3-30B-A3B weight stack (25.2M) are both under it. Nothing changed there, and nothing was run on ROCm.

Cells that stay above the raised ceiling

  • MLXCEL_PAGED_ATTENTION_NATIVE=0 at 8,193 tokens, B=4 (309 → 165, raised 97). Each K or V gather really copies about 257 blocks, 8.4M elements, and the attention that reads both copies counts them again. One row's layer passes the budget by itself, so those cuts come from bytes actually read.
  • Kimi-VL-A3B and Qwen3-30B-A3B at B=4 (215 → 214 and 290 → 290). These run qmm_sm80, which is left as it was (see above).

Verification

All on GB10 (DGX Spark): NVIDIA GB10, CUDA 13.0, driver 580.178.04, sm_121. A private 0700 TMPDIR was used.

Trees. This commit is on main 506ef099d, tree 1260e6e25f471c77f1edfa1bf17f5739c7d92049. Its parent, #3517, moved the MLX pin from fc344c98 to 5a48f9cc. So the rebase regenerated the indexing.cpp overlay from upstream 5a48f9cc, which picks up the one line upstream added (SliceUpdate::eval_gpu now gives an empty output an allocation before returning). The rebase also took the new pin hash into the device.cpp header. Verification and benchmarks used the following revisions:

  • This tree (1260e6e25, MLX 5a48f9cc):
    • cargo fmt --all -- --check: 0.
    • The cuda_graph_commit tests: 9 passed. The gather_qmm tests: 2 passed.
    • cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings: 0.
    • Immediately before the CHANGELOG-only amendment (identical code and tests, tree ccb28eb39), make verify-test-cuda: 17,829 passed, 0 failed, 517 ignored, over 202 test binaries.
    • The scripts/ci checks: 31 of 35 pass (34 CI scripts plus the license-header check). The four failures are the binary-assets and Helm ones described below.
  • The same change on main 4f4db5cb4 (tree d08e32515, MLX fc344c98): the full suite, the main-side test run and the rest of the checks below.
  • The same C++/CUDA change on main ed9ec2acd (MLX fc344c98): the nsys traces and decode A/B. Across both rebases the CUDA charging logic is unchanged; indexing.cpp was regenerated and comments were updated. The test file differs: one gather_qmm test was later split into a gather_qmv and a qmm_naive test.

Tests that fail on main. I took unmodified main 4f4db5cb4 with only this commit's cuda_graph_commit_tests.rs and lib.rs and ran:

cargo test --locked -p mlxcel-core --profile test-fast --features cuda --lib cuda_graph_commit -- --test-threads=1

Result: 5 passed, 4 failed.

  • Failed: builtin_take_charges_the_pool_slab_for_the_rows_named, builtin_take_of_a_source_past_i32_charges_the_rows_named, builtin_gather_qmv_charges_the_stacks_for_the_experts_named and builtin_gather_qmm_naive_charges_the_stacks_for_the_experts_named. Each failed on its graphs == 1 assertion.
  • Passed: the two "still charges whole" guards, and perf: Audit other pool-slab kernels for per-launch CUDA graph commits #3387's three tests.

Same command on this commit's tree: 9 passed, 0 failed.

The launch tests did not skip: GB10's default budget is 25 << 20 elements. The tests are:

  • builtin_take_charges_the_pool_slab_for_the_rows_named: one row, row 0, the last row, repeated rows and four rows, two takes sharing one graph with the expected bytes. An empty index (an empty output). The budget edge, where the largest row count that fits stays in one graph and one more row cuts.
  • builtin_take_still_charges_whole_reads_whole: every row; indexes of 128 and 129 entries naming one row; a take along axis 1; a broadcast source; a strided (transposed) view. Each is charged whole and cuts.
  • builtin_take_of_a_source_past_i32_charges_the_rows_named: a row-contiguous source of 2,147,483,904 elements, which takes the kernel's 64-bit index path, still charged by rows.
  • builtin_gather_qmv_charges_the_stacks_for_the_experts_named: one expert, expert 0, the last expert, repeats, and 7 experts (the most gather_qmv takes). Output is byte-identical to the under-budget 16-expert stack.
  • builtin_gather_qmm_naive_charges_the_stacks_for_the_experts_named: 8 rows with repeats, 16 experts, and the budget edge (82 experts fit, 84 cut).
  • builtin_gather_qmm_still_charges_whole_reads_whole: every expert, and one expert named 128 times (qmm_naive); a qmm_sm80 launch; and a plain quantized_matmul over the same weights as one 2-D matrix. Each cuts.
  • perf: Audit other pool-slab kernels for per-launch CUDA graph commits #3387's a_partly_read_input_is_charged_for_the_rows_read still passes. It covers the u64 extremes of the shared arithmetic.

Formatting and checks:

  • cargo fmt --all -- --check: 0.
  • python3 scripts/insert_apache_header.py --check: 0.
  • python3 scripts/ci/check_used_by.py and bash scripts/ci/check_used_by_test.sh: 0.
  • The other scripts/ci/check_*.py and scripts/ci/*_test.sh: 0, except four:
    • check_binary_assets.py, and the repository-as-committed case of check_binary_assets_test.sh, fail on main's undeclared tests/fixtures/audio/speech.flac, which this PR does not touch (fix(ci): declare the d1-omni speech clip as a binary asset #3548).
    • check_helm_chart.py and helm_chart_package_test.sh need a helm binary the host does not have.

Clippy (pinned toolchain 1.97.1), cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings:

Full suite: make verify-test-cuda runs cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1. It gave 17,829 passed, 0 failed, 517 ignored over 202 test binaries, on both ccb28eb39 (before the CHANGELOG-only amendment) and the 4f4db5cb4 tree.

Graph launches per decode step. Same method, cells, checkpoints and analyzer as the #3387 audit:

  • nsys profile --cuda-graph-trace=node around mlxcel-server --parallel 4 --no-prompt-cache -c 65536.
  • Greedy streaming /v1/completions with ignore_eos, a 4 GiB MLX cache cap.
  • scripts/nsys_cuda_graph_launches.py over the steady decode window.

Main and main-raised are main's traces, taken on main 42a391c6b (MLX fc344c98). This PR's traces are on main ed9ec2acd (MLX fc344c98). A cell counts as reproduced when it is within 1% of the #3387 record with the same per-step kernel counts; all 18 did.

Path Context B Main Main raised This PR
Qwen3-0.6B, pool gather decode 3,073 1 58.000 27.000 28.000
Qwen3-0.6B, pool gather decode 257 4 281.000 90.000 90.000
Qwen3-0.6B, MLXCEL_PAGED_ATTENTION_NATIVE=0 8,193 1 57.992 27.000 29.000
Qwen3-0.6B, MLXCEL_PAGED_ATTENTION_NATIVE=0 8,193 4 309.000 96.970 164.829
Qwen3-0.6B, prompt-lookup verify¹ 3,073 1 10.625 5.465 5.475
Kimi-VL-A3B, MLA decode (gather_qmv) 9,217 1 134.000 76.050 77.050
Kimi-VL-A3B, MLA decode (qmm_sm80, unchanged) 9,217 4 215.014 155.075 214.014
Qwen3-30B-A3B, expert gather (qmm_sm80, unchanged) 8,193 4 290.020 169.486 290.000
LFM2-8B-A1B, expert gather (qmm_naive) 9,218 4 66.000 56.041 60.000

¹ Per emitted token; the other rows are per decode step.

Every generated-text SHA-256 in the nine traces matches main's trace of the same cell. The prompts are a repeated eight-word sentence, though, so this parity check is weak. The decode runs below compare text too.

Decode tok/s against main. The driver is bench_decode.py from the #3385 record:

  • Arms main-a, this PR, main-b, rotated per round, three rounds, one fresh server per arm.
  • --parallel 4 --no-prompt-cache -c 65536, greedy, ignore_eos.
  • Medians are over three runs of this PR and six of main; the ranges are min to max.
  • Text was byte-identical between all arms in every cell.

The three cells that gain, per session:

Path First session: main → change (tok/s) Second session: main → change (tok/s)
Pool, 256 tokens, B=4 218.38 → 289.82 (+32.7%) 193.76 → 286.76 (+48.0%)
Kimi-VL, B=1 63.43 → 70.81 (+11.6%) 63.97 → 70.59 (+10.3%)
Prompt-lookup verify 275.37 → 288.61 (+4.8%) 274.83 → 288.70 (+5.0%)

Both sessions used MLX fc344c98; the first build additionally changed the batched qmm_sm80 path, which none of these three cells uses.

The full matrix of the second old-pin session (server-final, the C++/CUDA change on main ed9ec2acd, MLX fc344c98):

Path Prompt B Main median (range) This PR median (range) Change
Qwen3-0.6B, pool gather decode 3,072 1 79.02 (78.22–80.59) 79.56 (77.97–82.51) +0.7%
Qwen3-0.6B, pool gather decode 256 4 193.76 (175.91–260.51) 286.76 (286.42–300.36) +48.0%
Qwen3-0.6B, MLXCEL_PAGED_ATTENTION_NATIVE=0 8,192 1 36.82 (36.66–36.88) 36.87 (36.82–36.91) +0.1%
Qwen3-0.6B, MLXCEL_PAGED_ATTENTION_NATIVE=0 8,192 4 33.45 (33.28–33.54) 33.33 (33.32–33.48) −0.4%
Qwen3-0.6B, prompt-lookup verify 3,072 1 274.83 (258.89–279.80) 288.70 (268.55–289.96) +5.0%
Kimi-VL-A3B 8,192 1 63.97 (63.24–64.26) 70.59 (70.22–70.67) +10.3%
Kimi-VL-A3B (qmm_sm80, unchanged) 8,192 4 78.49 (73.52–81.33) 76.70 (76.11–76.79) −2.3%
Qwen3-30B-A3B (qmm_sm80, unchanged), first run 8,192 4 69.32 (68.43–69.67) 67.01 (63.68–67.39) −3.3%
Qwen3-30B-A3B (qmm_sm80, unchanged), repeat 8,192 4 67.95 (63.35–69.29) 68.82 (63.03–69.51) +1.3%
LFM2-8B-A1B (qmm_naive) 8,192 4 96.63 (95.47–98.21) 98.40 (98.33–99.42) +1.8%

The pool-gather B=4 cell is noisy on main (null arms 184.0 and 195.6 in this session, 189.3 and 228.9 in the first). Even so, in every session every run of this PR beats every main run.

The first Qwen3-30B-A3B B=4 run put all three runs of this PR below main's range. That cell launches the same graphs per step on both sides (290 against 290): its expert gather is qmm_sm80, which this PR leaves alone. So I repeated it with the same binaries. Both arms then fell into two bands, near 63 and near 69 tok/s, and the cell came out +1.3%. I read it as host noise, not a change.

LFM2-8B-A1B at B=4 is +1.8% here against a null spread of 1.4 tok/s, and was +0.2% in the first session, so it counts as flat.

Unchanged controls were measured with the first build of this change, which also charged qmm_sm80 by expert. They used the same driver and showed no effect beyond the null:

  • Gemma 4 26B-A4B (graph capture off): 256 and 8,192 at B=1, −0.1% / −0.1%.
  • Qwen3-0.6B with fused paged attention: 8,192 at B=1 and B=4, and 3,072 at B=4: −0.8% / +0.1% / −0.1%.
  • Qwen3-30B-A3B at B=1: 256 and 8,192, +0.6% / −0.1%.
  • LFM2-8B-A1B at B=1: 256 and 8,192, +1.5% / −0.1%.

Peak memory on a 2,048-token prefill chunk (#3495 acceptance item 4). Main 506ef099d against this commit, both built on MLX 5a48f9cc:

  • mlxcel-bench-decode -m MODEL -p benchmark --prompt-tokens 2048 --warmup-tokens 1 -n 1 --ignore-eos with MLXCEL_PREFILL_CHUNK=2048. The tool resets the MLX allocator's high-water mark before each phase and prints it after. Each phase is one 2,048-token prefill chunk plus one token. Two runs per arm, alternating. GB is 10^9 bytes.

    Checkpoint Main, MLX peak GB (two runs) This PR (two runs)
    Qwen3-0.6B 4-bit 1.14, 1.15 1.14, 1.16
    Kimi-VL-A3B 4-bit 9.96, 9.95 9.95, 9.95
    Qwen3-30B-A3B 4-bit 21.38, 21.40 21.40, 21.40
    LFM2-8B-A1B 4-bit 9.63, 9.63 9.63, 9.63

    Each figure is the larger of a run's two prefill phases, the warm-up and the measured pass; it is also the run's whole MLX peak, load included. The two arms agree to within 0.02 GB. This tool runs the plain KV cache, so it exercises gather_qmm but not the paged pool's take.

  • For the paged-pool path, a fresh server (--parallel 4 --no-prompt-cache -c 65536) served a 64-token warm-up, then a greedy request with exactly 2,048 prompt tokens and one output token. We sampled process VmRSS every 10 ms during the request and for 0.5 s after it. Two runs per arm in main/change/change/main order gave:

    Checkpoint Main peak VmRSS, GiB Change peak VmRSS, GiB
    Qwen3-0.6B 1.575, 1.569 1.578, 1.577
    Kimi-VL-A3B 2.127, 2.147 2.127, 2.147

    These are sampled host RSS peaks, not CUDA allocation peaks. On the shared host even the sign of the system-wide available-memory change varied; it cannot isolate this server's GPU memory.

Limits

  • Only GB10 (sm_121) was measured. Metal is untouched by this change. ROCm was not changed or run.
  • gather_qmm_rhs_sm80 and fp_gather_qmv are left counted whole, and no floating-point-mode checkpoint was measured.
  • The qmm_sm80 decision rests on noisy B=4 cells (Kimi-VL-A3B's main spread on this host is about 10%), and on the old pin fc344c98. update: advance MLX pin to 5a48f9cc #3517's pin 5a48f9cc changed qmm_sm80 upstream, and the per-expert variant was not re-measured there.
  • The graph-count traces and the full decode matrix are from the old pin fc344c98 (the same C++/CUDA change on main ed9ec2acd). There is no current-pin decode figure: the shared host did not provide three uncontested rounds with no other GPU compute or cargo/rustc activity. Interrupted rounds were discarded; the optional Kimi and verify repeats were not completed.
  • The unchanged-path controls (Gemma 4, fused paged attention, MoE at B=1) were benchmarked on the first build of this change, not on the final one.
  • Peak memory: the MLX allocator peak was measured only through mlxcel-bench-decode, which has no paged pool. On the paged-pool server path the MLX peak is not exposed. The sampled RSS comparison above cannot rule out an increase in device allocations.
  • Four scripts/ci checks could not pass on this host: the Helm checks need helm, and the binary-assets check fails on main itself (tests/fixtures/audio/speech.flac, fix(ci): declare the d1-omni speech clip as a binary asset #3548).

Hardening checks

  • Boundaries. For take: an empty index, one row, row 0 and the last row, repeats, every row, indexes as long as and one longer than the source that name one row, the budget edge, and a source past i32::MAX elements. For gather_qmm: one expert, expert 0, the last expert, repeats, the most gather_qmv takes, 16 experts, every expert, one expert named 128 times, and the budget edge. Not covered by a test, only by reading the code: a source with zero rows, or a stack whose [n, k] matrix is empty, reaches counted_input_elements with rows == 0 and is charged whole, which is zero elements.

  • Time complexity. The charge is O(1) per launch. It reads indices.size() and the source's shape, and never scans the index.

  • Other callers. set_input_rows_read has three users:

    • custom_kernel.cpp's rows_read_of_inputs loop (unchanged).
    • set_input_bytes_uncounted (unchanged).
    • The new set_input_array_rows_read.

    On main's CUDA backend, grep finds 230 set_input_array calls in 51 files. The gathers among them that read only part of a large input are listed under "Not changed". The budget itself (get_graph_limits, needs_commit) is unchanged. The ROCm encoder is covered above.

  • Formatting. cargo fmt --all -- --check passes on the final tree. Existing comments are kept. The CHANGELOG entry is under ## [Unreleased].

On DGX Spark a paged KV pool slab or a layer's expert stack is over the
25 << 20-element graph budget on its own, so a take of a few pool rows or
a gather_qmm over a few experts committed the CUDA graph it was encoded
into. A take along axis 0 now charges its source for the rows its index
names, and the gather_qmv and qmm_naive GatherQMM launchers charge the
weight, scale and bias stacks for the experts rhs_indices names, through
set_input_array_rows_read and set_input_stack_rows_read in the device.cpp
overlay. A source that is not row-contiguous, and an index with as many
entries as rows, stay charged whole. qmm_sm80 keeps charging its stacks
whole: charged by expert it showed no consistent decode gain on GB10
(measured at MLX fc344c98, before the pin moved).

Closes lablup#3495.
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:linux Linux (CUDA / packaging) specific labels Nov 29, 2026
@inureyes

Copy link
Copy Markdown
Member

Hardening review

Reviewed this PR against #3495. I merged origin/main (merge commit 63fe8a7fb, no conflicts). That brings in #3548, so the binary assets check now passes. I made no code changes.

Correctness. I read the change against MLX 5a48f9cc, the pin scripts/ci/mlx_pinned_commit.sh reports, which is also the base #3517 set:

  • Upstream set_input_array charges data_size() elements against max_mb_per_graph_ << 20. The overlay keeps that rule and only scales the charge, through counted_input_elements.
  • The charge never exceeds data_size, and the quotient is taken first, so row * stride cannot overflow.
  • An index with as many entries as the source has rows, or more, is charged whole. So is a source with zero rows, a non-row-contiguous source (broadcast or strided) and a stack of two dims or fewer.
  • An empty index is charged zero, and the kernel reads nothing.
  • The node-count limit still bounds every graph on its own.
  • rhs_indices.size() covers grid.z in gather_qmv and qmm_naive. run_batch_chunked passes a flat_view of bc entries, so each chunk is charged for its own experts.
  • A repeated or broadcast index is overcharged. That is safe.

Drift. Diffing indexing.cpp against upstream 5a48f9cc shows only the documented block. The README overlay count (46) matches the tree.

Security and performance. No new input-driven allocation or panic path. The charge is O(1) per launch. One low-risk point I left alone: the thread-local rows state is reset without RAII, so a bad_alloc inside set_input_array would leave it set. The #3387 custom_kernel.cpp code uses the same pattern.

Local validation (macOS). All exited 0:

  • cargo fmt --all -- --check
  • check_binary_assets.py and check_binary_assets_test.sh
  • check_nvrtc_headers.py and check_nvrtc_headers_test.sh
  • check_kernel_dtype_keys.py, check_env_var_docs.py, check_used_by.py
  • insert_apache_header.py --check

Not verified here. The overlay C++/CUDA does not compile on this Mac. cuda_graph_commit_tests.rs is gated on feature = "cuda", so neither the tests nor CUDA clippy were run. The GB10 results in the PR body are the only runtime evidence.

Remote CI on the merge commit was cancelled by maintainer decision. The local commands above stand in for it.

@inureyes
inureyes merged commit 952f09f into lablup:main Nov 29, 2026
32 of 36 checks passed
@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:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:linux Linux (CUDA / packaging) specific priority:medium Medium priority status:review Under review type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cuda): charge MLX Gather and GatherQMM for the rows they read so pool-slab and expert-stack gathers stop committing the CUDA graph on GB10

2 participants