Skip to content

cuda: opt-in Hopper wgmma path for Q1_0/Q2_0 prefill (sm_90a) - #39

Merged
bri-prism merged 2 commits into
prismfrom
feat/cuda-hopper-q1-wgmma
Jun 10, 2026
Merged

bri-prism merged 2 commits into
prismfrom
feat/cuda-hopper-q1-wgmma

Conversation

@bri-prism

@bri-prism bri-prism commented Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator

What

Opt-in Hopper (sm_90a) tensor-core path for Q1_0 mul_mat at prefill batch sizes (ne1 >= 128):

  • activations: fp32 -> int8 with per-128 absmax scales (warp-per-group quantize kernel)
  • weights: one-time repack per tensor to dense sign-bit words + scale array (cached)
  • dequant-in-SMEM: branchless SIMD bit->{+1,-1} unpack (no LUT — a __constant__ LUT
    serializes on divergent indices with real high-entropy weights)
  • 64x64x32 int8 wgmma (CuTe), 2-stage cp.async pipeline, exact per-block fp32 scaling on the
    accumulator drain
  • hybrid dispatch: persistent stream-K grid (fp32-additive atomic flush, fixup-free) for
    starved shapes; fixed tile grid otherwise; SM count queried per device

Gating

  • Build: -DGGML_CUDA_HOPPER_Q1=ON -DGGML_CUDA_CUTLASS_DIR=<cutlass checkout> (default OFF;
    stub compiles without CUTLASS)
  • Runtime: env GGML_HOPPER_Q1 must be set; otherwise (and for cc < 9.0, unsupported
    shapes/types, M/N/K % 128 != 0) the entry returns false and dispatch falls through to stock MMQ

Measured (H100 SXM 80GB HBM3)

1-bit test model (Q1_0):

test this path stock MMQ delta
pp512 3107 t/s 2870 t/s +8.3%
pp2048 3320 t/s 3057 t/s +8.6%

Ternary test model (Q2_0, second commit):

test this path stock MMQ delta
pp512 3029 t/s 2811 t/s +7.7%
pp2048 3028 t/s 2802 t/s +8.1%
  • test-backend-ops test -o MUL_MAT (q1_0 + q2_0): 86/86 OK
  • logit-KLD vs stock path on a 37k-token corpus: Q1_0 mean 0.0048 (same-top-p 97.3%),
    Q2_0 mean 0.0013 — the per-128 activation scale is noise-level vs stock per-32 q8_1
  • default build (option OFF) verified to compile and run unchanged

Limitations

  • Prefill only (batch >= 128); decode path untouched
  • Dense weight repack holds a per-tensor copy of the sign bits + scales (~bpw-equivalent
    memory) for the lifetime of the process
  • sm_90a only; no fattn/split-tensor interaction (guarded out)

Adds an experimental tensor-core path for Q1_0 mul_mat at batch >= 128:
activations quantized to int8 with per-128 absmax scales, weights repacked
once per tensor to dense sign-bit words, dequant-in-SMEM via branchless
SIMD unpack feeding 64x64x32 int8 wgmma, exact per-block fp32 scaling on
the accumulator drain. Hybrid dispatch: persistent stream-K grid for
starved shapes, fixed tile grid otherwise.

Opt-in at build time (-DGGML_CUDA_HOPPER_Q1=ON -DGGML_CUDA_CUTLASS_DIR=...)
and at runtime (env GGML_HOPPER_Q1); falls through to stock MMQ otherwise.

Measured on H100 SXM (1-bit test model): pp512 +8.3%, pp2048 +8.6% vs
stock MMQ; test-backend-ops MUL_MAT q1_0 43/43; logit-KLD vs stock path
0.0048 mean (noise-level).
Kernels templated on weight width (1- or 2-bit dense fields); Q2_0 adds a
per-tensor dense repack of the 2-bit (q-1) fields and a branchless SIMD
unpack (per-byte q - 1 via __vsub4, all four field values handled). Same
gating, dispatch, and activation-quant path as Q1_0.

Measured on H100 SXM (ternary test model): pp512 +7.7%, pp2048 +8.1% vs
stock MMQ; test-backend-ops MUL_MAT q1_0+q2_0 86/86; logit-KLD vs stock
path 0.0013 mean (noise-level).
@bri-prism bri-prism changed the title cuda: opt-in Hopper wgmma path for Q1_0 prefill (sm_90a) cuda: opt-in Hopper wgmma path for Q1_0/Q2_0 prefill (sm_90a) Jun 10, 2026
@bri-prism
bri-prism requested a review from khosravipasha June 10, 2026 18:05
@khosravipasha
khosravipasha requested a review from Copilot June 10, 2026 18:25

@khosravipasha khosravipasha left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice speed ups :D

one thing is I am in process of splitting up Q2_0 into two types, Q2_0 becomes gorup 64 for llama.cpp PRs, and PQ2_0 which is group 128. It is going to be on a different branch so won't collide with this so can merge it but need to reapply this later to do both Q2_0 and PQ2_0.

@bri-prism
bri-prism requested a review from khosravipasha June 10, 2026 18:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds an opt-in CUDA Hopper (sm_90a) WGMMA-based matmul path intended to speed up large-batch prefill for low-bit weight types (Q1_0 and Q2_0), with build- and runtime-gating so unsupported shapes/arches fall back to the existing MMQ kernels.

Changes:

  • Introduces a new Hopper-specific kernel pipeline: fp32 activations → per-128 int8 quant, cached dense repack of low-bit weights, and int8 WGMMA compute with per-block scaling.
  • Hooks the new path into ggml_cuda_mul_mat() dispatch for Q1_0/Q2_0 when ne1 >= 128.
  • Adds CMake options to enable the feature and point to a CUTLASS checkout for CuTe headers.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
ggml/src/ggml-cuda/mmq-hopper-q1.cu New Hopper WGMMA implementation, activation quantization, weight repack cache, and dispatch entrypoint.
ggml/src/ggml-cuda/ggml-cuda.cu Dispatch integration: tries Hopper path for Q1_0/Q2_0 prefill before falling back to MMQ.
ggml/src/ggml-cuda/CMakeLists.txt Adds build gating for Hopper path and CUTLASS include configuration.
ggml/CMakeLists.txt Adds GGML_CUDA_HOPPER_Q1 option and GGML_CUDA_CUTLASS_DIR cache variable.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +69 to +73
static DenseW get_dense_w(const void* wdata, long N, long K, int wbits, cudaStream_t stream) {
static std::unordered_map<const void*, DenseW> cache;
auto it = cache.find(wdata);
if (it != cache.end()) return it->second;
DenseW d{};
Comment on lines +33 to +37
const float scale = amax / 127.0f;
const float inv = scale > 0.f ? 1.0f / scale : 0.f;
char4 out = make_char4((char)lrintf(v.x * inv), (char)lrintf(v.y * inv), (char)lrintf(v.z * inv),
(char)lrintf(v.w * inv));
*(reinterpret_cast<char4*>(q + (size_t)m * K + kc * 128) + lane) = out;
Comment thread ggml/CMakeLists.txt
Comment on lines +208 to +209
option(GGML_CUDA_HOPPER_Q1 "ggml: opt-in sm_90a wgmma path for Q1_0 prefill" OFF)
set (GGML_CUDA_CUTLASS_DIR "" CACHE PATH "ggml: CUTLASS checkout root for GGML_CUDA_HOPPER_Q1")
Comment on lines +74 to +76
const long nb = N * (K / 128);
cudaMalloc(&d.bits, nb * 16 * wbits);
cudaMalloc(&d.dw, nb * sizeof(float));
Comment on lines +355 to +372
static bool attr_set = false;
if (!attr_set) {
cudaFuncSetAttribute(hopper_q1::lowbit_wgmma_ggml<1>, cudaFuncAttributeMaxDynamicSharedMemorySize, SMEM_BYTES);
cudaFuncSetAttribute(hopper_q1::lowbit_wgmma_ggml<2>, cudaFuncAttributeMaxDynamicSharedMemorySize, SMEM_BYTES);
cudaFuncSetAttribute(hopper_q1::lowbit_wgmma_ggml_sk<1>, cudaFuncAttributeMaxDynamicSharedMemorySize, SMEM_BYTES);
cudaFuncSetAttribute(hopper_q1::lowbit_wgmma_ggml_sk<2>, cudaFuncAttributeMaxDynamicSharedMemorySize, SMEM_BYTES);
attr_set = true;
}
const int ntm = (int)(M / hopper_q1::bM), ntn = (int)(N / hopper_q1::bN);
const int ntiles = ntm * ntn;
static int NSM = 0;
if (NSM == 0) cudaDeviceGetAttribute(&NSM, cudaDevAttrMultiProcessorCount, ctx.device);
if (ntiles < 8 * NSM) {
// starved grid -> stream-K (persistent; fp32-additive atomic flush into zeroed C)
const long total = (long)ntiles * (K / 128);
cudaMemsetAsync(dst->data, 0, (size_t)M * N * sizeof(float), stream);
kern_sk<<<NSM, 512, SMEM_BYTES, stream>>>(act_q.get(), act_d.get(),
wq.bits, wq.dw, (float*)dst->data, (int)M, (int)N, (int)K, ntn, total, NSM);
Comment on lines +141 to +150
if (GGML_CUDA_HOPPER_Q1)
# opt-in Hopper (sm_90a) wgmma path for Q1_0 prefill; needs CUTLASS headers (CuTe)
if (NOT GGML_CUDA_CUTLASS_DIR)
message(FATAL_ERROR "GGML_CUDA_HOPPER_Q1 requires GGML_CUDA_CUTLASS_DIR (CUTLASS checkout root)")
endif()
add_compile_definitions(GGML_USE_HOPPER_Q1)
include_directories(${GGML_CUDA_CUTLASS_DIR}/include ${GGML_CUDA_CUTLASS_DIR}/tools/util/include)
list(APPEND CMAKE_CUDA_FLAGS " --expt-relaxed-constexpr")
string(REPLACE ";" " " CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS}")
endif()
@bri-prism
bri-prism merged commit 9b98ac8 into prism Jun 10, 2026
1 check passed
bri-prism added a commit that referenced this pull request Jun 11, 2026
…dening)

Hardening follow-up to #39 — same Hopper sm_90a wgmma path. Adds: device-keyed repack cache + mutex + publish-after-sync, per-device attr_set/NSM (multi-GPU correctness), CUDA_CHECK on allocations, defensive int8 clamp, target-scoped CMake. Verified: perf-neutral vs #39 (worst -0.5%), +7.6-8.9% vs stock MMQ holds, 86/86 test-backend-ops; multi-arch/multi-GPU sweep green (2xH100 dual-GPU, L40S no-regression, H200 +6%). Closes the multi-GPU gap left open in #39.
@khosravipasha
khosravipasha deleted the feat/cuda-hopper-q1-wgmma branch June 27, 2026 21:58
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 2, 2026
Build the CUTLASS-based sm_90a path when GGML_CUDA_HOPPER_Q1 is enabled. Select it for supported prefill shapes and retain the existing MMQ fallback.

Source: PrismML-Eng#171

Originally written by bri-prism in PrismML-Eng#39:
576fdf6
0ce8b19

Source commits (PrismML-Eng/llama.cpp):
0845962
6ac5eb0

Co-authored-by: Pasha Khosravi <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: OpenAI Codex

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 24, 2026
Build the CUTLASS-based sm_90a path when GGML_CUDA_HOPPER_Q1 is enabled. Select it for supported prefill shapes and retain the existing MMQ fallback.

Source: PrismML-Eng#171

Originally written by bri-prism in PrismML-Eng#39:
576fdf6
0ce8b19

Source commits (PrismML-Eng/llama.cpp):
0845962
6ac5eb0

Co-authored-by: Pasha Khosravi <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: OpenAI Codex

Ported and rebased by Codex.
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.

3 participants