From a1c75b51234db11b00f521ae3e1449751dd674a3 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:51:09 +0900 Subject: [PATCH 1/7] hip: virtual-warp decode GEMVs for gfx11/gfx12 (same bits) Class: same bits. A wave does the block kernel's four warps in turn (no LDS, no barrier, 1-2 rows a wave) for Inkling 2 Mini's IQ4_NL / Q8_0 expert products, the grouped dense Q8_0 launch and the loop controllers' down product, on gfx11xx/gfx12xx only. The dot is written as an explicit d*sumi accumulate. CUDA and other HIP archs unchanged. STRATA_GEMV_VWARP=0 turns it off. Test: poly_gemv_vwarp_test (byte compare). Credit: measured and designed from E3 (rx9070xt-e123.md); idea of the wave layout from VecSzn's engine and merbanan/cobra91/Tako88's lanes work. Co-Authored-By: Claude Opus 5.5 --- engine/cmake/strata_poly.cmake | 5 + engine/include/strata/kernels/gfx_arch.hpp | 7 + engine/include/strata/kernels/native_mmvq.hpp | 11 + engine/src/kernels/cuda/native_mmvq.cu | 216 +++++++++++++ engine/src/poly/gemv_vwarp_test.cu | 286 ++++++++++++++++++ engine/src/poly/inkling2.cu | 73 +++++ 6 files changed, 598 insertions(+) create mode 100644 engine/src/poly/gemv_vwarp_test.cu diff --git a/engine/cmake/strata_poly.cmake b/engine/cmake/strata_poly.cmake index 11a4ab043e..6e38a822b9 100644 --- a/engine/cmake/strata_poly.cmake +++ b/engine/cmake/strata_poly.cmake @@ -272,6 +272,11 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA ${_strata_gpu_include_directories}) target_link_libraries(poly_norm_q8_test PRIVATE strata_kernels ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target}) + # the HIP gfx11 / gfx12 virtual-warp decode GEMVs against the block kernels, byte for byte; needs a card, run by hand + add_executable(poly_gemv_vwarp_test src/poly/gemv_vwarp_test.cu src/poly/inkling2.cu src/poly/kernels.cu) + target_include_directories(poly_gemv_vwarp_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include + ${_strata_gpu_include_directories}) + target_link_libraries(poly_gemv_vwarp_test PRIVATE strata_kernels ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target}) # a prompt batch's products with few outputs against the same sums on the host; needs a card, run by hand add_executable(poly_gemm_small_test src/poly/gemm_small_test.cu src/poly/kernels.cu) target_include_directories(poly_gemm_small_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include diff --git a/engine/include/strata/kernels/gfx_arch.hpp b/engine/include/strata/kernels/gfx_arch.hpp index 847d7a792e..e965cbf88f 100644 --- a/engine/include/strata/kernels/gfx_arch.hpp +++ b/engine/include/strata/kernels/gfx_arch.hpp @@ -41,4 +41,11 @@ inline bool gfx_arch_is_gfx1151(const char* gcn) { return gfx_arch_is(gcn, "gfx1 /// RDNA4 (Radeon AI PRO R9700 = gfx1201, RX 9070 / 9070 XT = gfx1201, RX 9060 = gfx1200). inline bool gfx_arch_is_gfx12(const char* gcn) { return gfx_arch_is(gcn, "gfx1200") || gfx_arch_is(gcn, "gfx1201"); } +/// RDNA3 / RDNA3.5 / RDNA4 (every gfx11xx and gfx12xx part): wave32, no matrix-core requirement. The decode GEMV +/// kernels without shared memory or barriers (native_gemv_vwarp_active) are chosen for these. +inline bool gfx_arch_is_rdna34(const char* gcn) { + return gcn != nullptr && gcn[0] == 'g' && gcn[1] == 'f' && gcn[2] == 'x' && + ((gcn[3] == '1' && gcn[4] == '1') || (gcn[3] == '1' && gcn[4] == '2')) && gcn[5] >= '0' && gcn[5] <= '9'; +} + } // namespace strata::kernels diff --git a/engine/include/strata/kernels/native_mmvq.hpp b/engine/include/strata/kernels/native_mmvq.hpp index d6d733b2de..7f273a2fe2 100644 --- a/engine/include/strata/kernels/native_mmvq.hpp +++ b/engine/include/strata/kernels/native_mmvq.hpp @@ -39,6 +39,17 @@ bool native_mmvq_multi_exact(); void native_mmvq_set_b6_rows(int mode); int native_mmvq_b6_rows(); +/// HIP gfx11 / gfx12 (RDNA3, RDNA4) only: decode GEMVs written as "virtual warps" (a wave does the block kernel's four +/// warps in turn, each into its own sum, added in the block's warp order; no shared memory, no barrier, one or two +/// rows a wave) for Inkling 2 Mini's shapes: the expert gate|up and down (IQ4_NL, Q8_0; n_in 2304 or 1152) through +/// native_mmvq_slots(), the grouped dense Q8_0 launch (native_mmvq_many, n_in 2304) and the loop controllers' down +/// product (ink_ctrl_down). Every output is bitwise the block kernels' (gemv_vwarp_test). True where the current +/// device takes them: a HIP build, a gfx11xx / gfx12xx card, STRATA_GEMV_VWARP not "0". Always false on CUDA, on +/// other HIP cards and on the gfx906 build. +bool native_gemv_vwarp_active(); +/// Tests: -1 (default) the rule above, 0 off, 1 on where the device and the build allow it. +void native_gemv_vwarp_force(int mode); + // One quantization may serve multiple weight matrices sharing the same input. // Q8_1 stores FP16 scale and FP16 warp sum of the ORIGINAL float inputs; it does // not reconstruct that sum from the quantized integers. diff --git a/engine/src/kernels/cuda/native_mmvq.cu b/engine/src/kernels/cuda/native_mmvq.cu index bfebd1ca87..789a26bfd6 100644 --- a/engine/src/kernels/cuda/native_mmvq.cu +++ b/engine/src/kernels/cuda/native_mmvq.cu @@ -2079,8 +2079,200 @@ __global__ void native_mmvq_slots_warp_kernel(S slots, int n_in, int n_out) { } } +#if defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) +// ======================= HIP gfx11 / gfx12: decode GEMVs as virtual warps (gemv_vwarp_test) ======================= +// +// The block kernels above (native_mmvq_slots_kernel, native_small_mmvq_many_kernel) run 4 warps a block, each lane +// adding its terms in order, then the 3 other warps' sums are added through shared memory and a barrier, then one +// warp_sum. Here one wave does the four warps in turn: virtual warp v keeps its own sum t[v] (the lane's terms of the +// block kernel's warp v, in the same order), and the sums are added v = 1, 2, 3 onto v = 0 and warp_summed: the same +// floats in the same order, so every output is the same bits, with no shared memory and no barrier, and RW row groups +// a wave. On an RX 9070 XT (E3 of rx9070xt-e123.md) the expert gate|up read 355 -> 520 GB/s and the down 270 -> 422. +// +// The term is written as d and sumi and accumulated explicitly: the traits' apply() gives the same bits, but its +// codegen was up to 1.6x slower. C = 1: an fma of d and sumi onto the sum; C = 2: a rounded product and then the add +// (the compiler contracts both to the one fma the block kernels use; they differ in scheduling). +template struct VwSplit; +template <> struct VwSplit> { + using F = SmallTraits; + __device__ static void go(const F::W& r, const Q81Block* __restrict__ x, int iqs, float& d, int& sumi) { + const int* q8 = reinterpret_cast(x->qs) + iqs; + sumi = 0; +#pragma unroll + for (int i = 0; i < 2; ++i) { + sumi = STRATA_DP4A(r.v[i].x, q8[i], sumi); + sumi = STRATA_DP4A(r.v[i].y, q8[i + 4], sumi); + } + d = r.dw * __low2float(x->ds); + } +}; +template <> struct VwSplit> { + using F = SmallTraits; + __device__ static void go(const F::W& r, const Q81Block* __restrict__ x, int iqs, float& d, int& sumi) { + sumi = 0; +#pragma unroll + for (int i = 0; i < 2; ++i) { + const int u = reinterpret_cast(x->qs)[iqs + i]; + sumi = STRATA_DP4A(r.v[i], u, sumi); + } + d = r.d0 * __low2float(x->ds); + } +}; +template +__device__ __forceinline__ float vw_term(float t, const typename F::Block* w, const Q81Block* x, int kqs) { + float d; + int sumi; + VwSplit::go(F::load(w, kqs), x, kqs, d, sumi); + if constexpr (C == 1) return __fmaf_rn(d, float(sumi), t); + else return __fadd_rn(t, __fmul_rn(d, float(sumi))); +} + +// native_mmvq_slots_kernel's launch of several matrices: blockIdx.y the matrix; a wave takes RW groups of ROWS rows, +// WPB waves a block. +template +__launch_bounds__(WPB * WARP) __global__ void native_mmvq_slots_vw_kernel(S slots, int n_in, int n_out) { + const SlotPtrs at = slot_ptrs(slots, int(blockIdx.y)); + const auto* w = static_cast(at.w); + const auto* x = static_cast(at.x); + float* y = at.y; + const int lane = int(threadIdx.x); + const int g0 = (int(blockIdx.x) * WPB + int(threadIdx.y)) * RW; // first row group of this wave + if (g0 * ROWS >= n_out) return; + const int bpr = n_in / F::DIV; + float t[RW][WARPS][ROWS]; +#pragma unroll + for (int r = 0; r < RW; ++r) +#pragma unroll + for (int v = 0; v < WARPS; ++v) +#pragma unroll + for (int i = 0; i < ROWS; ++i) t[r][v][i] = 0.0f; + for (int base = 0; base < bpr; base += F::BPI) { +#pragma unroll + for (int v = 0; v < WARPS; ++v) { + const int tid = WARP * v + lane; + const int kbx = tid / F::T + base; + if (kbx >= bpr) continue; + const int kby = kbx * F::KBY; + const int kqs = F::kqs(tid); +#pragma unroll + for (int r = 0; r < RW; ++r) +#pragma unroll + for (int i = 0; i < ROWS; ++i) { + const int row = (g0 + r) * ROWS + i; + if (row < n_out) { + const std::size_t block = std::size_t(row) * bpr + kbx; + t[r][v][i] = vw_term(t[r][v][i], w + block, x + kby, kqs); + } + } + } + } +#pragma unroll + for (int r = 0; r < RW; ++r) +#pragma unroll + for (int i = 0; i < ROWS; ++i) { + float s = t[r][0][i]; +#pragma unroll + for (int v = 1; v < WARPS; ++v) s += t[r][v][i]; + s = warp_sum(s); + const int row = (g0 + r) * ROWS + i; + if (lane == i && row < n_out) y[row] = s; + } +} + +template +void launch_slots_vw(const S& slots, int count, int n_in, int n_out, cudaStream_t s) { + const int groups = (n_out + ROWS - 1) / ROWS; + const dim3 blocks(unsigned((groups + RW * WPB - 1) / (RW * WPB)), unsigned(count)); + native_mmvq_slots_vw_kernel<<>>(slots, n_in, n_out); +} + +// native_small_mmvq_many_kernel of up to 8 matrices: a wave takes RW rows +template +__launch_bounds__(WPB * WARP) __global__ void native_q8_0_many_vw_kernel(MmvqMany a, int n_in, int total) { + using F = SmallTraits; + const int lane = int(threadIdx.x); + const int r0 = (int(blockIdx.x) * WPB + int(threadIdx.y)) * RW; + if (r0 >= total) return; + const int bpr = n_in / 32; + float t[RW][WARPS]; + const Q80Block* wr[RW]; + const Q81Block* xr[RW]; + int mrow[RW], mm[RW]; +#pragma unroll + for (int r = 0; r < RW; ++r) { + const int g = min(r0 + r, total - 1); + int m = 0; + while (g >= a.first[m + 1]) ++m; + mm[r] = m; + mrow[r] = g - a.first[m]; + wr[r] = static_cast(a.w[m]) + std::size_t(mrow[r]) * bpr; + xr[r] = a.x[m]; +#pragma unroll + for (int v = 0; v < WARPS; ++v) t[r][v] = 0.0f; + } + for (int base = 0; base < bpr; base += F::BPI) { +#pragma unroll + for (int v = 0; v < WARPS; ++v) { + const int tid = WARP * v + lane; + const int kbx = tid / F::T + base; + if (kbx >= bpr) continue; + const int kqs = F::kqs(tid); +#pragma unroll + for (int r = 0; r < RW; ++r) t[r][v] = vw_term(t[r][v], wr[r] + kbx, xr[r] + kbx, kqs); + } + } +#pragma unroll + for (int r = 0; r < RW; ++r) { + float s = t[r][0]; +#pragma unroll + for (int v = 1; v < WARPS; ++v) s += t[r][v]; + s = warp_sum(s); + if (lane == 0 && r0 + r < total) a.y[mm[r]][mrow[r]] = s; + } +} + +// Whether the current device takes the virtual-warp kernels: gfx11xx / gfx12xx, and not STRATA_GEMV_VWARP=0. +int g_vwarp_force = -1; +bool vwarp_device_on() { + static signed char arch_ok[16]; // per device: 0 unknown, 1 not gfx11xx / gfx12xx, 2 it is + if (g_vwarp_force == 0) return false; + int dev = 0; + if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 16) return false; + if (arch_ok[dev] == 0) { + cudaDeviceProp p; + const bool arch = cudaGetDeviceProperties(&p, dev) == cudaSuccess && strata::kernels::gfx_arch_is_rdna34(p.gcnArchName); + (void) cudaGetLastError(); + arch_ok[dev] = arch ? 2 : 1; + } + if (arch_ok[dev] != 2) return false; + static const bool env_off = [] { + const char* v = std::getenv("STRATA_GEMV_VWARP"); + return v != nullptr && v[0] == '0'; + }(); + return g_vwarp_force == 1 || !env_off; +} + +// Inkling 2 Mini's expert shapes only (n_in 2304 gate|up, 1152 down; the two formats E3 measured): the variant per shape. +template +bool launch_slots_vwarp(const S& slots, int count, int n_in, int n_out, cudaStream_t s) { + using IQ4 = SmallTraits; + using Q8 = SmallTraits; + if constexpr (std::is_same_v) { + if (n_in == 2304) { launch_slots_vw(slots, count, n_in, n_out, s); return true; } + if (n_in == 1152) { launch_slots_vw(slots, count, n_in, n_out, s); return true; } + } else if constexpr (std::is_same_v) { + if (n_in == 2304) { launch_slots_vw(slots, count, n_in, n_out, s); return true; } + if (n_in == 1152) { launch_slots_vw(slots, count, n_in, n_out, s); return true; } + } + return false; +} +#endif + template void launch_slots(const S& slots, int count, int n_in, int n_out, cudaStream_t s) { +#if defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) + if (vwarp_device_on() && n_in % 32 == 0 && launch_slots_vwarp(slots, count, n_in, n_out, s)) return; +#endif const dim3 threads(WARP, WARPS); if (native_mmvq_slots_wide() && n_in / F::DIV * F::T <= WARP && n_in % F::DIV == 0) { constexpr int ROWS = 2; @@ -4372,6 +4564,14 @@ void native_mmvq_many(int ggml_type, const NativeMmvqMany& m, int n_in, void* st for (int j = m.count; j < 8; ++j) a.first[j + 1] = a.first[m.count]; const dim3 grid(unsigned(a.first[m.count])), threads(WARP, WARPS); const auto s = static_cast(stream); +#if defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) + if (ggml_type == 8 && n_in == 2304 && vwarp_device_on()) { // Inkling 2 Mini's q|k|v|gate|rel|sink launch + const int total = a.first[m.count]; + native_q8_0_many_vw_kernel<1, 4, 2><<>>(a, n_in, total); + launch_check(); + return; + } +#endif switch (ggml_type) { case 2: native_small_mmvq_many_kernel<<>>(a, n_in); break; case 6: native_small_mmvq_many_kernel<<>>(a, n_in); break; @@ -4662,6 +4862,22 @@ void launch_slots_of(int ggml_type, const S& slots, int count, int n_in, int n_o } // namespace +bool native_gemv_vwarp_active() { +#if defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) + return vwarp_device_on(); +#else + return false; +#endif +} + +void native_gemv_vwarp_force(int mode) { +#if defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) + g_vwarp_force = mode; +#else + (void) mode; +#endif +} + bool native_mmvq_slots_wide() { static const bool on = [] { const char* v = std::getenv("STRATA_SLOTS_WARP"); diff --git a/engine/src/poly/gemv_vwarp_test.cu b/engine/src/poly/gemv_vwarp_test.cu new file mode 100644 index 0000000000..486297fa60 --- /dev/null +++ b/engine/src/poly/gemv_vwarp_test.cu @@ -0,0 +1,286 @@ +// src/poly/gemv_vwarp_test.cu - the HIP gfx11 / gfx12 virtual-warp decode GEMVs (kernels::native_gemv_vwarp_force) against the +// block kernels they replace, byte for byte: Inkling 2 Mini's expert products through native_mmvq_slots() and +// native_mmvq_ids() (IQ4_NL and Q8_0, n_in 2304 and 1152, 1 to 32 experts), the grouped dense Q8_0 launch +// native_mmvq_many(), and the loop controllers' down product ink_ctrl_down() (F32 and BF16). Every output of the new +// kernels must be the same bytes as the old kernels' (outputs start as 0xff bytes, so an unwritten value differs). +// +// usage: poly_gemv_vwarp_test (exit 0: every byte the same; 77: not a gfx11 / gfx12 card, nothing to compare) +#include "inkling2_kernels.hpp" +#include "strata/kernels/native_mmvq.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace { + +using namespace strata; +std::mt19937 rng(20261124); +cudaStream_t st; +int g_fail = 0, g_cases = 0; + +void ck(cudaError_t e, const char* what) { + if (e != cudaSuccess) { + std::printf("GPU error at %s: %s\n", what, cudaGetErrorString(e)); + std::exit(1); + } +} +template T* upload(const std::vector& v) { + T* d = nullptr; + ck(cudaMalloc(&d, std::max(1, v.size() * sizeof(T))), "malloc"); + ck(cudaMemcpy(d, v.data(), v.size() * sizeof(T), cudaMemcpyHostToDevice), "upload"); + return d; +} +float* fill_ff(size_t n) { + float* d = nullptr; + ck(cudaMalloc(&d, n * 4), "malloc"); + ck(cudaMemset(d, 0xff, n * 4), "memset"); + return d; +} +std::vector grab(const void* p, size_t n) { + std::vector h(n); + ck(cudaStreamSynchronize(st), "sync"); + ck(cudaMemcpy(h.data(), p, n, cudaMemcpyDeviceToHost), "download"); + return h; +} +uint16_t half_bits(float f) { + const __half h = __float2half(f); + uint16_t b; + std::memcpy(&b, &h, 2); + return b; +} +// random weight blocks: a half scale in bytes 0..1, random code bytes after it (IQ4_NL 18 bytes, Q8_0 34) +std::vector rand_blocks(size_t nblocks, int bsize) { + std::vector v(nblocks * bsize); + std::uniform_int_distribution byte(0, 255); + std::uniform_real_distribution sc(0.002f, 0.02f); + for (size_t b = 0; b < nblocks; ++b) { + uint8_t* p = v.data() + b * bsize; + const uint16_t d = half_bits(sc(rng) * (byte(rng) & 1 ? 1.f : -1.f)); + std::memcpy(p, &d, 2); + for (int i = 2; i < bsize; ++i) p[i] = uint8_t(byte(rng)); + } + return v; +} +std::vector rand_q81(int n) { // n / 32 Q8_1 blocks: ds = (d, d * sum) + const int nb = n / 32; + std::vector v(size_t(nb) * 36); + std::uniform_int_distribution q(-127, 127); + std::uniform_real_distribution sc(0.005f, 0.05f); + for (int b = 0; b < nb; ++b) { + uint8_t* p = v.data() + size_t(b) * 36; + int8_t qs[32]; + int sum = 0; + for (int i = 0; i < 32; ++i) qs[i] = int8_t(q(rng)), sum += qs[i]; + const float d = sc(rng); + const uint16_t h0 = half_bits(d), h1 = half_bits(d * float(sum)); + std::memcpy(p, &h0, 2); + std::memcpy(p + 2, &h1, 2); + std::memcpy(p + 4, qs, 32); + } + return v; +} +std::vector randf(size_t n, float s) { + std::normal_distribution g(0.0f, s); + std::vector v(n); + for (auto& f : v) f = g(rng); + return v; +} +void verdict(const char* what, bool same) { + ++g_cases; + if (!same) ++g_fail; + std::printf("%s %s\n", same ? "ok " : "FAIL", what); +} + +int bsize_of(int type) { return type == 8 ? 34 : 18; } + +// ---- expert slots and ids ---- +void test_slots(int type, int n_in, int n_out, int count) { + char what[160]; + std::snprintf(what, sizeof what, "slots type %2d n_in %4d n_out %4d experts %2d", type, n_in, n_out, count); + std::vector w; + std::vector x; + kernels::MmvqSlots s0{}, s1{}; + std::vector y0, y1; + for (int j = 0; j < count; ++j) { + w.push_back(upload(rand_blocks(size_t(n_out) * (n_in / 32), bsize_of(type)))); + x.push_back(upload(rand_q81(n_in))); + y0.push_back(fill_ff(n_out)), y1.push_back(fill_ff(n_out)); + s0.w[j] = s1.w[j] = w[j], s0.x[j] = s1.x[j] = x[j], s0.y[j] = y0[j], s1.y[j] = y1[j]; + } + kernels::native_gemv_vwarp_force(0); + const bool ok0 = kernels::native_mmvq_slots(type, s0, count, n_in, n_out, st); + kernels::native_gemv_vwarp_force(1); + const bool ok1 = kernels::native_mmvq_slots(type, s1, count, n_in, n_out, st); + bool same = ok0 && ok1; + for (int j = 0; j < count; ++j) same = same && grab(y0[j], size_t(n_out) * 4) == grab(y1[j], size_t(n_out) * 4); + verdict(what, same); + for (int j = 0; j < count; ++j) cudaFree(w[j]), cudaFree(x[j]), cudaFree(y0[j]), cudaFree(y1[j]); +} + +void test_ids(int type, int n_in, int n_out, int count) { + char what[160]; + std::snprintf(what, sizeof what, "ids type %2d n_in %4d n_out %4d experts %2d", type, n_in, n_out, count); + const int nexp = 12; // the table's experts; the ids pick count of them (repeats allowed) + const size_t off = 64; // a byte offset into every expert's allocation + const size_t wbytes = size_t(n_out) * (n_in / 32) * bsize_of(type); + std::vector w; + std::vector table; + for (int e = 0; e < nexp; ++e) { + auto b = rand_blocks((wbytes + off) / bsize_of(type) + 1, bsize_of(type)); + w.push_back(upload(b)); + table.push_back(w.back()); + } + std::vector ids(count); + for (int j = 0; j < count; ++j) ids[j] = int32_t((j * 5 + 3) % nexp); + const size_t xs = size_t(n_in / 32) * 36; + std::vector xh; + for (int j = 0; j < count; ++j) { + auto q = rand_q81(n_in); + xh.insert(xh.end(), q.begin(), q.end()); + } + const void* const* dtable = nullptr; + ck(cudaMalloc((void**) &dtable, nexp * sizeof(void*)), "malloc"); + ck(cudaMemcpy(const_cast(reinterpret_cast(dtable)), table.data(), nexp * sizeof(void*), cudaMemcpyHostToDevice), "up"); + int32_t* dids = upload(ids); + uint8_t* dx = upload(xh); + float* y0 = fill_ff(size_t(count) * n_out); + float* y1 = fill_ff(size_t(count) * n_out); + kernels::MmvqIds a{dtable, dids, off, dx, xs, 1, y0, size_t(n_out)}; + kernels::MmvqIds b = a; + b.y = y1; + kernels::native_gemv_vwarp_force(0); + const bool ok0 = kernels::native_mmvq_ids(type, a, count, n_in, n_out, st); + kernels::native_gemv_vwarp_force(1); + const bool ok1 = kernels::native_mmvq_ids(type, b, count, n_in, n_out, st); + const size_t n = size_t(count) * n_out * 4; + verdict(what, ok0 && ok1 && grab(y0, n) == grab(y1, n)); + for (auto* p : w) cudaFree(p); + cudaFree(const_cast(reinterpret_cast(dtable))), cudaFree(dids), cudaFree(dx), cudaFree(y0), cudaFree(y1); +} + +// ---- grouped dense ---- +void test_many(int type, int n_in, const std::vector& n_outs) { + char what[200]; + int off = std::snprintf(what, sizeof what, "many type %2d n_in %4d rows", type, n_in); + for (int o : n_outs) off += std::snprintf(what + off, sizeof what - off, " %d", o); + kernels::NativeMmvqMany m{}; + m.count = int(n_outs.size()); + std::vector w; + std::vector x; + std::vector y0, y1; + kernels::NativeMmvqMany m1{}; + m1.count = m.count; + for (int j = 0; j < m.count; ++j) { + w.push_back(upload(rand_blocks(size_t(n_outs[j]) * (n_in / 32), bsize_of(type)))); + x.push_back(upload(rand_q81(n_in))); + y0.push_back(fill_ff(n_outs[j])), y1.push_back(fill_ff(n_outs[j])); + m.w[j] = m1.w[j] = w[j], m.x_q8_1[j] = m1.x_q8_1[j] = x[j], m.y[j] = y0[j], m1.y[j] = y1[j]; + m.n_out[j] = m1.n_out[j] = n_outs[j]; + } + if (!kernels::native_mmvq_many_fits(type, m, n_in)) { + std::printf("FAIL %s: native_mmvq_many does not take it\n", what); + ++g_fail, ++g_cases; + return; + } + kernels::native_gemv_vwarp_force(0); + kernels::native_mmvq_many(type, m, n_in, st); + kernels::native_gemv_vwarp_force(1); + kernels::native_mmvq_many(type, m1, n_in, st); + bool same = true; + for (int j = 0; j < m.count; ++j) same = same && grab(y0[j], size_t(n_outs[j]) * 4) == grab(y1[j], size_t(n_outs[j]) * 4); + verdict(what, same); + for (int j = 0; j < m.count; ++j) cudaFree(w[j]), cudaFree(x[j]), cudaFree(y0[j]), cudaFree(y1[j]); +} + +// ---- loop controllers' down product ---- +void test_ctrl_down(bool bf16, int count, int n_embd, int rank, int rows) { + char what[160]; + std::snprintf(what, sizeof what, "ctrl_down %s controllers %d n_embd %d rank %d rows %d", bf16 ? "BF16" : "F32 ", count, + n_embd, rank, rows); + std::vector cur(count), msg(count); + std::vector down(count); + std::vector y0(count), y1(count); + std::vector frees; + for (int j = 0; j < count; ++j) { + float* c = upload(randf(size_t(n_embd) * rows, 1.0f)); + float* mg = upload(randf(size_t(n_embd) * rows, 0.7f)); + cur[j] = c, msg[j] = mg; + const auto wf = randf(size_t(rank) * 2 * n_embd, 0.02f); + if (bf16) { + std::vector wb(wf.size()); + for (size_t i = 0; i < wf.size(); ++i) { + uint32_t u; + std::memcpy(&u, &wf[i], 4); + wb[i] = uint16_t(u >> 16); + } + down[j] = upload(wb); + } else { + down[j] = upload(wf); + } + y0[j] = fill_ff(size_t(rank) * rows), y1[j] = fill_ff(size_t(rank) * rows); + frees.push_back(c), frees.push_back(mg), frees.push_back(const_cast(down[j])); + } + kernels::native_gemv_vwarp_force(0); + const bool ok0 = poly::ink_ctrl_down(cur.data(), msg.data(), down.data(), y0.data(), count, n_embd, rank, rows, st, bf16); + kernels::native_gemv_vwarp_force(1); + const bool ok1 = poly::ink_ctrl_down(cur.data(), msg.data(), down.data(), y1.data(), count, n_embd, rank, rows, st, bf16); + bool same = ok0 && ok1; + for (int j = 0; j < count; ++j) same = same && grab(y0[j], size_t(rank) * rows * 4) == grab(y1[j], size_t(rank) * rows * 4); + verdict(what, same); + for (void* p : frees) cudaFree(p); + for (int j = 0; j < count; ++j) cudaFree(y0[j]), cudaFree(y1[j]); +} + +} // namespace + +int main() { + int n = 0; + if (cudaGetDeviceCount(&n) != cudaSuccess || n == 0) { + std::printf("no card\n"); + return 77; + } + ck(cudaStreamCreateWithFlags(&st, cudaStreamNonBlocking), "stream"); + kernels::native_gemv_vwarp_force(1); + if (!kernels::native_gemv_vwarp_active()) { + std::printf("not a gfx11 / gfx12 HIP device: the virtual-warp kernels are not built for it, nothing to compare\n"); + return 77; + } + for (int type : {20, 8}) { + for (int count : {1, 5, 8, 32}) { + test_slots(type, 2304, 2304, count); // gate|up + test_slots(type, 1152, 2304, count); // down + } + test_slots(type, 2304, 1152, 8); // the up of a gate / up pair (UD-Q2's mixed layers) + test_slots(type, 2304, 2303, 3); // an odd row count: the tails of the row groups + test_slots(type, 1152, 2301, 3); + test_slots(type, 2304, 7, 2); + test_slots(type, 1152, 1, 2); + test_ids(type, 2304, 2304, 8); + test_ids(type, 1152, 2304, 8); + test_ids(type, 1152, 2303, 5); + } + test_many(8, 2304, {3072, 1024, 1024, 3072, 384, 24}); // q|k|v|gate|rel|sink + test_many(8, 2304, {3072, 1024, 1024}); + test_many(8, 2304, {2304}); + test_many(8, 2304, {7, 1001, 3, 2}); // odd totals and row counts + test_many(8, 2304, {1}); + test_many(8, 3072, {2304}); // attn_output's n_in: the block kernel + test_many(20, 2304, {1024, 384}); // other formats are untouched + test_many(2, 2304, {1024, 384}); + for (bool bf16 : {false, true}) + for (int count : {1, 2, 3}) { + test_ctrl_down(bf16, count, 2304, 64, 1); + test_ctrl_down(bf16, count, 2304, 64, 3); + } + test_ctrl_down(false, 3, 2304, 62, 2); // an even rank that is not 64 + test_ctrl_down(false, 3, 2304, 63, 2); // an odd rank: the block kernel + std::printf("gemv_vwarp_test: %d cases, %d FAILED\n", g_cases, g_fail); + return g_fail == 0 ? 0 : 1; +} diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index c5d7266393..4c91d5632f 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -6,6 +6,7 @@ // q81_div(): a block's scale divided as native_mmvq.cu's --use_fast_math division does it (q8_1_finite.hpp) #define STRATA_Q81_FASTMATH_DIV 1 #include "inkling2_kernels.hpp" +#include "strata/kernels/native_mmvq.hpp" #include "strata/kernels/pdl.hpp" #include "strata/kernels/q8_1_finite.hpp" @@ -685,6 +686,68 @@ bool fast_ctrl_on() { return g_fast_ctrl == 1; #endif } +#if defined(__HIPCC__) +// ink_ctrl_down_kernel for HIP gfx11 / gfx12 (kernels::native_gemv_vwarp_active): O outputs of one controller a block, +// each its own product, with one norm shared. The kernel above's thread t has the sums of its threads t and +// t + kDownThreads (V = 2 here: virtual thread v of the block's 1024 sums i = v * kDownThreads + tid + 1024 k, as acc0 +// and acc1 do), the 32 warps' sums are added in their order: the same bits, with half the blocks' norm work and each +// block streaming O rows (E3: 21.5 -> 15.8 us on an RX 9070 XT). +template +__global__ void __launch_bounds__(kDownThreads) ink_ctrl_down_vw_kernel(CtrlDown d, int n_embd, int rank) { + constexpr int V = 2; + __shared__ float pa[kCatThreads / WARP], pb[kCatThreads / WARP], part[O][32]; + const int per = rank / O; // blocks a controller + const int j = blockIdx.x / per, o0 = (blockIdx.x % per) * O, tid = threadIdx.x; + const size_t row = size_t(blockIdx.y) * n_embd; + const float* cur = d.cur[j] + row; + const float* msg = d.msg[j] + row; + if (tid < kCatThreads) { + float a, b; + cat_squares(cur, msg, n_embd, tid, a, b); + a = warp_sum(a); + b = warp_sum(b); + if ((tid & 31) == 0) pa[tid >> 5] = a, pb[tid >> 5] = b; + } + __syncthreads(); + float sa = 0.0f, sb = 0.0f; + for (int k = 0; k < kCatThreads / WARP; ++k) sa += pa[k]; + for (int k = 0; k < kCatThreads / WARP; ++k) sb += pb[k]; + const float ia = cat_scale(sa, n_embd), ib = cat_scale(sb, n_embd); + const int n_in = 2 * n_embd; + float acc[O][V]; +#pragma unroll + for (int q = 0; q < O; ++q) +#pragma unroll + for (int v = 0; v < V; ++v) acc[q][v] = 0.0f; + for (int b0 = 0; b0 < n_in; b0 += V * kDownThreads) { +#pragma unroll + for (int v = 0; v < V; ++v) { + const int i = b0 + v * kDownThreads + tid; + if (i < n_in) { + const float xi = i < n_embd ? cur[i] * ia : msg[i - n_embd] * ib; +#pragma unroll + for (int q = 0; q < O; ++q) { + const W* __restrict__ wr = static_cast(d.w[j]) + size_t(o0 + q) * n_in; + acc[q][v] += wval(wr, i) * xi; + } + } + } + } +#pragma unroll + for (int q = 0; q < O; ++q) +#pragma unroll + for (int v = 0; v < V; ++v) { + const float sv = warp_sum(acc[q][v]); + if ((tid & 31) == 0) part[q][(v * kDownThreads + tid) >> 5] = sv; + } + __syncthreads(); + if (tid < O) { + float sum = 0.0f; + for (int k = 0; k < 2 * kDownThreads / WARP; ++k) sum += part[tid][k]; + d.y[j][size_t(blockIdx.y) * rank + o0 + tid] = sum; + } +} +#endif struct GateSet { const float* cur[kSet]; @@ -2914,6 +2977,16 @@ bool ink_ctrl_down(const float* const* cur, const float* const* msg, const void* launched("ink_ctrl_down_fast"); return true; } +#if defined(__HIPCC__) + if (rank % 2 == 0 && strata::kernels::native_gemv_vwarp_active()) { + if (bf16) + ink_ctrl_down_vw_kernel<<>>(d, n_embd, rank); + else + ink_ctrl_down_vw_kernel<<>>(d, n_embd, rank); + launched("ink_ctrl_down"); + return true; + } +#endif if (bf16) launch_pdl(ink_ctrl_down_kernel, dim3(count * rank, rows), dim3(kDownThreads), 0, cs(stream), d, n_embd, rank); From 47d2beb8d36b1719dac159c00397c1d08e4c0b83 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:05:36 +0900 Subject: [PATCH 2/7] hip: gemv_vwarp_test: plain void** table --- engine/src/poly/gemv_vwarp_test.cu | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/engine/src/poly/gemv_vwarp_test.cu b/engine/src/poly/gemv_vwarp_test.cu index 486297fa60..e4b1a631a2 100644 --- a/engine/src/poly/gemv_vwarp_test.cu +++ b/engine/src/poly/gemv_vwarp_test.cu @@ -145,9 +145,9 @@ void test_ids(int type, int n_in, int n_out, int count) { auto q = rand_q81(n_in); xh.insert(xh.end(), q.begin(), q.end()); } - const void* const* dtable = nullptr; + void** dtable = nullptr; ck(cudaMalloc((void**) &dtable, nexp * sizeof(void*)), "malloc"); - ck(cudaMemcpy(const_cast(reinterpret_cast(dtable)), table.data(), nexp * sizeof(void*), cudaMemcpyHostToDevice), "up"); + ck(cudaMemcpy(dtable, table.data(), nexp * sizeof(void*), cudaMemcpyHostToDevice), "up"); int32_t* dids = upload(ids); uint8_t* dx = upload(xh); float* y0 = fill_ff(size_t(count) * n_out); @@ -162,7 +162,7 @@ void test_ids(int type, int n_in, int n_out, int count) { const size_t n = size_t(count) * n_out * 4; verdict(what, ok0 && ok1 && grab(y0, n) == grab(y1, n)); for (auto* p : w) cudaFree(p); - cudaFree(const_cast(reinterpret_cast(dtable))), cudaFree(dids), cudaFree(dx), cudaFree(y0), cudaFree(y1); + cudaFree(dtable), cudaFree(dids), cudaFree(dx), cudaFree(y0), cudaFree(y1); } // ---- grouped dense ---- From 47a41fe321063d6b63c7ad292380fe18ad39e479 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:07:13 +0900 Subject: [PATCH 3/7] hip: gemv_vwarp_test: synchronize the null-stream fills --- engine/src/poly/gemv_vwarp_test.cu | 2 ++ 1 file changed, 2 insertions(+) diff --git a/engine/src/poly/gemv_vwarp_test.cu b/engine/src/poly/gemv_vwarp_test.cu index e4b1a631a2..03f56255b4 100644 --- a/engine/src/poly/gemv_vwarp_test.cu +++ b/engine/src/poly/gemv_vwarp_test.cu @@ -35,12 +35,14 @@ template T* upload(const std::vector& v) { T* d = nullptr; ck(cudaMalloc(&d, std::max(1, v.size() * sizeof(T))), "malloc"); ck(cudaMemcpy(d, v.data(), v.size() * sizeof(T), cudaMemcpyHostToDevice), "upload"); + ck(cudaDeviceSynchronize(), "sync"); // the null stream's copies are done before a launch on the non-blocking stream return d; } float* fill_ff(size_t n) { float* d = nullptr; ck(cudaMalloc(&d, n * 4), "malloc"); ck(cudaMemset(d, 0xff, n * 4), "memset"); + ck(cudaDeviceSynchronize(), "sync"); // the memset runs on the null stream: done before the launch on st return d; } std::vector grab(const void* p, size_t n) { From 67bb904b73e3276d4af5eaa65ba2bd27566e823d Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:23:46 +0900 Subject: [PATCH 4/7] hip: gemv_vwarp_test --bench (cold rotation, old vs new kernels) --- engine/src/poly/gemv_vwarp_test.cu | 98 +++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/engine/src/poly/gemv_vwarp_test.cu b/engine/src/poly/gemv_vwarp_test.cu index 03f56255b4..fd75988cc1 100644 --- a/engine/src/poly/gemv_vwarp_test.cu +++ b/engine/src/poly/gemv_vwarp_test.cu @@ -240,9 +240,98 @@ void test_ctrl_down(bool bf16, int count, int n_embd, int rank, int rows) { for (int j = 0; j < count; ++j) cudaFree(y0[j]), cudaFree(y1[j]); } +// ---- --bench [reps]: the old and the new kernels' time a launch, weights rotating through a pool far larger than the +// Infinity Cache ("cold": what a decode token sees), the two taken in turn ---- +template double time_it(L&& launch, int nsets, int reps) { + cudaEvent_t a, b; + ck(cudaEventCreate(&a), "ev"); + ck(cudaEventCreate(&b), "ev"); + for (int i = 0; i < 20; ++i) launch(i % nsets); + std::vector r; + for (int k = 0; k < 5; ++k) { + ck(cudaEventRecord(a, st), "rec"); + for (int i = 0; i < reps; ++i) launch((i * 7 + k) % nsets); + ck(cudaEventRecord(b, st), "rec"); + ck(cudaEventSynchronize(b), "sync"); + float ms = 0; + ck(cudaEventElapsedTime(&ms, a, b), "elapsed"); + r.push_back(1000.0 * ms / reps); + } + std::sort(r.begin(), r.end()); + return r[2]; +} +void bench_slots(int type, int n_in, int n_out, int reps) { + const int count = 8, pool = type == 8 ? 64 : 128; + std::vector w; + for (int i = 0; i < pool; ++i) w.push_back(upload(rand_blocks(size_t(n_out) * (n_in / 32), bsize_of(type)))); + std::vector x; + std::vector y; + for (int j = 0; j < count; ++j) x.push_back(upload(rand_q81(n_in))), y.push_back(fill_ff(n_out)); + const double bytes = double(count) * n_out * (n_in / 32) * bsize_of(type); + auto run = [&](int mode) { + kernels::native_gemv_vwarp_force(mode); + return time_it([&](int set) { + kernels::MmvqSlots sl{}; + for (int j = 0; j < count; ++j) sl.w[j] = w[(size_t(set) * count + j) % w.size()], sl.x[j] = x[j], sl.y[j] = y[j]; + kernels::native_mmvq_slots(type, sl, count, n_in, n_out, st); + }, pool / count, reps); + }; + const double o1 = run(0), n1 = run(1), o2 = run(0), n2 = run(1); + const double o = std::min(o1, o2), nn = std::min(n1, n2); + std::printf("BENCH slots type %2d %4d -> %4d x%d: old %6.1f us %6.1f GB/s | new %6.1f us %6.1f GB/s | %+.1f%%\n", type, n_in, + n_out, count, o, bytes / o / 1e3, nn, bytes / nn / 1e3, 100.0 * (o / nn - 1)); + for (auto* p : w) cudaFree(p); +} +void bench_many(int reps) { + const std::vector outs = {3072, 1024, 1024, 3072, 384, 24}; + const int n_in = 2304, copies = 16; + std::vector> w(copies); + double bytes = 0; + for (int c = 0; c < copies; ++c) + for (int o : outs) w[c].push_back(upload(rand_blocks(size_t(o) * (n_in / 32), 34))); + std::vector x; + std::vector y; + for (int o : outs) x.push_back(upload(rand_q81(n_in))), y.push_back(fill_ff(o)), bytes += double(o) * (n_in / 32) * 34; + auto run = [&](int mode) { + kernels::native_gemv_vwarp_force(mode); + return time_it([&](int set) { + kernels::NativeMmvqMany m{}; + m.count = int(outs.size()); + for (int j = 0; j < m.count; ++j) m.w[j] = w[set][j], m.x_q8_1[j] = x[j], m.y[j] = y[j], m.n_out[j] = outs[j]; + kernels::native_mmvq_many(8, m, n_in, st); + }, copies, reps); + }; + const double o1 = run(0), n1 = run(1), o2 = run(0), n2 = run(1); + const double o = std::min(o1, o2), nn = std::min(n1, n2); + std::printf("BENCH many Q8_0 q|k|v|gate|rel|sink: old %6.1f us %6.1f GB/s | new %6.1f us %6.1f GB/s | %+.1f%%\n", o, + bytes / o / 1e3, nn, bytes / nn / 1e3, 100.0 * (o / nn - 1)); +} +void bench_ctrl(int reps) { + const int n_embd = 2304, rank = 64, copies = 32; + std::vector> dw(copies); + for (int c = 0; c < copies; ++c) + for (int j = 0; j < 3; ++j) dw[c].push_back(upload(randf(size_t(rank) * 2 * n_embd, 0.02f))); + float *cur[3], *msg[3], *ch[3]; + for (int j = 0; j < 3; ++j) cur[j] = upload(randf(n_embd, 1.0f)), msg[j] = upload(randf(n_embd, 0.7f)), ch[j] = fill_ff(rank); + const double bytes = 3.0 * rank * 2 * n_embd * 4; + auto run = [&](int mode) { + kernels::native_gemv_vwarp_force(mode); + return time_it([&](int set) { + const float* c[3] = {cur[0], cur[1], cur[2]}; + const float* m[3] = {msg[0], msg[1], msg[2]}; + const void* d[3] = {dw[set][0], dw[set][1], dw[set][2]}; + poly::ink_ctrl_down(c, m, d, ch, 3, n_embd, rank, 1, st, false); + }, copies, reps); + }; + const double o1 = run(0), n1 = run(1), o2 = run(0), n2 = run(1); + const double o = std::min(o1, o2), nn = std::min(n1, n2); + std::printf("BENCH ctrl_down F32 3 x 64 x 4608: old %6.1f us %6.1f GB/s | new %6.1f us %6.1f GB/s | %+.1f%%\n", o, + bytes / o / 1e3, nn, bytes / nn / 1e3, 100.0 * (o / nn - 1)); +} + } // namespace -int main() { +int main(int argc, char** argv) { int n = 0; if (cudaGetDeviceCount(&n) != cudaSuccess || n == 0) { std::printf("no card\n"); @@ -254,6 +343,13 @@ int main() { std::printf("not a gfx11 / gfx12 HIP device: the virtual-warp kernels are not built for it, nothing to compare\n"); return 77; } + if (argc > 1 && std::strcmp(argv[1], "--bench") == 0) { + const int reps = argc > 2 ? std::max(10, std::atoi(argv[2])) : 200; + for (int type : {20, 8}) bench_slots(type, 2304, 2304, reps), bench_slots(type, 1152, 2304, reps); + bench_many(reps); + bench_ctrl(reps); + return 0; + } for (int type : {20, 8}) { for (int count : {1, 5, 8, 32}) { test_slots(type, 2304, 2304, count); // gate|up From dce1b329d8ee3d4007281572d83fd5d4b4c26090 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:31:41 +0900 Subject: [PATCH 5/7] hip: narrow to the grouped Q8_0 launch and ink_ctrl_down (the expert variants did not gain) --- engine/include/strata/kernels/native_mmvq.hpp | 11 +- engine/src/kernels/cuda/native_mmvq.cu | 94 +-------------- engine/src/poly/gemv_vwarp_test.cu | 107 +----------------- 3 files changed, 10 insertions(+), 202 deletions(-) diff --git a/engine/include/strata/kernels/native_mmvq.hpp b/engine/include/strata/kernels/native_mmvq.hpp index 7f273a2fe2..1a6190e527 100644 --- a/engine/include/strata/kernels/native_mmvq.hpp +++ b/engine/include/strata/kernels/native_mmvq.hpp @@ -40,12 +40,11 @@ void native_mmvq_set_b6_rows(int mode); int native_mmvq_b6_rows(); /// HIP gfx11 / gfx12 (RDNA3, RDNA4) only: decode GEMVs written as "virtual warps" (a wave does the block kernel's four -/// warps in turn, each into its own sum, added in the block's warp order; no shared memory, no barrier, one or two -/// rows a wave) for Inkling 2 Mini's shapes: the expert gate|up and down (IQ4_NL, Q8_0; n_in 2304 or 1152) through -/// native_mmvq_slots(), the grouped dense Q8_0 launch (native_mmvq_many, n_in 2304) and the loop controllers' down -/// product (ink_ctrl_down). Every output is bitwise the block kernels' (gemv_vwarp_test). True where the current -/// device takes them: a HIP build, a gfx11xx / gfx12xx card, STRATA_GEMV_VWARP not "0". Always false on CUDA, on -/// other HIP cards and on the gfx906 build. +/// warps in turn, each into its own sum, added in the block's warp order; no shared memory, no barrier) for Inkling 2 +/// Mini's grouped dense Q8_0 launch (native_mmvq_many, n_in 2304) and the loop controllers' down product +/// (ink_ctrl_down). Every output is bitwise the block kernels' (gemv_vwarp_test). True where the current device takes +/// them: a HIP build, a gfx11xx / gfx12xx card, STRATA_GEMV_VWARP not "0". Always false on CUDA, on other HIP cards and +/// on the gfx906 build. bool native_gemv_vwarp_active(); /// Tests: -1 (default) the rule above, 0 off, 1 on where the device and the build allow it. void native_gemv_vwarp_force(int mode); diff --git a/engine/src/kernels/cuda/native_mmvq.cu b/engine/src/kernels/cuda/native_mmvq.cu index 789a26bfd6..23edad039b 100644 --- a/engine/src/kernels/cuda/native_mmvq.cu +++ b/engine/src/kernels/cuda/native_mmvq.cu @@ -2082,30 +2082,18 @@ __global__ void native_mmvq_slots_warp_kernel(S slots, int n_in, int n_out) { #if defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) // ======================= HIP gfx11 / gfx12: decode GEMVs as virtual warps (gemv_vwarp_test) ======================= // -// The block kernels above (native_mmvq_slots_kernel, native_small_mmvq_many_kernel) run 4 warps a block, each lane +// The block kernel above (native_small_mmvq_many_kernel) runs 4 warps a block, each lane // adding its terms in order, then the 3 other warps' sums are added through shared memory and a barrier, then one // warp_sum. Here one wave does the four warps in turn: virtual warp v keeps its own sum t[v] (the lane's terms of the // block kernel's warp v, in the same order), and the sums are added v = 1, 2, 3 onto v = 0 and warp_summed: the same // floats in the same order, so every output is the same bits, with no shared memory and no barrier, and RW row groups -// a wave. On an RX 9070 XT (E3 of rx9070xt-e123.md) the expert gate|up read 355 -> 520 GB/s and the down 270 -> 422. +// a wave. Inkling 2 Mini's grouped q|k|v|gate|rel|sink Q8_0 launch: 40.6 -> 39.0 us on an RX 9070 XT (the expert +// products' variants did not gain on this host and are not here). // // The term is written as d and sumi and accumulated explicitly: the traits' apply() gives the same bits, but its // codegen was up to 1.6x slower. C = 1: an fma of d and sumi onto the sum; C = 2: a rounded product and then the add // (the compiler contracts both to the one fma the block kernels use; they differ in scheduling). template struct VwSplit; -template <> struct VwSplit> { - using F = SmallTraits; - __device__ static void go(const F::W& r, const Q81Block* __restrict__ x, int iqs, float& d, int& sumi) { - const int* q8 = reinterpret_cast(x->qs) + iqs; - sumi = 0; -#pragma unroll - for (int i = 0; i < 2; ++i) { - sumi = STRATA_DP4A(r.v[i].x, q8[i], sumi); - sumi = STRATA_DP4A(r.v[i].y, q8[i + 4], sumi); - } - d = r.dw * __low2float(x->ds); - } -}; template <> struct VwSplit> { using F = SmallTraits; __device__ static void go(const F::W& r, const Q81Block* __restrict__ x, int iqs, float& d, int& sumi) { @@ -2127,65 +2115,6 @@ __device__ __forceinline__ float vw_term(float t, const typename F::Block* w, co else return __fadd_rn(t, __fmul_rn(d, float(sumi))); } -// native_mmvq_slots_kernel's launch of several matrices: blockIdx.y the matrix; a wave takes RW groups of ROWS rows, -// WPB waves a block. -template -__launch_bounds__(WPB * WARP) __global__ void native_mmvq_slots_vw_kernel(S slots, int n_in, int n_out) { - const SlotPtrs at = slot_ptrs(slots, int(blockIdx.y)); - const auto* w = static_cast(at.w); - const auto* x = static_cast(at.x); - float* y = at.y; - const int lane = int(threadIdx.x); - const int g0 = (int(blockIdx.x) * WPB + int(threadIdx.y)) * RW; // first row group of this wave - if (g0 * ROWS >= n_out) return; - const int bpr = n_in / F::DIV; - float t[RW][WARPS][ROWS]; -#pragma unroll - for (int r = 0; r < RW; ++r) -#pragma unroll - for (int v = 0; v < WARPS; ++v) -#pragma unroll - for (int i = 0; i < ROWS; ++i) t[r][v][i] = 0.0f; - for (int base = 0; base < bpr; base += F::BPI) { -#pragma unroll - for (int v = 0; v < WARPS; ++v) { - const int tid = WARP * v + lane; - const int kbx = tid / F::T + base; - if (kbx >= bpr) continue; - const int kby = kbx * F::KBY; - const int kqs = F::kqs(tid); -#pragma unroll - for (int r = 0; r < RW; ++r) -#pragma unroll - for (int i = 0; i < ROWS; ++i) { - const int row = (g0 + r) * ROWS + i; - if (row < n_out) { - const std::size_t block = std::size_t(row) * bpr + kbx; - t[r][v][i] = vw_term(t[r][v][i], w + block, x + kby, kqs); - } - } - } - } -#pragma unroll - for (int r = 0; r < RW; ++r) -#pragma unroll - for (int i = 0; i < ROWS; ++i) { - float s = t[r][0][i]; -#pragma unroll - for (int v = 1; v < WARPS; ++v) s += t[r][v][i]; - s = warp_sum(s); - const int row = (g0 + r) * ROWS + i; - if (lane == i && row < n_out) y[row] = s; - } -} - -template -void launch_slots_vw(const S& slots, int count, int n_in, int n_out, cudaStream_t s) { - const int groups = (n_out + ROWS - 1) / ROWS; - const dim3 blocks(unsigned((groups + RW * WPB - 1) / (RW * WPB)), unsigned(count)); - native_mmvq_slots_vw_kernel<<>>(slots, n_in, n_out); -} - // native_small_mmvq_many_kernel of up to 8 matrices: a wave takes RW rows template __launch_bounds__(WPB * WARP) __global__ void native_q8_0_many_vw_kernel(MmvqMany a, int n_in, int total) { @@ -2252,27 +2181,10 @@ bool vwarp_device_on() { return g_vwarp_force == 1 || !env_off; } -// Inkling 2 Mini's expert shapes only (n_in 2304 gate|up, 1152 down; the two formats E3 measured): the variant per shape. -template -bool launch_slots_vwarp(const S& slots, int count, int n_in, int n_out, cudaStream_t s) { - using IQ4 = SmallTraits; - using Q8 = SmallTraits; - if constexpr (std::is_same_v) { - if (n_in == 2304) { launch_slots_vw(slots, count, n_in, n_out, s); return true; } - if (n_in == 1152) { launch_slots_vw(slots, count, n_in, n_out, s); return true; } - } else if constexpr (std::is_same_v) { - if (n_in == 2304) { launch_slots_vw(slots, count, n_in, n_out, s); return true; } - if (n_in == 1152) { launch_slots_vw(slots, count, n_in, n_out, s); return true; } - } - return false; -} #endif template void launch_slots(const S& slots, int count, int n_in, int n_out, cudaStream_t s) { -#if defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) - if (vwarp_device_on() && n_in % 32 == 0 && launch_slots_vwarp(slots, count, n_in, n_out, s)) return; -#endif const dim3 threads(WARP, WARPS); if (native_mmvq_slots_wide() && n_in / F::DIV * F::T <= WARP && n_in % F::DIV == 0) { constexpr int ROWS = 2; diff --git a/engine/src/poly/gemv_vwarp_test.cu b/engine/src/poly/gemv_vwarp_test.cu index fd75988cc1..b81418bf8b 100644 --- a/engine/src/poly/gemv_vwarp_test.cu +++ b/engine/src/poly/gemv_vwarp_test.cu @@ -1,7 +1,6 @@ // src/poly/gemv_vwarp_test.cu - the HIP gfx11 / gfx12 virtual-warp decode GEMVs (kernels::native_gemv_vwarp_force) against the -// block kernels they replace, byte for byte: Inkling 2 Mini's expert products through native_mmvq_slots() and -// native_mmvq_ids() (IQ4_NL and Q8_0, n_in 2304 and 1152, 1 to 32 experts), the grouped dense Q8_0 launch -// native_mmvq_many(), and the loop controllers' down product ink_ctrl_down() (F32 and BF16). Every output of the new +// block kernels they replace, byte for byte: the grouped dense Q8_0 launch native_mmvq_many() and the loop controllers' +// down product ink_ctrl_down() (F32 and BF16). Every output of the new // kernels must be the same bytes as the old kernels' (outputs start as 0xff bytes, so an unwritten value differs). // // usage: poly_gemv_vwarp_test (exit 0: every byte the same; 77: not a gfx11 / gfx12 card, nothing to compare) @@ -102,71 +101,6 @@ void verdict(const char* what, bool same) { int bsize_of(int type) { return type == 8 ? 34 : 18; } -// ---- expert slots and ids ---- -void test_slots(int type, int n_in, int n_out, int count) { - char what[160]; - std::snprintf(what, sizeof what, "slots type %2d n_in %4d n_out %4d experts %2d", type, n_in, n_out, count); - std::vector w; - std::vector x; - kernels::MmvqSlots s0{}, s1{}; - std::vector y0, y1; - for (int j = 0; j < count; ++j) { - w.push_back(upload(rand_blocks(size_t(n_out) * (n_in / 32), bsize_of(type)))); - x.push_back(upload(rand_q81(n_in))); - y0.push_back(fill_ff(n_out)), y1.push_back(fill_ff(n_out)); - s0.w[j] = s1.w[j] = w[j], s0.x[j] = s1.x[j] = x[j], s0.y[j] = y0[j], s1.y[j] = y1[j]; - } - kernels::native_gemv_vwarp_force(0); - const bool ok0 = kernels::native_mmvq_slots(type, s0, count, n_in, n_out, st); - kernels::native_gemv_vwarp_force(1); - const bool ok1 = kernels::native_mmvq_slots(type, s1, count, n_in, n_out, st); - bool same = ok0 && ok1; - for (int j = 0; j < count; ++j) same = same && grab(y0[j], size_t(n_out) * 4) == grab(y1[j], size_t(n_out) * 4); - verdict(what, same); - for (int j = 0; j < count; ++j) cudaFree(w[j]), cudaFree(x[j]), cudaFree(y0[j]), cudaFree(y1[j]); -} - -void test_ids(int type, int n_in, int n_out, int count) { - char what[160]; - std::snprintf(what, sizeof what, "ids type %2d n_in %4d n_out %4d experts %2d", type, n_in, n_out, count); - const int nexp = 12; // the table's experts; the ids pick count of them (repeats allowed) - const size_t off = 64; // a byte offset into every expert's allocation - const size_t wbytes = size_t(n_out) * (n_in / 32) * bsize_of(type); - std::vector w; - std::vector table; - for (int e = 0; e < nexp; ++e) { - auto b = rand_blocks((wbytes + off) / bsize_of(type) + 1, bsize_of(type)); - w.push_back(upload(b)); - table.push_back(w.back()); - } - std::vector ids(count); - for (int j = 0; j < count; ++j) ids[j] = int32_t((j * 5 + 3) % nexp); - const size_t xs = size_t(n_in / 32) * 36; - std::vector xh; - for (int j = 0; j < count; ++j) { - auto q = rand_q81(n_in); - xh.insert(xh.end(), q.begin(), q.end()); - } - void** dtable = nullptr; - ck(cudaMalloc((void**) &dtable, nexp * sizeof(void*)), "malloc"); - ck(cudaMemcpy(dtable, table.data(), nexp * sizeof(void*), cudaMemcpyHostToDevice), "up"); - int32_t* dids = upload(ids); - uint8_t* dx = upload(xh); - float* y0 = fill_ff(size_t(count) * n_out); - float* y1 = fill_ff(size_t(count) * n_out); - kernels::MmvqIds a{dtable, dids, off, dx, xs, 1, y0, size_t(n_out)}; - kernels::MmvqIds b = a; - b.y = y1; - kernels::native_gemv_vwarp_force(0); - const bool ok0 = kernels::native_mmvq_ids(type, a, count, n_in, n_out, st); - kernels::native_gemv_vwarp_force(1); - const bool ok1 = kernels::native_mmvq_ids(type, b, count, n_in, n_out, st); - const size_t n = size_t(count) * n_out * 4; - verdict(what, ok0 && ok1 && grab(y0, n) == grab(y1, n)); - for (auto* p : w) cudaFree(p); - cudaFree(dtable), cudaFree(dids), cudaFree(dx), cudaFree(y0), cudaFree(y1); -} - // ---- grouped dense ---- void test_many(int type, int n_in, const std::vector& n_outs) { char what[200]; @@ -260,28 +194,6 @@ template double time_it(L&& launch, int nsets, int reps) { std::sort(r.begin(), r.end()); return r[2]; } -void bench_slots(int type, int n_in, int n_out, int reps) { - const int count = 8, pool = type == 8 ? 64 : 128; - std::vector w; - for (int i = 0; i < pool; ++i) w.push_back(upload(rand_blocks(size_t(n_out) * (n_in / 32), bsize_of(type)))); - std::vector x; - std::vector y; - for (int j = 0; j < count; ++j) x.push_back(upload(rand_q81(n_in))), y.push_back(fill_ff(n_out)); - const double bytes = double(count) * n_out * (n_in / 32) * bsize_of(type); - auto run = [&](int mode) { - kernels::native_gemv_vwarp_force(mode); - return time_it([&](int set) { - kernels::MmvqSlots sl{}; - for (int j = 0; j < count; ++j) sl.w[j] = w[(size_t(set) * count + j) % w.size()], sl.x[j] = x[j], sl.y[j] = y[j]; - kernels::native_mmvq_slots(type, sl, count, n_in, n_out, st); - }, pool / count, reps); - }; - const double o1 = run(0), n1 = run(1), o2 = run(0), n2 = run(1); - const double o = std::min(o1, o2), nn = std::min(n1, n2); - std::printf("BENCH slots type %2d %4d -> %4d x%d: old %6.1f us %6.1f GB/s | new %6.1f us %6.1f GB/s | %+.1f%%\n", type, n_in, - n_out, count, o, bytes / o / 1e3, nn, bytes / nn / 1e3, 100.0 * (o / nn - 1)); - for (auto* p : w) cudaFree(p); -} void bench_many(int reps) { const std::vector outs = {3072, 1024, 1024, 3072, 384, 24}; const int n_in = 2304, copies = 16; @@ -345,25 +257,10 @@ int main(int argc, char** argv) { } if (argc > 1 && std::strcmp(argv[1], "--bench") == 0) { const int reps = argc > 2 ? std::max(10, std::atoi(argv[2])) : 200; - for (int type : {20, 8}) bench_slots(type, 2304, 2304, reps), bench_slots(type, 1152, 2304, reps); bench_many(reps); bench_ctrl(reps); return 0; } - for (int type : {20, 8}) { - for (int count : {1, 5, 8, 32}) { - test_slots(type, 2304, 2304, count); // gate|up - test_slots(type, 1152, 2304, count); // down - } - test_slots(type, 2304, 1152, 8); // the up of a gate / up pair (UD-Q2's mixed layers) - test_slots(type, 2304, 2303, 3); // an odd row count: the tails of the row groups - test_slots(type, 1152, 2301, 3); - test_slots(type, 2304, 7, 2); - test_slots(type, 1152, 1, 2); - test_ids(type, 2304, 2304, 8); - test_ids(type, 1152, 2304, 8); - test_ids(type, 1152, 2303, 5); - } test_many(8, 2304, {3072, 1024, 1024, 3072, 384, 24}); // q|k|v|gate|rel|sink test_many(8, 2304, {3072, 1024, 1024}); test_many(8, 2304, {2304}); From 61c6809c3a5bd43e33be6380b2aaf1299a32cc08 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:49:21 +0900 Subject: [PATCH 6/7] hip: Q8_0 virtual-warp kernel with the explicit fma only, drop dead params; add_test, 8-matrix case, keep pending errors --- engine/cmake/strata_poly.cmake | 2 + engine/include/strata/kernels/native_mmvq.hpp | 13 +-- engine/src/kernels/cuda/native_mmvq.cu | 89 ++++++------------- engine/src/poly/gemv_vwarp_test.cu | 2 + 4 files changed, 40 insertions(+), 66 deletions(-) diff --git a/engine/cmake/strata_poly.cmake b/engine/cmake/strata_poly.cmake index 6e38a822b9..f22c2ea4de 100644 --- a/engine/cmake/strata_poly.cmake +++ b/engine/cmake/strata_poly.cmake @@ -277,6 +277,8 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA target_include_directories(poly_gemv_vwarp_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories}) target_link_libraries(poly_gemv_vwarp_test PRIVATE strata_kernels ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target}) + add_test(NAME poly_gemv_vwarp_test COMMAND poly_gemv_vwarp_test) + set_tests_properties(poly_gemv_vwarp_test PROPERTIES SKIP_RETURN_CODE 77) # a prompt batch's products with few outputs against the same sums on the host; needs a card, run by hand add_executable(poly_gemm_small_test src/poly/gemm_small_test.cu src/poly/kernels.cu) target_include_directories(poly_gemm_small_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include diff --git a/engine/include/strata/kernels/native_mmvq.hpp b/engine/include/strata/kernels/native_mmvq.hpp index 1a6190e527..39b2e33507 100644 --- a/engine/include/strata/kernels/native_mmvq.hpp +++ b/engine/include/strata/kernels/native_mmvq.hpp @@ -39,12 +39,13 @@ bool native_mmvq_multi_exact(); void native_mmvq_set_b6_rows(int mode); int native_mmvq_b6_rows(); -/// HIP gfx11 / gfx12 (RDNA3, RDNA4) only: decode GEMVs written as "virtual warps" (a wave does the block kernel's four -/// warps in turn, each into its own sum, added in the block's warp order; no shared memory, no barrier) for Inkling 2 -/// Mini's grouped dense Q8_0 launch (native_mmvq_many, n_in 2304) and the loop controllers' down product -/// (ink_ctrl_down). Every output is bitwise the block kernels' (gemv_vwarp_test). True where the current device takes -/// them: a HIP build, a gfx11xx / gfx12xx card, STRATA_GEMV_VWARP not "0". Always false on CUDA, on other HIP cards and -/// on the gfx906 build. +/// HIP gfx11 / gfx12 (RDNA3, RDNA4) only: two Inkling 2 Mini decode kernels rewritten for the same bits, faster. The +/// grouped dense Q8_0 launch (native_mmvq_many, n_in 2304) runs "virtual warps": a wave does the block kernel's four +/// warps in turn, each into its own sum, added in the block's warp order (no shared memory, no barrier). The loop +/// controllers' down product (ink_ctrl_down, even rank) computes two outputs a block (it keeps its shared memory and +/// barriers). Every output is bitwise the old kernels' (poly_gemv_vwarp_test). True where the current device takes +/// them: a HIP build (not the gfx906 one), a gfx11xx / gfx12xx card, STRATA_GEMV_VWARP not "0". Always false on CUDA and +/// on other HIP cards. bool native_gemv_vwarp_active(); /// Tests: -1 (default) the rule above, 0 off, 1 on where the device and the build allow it. void native_gemv_vwarp_force(int mode); diff --git a/engine/src/kernels/cuda/native_mmvq.cu b/engine/src/kernels/cuda/native_mmvq.cu index 23edad039b..4874b448a3 100644 --- a/engine/src/kernels/cuda/native_mmvq.cu +++ b/engine/src/kernels/cuda/native_mmvq.cu @@ -2090,77 +2090,47 @@ __global__ void native_mmvq_slots_warp_kernel(S slots, int n_in, int n_out) { // a wave. Inkling 2 Mini's grouped q|k|v|gate|rel|sink Q8_0 launch: 40.6 -> 39.0 us on an RX 9070 XT (the expert // products' variants did not gain on this host and are not here). // -// The term is written as d and sumi and accumulated explicitly: the traits' apply() gives the same bits, but its -// codegen was up to 1.6x slower. C = 1: an fma of d and sumi onto the sum; C = 2: a rounded product and then the add -// (the compiler contracts both to the one fma the block kernels use; they differ in scheduling). -template struct VwSplit; -template <> struct VwSplit> { - using F = SmallTraits; - __device__ static void go(const F::W& r, const Q81Block* __restrict__ x, int iqs, float& d, int& sumi) { - sumi = 0; +// The term is written as d and sumi and accumulated with an explicit fma onto the sum: the traits' apply() gives the same +// bits, but its codegen was up to 1.6x slower. The block kernels' `tmp += apply()` is the same one fma (d = d0 * d1 +// first, then fma(d, sumi, tmp)), which the byte compare in gemv_vwarp_test checks. +__device__ __forceinline__ float vw_q8_0_term(float t, const Q80Block* __restrict__ w, const Q81Block* __restrict__ x, int kqs) { + const auto r = SmallTraits::load(w, kqs); + int sumi = 0; #pragma unroll - for (int i = 0; i < 2; ++i) { - const int u = reinterpret_cast(x->qs)[iqs + i]; - sumi = STRATA_DP4A(r.v[i], u, sumi); - } - d = r.d0 * __low2float(x->ds); - } -}; -template -__device__ __forceinline__ float vw_term(float t, const typename F::Block* w, const Q81Block* x, int kqs) { - float d; - int sumi; - VwSplit::go(F::load(w, kqs), x, kqs, d, sumi); - if constexpr (C == 1) return __fmaf_rn(d, float(sumi), t); - else return __fadd_rn(t, __fmul_rn(d, float(sumi))); -} - -// native_small_mmvq_many_kernel of up to 8 matrices: a wave takes RW rows -template -__launch_bounds__(WPB * WARP) __global__ void native_q8_0_many_vw_kernel(MmvqMany a, int n_in, int total) { + for (int i = 0; i < 2; ++i) sumi = STRATA_DP4A(r.v[i], reinterpret_cast(x->qs)[kqs + i], sumi); + return __fmaf_rn(r.d0 * __low2float(x->ds), float(sumi), t); +} + +// native_small_mmvq_many_kernel of up to 8 matrices: a wave (WPB = 4 a block) takes one row +__launch_bounds__(4 * WARP) __global__ void native_q8_0_many_vw_kernel(MmvqMany a, int n_in, int total) { using F = SmallTraits; const int lane = int(threadIdx.x); - const int r0 = (int(blockIdx.x) * WPB + int(threadIdx.y)) * RW; - if (r0 >= total) return; + const int row = int(blockIdx.x) * 4 + int(threadIdx.y); + if (row >= total) return; + int m = 0; + while (row >= a.first[m + 1]) ++m; + const int mrow = row - a.first[m]; const int bpr = n_in / 32; - float t[RW][WARPS]; - const Q80Block* wr[RW]; - const Q81Block* xr[RW]; - int mrow[RW], mm[RW]; -#pragma unroll - for (int r = 0; r < RW; ++r) { - const int g = min(r0 + r, total - 1); - int m = 0; - while (g >= a.first[m + 1]) ++m; - mm[r] = m; - mrow[r] = g - a.first[m]; - wr[r] = static_cast(a.w[m]) + std::size_t(mrow[r]) * bpr; - xr[r] = a.x[m]; -#pragma unroll - for (int v = 0; v < WARPS; ++v) t[r][v] = 0.0f; - } + const Q80Block* wr = static_cast(a.w[m]) + std::size_t(mrow) * bpr; + const Q81Block* xr = a.x[m]; + float t[WARPS] = {}; for (int base = 0; base < bpr; base += F::BPI) { #pragma unroll for (int v = 0; v < WARPS; ++v) { const int tid = WARP * v + lane; const int kbx = tid / F::T + base; if (kbx >= bpr) continue; - const int kqs = F::kqs(tid); -#pragma unroll - for (int r = 0; r < RW; ++r) t[r][v] = vw_term(t[r][v], wr[r] + kbx, xr[r] + kbx, kqs); + t[v] = vw_q8_0_term(t[v], wr + kbx, xr + kbx, F::kqs(tid)); } } + float s = t[0]; #pragma unroll - for (int r = 0; r < RW; ++r) { - float s = t[r][0]; -#pragma unroll - for (int v = 1; v < WARPS; ++v) s += t[r][v]; - s = warp_sum(s); - if (lane == 0 && r0 + r < total) a.y[mm[r]][mrow[r]] = s; - } + for (int v = 1; v < WARPS; ++v) s += t[v]; + s = warp_sum(s); + if (lane == 0) a.y[m][mrow] = s; } -// Whether the current device takes the virtual-warp kernels: gfx11xx / gfx12xx, and not STRATA_GEMV_VWARP=0. +// Whether the current device takes the virtual-warp kernels (this block is HIP-only: __HIPCC__ and not STRATA_HIP_GFX906): gfx11xx / gfx12xx, and not STRATA_GEMV_VWARP=0. int g_vwarp_force = -1; bool vwarp_device_on() { static signed char arch_ok[16]; // per device: 0 unknown, 1 not gfx11xx / gfx12xx, 2 it is @@ -2169,9 +2139,8 @@ bool vwarp_device_on() { if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 16) return false; if (arch_ok[dev] == 0) { cudaDeviceProp p; - const bool arch = cudaGetDeviceProperties(&p, dev) == cudaSuccess && strata::kernels::gfx_arch_is_rdna34(p.gcnArchName); - (void) cudaGetLastError(); - arch_ok[dev] = arch ? 2 : 1; + if (cudaGetDeviceProperties(&p, dev) != cudaSuccess) return false; // not cached; the error stays for the caller + arch_ok[dev] = strata::kernels::gfx_arch_is_rdna34(p.gcnArchName) ? 2 : 1; } if (arch_ok[dev] != 2) return false; static const bool env_off = [] { @@ -4479,7 +4448,7 @@ void native_mmvq_many(int ggml_type, const NativeMmvqMany& m, int n_in, void* st #if defined(__HIPCC__) && !defined(STRATA_HIP_GFX906) if (ggml_type == 8 && n_in == 2304 && vwarp_device_on()) { // Inkling 2 Mini's q|k|v|gate|rel|sink launch const int total = a.first[m.count]; - native_q8_0_many_vw_kernel<1, 4, 2><<>>(a, n_in, total); + native_q8_0_many_vw_kernel<<>>(a, n_in, total); launch_check(); return; } diff --git a/engine/src/poly/gemv_vwarp_test.cu b/engine/src/poly/gemv_vwarp_test.cu index b81418bf8b..68fbde3948 100644 --- a/engine/src/poly/gemv_vwarp_test.cu +++ b/engine/src/poly/gemv_vwarp_test.cu @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -262,6 +263,7 @@ int main(int argc, char** argv) { return 0; } test_many(8, 2304, {3072, 1024, 1024, 3072, 384, 24}); // q|k|v|gate|rel|sink + test_many(8, 2304, {3072, 1024, 1024, 3072, 384, 24, 32, 32}); // with k_dyn / v_dyn (taps * n_kv rows): 8 matrices test_many(8, 2304, {3072, 1024, 1024}); test_many(8, 2304, {2304}); test_many(8, 2304, {7, 1001, 3, 2}); // odd totals and row counts From 909ef96d8c88a88aee547588576803ef683493de Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:45:22 +0900 Subject: [PATCH 7/7] settings register: STRATA_GEMV_VWARP Registered as a rollback of the virtual-warp GEMV kernels. Co-Authored-By: Claude Sonnet 5.5 --- terminal/data/settings-register.json | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 1b115a1705..f624c9c469 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -11470,6 +11470,39 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_GEMV_VWARP", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/kernels/cuda/native_mmvq.cu", + "line": 2147, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "a1c75b5123", + "first_date": "2026-11-24", + "since": null, + "last_commit": "61c6809c3a", + "last_date": "2026-11-24", + "status": "rollback", + "owner": "", + "note": "HIP on gfx11xx and gfx12xx (not the gfx906 build): 0 goes back to the earlier kernels for the grouped Q8_0 decode GEMV and ink_ctrl_down instead of the virtual-warp ones (same bits).", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_GFX1030_EXP8", "backends": [