Skip to content

perf(ggml-cuda): two rows per wave and LDS-staged activations on the batch-invariant Q8_0 verify GEMV - #859

Merged
davide221 merged 4 commits into
Luce-Org:mainfrom
dusterbloom:pr/qwen38-mmvq-verify
Oct 24, 2026
Merged

davide221 merged 4 commits into
Luce-Org:mainfrom
dusterbloom:pr/qwen38-mmvq-verify

Conversation

@dusterbloom

@dusterbloom dusterbloom commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

What

The batch-invariant Q8_0 multi-column MMVQ launch (the dense trunk of the MTP verify rows on qwen4exp) gave every wave one weight row and streamed the T activation columns through the caches once per row group.

  • be02b6dd: two weight rows per wave share the activation loads (LUCE_MMVQ_INVARIANT_ROWS, 1 / 2 default / 4).
  • 682df16b: a block of 4 to 32 waves stages the columns' q8_1 blocks in LDS once and its waves dot their rows against them (LUCE_MMVQ_INVARIANT_LDS, default 16, 0 = off; LUCE_MMVQ_INVARIANT_LDS_BLOCKS).

Every lane keeps the one-column K traversal, dp4a chain, per-lane accumulation and warp reduction, so each output column stays bit-identical to its one-column product. Shapes that do not fit (fusion, other tables, row counts that do not divide) keep the previous kernel.

Tests

test_mmvq_invariant_rows_parity compares every column against its own one-column product with memcmp at 1, 2 and 4 rows and on the LDS-staged launch, on the qwen4exp dense shapes. Passes on gfx1151.

Measurement

Radeon 8060S (gfx1151), Qwen3.8-Flash-Next UD-Q4_K_XL (111 GB, 4 shards) with the shared Q8_0 MTP sidecar, --max-ctx 32768, prefix and disk caches off, temperature 0, distinct prompt per request. Base = main 8194012, arm = base + this PR, ABBA blocks with a server restart per block; 20 measured decode requests (256 new tokens) and 6 prefill requests (~11.3K tokens) per block.

base this PR
decode tok/s, server (median / mean / best / min) 32.00 / 32.29 / 37.80 / 27.40 34.00 / 34.70 / 43.30 / 30.10
decode tok/s, client median (per block) 32.68, 32.38 34.23, 34.22
prefill tok/s, server (median / mean / best / min) 1259.5 / 1247.2 / 1297.0 / 1029.0 1263.5 / 1230.6 / 1277.2 / 1001.8
decode wall s (median) 8.36 7.89
prefill wall s (median) 9.17 9.12

Decode +5.5% (pooled client median 32.43 to 34.23 tok/s; faster on 19 of 20 prompts), prefill unchanged. Outputs are identical: 0 of 104 generations differ between the arms, and both arms generate the same token counts.

Not measured: CUDA devices (the change compiles there; the CI GPU jobs cover correctness) and long-context decode.

View guided diff

… verify GEMV

The batch-invariant Q8_0 multi-column launch (the MTP verify rows' dense
trunk) gave every wave one weight row, so the activation columns were read
once per row. Two rows per wave share those loads while each row keeps its
own single-column K traversal, per-lane accumulation and warp reduction, so
every output column stays bit-identical to its one-column product.
LUCE_MMVQ_INVARIANT_ROWS selects 1, 2 (default) or 4 rows, and
ggml_backend_cuda_set_mmvq_invariant_rows_override() does the same per
calling thread for tests. Tables where the single-column shape is not one
wave, and row counts that do not divide evenly, keep one row per wave.

test_mmvq_invariant_rows_parity checks every column against its own
one-column product with memcmp at 1, 2 and 4 rows on the qwen4exp dense
shapes.
…ariant Q8_0 verify GEMV

On the batch-invariant Q8_0 multi-column launch every wave streamed the T
activation columns through the caches once per weight-row group. A block of
4 to 32 waves now copies the columns' q8_1 blocks into LDS and each wave dots
its own rows against them. When the columns fit the LDS the block stages them
once and its waves take row groups from a block-local counter, up to 32768
rows in a persistent grid and above that one block per 16 row groups;
otherwise the columns are staged 128 blocks at a time.

Every lane keeps the K traversal of the one-column kernel and the same dp4a
chain, per-lane accumulation and warp reduction, only the source of the
activation ints changes, so each output column stays bit-identical to its
one-column product. LUCE_MMVQ_INVARIANT_LDS (default 16 waves, 0 = off) and
LUCE_MMVQ_INVARIANT_LDS_BLOCKS tune it, and
ggml_backend_cuda_set_mmvq_invariant_lds_override() sets it per calling
thread. Launches with fusion, other tables and row counts that do not divide
into whole blocks keep the previous kernel.

test_mmvq_invariant_rows_parity now also compares the LDS-staged launch with
the one-column product with memcmp.

@davide221 davide221 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review

Found 1 issue.

Copy to agent

Use the copy button in the top-right corner of the block, then paste into your agent.

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.

Repository: Luce-Org/lucebox
Reviewed commit: 682df16b37fed019e05d3ac60c13c0a261cd7b63
Verify the findings against the current code, run relevant checks for any fixes, and explain any findings you reject.

<file name="server/deps/llama.cpp/ggml/src/ggml-cuda/mmvq.cu">

<violation number="1" line="3413" end_line="3413">
LDS=32 waves per block instantiates a 2048-thread block on HIP AMD, over the 1024-thread hardware limit

`GGML_MMVQ_INVARIANT_LAUNCH_RPB(NC, 2, 32)` is a documented, selectable configuration (`LUCE_MMVQ_INVARIANT_LDS=32` is accepted by `ggml_backend_cuda_set_mmvq_invariant_lds_override` and by `mmvq_invariant_lds_waves`, which permits waves==32 with rpb==2). On HIP AMD `ggml_cuda_get_physical_warp_size()` is 64, so `calc_launch_params` builds `block_dims = (64, 1*32)` = 2048 threads per block (line 2420), and `__launch_bounds__(...*32*64)` (line 1084) declares the same. AMD GPUs cap a workgroup at 1024 threads, so hipcc either rejects the launch bounds at compile time (breaking the whole ggml-cuda build) or every such launch fails with an invalid-configuration error, leaving the MUL_MAT unwritten. Cap the waves-per-block so `waves*warp_size &lt;= 1024` (i.e. drop 32 on 64-lane devices), or reject waves==32 in `mmvq_invariant_lds_waves` when the physical warp size is 64.

<suggestion>

</suggestion>
</violation>

</file>
Issue overview
  1. LDS=32 waves per block instantiates a 2048-thread block on HIP AMD, over the 1024-thread hardware limit · likely (server/deps/llama.cpp/ggml/src/ggml-cuda/mmvq.cu:3413)

Reviewed 682df16b · 2 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again

GGML_MMVQ_INVARIANT_LAUNCH_RPB(NC, 2, 4); \
GGML_MMVQ_INVARIANT_LAUNCH_RPB(NC, 2, 8); \
GGML_MMVQ_INVARIANT_LAUNCH_RPB(NC, 2, 16); \
GGML_MMVQ_INVARIANT_LAUNCH_RPB(NC, 2, 32); \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LDS=32 waves per block instantiates a 2048-thread block on HIP AMD, over the 1024-thread hardware limit · likely

GGML_MMVQ_INVARIANT_LAUNCH_RPB(NC, 2, 32) is a documented, selectable configuration (LUCE_MMVQ_INVARIANT_LDS=32 is accepted by ggml_backend_cuda_set_mmvq_invariant_lds_override and by mmvq_invariant_lds_waves, which permits waves==32 with rpb==2). On HIP AMD ggml_cuda_get_physical_warp_size() is 64, so calc_launch_params builds block_dims = (64, 1*32) = 2048 threads per block (line 2420), and __launch_bounds__(...*32*64) (line 1084) declares the same. AMD GPUs cap a workgroup at 1024 threads, so hipcc either rejects the launch bounds at compile time (breaking the whole ggml-cuda build) or every such launch fails with an invalid-configuration error, leaving the MUL_MAT unwritten. Cap the waves-per-block so waves*warp_size <= 1024 (i.e. drop 32 on 64-lane devices), or reject waves==32 in mmvq_invariant_lds_waves when the physical warp size is 64.

Checked (85%): Line 3413 unconditionally instantiates mul_mat_vec_q with c_invariant_lds_waves=32, and on HIP GFX8/GFX9 builds ggml_cuda_get_physical_warp_size() is a constexpr 64 (common.cuh:359-365), so launch_bounds at mmvq.cu:1084 evaluates to 13264 = 2048 threads — over AMD's 1024-thread workgroup limit, causing a compile error in every HIP build for those archs regardless of runtime config; and mmvq_invariant_lds_waves (line 912) accepts waves==32 with rpb==2, so a runtime launch of block_dims=(64, 32) = 2048 threads is also selectable and would fail.

Suggested change
GGML_MMVQ_INVARIANT_LAUNCH_RPB(NC, 2, 32); \

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on the limit; fixed in ea9531c. The selector now refuses waves*warp_size > 1024 for the device warp size (constexpr mmvq_invariant_lds_fit, with a static_assert pinning 32x32 taken, 32x64 refused, 16x64 taken). The 32-wave instantiation stays, since it is valid on 32-lane waves.

Two corrections to the finding:

  • It is not reachable on this revision. GCN/CDNA, the only 64-lane parts, use the GCN table, whose single-column entry is 2 warps, so the existing one-warp check already turned LDS staging off there. The guard keeps a later table change from launching an over-limit block.
  • hipcc does not reject __launch_bounds__(2048): a kernel with that bound compiles clean for gfx942 and gfx90a, so there is no build break.

dusterbloom and others added 2 commits October 22, 2026 10:57
…e waves, a 2048-thread block

The waves check moves into constexpr mmvq_invariant_lds_fit, which also
requires waves*warp_size <= 1024 for the device warp size; a static_assert
pins 32x32 taken, 32x64 refused, 16x64 taken. Not reachable today: GCN/CDNA
use the GCN table, whose two warps already fail the one-warp requirement, so
this keeps a later table change from launching an over-limit block.
CMakeLists.txt and ENVIRONMENT.md: keep this PR's test target and LUCE_MMVQ_INVARIANT_* rows next to the ones Luce-Org#860 and Luce-Org#862 added on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@davide221
davide221 merged commit d836135 into Luce-Org:main Oct 24, 2026
6 of 8 checks passed
davide221 added a commit to dusterbloom/lucebox-hub that referenced this pull request Oct 24, 2026
…er branch

kvflash_pager.h reset(): keep Luce-Org#854's sync_page_stream_() (checked wait that
clears page_work_queued_) and this branch's free_host_buffer_() helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221 added a commit to dusterbloom/lucebox-hub that referenced this pull request Oct 24, 2026
…led snapshot branch

Via the Luce-Org#848 branch merge. server/CMakeLists.txt keeps both new unit tests
(test_kvflash_restore_plan.cpp and main's test_dflash_window.cpp).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221 added a commit to dusterbloom/lucebox-hub that referenced this pull request Oct 24, 2026
…led grid branch

Via the Luce-Org#849 branch merge; no new conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221 added a commit to dusterbloom/lucebox-hub that referenced this pull request Oct 24, 2026
…sion restore branch

Via the Luce-Org#850 branch merge. server/CMakeLists.txt keeps this branch's live
restore and resume tests plus main's test_dflash_window.cpp.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants