okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9d642d1f7
main
1#include "strata/core/bw_dense_defaults.hpp" 2#include "strata/kernels/bf16_gemv.hpp" 3#include "strata/kernels/window_limits.hpp" 4#include "s26_tsum.cuh" 5#include "strata/kernels/bf16_bits.hpp" 6#include "strata/kernels/pdl.hpp" 7 8#include <cuda_runtime.h> 9#include <cstdio> 10#include <cstdlib> 11#include <limits> 12#include <stdexcept> 13#include <string> 14 15namespace strata::kernels { 16namespace { 17 18// The FP32-activation MMVF implementation below is adapted from llama.cpp 19// 3cf03257f219afbe7334045ff7c6a06ac68c627d, ggml/src/ggml-cuda/{mmvf.cu,common.cuh}. 20// Scope: ordinary contiguous BF16 matrix, one FP32 activation vector, no fusion/ids/channels. 21// 22// MIT License 23// Copyright (c) 2023-2026 The ggml authors 24// 25// Permission is hereby granted, free of charge, to any person obtaining a copy 26// of this software and associated documentation files (the "Software"), to deal 27// in the Software without restriction, including without limitation the rights 28// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 29// copies of the Software, and to permit persons to whom the Software is 30// furnished to do so, subject to the following conditions: 31// 32// The above copyright notice and this permission notice shall be included in all 33// copies or substantial portions of the Software. 34// 35// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 36// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 37// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 38// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 39// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 40// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 41// SOFTWARE. 42__device__ __forceinline__ float mmvf_warp_sum(float value) { 43#pragma unroll 44 for (int offset = 16; offset > 0; offset >>= 1) 45 value += __shfl_xor_sync(0xffffffffu, value, offset, 32); 46 return value; 47} 48 49// STRATA_MMVF_CHECK=1 (diagnostic): the constant-trip fast path also runs the plain loop with volatile loads (so the loads happen again, later) and 50// compares each thread's accumulator bit for bit: g_mmvf_chk[0] checks, [1] differing accumulators, [2..4] block / thread / n_in of the first one. 51// A difference means the activations or weights changed between the early and the late loads (no arithmetic differs between the two loops). 52__device__ unsigned long long g_mmvf_chk[8]; 53__device__ __forceinline__ void mmvf_chk_note(float a, float b, int blk, int t, int n_in) { 54 atomicAdd(&g_mmvf_chk[0], 1ull); 55 if (__float_as_uint(a) != __float_as_uint(b)) { 56 if (atomicAdd(&g_mmvf_chk[1], 1ull) == 0ull) { g_mmvf_chk[2] = (unsigned long long) blk; g_mmvf_chk[3] = (unsigned long long) t; g_mmvf_chk[4] = (unsigned long long) n_in; 57 g_mmvf_chk[5] = __float_as_uint(a); g_mmvf_chk[6] = __float_as_uint(b); } 58 } 59} 60 61template <int BLOCK_SIZE> 62__global__ void bf16_f32_mmvf_kernel(const float* __restrict__ x, const uint16_t* __restrict__ w, 63 float* __restrict__ y, int n_in, int fast) { 64 const int t = threadIdx.x; 65 const uint16_t* row = w + (size_t) blockIdx.x * n_in; 66 const uint32_t* weights2 = reinterpret_cast<const uint32_t*>(row); 67 const float2* inputs2 = reinterpret_cast<const float2*>(x); 68 __shared__ float partials[32]; 69 if (t < 32) partials[t] = 0.0f; 70 __syncthreads(); 71 float acc = 0.0f; 72 if (BLOCK_SIZE == 256 && fast != 0 && n_in == 2560) { 73 // STRATA_SMALL_UNR bit 0: the router-shaped single-column read, five passes of one weight pair each (all in range), constant 74 // trip count so the weight and activation loads of the five passes are in flight together. The same FMAs in the same order. 75 uint32_t wv[5]; 76 float2 xv[5]; 77#pragma unroll 78 for (int qq = 0; qq < 5; ++qq) { wv[qq] = weights2[t + qq * BLOCK_SIZE]; xv[qq] = inputs2[t + qq * BLOCK_SIZE]; } 79#pragma unroll 80 for (int qq = 0; qq < 5; ++qq) { 81 acc = __fmaf_rn(f32_from_bf16((uint16_t) wv[qq]), xv[qq].x, acc); 82 acc = __fmaf_rn(f32_from_bf16((uint16_t) (wv[qq] >> 16)), xv[qq].y, acc); 83 } 84 if (fast == 2) { 85 float acc2 = 0.0f; 86 for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { 87 const uint32_t weight = *(const volatile uint32_t*) (weights2 + pair); 88 const volatile float* ip = reinterpret_cast<const volatile float*>(inputs2 + pair); 89 const float2 input = make_float2(ip[0], ip[1]); 90 acc2 = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc2); 91 acc2 = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc2); 92 } 93 mmvf_chk_note(acc, acc2, (int) blockIdx.x, t, n_in); 94 } 95 } else 96 for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { 97 const uint32_t weight = weights2[pair]; 98 const float2 input = inputs2[pair]; 99 // Match the two ordered multiply-adds in ggml_cuda_mad, not a pair sum followed by one add. 100 acc = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc); 101 acc = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc); 102 } 103 acc = mmvf_warp_sum(acc); 104 if constexpr (BLOCK_SIZE > 32) { 105 // All lanes have the same reduced value; one store avoids a same-value shared-memory race. 106 if ((t & 31) == 0) partials[t / 32] = acc; 107 __syncthreads(); 108 if (t < 32) acc = mmvf_warp_sum(partials[t]); 109 } 110 if (t == 0) y[blockIdx.x] = acc; 111} 112 113// the same kernel for up to 8 activation rows - the weight row is read ONCE and every 114// token keeps its own accumulator with exactly the single-row kernel's order (pairs, two ordered FMAs, the same warp 115// and block reductions), so each output is bit-identical to a bf16_f32_mmvf_kernel launch of its own. 116template <int BLOCK_SIZE, int NT, bool EXACT_T = true> 117__global__ void bf16_f32_mmvf_multi_kernel(const float* x_, int64_t ldx, const uint16_t* __restrict__ w, 118 float* y_, int64_t ldy, int n_in, int n_tok, int fast) { 119 const float* STRATA_PDL_RESTRICT x = x_; // __restrict__ below sm_70 only (pdl.hpp, #1469) 120 float* STRATA_PDL_RESTRICT y = y_; 121 const int t = threadIdx.x; 122 const uint16_t* row = w + (size_t) blockIdx.x * n_in; 123 const uint32_t* weights2 = reinterpret_cast<const uint32_t*>(row); 124 __shared__ float partials[NT][32]; 125 if (t < 32) { 126#pragma unroll 127 for (int k = 0; k < NT; ++k) partials[k][t] = 0.0f; 128 } 129 __syncthreads(); 130 float acc[NT]; 131#pragma unroll 132 for (int k = 0; k < NT; ++k) acc[k] = 0.0f; 133 // PDL (pdl.hpp): this thread's first PRE weight pairs are loaded before waiting for the activations 134 constexpr int PRE = kPdlPrefetch ? 8 : 0; 135 uint32_t wpre[PRE > 0 ? PRE : 1]; 136#pragma unroll 137 for (int q = 0; q < PRE; ++q) { 138 const int pair = t + q * BLOCK_SIZE; 139 wpre[q] = pair < n_in / 2 ? __ldg(weights2 + pair) : 0u; 140 } 141 pdl_wait(); 142 if (BLOCK_SIZE == 256 && fast != 0 && n_in == 2560) { 143 // BW-DENSE (STRATA_SMALL_UNR bit 0): the router-shaped read (2560 inputs, 256 threads: five passes of one weight pair each, all 144 // in range) with constant trip counts, so the activation loads of every pass are in flight together instead of one memory 145 // latency a pass. The same FMAs in the same order (pass ascending, w0 then w1): bitwise the loop below. 146 float2 xin[NT][5]; 147#pragma unroll 148 for (int qq = 0; qq < 5; ++qq) 149#pragma unroll 150 for (int k = 0; k < NT; ++k) 151 if (EXACT_T || k < n_tok) xin[k][qq] = __ldg(reinterpret_cast<const float2*>(x + (size_t) k * ldx) + (t + qq * BLOCK_SIZE)); 152#pragma unroll 153 for (int qq = 0; qq < 5; ++qq) { 154 uint32_t weight; 155 if constexpr (PRE >= 5) weight = wpre[qq]; 156 else weight = __ldg(weights2 + t + qq * BLOCK_SIZE); 157 const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); 158#pragma unroll 159 for (int k = 0; k < NT; ++k) { 160 if (EXACT_T || k < n_tok) { 161 acc[k] = __fmaf_rn(w0, xin[k][qq].x, acc[k]); 162 acc[k] = __fmaf_rn(w1, xin[k][qq].y, acc[k]); 163 } 164 } 165 } 166 if (fast == 2) { 167#pragma unroll 168 for (int k = 0; k < NT; ++k) { 169 if (EXACT_T || k < n_tok) { 170 float acc2 = 0.0f; 171 for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { 172 const uint32_t weight = *(const volatile uint32_t*) (weights2 + pair); 173 const volatile float* ip = reinterpret_cast<const volatile float*>(reinterpret_cast<const float2*>(x + (size_t) k * ldx) + pair); 174 const float2 input = make_float2(ip[0], ip[1]); 175 acc2 = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc2); 176 acc2 = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc2); 177 } 178 mmvf_chk_note(acc[k], acc2, (int) blockIdx.x, t, n_in); 179 } 180 } 181 } 182 } else { 183 int q = 0; 184 for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE, ++q) { 185 const uint32_t weight = q < PRE ? wpre[q] : __ldg(weights2 + pair); 186 const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); 187#pragma unroll 188 for (int k = 0; k < NT; ++k) { 189 if (EXACT_T || k < n_tok) { 190 const float2 input = __ldg(reinterpret_cast<const float2*>(x + (size_t) k * ldx) + pair); 191 acc[k] = __fmaf_rn(w0, input.x, acc[k]); 192 acc[k] = __fmaf_rn(w1, input.y, acc[k]); 193 } 194 } 195 } 196 } 197#pragma unroll 198 for (int k = 0; k < NT; ++k) 199 if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(acc[k]); 200 if constexpr (BLOCK_SIZE > 32) { 201 if ((t & 31) == 0) 202#pragma unroll 203 for (int k = 0; k < NT; ++k) 204 if (EXACT_T || k < n_tok) partials[k][t / 32] = acc[k]; 205 __syncthreads(); 206 if (t < 32) 207#pragma unroll 208 for (int k = 0; k < NT; ++k) 209 if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(partials[k][t]); 210 } 211 if (t == 0) 212#pragma unroll 213 for (int k = 0; k < NT; ++k) 214 if (EXACT_T || k < n_tok) y[(size_t) k * ldy + blockIdx.x] = acc[k]; 215} 216 217// S25 (STRATA_MMVF_ROWS=1): RPB output rows per block. Each block read its row's weights once but all T activation 218// rows again (n_out blocks x T x n_in floats through L2 - the part that grew with T); here a block reads the 219// activations once for RPB rows. Per output, thread t still walks pairs t, t + BLOCK_SIZE, ... with the same two 220// ordered FMAs and the same warp and block reductions: bit-identical to bf16_f32_mmvf_multi_kernel. 221// S26 STRATA_LFUSE=1 (AUX): one more block computes row 0 of w_aux into y_aux[k * ldy_aux] - the same per-output 222// code, so each aux output is bitwise what its own bf16_f32_mmvf_multi_kernel launch gave (the shared expert's gate) 223template <int BLOCK_SIZE, int NT, int RPB, bool TS = false, bool AUX = false> 224__global__ void bf16_f32_mmvf_rows_kernel(const float* __restrict__ x, int64_t ldx, const uint16_t* __restrict__ w, 225 float* __restrict__ y, int64_t ldy, int n_in, int n_out, int n_tok, 226 const uint16_t* __restrict__ w_aux = nullptr, float* __restrict__ y_aux = nullptr, 227 int64_t ldy_aux = 0) { 228 const int t = threadIdx.x; 229 int o0 = blockIdx.x * RPB; 230 if constexpr (AUX) { 231 if (blockIdx.x == gridDim.x - 1) { w = w_aux; y = y_aux; ldy = ldy_aux; n_out = 1; o0 = 0; } 232 } 233 __shared__ float partials[RPB][NT][32]; 234 if constexpr (BLOCK_SIZE > 32) { 235 if (t < 32) 236#pragma unroll 237 for (int r = 0; r < RPB; ++r) 238#pragma unroll 239 for (int k = 0; k < NT; ++k) partials[r][k][t] = 0.0f; 240 __syncthreads(); 241 } 242 float acc[RPB][NT]; 243#pragma unroll 244 for (int r = 0; r < RPB; ++r) 245#pragma unroll 246 for (int k = 0; k < NT; ++k) acc[r][k] = 0.0f; 247 for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { 248 float2 in[NT]; 249#pragma unroll 250 for (int k = 0; k < NT; ++k) 251 if (k < n_tok) in[k] = reinterpret_cast<const float2*>(x + (size_t) k * ldx)[pair]; 252#pragma unroll 253 for (int r = 0; r < RPB; ++r) { 254 if (o0 + r >= n_out) break; 255 const uint32_t weight = reinterpret_cast<const uint32_t*>(w + (size_t) (o0 + r) * n_in)[pair]; 256 const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); 257#pragma unroll 258 for (int k = 0; k < NT; ++k) { 259 if (k < n_tok) { 260 acc[r][k] = __fmaf_rn(w0, in[k].x, acc[r][k]); 261 acc[r][k] = __fmaf_rn(w1, in[k].y, acc[r][k]); 262 } 263 } 264 } 265 } 266 if constexpr (TS) { // S26 STRATA_TSUM=1: the RPB x NT warp sums (both stages) as transposed butterflies, bitwise the same 267 constexpr int V = RPB * NT, P = s26ts::pow2_ceil(V); 268 const int lane = t & 31, j = s26ts::tsum_token<P>(lane); 269 const bool own = lane == s26ts::tsum_lane<P>(j) && j < V; 270 float v[P]; 271#pragma unroll 272 for (int q = 0; q < P; ++q) v[q] = q < V ? acc[q / NT][q % NT] : 0.0f; 273 float sum = s26ts::tsum<P>(v, lane); 274 if constexpr (BLOCK_SIZE > 32) { 275 if (own) partials[j / NT][j % NT][t / 32] = sum; 276 __syncthreads(); 277 if (t >= 32) return; 278#pragma unroll 279 for (int q = 0; q < P; ++q) v[q] = q < V ? partials[q / NT][q % NT][t] : 0.0f; 280 sum = s26ts::tsum<P>(v, lane); 281 } 282 const int r = j / NT, k = j % NT; 283 if (own && o0 + r < n_out && k < n_tok) y[(size_t) k * ldy + o0 + r] = sum; 284 return; 285 } 286#pragma unroll 287 for (int r = 0; r < RPB; ++r) 288#pragma unroll 289 for (int k = 0; k < NT; ++k) acc[r][k] = mmvf_warp_sum(acc[r][k]); 290 if constexpr (BLOCK_SIZE > 32) { 291 if ((t & 31) == 0) 292#pragma unroll 293 for (int r = 0; r < RPB; ++r) 294#pragma unroll 295 for (int k = 0; k < NT; ++k) partials[r][k][t / 32] = acc[r][k]; 296 __syncthreads(); 297 if (t < 32) 298#pragma unroll 299 for (int r = 0; r < RPB; ++r) 300#pragma unroll 301 for (int k = 0; k < NT; ++k) acc[r][k] = mmvf_warp_sum(partials[r][k][t]); 302 } 303 if (t == 0) 304#pragma unroll 305 for (int r = 0; r < RPB; ++r) 306 if (o0 + r < n_out) 307#pragma unroll 308 for (int k = 0; k < NT; ++k) 309 if (k < n_tok) y[(size_t) k * ldy + o0 + r] = acc[r][k]; 310} 311 312template <int B> 313void launch_rows(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, int n_in, int n_out, int n_tok, 314 cudaStream_t st, const uint16_t* w_aux = nullptr, float* y_aux = nullptr, int64_t ldy_aux = 0) { 315 constexpr int RPB = 4; 316 const unsigned nb = (unsigned) ((n_out + RPB - 1) / RPB); 317 static const bool ts = [] { const char* v = std::getenv("STRATA_TSUM"); return v && v[0] == '1'; }(); 318 if (w_aux != nullptr) { // S26 STRATA_LFUSE: + one aux row block 319#define S26_AUX(TSV) \ 320 if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<B, 4, RPB, TSV, true><<<nb + 1, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok, w_aux, y_aux, ldy_aux); \ 321 else bf16_f32_mmvf_rows_kernel<B, 8, RPB, TSV, true><<<nb + 1, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok, w_aux, y_aux, ldy_aux); 322 if (ts) { S26_AUX(true) } else { S26_AUX(false) } 323#undef S26_AUX 324 return; 325 } 326 if (ts) { 327 if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<B, 4, RPB, true><<<nb, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); 328 else bf16_f32_mmvf_rows_kernel<B, 8, RPB, true><<<nb, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); 329 return; 330 } 331 if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<B, 4, RPB><<<nb, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); 332 else bf16_f32_mmvf_rows_kernel<B, 8, RPB><<<nb, B, 0, st>>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); 333} 334 335void mmvf_chk_report() { 336 unsigned long long h[8] = {}; 337 if (cudaMemcpyFromSymbol(h, g_mmvf_chk, sizeof(h)) != cudaSuccess) return; 338 std::fprintf(stderr, "strata mmvf check: %llu accumulators compared, %llu differ; first: block %llu thread %llu n_in %llu fast %08llx plain %08llx\n", h[0], h[1], h[2], h[3], h[4], h[5], h[6]); 339} 340// BW-DENSE STRATA_SMALL_UNR bit 0: see bf16_f32_mmvf_multi_kernel (bw_dense_defaults.hpp: on for a 12.0 card, else off; the variable wins) 341int mmvf_fast_bit() { 342 static int per_dev[64]; // 0 unknown, else bit + 1 343 const char* e = std::getenv("STRATA_SMALL_UNR"); 344 if (e != nullptr) return std::atoi(e) & 1; 345#if defined(__HIPCC__) 346 return 0; 347#else 348 int dev = 0; 349 if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return 0; } 350 if (!per_dev[dev]) { 351 int ma = 0, mi = 0; 352 const bool ok = cudaDeviceGetAttribute(&ma, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess && 353 cudaDeviceGetAttribute(&mi, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess; 354 if (!ok) cudaGetLastError(); 355 per_dev[dev] = 1 + (ok ? (strata::small_unr_default(ma, mi) & 1) : 0); 356 } 357 return per_dev[dev] - 1; 358#endif 359} 360// AMD-Q STRATA_MMVF_CHECK=1 (with the switch on): the kernel also computes the plain read and counts the accumulators that differ (2 = check mode) 361int mmvf_fast() { 362 const int b = mmvf_fast_bit(); 363 if (b == 0) return 0; 364 static const bool chk = [] { 365 const char* c = std::getenv("STRATA_MMVF_CHECK"); 366 const bool on = c != nullptr && c[0] == '1'; 367 if (on) std::atexit(mmvf_chk_report); 368 return on; 369 }(); 370 return chk ? 2 : b; 371} 372int mmvf_block_size(int64_t n_in) { 373 int best = 32; 374 int64_t best_iterations = (n_in + 63) / 64; 375 for (int candidate = 64; candidate <= 256; candidate += 32) { 376 const int64_t iterations = (n_in + 2 * candidate - 1) / (2 * candidate); 377 if (iterations < best_iterations) { 378 best_iterations = iterations; 379 best = candidate; 380 } 381 } 382 return best; 383} 384 385} // namespace 386 387bool bf16_gemv_fp32_mmvf_multi_aux(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, 388 int64_t n_in, int64_t n_out, int n_tok, const uint16_t* w_aux, float* y_aux, 389 int64_t ldy_aux, void* stream) { 390 static const bool rows = [] { const char* v = std::getenv("STRATA_MMVF_ROWS"); return v && v[0] == '1'; }(); 391 // exactly the conditions under which both calls would take the rows / multi kernels at the same block size 392 if (!rows || n_out < 64 || n_tok < 2 || n_tok > 8 || n_in <= 0 || (n_in & 1) != 0 || (ldx & 1) != 0 || x == nullptr || 393 w == nullptr || y == nullptr || w_aux == nullptr || y_aux == nullptr || (reinterpret_cast<uintptr_t>(x) & 7u) != 0) 394 return false; 395 const cudaStream_t st = (cudaStream_t) stream; 396 const int ni = (int) n_in, no = (int) n_out; 397 switch (mmvf_block_size(n_in)) { 398 case 32: launch_rows<32>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; 399 case 64: launch_rows<64>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; 400 case 96: launch_rows<96>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; 401 case 128: launch_rows<128>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; 402 case 160: launch_rows<160>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; 403 case 192: launch_rows<192>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; 404 case 224: launch_rows<224>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; 405 default: launch_rows<256>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; 406 } 407 const cudaError_t result = cudaGetLastError(); 408 if (result != cudaSuccess) 409 throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi_aux launch: ") + cudaGetErrorString(result)); 410 return true; 411} 412 413void bf16_gemv_fp32_mmvf_multi(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, 414 int64_t n_in, int64_t n_out, int n_tok, void* stream) { 415 if (n_tok == 1 && ldy >= n_out) { bf16_gemv_fp32_mmvf(x, w, y, n_in, n_out, stream); return; } 416 if (n_tok < 1 || n_tok > STRATA_VERIFY_MAX_T || n_in <= 0 || (n_in & 1) != 0 || n_out <= 0 || (ldx & 1) != 0 || 417 x == nullptr || w == nullptr || y == nullptr || (reinterpret_cast<uintptr_t>(x) & 7u) != 0) 418 throw std::invalid_argument("bf16_gemv_fp32_mmvf_multi: 1..8 rows (16 in a wide build), even n_in/ldx, aligned pointers"); 419 const cudaStream_t st = (cudaStream_t) stream; 420 static const bool rows = [] { const char* v = std::getenv("STRATA_MMVF_ROWS"); return v && v[0] == '1'; }(); 421 if (rows && n_out >= 64 && n_tok <= 8) { // (the opt-in rows kernels hold 8 columns: a wider window takes the switch below) 422 const int ni = (int) n_in, no = (int) n_out; 423 switch (mmvf_block_size(n_in)) { 424 case 32: launch_rows<32>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; 425 case 64: launch_rows<64>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; 426 case 96: launch_rows<96>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; 427 case 128: launch_rows<128>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; 428 case 160: launch_rows<160>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; 429 case 192: launch_rows<192>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; 430 case 224: launch_rows<224>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; 431 default: launch_rows<256>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; 432 } 433 const cudaError_t result = cudaGetLastError(); 434 if (result != cudaSuccess) 435 throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi launch: ") + cudaGetErrorString(result)); 436 return; 437 } 438 // 9..16 rows (wide builds only): one exact instantiation per row count, each row's dot independent of the others. 7 and 8 439 // rows stay with the default case below, the instantiation they always had. 440#if STRATA_VERIFY_MAX_T > 8 441#define STRATA_MMVF_W(N, T) case T: launch_pdl(bf16_f32_mmvf_multi_kernel<N, T, true>, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; 442#if STRATA_VERIFY_MAX_T > 12 443#define STRATA_MMVF_W13(N) STRATA_MMVF_W(N, 13) STRATA_MMVF_W(N, 14) STRATA_MMVF_W(N, 15) STRATA_MMVF_W(N, 16) 444#else 445#define STRATA_MMVF_W13(N) 446#endif 447#define STRATA_MMVF_WIDE(N) \ 448 STRATA_MMVF_W(N, 9) STRATA_MMVF_W(N, 10) STRATA_MMVF_W(N, 11) STRATA_MMVF_W(N, 12) \ 449 STRATA_MMVF_W13(N) 450#else 451#define STRATA_MMVF_WIDE(N) 452#endif 453#define STRATA_MMVF_M(N) case N: \ 454 switch (n_tok) { \ 455 STRATA_MMVF_WIDE(N) \ 456 case 1: launch_pdl(bf16_f32_mmvf_multi_kernel<N, 1, true>, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ 457 case 2: launch_pdl(bf16_f32_mmvf_multi_kernel<N, 2, true>, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ 458 case 3: launch_pdl(bf16_f32_mmvf_multi_kernel<N, 3, true>, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ 459 case 4: launch_pdl(bf16_f32_mmvf_multi_kernel<N, 4, true>, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ 460 case 5: launch_pdl(bf16_f32_mmvf_multi_kernel<N, 5, true>, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ 461 case 6: launch_pdl(bf16_f32_mmvf_multi_kernel<N, 6, true>, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ 462 default: launch_pdl(bf16_f32_mmvf_multi_kernel<N, 8, false>, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ 463 } break 464 switch (mmvf_block_size(n_in)) { 465 STRATA_MMVF_M(32); STRATA_MMVF_M(64); STRATA_MMVF_M(96); STRATA_MMVF_M(128); 466 STRATA_MMVF_M(160); STRATA_MMVF_M(192); STRATA_MMVF_M(224); STRATA_MMVF_M(256); 467 } 468#undef STRATA_MMVF_M 469#undef STRATA_MMVF_WIDE 470#ifdef STRATA_MMVF_W 471#undef STRATA_MMVF_W 472#endif 473 const cudaError_t result = cudaGetLastError(); 474 if (result != cudaSuccess) 475 throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi launch: ") + cudaGetErrorString(result)); 476} 477 478void bf16_gemv_fp32_mmvf(const float* x, const uint16_t* w, float* y, 479 int64_t n_in, int64_t n_out, void* stream) { 480 if (n_in <= 0 || (n_in & 1) != 0 || n_in > std::numeric_limits<int>::max() || 481 n_out <= 0 || n_out > std::numeric_limits<int>::max()) 482 throw std::invalid_argument("bf16_gemv_fp32_mmvf: require positive even n_in and positive n_out <= INT_MAX"); 483 if (x == nullptr || w == nullptr || y == nullptr || 484 (reinterpret_cast<uintptr_t>(x) & 7u) != 0 || 485 (reinterpret_cast<uintptr_t>(w) & 3u) != 0 || 486 (reinterpret_cast<uintptr_t>(y) & 3u) != 0) 487 throw std::invalid_argument("bf16_gemv_fp32_mmvf: null or misaligned pointer"); 488 const cudaStream_t st = (cudaStream_t) stream; 489#define STRATA_MMVF_CASE(N) case N: \ 490 bf16_f32_mmvf_kernel<N><<<(unsigned) n_out, N, 0, st>>>(x, w, y, (int) n_in, mmvf_fast()); break 491 switch (mmvf_block_size(n_in)) { 492 STRATA_MMVF_CASE(32); 493 STRATA_MMVF_CASE(64); 494 STRATA_MMVF_CASE(96); 495 STRATA_MMVF_CASE(128); 496 STRATA_MMVF_CASE(160); 497 STRATA_MMVF_CASE(192); 498 STRATA_MMVF_CASE(224); 499 STRATA_MMVF_CASE(256); 500 } 501#undef STRATA_MMVF_CASE 502 const cudaError_t result = cudaGetLastError(); 503 if (result != cudaSuccess) 504 throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf launch: ") + cudaGetErrorString(result)); 505} 506 507 508 509void bf16_gemv_fp32_mmvf_cols(const float* x, const uint16_t* w, float* y, int64_t n_in, int64_t n_out, int ncols, 510 void* stream) { 511 // columns contiguous: the existing multi-row kernel (weight read once, each output bitwise its one-row call), 512 // up to 8 rows per launch 513 for (int c0 = 0; c0 < ncols; c0 += 8) { 514 const int nc = ncols - c0 < 8 ? ncols - c0 : 8; 515 if (nc == 1) bf16_gemv_fp32_mmvf(x + (size_t) c0 * n_in, w, y + (size_t) c0 * n_out, n_in, n_out, stream); 516 else bf16_gemv_fp32_mmvf_multi(x + (size_t) c0 * n_in, n_in, w, y + (size_t) c0 * n_out, n_out, n_in, n_out, nc, 517 stream); 518 } 519} 520 521} // namespace strata::kernels