Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions engine/cmake/strata_poly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions engine/include/strata/kernels/gfx_arch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions engine/include/strata/kernels/native_mmvq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
97 changes: 97 additions & 0 deletions engine/src/kernels/cuda/native_mmvq.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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<Q80Block, 8>::load(w, kqs);
int sumi = 0;
#pragma unroll
for (int i = 0; i < 2; ++i) sumi = STRATA_DP4A(r.v[i], reinterpret_cast<const int*>(x->qs)[kqs + i], sumi);
return __fmaf_rn(r.d0 * __low2float(x->ds), float(sumi), t);
}

// native_small_mmvq_many_kernel<Q80Block, 8> 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<Q80Block, 8>;
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<const Q80Block*>(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<typename F, typename S>
void launch_slots(const S& slots, int count, int n_in, int n_out, cudaStream_t s) {
const dim3 threads(WARP, WARPS);
Expand Down Expand Up @@ -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<cudaStream_t>(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<<<unsigned((total + 3) / 4), dim3(WARP, 4), 0, s>>>(a, n_in, total);
launch_check();
return;
}
#endif
switch (ggml_type) {
case 2: native_small_mmvq_many_kernel<Q40Block, 4><<<grid, threads, 0, s>>>(a, n_in); break;
case 6: native_small_mmvq_many_kernel<Q50Block, 4><<<grid, threads, 0, s>>>(a, n_in); break;
Expand Down Expand Up @@ -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");
Expand Down
Loading
Loading