diff --git a/engine/cmake/strata_poly.cmake b/engine/cmake/strata_poly.cmake index 11a4ab043..f22c2ea4d 100644 --- a/engine/cmake/strata_poly.cmake +++ b/engine/cmake/strata_poly.cmake @@ -272,6 +272,13 @@ 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}) + 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/gfx_arch.hpp b/engine/include/strata/kernels/gfx_arch.hpp index 847d7a792..e965cbf88 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 d6d733b2d..39b2e3350 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: 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); + // 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 bfebd1ca8..4874b448a 100644 --- a/engine/src/kernels/cuda/native_mmvq.cu +++ b/engine/src/kernels/cuda/native_mmvq.cu @@ -2079,6 +2079,79 @@ __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 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. 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 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) 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 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; + 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; + t[v] = vw_q8_0_term(t[v], wr + kbx, xr + kbx, F::kqs(tid)); + } + } + float s = t[0]; +#pragma unroll + 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 (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 + 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; + 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 = [] { + const char* v = std::getenv("STRATA_GEMV_VWARP"); + return v != nullptr && v[0] == '0'; + }(); + return g_vwarp_force == 1 || !env_off; +} + +#endif + template void launch_slots(const S& slots, int count, int n_in, int n_out, cudaStream_t s) { const dim3 threads(WARP, WARPS); @@ -4372,6 +4445,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<<>>(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 +4743,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 000000000..68fbde394 --- /dev/null +++ b/engine/src/poly/gemv_vwarp_test.cu @@ -0,0 +1,283 @@ +// 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: 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 +#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"); + 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) { + 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; } + +// ---- 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]); +} + +// ---- --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_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 argc, char** argv) { + 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; + } + if (argc > 1 && std::strcmp(argv[1], "--bench") == 0) { + const int reps = argc > 2 ? std::max(10, std::atoi(argv[2])) : 200; + bench_many(reps); + bench_ctrl(reps); + 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 + 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 c5d726639..4c91d5632 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); diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 1b115a170..f624c9c46 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": [