Repository navigation
cuda: opt-in Hopper wgmma path for Q1_0/Q2_0 prefill (sm_90a) - #39
Conversation
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 whenne1 >= 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.
| 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{}; |
| 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; |
| 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") |
| const long nb = N * (K / 128); | ||
| cudaMalloc(&d.bits, nb * 16 * wbits); | ||
| cudaMalloc(&d.dw, nb * sizeof(float)); |
| 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); |
| 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() |
…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.
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.
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.
What
Opt-in Hopper (sm_90a) tensor-core path for
Q1_0mul_matat prefill batch sizes (ne1 >= 128):__constant__LUTserializes on divergent indices with real high-entropy weights)
accumulator drain
starved shapes; fixed tile grid otherwise; SM count queried per device
Gating
-DGGML_CUDA_HOPPER_Q1=ON -DGGML_CUDA_CUTLASS_DIR=<cutlass checkout>(default OFF;stub compiles without CUTLASS)
GGML_HOPPER_Q1must be set; otherwise (and for cc < 9.0, unsupportedshapes/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):
Ternary test model (Q2_0, second commit):
test-backend-ops test -o MUL_MAT(q1_0 + q2_0): 86/86 OKQ2_0 mean 0.0013 — the per-128 activation scale is noise-level vs stock per-32 q8_1
Limitations
memory) for the lifetime of the process