okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
09b603886
main
1// src/prefill/moe_fused_iq.cu - see include/strata/prefill/moe_fused_iq.hpp (#136: the native packs' prompt experts on 2// the fused int8 kernels of moe_fused.cu). 3// 4// The arithmetic. An activation block of 32 values is x = d_x * a, a = round(x / d_x) in -127..127, stored in natural 5// order (per 64 values: 64 codes, then {d_0, d_1} and 8 unused bytes - 80 bytes, the Q2_0 path's size). A weight 6// block of 32 (or 16) values is w = d_w * q with q an int8: the i-quants' codebook entries with their signs applied 7// (IQ2_XXS / IQ2_XS / IQ2_S: 0..43, IQ3_XXS: 0..62, IQ3_S: 0..15, IQ4_XS / IQ4_NL: -127..113), Q2_0's code - 1. So 8// sum w x = d_w * d_x * sum q a, |sum q a| <= 32 * 127 * 127 < 2^22, 9// and the int32 dot becomes a float exactly with one float add: the mma starts from C = 0x4B400000 (the bits of 10// 1.5 * 2^23), so as_float(D) - 1.5 * 2^23 is the dot. Per output and 32 values: that add, d_w * d_x, one fma (the 11// formats with a scale per 16 values: two dots, each times its d_w, then d_x). d_w is llama.cpp's own: IQ2_XXS 12// d (2s + 1) / 8, IQ2_XS / IQ2_S d (2s + 1) / 8 per 16, IQ3_XXS d (2s + 1) / 4, IQ3_S d (2s + 1), IQ4_XS d (s - 32), 13// IQ4_NL and Q2_0 d (dequantize_row_* in ggml-quants.c, mmq-load-tiles.cuh). 14// 15// The load stage. Per 64 values of K (a stage), a thread decodes one 32-value sub-block of one of the work item's 16// weight rows: its block bytes are read from the blob with 16-bit loads (the GGUF blocks are 2-byte aligned: 66 / 74 / 17// 82 / 98 / 110 / 136 / 18 bytes) one stage ahead into registers (and two 256-value blocks ahead into L2), then turned 18// into 32 int8 and the sub-block's scales in shared memory, double-buffered. The codebooks live in shared memory (up 19// to 8 KB, IQ2_S). The activations arrive by cp.async, four stages deep, as in moe_fused.cu; the fragments come by 20// ldmatrix. The products are mma.sync m16n8k32 (m16n8k16 for the formats with a scale per 16 values); the epilogues 21// (SwiGLU, H to int8 per 32 features; down into the per-slot rows) are moe_fused.cu's. 22#include "strata/prefill/moe_fused_iq.hpp" 23#include "strata/prefill/pf_fused_override.hpp" 24#include "strata/prefill/fused_tile_rule.hpp" 25#include "strata/prefill/fused_tq_default.hpp" 26#include "strata/prefill/fused_tq_hip.hpp" 27#include "strata/kernels/gfx_arch.hpp" 28#include "strata/core/emulate.hpp" 29#include "strata/core/sm120_series.hpp" 30#include "strata/core/gfx12_platform_defaults.hpp" 31 32#if defined(__HIPCC__) 33#include "moe_f16_common.cuh" 34#include "moe_f16_kq.hpp" 35#include "moe_p1a.hpp" 36#endif 37 38#include <cuda_fp16.h> 39#include <cuda_runtime.h> 40 41#include "ggml.h" 42 43#define GGML_COMMON_DECL_CUDA 44#define GGML_COMMON_IMPL_CUDA 45#include "ggml-common.h" 46// (STRATA_PF_FUSED_TQ=1: CUDA sm_80+, HIP gfx12) the trellis types' int8 codebook, the same table the decode kernels and the trellis MMQ tiles 47// read (tq.cuh's tq_lane_to_q8): the fused kernels take MMQ's int8 weight values exactly 48#include "../kernels/cuda/tq_lut_i8.cuh" 49 50#include <algorithm> 51#include <cstdio> 52#include <cstdlib> 53#include <cstring> 54#include <mutex> 55#include <type_traits> 56 57namespace strata::prefill::fused { 58namespace { 59 60void ck(cudaError_t e, const char* what) { 61 if (e != cudaSuccess) { 62 std::fprintf(stderr, "prefill fused experts (native): %s: %s\n", what, cudaGetErrorString(e)); 63 std::exit(1); 64 } 65} 66 67constexpr int AB = 80; // activation bytes per 64 values: 64 codes, {d0, d1}, 8 unused 68constexpr int MAGIC = 0x4B400000; // the bits of 1.5 * 2^23 69constexpr float MAGICF = 12582912.0f; 70// 16 warps, each 64 weight rows x 16 routed rows; WW of them along the weight rows and 16 / WW along the routed rows: a 71// work item is (WR = 64 WW weight rows) x (TR = 256 / WW routed rows of one expert) - 256 x 64 or 128 x 128, chosen 72// per launch by the rows per expert (pick_ww) 73constexpr int THREADS = 512; 74constexpr int WLD = 80; // bytes per decoded weight row of a stage: 64 int8 + 16 (no bank conflicts) 75__host__ __device__ constexpr int tile_rows(int ww, int nt = THREADS) { return nt / 32 / ww * 16; } 76__host__ __device__ constexpr int weight_rows(int ww) { return 64 * ww; } 77constexpr int ASTAGES = 4; // cp.async depth of the activations (64 values of K a stage) 78// Turing: 2 (its copies are plain loads, so depth buys nothing, and 4 does not fit its 64 KB of shared memory) 79#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 800 80constexpr int ASTAGES_DEV = 2; 81constexpr bool T75_DEV = true; // the Q2_0 stage epilogue in native_kernel: fewer instructions, its sums in another order 82#else 83constexpr int ASTAGES_DEV = ASTAGES; 84constexpr bool T75_DEV = false; 85#endif 86constexpr int GU_ROWS_K = kMoeEmbd, D_ROWS_K = kMoeFF; // K of gate/up (n_embd) and of down (n_ff): the compiled geometry's (2560 and 640 for Flash-Next) 87// the weight rows of a product: gate/up 2 n_ff (1280), down n_embd (2560) 88constexpr int GU_ROWS_N = kMoeGuRows, D_ROWS_N = kMoeEmbd; 89constexpr int H_ROW_BYTES = D_ROWS_K / 64 * AB; // bytes of a row of H in the int8 activation layout (800 for Flash-Next) 90static_assert(GU_ROWS_K % 256 == 0 && D_ROWS_K % 64 == 0, "the native expert kernels: K of gate/up is whole 256-value super-blocks, K of down whole 64-value stages"); 91 92// the formats (ggml type ids) 93static_assert(GGML_TYPE_IQ2_XXS == strata::prefill::fused_tile::kTypeIq2Xxs && GGML_TYPE_IQ2_XS == strata::prefill::fused_tile::kTypeIq2Xs && 94 GGML_TYPE_IQ2_S == strata::prefill::fused_tile::kTypeIq2S && GGML_TYPE_IQ3_XXS == strata::prefill::fused_tile::kTypeIq3Xxs && 95 GGML_TYPE_IQ3_S == strata::prefill::fused_tile::kTypeIq3S && GGML_TYPE_IQ4_XS == strata::prefill::fused_tile::kTypeIq4Xs, 96 "fused_tile_rule.hpp's default_gate_format uses ggml's type ids"); 97static_assert(GGML_TYPE_Q5_1 == strata::prefill::fused_tile::kTypeQ5_1 && GGML_TYPE_Q8_0 == strata::prefill::fused_tile::kTypeQ8_0 && GGML_TYPE_Q4_K == strata::prefill::fused_tile::kTypeQ4K && 98 GGML_TYPE_Q5_K == strata::prefill::fused_tile::kTypeQ5K && GGML_TYPE_Q6_K == strata::prefill::fused_tile::kTypeQ6K, 99 "fused_tile_rule.hpp's wide_pair and kq_flash_kind use ggml's type ids"); 100constexpr int T_IQ2_XXS = GGML_TYPE_IQ2_XXS, T_IQ2_XS = GGML_TYPE_IQ2_XS, T_IQ2_S = GGML_TYPE_IQ2_S, 101 T_IQ3_XXS = GGML_TYPE_IQ3_XXS, T_IQ3_S = GGML_TYPE_IQ3_S, T_IQ4_XS = GGML_TYPE_IQ4_XS, 102 T_IQ4_NL = GGML_TYPE_IQ4_NL, T_Q2_0 = GGML_TYPE_Q2_0, 103 // Aurora (opt-in, HIP gfx11 only): Unsloth UD-Q4_K_XL's experts - gate/up Q4_K (one layer Q5_K), down Q5_1 104 // (five layers Q8_0). Their weights are w = d_w q + m_w (a minimum), so a sub-block's dot is 105 // sum w x = d_x (d_w sum q a + m_w sum a): 106 // the activation blocks carry sum a per 32 values in their eight spare bytes (quant_act_nat_kernel and 107 // the H epilogue write it for every path; no other format reads it) 108 T_Q4_K = GGML_TYPE_Q4_K, T_Q5_K = GGML_TYPE_Q5_K, T_Q5_0 = GGML_TYPE_Q5_0, T_Q5_1 = GGML_TYPE_Q5_1, T_Q8_0 = GGML_TYPE_Q8_0, 109 // CUDA opt-in (STRATA_PF_FUSED_Q6K=1): UD-Q6_K_XL's gate/up (47 layers; UD-Q5_K_XL's one Q6_K layer): w = d sc (q - 32), an 110 // int8 scale per 16 values, no minimum 111 T_Q6_K = GGML_TYPE_Q6_K, 112 // CUDA opt-in (STRATA_PF_FUSED_TQ=1): the Gyro trellis types (agentionai's ids, cmake/strata_ggml_tq_overlay.cmake; the pinned 113 // ggml.h has no enumerators for them): 128 weights a block, {fp16 d; qs}; Gyro-M TQ2_T, Gyro-S TQK6 gate/up + TQK7 down 114 T_TQ2_T = 144, T_TQK6 = 145, T_TQK7 = 146, 115 // NVFP4 (ModelOpt's packs): 64-value blocks {4 UE4M3 scales, 32 bytes of E2M1 codes}, one block a stage. Its int8 weights are the E2M1 values 116 // doubled (kvalues_fp4: 0, 1, 2, 3, 4, 6, 8, 12 with signs), one scale d_ue4m3 / 2 per 16 values (the per-16 path); the per-expert weight_scale_2 117 // values (s_gate, s_up, s_down: the blob's 16-byte tail) go on the FP32 products in the epilogues 118 T_NVFP4 = GGML_TYPE_NVFP4; 119// Turing (sm_75, STRATA_PF_FUSED_SM75): the i-quant gate/up formats and the IQ4_NL down that have a set-up instance of native_kernel there, one bit each (DevInfo::t75_set; a down's bits are 120// 8 up). The kernels are the same source as sm_80+'s (the Turing MMAs are m8n8k16, mma32 / mma16 below); the block gets 64 KB of shared memory there, so a format is set up only if its 121// instance fits (setup_ww), and its 64-row (WW 4) instance only if that fits too (t75_w4). kT75Formats is the list of formats taken on Turing (a commit adds one format at a time). 122__host__ __device__ constexpr int t75_slot(int t) { 123 return t == T_IQ2_XXS ? 0 : t == T_IQ2_XS ? 1 : t == T_IQ2_S ? 2 : t == T_IQ3_XXS ? 3 : t == T_IQ3_S ? 4 : t == T_IQ4_XS ? 5 : t == T_IQ4_NL ? 6 : -1; 124} 125__host__ __device__ constexpr unsigned t75_bit(int t, bool gu) { return t75_slot(t) < 0 ? 0u : 1u << (t75_slot(t) + (gu ? 0 : 8)); } 126constexpr unsigned kT75Formats = t75_bit(T_IQ3_S, true) | t75_bit(T_IQ4_NL, false) | t75_bit(T_IQ2_S, true) | t75_bit(T_IQ3_XXS, true) | t75_bit(T_IQ4_XS, true) | 127 t75_bit(T_IQ2_XXS, true) | t75_bit(T_IQ2_XS, true); 128// the trellis types: 128-value blocks (32 steps of 4 weights), two 32-value sub-blocks a 64-value stage 129__host__ __device__ constexpr bool is_tq(int t) { return t == T_TQ2_T || t == T_TQK6 || t == T_TQK7; } 130__host__ __device__ constexpr int tq_k(int t) { return t == T_TQK6 ? 6 : t == T_TQK7 ? 7 : 8; } 131static_assert(sizeof(block_iq2_xxs) == 66 && sizeof(block_iq2_xs) == 74 && sizeof(block_iq2_s) == 82 && 132 sizeof(block_iq3_xxs) == 98 && sizeof(block_iq3_s) == 110 && sizeof(block_iq4_xs) == 136 && 133 sizeof(block_iq4_nl) == 18 && sizeof(block_q2_0) == 18 && sizeof(block_q4_K) == 144 && 134 sizeof(block_q5_K) == 176 && sizeof(block_q5_0) == 22 && sizeof(block_q5_1) == 24 && sizeof(block_q8_0) == 34 && 135 sizeof(block_q6_K) == 210 && sizeof(block_nvfp4) == 36, 136 "the block layouts this file decodes"); 137 138// block bytes, scale per 16 values, codebook bytes in shared memory 139__host__ __device__ constexpr int block_bytes(int t) { 140 return t == T_IQ2_XXS ? 66 : t == T_IQ2_XS ? 74 : t == T_IQ2_S ? 82 : t == T_IQ3_XXS ? 98 : t == T_IQ3_S ? 110 141 : t == T_IQ4_XS ? 136 : t == T_Q4_K ? 144 : t == T_Q5_K ? 176 : t == T_Q5_0 ? 22 : t == T_Q5_1 ? 24 : t == T_Q8_0 ? 34 142 : t == T_Q6_K ? 210 : t == T_NVFP4 ? 36 : t == T_TQ2_T ? 34 : t == T_TQK6 ? 26 : t == T_TQK7 ? 30 : 18; 143} 144// the formats with a minimum (w = d q + m), and the raw words a sub-block's load stage holds 145__host__ __device__ constexpr bool has_min(int t) { return t == T_Q4_K || t == T_Q5_K || t == T_Q5_1; } 146__host__ __device__ constexpr int raw_words(int t) { 147 return t == T_Q4_K ? 10 : t == T_Q5_K ? 18 : t == T_Q5_0 ? 6 : t == T_Q5_1 ? 6 : t == T_Q8_0 ? 9 : t == T_Q6_K ? 18 : t == T_TQ2_T ? 4 : 5; 148} 149__host__ __device__ constexpr bool per16(int t) { return t == T_IQ2_XS || t == T_IQ2_S || t == T_Q6_K || t == T_NVFP4; } 150__host__ __device__ constexpr int grid_bytes(int t) { 151 return t == T_IQ2_XXS ? 256 * 8 : t == T_IQ2_XS ? 512 * 8 : t == T_IQ2_S ? 1024 * 8 : t == T_IQ3_XXS ? 256 * 4 152 : t == T_IQ3_S ? 512 * 4 : is_tq(t) ? 2048 * 2 : 0; // (the trellis types: the int8 codebook tq_lut_i8, 2,048 points of two int8) 153} 154// ---- weight staging (CUDA sm_80+): the 512-thread 128-row instances of the formats below copy each weight row's slot into shared memory with 16-byte 155// cp.async (a ring of two slots, issued two slots ahead) instead of every decode thread reading it from global memory in 2- and 4-byte loads; the decode 156// then runs the same load_unit on the shared-memory address, so the bits are the same. It is chosen per instance at compile time, only where it was measured 157// faster (RTX A4000, one layer: +10..+16% for these pairs); every other instance keeps its code and its shared memory unchanged. 158// A slot is the bytes of SPB stages (64 values of K each) of a row: a 256-value block for the gate/up i-quants, two stages of 32-value blocks (four blocks) for IQ4_NL / Q5_0 / Q8_0 on either side, 159// and for the Q6_K gate/up half a super-block gathered from three windows (ql, qh, scales + d). The GGUF blocks are only 2-byte aligned, so a slot is copied as the 16-byte chunks of the aligned 160// window around it (SW_CHUNKS of them). 161constexpr int kSwAst = 3; // the staged instances' activation stages (the ring needs SPB >= AST - 1; 4 stages would not fit the ring beside it) 162constexpr size_t kSmemOptIn = 101376; // 99 KB: the opt-in shared memory a block gets on sm_86 / sm_89 / sm_120 / sm_121 163__host__ __device__ constexpr bool sw_on(int t, bool gu) { 164#if defined(__HIPCC__) 165 (void) t; (void) gu; 166 return false; 167#else 168 // (the K-quant downs: Q5_K / Q6_K rows of 256-value super-blocks, i.e. a K that is a multiple of 256 - Qwen3.6's n_ff 512; the Flash-Next geometry (640) has none; the K-quant gate/up Q4_K / Q5_K: 169 // their K is the model width, 2,560 / 2,048 = 10 / 8 super-blocks) 170 return gu ? (t == T_IQ3_XXS || t == T_IQ3_S || t == T_IQ4_XS || t == T_Q8_0 || t == T_Q6_K || t == T_Q4_K || t == T_Q5_K) 171 : (t == T_IQ4_NL || t == T_Q8_0 || t == T_Q5_0 || ((t == T_Q5_K || t == T_Q6_K) && D_ROWS_K % 256 == 0)); 172#endif 173} 174__host__ __device__ constexpr bool sw_inst(int t, bool gu, int ww, int nt, bool f16) { return sw_on(t, gu) && ww == 2 && nt == THREADS && !f16; } 175// the 32-value-block formats (two blocks a 64-value stage), gate/up (Q8_0) or down 176__host__ __device__ constexpr bool sw_b32(int t) { return t == T_IQ4_NL || t == T_Q5_0 || t == T_Q8_0; } 177// the gate/up formats whose block spans 256 values (the kernel's GU256: one block per four stages) 178// (a K-quant down with K a multiple of 256 walks its super-blocks the same way: Q5_K / Q6_K) 179__host__ __device__ constexpr bool sw_gu256(int t, bool gu) { 180 return (gu || ((t == T_Q5_K || t == T_Q6_K) && D_ROWS_K % 256 == 0)) && t != T_Q2_0 && t != T_Q8_0 && t != T_NVFP4 && !is_tq(t); 181} 182// stages a slot: a whole 256-value block (gate/up i-quants), else two (four 32-value blocks; the Q6_K gate/up and the Q5_K / Q6_K downs: half a super-block) 183__host__ __device__ constexpr int sw_spb(int t, bool gu) { return sw_gu256(t, gu) && t != T_Q6_K && t != T_Q5_K ? 4 : 2; } 184__host__ __device__ constexpr int sw_slot_bytes(int t, bool gu) { return sw_gu256(t, gu) ? block_bytes(t) : sw_spb(t, gu) * 2 * block_bytes(t); } // (Q6_K: unused, its slot is three windows) 185// 16-byte chunks of a row's slot: the aligned window is at most slot + 14 bytes; Q6_K: ql 64 B (5) + qh 32 B (3) + scales and d, at most 18 B (2); 186// Q5_K (down only): qs 64 B (5) + qh 32 B (3) + d, dmin and the 12 scale bytes 16 B (2) 187__host__ __device__ constexpr int sw_chunks(int t, bool gu) { return (t == T_Q6_K || t == T_Q5_K) ? 10 : (sw_slot_bytes(t, gu) + 14 + 15) / 16; } 188constexpr int WLDH = 144; // F16: bytes per decoded weight row of a stage: 64 halves + 16 (ldmatrix rows apart) 189// shared memory of an instance. `astages` is the device's depth (host_astages()); a staged instance takes kSwAst instead and adds its ring, sized for ITS side 190// (gu) only: a failed shared-memory set-up makes dev_info() drop every fused kernel 191__host__ __device__ constexpr size_t smem_bytes(int t, int ww, int astages, int nt, bool f16, bool gu) { 192 const bool sw = sw_inst(t, gu, ww, nt, f16) && astages == ASTAGES; 193 return (size_t) 2 * weight_rows(ww) * (f16 ? WLDH : WLD + 16) + (size_t) (sw ? kSwAst : astages) * tile_rows(ww, nt) * AB + 194 (size_t) tile_rows(ww, nt) * 4 + grid_bytes(t) + (sw ? (size_t) 2 * weight_rows(ww) * 16 * sw_chunks(t, gu) : 0); 195} 196 197// moe_fused.cu's grouping tables (the same layout: fused::group writes them) 198struct Tables { 199 int32_t *cnt, *off, *fill, *ts; 200 int2* tiles; 201}; 202size_t tiles_at(int n_expert) { return ((size_t) (4 * n_expert + 2) * 4 + 15) / 16 * 16; } 203Tables tables(void* scratch, int n_expert) { 204 int32_t* p = (int32_t*) scratch; 205 return {p, p + n_expert, p + 2 * n_expert + 1, p + 3 * n_expert + 1, 206 (int2*) ((uint8_t*) scratch + tiles_at(n_expert))}; 207} 208 209// ---- activations in natural order: one warp per 64 values 210__global__ void quant_act_nat_kernel(const float* __restrict__ x, int64_t nblk, uint8_t* __restrict__ xa) { 211 const int64_t w = (int64_t) blockIdx.x * (blockDim.x / 32) + threadIdx.x / 32; 212 const int lane = threadIdx.x & 31; 213 if (w >= nblk) return; 214 const float v0 = x[w * 64 + lane], v1 = x[w * 64 + 32 + lane]; 215 float a0 = fabsf(v0), a1 = fabsf(v1); 216#pragma unroll 217 for (int o = 16; o > 0; o >>= 1) { 218 a0 = fmaxf(a0, __shfl_xor_sync(0xffffffffu, a0, o)); 219 a1 = fmaxf(a1, __shfl_xor_sync(0xffffffffu, a1, o)); 220 } 221 uint8_t* out = xa + w * AB; 222 const int c0 = a0 > 0.0f ? __float2int_rn(v0 * (127.0f / a0)) : 0, c1 = a1 > 0.0f ? __float2int_rn(v1 * (127.0f / a1)) : 0; 223 out[lane] = (uint8_t) (int8_t) c0; 224 out[32 + lane] = (uint8_t) (int8_t) c1; 225 int s0 = c0, s1 = c1; // the codes' sums per 32 values (the formats with a minimum read them) 226#pragma unroll 227 for (int o = 16; o > 0; o >>= 1) { 228 s0 += __shfl_xor_sync(0xffffffffu, s0, o); 229 s1 += __shfl_xor_sync(0xffffffffu, s1, o); 230 } 231 if (lane == 0) *(float4*) (out + 64) = make_float4(a0 / 127.0f, a1 / 127.0f, (float) s0, (float) s1); 232} 233 234// (STRATA_PF_FUSED_TQ=1: CUDA sm_80+, HIP gfx12, wave32) the block-Hadamard rotation of a Hadamard-folded expert's input (include/strata/kernels/cpu/hadamard.hpp): 235// y = H_128 (s * x) / sqrt(128) per block of 128 values, H the Sylvester-Walsh-Hadamard matrix. One warp a block: lane l holds values 236// 32 k + l (k = 0..3); the in-place fast transform's butterflies over strides 1-16 by shuffles, 32 and 64 between the registers. 237__device__ __forceinline__ void fwht128_warp(float (&v)[4], int lane) { 238#pragma unroll 239 for (int st = 1; st < 32; st <<= 1) { 240#pragma unroll 241 for (int k = 0; k < 4; ++k) { 242 const float o = __shfl_xor_sync(0xffffffffu, v[k], st); 243 v[k] = (lane & st) ? o - v[k] : v[k] + o; 244 } 245 } 246 const float a0 = v[0] + v[1], a1 = v[0] - v[1], a2 = v[2] + v[3], a3 = v[2] - v[3]; 247 v[0] = (a0 + a2) * 0.08838834764831845f; v[1] = (a1 + a3) * 0.08838834764831845f; // (1 / sqrt(128)) 248 v[2] = (a0 - a2) * 0.08838834764831845f; v[3] = (a1 - a3) * 0.08838834764831845f; 249} 250// in place, rows x cols (cols a multiple of 128): every row's blocks rotated (signs: nullptr = identity) 251__global__ void hadamard128_kernel(float* __restrict__ x, int64_t nblk, int cols, const float* __restrict__ signs) { 252 const int64_t w = (int64_t) blockIdx.x * (blockDim.x / 32) + threadIdx.x / 32; 253 const int lane = threadIdx.x & 31; 254 if (w >= nblk) return; 255 float* p = x + w * 128; 256 const int c0 = (int) ((w * 128) % cols); 257 float v[4]; 258#pragma unroll 259 for (int k = 0; k < 4; ++k) v[k] = p[32 * k + lane] * (signs ? signs[c0 + 32 * k + lane] : 1.0f); 260 fwht128_warp(v, lane); 261#pragma unroll 262 for (int k = 0; k < 4; ++k) p[32 * k + lane] = v[k]; 263} 264// the FP32 H rows the trellis gate/up wrote (a folded down; n_ff values at the start of each row's n_embd-float down-output row): rotated, 265// then int8 per 32 values into the down product's activation layout (quant_act_nat_kernel's: 64 codes, the two scales, the two code sums 266// per 80 bytes), for the rows of experts e0..e1 only (tb.off on the device) 267__global__ void rot_quant_h_kernel(const float* __restrict__ hf, const int32_t* __restrict__ off, int e0, int e1, int64_t n, 268 const float* __restrict__ signs, uint8_t* __restrict__ ha) { 269 constexpr int BLK = D_ROWS_K / 128; // Hadamard blocks a row (5 for n_ff 640) 270 const int64_t w = (int64_t) blockIdx.x * (blockDim.x / 32) + threadIdx.x / 32; 271 const int lane = threadIdx.x & 31; 272 if (w >= n * BLK) return; 273 const int64_t row = w / BLK; 274 const int b = (int) (w % BLK); 275 if (row < off[e0] || row >= off[e1]) return; 276 const float* p = hf + row * D_ROWS_N + 128 * b; 277 float v[4]; 278#pragma unroll 279 for (int k = 0; k < 4; ++k) v[k] = p[32 * k + lane] * (signs ? signs[128 * b + 32 * k + lane] : 1.0f); 280 fwht128_warp(v, lane); 281#pragma unroll 282 for (int k = 0; k < 4; ++k) { // value 128 b + 32 k + lane: AB block (128 b + 32 k) / 64, half k & 1 283 float am = fabsf(v[k]); 284#pragma unroll 285 for (int o = 16; o > 0; o >>= 1) am = fmaxf(am, __shfl_xor_sync(0xffffffffu, am, o)); 286 const int code = am > 0.0f ? __float2int_rn(v[k] * (127.0f / am)) : 0; 287 int cs = code; 288#pragma unroll 289 for (int o = 16; o > 0; o >>= 1) cs += __shfl_xor_sync(0xffffffffu, cs, o); 290 uint8_t* o = ha + row * H_ROW_BYTES + ((128 * b + 32 * k) / 64) * AB; 291 const int hh = k & 1; 292 o[32 * hh + lane] = (uint8_t) (int8_t) code; 293 if (lane == 0) { *(float*) (o + 64 + 4 * hh) = am / 127.0f; *(float*) (o + 72 + 4 * hh) = (float) cs; } 294 } 295} 296 297#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 298__device__ __forceinline__ void cp16(void* dst, const void* src) { 299 asm volatile("cp.async.cg.shared.global [%0], [%1], 16;\n" ::"r"((unsigned) __cvta_generic_to_shared(dst)), 300 "l"(src)); 301} 302__device__ __forceinline__ void cp_commit() { asm volatile("cp.async.commit_group;\n" ::); } 303__device__ __forceinline__ void pf_l2(const void* p) { asm volatile("prefetch.global.L2 [%0];\n" ::"l"(p)); } 304template <int N> __device__ __forceinline__ void cp_wait() { asm volatile("cp.async.wait_group %0;\n" ::"n"(N)); } 305// d = MAGIC + A (16 x 32 s8, row) * B (32 x 8 s8, col): the int32 dot with the magic bias already added (the C 306// operand), so as_float(d) - 1.5 * 2^23 is the dot as a float 307__device__ __forceinline__ void mma32(int (&d)[4], const uint32_t (&a)[4], uint32_t b0, uint32_t b1) { 308 asm("mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, {%10,%10,%10,%10};\n" 309 : "=r"(d[0]), "=r"(d[1]), "=r"(d[2]), "=r"(d[3]) 310 : "r"(a[0]), "r"(a[1]), "r"(a[2]), "r"(a[3]), "r"(b0), "r"(b1), "r"(MAGIC)); 311} 312// the same at m16n8k16 (A 16 x 16, B 16 x 8) 313__device__ __forceinline__ void mma16(int (&d)[4], uint32_t a0, uint32_t a1, uint32_t b0) { 314 asm("mma.sync.aligned.m16n8k16.row.col.s32.s8.s8.s32 {%0,%1,%2,%3}, {%4,%5}, {%6}, {%7,%7,%7,%7};\n" 315 : "=r"(d[0]), "=r"(d[1]), "=r"(d[2]), "=r"(d[3]) 316 : "r"(a0), "r"(a1), "r"(b0), "r"(MAGIC)); 317} 318__device__ __forceinline__ float dotf(int d) { return __int_as_float(d) - MAGICF; } 319// F16 (STRATA_PF_FUSED_F16): d += A (16 x 16 f16, row) * B (16 x 8 f16, col), FP32 accumulators in place 320__device__ __forceinline__ void mma16h(float (&d)[4], const uint32_t (&a)[4], uint32_t b0, uint32_t b1) { 321 asm("mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, {%0,%1,%2,%3};\n" 322 : "+f"(d[0]), "+f"(d[1]), "+f"(d[2]), "+f"(d[3]) 323 : "r"(a[0]), "r"(a[1]), "r"(a[2]), "r"(a[3]), "r"(b0), "r"(b1)); 324} 325// four int8 (bytes 0-3) times a scale as two half2: lo = bytes 0, 1, hi = bytes 2, 3. The flipped sign bit makes 326// x + 128 the mantissa of 1024 (exact), minus 1152 is x, then one packed multiply. 327__device__ __forceinline__ void i8x4_h2(uint32_t w, __half2 sc, uint32_t& lo, uint32_t& hi) { 328 const uint32_t c = w ^ 0x80808080u; 329 const __half2 m = __floats2half2_rn(1152.0f, 1152.0f); 330 const uint32_t l = __byte_perm(c, 0x64646464u, 0x4140), h = __byte_perm(c, 0x64646464u, 0x4342); 331 const __half2 lv = __hmul2(__hsub2(*(const __half2*) &l, m), sc), hv = __hmul2(__hsub2(*(const __half2*) &h, m), sc); 332 lo = *(const uint32_t*) &lv; 333 hi = *(const uint32_t*) &hv; 334} 335// four 8 x 16-byte matrices, row addresses from lanes 0-7, 8-15, 16-23, 24-31: lane (g, t) gets bytes 4t..4t+3 of row g 336// of each - an mma fragment 337__device__ __forceinline__ void ldsm4(uint32_t (&r)[4], const void* p) { 338 asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n" 339 : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3]) 340 : "r"((unsigned) __cvta_generic_to_shared(p)) 341 : "memory"); 342} 343 344#elif defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750 345// Turing (STRATA_PF_FUSED_SM75): plain copies instead of cp.async (the same bytes; the barriers around them stay), and 346// the s8 MMAs as m8n8k16 - m16n8k32 is four (rows g / g + 8 by k 0-15 / 16-31), m16n8k16 two (rows g / g + 8), each 347// pair starting from MAGIC. The int32 sums are exact in any order: the same d as the sm_80 instructions. 348__device__ __forceinline__ void cp16(void* dst, const void* src) { *(uint4*) dst = __ldg((const uint4*) src); } 349__device__ __forceinline__ void cp_commit() {} 350__device__ __forceinline__ void pf_l2(const void* p) { asm volatile("prefetch.global.L2 [%0];\n" ::"l"(p)); } 351template <int N> __device__ __forceinline__ void cp_wait() {} 352__device__ __forceinline__ void mma8_s8(int& c0, int& c1, uint32_t a, uint32_t b) { 353 asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0,%1}, {%2}, {%3}, {%0,%1};\n" 354 : "+r"(c0), "+r"(c1) : "r"(a), "r"(b)); 355} 356__device__ __forceinline__ void mma8_s8_c(int& d0, int& d1, uint32_t a, uint32_t b, int c) { 357 asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0,%1}, {%2}, {%3}, {%4,%4};\n" 358 : "=r"(d0), "=r"(d1) : "r"(a), "r"(b), "r"(c)); 359} 360__device__ __forceinline__ void mma32(int (&d)[4], const uint32_t (&a)[4], uint32_t b0, uint32_t b1) { 361 mma8_s8_c(d[0], d[1], a[0], b0, MAGIC); 362 mma8_s8(d[0], d[1], a[2], b1); 363 mma8_s8_c(d[2], d[3], a[1], b0, MAGIC); 364 mma8_s8(d[2], d[3], a[3], b1); 365} 366__device__ __forceinline__ void mma16(int (&d)[4], uint32_t a0, uint32_t a1, uint32_t b0) { 367 mma8_s8_c(d[0], d[1], a0, b0, MAGIC); 368 mma8_s8_c(d[2], d[3], a1, b0, MAGIC); 369} 370__device__ __forceinline__ float dotf(int d) { return __int_as_float(d) - MAGICF; } 371// F16 (STRATA_PF_FUSED_F16) is sm_80+ only (dev_info stops before the NT = 256 setup on Turing): stubs so that its 372// native_kernel instantiations compile here 373__device__ __forceinline__ void mma16h(float (&)[4], const uint32_t (&)[4], uint32_t, uint32_t) { __trap(); } 374__device__ __forceinline__ void i8x4_h2(uint32_t, __half2, uint32_t&, uint32_t&) { __trap(); } 375__device__ __forceinline__ void ldsm4(uint32_t (&r)[4], const void* p) { 376 asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n" 377 : "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3]) 378 : "r"((unsigned) __cvta_generic_to_shared(p)) 379 : "memory"); 380} 381#endif 382#if (defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750) || defined(__HIPCC__) 383#if defined(__HIPCC__) 384__device__ __forceinline__ float dotf(int d) { return __int_as_float(d) - MAGICF; } 385#endif 386// ---- the load stage: a 32-value sub-block's bytes into registers, then int8 and scales 387__device__ __forceinline__ uint32_t ld16(const uint8_t* p) { return *(const uint16_t*) p; } 388__device__ __forceinline__ uint32_t ld32(const uint8_t* p) { return ld16(p) | (ld16(p + 2) << 16); } 389__device__ __forceinline__ float half_at(uint32_t w) { return __half2float(__ushort_as_half((unsigned short) w)); } 390// NVFP4's UE4M3 scale byte, halved (the E2M1 values are doubled): ggml_cuda_ue4m3_to_fp32 and iq_kernels.cu's ue4m3_to_f32_half, from the bits (every such value is exact in FP32); 391// 0 and 0x7F (NaN) are 0. Normal: 2^(exp - 8) (1 + man / 8) = exponent field exp + 119, mantissa man << 20; exp 0: man / 1024. 392__device__ __forceinline__ float ue4m3_half(uint32_t x) { 393 x &= 0xFF; 394 if (x == 0 || x == 0x7F) return 0.0f; 395 const uint32_t ex = (x >> 3) & 0xF, man = x & 0x7; 396 return ex == 0 ? (float) man * 0.0009765625f : __uint_as_float(((ex + 119u) << 23) | (man << 20)); 397} 398// llama.cpp's sign unpacking: 7 bits of signs, the 8th their parity (bit 7 of v may be anything) 399__device__ __forceinline__ uint32_t unpack_ksigns(uint32_t v) { 400 v &= 0xFF; 401 const uint32_t p = __popc(v) & 1; 402 return (v ^ p << 7) * 0x01010101u; 403} 404// 8 bytes of a codebook entry with the sign byte `s` (broadcast) applied: bits 0-3 to .x, 4-7 to .y 405__device__ __forceinline__ void signed8(uint32_t gx, uint32_t gy, uint32_t s, uint32_t& qx, uint32_t& qy) { 406 const uint32_t m0 = __vcmpne4(s & 0x08040201u, 0), m1 = __vcmpne4(s & 0x80402010u, 0); 407 qx = __vsub4(gx ^ m0, m0); 408 qy = __vsub4(gy ^ m1, m1); 409} 410#if defined(__HIPCC__) 411// gfx11 (Aurora): the signs from a shared-memory table instead of the compare / subtract emulation. An entry (one per 412// sign byte b; for the 7-bit sign formats per 7 bits, the parity bit folded in) is {m0, c0, m1, c1}: m = 0xFF in the 413// bytes whose sign bit is set (bits 0-3 for .x, 4-7 for .y), c = m & 0x01010101. q = (g ^ m) + c is -g where the sign 414// is set - the two's complement, byte-wise - and g elsewhere; no byte carries into the next, because no codebook entry 415// has a zero byte (256 - g with g in 1..127; the grids' bytes are 1..62), so it is the bytes signed8 gives. 416__device__ __forceinline__ void signed8t(uint32_t gx, uint32_t gy, const uint4 sg, uint32_t& qx, uint32_t& qy) { 417 qx = (gx ^ sg.x) + sg.y; 418 qy = (gy ^ sg.z) + sg.w; 419} 420__host__ __device__ constexpr int sign_entries(int t) { return t == T_IQ2_S || t == T_IQ3_S ? 256 : 128; } 421__device__ __forceinline__ uint4 sign_entry(int i, bool parity) { 422 uint32_t b = (uint32_t) i; 423 if (parity) b ^= (__popc(b) & 1) << 7; 424 const uint32_t m0 = __vcmpne4(((b & 15) * 0x01010101u) & 0x08040201u, 0), 425 m1 = __vcmpne4(((b >> 4) * 0x01010101u) & 0x08040201u, 0); 426 return make_uint4(m0, m0 & 0x01010101u, m1, m1 & 0x01010101u); 427} 428#endif 429// 8 nibbles of q4 through a 16-entry int8 table (4 words): the low nibbles' values in .x, the high ones' in .y 430// W (X5): direct-selector form on gfx: p = low 3 bits of each nibble picks within the table half, bit 3 picks the half; 431// exhaustively bit-exact over all 2^32 inputs (X5). Disable with -DSTRATA_W_NO_T16. 432#if defined(__HIPCC__) && !defined(STRATA_W_NO_T16) 433__device__ __forceinline__ uint32_t table16_one(uint32_t x, const uint32_t (&t)[4]) { 434 const uint32_t p = x & 0x07070707u; 435 const uint32_t a = __builtin_amdgcn_perm(t[1], t[0], p), b = __builtin_amdgcn_perm(t[3], t[2], p); 436 return __builtin_amdgcn_perm(b, a, ((x >> 1) & 0x04040404u) | 0x03020100u); 437} 438__device__ __forceinline__ void table16(uint32_t q4, const uint32_t (&t)[4], uint32_t& lo, uint32_t& hi) { 439 lo = table16_one(q4, t); 440 hi = table16_one(q4 >> 4, t); 441} 442#else 443__device__ __forceinline__ void table16(uint32_t q4, const uint32_t (&t)[4], uint32_t& lo, uint32_t& hi) { 444 uint32_t tmp[2]; 445 const uint32_t sel = 0x32103210u | ((q4 & 0x88888888u) >> 1); 446#pragma unroll 447 for (int i = 0; i < 2; ++i) { 448 const uint32_t sh = 16 * i; 449 const uint32_t l = __byte_perm(t[0], t[1], q4 >> sh), h = __byte_perm(t[2], t[3], q4 >> sh); 450 tmp[i] = __byte_perm(l, h, sel >> sh); 451 } 452 lo = __byte_perm(tmp[0], tmp[1], 0x6420); 453 hi = __byte_perm(tmp[0], tmp[1], 0x7531); 454} 455#endif 456 457// Raw bytes of sub-block `ib` of the block at `bp` (IQ: the 256-value super-block, ib 0..7; Q2_0: the 64-value block, 458// ib 0..1; IQ4_NL: the 32-value block). 459// 32 bits at a 2-byte aligned address (one load when it is 4-byte aligned) 460__device__ __forceinline__ uint32_t ld32a(const uint8_t* p) { 461 return ((uintptr_t) p & 3) == 0 ? *(const uint32_t*) p : ld32(p); 462} 463template <int T, int NW> __device__ __forceinline__ void load_unit(const uint8_t* bp, int ib, uint32_t (&w)[NW]) { 464 static_assert(NW >= raw_words(T), "raw words"); 465 if constexpr (T == T_IQ2_XXS) { 466 w[0] = ld32(bp + 2 + 8 * ib); w[1] = ld32(bp + 6 + 8 * ib); w[2] = ld16(bp); 467 } else if constexpr (T == T_IQ2_XS) { 468 w[0] = ld32(bp + 2 + 8 * ib); w[1] = ld32(bp + 6 + 8 * ib); w[2] = ld16(bp) | ((uint32_t) bp[66 + ib] << 16); 469 } else if constexpr (T == T_IQ2_S) { 470 w[0] = ld32(bp + 2 + 4 * ib); w[1] = ld32(bp + 34 + 4 * ib); 471 w[2] = ld16(bp) | ((uint32_t) bp[66 + ib] << 16) | ((uint32_t) bp[74 + ib] << 24); 472 } else if constexpr (T == T_IQ3_XXS) { 473 w[0] = ld32(bp + 2 + 8 * ib); w[1] = ld32(bp + 6 + 8 * ib); w[2] = ld32(bp + 66 + 4 * ib); w[3] = ld16(bp); 474 } else if constexpr (T == T_IQ3_S) { 475 w[0] = ld32(bp + 2 + 8 * ib); w[1] = ld32(bp + 6 + 8 * ib); w[2] = ld32(bp + 74 + 4 * ib); 476 w[3] = ld16(bp) | ((uint32_t) bp[66 + ib] << 16) | ((uint32_t) ((bp[106 + ib / 2] >> (4 * (ib & 1))) & 15) << 24); 477 } else if constexpr (T == T_IQ4_XS) { 478#pragma unroll 479 for (int k = 0; k < 4; ++k) w[k] = ld32(bp + 8 + 16 * ib + 4 * k); 480 const uint32_t ls = ((bp[4 + ib / 2] >> (4 * (ib & 1))) & 15) | (((ld16(bp + 2) >> (2 * ib)) & 3) << 4); 481 w[4] = ld16(bp) | (ls << 16); 482 } else if constexpr (T == T_IQ4_NL) { 483#pragma unroll 484 for (int k = 0; k < 4; ++k) w[k] = ld32(bp + 2 + 4 * k); 485 w[4] = ld16(bp); 486 } else if constexpr (T == T_NVFP4) { // d[4], qs[32] (36 bytes): sub-block ib (0, 1) = values 32 ib.. = qs bytes 16 ib.. and the scale bytes 2 ib, 2 ib + 1 487#pragma unroll 488 for (int k = 0; k < 4; ++k) w[k] = ld32a(bp + 4 + 16 * ib + 4 * k); 489 w[4] = ld16(bp + 2 * ib); 490 } else if constexpr (T == T_Q4_K || T == T_Q5_K) { 491 // qs: 32 bytes of the sub-block pair (ib / 2), the nibble chosen in convert; w[8] = d | dmin << 16; w[9]: the 492 // scale/min bytes (get_scale_min_k4's: ib < 4: sc[ib], sc[ib + 4]; else sc[ib + 4], sc[ib - 4], sc[ib]), ib << 24 493 constexpr int QS = T == T_Q4_K ? 16 : 48; 494 const uint8_t* q = bp + QS + 32 * (ib >> 1); 495#pragma unroll 496 for (int k = 0; k < 8; ++k) w[k] = ld32a(q + 4 * k); 497 w[8] = ld32a(bp); 498 const uint8_t* sc = bp + 4; 499 w[9] = ib < 4 ? (uint32_t) sc[ib] | ((uint32_t) sc[ib + 4] << 8) 500 : (uint32_t) sc[ib + 4] | ((uint32_t) sc[ib - 4] << 8) | ((uint32_t) sc[ib] << 16); 501 w[9] |= (uint32_t) ib << 24; 502 if constexpr (T == T_Q5_K) { 503#pragma unroll 504 for (int k = 0; k < 8; ++k) w[10 + k] = ld32a(bp + 16 + 4 * k); // qh 505 } 506 } else if constexpr (T == T_Q5_0) { // d, qh, qs[16] (22 bytes: 2-byte aligned only) 507#pragma unroll 508 for (int k = 0; k < 4; ++k) w[k] = ld32(bp + 6 + 4 * k); 509 w[4] = ld32(bp + 2); 510 w[5] = ld16(bp); 511 } else if constexpr (T == T_Q5_1) { // d, m, qh, qs[16] 512#pragma unroll 513 for (int k = 0; k < 4; ++k) w[k] = ld32a(bp + 8 + 4 * k); 514 w[4] = ld32a(bp + 4); 515 w[5] = ld32a(bp); 516 } else if constexpr (T == T_Q8_0) { // d, qs[32] 517#pragma unroll 518 for (int k = 0; k < 8; ++k) w[k] = ld32(bp + 2 + 4 * k); 519 w[8] = ld16(bp); 520 } else if constexpr (T == T_Q6_K) { 521 // ql[128], qh[64], scales[16] (int8), d (210 bytes: 2-byte aligned only). Sub-block ib = values 32 ib.. of the 256: half 522 // n = ib / 4, quarter r = ib % 4 (ggml's dequantize_row_q6_K): low nibbles from ql[64 n + 32 (r & 1) ..] (bits 4 (r >> 1)..), 523 // two high bits from qh[32 n ..] (bits 2 r..), scales[8 n + 2 r] for values 0-15 and [.. + 1] for 16-31. w[0..7] ql, w[8..15] 524 // qh, w[16] = d | the two scales << 16, w[17] = r 525 const int n = ib >> 2, r = ib & 3; 526#pragma unroll 527 for (int k = 0; k < 8; ++k) w[k] = ld32(bp + 64 * n + 32 * (r & 1) + 4 * k); 528#pragma unroll 529 for (int k = 0; k < 8; ++k) w[8 + k] = ld32(bp + 128 + 32 * n + 4 * k); 530 w[16] = ld16(bp + 208) | (ld16(bp + 192 + 8 * n + 2 * r) << 16); 531 w[17] = (uint32_t) r; 532 } else if constexpr (T == T_TQ2_T) { 533 // (STRATA_PF_FUSED_TQ=1) sub-block ib = steps 8 ib .. 8 ib + 7 of the block {fp16 d; qs[32]}: step t's state is 534 // qs[(t + 31) % 32] << 8 | qs[t] (tq.cuh tq_states4), so the 8 bytes qs[8 ib ..] and the byte before them (circular) 535 w[0] = ld32(bp + 2 + 8 * ib); w[1] = ld32(bp + 6 + 8 * ib); w[2] = bp[2 + ((8 * ib + 31) & 31)]; w[3] = ld16(bp); 536 } else if constexpr (T == T_TQK6 || T == T_TQK7) { 537 // (STRATA_PF_FUSED_TQ=1) {fp16 d; qs[4 K]}, qs a circular stream of 32 K bits (2 K u16 words, LSB first); step t's state is the 16 538 // bits from stream bit (31 - t) K (tq.cuh tq_states4). Steps 8 ib .. 8 ib + 7 lie in the 8 K + 15 bits from bit (24 - 8 ib) K: six u16 539 // words from word (24 - 8 ib) K / 16, wrapped modulo 2 K, into w[0..2]; w[3] = d, w[4] = the start bit inside the first word 540 constexpr int K = tq_k(T), NWD = 2 * K; // (the u16 words of qs; NWD: NW is the template parameter) 541 const int s0 = (24 - 8 * ib) * K, wb = s0 >> 4; 542 uint32_t h[6]; 543#pragma unroll 544 for (int k = 0; k < 6; ++k) h[k] = ld16(bp + 2 + 2 * ((wb + k) % NWD)); 545 w[0] = h[0] | (h[1] << 16); w[1] = h[2] | (h[3] << 16); w[2] = h[4] | (h[5] << 16); 546 w[3] = ld16(bp); 547 w[4] = (uint32_t) (s0 & 15); 548 } else { // Q2_0 549 w[0] = ld32(bp + 2 + 8 * ib); w[1] = ld32(bp + 6 + 8 * ib); w[2] = ld16(bp); 550 } 551} 552 553// load_unit's bytes in two passes (native_kernel's opt-in NT = 256 instances only, CUDA): `issue` loads the 16-bit and 554// 8-bit pieces into registers and `take` assembles the words from them a stage later, so no instruction waits on the 555// loads in the stage that issues them (in one pass the shifts and ORs right after the loads stalled every stage on a 556// memory round trip). The same bytes in the same words as load_unit. 557constexpr int NP = 12; // pieces: at most IQ4_XS's 10 halves and a byte 558struct LdIssue { 559 uint32_t* pc; int k; 560 __device__ __forceinline__ uint32_t h(const uint8_t* p) { pc[k++] = *(const uint16_t*) p; return 0; } 561 __device__ __forceinline__ uint32_t b(const uint8_t* p) { pc[k++] = *p; return 0; } 562}; 563struct LdTake { 564 const uint32_t* pc; int k; 565 __device__ __forceinline__ uint32_t h(const uint8_t*) { return pc[k++]; } 566 __device__ __forceinline__ uint32_t b(const uint8_t*) { return pc[k++]; } 567}; 568template <class L> __device__ __forceinline__ uint32_t lw(L& l, const uint8_t* p) { 569 const uint32_t lo = l.h(p); 570 const uint32_t hi = l.h(p + 2); 571 return lo | (hi << 16); 572} 573template <int T, class L, int NW> __device__ __forceinline__ void load_unit_l(L& l, const uint8_t* bp, int ib, uint32_t (&w)[NW]) { 574 static_assert(NW >= raw_words(T), "raw words"); 575 if constexpr (T == T_IQ2_XXS || T == T_Q2_0) { 576 w[0] = lw(l, bp + 2 + 8 * ib); w[1] = lw(l, bp + 6 + 8 * ib); w[2] = l.h(bp); 577 } else if constexpr (T == T_IQ2_XS) { 578 w[0] = lw(l, bp + 2 + 8 * ib); w[1] = lw(l, bp + 6 + 8 * ib); 579 const uint32_t d = l.h(bp); const uint32_t s = l.b(bp + 66 + ib); 580 w[2] = d | (s << 16); 581 } else if constexpr (T == T_IQ2_S) { 582 w[0] = lw(l, bp + 2 + 4 * ib); w[1] = lw(l, bp + 34 + 4 * ib); 583 const uint32_t d = l.h(bp); const uint32_t qh = l.b(bp + 66 + ib); const uint32_t sc = l.b(bp + 74 + ib); 584 w[2] = d | (qh << 16) | (sc << 24); 585 } else if constexpr (T == T_IQ3_XXS) { 586 w[0] = lw(l, bp + 2 + 8 * ib); w[1] = lw(l, bp + 6 + 8 * ib); w[2] = lw(l, bp + 66 + 4 * ib); w[3] = l.h(bp); 587 } else if constexpr (T == T_IQ3_S) { 588 w[0] = lw(l, bp + 2 + 8 * ib); w[1] = lw(l, bp + 6 + 8 * ib); w[2] = lw(l, bp + 74 + 4 * ib); 589 const uint32_t d = l.h(bp); const uint32_t qh = l.b(bp + 66 + ib); const uint32_t sc = l.b(bp + 106 + ib / 2); 590 w[3] = d | (qh << 16) | (((sc >> (4 * (ib & 1))) & 15) << 24); 591 } else if constexpr (T == T_IQ4_XS) { 592#pragma unroll 593 for (int k = 0; k < 4; ++k) w[k] = lw(l, bp + 8 + 16 * ib + 4 * k); 594 const uint32_t lo = l.b(bp + 4 + ib / 2); const uint32_t hi = l.h(bp + 2); const uint32_t d = l.h(bp); 595 const uint32_t ls = ((lo >> (4 * (ib & 1))) & 15) | (((hi >> (2 * ib)) & 3) << 4); 596 w[4] = d | (ls << 16); 597 } else { // IQ4_NL 598 static_assert(T == T_IQ4_NL, "the CUDA native formats"); 599#pragma unroll 600 for (int k = 0; k < 4; ++k) w[k] = lw(l, bp + 2 + 4 * k); 601 w[4] = l.h(bp); 602 } 603} 604 605// The sub-block as 32 int8 (q[0..7], natural order) and its scales (s0: values 0-15, s1: 16-31). 606template <int T, int NW> 607__device__ __forceinline__ void convert(const uint32_t (&w)[NW], const uint8_t* grid, const uint32_t (&kv)[4], 608 uint32_t (&q)[8], float& s0, float& s1, const uint4* sgn = nullptr) { 609 if constexpr (T == T_Q4_K || T == T_Q5_K) { 610 const int ib = (int) (w[9] >> 24), sh = 4 * (ib & 1); 611 uint32_t sc, mn; 612 if (ib < 4) { sc = w[9] & 63; mn = (w[9] >> 8) & 63; } 613 else { 614 sc = ((w[9] & 0xF) | (((w[9] >> 8) & 0xC0) >> 2)); // sc[ib + 4] & 15 | (sc[ib - 4] >> 6) << 4 615 mn = (((w[9]) >> 4) & 0xF) | (((w[9] >> 16) & 0xC0) >> 2); // sc[ib + 4] >> 4 | (sc[ib] >> 6) << 4 616 } 617#pragma unroll 618 for (int k = 0; k < 8; ++k) { 619 q[k] = (w[k] >> sh) & 0x0F0F0F0Fu; 620 if constexpr (T == T_Q5_K) q[k] |= ((w[10 + k] >> ib) & 0x01010101u) << 4; 621 } 622 s0 = half_at(w[8]) * (float) sc; 623 s1 = -(half_at(w[8] >> 16) * (float) mn); 624 } else if constexpr (T == T_NVFP4) { 625 // each 16-value group is 8 bytes, split-half (byte j: low nibble value j, high nibble value 8 + j): the low nibbles of w[0], w[1] are values 0-7, their high 626 // nibbles 8-15 (the second group: w[2], w[3]); table16 turns a word's low / high nibbles into 4 int8 each (the doubled E2M1 values, signs included) 627 uint32_t l0, h0, l1, h1, l2, h2, l3, h3; 628 table16(w[0], kv, l0, h0); 629 table16(w[1], kv, l1, h1); 630 table16(w[2], kv, l2, h2); 631 table16(w[3], kv, l3, h3); 632 q[0] = l0; q[1] = l1; q[2] = h0; q[3] = h1; 633 q[4] = l2; q[5] = l3; q[6] = h2; q[7] = h3; 634 s0 = ue4m3_half(w[4]); 635 s1 = ue4m3_half(w[4] >> 8); 636 } else if constexpr (T == T_Q5_0) { // w = d (q - 16): the signed codes (like Q8_0's), no minimum 637#pragma unroll 638 for (int k = 0; k < 4; ++k) { 639 const uint32_t lo = ((w[4] >> (4 * k)) & 0xF) * 0x00204081u, hi = ((w[4] >> (16 + 4 * k)) & 0xF) * 0x00204081u; 640 const uint32_t a = (w[k] & 0x0F0F0F0Fu) | ((lo & 0x01010101u) << 4); 641 const uint32_t b = ((w[k] >> 4) & 0x0F0F0F0Fu) | ((hi & 0x01010101u) << 4); 642 q[k] = ((a | 0x80808080u) - 0x10101010u) ^ 0x80808080u; // a byte a in 0..31 -> the int8 a - 16 643 q[4 + k] = ((b | 0x80808080u) - 0x10101010u) ^ 0x80808080u; 644 } 645 s0 = s1 = half_at(w[5]); 646 } else if constexpr (T == T_Q5_1) { 647#pragma unroll 648 for (int k = 0; k < 4; ++k) { 649 const uint32_t lo = ((w[4] >> (4 * k)) & 0xF) * 0x00204081u, hi = ((w[4] >> (16 + 4 * k)) & 0xF) * 0x00204081u; 650 q[k] = (w[k] & 0x0F0F0F0Fu) | ((lo & 0x01010101u) << 4); 651 q[4 + k] = ((w[k] >> 4) & 0x0F0F0F0Fu) | ((hi & 0x01010101u) << 4); 652 } 653 s0 = half_at(w[5]); 654 s1 = half_at(w[5] >> 16); 655 } else if constexpr (T == T_Q8_0) { 656#pragma unroll 657 for (int k = 0; k < 8; ++k) q[k] = w[k]; 658 s0 = s1 = half_at(w[8]); 659 } else if constexpr (T == T_Q6_K) { // w = d sc (q - 32): the signed codes, an int8 scale per 16 values, no minimum 660 const int r = (int) w[17], sl = 4 * (r >> 1), sh = 2 * r; 661#pragma unroll 662 for (int k = 0; k < 8; ++k) { 663 const uint32_t a = ((w[k] >> sl) & 0x0F0F0F0Fu) | (((w[8 + k] >> sh) & 0x03030303u) << 4); // 0..63 a byte 664 q[k] = ((a | 0x80808080u) - 0x20202020u) ^ 0x80808080u; // the int8 a - 32 665 } 666 const float d = half_at(w[16]); 667 s0 = d * (float) (int8_t) (w[16] >> 16); 668 s1 = d * (float) (int8_t) (w[16] >> 24); 669 } else if constexpr (is_tq(T)) { 670 // (STRATA_PF_FUSED_TQ=1) the 8 steps' 15-bit states, each x = s * 0x9e3779b1 -> codebook points x >> 21 and (x >> 10) & 2047, two 671 // int8 each (tq_lut_i8 in shared memory): 4 weights a step, in weight order, as tq.cuh's tq_lane_to_q8 packs them for MMQ; one scale 672 // d * TQ_LUT_I8_SCALE for the block 673 const uint16_t* lut = (const uint16_t*) grid; 674 uint32_t st[8]; 675 if constexpr (T == T_TQ2_T) { 676 uint32_t prev = w[2]; 677#pragma unroll 678 for (int j = 0; j < 8; ++j) { 679 const uint32_t b = (w[j >> 2] >> (8 * (j & 3))) & 0xFFu; 680 st[j] = (prev << 8) | b; 681 prev = b; 682 } 683 } else { 684 constexpr int K = tq_k(T); 685 const uint64_t lo = (uint64_t) w[0] | ((uint64_t) w[1] << 32), hi = (uint64_t) w[1] | ((uint64_t) w[2] << 32); 686 const int r = (int) w[4]; 687#pragma unroll 688 for (int j = 0; j < 8; ++j) { 689 const int rel = r + (7 - j) * K; // <= 15 + 7 K: inside the 96 bits 690 st[j] = (uint32_t) ((rel <= 48 ? lo >> rel : hi >> (rel - 32)) & 0xFFFFu); 691 } 692 } 693#pragma unroll 694 for (int j = 0; j < 8; ++j) { 695 const uint32_t x = (st[j] & 0x7FFFu) * 0x9e3779b1u; // (TQ_STATE_MASK: 15 state bits) 696 q[j] = (uint32_t) lut[x >> 21] | ((uint32_t) lut[(x >> 10) & 2047u] << 16); 697 } 698 s0 = s1 = half_at(w[3]) * TQ_LUT_I8_SCALE; 699 } else if constexpr (T == T_IQ2_XXS) { 700 const uint2* g = (const uint2*) grid; 701#pragma unroll 702 for (int l = 0; l < 4; ++l) { 703 const uint2 e = g[(w[0] >> (8 * l)) & 255]; 704#if defined(__HIPCC__) 705 signed8t(e.x, e.y, sgn[(w[1] >> (7 * l)) & 127], q[2 * l], q[2 * l + 1]); 706#else 707 signed8(e.x, e.y, unpack_ksigns(w[1] >> (7 * l)), q[2 * l], q[2 * l + 1]); 708#endif 709 } 710 s0 = s1 = half_at(w[2]) * (float) ((w[1] >> 27) | 1) * 0.125f; 711 } else if constexpr (T == T_IQ2_XS) { 712 const uint2* g = (const uint2*) grid; 713#pragma unroll 714 for (int l = 0; l < 4; ++l) { 715 const uint32_t c = (w[l >> 1] >> (16 * (l & 1))) & 0xFFFF; 716 const uint2 e = g[c & 511]; 717#if defined(__HIPCC__) 718 signed8t(e.x, e.y, sgn[(c >> 9) & 127], q[2 * l], q[2 * l + 1]); 719#else 720 signed8(e.x, e.y, unpack_ksigns(c >> 9), q[2 * l], q[2 * l + 1]); 721#endif 722 } 723 const float d = half_at(w[2]); 724 const uint32_t sc = w[2] >> 16; 725 s0 = d * (float) (2 * (sc & 15) + 1) * 0.125f; 726 s1 = d * (float) (2 * ((sc >> 4) & 15) + 1) * 0.125f; 727 } else if constexpr (T == T_IQ2_S) { 728 const uint2* g = (const uint2*) grid; 729 const uint32_t qh = (w[2] >> 16) & 255; 730#pragma unroll 731 for (int l = 0; l < 4; ++l) { 732 const uint2 e = g[((w[0] >> (8 * l)) & 255) | ((qh << (8 - 2 * l)) & 0x300)]; 733#if defined(__HIPCC__) 734 signed8t(e.x, e.y, sgn[(w[1] >> (8 * l)) & 255], q[2 * l], q[2 * l + 1]); 735#else 736 signed8(e.x, e.y, ((w[1] >> (8 * l)) & 255) * 0x01010101u, q[2 * l], q[2 * l + 1]); 737#endif 738 } 739 const float d = half_at(w[2]); 740 const uint32_t sc = w[2] >> 24; 741 s0 = d * (float) (2 * (sc & 15) + 1) * 0.125f; 742 s1 = d * (float) (2 * (sc >> 4) + 1) * 0.125f; 743 } else if constexpr (T == T_IQ3_XXS) { 744 const uint32_t* g = (const uint32_t*) grid; 745#pragma unroll 746 for (int l = 0; l < 4; ++l) { 747 const uint32_t i0 = (w[l >> 1] >> (16 * (l & 1))) & 255, i1 = (w[l >> 1] >> (16 * (l & 1) + 8)) & 255; 748#if defined(__HIPCC__) 749 signed8t(g[i0], g[i1], sgn[(w[2] >> (7 * l)) & 127], q[2 * l], q[2 * l + 1]); 750#else 751 signed8(g[i0], g[i1], unpack_ksigns(w[2] >> (7 * l)), q[2 * l], q[2 * l + 1]); 752#endif 753 } 754 s0 = s1 = half_at(w[3]) * (float) (2 * (w[2] >> 28) + 1) * 0.25f; 755 } else if constexpr (T == T_IQ3_S) { 756 const uint32_t* g = (const uint32_t*) grid; 757 const uint32_t qh = (w[3] >> 16) & 255; 758#pragma unroll 759 for (int l = 0; l < 4; ++l) { 760 const uint32_t i0 = (w[l >> 1] >> (16 * (l & 1))) & 255, i1 = (w[l >> 1] >> (16 * (l & 1) + 8)) & 255; 761#if defined(__HIPCC__) 762 signed8t(g[i0 | ((qh << (8 - 2 * l)) & 256)], g[i1 | ((qh << (7 - 2 * l)) & 256)], 763 sgn[(w[2] >> (8 * l)) & 255], q[2 * l], q[2 * l + 1]); 764#else 765 signed8(g[i0 | ((qh << (8 - 2 * l)) & 256)], g[i1 | ((qh << (7 - 2 * l)) & 256)], 766 ((w[2] >> (8 * l)) & 255) * 0x01010101u, q[2 * l], q[2 * l + 1]); 767#endif 768 } 769 s0 = s1 = half_at(w[3]) * (float) (1 + 2 * (w[3] >> 24)); 770 } else if constexpr (T == T_IQ4_XS || T == T_IQ4_NL) { 771#pragma unroll 772 for (int k = 0; k < 4; ++k) table16(w[k], kv, q[k], q[4 + k]); 773 s0 = s1 = T == T_IQ4_XS ? half_at(w[4]) * (float) ((int) (w[4] >> 16) - 32) : half_at(w[4]); 774 } else { // Q2_0: code - 1 via a byte table {-1, 0, 1, 2} 775#pragma unroll 776 for (int h = 0; h < 4; ++h) { 777 const uint32_t c = (w[h >> 1] >> (16 * (h & 1))) & 0xFFFF; 778 const uint32_t qe = __byte_perm(0x020100FFu, 0x020100FFu, c & 0x7777); 779 const uint32_t qo = __byte_perm(0x020100FFu, 0x020100FFu, (c >> 2) & 0x7777); 780 q[2 * h] = __byte_perm(qe, qo, 0x5140); 781 q[2 * h + 1] = __byte_perm(qe, qo, 0x7362); 782 } 783 s0 = s1 = half_at(w[2]); 784 } 785} 786 787template <int T> __device__ __forceinline__ const void* grid_src() { 788 if constexpr (T == T_IQ2_XXS) return iq2xxs_grid; 789 else if constexpr (T == T_IQ2_XS) return iq2xs_grid; 790 else if constexpr (T == T_IQ2_S) return iq2s_grid; 791 else if constexpr (T == T_IQ3_XXS) return iq3xxs_grid; 792 else if constexpr (T == T_IQ3_S) return iq3s_grid; 793 else if constexpr (is_tq(T)) return tq_lut_i8; // (STRATA_PF_FUSED_TQ=1) 794 else return nullptr; 795} 796#endif 797#if !defined(STRATA_FUSED_DECODE_ONLY) // (tests/cuda/prefill_decode_golden.cu: the decode above only) 798// One work item = (TR routed rows of expert e - one or two of group()'s 64-row tiles -, a block of WR weight rows); 799// a persistent grid walks the batch's items. GU: gate/up rows of WR / 2 features (format WT) against the rows' 800// tokens' activations, SwiGLU, H to int8 per 32 features into `out`. !GU: down rows (format WT) against H, FP32 into 801// `dm`. Warp (wf, wt): weight rows 64 wf.. as four m16 tiles (GU: a tile = 8 features, gate rows as mma rows 0-7 and 802// up rows as 8-15, so a lane holds gate and up of one feature), routed rows 16 wt.. as two n8. 803// NT (STRATA_PF_FUSED_NT=256, sm_80+ opt-in): 256 threads, two blocks a multiprocessor - one block's products run while 804// the other waits at a barrier. WW 2 only (its 2 WR = 256 decode units are the block's threads); a work item is 128 805// weight rows x one 64-row tile. Every output is the same thread's sum in the same order: the same bits. The NT = 256 806// instances take a WideBatch (STRATA_PF_BATCH up to kBatchRoom experts a launch), the default ones Batch. 807// 808// F16 (STRATA_PF_FUSED_F16=1, NT 256 only; changes the output at rounding level): the decoded weights times their 809// scales as F16 and the activations times theirs as F16, m16n8k16 F16 products into FP32 accumulators - no FP32 work 810// per 32 values. The k order inside a 16-value group is permuted the same way for both operands (stored half2 word t 811// holds values 4t, 4t + 1 and word 4 + t values 4t + 2, 4t + 3), so the activations' int8 ldmatrix fragments become 812// the F16 B fragments in registers; the accumulator layout, and so the epilogues, are the int8 kernel's. 813#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750 814// (CUDA, STRATA_PF_FUSED_KQ=1, A1b) a register-lean load of the K-quant gate/up sub-blocks. The row's two decode threads (uj 0 815// and 1, adjacent lanes) read the same 32 qs bytes (Q5_K also the same 32 qh bytes) and each keeps one nibble (one bit) of every 816// byte: here each loads only its half (16 bytes: bytes 16 uj ..) and the two halves are swapped by a shuffle when the stage is 817// decoded (kq_lean_expand), so a thread holds 6 (Q4_K) / 10 (Q5_K) raw words across a stage instead of load_unit's 10 / 18. 818// The words kq_lean_expand gives are load_unit's: convert<WT> and so the arithmetic are unchanged. 819template <int T> __host__ __device__ constexpr int kq_lean_words() { return T == T_Q4_K ? 6 : 10; } 820template <int T> __device__ __forceinline__ void kq_lean_load(const uint8_t* bp, int ib, int uj, uint32_t (&w)[kq_lean_words<T>()]) { 821 constexpr int QS = T == T_Q4_K ? 16 : 48; 822 const uint8_t* q = bp + QS + 32 * (ib >> 1) + 16 * uj; 823#pragma unroll 824 for (int k = 0; k < 4; ++k) w[k] = ld32a(q + 4 * k); 825 w[4] = ld32a(bp); 826 const uint8_t* sc = bp + 4; 827 w[5] = ib < 4 ? (uint32_t) sc[ib] | ((uint32_t) sc[ib + 4] << 8) 828 : (uint32_t) sc[ib + 4] | ((uint32_t) sc[ib - 4] << 8) | ((uint32_t) sc[ib] << 16); 829 w[5] |= (uint32_t) ib << 24; 830 if constexpr (T == T_Q5_K) { 831#pragma unroll 832 for (int k = 0; k < 4; ++k) w[6 + k] = ld32a(bp + 16 + 16 * uj + 4 * k); // qh 833 } 834} 835// load_unit's words from the lean ones and the partner thread's (lane ^ 1: every lane of the warp calls this) 836template <int T> __device__ __forceinline__ void kq_lean_expand(const uint32_t (&w)[kq_lean_words<T>()], int uj, uint32_t (&f)[raw_words(T)]) { 837#pragma unroll 838 for (int k = 0; k < 4; ++k) { 839 const uint32_t o = __shfl_xor_sync(0xffffffffu, w[k], 1); 840 f[k] = uj ? o : w[k]; 841 f[4 + k] = uj ? w[k] : o; 842 } 843 f[8] = w[4]; 844 f[9] = w[5]; 845 if constexpr (T == T_Q5_K) { 846#pragma unroll 847 for (int k = 0; k < 4; ++k) { 848 const uint32_t o = __shfl_xor_sync(0xffffffffu, w[6 + k], 1); 849 f[10 + k] = uj ? o : w[6 + k]; 850 f[14 + k] = uj ? w[6 + k] : o; 851 } 852 } 853} 854// (CUDA, STRATA_PF_FUSED_Q6K=1) the same for the Q6_K gate/up: the row's two decode threads take sub-blocks 2 k and 2 k + 1 of a stage, 855// whose low nibbles are different ql bytes (each loads its own 32) but whose two high bits come from the same 32 qh bytes: each loads 856// half of them (bytes 16 uj ..) and the halves are swapped when the stage is decoded. 14 raw words across a stage instead of 18; the 857// words kq_lean_expand gives are load_unit<T_Q6_K>'s, so convert and the arithmetic are unchanged. (Explicit specializations: the 858// Q4_K / Q5_K forms above are not touched.) 859template <> __host__ __device__ constexpr int kq_lean_words<T_Q6_K>() { return 14; } 860template <> __device__ __forceinline__ void kq_lean_load<T_Q6_K>(const uint8_t* bp, int ib, int uj, uint32_t (&w)[14]) { 861 const int n = ib >> 2, r = ib & 3; 862#pragma unroll 863 for (int k = 0; k < 8; ++k) w[k] = ld32(bp + 64 * n + 32 * (r & 1) + 4 * k); 864#pragma unroll 865 for (int k = 0; k < 4; ++k) w[8 + k] = ld32(bp + 128 + 32 * n + 16 * uj + 4 * k); 866 w[12] = ld16(bp + 208) | (ld16(bp + 192 + 8 * n + 2 * r) << 16); 867 w[13] = (uint32_t) r; 868} 869template <> __device__ __forceinline__ void kq_lean_expand<T_Q6_K>(const uint32_t (&w)[14], int uj, uint32_t (&f)[raw_words(T_Q6_K)]) { 870#pragma unroll 871 for (int k = 0; k < 8; ++k) f[k] = w[k]; 872#pragma unroll 873 for (int k = 0; k < 4; ++k) { 874 const uint32_t o = __shfl_xor_sync(0xffffffffu, w[8 + k], 1); 875 f[8 + k] = uj ? o : w[8 + k]; 876 f[12 + k] = uj ? w[8 + k] : o; 877 } 878 f[16] = w[12]; 879 f[17] = w[13]; 880} 881#endif 882 883template <int NT> using BatchOf = std::conditional_t<NT == THREADS, Batch, WideBatch>; 884template <int WT, bool GU, int WW, int NT = THREADS, bool F16 = false> 885__global__ void __launch_bounds__(NT, NT == THREADS ? 1 : 2) 886native_kernel(const BatchOf<NT> b, const NativeGeom geo, const Tables tb, const uint8_t* __restrict__ act, 887 const int32_t* __restrict__ src, uint8_t* __restrict__ out, float* __restrict__ dm) { 888#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750 889 constexpr int TR = tile_rows(WW, NT), WR = weight_rows(WW); 890 static_assert(2 * WR <= NT, "every decode unit is a thread of the block"); 891 constexpr int WLDX = F16 ? WLDH : WLD; 892 constexpr int WT_BYTES = WR * WLDX, WS_FLOATS = F16 ? 0 : WR * 4, ACT_STAGE = TR * AB; 893 constexpr bool TWO_PASS = NT != THREADS; // the two-pass loads (load_unit_l) only in the opt-in NT = 256 kernels 894 static_assert(!(TWO_PASS && WT == T_Q5_0), "the NT = 256 kernels have no Q5_0 form"); 895 static_assert(!(TWO_PASS && (has_min(WT) || WT == T_Q8_0 || WT == T_Q6_K || is_tq(WT))), "the NT = 256 kernels have no K-quant / Q5_1 / Q8_0 / Q6_K / trellis form"); 896 constexpr bool SWT = sw_inst(WT, GU, WW, NT, F16) && ASTAGES_DEV == ASTAGES; // weight staging (see sw_on) 897 // (A1b) the K-quant gate/up's lean loads (kq_lean_load); a staged K-quant DOWN (Q5_K / Q6_K, K = 512) gathers the same lean words from its slot's windows (sw_q5 / sw_q6) 898 constexpr bool KLEAN = (GU && (WT == T_Q4_K || WT == T_Q5_K || WT == T_Q6_K)) || (SWT && !GU && (WT == T_Q5_K || WT == T_Q6_K)); 899 constexpr int RAW = KLEAN ? kq_lean_words<WT>() : raw_words(WT) > 5 ? raw_words(WT) : 5; // the raw words of a sub-block: 5, Q5_0's 6 900 constexpr int NS = (GU ? GU_ROWS_K : D_ROWS_K) / 64; // 64-value stages along K 901 constexpr int NFB = (GU ? GU_ROWS_N : D_ROWS_N) / WR; // weight-row blocks per tile 902 static_assert(GU_ROWS_N % WR == 0 && D_ROWS_N % WR == 0, "the weight rows of a product are whole work items (2 n_ff and n_embd are multiples of 64 WW)"); 903 constexpr int ACT_LD = NS * AB; // bytes per activation row: 3200 (a token), 800 (a row's H) 904 constexpr int BS = block_bytes(WT); 905 constexpr bool K16 = per16(WT); 906 // weight staging (see sw_on): this instance's slots, its activation depth, and a ring at the end of shared memory 907 constexpr int AST = SWT ? kSwAst : ASTAGES_DEV; 908 constexpr int SW_SPB = sw_spb(WT, GU), SW_SB = sw_slot_bytes(WT, GU), SW_NCH = sw_chunks(WT, GU), SW_ROW = 16 * SW_NCH; 909 static_assert(!SWT || (NS % SW_SPB == 0 && SW_SPB >= AST - 1), "staging: whole slots a row, and deep enough for the ring (a slot is read until two stages after the next copy is issued)"); 910 static_assert(!SWT || smem_bytes(WT, WW, ASTAGES, NT, F16, GU) <= kSmemOptIn, "staging: the ring does not fit the shared memory a block can get"); 911 extern __shared__ __align__(16) uint8_t smem[]; 912 uint8_t* wt = smem; // [2][WR][WLD] decoded int8 weights 913 float* ws = (float*) (smem + 2 * WT_BYTES); // [2][WR][4] their scales per 16 values 914 uint8_t* stages = (uint8_t*) (ws + 2 * WS_FLOATS); // [AST][TR][AB] activations 915 int* srow = (int*) (stages + AST * ACT_STAGE); // [TR] the activation row of each tile row 916 uint8_t* sgrid = (uint8_t*) (srow + TR); // the codebook 917 uint8_t* sraw = sgrid + grid_bytes(WT); // staged: [2][WR][SW_ROW] the slots' raw bytes (16-byte aligned: every size before it is a multiple of 16) 918 919 const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5, g = lane >> 2, tig = lane & 3; 920 const int wf = warp % WW, nb0 = 16 * (warp / WW); 921 if constexpr (grid_bytes(WT) > 0) { 922 const uint32_t* gs = (const uint32_t*) grid_src<WT>(); 923 for (int i = tid; i < grid_bytes(WT) / 4; i += NT) ((uint32_t*) sgrid)[i] = gs[i]; 924 } 925 uint32_t kv[4] = {0, 0, 0, 0}; 926 if constexpr (WT == T_IQ4_XS || WT == T_IQ4_NL) { 927#pragma unroll 928 for (int k = 0; k < 16; ++k) kv[k >> 2] |= (uint32_t) (uint8_t) kvalues_iq4nl[k] << (8 * (k & 3)); 929 } 930 if constexpr (WT == T_NVFP4) { // the doubled E2M1 values 931#pragma unroll 932 for (int k = 0; k < 16; ++k) kv[k >> 2] |= (uint32_t) (uint8_t) kvalues_fp4[k] << (8 * (k & 3)); 933 } 934 // Local weight rows: m tile i of warp wf is rows 64 wf + 16 i.. (mma rows g and g + 8: rows +g and +8+g). For 935 // gate/up a tile is 8 features, its gate rows then its up rows, so a lane holds gate and up of one feature. The 936 // fragments come by ldmatrix: lane l gives the address of row (l & 7) of matrix l >> 3 - A: {rows 0-7, rows 8-15} 937 // x {bytes 0-15, 16-31} of a 32-value chunk; B: {tokens nb0 + 0-7, + 8-15} x {bytes 0-15, 16-31}. 938 const int lm = lane >> 3, l8 = lane & 7; 939 const int a_off = (64 * wf + 8 * (lm & 1) + l8) * WLDX + 16 * (lm >> 1); 940 const int b_off = (nb0 + 8 * (lm >> 1) + l8) * AB + 16 * (lm & 1); 941 // this thread's decode unit (the first 2 WR threads): local weight row ur, sub-block uj of each stage 942 const bool dec = tid < 2 * WR; 943 const int ur = dec ? tid >> 1 : 0, uj = tid & 1; 944 const int t0 = tb.ts[b.e0], nwork = (tb.ts[b.e1] - t0) * NFB; 945 for (int w = blockIdx.x; w < nwork; w += gridDim.x) { 946 const int2 tl = tb.tiles[t0 + w / NFB]; 947 const int fb = w % NFB, e = tl.x, row0 = tl.y; 948 if ((row0 - tb.off[e]) % TR != 0) continue; // a 64-row tile inside an earlier item's TR rows 949 const int nrows = min(TR, tb.off[e + 1] - row0); 950 const uint8_t* blob = b.blob[e - b.e0]; 951 const int rbase = fb * WR; 952 // NVFP4: the expert's weight_scale_2 values, the blob's 16-byte tail {s_gate, s_up, s_down, 0} behind the down matrix (cpu::NativeFmt::tail_off = down_off + d_row n_embd), go on the 953 // FP32 products: gate by s_gate and up by s_up before the SwiGLU, down by s_down as the rows are written. Each scale stays on its own projection (NVFP4.md, "Where the global 954 // scales go"); H and the int8 activations keep FP32 scales, so the ~1e-4 values never meet an FP16 range. (The tail is read with 16-bit loads: the blob is 2-byte aligned.) 955 float t_gate = 1.0f, t_up = 1.0f, t_down = 1.0f; 956 if constexpr (WT == T_NVFP4) { 957 const uint8_t* tl = blob + geo.down_off + geo.d_row * (size_t) D_ROWS_N; 958 if constexpr (GU) { t_gate = __uint_as_float(ld32(tl)); t_up = __uint_as_float(ld32(tl + 4)); } 959 else t_down = __uint_as_float(ld32(tl + 8)); 960 } 961 // local row ur: gate/up - feature (WR / 2) fb + 8 (ur >> 4) + (ur & 7), its up row when bit 3 is set 962 const uint8_t* wrow = GU ? blob + ((ur & 8) ? geo.up_off : 0) + 963 (size_t) (fb * (WR / 2) + 8 * (ur >> 4) + (ur & 7)) * geo.gu_row 964 : blob + geo.down_off + (size_t) (rbase + ur) * geo.d_row; 965 // gate/up in Q2_0 (64-value blocks): a block per stage, as the down rows read theirs; in Q8_0 (32-value blocks, CUDA 966 // STRATA_PF_FUSED_Q8GU=1): two blocks a stage, as the Q8_0 down rows read theirs 967 constexpr bool GU256 = (GU || ((WT == T_Q4_K || WT == T_Q5_K || WT == T_Q6_K) && D_ROWS_K % 256 == 0)) && WT != T_Q2_0 && WT != T_Q8_0 && WT != T_NVFP4 && !is_tq(WT); // (a K-quant DOWN walks its 256-value super-blocks too, when n_ff is a multiple of 256: Qwen3.6's 512) 968 constexpr bool B128 = is_tq(WT); // (CUDA, STRATA_PF_FUSED_TQ=1) 128-value blocks, gate/up and down: a block per two stages, sub-blocks 0-3 969 auto unit = [&](int s) -> const uint8_t* { // the block of stage s's sub-block 970 if (B128) return wrow + (s >> 1) * BS; 971 if (GU256) return wrow + (s >> 2) * BS; 972 return (WT == T_IQ4_NL || WT == T_Q5_0 || WT == T_Q5_1 || WT == T_Q8_0) ? wrow + (2 * s + uj) * BS : wrow + s * BS; // 32-value blocks: two a stage 973 }; 974 auto sub = [&](int s) { return B128 ? 2 * (s & 1) + uj : GU256 ? 2 * (s & 3) + uj : uj; }; 975 // (staging) slot j of every weight row of the item into ring slot j & 1: each row's slot is the 16-byte chunks of the aligned window around its bytes (a 976 // chunk that starts past the slot's end is not copied; the others stay inside the aligned 16 bytes of a valid byte) 977 auto sw_copy = [&](int j) { 978 if constexpr (SWT) { 979 if (j >= NS / SW_SPB) return; 980 uint8_t* d = sraw + (j & 1) * (WR * SW_ROW); 981 for (int c = tid; c < WR * SW_NCH; c += NT) { 982 const int rr = c / SW_NCH, k = c - SW_NCH * rr; // weight row rr of the item (the numbering of ur above) 983 const uint8_t* rp = GU ? blob + ((rr & 8) ? geo.up_off : 0) + (size_t) (fb * (WR / 2) + 8 * (rr >> 4) + (rr & 7)) * geo.gu_row 984 : blob + geo.down_off + (size_t) (rbase + rr) * geo.d_row; 985 if constexpr (WT == T_Q6_K) { // slot j = half n of super-block j / 2: three windows, chunks 0-4 ql (64 B), 5-7 qh (32 B), 8-9 scales + d (the bytes up to the end of d) 986 const uint8_t* bb = rp + (size_t) (j >> 1) * BS; 987 const int n = j & 1; 988 const uint8_t *a, *e; 989 int dk; 990 if (k < 5) { a = bb + 64 * n; e = a + 64; dk = k; } 991 else if (k < 8) { a = bb + 128 + 32 * n; e = a + 32; dk = k - 5; } 992 else { a = bb + 192 + 8 * n; e = bb + 210; dk = k - 8; } 993 const uint8_t* cs = (const uint8_t*) ((uintptr_t) a & ~(uintptr_t) 15) + 16 * dk; 994 if (cs < e) cp16(d + rr * SW_ROW + 16 * k, cs); 995 } else if constexpr (WT == T_Q5_K) { // (down) slot j = half n of super-block j / 2: chunks 0-4 qs of the two stages (64 B at 48 + 64 n), 5-7 qh (32 B at 16), 8-9 d, dmin, scales (16 B at 0) 996 const uint8_t* bb = rp + (size_t) (j >> 1) * BS; 997 const int n = j & 1; 998 const uint8_t *a, *e; 999 int dk; 1000 if (k < 5) { a = bb + 48 + 64 * n; e = a + 64; dk = k; } 1001 else if (k < 8) { a = bb + 16; e = a + 32; dk = k - 5; } 1002 else { a = bb; e = a + 16; dk = k - 8; } 1003 const uint8_t* cs = (const uint8_t*) ((uintptr_t) a & ~(uintptr_t) 15) + 16 * dk; 1004 if (cs < e) cp16(d + rr * SW_ROW + 16 * k, cs); 1005 } else { // (the S1 form, unchanged) 1006 const uint8_t* a = rp + (size_t) j * SW_SB; 1007 const uint8_t* cs = (const uint8_t*) ((uintptr_t) a & ~(uintptr_t) 15) + 16 * k; 1008 if (cs < a + SW_SB) cp16(d + rr * SW_ROW + 16 * k, cs); 1009 } 1010 } 1011 } 1012 }; 1013 // (staging) the shared-memory address that stands for unit(s): the same bytes at the same offset inside the window 1014 auto sw_unit = [&](int s) -> const uint8_t* { 1015 static_assert(!SWT || GU256 || sw_b32(WT), "staging: the gate/up i-quants' 256-value blocks and the 32-value-block formats (IQ4_NL, Q5_0, Q8_0) only"); 1016 const int j = s / SW_SPB, si = s - SW_SPB * j; 1017 const uint8_t* p0 = sraw + (j & 1) * (WR * SW_ROW) + ur * SW_ROW + ((uintptr_t) (wrow + (size_t) j * SW_SB) & 15); 1018 if constexpr (GU256) return p0; 1019 else return p0 + (2 * si + uj) * BS; // 32-value blocks (IQ4_NL / Q5_0 / Q8_0, gate/up or down): two a stage 1020 }; 1021 // (staging, Q6_K gate/up) kq_lean_load<T_Q6_K>'s 14 words from the three gathered windows: the same bytes at the same offsets inside each window (ql: bb + 64 n + 32 (r & 1) + ..., qh: 1022 // bb + 128 + 32 n + 16 uj + ..., scales / d: bb + 192 + 8 n + 2 r and bb + 208), bb = the slot's super-block 1023 auto sw_q6 = [&](int s, uint32_t (&w)[RAW]) { 1024 if constexpr (SWT && WT == T_Q6_K) { 1025 const int j = s >> 1, n = j & 1, r = sub(s) & 3; 1026 const uint8_t* slot = sraw + (j & 1) * (WR * SW_ROW) + ur * SW_ROW; 1027 const uint8_t* bb = wrow + (size_t) (j >> 1) * BS; 1028 const uint8_t* pa = slot + ((uintptr_t) (bb + 64 * n) & 15); 1029 const uint8_t* pb = slot + 80 + ((uintptr_t) (bb + 128 + 32 * n) & 15); 1030 const uint8_t* pq = slot + 128 + ((uintptr_t) (bb + 192 + 8 * n) & 15); 1031#pragma unroll 1032 for (int k = 0; k < 8; ++k) w[k] = ld32(pa + 32 * (r & 1) + 4 * k); 1033#pragma unroll 1034 for (int k = 0; k < 4; ++k) w[8 + k] = ld32(pb + 16 * uj + 4 * k); 1035 w[12] = ld16(pq + 16 - 8 * n) | (ld16(pq + 2 * r) << 16); 1036 w[13] = (uint32_t) r; 1037 } 1038 }; 1039 // (staging, Q5_K down) kq_lean_load<T_Q5_K>'s 10 words from the three windows: the same bytes at the same offsets inside each (qs: bb + 48 + 32 (s & 3) + 16 uj, in the slot's 1040 // window from bb + 48 + 64 n that is 32 (s & 1) + 16 uj; qh: bb + 16 + 16 uj; d / dmin / scales: bb, bb + 4), bb = the slot's super-block 1041 auto sw_q5 = [&](int s, uint32_t (&w)[RAW]) { 1042 if constexpr (SWT && WT == T_Q5_K) { 1043 const int j = s >> 1, n = j & 1, ib = sub(s); 1044 const uint8_t* slot = sraw + (j & 1) * (WR * SW_ROW) + ur * SW_ROW; 1045 const uint8_t* bb = wrow + (size_t) (j >> 1) * BS; 1046 const uint8_t* pq = slot + ((uintptr_t) (bb + 48 + 64 * n) & 15) + 32 * (s & 1) + 16 * uj; 1047 const uint8_t* ph = slot + 80 + ((uintptr_t) (bb + 16) & 15) + 16 * uj; 1048 const uint8_t* pd = slot + 128 + ((uintptr_t) bb & 15); 1049#pragma unroll 1050 for (int k = 0; k < 4; ++k) w[k] = ld32a(pq + 4 * k); 1051 w[4] = ld32a(pd); 1052 const uint8_t* sc = pd + 4; 1053 w[5] = ib < 4 ? (uint32_t) sc[ib] | ((uint32_t) sc[ib + 4] << 8) 1054 : (uint32_t) sc[ib + 4] | ((uint32_t) sc[ib - 4] << 8) | ((uint32_t) sc[ib] << 16); 1055 w[5] |= (uint32_t) ib << 24; 1056#pragma unroll 1057 for (int k = 0; k < 4; ++k) w[6 + k] = ld32a(ph + 4 * k); 1058 } 1059 }; 1060 auto load_sw = [&](int s, uint32_t (&w)[RAW]) { 1061 if constexpr (SWT) { 1062 if constexpr (WT == T_Q5_K) sw_q5(s, w); 1063 else if constexpr (WT == T_Q6_K) sw_q6(s, w); 1064 else if constexpr (WT == T_Q4_K && GU) kq_lean_load<WT>(sw_unit(s), sub(s), uj, w); // (K-quant gate/up: the lean words from the slot, as the unstaged path reads them from the block) 1065 else load_unit<WT>(sw_unit(s), sub(s), w); 1066 } 1067 }; 1068 // the weight bytes into L2 ahead of the register loads (one stage ahead only - less than a DRAM round trip): 1069 // gate/up PF super-blocks ahead (the row's two threads take a block's first and last line), down the whole row 1070 // slice at once (180 or 360 bytes a row) 1071 constexpr int PF = 2; 1072 constexpr int SBB = WT == T_Q2_0 || WT == T_NVFP4 ? 4 * BS : WT == T_Q8_0 ? 8 * BS : is_tq(WT) ? 2 * BS : BS; // bytes per 256 values of a row 1073 auto prefetch_sb = [&](int sb) { 1074 if (dec && sb < GU_ROWS_K / 256) pf_l2(wrow + sb * SBB + (uj ? SBB - 1 : 0)); 1075 }; 1076 if constexpr (SWT) { // staged: the weight bytes come by cp.async below, no L2 prefetch 1077 } else if (GU) { 1078#pragma unroll 1079 for (int sb = 0; sb < PF; ++sb) prefetch_sb(sb); 1080 } else if (dec) { 1081 for (size_t o = 128 * (size_t) uj; o < geo.d_row + 127; o += 256) pf_l2(wrow + min(o, geo.d_row - 1)); 1082 } 1083 __syncthreads(); // the previous item is done with the buffers 1084 if (tid < TR) srow[tid] = tid < nrows ? (GU ? src[row0 + tid] : row0 + tid) : -1; 1085 __syncthreads(); // srow 1086 // the rows past the item's end are not loaded: a warp entirely past it skips the products, and the columns 1087 // of a partial one are never written (no reduction mixes columns) 1088 auto load_act = [&](int s) { 1089 uint8_t* st = stages + (s % AST) * ACT_STAGE; 1090 for (int c = tid; c < TR * 5; c += NT) { 1091 const int r = c / 5, q = c % 5; 1092 if (r < nrows) cp16(st + r * AB + q * 16, act + (size_t) srow[r] * ACT_LD + s * AB + q * 16); 1093 } 1094 }; 1095 auto put = [&](const uint32_t (&raw)[RAW], int buf) { 1096 if (!dec) return; 1097 uint32_t q[8]; 1098 float s0, s1; 1099 if constexpr (KLEAN) { // load_unit's words back from the lean ones (a shuffle with the row's other thread) 1100 uint32_t f[raw_words(WT)]; 1101 kq_lean_expand<WT>(raw, uj, f); 1102 convert<WT>(f, sgrid, kv, q, s0, s1); 1103 } else 1104 convert<WT>(raw, sgrid, kv, q, s0, s1); 1105 if constexpr (F16) { // two 16-value groups of 8 half2 words (the permuted order above) 1106 uint4* d = (uint4*) (wt + buf * WT_BYTES + ur * WLDX + 64 * uj); 1107#pragma unroll 1108 for (int k = 0; k < 2; ++k) { 1109 const __half2 sc = __float2half2_rn(k ? s1 : s0); 1110 uint32_t lo[4], hi[4]; 1111#pragma unroll 1112 for (int w = 0; w < 4; ++w) i8x4_h2(q[4 * k + w], sc, lo[w], hi[w]); 1113 d[2 * k] = make_uint4(lo[0], lo[1], lo[2], lo[3]); 1114 d[2 * k + 1] = make_uint4(hi[0], hi[1], hi[2], hi[3]); 1115 } 1116 } else { 1117 uint4* d = (uint4*) (wt + buf * WT_BYTES + ur * WLD + 32 * uj); 1118 d[0] = make_uint4(q[0], q[1], q[2], q[3]); 1119 d[1] = make_uint4(q[4], q[5], q[6], q[7]); 1120 *(float2*) (ws + buf * WS_FLOATS + ur * 4 + 2 * uj) = make_float2(s0, s1); 1121 } 1122 }; 1123#pragma unroll 1124 for (int s = 0; s < AST - 1; ++s) { 1125 if (s < NS) load_act(s); 1126 if constexpr (SWT) { if (s < 2) sw_copy(s); } // slots 0 and 1 go with the first two activation groups 1127 cp_commit(); 1128 } 1129 if constexpr (SWT) { // slot 0 in; with two-stage slots slot 1 too (its first stage, 2, is read at the end of the first iteration, before any later wait) 1130 if constexpr (SW_SPB == 2) cp_wait<0>(); else cp_wait<AST - 2>(); 1131 __syncthreads(); 1132 } 1133 uint32_t raw[RAW] = {}; 1134 uint32_t pc[NP]; // stage s + 2's pieces in flight (load_unit_l) 1135 // (the two-pass loads exist for the NT = 256 instances' formats only: no Q5_0 instance takes them) 1136 auto issue = [&](int s) { if constexpr (TWO_PASS) { LdIssue l{pc, 0}; uint32_t w[5]; load_unit_l<WT>(l, unit(s), sub(s), w); } }; 1137 auto take = [&](int s) { if constexpr (TWO_PASS) { LdTake l{pc, 0}; load_unit_l<WT>(l, unit(s), sub(s), raw); } }; 1138 if constexpr (SWT) { if (dec) load_sw(0, raw); } else 1139 if constexpr (KLEAN) { if (dec) kq_lean_load<WT>(unit(0), sub(0), uj, raw); } else 1140 if (dec) load_unit<WT>(unit(0), sub(0), raw); 1141 if (dec && TWO_PASS && 2 < NS) issue(2); 1142 put(raw, 0); 1143 if constexpr (SWT) { if (dec) load_sw(1, raw); } else 1144 if constexpr (KLEAN) { if (dec) kq_lean_load<WT>(unit(1), sub(1), uj, raw); } else 1145 if (dec) load_unit<WT>(unit(1), sub(1), raw); 1146 // a warp whose rows are all past the tile's end only takes part in the loads 1147 const bool on0 = nb0 < nrows, on1 = nb0 + 8 < nrows; 1148 float acc[4][2][4]; 1149#pragma unroll 1150 for (int i = 0; i < 4; ++i) 1151#pragma unroll 1152 for (int n = 0; n < 2; ++n) 1153#pragma unroll 1154 for (int q = 0; q < 4; ++q) acc[i][n][q] = 0.0f; 1155 for (int s = 0; s < NS; ++s) { 1156 cp_wait<AST - 2>(); 1157 __syncthreads(); 1158 if (s + AST - 1 < NS) load_act(s + AST - 1); 1159 if constexpr (SWT) { if (s % SW_SPB == SW_SPB - 2) sw_copy(s / SW_SPB + 2); } // the ring slot's last reader is behind this stage's barrier; the copy is waited for two slots on 1160 cp_commit(); 1161 if (!SWT && GU && (s & 3) == 0) prefetch_sb((s >> 2) + PF); 1162 if (on0) { 1163 const uint8_t* W = wt + (s & 1) * WT_BYTES; 1164 const float* S = ws + (s & 1) * WS_FLOATS; 1165 const uint8_t* sa = stages + (s % AST) * ACT_STAGE; 1166 // the stage's scales: per weight row 4 (per 16 values), per routed row 2 (per 32) 1167 float2 dx[2][2]; // [n8 tile][column 2 tig + cc] 1168#pragma unroll 1169 for (int n = 0; n < 2; ++n) 1170#pragma unroll 1171 for (int cc = 0; cc < 2; ++cc) dx[n][cc] = F16 ? float2{} : *(const float2*) (sa + (nb0 + 8 * n + 2 * tig + cc) * AB + 64); 1172 // (the formats with a minimum, CUDA K-quant opt-in) the activation codes' sums per 32 values, bytes 72-79 1173 float2 sx[2][2]; 1174 if constexpr (has_min(WT)) { 1175#pragma unroll 1176 for (int n = 0; n < 2; ++n) 1177#pragma unroll 1178 for (int cc = 0; cc < 2; ++cc) { // (A1b) d_x times the sum, once a stage: the minimum term is then one FMA 1179 const float2 sm = *(const float2*) (sa + (nb0 + 8 * n + 2 * tig + cc) * AB + 72); 1180 sx[n][cc] = make_float2(dx[n][cc].x * sm.x, dx[n][cc].y * sm.y); 1181 } 1182 } 1183 if constexpr (F16) { 1184#pragma unroll 1185 for (int h = 0; h < 2; ++h) { 1186 uint32_t bq[4]; // {n0 k 0-15, n0 16-31, n1 0-15, n1 16-31}: token nb0 + 8 n + g 1187 ldsm4(bq, sa + b_off + 32 * h); 1188 uint32_t bh[2][2][2]; // [n][16-value group][b0, b1] 1189#pragma unroll 1190 for (int n = 0; n < 2; ++n) { 1191 const __half2 dxh = __float2half2_rn(*(const float*) (sa + (nb0 + 8 * n + g) * AB + 64 + 4 * h)); 1192#pragma unroll 1193 for (int k = 0; k < 2; ++k) i8x4_h2(bq[2 * n + k], dxh, bh[n][k][0], bh[n][k][1]); 1194 } 1195#pragma unroll 1196 for (int i = 0; i < 4; ++i) 1197#pragma unroll 1198 for (int k = 0; k < 2; ++k) { 1199 uint32_t a[4]; 1200 ldsm4(a, W + a_off + 16 * i * WLDX + 64 * h + 32 * k); 1201 mma16h(acc[i][0], a, bh[0][k][0], bh[0][k][1]); 1202 if (on1) mma16h(acc[i][1], a, bh[1][k][0], bh[1][k][1]); 1203 } 1204 } 1205 } else 1206 if constexpr (WT == T_Q2_0 && T75_DEV) { 1207 // Turing, Q2_0: one d per 64 values, so the stage's two halves share their weight scale - each row's dot 1208 // is d (dx_0 dot_0 + dx_1 dot_1): the activation scales per half, then d once 1209 uint32_t bq0[4], bq1[4]; // the two halves' {n0 0-15, n0 16-31, n1 0-15, n1 16-31} 1210 ldsm4(bq0, sa + b_off); 1211 ldsm4(bq1, sa + b_off + 32); 1212#pragma unroll 1213 for (int i = 0; i < 4; ++i) { 1214 uint32_t a0[4], a1[4]; 1215 ldsm4(a0, W + a_off + 16 * i * WLD); 1216 ldsm4(a1, W + a_off + 16 * i * WLD + 32); 1217 const float wa = S[(64 * wf + 16 * i + g) * 4], wb = S[(64 * wf + 16 * i + 8 + g) * 4]; 1218#pragma unroll 1219 for (int n = 0; n < 2; ++n) { 1220 if (n == 1 && !on1) break; 1221 int d0[4], d1[4]; 1222 mma32(d0, a0, bq0[2 * n], bq0[2 * n + 1]); 1223 mma32(d1, a1, bq1[2 * n], bq1[2 * n + 1]); 1224#pragma unroll 1225 for (int q = 0; q < 4; ++q) { 1226 const float t = fmaf(dx[n][q & 1].y, dotf(d1[q]), dx[n][q & 1].x * dotf(d0[q])); 1227 acc[i][n][q] = fmaf(q < 2 ? wa : wb, t, acc[i][n][q]); 1228 } 1229 } 1230 } 1231 } else 1232#pragma unroll 1233 for (int h = 0; h < 2; ++h) { // the stage's two 32-value halves 1234 uint32_t bq[4]; // {n0 bytes 0-15, n0 16-31, n1 0-15, n1 16-31} 1235 ldsm4(bq, sa + b_off + 32 * h); 1236#pragma unroll 1237 for (int i = 0; i < 4; ++i) { 1238 uint32_t a[4]; // {rows g, g + 8} x {bytes 0-15, 16-31} 1239 ldsm4(a, W + a_off + 16 * i * WLD + 32 * h); 1240 const float2 swa = *(const float2*) (S + (64 * wf + 16 * i + g) * 4 + 2 * h); 1241 const float2 swb = *(const float2*) (S + (64 * wf + 16 * i + 8 + g) * 4 + 2 * h); 1242 const float wa0 = swa.x, wa1 = swa.y, wb0 = swb.x, wb1 = swb.y; 1243#pragma unroll 1244 for (int n = 0; n < 2; ++n) { 1245 if (n == 1 && !on1) break; 1246 if constexpr (K16) { 1247 // a scale per 16 weights: the two halves' dots, each times its scale, then d_x 1248 int d0[4], d1[4]; 1249 mma16(d0, a[0], a[1], bq[2 * n]); 1250 mma16(d1, a[2], a[3], bq[2 * n + 1]); 1251#pragma unroll 1252 for (int q = 0; q < 4; ++q) { 1253 const float v = fmaf(q < 2 ? wa1 : wb1, dotf(d1[q]), (q < 2 ? wa0 : wb0) * dotf(d0[q])); 1254 acc[i][n][q] = fmaf(h ? dx[n][q & 1].y : dx[n][q & 1].x, v, acc[i][n][q]); 1255 } 1256 } else { 1257 int d[4]; 1258 mma32(d, a, bq[2 * n], bq[2 * n + 1]); 1259#pragma unroll 1260 for (int q = 0; q < 4; ++q) { 1261 const float p = (q < 2 ? wa0 : wb0) * (h ? dx[n][q & 1].y : dx[n][q & 1].x); 1262 acc[i][n][q] = fmaf(p, dotf(d[q]), acc[i][n][q]); 1263 if constexpr (has_min(WT)) // w = s0 q + s1: + s1 d_x (sum of the codes), as native_w11_kernel 1264 acc[i][n][q] = fmaf(q < 2 ? wa1 : wb1, h ? sx[n][q & 1].y : sx[n][q & 1].x, acc[i][n][q]); 1265 } 1266 } 1267 } 1268 } 1269 } 1270 } 1271 // the next stage's weights (into the other buffer, read after the next barrier). (Decoding them before 1272 // this stage's products instead was slower on the RTX 5070: a layer of 2048 / 3584 / 8192 tokens, IQ2_S 1273 // 5.2 / 9.5 / 17.5 ms -> 6.0 / 10.2 / 18.3.) 1274 if (s + 1 < NS) { 1275 put(raw, (s + 1) & 1); 1276 if (dec && s + 2 < NS) { 1277 if constexpr (TWO_PASS) { 1278 take(s + 2); 1279 if (s + 3 < NS) issue(s + 3); 1280 } else { 1281 if constexpr (SWT) load_sw(s + 2, raw); else 1282 if constexpr (KLEAN) kq_lean_load<WT>(unit(s + 2), sub(s + 2), uj, raw); else 1283 load_unit<WT>(unit(s + 2), sub(s + 2), raw); 1284 } 1285 } 1286 } 1287 } 1288 if (!on0) continue; 1289 if (GU) { 1290 // SwiGLU of feature (WR / 2) fb + 32 wf + 8 i + g (lane g of m tile i: rows 64 wf + 16 i + g and + 8), tile 1291 // rows nb0 + 8 n + 2 tig + c; H to int8 per row over the warp's 32 features (one 32-value half-block of the 1292 // down product's K) 1293 const int blk = WW * fb + wf; 1294#pragma unroll 1295 for (int n = 0; n < 2; ++n) { 1296 if (n == 1 && !on1) break; 1297#pragma unroll 1298 for (int c = 0; c < 2; ++c) { 1299 float h[4], am = 0.0f; 1300#pragma unroll 1301 for (int i = 0; i < 4; ++i) { 1302 float gt = acc[i][n][c], up = acc[i][n][2 + c]; 1303 if constexpr (WT == T_NVFP4) { gt *= t_gate; up *= t_up; } // (the expert's s_gate / s_up) 1304 h[i] = gt / (1.0f + __expf(-gt)) * up; 1305 am = fmaxf(am, fabsf(h[i])); 1306 } 1307#pragma unroll 1308 for (int o = 4; o < 32; o <<= 1) am = fmaxf(am, __shfl_xor_sync(0xffffffffu, am, o)); 1309 const float inv = am > 0.0f ? 127.0f / am : 0.0f; 1310 const int r = nb0 + 8 * n + 2 * tig + c; 1311 if constexpr (is_tq(WT)) { 1312 // (CUDA, STRATA_PF_FUSED_TQ=1, a Hadamard-folded down: set_down_rotation) H as FP32, n_ff values at the start of the row's 1313 // own down-output row in `dm` (n_embd floats a row; the down product rewrites it only after rot_quant_h_kernel has read 1314 // it): rotated and quantized there before the down product (the rotation needs the whole 128-feature block of the row) 1315 if (dm != nullptr) { 1316 if (r < nrows) { 1317#pragma unroll 1318 for (int i = 0; i < 4; ++i) dm[(size_t) (row0 + r) * D_ROWS_N + (WR / 2) * fb + 32 * wf + 8 * i + g] = h[i]; 1319 } 1320 continue; 1321 } 1322 } 1323 if constexpr (has_min(WT)) { 1324 // (K-quant gate/up, STRATA_PF_FUSED_KQ=1, CUDA) the down formats with a minimum (Q5_1) read the codes' sum 1325 // per 32 values from the H block's spare bytes 72-79, as quant_act_nat_kernel writes them for the 1326 // activations and HIP's native_w11_kernel for H: the same codes, summed over the warp's 32 features 1327 int code[4], cs = 0; 1328#pragma unroll 1329 for (int i = 0; i < 4; ++i) { code[i] = __float2int_rn(h[i] * inv); cs += code[i]; } 1330#pragma unroll 1331 for (int o = 4; o < 32; o <<= 1) cs += __shfl_xor_sync(0xffffffffu, cs, o); 1332 if (r < nrows) { 1333 uint8_t* o = out + (size_t) (row0 + r) * H_ROW_BYTES + (blk >> 1) * AB; 1334 const int hh = blk & 1; 1335#pragma unroll 1336 for (int i = 0; i < 4; ++i) o[32 * hh + 8 * i + g] = (uint8_t) (int8_t) code[i]; 1337 if (g == 0) { 1338 *(float*) (o + 64 + 4 * hh) = am / 127.0f; 1339 *(float*) (o + 72 + 4 * hh) = (float) cs; 1340 } 1341 } 1342 } else 1343 if (r < nrows) { 1344 uint8_t* o = out + (size_t) (row0 + r) * H_ROW_BYTES + (blk >> 1) * AB; 1345 const int hh = blk & 1; 1346#pragma unroll 1347 for (int i = 0; i < 4; ++i) o[32 * hh + 8 * i + g] = (uint8_t) (int8_t) __float2int_rn(h[i] * inv); 1348 if (g == 0) *(float*) (o + 64 + 4 * hh) = am / 127.0f; 1349 } 1350 } 1351 } 1352 } else { 1353#pragma unroll 1354 for (int i = 0; i < 4; ++i) 1355#pragma unroll 1356 for (int n = 0; n < 2; ++n) { 1357 if (n == 1 && !on1) break; 1358#pragma unroll 1359 for (int q = 0; q < 4; ++q) { 1360 const int r = nb0 + 8 * n + 2 * tig + (q & 1); 1361 if (r < nrows) { 1362 float v = acc[i][n][q]; 1363 if constexpr (WT == T_NVFP4) v *= t_down; // (the expert's s_down) 1364 dm[(size_t) (row0 + r) * kMoeEmbd + rbase + 64 * wf + 16 * i + 8 * (q >> 1) + g] = v; 1365 } 1366 } 1367 } 1368 } 1369 } 1370#endif 1371} 1372 1373#if !defined(__HIPCC__) 1374// STRATA_PF_FUSED_WS=1 (sm_80+, opt-in; the F16 products of native_kernel's F16 path, so the same rounding as 1375// STRATA_PF_FUSED_F16): warp-specialized. 16 warps at the defaults: warps 8-15 (the producers, STRATA_WS_PROD threads) 1376// decode the weights to F16 and copy the activations into a ring of WS_SLOTS stages; warps 0-7 (the consumers, WS_CONS 1377// threads, native_kernel's NT = 256 tiling: 128 weight rows x 64 routed rows) run the products. Named barriers per 1378// slot (full: producers arrive, consumers wait; empty: the reverse) replace the block-wide barrier per stage, and the 1379// ring runs on across work items, so the decode of the next stages and of the next item overlaps the products and the 1380// epilogue. 1381#ifndef STRATA_WS_PROD 1382#define STRATA_WS_PROD 256 1383#endif 1384#ifndef STRATA_WS_SLOTS 1385#define STRATA_WS_SLOTS 2 1386#endif 1387// STRATA_WS_NB: n8 tiles a consumer warp (2: 8 consumer warps of 16 routed rows; 4: 4 warps of 32 rows, each weight 1388// fragment read from shared memory once for twice the products) 1389#ifndef STRATA_WS_NB 1390#define STRATA_WS_NB 2 1391#endif 1392constexpr int WS_NB = STRATA_WS_NB; 1393constexpr int WS_CONS = 32 * 2 * (64 / (8 * WS_NB)), WS_PROD = STRATA_WS_PROD, WS_THREADS = WS_CONS + WS_PROD, WS_SLOTS = STRATA_WS_SLOTS; 1394// STRATA_WS_KS: 64-value stages per ring slot (one handshake per slot: fewer barriers per product) 1395#ifndef STRATA_WS_KS 1396#define STRATA_WS_KS 2 1397#endif 1398constexpr int WS_KS = STRATA_WS_KS; 1399// STRATA_WS_HTAB (default 0, off; 1 or 2 on): IQ3_XXS / IQ3_S producers decode through one table of (codebook entry, 1400// 4 sign bits) -> the four signed values as F16 (32 / 64 KB of shared memory): one 8-byte load and two packed multiplies 1401// a quad instead of the codebook load, the sign masks, the int8 -> F16 conversion. Exact small integers either way: the 1402// same bits. 1403#ifndef STRATA_WS_HTAB 1404#define STRATA_WS_HTAB 0 1405#endif 1406__host__ __device__ constexpr bool ws_htab(int t) { return STRATA_WS_HTAB && (t == T_IQ3_XXS || t == T_IQ3_S); } 1407__host__ __device__ constexpr size_t ws_htab_bytes(int t) { 1408 return !ws_htab(t) ? 0 : STRATA_WS_HTAB == 2 ? (size_t) (t == T_IQ3_S ? 512 : 256) * 8 + 16 * 8 1409 : (size_t) (t == T_IQ3_S ? 512 : 256) * 16 * 8; 1410} 1411__host__ __device__ constexpr size_t ws_smem_bytes(int t) { 1412 return (size_t) WS_SLOTS * WS_KS * (128 * WLDH + 64 * AB) + 2 * 64 * 4 + grid_bytes(t) + ws_htab_bytes(t); 1413} 1414#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 1415__device__ __forceinline__ void nbar_sync(int id, int n) { asm volatile("bar.sync %0, %1;\n" ::"r"(id), "r"(n) : "memory"); } 1416__device__ __forceinline__ void nbar_arrive(int id, int n) { asm volatile("bar.arrive %0, %1;\n" ::"r"(id), "r"(n) : "memory"); } 1417#endif 1418template <int WT, bool GU> 1419__global__ void __launch_bounds__(WS_THREADS, 1) 1420native_ws_kernel(const WideBatch b, const NativeGeom geo, const Tables tb, const uint8_t* __restrict__ act, 1421 const int32_t* __restrict__ src, uint8_t* __restrict__ out, float* __restrict__ dm) { 1422#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 1423 constexpr int WS_UPT = 256 / WS_PROD; // decode units (sub-blocks) a producer thread takes per stage 1424 constexpr int TR = 64, WR = 128, WW = 2; 1425 constexpr int NS = (GU ? GU_ROWS_K : D_ROWS_K) / 64; 1426 constexpr int NFB = (GU ? GU_ROWS_N : D_ROWS_N) / WR; 1427 constexpr int ACT_LD = NS * AB; 1428 constexpr int BS = block_bytes(WT); 1429 constexpr int WT_BYTES = WR * WLDH, ACT_STAGE = TR * AB; 1430 constexpr bool GU256 = GU && WT != T_Q2_0; 1431 extern __shared__ __align__(16) uint8_t smem[]; 1432 uint8_t* wt = smem; // [SLOTS][WR][WLDH] F16 weights 1433 uint8_t* stages = smem + WS_SLOTS * WS_KS * WT_BYTES; // [SLOTS * KS][TR][AB] activations 1434 int* srow = (int*) (stages + WS_SLOTS * WS_KS * ACT_STAGE); // [2][TR] (producers only) 1435 static_assert(NS % WS_KS == 0, "whole slots"); 1436 uint8_t* sgrid = (uint8_t*) (srow + 2 * TR); 1437 uint2* htab = (uint2*) (sgrid + grid_bytes(WT)); // ws_htab: [entry][4 sign bits] four F16 1438 const int tid = threadIdx.x; 1439 if constexpr (grid_bytes(WT) > 0) { 1440 const uint32_t* gs = (const uint32_t*) grid_src<WT>(); 1441 for (int i = tid; i < grid_bytes(WT) / 4; i += WS_THREADS) ((uint32_t*) sgrid)[i] = gs[i]; 1442 } 1443 if constexpr (ws_htab(WT) && STRATA_WS_HTAB == 2) { 1444 const uint32_t* gs = (const uint32_t*) grid_src<WT>(); 1445 constexpr int NE = WT == T_IQ3_S ? 512 : 256; 1446 for (int i = tid; i < NE + 16; i += WS_THREADS) { 1447 if (i < NE) { // the codebook entry's four values as F16 (exact small integers) 1448 const uint32_t gw = gs[i]; 1449 const __half2 lo = __floats2half2_rn((float) (gw & 255), (float) ((gw >> 8) & 255)), 1450 hi = __floats2half2_rn((float) ((gw >> 16) & 255), (float) (gw >> 24)); 1451 htab[i] = make_uint2(*(const uint32_t*) &lo, *(const uint32_t*) &hi); 1452 } else { // four sign bits as the F16 sign-bit masks of the two half2 1453 const uint32_t sg = (uint32_t) (i - NE); 1454 htab[i] = make_uint2(((sg & 1) << 15) | ((sg & 2) << 30), ((sg & 4) << 13) | ((sg & 8) << 28)); 1455 } 1456 } 1457 } else if constexpr (ws_htab(WT)) { 1458 const uint32_t* gs = (const uint32_t*) grid_src<WT>(); 1459 for (int i = tid; i < (int) (ws_htab_bytes(WT) / 8); i += WS_THREADS) { 1460 const uint32_t gw = gs[i >> 4], sg = (uint32_t) i & 15; 1461 __half h[4]; 1462#pragma unroll 1463 for (int j = 0; j < 4; ++j) { 1464 const float v = (float) ((gw >> (8 * j)) & 255); 1465 h[j] = __float2half_rn((sg >> j) & 1 ? -v : v); 1466 } 1467 const __half2 lo = __halves2half2(h[0], h[1]), hi = __halves2half2(h[2], h[3]); 1468 htab[i] = make_uint2(*(const uint32_t*) &lo, *(const uint32_t*) &hi); 1469 } 1470 } 1471 __syncthreads(); 1472 const int t0 = tb.ts[b.e0], nwork = (tb.ts[b.e1] - t0) * NFB; 1473 const int nmine = (int) blockIdx.x < nwork ? (nwork - 1 - (int) blockIdx.x) / (int) gridDim.x + 1 : 0; 1474 const int qtotal = nmine * NS; 1475 auto item = [&](int k, int& e, int& row0, int& nrows, int& fb) { 1476 const int w = (int) blockIdx.x + k * (int) gridDim.x; 1477 const int2 tl = tb.tiles[t0 + w / NFB]; 1478 fb = w % NFB; e = tl.x; row0 = tl.y; 1479 nrows = min(TR, tb.off[e + 1] - row0); 1480 }; 1481 if (tid >= WS_CONS) { 1482 // ---- producers: weight row ur (both 32-value sub-blocks of a stage), and a share of the activation copies 1483 const int p = tid - WS_CONS, ur = WS_UPT == 2 ? p : p >> 1, uj0 = WS_UPT == 2 ? 0 : p & 1; 1484 uint32_t kv[4] = {0, 0, 0, 0}; 1485 if constexpr (WT == T_IQ4_XS || WT == T_IQ4_NL) { 1486#pragma unroll 1487 for (int k = 0; k < 16; ++k) kv[k >> 2] |= (uint32_t) (uint8_t) kvalues_iq4nl[k] << (8 * (k & 3)); 1488 } 1489 int q = 0; 1490 for (int k = 0; k < nmine; ++k) { 1491 int e, row0, nrows, fb; 1492 item(k, e, row0, nrows, fb); 1493 const uint8_t* blob = b.blob[e - b.e0]; 1494 const uint8_t* wrow = GU ? blob + ((ur & 8) ? geo.up_off : 0) + 1495 (size_t) (fb * (WR / 2) + 8 * (ur >> 4) + (ur & 7)) * geo.gu_row 1496 : blob + geo.down_off + (size_t) (fb * WR + ur) * geo.d_row; 1497 auto unit = [&](int s, int uj) -> const uint8_t* { 1498 if (GU256) return wrow + (s >> 2) * BS; 1499 return WT == T_IQ4_NL ? wrow + (2 * s + uj) * BS : wrow + s * BS; 1500 }; 1501 auto sub = [&](int s, int uj) { return GU256 ? 2 * (s & 3) + uj : uj; }; 1502 int* sr = srow + (k & 1) * TR; 1503 if (p < TR) sr[p] = p < nrows ? (GU ? src[row0 + p] : row0 + p) : -1; 1504 if (GU) { 1505 for (int sb = 0; sb < 2; ++sb) pf_l2(wrow + sb * BS + (uj0 ? BS - 1 : 0)); 1506 } else { 1507 for (size_t o = 128 * (size_t) uj0; o < geo.d_row; o += 128 * (3 - WS_UPT)) pf_l2(wrow + o); 1508 } 1509 nbar_sync(1 + 2 * WS_SLOTS, WS_PROD); // sr 1510 uint32_t raw[WS_UPT][5], pc[WS_UPT][NP]; 1511 auto issue = [&](int s) { 1512#pragma unroll 1513 for (int u = 0; u < WS_UPT; ++u) { LdIssue l{pc[u], 0}; uint32_t w5[5]; load_unit_l<WT>(l, unit(s, uj0 + u), sub(s, uj0 + u), w5); } 1514 }; 1515 auto take = [&](int s) { 1516#pragma unroll 1517 for (int u = 0; u < WS_UPT; ++u) { LdTake l{pc[u], 0}; load_unit_l<WT>(l, unit(s, uj0 + u), sub(s, uj0 + u), raw[u]); } 1518 }; 1519#pragma unroll 1520 for (int u = 0; u < WS_UPT; ++u) load_unit<WT>(unit(0, uj0 + u), sub(0, uj0 + u), raw[u]); 1521 if (1 < NS) issue(1); 1522#pragma unroll WS_KS 1523 for (int s = 0; s < NS; ++s, ++q) { 1524 const int qs = q / WS_KS, slot = qs % WS_SLOTS, ks = q % WS_KS, buf = slot * WS_KS + ks; 1525 if (ks == 0 && qs >= WS_SLOTS) nbar_sync(1 + WS_SLOTS + slot, WS_THREADS); // the consumers are done with the slot 1526 uint8_t* st = stages + buf * ACT_STAGE; 1527 for (int c = p; c < TR * 5; c += WS_PROD) { 1528 const int r = c / 5, qq = c % 5; 1529 if (r < nrows) cp16(st + r * AB + qq * 16, act + (size_t) sr[r] * ACT_LD + s * AB + qq * 16); 1530 } 1531 cp_commit(); 1532 if (GU && (s & 3) == 0 && (s >> 2) + 2 < GU_ROWS_K / 256) pf_l2(wrow + ((s >> 2) + 2) * BS + (uj0 ? BS - 1 : 0)); 1533#pragma unroll 1534 for (int u = 0; u < WS_UPT; ++u) { 1535 if constexpr (ws_htab(WT)) { 1536 const uint32_t* w = raw[u]; 1537 const float sf = WT == T_IQ3_XXS ? half_at(w[3]) * (float) (2 * (w[2] >> 28) + 1) * 0.25f 1538 : half_at(w[3]) * (float) (1 + 2 * (w[3] >> 24)); 1539 const __half2 sc = __float2half2_rn(sf); 1540 uint32_t lo[8], hi[8]; 1541#pragma unroll 1542 for (int l = 0; l < 4; ++l) { 1543 uint32_t i0 = (w[l >> 1] >> (16 * (l & 1))) & 255, i1 = (w[l >> 1] >> (16 * (l & 1) + 8)) & 255, s8; 1544 if constexpr (WT == T_IQ3_XXS) { 1545 const uint32_t v = (w[2] >> (7 * l)) & 127; 1546 s8 = v | ((__popc(v) & 1) << 7); 1547 } else { 1548 const uint32_t qh = (w[3] >> 16) & 255; 1549 i0 |= (qh << (8 - 2 * l)) & 256; 1550 i1 |= (qh << (7 - 2 * l)) & 256; 1551 s8 = (w[2] >> (8 * l)) & 255; 1552 } 1553 uint2 e0, e1; 1554 if constexpr (STRATA_WS_HTAB == 2) { 1555 constexpr int NE = WT == T_IQ3_S ? 512 : 256; 1556 const uint2 g0 = htab[i0], g1 = htab[i1], m0 = htab[NE + (s8 & 15)], m1 = htab[NE + (s8 >> 4)]; 1557 e0 = make_uint2(g0.x ^ m0.x, g0.y ^ m0.y); 1558 e1 = make_uint2(g1.x ^ m1.x, g1.y ^ m1.y); 1559 } else { 1560 e0 = htab[i0 * 16 + (s8 & 15)]; 1561 e1 = htab[i1 * 16 + (s8 >> 4)]; 1562 } 1563 const __half2 a0 = __hmul2(*(const __half2*) &e0.x, sc), b0 = __hmul2(*(const __half2*) &e0.y, sc), 1564 a1 = __hmul2(*(const __half2*) &e1.x, sc), b1 = __hmul2(*(const __half2*) &e1.y, sc); 1565 lo[2 * l] = *(const uint32_t*) &a0; hi[2 * l] = *(const uint32_t*) &b0; 1566 lo[2 * l + 1] = *(const uint32_t*) &a1; hi[2 * l + 1] = *(const uint32_t*) &b1; 1567 } 1568 uint4* d = (uint4*) (wt + buf * WT_BYTES + ur * WLDH + 64 * (uj0 + u)); 1569#pragma unroll 1570 for (int k = 0; k < 2; ++k) { 1571 d[2 * k] = make_uint4(lo[4 * k], lo[4 * k + 1], lo[4 * k + 2], lo[4 * k + 3]); 1572 d[2 * k + 1] = make_uint4(hi[4 * k], hi[4 * k + 1], hi[4 * k + 2], hi[4 * k + 3]); 1573 } 1574 continue; 1575 } 1576 uint32_t qv[8]; 1577 float s0, s1; 1578 convert<WT>(raw[u], sgrid, kv, qv, s0, s1); 1579 uint4* d = (uint4*) (wt + buf * WT_BYTES + ur * WLDH + 64 * (uj0 + u)); 1580#pragma unroll 1581 for (int h = 0; h < 2; ++h) { 1582 const __half2 sc = __float2half2_rn(h ? s1 : s0); 1583 uint32_t lo[4], hi[4]; 1584#pragma unroll 1585 for (int w = 0; w < 4; ++w) i8x4_h2(qv[4 * h + w], sc, lo[w], hi[w]); 1586 d[2 * h] = make_uint4(lo[0], lo[1], lo[2], lo[3]); 1587 d[2 * h + 1] = make_uint4(hi[0], hi[1], hi[2], hi[3]); 1588 } 1589 } 1590 if (s + 1 < NS) { 1591 take(s + 1); 1592 if (s + 2 < NS) issue(s + 2); 1593 } 1594 if (ks == WS_KS - 1) { 1595 cp_wait<0>(); 1596 nbar_arrive(1 + slot, WS_THREADS); // the slot is full 1597 } 1598 } 1599 } 1600 return; 1601 } 1602 // ---- consumers: WS_NB n8 tiles (8 WS_NB routed rows) by 64 weight rows a warp, F16 products 1603 constexpr int NB = WS_NB; 1604 const int lane = tid & 31, warp = tid >> 5, g = lane >> 2, tig = lane & 3; 1605 const int wf = warp % WW, nb0 = 8 * NB * (warp / WW); 1606 const int lm = lane >> 3, l8 = lane & 7; 1607 const int a_off = (64 * wf + 8 * (lm & 1) + l8) * WLDH + 16 * (lm >> 1); 1608 const int b_off = (nb0 + 8 * (lm >> 1) + l8) * AB + 16 * (lm & 1); 1609 int q = 0; 1610 for (int k = 0; k < nmine; ++k) { 1611 int e, row0, nrows, fb; 1612 item(k, e, row0, nrows, fb); 1613 const bool on0 = nb0 < nrows; 1614 float acc[4][NB][4]; 1615#pragma unroll 1616 for (int i = 0; i < 4; ++i) 1617#pragma unroll 1618 for (int n = 0; n < NB; ++n) 1619#pragma unroll 1620 for (int c = 0; c < 4; ++c) acc[i][n][c] = 0.0f; 1621 for (int s = 0; s < NS; ++s, ++q) { 1622 const int qs = q / WS_KS, slot = qs % WS_SLOTS, ks = q % WS_KS, buf = slot * WS_KS + ks; 1623 if (ks == 0) nbar_sync(1 + slot, WS_THREADS); // the slot is full 1624 if (on0) { 1625 const uint8_t* W = wt + buf * WT_BYTES; 1626 const uint8_t* sa = stages + buf * ACT_STAGE; 1627#pragma unroll 1628 for (int h = 0; h < 2; ++h) { 1629 uint32_t bh[NB][2][2]; 1630#pragma unroll 1631 for (int pr = 0; pr < NB / 2; ++pr) { 1632 uint32_t bq[4]; 1633 ldsm4(bq, sa + b_off + 16 * pr * AB + 32 * h); 1634#pragma unroll 1635 for (int nn = 0; nn < 2; ++nn) { 1636 const int n = 2 * pr + nn; 1637 const __half2 dxh = __float2half2_rn(*(const float*) (sa + (nb0 + 8 * n + g) * AB + 64 + 4 * h)); 1638#pragma unroll 1639 for (int kk = 0; kk < 2; ++kk) i8x4_h2(bq[2 * nn + kk], dxh, bh[n][kk][0], bh[n][kk][1]); 1640 } 1641 } 1642#pragma unroll 1643 for (int i = 0; i < 4; ++i) 1644#pragma unroll 1645 for (int kk = 0; kk < 2; ++kk) { 1646 uint32_t a[4]; 1647 ldsm4(a, W + a_off + 16 * i * WLDH + 64 * h + 32 * kk); 1648#pragma unroll 1649 for (int n = 0; n < NB; ++n) 1650 if (n == 0 || nb0 + 8 * n < nrows) mma16h(acc[i][n], a, bh[n][kk][0], bh[n][kk][1]); 1651 } 1652 } 1653 } 1654 if (ks == WS_KS - 1 && (qs + WS_SLOTS) * WS_KS < qtotal) nbar_arrive(1 + WS_SLOTS + slot, WS_THREADS); // the slot is free 1655 } 1656 if (!on0) continue; 1657 if (GU) { 1658 const int blk = WW * fb + wf; 1659#pragma unroll 1660 for (int n = 0; n < NB; ++n) { 1661 if (nb0 + 8 * n >= nrows) break; 1662#pragma unroll 1663 for (int c = 0; c < 2; ++c) { 1664 float h[4], am = 0.0f; 1665#pragma unroll 1666 for (int i = 0; i < 4; ++i) { 1667 const float gt = acc[i][n][c], up = acc[i][n][2 + c]; 1668 h[i] = gt / (1.0f + __expf(-gt)) * up; 1669 am = fmaxf(am, fabsf(h[i])); 1670 } 1671#pragma unroll 1672 for (int o = 4; o < 32; o <<= 1) am = fmaxf(am, __shfl_xor_sync(0xffffffffu, am, o)); 1673 const float inv = am > 0.0f ? 127.0f / am : 0.0f; 1674 const int r = nb0 + 8 * n + 2 * tig + c; 1675 if (r < nrows) { 1676 uint8_t* o = out + (size_t) (row0 + r) * H_ROW_BYTES + (blk >> 1) * AB; 1677 const int hh = blk & 1; 1678#pragma unroll 1679 for (int i = 0; i < 4; ++i) o[32 * hh + 8 * i + g] = (uint8_t) (int8_t) __float2int_rn(h[i] * inv); 1680 if (g == 0) *(float*) (o + 64 + 4 * hh) = am / 127.0f; 1681 } 1682 } 1683 } 1684 } else { 1685 const int rbase = fb * WR; 1686#pragma unroll 1687 for (int i = 0; i < 4; ++i) 1688#pragma unroll 1689 for (int n = 0; n < NB; ++n) { 1690 if (nb0 + 8 * n >= nrows) break; 1691#pragma unroll 1692 for (int c = 0; c < 4; ++c) { 1693 const int r = nb0 + 8 * n + 2 * tig + (c & 1); 1694 if (r < nrows) dm[(size_t) (row0 + r) * kMoeEmbd + rbase + 64 * wf + 16 * i + 8 * (c >> 1) + g] = acc[i][n][c]; 1695 } 1696 } 1697 } 1698 } 1699#endif 1700} 1701#endif // !__HIPCC__ 1702 1703#if defined(__HIPCC__) 1704// ---- Aurora (S23): the native packs' fused experts on gfx11 (RDNA3 / RDNA3.5) matrix cores, v_wmma_i32_16x16x16_iu8 1705// (wave32). The CUDA kernel's arithmetic: a 32-value sub-block decoded to int8 (load_unit / convert above) and its 1706// scales; per 32 values the int32 dot starts from MAGIC (the C operand), so as_float(d) - 1.5 * 2^23 is the dot; the 1707// formats with a scale per 16 values take each 16-value k-step's dot alone. Fragments (gfx11): A lane l holds the 16 1708// k of row l % 16 (lanes 16..31 repeat lanes 0..15), B lane l the 16 k of column l % 16, C/D lane l holds 1709// D[2i + l / 16][l % 16]. Both operands are in natural order: the decoded weights (LDS, double-buffered) and the 1710// activations (quant_act_nat_kernel; read by each lane from global/L2, a stage ahead). 1711// A work item = NW_ROWS weight rows x a 64-row tile; 8 waves: 4 along the weight rows (32 each) x 2 along the tile 1712// (32 each), each 2 x 2 WMMA tiles. Gate/up: local row r is feature r / 2, its gate (r even) or up (r odd) row, so a 1713// lane pair l, l + 16 holds gate and up of one feature. 1714// gfx12 (RDNA4: gfx1200 / gfx1201, wave32) has the same instruction with another layout (checked on gfx1201, R9700): 1715// A lane l holds A[l % 16][8 (l / 16) + j], B lane l B[8 (l / 16) + j][l % 16] (j = 0..7: 8 int8, two VGPRs, no 1716// replication), C/D lane l holds D[8 (l / 16) + i][l % 16]. A WMMA still contracts 16 k; lane half hi supplies k 1717// 8 hi .. 8 hi + 7 of them, so a 16-value k-step is still one WMMA (the K16 scales stay per WMMA). NW_DROW is the 1718// D row of a lane's element i; on gfx12 a lane's 8 rows are consecutive, so a feature's gate (even local row) and up 1719// (odd) sit in one lane (elements 2j, 2j + 1) and the SwiGLU needs no cross-lane exchange. 1720#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || defined(__gfx1151__) 1721#define STRATA_NAT_W11 1 1722#else 1723#define STRATA_NAT_W11 0 1724#endif 1725#if defined(__gfx1200__) || defined(__gfx1201__) 1726#define STRATA_NAT_W12 1 1727#else 1728#define STRATA_NAT_W12 0 1729#endif 1730#define STRATA_NAT_WMMA (STRATA_NAT_W11 || STRATA_NAT_W12) 1731#if STRATA_NAT_W12 1732#define NW_DROW(i, hi) (8 * (hi) + (i)) 1733#else 1734#define NW_DROW(i, hi) (2 * (i) + (hi)) 1735#endif 1736// The VGPR cap of the occupancy variant (amdgpu_waves_per_eu). 8 was measured against a clang before 22; clang 22 1737// (ROCm 7.10) computes wrong results with it (#1180: gfx1100, every run), so from clang 22 the cap is off - except for 1738// gfx1151 on clang 23, where the capped kernels give the same bits and take less time (measured with ROCm 7.14.1's 1739// clang 23, hip_prefill_fused_iq, one layer, 2048-8192 tokens: gate/up IQ2_XXS / IQ3_XXS / IQ3_S / IQ4_XS with down 1740// Q2_0 / IQ4_NL 8-15%; IQ2_S / Q2_0 2-4%, from its Q2_0 down: IQ2_S's LDS leaves room for 3 blocks per WGP, so the 1741// compiler keeps its 238 VGPRs). Two formats stay uncapped there: IQ2_XS gate/up spills 104 VGPRs under the cap and 1742// takes 61-66% more time, and Q8_0 down (18 spilled) took -1.9% to +1.9% time. The test is the clang major version: 1743// other clang 23 builds are not measured, and clang 24 and newer keep the clang 22 rule. NW_LB is the variant's 1744// template argument, the same in the host and every device pass (the launch names that instance); whether its cap 1745// applies is decided per architecture and format in the device pass (nw_capped). -DSTRATA_W_LB=N sets the cap on 1746// every architecture and format. 1747#if defined(STRATA_W_LB) 1748constexpr int NW_LB = STRATA_W_LB; 1749#else 1750constexpr int NW_LB = 8; 1751#endif 1752constexpr bool nw_capped(int wt) { 1753#if defined(STRATA_W_LB) || !defined(__clang_major__) || __clang_major__ < 22 1754 (void) wt; 1755 return true; 1756#elif defined(__gfx1151__) && (__clang_major__ == 23 || (__clang_major__ >= 24 && !defined(STRATA_W_NOCAP24))) 1757 return wt != T_IQ2_XS && wt != T_Q8_0; 1758#else 1759 (void) wt; 1760 return false; 1761#endif 1762} 1763typedef int nw_i4 __attribute__((ext_vector_type(4))); 1764typedef int nw_i2 __attribute__((ext_vector_type(2))); 1765typedef int nw_i8 __attribute__((ext_vector_type(8))); 1766constexpr int NW_ROWS = 128; 1767constexpr int NW_THREADS = 256; 1768 1769// one A / B fragment: gfx11 16 int8 per lane (uint4), gfx12 8 (uint2) 1770#if STRATA_NAT_W12 1771typedef uint2 nw_frag; 1772typedef nw_i2 nw_ab; 1773__device__ __forceinline__ nw_ab nw_v(uint2 v) { return nw_ab{(int) v.x, (int) v.y}; } 1774#else 1775typedef uint4 nw_frag; 1776typedef nw_i4 nw_ab; 1777__device__ __forceinline__ nw_ab nw_v(uint4 v) { return nw_ab{(int) v.x, (int) v.y, (int) v.z, (int) v.w}; } 1778#endif 1779// Workgroup barrier with the LDS wait written out (idea and wording of libstrix's barrier(), asf0/libstrix, MIT): in WGP mode 1780// the compiler may drop the s_waitcnt before __syncthreads()'s s_barrier (llvm/llvm-project#220357), so a read after the 1781// barrier can overtake another wave's LDS store. Release/acquire fences keep LDS accesses from moving across it. 1782__device__ __forceinline__ void nw_barrier() { 1783#if STRATA_NAT_W11 1784 __builtin_amdgcn_fence(__ATOMIC_RELEASE, "workgroup", "local"); 1785 __builtin_amdgcn_s_waitcnt(0x7 | (0x3f << 10)); // gfx11 simm16: expcnt [2:0], lgkmcnt [9:4], vmcnt [15:10]: lgkmcnt(0) only 1786 __builtin_amdgcn_s_barrier(); 1787 __builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "workgroup", "local"); 1788#else 1789 __syncthreads(); 1790#endif 1791} 1792__device__ __forceinline__ nw_i8 nw_wmma(nw_ab a, nw_ab b, nw_i8 c) { 1793#if STRATA_NAT_W11 1794 return __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a, true, b, c, false); 1795#elif STRATA_NAT_W12 1796 return __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a, true, b, c, false); 1797#else 1798 __builtin_trap(); 1799 return c; 1800#endif 1801} 1802 1803// W (X1): ALIAS = the H tile lives on the weight buffers (one extra barrier); LB > 0 = amdgpu_waves_per_eu(LB) (VGPR cap) 1804// where nw_capped(WT) says the cap applies. 1805// Disable both with -DSTRATA_W_NO_OCC (the launch sites then use <WT, GU, false, 0> and grid factor wgp_blocks). 1806template <int WT, bool GU, bool ALIAS = false, int LB = 0> 1807__global__ void __launch_bounds__(NW_THREADS) __attribute__((amdgpu_waves_per_eu(LB > 0 && nw_capped(WT) ? LB : 1))) 1808native_w11_kernel(const Batch b, const NativeGeom geo, const Tables tb, const uint8_t* __restrict__ act, 1809 const int32_t* __restrict__ src, uint8_t* __restrict__ out, float* __restrict__ dm) { 1810#if STRATA_NAT_WMMA 1811 constexpr int NS = (GU ? GU_ROWS_K : D_ROWS_K) / 64; // 64-value stages along K 1812 constexpr int NFB = (GU ? GU_ROWS_N : D_ROWS_N) / NW_ROWS; // work items per tile 1813 constexpr int ACT_LD = NS * AB; 1814 constexpr int BS = block_bytes(WT); 1815 constexpr bool K16 = per16(WT); 1816 constexpr int GB = grid_bytes(WT) > 0 ? grid_bytes(WT) : 16; 1817 __shared__ __align__(16) uint8_t wtbuf[2 * NW_ROWS * WLD]; 1818 uint8_t(*wt)[NW_ROWS][WLD] = reinterpret_cast<uint8_t(*)[NW_ROWS][WLD]>(wtbuf); 1819 __shared__ __align__(16) float ws[2][NW_ROWS][4]; // their scales per 16 values 1820 __shared__ __align__(16) uint8_t sgrid[GB]; 1821 constexpr int SGN = (WT == T_IQ2_XXS || WT == T_IQ2_XS || WT == T_IQ2_S || WT == T_IQ3_XXS || WT == T_IQ3_S) 1822 ? sign_entries(WT) : 1; 1823 __shared__ __align__(16) uint4 ssign[SGN]; // the signs of a sign byte, as byte masks 1824 __shared__ int srow[kTileRows]; 1825 __shared__ float hs_sep[(GU && !ALIAS) ? kTileRows : 1][65]; 1826 static_assert(!ALIAS || (size_t) kTileRows * 65 * 4 <= (size_t) 2 * NW_ROWS * WLD, "H tile must fit the weight buffers"); 1827 float(*hs)[65] = ALIAS ? reinterpret_cast<float(*)[65]>(wtbuf) : hs_sep; 1828 const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5; 1829 const int wm = wave & 3, wn = wave >> 2, l16 = lane & 15, hi = lane >> 4; 1830 if constexpr (grid_bytes(WT) > 0) { 1831 const uint32_t* gs = (const uint32_t*) grid_src<WT>(); 1832 for (int i = tid; i < grid_bytes(WT) / 4; i += NW_THREADS) ((uint32_t*) sgrid)[i] = gs[i]; 1833 } 1834 if constexpr (SGN > 1) { 1835 for (int i = tid; i < SGN; i += NW_THREADS) ssign[i] = sign_entry(i, SGN == 128); 1836 } 1837 uint32_t kv[4] = {0, 0, 0, 0}; 1838 if constexpr (WT == T_IQ4_XS || WT == T_IQ4_NL) { 1839#pragma unroll 1840 for (int k = 0; k < 16; ++k) kv[k >> 2] |= (uint32_t) (uint8_t) kvalues_iq4nl[k] << (8 * (k & 3)); 1841 } 1842 const int ur = tid >> 1, uj = tid & 1; // this thread's decode unit: row ur, sub-block uj 1843 const int t0 = tb.ts[b.e0], nwork = (tb.ts[b.e1] - t0) * NFB; 1844 for (int w = blockIdx.x; w < nwork; w += gridDim.x) { 1845 const int2 tl = tb.tiles[t0 + w / NFB]; 1846 const int fb = w % NFB, e = tl.x, row0 = tl.y, nrows = min(kTileRows, tb.off[e + 1] - row0); 1847 const uint8_t* blob = b.blob[e - b.e0]; 1848 const int rbase = fb * NW_ROWS; 1849 const uint8_t* wrow = GU ? blob + ((ur & 1) ? geo.up_off : 0) + (size_t) (fb * (NW_ROWS / 2) + (ur >> 1)) * geo.gu_row 1850 : blob + geo.down_off + (size_t) (rbase + ur) * geo.d_row; 1851 constexpr bool B128 = is_tq(WT); // (STRATA_PF_FUSED_TQ=1, gfx12) 128-value blocks, gate/up and down: a block per two stages, sub-blocks 0-3 1852 // (STRATA_PF_FUSED_Q8GU=1, gfx12) a Q8_0 gate/up has 32-value blocks, two a stage, like its down: not a 256-value superblock 1853 // (K-quant DOWN, gfx12 Qwen3.6 copy: n_ff 512 = two whole 256-value super-blocks a row, UD-Q4_K_XL's Q5_K / Q6_K downs) the same walk for a down row: a Q4_K / Q5_K / Q6_K down with K a multiple 1854 // of 256, as the CUDA kernel's GU256 (D_ROWS_K % 256 == 0). False for every other instance: GU instances keep their value, and the downs that exist (Q2_0, Q5_0, Q5_1, Q8_0, IQ4_NL, 1855 // the trellis types) are no K-quants. 1856 constexpr bool KQD256 = !GU && (WT == T_Q4_K || WT == T_Q5_K || WT == T_Q6_K) && D_ROWS_K % 256 == 0; 1857 constexpr bool GU256 = (GU && WT != T_Q2_0 && WT != T_Q8_0) || KQD256; 1858 auto unit = [&](int s) -> const uint8_t* { 1859 if constexpr (B128) return wrow + (s >> 1) * BS; 1860 if (GU256) return wrow + (s >> 2) * BS; // (Q2_0 gate/up: 64-value blocks, one a stage, like its down) 1861 return (WT == T_IQ4_NL || WT == T_Q5_0 || WT == T_Q5_1 || WT == T_Q8_0) ? wrow + (2 * s + uj) * BS : wrow + s * BS; 1862 }; 1863 auto sub = [&](int s) { return B128 ? 2 * (s & 1) + uj : GU256 ? 2 * (s & 3) + uj : uj; }; 1864 auto put = [&](const uint32_t (&raw)[raw_words(WT)], int buf) { 1865 uint32_t q[8]; 1866 float s0, s1; 1867 convert<WT>(raw, sgrid, kv, q, s0, s1, ssign); 1868 uint4* d = (uint4*) &wt[buf][ur][32 * uj]; 1869 d[0] = make_uint4(q[0], q[1], q[2], q[3]); 1870 d[1] = make_uint4(q[4], q[5], q[6], q[7]); 1871 *(float2*) &ws[buf][ur][2 * uj] = make_float2(s0, s1); 1872 }; 1873 __syncthreads(); // the previous item is done with the buffers 1874 if (tid < kTileRows) { 1875 const int r = row0 + min(tid, nrows - 1); // rows past the tile's end repeat its last one 1876 srow[tid] = GU ? src[r] : r; 1877 } 1878 uint32_t raw[raw_words(WT)] = {}; 1879 load_unit<WT>(unit(0), sub(0), raw); 1880 put(raw, 0); 1881 if (NS > 1) load_unit<WT>(unit(1), sub(1), raw); 1882 __syncthreads(); // srow 1883 const bool on = 32 * wn < nrows; 1884 const uint8_t* brow[2]; 1885#pragma unroll 1886 for (int nt = 0; nt < 2; ++nt) brow[nt] = act + (size_t) srow[32 * wn + 16 * nt + l16] * ACT_LD; 1887 float acc[2][2][8]; 1888#pragma unroll 1889 for (int mt = 0; mt < 2; ++mt) 1890#pragma unroll 1891 for (int nt = 0; nt < 2; ++nt) 1892#pragma unroll 1893 for (int i = 0; i < 8; ++i) acc[mt][nt][i] = 0.0f; 1894 nw_frag bq[2][4]; // B per 16-value k-step: bq[nt][2 h + kk] = k 32 h + 16 kk .. (gfx12: this lane's 8 of them) 1895 float2 bx[2], bsum[2]; // the activation scales, and (the formats with a minimum) the codes' sums per 32 values 1896 auto fetch_b = [&](int s, nw_frag (&q)[2][4], float2 (&x)[2], float2 (&sm)[2]) { 1897#pragma unroll 1898 for (int nt = 0; nt < 2; ++nt) { 1899#if STRATA_NAT_W12 1900 const uint2* p = reinterpret_cast<const uint2*>(brow[nt] + s * AB + 8 * hi); // k 16 j + 8 hi .. + 7 1901 q[nt][0] = p[0]; q[nt][1] = p[2]; q[nt][2] = p[4]; q[nt][3] = p[6]; 1902#else 1903 const uint4* p = reinterpret_cast<const uint4*>(brow[nt] + s * AB); 1904 q[nt][0] = p[0]; q[nt][1] = p[1]; q[nt][2] = p[2]; q[nt][3] = p[3]; 1905#endif 1906 x[nt] = *reinterpret_cast<const float2*>(brow[nt] + s * AB + 64); 1907 if constexpr (has_min(WT)) sm[nt] = *reinterpret_cast<const float2*>(brow[nt] + s * AB + 72); 1908 else sm[nt] = make_float2(0.0f, 0.0f); 1909 } 1910 }; 1911 if (on) fetch_b(0, bq, bx, bsum); 1912 for (int s = 0; s < NS; ++s) { 1913#ifndef STRATA_Q_TWOBAR 1914 nw_barrier(); // stage s's weights are in buffer s & 1 1915#else 1916 __syncthreads(); // stage s's weights are in buffer s & 1 1917#endif 1918 nw_frag nbq[2][4]; 1919 float2 nbx[2], nbsum[2]; 1920 if (on && s + 1 < NS) fetch_b(s + 1, nbq, nbx, nbsum); 1921 if (on) { 1922 const int bf = s & 1; 1923#pragma unroll 1924 for (int h = 0; h < 2; ++h) { 1925#pragma unroll 1926 for (int mt = 0; mt < 2; ++mt) { 1927 const int rb = 32 * wm + 16 * mt; 1928#if STRATA_NAT_W12 1929 // k 32 h + 8 hi .. + 7 (A0) and 32 h + 16 + 8 hi .. + 7 (A1) of row rb + l16 1930 const uint2* ap = reinterpret_cast<const uint2*>(&wt[bf][rb + l16][32 * h + 8 * hi]); 1931 const nw_ab A0 = nw_v(ap[0]), A1 = nw_v(ap[2]); 1932#else 1933 const uint4* ap = reinterpret_cast<const uint4*>(&wt[bf][rb + l16][32 * h]); 1934 const nw_ab A0 = nw_v(ap[0]), A1 = nw_v(ap[1]); 1935#endif 1936 float w0[8], w1[8]; 1937#pragma unroll 1938 for (int i = 0; i < 8; ++i) { 1939 const float2 sw = *reinterpret_cast<const float2*>(&ws[bf][rb + NW_DROW(i, hi)][2 * h]); 1940 w0[i] = sw.x; w1[i] = sw.y; 1941 } 1942#pragma unroll 1943 for (int nt = 0; nt < 2; ++nt) { 1944 const float dx = h ? bx[nt].y : bx[nt].x; 1945 const nw_ab B0 = nw_v(bq[nt][2 * h]), B1 = nw_v(bq[nt][2 * h + 1]); 1946 const nw_i8 m = nw_i8{MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC, MAGIC}; 1947 if constexpr (K16) { 1948 const nw_i8 d0 = nw_wmma(A0, B0, m), d1 = nw_wmma(A1, B1, m); 1949#pragma unroll 1950 for (int i = 0; i < 8; ++i) { 1951 const float v = fmaf(w1[i], dotf(d1[i]), w0[i] * dotf(d0[i])); 1952 acc[mt][nt][i] = fmaf(dx, v, acc[mt][nt][i]); 1953 } 1954 } else { 1955 const nw_i8 d = nw_wmma(A1, B1, nw_wmma(A0, B0, m)); 1956#pragma unroll 1957 for (int i = 0; i < 8; ++i) { 1958 acc[mt][nt][i] = fmaf(w0[i] * dx, dotf(d[i]), acc[mt][nt][i]); 1959 if constexpr (has_min(WT)) acc[mt][nt][i] = fmaf(w1[i] * dx, h ? bsum[nt].y : bsum[nt].x, acc[mt][nt][i]); 1960 } 1961 } 1962 } 1963 } 1964 } 1965 } 1966 if (s + 1 < NS) { 1967 put(raw, (s + 1) & 1); // the other buffer: its readers passed this barrier 1968#ifndef STRATA_Q_TWOBAR 1969 // (the next stage's first barrier orders these writes: nw_barrier waits for them explicitly) 1970#else 1971 __syncthreads(); // the writes of every thread are in LDS before the next stage reads them (#1180) 1972#endif 1973 if (s + 2 < NS) load_unit<WT>(unit(s + 2), sub(s + 2), raw); 1974 if (on) { 1975#pragma unroll 1976 for (int nt = 0; nt < 2; ++nt) { 1977 bx[nt] = nbx[nt]; 1978 bsum[nt] = nbsum[nt]; 1979#pragma unroll 1980 for (int j = 0; j < 4; ++j) bq[nt][j] = nbq[nt][j]; 1981 } 1982 } 1983 } 1984 } 1985 if constexpr (GU) { 1986 if constexpr (ALIAS) __syncthreads(); // every wave is done reading the weight buffers 1987 if (on) { 1988#pragma unroll 1989 for (int mt = 0; mt < 2; ++mt) 1990#pragma unroll 1991 for (int nt = 0; nt < 2; ++nt) { 1992#if STRATA_NAT_W12 1993 // lane l: local rows rb + 8 hi + i = features 16 wm + 8 mt + 4 hi + j, gate (i = 2j) and up (2j + 1) 1994#pragma unroll 1995 for (int j = 0; j < 4; ++j) { 1996 const float gt = acc[mt][nt][2 * j], up = acc[mt][nt][2 * j + 1]; 1997 hs[32 * wn + 16 * nt + l16][16 * wm + 8 * mt + 4 * hi + j] = gt / (1.0f + __expf(-gt)) * up; 1998 } 1999#else 2000#pragma unroll 2001 for (int i = 0; i < 8; ++i) { 2002 const float up = __shfl_xor_sync(0xffffffffu, acc[mt][nt][i], 16); 2003 const float gt = acc[mt][nt][i]; 2004 if (hi == 0) hs[32 * wn + 16 * nt + l16][16 * wm + 8 * mt + i] = gt / (1.0f + __expf(-gt)) * up; 2005 } 2006#endif 2007 } 2008 } 2009 __syncthreads(); 2010 if constexpr (is_tq(WT)) { 2011 // (STRATA_PF_FUSED_TQ=1, gfx12, a Hadamard-folded down: set_down_rotation) H as FP32, n_ff values at the start of the row's own 2012 // down-output row in `dm` (n_embd floats a row; the down product rewrites it only after rot_quant_h_kernel has read it), as 2013 // CUDA's native_kernel does (the dm-row invariant: RESULTS-OWN-4X section 8): rotated and quantized there before the down product 2014 if (dm != nullptr) { 2015 if (tid < 2 * kTileRows) { 2016 const int r = tid >> 1, hh = tid & 1; 2017 if (r < nrows) { 2018 float* o = dm + (size_t) (row0 + r) * kMoeEmbd + (NW_ROWS / 2) * fb + 32 * hh; 2019#pragma unroll 8 2020 for (int j = 0; j < 32; ++j) o[j] = hs[r][32 * hh + j]; 2021 } 2022 } 2023 continue; // (the next work item: its first barrier orders these reads of hs before the buffers are reused) 2024 } 2025 } 2026 // H block fb (features 64 fb ..) to int8 per 32, natural order: a thread per (tile row, half) 2027 if (tid < 2 * kTileRows) { 2028 const int r = tid >> 1, hh = tid & 1; 2029 if (r < nrows) { 2030 float am = 0.0f; 2031#pragma unroll 8 2032 for (int j = 0; j < 32; ++j) am = fmaxf(am, fabsf(hs[r][32 * hh + j])); 2033 const float inv = am > 0.0f ? 127.0f / am : 0.0f; 2034 uint8_t* o = out + (size_t) (row0 + r) * H_ROW_BYTES + (size_t) fb * AB; 2035 uint32_t wd[8] = {}; 2036 int csum = 0; 2037#pragma unroll 2038 for (int j = 0; j < 32; ++j) { 2039 const int c = __float2int_rn(hs[r][32 * hh + j] * inv); 2040 csum += c; 2041 wd[j >> 2] |= (uint32_t) (uint8_t) (int8_t) c << (8 * (j & 3)); 2042 } 2043 uint4* o4 = reinterpret_cast<uint4*>(o + 32 * hh); 2044 o4[0] = make_uint4(wd[0], wd[1], wd[2], wd[3]); 2045 o4[1] = make_uint4(wd[4], wd[5], wd[6], wd[7]); 2046 *reinterpret_cast<float*>(o + 64 + 4 * hh) = am / 127.0f; 2047 *reinterpret_cast<float*>(o + 72 + 4 * hh) = (float) csum; // (the formats with a minimum) 2048 } 2049 } 2050 } else { 2051 if (on) { 2052#pragma unroll 2053 for (int mt = 0; mt < 2; ++mt) 2054#pragma unroll 2055 for (int nt = 0; nt < 2; ++nt) { 2056 const int r = 32 * wn + 16 * nt + l16; 2057 if (r < nrows) { 2058#if STRATA_NAT_W12 2059 // columns rbase + 32 wm + 16 mt + 8 hi .. + 7: contiguous, 32-byte aligned (dm: 16, checked in experts_native) 2060 float4* d = reinterpret_cast<float4*>(dm + (size_t) (row0 + r) * kMoeEmbd + rbase + 32 * wm + 16 * mt + 8 * hi); 2061 d[0] = make_float4(acc[mt][nt][0], acc[mt][nt][1], acc[mt][nt][2], acc[mt][nt][3]); 2062 d[1] = make_float4(acc[mt][nt][4], acc[mt][nt][5], acc[mt][nt][6], acc[mt][nt][7]); 2063#else 2064 float* d = dm + (size_t) (row0 + r) * kMoeEmbd + rbase + 32 * wm + 16 * mt + hi; 2065#pragma unroll 2066 for (int i = 0; i < 8; ++i) d[2 * i] = acc[mt][nt][i]; 2067#endif 2068 } 2069 } 2070 } 2071 } 2072 } 2073#else 2074 __builtin_trap(); 2075#endif 2076} 2077 2078// STRATA_PF_F16=1 (gfx11): the other native formats on the F16 matrix cores - moe_f16_common.cuh's work item, 2079// staged activations and epilogues. load_unit / convert decode each sub-block once into the weight tile in LDS (int8 2080// codes, the scales as half2), and each wave turns its own 16 rows into F16 before its products. Gate/up rows 0..63 2081// are the item's gate rows, 64..127 its up rows; down rows are output rows. The output differs from 2082// native_w11_kernel's (F16 products instead of int8 dots with float scales). 2083// gfx1151, IQ3_S / IQ4_NL, 16384 tokens x top 10 over 512 experts, one 128-expert launch (ms, native_w11_kernel -> 2084// this): gate/up 11.65 -> 9.87, down 6.11 -> 4.71; weights decoded to F16 in LDS (18 KB a block) were 9.83 and 5.71. 2085template <int WT, bool GU, int BN, bool D16 = false> 2086__global__ void __launch_bounds__(f16::THREADS) 2087native_f16_kernel(const Batch b, const NativeGeom geo, const Tables tb, const uint8_t* __restrict__ act, 2088 const int32_t* __restrict__ src, uint8_t* __restrict__ out, float* __restrict__ dm) { 2089#if STRATA_F16_DEVICE 2090 // (moe_fused_iq.cu has its own THREADS and AB: the f16 names are qualified where they collide) 2091 using namespace f16; 2092 constexpr int NS = (GU ? GU_ROWS_K : D_ROWS_K) / 64; // 64-value stages along K 2093 constexpr int NFB = GU ? GU_NFB : D_NFB; 2094 constexpr int RW = raw_words(WT); 2095 constexpr int BS = block_bytes(WT); 2096 constexpr bool K16 = per16(WT); 2097 constexpr int GB = grid_bytes(WT) > 0 ? grid_bytes(WT) : 16; 2098 constexpr int SGN = (WT == T_IQ2_XXS || WT == T_IQ2_XS || WT == T_IQ2_S || WT == T_IQ3_XXS || WT == T_IQ3_S) 2099 ? sign_entries(WT) : 1; 2100 constexpr int WLD8 = 80; // bytes per weight row: 64 int8 codes + 16 (rows 80 bytes apart spread a fragment read) 2101 constexpr int W_BYTES = ROWS * WLD8 + 2 * ROWS * 4; // the codes, then the (s0, s1) half2 per row and sub-block 2102 constexpr int STAGE_BYTES = W_BYTES + ActStage<BN>::BYTES; 2103 static_assert(GU_EPI_BYTES <= STAGE_BYTES && D_EPI_BYTES <= STAGE_BYTES, "the epilogues fit the stage"); 2104 __shared__ __align__(16) uint8_t lds[STAGE_BYTES]; 2105 __shared__ __align__(16) uint8_t sgrid[GB]; 2106 __shared__ __align__(16) uint4 ssign[SGN]; 2107 __shared__ int srow[BN]; 2108 uint8_t* s_w = lds; 2109 auto* s_sc = reinterpret_cast<uint32_t*>(lds + ROWS * WLD8); 2110 auto* s_act = reinterpret_cast<uint4*>(lds + W_BYTES); 2111 const int tid = threadIdx.x, wave = tid >> 5, sub_lane = tid & 15; 2112 2113 int fb, e, row0, nrows; 2114 if (!work_item<BN, NFB>(b, tb.ts, tb.tiles, tb.off, blockIdx.x, fb, e, row0, nrows)) return; 2115 if constexpr (grid_bytes(WT) > 0) { 2116 const uint32_t* gs = (const uint32_t*) grid_src<WT>(); 2117 for (int i = tid; i < grid_bytes(WT) / 4; i += f16::THREADS) ((uint32_t*) sgrid)[i] = gs[i]; 2118 } 2119 if constexpr (SGN > 1) { 2120 for (int i = tid; i < SGN; i += f16::THREADS) ssign[i] = sign_entry(i, SGN == 128); 2121 } 2122 uint32_t kv[4] = {0, 0, 0, 0}; 2123 if constexpr (WT == T_IQ4_XS || WT == T_IQ4_NL) { 2124#pragma unroll 2125 for (int k = 0; k < 16; ++k) kv[k >> 2] |= (uint32_t) (uint8_t) kvalues_iq4nl[k] << (8 * (k & 3)); 2126 } 2127 const int live_tiles = (nrows + 15) / 16; 2128 const uint8_t* blob = b.blob[e - b.e0]; 2129 if (tid < BN) srow[tid] = tid < nrows ? (GU ? src[row0 + tid] : row0 + tid) : -1; // tokens; H by routed row 2130 __syncthreads(); 2131 2132 const int ur = tid >> 1, uj = tid & 1; // this thread's decode unit: weight-tile row ur, sub-block uj of a stage 2133 const uint8_t* wrow = GU ? blob + (ur >= 64 ? geo.up_off : 0) + (size_t) (fb * 64 + (ur & 63)) * geo.gu_row 2134 : blob + geo.down_off + (size_t) (fb * ROWS + ur) * geo.d_row; 2135 const auto unit = [&](int s) -> const uint8_t* { 2136 if (GU) return wrow + (s >> 2) * BS; 2137 return (WT == T_IQ4_NL || WT == T_Q5_0 || WT == T_Q8_0) ? wrow + (2 * s + uj) * BS : wrow + s * BS; 2138 }; 2139 const auto sub = [&](int s) { return GU ? 2 * (s & 3) + uj : uj; }; 2140 uint32_t raw[RW]; 2141 ActStage<BN> as; 2142 as.init(act, GU ? GU_ACT_LD : H_LD, srow, tid); 2143 const auto fetch_stage = [&](int s) { 2144 load_unit<WT>(unit(s), sub(s), raw); 2145 as.fetch(s); 2146 }; 2147 // the sub-block as int8 codes and its scales into the weight tile 2148 const auto commit_stage = [&]() { 2149 uint32_t q[8]; 2150 float s0, s1; 2151 convert<WT, RW>(raw, sgrid, kv, q, s0, s1, ssign); 2152 uint4* d = reinterpret_cast<uint4*>(s_w + ur * WLD8 + 32 * uj); 2153 d[0] = make_uint4(q[0], q[1], q[2], q[3]); 2154 d[1] = make_uint4(q[4], q[5], q[6], q[7]); 2155 s_sc[uj * ROWS + ur] = __builtin_bit_cast(uint32_t, __floats2half2_rn(s0, K16 ? s1 : s0)); 2156 as.commit(s_act); 2157 }; 2158 v8f acc[BN / 16]; 2159#pragma unroll 2160 for (int j = 0; j < BN / 16; ++j) acc[j] = v8f{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; 2161 // int8 codes to F16 per wave (gufo's Q8_0 decode): the sign bit flipped carries q + 128, a byte permute makes it 2162 // the mantissa of 1024 (exact), minus 1152 is q, then one packed multiply by the sub-block's scale 2163 const __half2 magic = __floats2half2_rn(-1152.0f, -1152.0f), zero2 = __floats2half2_rn(0.0f, 0.0f); 2164 const auto compute_stage = [&]() { 2165 const int row = wave * 16 + sub_lane; 2166 const uint4* wp = reinterpret_cast<const uint4*>(s_w + row * WLD8); 2167#pragma unroll 2168 for (int kb = 0; kb < 2; ++kb) { 2169 const uint4 c0 = wp[2 * kb], c1 = wp[2 * kb + 1]; 2170 const __half2 sc = __builtin_bit_cast(__half2, s_sc[kb * ROWS + row]); 2171 const __half2 sc0 = __low2half2(sc), sc1 = __high2half2(sc); 2172 const uint32_t words[8] = {c0.x, c0.y, c0.z, c0.w, c1.x, c1.y, c1.z, c1.w}; 2173 uint32_t hw[16]; 2174#pragma unroll 2175 for (int i = 0; i < 8; ++i) { // values 4i .. 4i + 3 (s0: values 0-15, s1: 16-31) 2176 const uint32_t c = words[i] ^ 0x80808080u; 2177 const uint32_t p0 = __builtin_amdgcn_perm(c, 0x64646464u, 0x01050004u); 2178 const uint32_t p1 = __builtin_amdgcn_perm(c, 0x64646464u, 0x03070206u); 2179 const __half2 s2 = i < 4 ? sc0 : sc1; 2180 hw[2 * i] = __builtin_bit_cast(uint32_t, __hfma2(__hadd2(__builtin_bit_cast(__half2, p0), magic), s2, zero2)); 2181 hw[2 * i + 1] = __builtin_bit_cast(uint32_t, __hfma2(__hadd2(__builtin_bit_cast(__half2, p1), magic), s2, zero2)); 2182 } 2183 v16h a_lo, a_hi; 2184 __builtin_memcpy(&a_lo, &hw[0], 32); 2185 __builtin_memcpy(&a_hi, &hw[8], 32); 2186 mma_tiles<BN>(acc, a_lo, a_hi, s_act, kb, sub_lane, live_tiles); 2187 } 2188 }; 2189 fetch_stage(0); 2190 for (int s = 0; s < NS; ++s) { 2191 commit_stage(); 2192 __syncthreads(); 2193 if (s + 1 < NS) fetch_stage(s + 1); 2194 compute_stage(); 2195 __syncthreads(); 2196 } 2197 if constexpr (GU) gu_epilogue<BN>(acc, lds, tid, live_tiles, nrows, row0, fb, out); 2198 else down_epilogue<BN, D16>(acc, lds, tid, live_tiles, nrows, row0, fb, dm); 2199#else 2200 (void) b; (void) geo; (void) tb; (void) act; (void) src; (void) out; (void) dm; 2201 __builtin_trap(); 2202#endif 2203} 2204#endif // __HIPCC__ 2205 2206unsigned blocks(int64_t n, int per) { return (unsigned) ((n + per - 1) / per); } 2207 2208// per device: whether every kernel here runs (sm_80+, device code in this build, fits), and their occupancy 2209struct DevInfo { 2210 bool done = false, ok = false; 2211 bool g1151 = false; // gfx1151 (Strix Halo): Q8_0 down layers on the F16 kernel by default (q8d_f16_default) 2212 bool w12 = false; // gfx12 (RDNA4): the native kernels only - the Q2_0 pack's (moe_fused.cu) are not ported there 2213 bool t75 = false; // Turing (STRATA_PF_FUSED_SM75): the Q2_0 / Q2_0 layers only (what was set up and checked there) 2214 unsigned t75_set = 0, t75_w4 = 0; // Turing: the i-quant gate/up and IQ4_NL down instances set up (t75_bit), and those whose 64-row (WW 4) instance fits 64 KB too 2215 bool q2n = false; // sm_80+ with STRATA_PF_FUSED_Q2N=1: the native Q2_0 pack's Q2_0 gate/up is set up as well (opt-in) 2216 bool s120 = false; // compute capability 12.0 (sm120_series.hpp): the default for the formats measured there 2217 bool s121 = false; // GB10 (sm_121, DGX Spark): 128-row tiles win from 65,536 routed rows a launch up (pick_ww) 2218 bool s86 = false; // the sm_86 rule class, compute capability 8.0 / 8.6 / 8.9 (RTX A6000 / A40 / RTX 3090 class; A100 and RTX 40 since 11-12): the same default as s120 with 80+ SMs (STRATA_PF_FUSED_SM86=0 off) 2219 int sms = 0, occ = 1; 2220 int occ_nt = 0; // blocks per multiprocessor of the NT = 256 kernels (0: not set up) 2221 bool ws = false; // STRATA_PF_FUSED_WS: the warp-specialized kernels are set up 2222#if !defined(__HIPCC__) 2223 bool q50 = false; // sm_80+ with STRATA_PF_FUSED_Q50=1: the Q5_0 down kernels are set up (opt-in, phase 4 step 1) 2224 int occ_q50 = 1; // their blocks per multiprocessor (kept apart, so the other formats' grids do not change) 2225 bool kq = false; // the K-quant kernels (Q4_K / Q5_K gate/up, Q5_1 / Q8_0 down) are set up: by default on the cards of fused_tile::kq_takes, STRATA_PF_FUSED_KQ=1 on any sm_80+ card, =0 off 2226 int occ_kq = 1; // their blocks per multiprocessor (their own, as occ_q50) 2227 bool kq8 = false; // with kq: the Q8_0 down kernels are set up too (A2; UD-Q4_K_XL's five Q8_0-down layers) 2228 int occ_kq8 = 1; 2229 bool q8d = false; // sm_80+ with STRATA_PF_FUSED_Q8D=1: the same Q8_0 down kernels behind an i-quant gate/up (opt-in, phase 4; their occupancy is occ_kq8) 2230 bool q8gu = false; // sm_80+ with STRATA_PF_FUSED_Q8GU=1: the Q8_0 gate/up kernels, and the Q8_0 down ones behind them (opt-in, phase 4) 2231 int occ_q8gu = 1; // the Q8_0 gate/up kernels' blocks per multiprocessor (the down keeps occ_kq8) 2232 bool q6k = false; // sm_80+ with STRATA_PF_FUSED_Q6K=1: the Q6_K gate/up kernels, and the Q8_0 down ones behind them (opt-in, phase 4) 2233 int occ_q6k = 1; // the Q6_K gate/up kernels' blocks per multiprocessor (the down keeps occ_kq8) 2234 bool tq = false; // the trellis kernels (TQK6 / TQ2_T gate/up, TQK7 / TQ2_T down) are set up: by default on sm_86 with 80+ SMs, STRATA_PF_FUSED_TQ=1 on any sm_80+ (fused_tq_default.hpp) 2235 int occ_tq_gu = 1, occ_tq_d = 1; // their blocks per multiprocessor (their own) 2236 bool nvfp4 = false; // the NVFP4 kernels (gate/up and down) are set up on this sm_80+ card (whether a layer takes them: native_supported) 2237 int occ_nvfp4_gu = 1, occ_nvfp4_d = 1; // their blocks per multiprocessor (their own) 2238#endif 2239}; 2240std::mutex g_mu; 2241DevInfo g_dev[32]; 2242 2243#if !defined(__HIPCC__) 2244// the activation stages of native_kernel on the current device: its ASTAGES_DEV (2 below sm_80) 2245int host_astages() { 2246 static int per[64] = {}; 2247 int dev = 0; 2248 if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return ASTAGES; } 2249 if (!per[dev]) { 2250 int major = 0; 2251 cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev); 2252 per[dev] = strata::cc_major_of(major) < 8 ? 2 : ASTAGES; 2253 } 2254 return per[dev]; 2255} 2256// STRATA_PF_FUSED_Q50 on CUDA: "1" sets the Q5_0 down kernels up (dev_info) and lets native_supported take the Q5_0-down layers on 2257// any sm_80+ card; anything else (unset, "0") keeps those layers on the dequantize + FP16 GEMM path (the default) 2258bool q50_cuda_requested() { 2259 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q50"); return v != nullptr && v[0] == '1'; }(); 2260 return on; 2261} 2262// STRATA_PF_FUSED_Q8D on CUDA: "1" sets the Q8_0 down kernels up (dev_info) and lets native_supported take a Q8_0 down behind an i-quant 2263// gate/up (the pinned UD-IQ4_XS: IQ3_S / Q8_0 in 4 layers, IQ4_XS / Q8_0 in 1) on any sm_80+ card; anything else keeps those layers on MMQ 2264bool q8d_cuda_requested() { 2265 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q8D"); return v != nullptr && v[0] == '1'; }(); 2266 return on; 2267} 2268// STRATA_PF_FUSED_Q8GU on CUDA: "1" sets the Q8_0 gate/up kernels up (dev_info) and lets native_supported take a Q8_0 gate/up with a Q8_0 2269// down (the Q8_0 set's 48 layers, UD-Q6_K_XL's one Q8_0 layer) on any sm_80+ card; anything else keeps those layers on MMQ 2270bool q8gu_cuda_requested() { 2271 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q8GU"); return v != nullptr && v[0] == '1'; }(); 2272 return on; 2273} 2274// STRATA_PF_FUSED_Q6K on CUDA: "1" sets the Q6_K gate/up kernels up (dev_info) and lets native_supported take a Q6_K gate/up with a Q8_0 2275// down (UD-Q6_K_XL's 47 layers, UD-Q5_K_XL's one Q6_K layer) on any sm_80+ card; anything else keeps those layers on MMQ 2276bool q6k_cuda_requested() { 2277 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q6K"); return v != nullptr && v[0] == '1'; }(); 2278 return on; 2279} 2280// STRATA_PF_FUSED_TQ on CUDA (the rule is include/strata/prefill/fused_tq_default.hpp, pinned by tests/prefill/fused_tq_default_test.cpp): unset = the trellis kernels are 2281// set up (dev_info) and native_supported takes Gyro-S's TQK6 / TQK7 layers on a card of the sm_86 class (compute capability 8.6, and 8.0 / 8.9 since 11-12; fused_tile::s86_class) with 80+ SMs (RTX A6000 class; the A6000 gate series is 2282// Gyro-S); "1" takes them on any sm_80+ card, and Gyro-M's TQ2_T / TQ2_T layers too (no gate series: they are never taken by default); any other value ("0") and every other card: MMQ. The Hadamard rotations of a folded layer run inside 2283// experts_native (set_down_rotation), the gate/up input arrives rotated. 2284int tq_env() { 2285 static const int v = strata::prefill::fused_tq::env_value(); 2286 return v; 2287} 2288[[maybe_unused]] bool kq_on(); // STRATA_PF_FUSED_KQ (below): the HIP builds' reading of the switch (the CUDA builds decide in dev_info with fused_tile::kq_takes) 2289template <int T, bool GU, int WW, int NT = THREADS, bool F16 = false> bool setup_ww(int& occ) { 2290 cudaFuncAttributes fa{}; 2291 if (cudaFuncGetAttributes(&fa, native_kernel<T, GU, WW, NT, F16>) != cudaSuccess || fa.ptxVersion < 75) return false; 2292 const size_t sm = smem_bytes(T, WW, host_astages(), NT, F16, GU); 2293 { // the block's shared memory on the card as the code sees it (STRATA_EMULATE_CC=75 on a bigger card: 64 KB, so the Turing set-up does not accept what a real Turing card refuses) 2294 int dev = 0, optin = 0; 2295 cudaGetDevice(&dev); 2296 if (cudaDeviceGetAttribute(&optin, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev) == cudaSuccess && sm > (size_t) strata::smem_optin_of(optin)) return false; 2297 } 2298 if (cudaFuncSetAttribute(native_kernel<T, GU, WW, NT, F16>, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) sm) != cudaSuccess) 2299 return false; 2300 int o = 0; 2301 if (cudaOccupancyMaxActiveBlocksPerMultiprocessor(&o, native_kernel<T, GU, WW, NT, F16>, NT, sm) != cudaSuccess || o < 1) 2302 return false; 2303 occ = std::min(occ, o); 2304 return true; 2305} 2306template <int T, bool GU> bool setup_one(int& occ) { return setup_ww<T, GU, 4>(occ) && setup_ww<T, GU, 2>(occ); } 2307// Turing: one i-quant instance pair. The 128-row (WW 2) instance must fit and be set up; the 64-row one (WW 4: 256 weight rows, about 6 KB more shared memory) only if it fits 64 KB too. 2308// A format not in kT75Formats is left out. A failed WW 2 set-up leaves occ as it was. 2309template <int T, bool GU> void t75_setup(DevInfo& d, int& occ) { 2310 if ((kT75Formats & t75_bit(T, GU)) == 0) return; 2311 int o = occ; 2312 if (!setup_ww<T, GU, 2>(o)) return; 2313 int o4 = o; 2314 if (setup_ww<T, GU, 4>(o4)) { d.t75_w4 |= t75_bit(T, GU); o = o4; } 2315 occ = o; 2316 d.t75_set |= t75_bit(T, GU); 2317} 2318// STRATA_PF_FUSED_NT=256 (sm_80+, opt-in): the 256-thread kernels (native_kernel's NT) 2319// STRATA_PF_FUSED_F16=1 (sm_80+, opt-in, changes the output at rounding level): the NT = 256 kernels' F16 products 2320bool f16c_requested() { 2321 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_F16"); return v != nullptr && v[0] == '1'; }(); 2322 return on; 2323} 2324bool nt256_requested() { 2325 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_NT"); return v != nullptr && std::atoi(v) == 256; }(); 2326 return on || f16c_requested(); 2327} 2328bool ws_requested() { 2329 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_WS"); return v != nullptr && v[0] == '1'; }(); 2330 return on; 2331} 2332template <int T, bool GU> bool setup_ws() { 2333 cudaFuncAttributes fa{}; 2334 if (cudaFuncGetAttributes(&fa, native_ws_kernel<T, GU>) != cudaSuccess || fa.ptxVersion < 80) return false; 2335 if (cudaFuncSetAttribute(native_ws_kernel<T, GU>, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) ws_smem_bytes(T)) != 2336 cudaSuccess) return false; 2337 int o = 0; 2338 return cudaOccupancyMaxActiveBlocksPerMultiprocessor(&o, native_ws_kernel<T, GU>, WS_THREADS, ws_smem_bytes(T)) == cudaSuccess && o >= 1; 2339} 2340template <int T, bool GU> bool setup_nt(int& occ) { 2341 return f16c_requested() ? setup_ww<T, GU, 2, 256, true>(occ) : setup_ww<T, GU, 2, 256>(occ); 2342} 2343#endif 2344 2345#if defined(__HIPCC__) 2346// Resident blocks per multiprocessor for the gfx11 WMMA prompt-expert kernels' persistent grids. HIP counts a gfx11 2347// WGP (two CUs, which these kernels use as one in the default WGP mode) as one multiprocessor, and the occupancy query 2348// answers 1 block for these kernels where 2 run side by side - so the grid was half the GPU's room. gfx1151 (20 2349// WGPs), 8192 tokens x top 10, IQ3_S / IQ4_NL, one layer: 1 block per WGP 60.7 ms, 2 44.7, 3 49.9, 4 45.2 (the same 2350// work items and arithmetic: the results do not change). STRATA_PF_OCC=N sets the blocks per WGP (an experiment knob). 2351static int wgp_blocks(int occ) { 2352 static const int env = [] { 2353 const char* v = std::getenv("STRATA_PF_OCC"); 2354 return v != nullptr ? std::atoi(v) : 0; 2355 }(); 2356 return env > 0 ? env : 2 * std::max(occ, 1); 2357} 2358// The occupancy variants' (W, X1) blocks per WGP: as many as are resident at once, so the persistent grid has no second 2359// wave. gfx12 (a WGP: 128 KB of LDS, 1536 VGPRs per SIMD; the compiler's occupancy, -Rpass-analysis=kernel-resource-usage, 2360// clang 23): gate/up IQ2_S (37,120 B of LDS) and IQ2_XS (203 VGPRs) 3, every other one 4. R9700, IQ2_S / IQ4_NL, one 2361// layer, 4 runs each: 4K 8.80-8.93 ms at 4, 8.18-8.33 at 3; 16K -3%; IQ3_S / IQ4_NL 2-3% slower at 3. gfx11 keeps 4 2362// (measured on gfx1151). Not from the occupancy API: it counts 64 KB of LDS per multiprocessor (37,120 B -> 1 block). 2363// STRATA_PF_OCC_W=N sets it for every occupancy variant (an experiment knob). 2364static int occ_blocks(bool w12, int type, bool gu) { 2365 static const int env = [] { 2366 const char* v = std::getenv("STRATA_PF_OCC_W"); 2367 return v != nullptr ? std::atoi(v) : 0; 2368 }(); 2369 if (env > 0) return env; 2370 // gfx11 too (gfx1151, IQ2_S gate/up: 235 VGPRs and 37,120 B of LDS leave room for 3 blocks of 8 waves per WGP; a grid of 4 per WGP 2371 // ran a second, lopsided wave: 16K tokens, one layer, IQ2_S / IQ4_NL 81.7 -> 76.5 ms) 2372 // The K-quant kernels (Q4_K / Q5_K gate/up, Q5_1 down) reach this launch only with the trial switch STRATA_PF_KQ_OCC=1 (experts_native): 2373 // 3 blocks per WGP, measured on gfx1201 (R9700, hip_prefill_fused_iq, one layer, 8,192 tokens x top 10 over 512 experts): Q4_K / Q5_1 16.57 ms 2374 // on the default launch, 15.96 at 4 blocks, 15.54 at 3, 16.52 at 2; Q5_K / Q5_1 17.25 / 16.69 / 16.20 / 17.18; parity and outputs unchanged. 2375 if (type == T_Q4_K || type == T_Q5_K || type == T_Q5_1) return 3; 2376 return gu && (type == T_IQ2_S || type == T_IQ2_XS) ? 3 : 4; 2377} 2378#endif 2379const DevInfo& dev_info() { 2380 int dev = 0; 2381 cudaGetDevice(&dev); 2382 std::lock_guard<std::mutex> lk(g_mu); 2383 DevInfo& d = g_dev[dev & 31]; 2384 if (d.done) return d; 2385 d.done = true; 2386#if defined(STRATA_HIP_GFX906) 2387 // gfx906 reports compute capability 9.0 through HIP, but the mma.sync bodies above are CUDA sm_80+ only and 2388 // empty in a hipcc build: never available here (the prompt path keeps its own expert GEMMs) 2389 return d; 2390#elif defined(__HIPCC__) 2391 { // gfx11 / gfx12 with this build's code: the WMMA kernels (one occupancy for all: the same shape and LDS budget) 2392 cudaDeviceGetAttribute(&d.sms, cudaDevAttrMultiProcessorCount, dev); 2393 cudaDeviceProp prop; 2394 hipFuncAttributes fa{}; 2395 const bool props = cudaGetDeviceProperties(&prop, dev) == cudaSuccess; 2396 d.w12 = props && strata::kernels::gfx_arch_is_gfx12_wmma(prop.gcnArchName); 2397 d.g1151 = props && strata::kernels::gfx_arch_is_gfx1151(prop.gcnArchName); 2398 // the arch lists of STRATA_NAT_W11 / STRATA_NAT_W12 (gfx_arch.hpp: never a prefix - another gfx11 / gfx12 part 2399 // has the trapping body, which hipFuncGetAttributes and the occupancy query still accept). And the body this 2400 // build loaded for the device is the real one: the __builtin_trap() stub (a gfx11-generic / gfx12-generic or 2401 // SPIR-V build on a listed card) declares no LDS, the real kernel at least its weight buffers. 2402 if (!props || !(strata::kernels::gfx_arch_is_gfx11_wmma(prop.gcnArchName) || d.w12) || 2403 hipFuncGetAttributes(&fa, reinterpret_cast<const void*>(native_w11_kernel<T_IQ3_XXS, true>)) != hipSuccess || 2404 fa.sharedSizeBytes < (size_t) 2 * NW_ROWS * WLD) { 2405 cudaGetLastError(); 2406 return d; 2407 } 2408 int o = 0; 2409 if (hipOccupancyMaxActiveBlocksPerMultiprocessor(&o, native_w11_kernel<T_IQ2_S, true>, NW_THREADS, 0) != hipSuccess) 2410 o = 0; 2411 d.ok = o >= 1; 2412 d.occ = d.ok ? o : 1; 2413 cudaGetLastError(); 2414 return d; 2415 } 2416#else 2417 int major = 0, minor = 0; 2418 cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev); 2419 cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev); 2420 d.s120 = strata::sm120_default(major, minor); // (takes the raw attributes, as is_sm120 does) 2421 d.s121 = major == 12 && minor == 1; // GB10 (DGX Spark): its own tile threshold, see pick_ww 2422 // the sm_86 rule class (raw attributes, as s120; fused_tile::s86_class: compute capability 8.6, and since 11-12 8.0 and 8.9): STRATA_PF_FUSED_SM86=0 is a diagnostic for bug reports (MMQ on these cards again); it goes with MMQ 2423 static const bool sm86_off = [] { const char* v = std::getenv("STRATA_PF_FUSED_SM86"); return v != nullptr && v[0] == '0'; }(); 2424 d.s86 = strata::prefill::fused_tile::s86_class(major, minor, sm86_off); 2425 major = strata::cc_major_of(major); // STRATA_EMULATE_CC counts (tests) 2426 minor = strata::cc_minor_of(minor); 2427 cudaDeviceGetAttribute(&d.sms, cudaDevAttrMultiProcessorCount, dev); 2428 // Turing (sm_75): the same kernels with plain copies and m8n8k16 MMAs, opt-in (STRATA_PF_FUSED_SM75=1) 2429 static const bool sm75 = [] { const char* v = std::getenv("STRATA_PF_FUSED_SM75"); return v != nullptr && v[0] == '1'; }(); 2430 if (major < 8 && !(major == 7 && minor == 5 && sm75)) return d; 2431 int occ = 1 << 20; 2432 if (major < 8) { // Turing: the canonical Q2_0 pack's kernels (the codebook formats' do not fit 64 KB at 4 warps a row) 2433 d.t75 = true; 2434 d.ok = setup_one<T_Q2_0, true>(occ) && setup_one<T_Q2_0, false>(occ); 2435 // the i-quant pairs (kT75Formats) on the same kernels, in a build that takes the Turing set (STRATA_PF_FUSED_SM75); STRATA_PF_FUSED_SM75_IQ=0 leaves them off (a diagnostic: the int8 dp4a kernels again) 2436 static const bool iq_off = [] { const char* v = std::getenv("STRATA_PF_FUSED_SM75_IQ"); return v != nullptr && v[0] == '0'; }(); 2437 if (d.ok && !iq_off) { 2438 t75_setup<T_IQ2_XXS, true>(d, occ); t75_setup<T_IQ2_XS, true>(d, occ); t75_setup<T_IQ2_S, true>(d, occ); 2439 t75_setup<T_IQ3_XXS, true>(d, occ); t75_setup<T_IQ3_S, true>(d, occ); t75_setup<T_IQ4_XS, true>(d, occ); 2440 t75_setup<T_IQ4_NL, false>(d, occ); 2441 } 2442 d.occ = d.ok ? occ : 1; 2443 cudaGetLastError(); 2444 return d; 2445 } 2446 d.ok = setup_one<T_IQ2_XXS, true>(occ) && setup_one<T_IQ2_XS, true>(occ) && setup_one<T_IQ2_S, true>(occ) && 2447 setup_one<T_IQ3_XXS, true>(occ) && setup_one<T_IQ3_S, true>(occ) && setup_one<T_IQ4_XS, true>(occ) && 2448 setup_one<T_Q2_0, false>(occ) && setup_one<T_IQ4_NL, false>(occ); // (Q2_0 gate/up: the Turing opt-in's, or below) 2449 // STRATA_PF_FUSED_Q2N=1: the native Q2_0 pack's Q2_0 gate/up on these kernels here too (what #2145 took with 2450 // STRATA_PF_FUSED=1 on every CUDA card until b6443f3a kept it to Turing). Its own variable, so STRATA_PF_FUSED=1 keeps 2451 // its bits: unset, the kernel is not set up and the Q2_0 layers keep MMQ. A failed setup leaves occ as it was. 2452 static const bool q2n = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q2N"); return v != nullptr && v[0] == '1'; }(); 2453 if (int o2 = occ; q2n && d.ok && setup_one<T_Q2_0, true>(o2)) { d.q2n = true; occ = o2; } 2454 d.occ = d.ok ? occ : 1; 2455 // STRATA_PF_FUSED_Q50=1 (opt-in on CUDA, off by default): the Q5_0 down projections (UD-IQ4_XS v31: 33 of 48 layers, which have 2456 // no MMQ tile here and otherwise take the dequantize + FP16 GEMM path) on native_kernel - the decode is the HIP kernels' 2457 // (load_unit / convert<T_Q5_0>: int8 codes q - 16, one scale per 32 values), the products the IQ4_NL down's (int8 mma.sync, 2458 // FP32 scales and sums). Their occupancy is their own (occ_q50), so the other formats' launches do not change; a failed 2459 // setup leaves those layers on the FP16 path. 2460 if (int o3 = 1 << 20; d.ok && q50_cuda_requested() && setup_one<T_Q5_0, false>(o3)) { d.q50 = true; d.occ_q50 = o3; } 2461 // The K-quant experts of UD-Q4_K_XL - gate/up Q4_K / Q5_K, down Q5_1 (or Q8_0) - on native_kernel instead of MMQ (libstrata_kq). On CUDA they are the 2462 // default on the cards the i-quant default rule takes (fused_tile::kq_takes: sm_86-class and sm_120 with 80+ SMs, the staged launches on 40-79 SMs unless 2463 // STRATA_PF_FUSED_SM40=0), STRATA_PF_FUSED_KQ=1 sets them up on every sm_80+ card, STRATA_PF_FUSED_KQ=0 (or STRATA_PF_FUSED=0, native_supported) leaves them on MMQ. 2464 // The decode is the HIP kernels' (load_unit / convert: int8 codes, a scale and a minimum per 32 values); the minimum enters as s1 d_x (sum of the activation 2465 // codes), from the sums quant_act_nat_kernel and the K-quant gate/up epilogue store in the spare bytes. Their own occupancy (occ_kq); a failed setup leaves 2466 // those layers on MMQ. (kq_on() below is the HIP builds' reading of the switch; strata-q36 sets STRATA_PF_FUSED_KQ=1 itself, so its cards are unchanged.) 2467 static const int kq_env = strata::prefill::fused_tile::pf_env(std::getenv("STRATA_PF_FUSED_KQ")); 2468 static const int kq_sm40_env = strata::prefill::fused_tile::sm40_env_of(std::getenv("STRATA_PF_FUSED_SM40")); 2469 const bool kq_want = strata::prefill::fused_tile::kq_takes(kq_env, {d.s120, d.s121, d.sms, d.s86}, kq_sm40_env); 2470 if (int o4 = 1 << 20; d.ok && kq_want && setup_one<T_Q4_K, true>(o4) && setup_one<T_Q5_K, true>(o4) && setup_one<T_Q5_1, false>(o4)) { 2471 d.kq = true; 2472 d.occ_kq = o4; 2473 // (A2) the Q8_0 down behind a K-quant gate/up: int8 codes and one scale per 32 values, no minimum (load_unit / convert<T_Q8_0>); 2474 // its own occupancy, and a failed setup leaves those layers on MMQ 2475 if (int o5 = 1 << 20; setup_one<T_Q8_0, false>(o5)) { d.kq8 = true; d.occ_kq8 = o5; } 2476 } 2477 // STRATA_PF_FUSED_Q8D=1 (opt-in on CUDA, off by default): the Q8_0 down behind an i-quant gate/up on the same Q8_0 down instances as 2478 // A2's. The Q8_0 down reads no code sums (has_min is false), so the i-quant gate/up epilogue's H feeds it unchanged. The occupancy is 2479 // the one A2 found for the same instances (occ_kq8); a failed setup leaves those layers on MMQ. 2480 if (int o6 = 1 << 20; d.ok && q8d_cuda_requested() && setup_one<T_Q8_0, false>(o6)) { d.q8d = true; d.occ_kq8 = o6; } 2481 // STRATA_PF_FUSED_Q8GU=1 (opt-in on CUDA, off by default): a Q8_0 gate/up (int8 codes, one scale per 32 values: two blocks a 64-value 2482 // stage, as the Q8_0 down reads them) with a Q8_0 down on A2's down instances. Both set up or neither; the gate/up's occupancy is its own. 2483 if (int o7 = 1 << 20, o8 = 1 << 20; d.ok && q8gu_cuda_requested() && setup_one<T_Q8_0, true>(o7) && setup_one<T_Q8_0, false>(o8)) { 2484 d.q8gu = true; 2485 d.occ_q8gu = o7; 2486 d.occ_kq8 = o8; 2487 } 2488 // STRATA_PF_FUSED_Q6K=1 (opt-in on CUDA, off by default): a Q6_K gate/up (int8 codes q - 32, an int8 scale times d per 16 values: the 2489 // per-16-value scale path of IQ2_XS / IQ2_S) with a Q8_0 down on A2's down instances. Both set up or neither; its own occupancy. 2490 if (int o9 = 1 << 20, o10 = 1 << 20; d.ok && q6k_cuda_requested() && setup_one<T_Q6_K, true>(o9) && setup_one<T_Q8_0, false>(o10)) { 2491 d.q6k = true; 2492 d.occ_q6k = o9; 2493 d.occ_kq8 = o10; 2494 } 2495 // the trellis experts (Gyro-S TQK6 gate/up + TQK7 down, Gyro-M TQ2_T both) on native_kernel, by default on sm_86 with 80+ SMs, STRATA_PF_FUSED_TQ=1 on any sm_80+ card, =0 off: 2496 // the decode is MMQ's (tq.cuh: 15-bit states, the int8 codebook tq_lut_i8, one scale d * TQ_LUT_I8_SCALE a 128-value block), the products the int8 2497 // mma.sync path. All four instance pairs set up or none; their own occupancy. 2498 if (int og = 1 << 20, od = 1 << 20; d.ok && strata::prefill::fused_tq::on(tq_env(), d.s86, d.sms) && setup_one<T_TQK6, true>(og) && setup_one<T_TQ2_T, true>(og) && 2499 setup_one<T_TQK7, false>(od) && setup_one<T_TQ2_T, false>(od)) { 2500 d.tq = true; 2501 d.occ_tq_gu = og; 2502 d.occ_tq_d = od; 2503 } 2504 // NVFP4 (ModelOpt packs: gate/up and down both NVFP4): 64-value blocks, a scale per 16 values on the per-16 path, the per-expert scales on the FP32 products. Set up on every 2505 // sm_80+ card; native_supported decides which layers take it (STRATA_PF_FUSED=1 as given or as a build without MMQ sets it, STRATA_PREFILL_NVFP4 unset or w4a8). 2506 if (int og = 1 << 20, od = 1 << 20; d.ok && setup_one<T_NVFP4, true>(og) && setup_one<T_NVFP4, false>(od)) { 2507 d.nvfp4 = true; 2508 d.occ_nvfp4_gu = og; 2509 d.occ_nvfp4_d = od; 2510 } 2511 if (d.ok && nt256_requested()) { 2512 int o2 = 1 << 20; 2513 const bool ok2 = setup_nt<T_IQ2_XXS, true>(o2) && setup_nt<T_IQ2_XS, true>(o2) && setup_nt<T_IQ2_S, true>(o2) && 2514 setup_nt<T_IQ3_XXS, true>(o2) && setup_nt<T_IQ3_S, true>(o2) && setup_nt<T_IQ4_XS, true>(o2) && 2515 setup_nt<T_Q2_0, false>(o2) && setup_nt<T_IQ4_NL, false>(o2) && 2516 (!d.q2n || setup_nt<T_Q2_0, true>(o2)); // (the Q2_0 gate/up of STRATA_PF_FUSED_Q2N=1 too) 2517 d.occ_nt = ok2 ? o2 : 0; 2518 if (!ok2) std::fprintf(stderr, "prefill fused experts (native): STRATA_PF_FUSED_NT=256 is not available here\n"); 2519 } 2520 if (d.ok && ws_requested()) { 2521 d.ws = setup_ws<T_IQ2_XXS, true>() && setup_ws<T_IQ2_XS, true>() && setup_ws<T_IQ2_S, true>() && 2522 setup_ws<T_IQ3_XXS, true>() && setup_ws<T_IQ3_S, true>() && setup_ws<T_IQ4_XS, true>() && 2523 setup_ws<T_Q2_0, false>() && setup_ws<T_IQ4_NL, false>() && 2524 (!d.q2n || setup_ws<T_Q2_0, true>()); // (the Q2_0 gate/up of STRATA_PF_FUSED_Q2N=1 too) 2525 if (!d.ws) std::fprintf(stderr, "prefill fused experts (native): STRATA_PF_FUSED_WS=1 is not available here\n"); 2526 } 2527 cudaGetLastError(); 2528#endif 2529 return d; 2530} 2531 2532// opt-in on top of STRATA_PF_FUSED=1: STRATA_PF_FUSED_KQ=1 takes UD-Q4_K_XL's formats (the output differs from the FP16 2533// expert path's at rounding level; KL-gated, see aurora_s23.md) 2534bool kq_on() { 2535 static const bool on = [] { 2536 const char* v = std::getenv("STRATA_PF_FUSED_KQ"); 2537 if (v != nullptr && v[0] != '\0') return v[0] == '1'; 2538#if defined(__HIPCC__) && defined(_WIN32) 2539 // Round 0215 (STRIX-FIT): on a Strix Halo (gfx1151) under Windows the K-quant experts of UD-Q4_K_XL (Q4_K / Q5_K gate/up, Q5_1 / Q8_0 down) 2540 // take the fused matrix-core kernels (the F16 ones with STRATA_PF_F16=1, which the gfx1151 prompt set turns on) by default. 2541 // STRATA_PF_FUSED_KQ=0 or STRATA_GFX1151_DEFAULTS=0 keeps the earlier route. 2542 const char* off = std::getenv("STRATA_GFX1151_DEFAULTS"); 2543 if (off != nullptr && off[0] == '0') return false; 2544 int dev = 0; 2545 cudaDeviceProp prop; 2546 if (cudaGetDevice(&dev) == cudaSuccess && cudaGetDeviceProperties(&prop, dev) == cudaSuccess) 2547 return strata::kernels::gfx_arch_is_gfx1151(prop.gcnArchName); 2548 cudaGetLastError(); 2549#endif 2550 return false; 2551 }(); 2552 return on; 2553} 2554// STRATA_PF_F16=1 (gfx11): the formats native_f16_kernel takes (the K-quants are moe_f16_kq.cu's) 2555bool f16_gu(int t) { 2556 return t == T_IQ2_XXS || t == T_IQ2_XS || t == T_IQ2_S || t == T_IQ3_XXS || t == T_IQ3_S || t == T_IQ4_XS; 2557} 2558bool f16_d(int t) { return t == T_Q2_0 || t == T_IQ4_NL || t == T_Q8_0 || t == T_Q5_0; } 2559#if defined(__HIPCC__) 2560bool tq_hip_requested(); // (STRATA_PF_FUSED_TQ on HIP: below) 2561bool tq_hip_device(const DevInfo& d); // (the trellis instances on this device: gfx12, gfx1151 by its own rule; below) 2562bool q8gu_hip_requested(); // (STRATA_PF_FUSED_Q8GU on HIP: below) 2563bool q6k_hip_requested(); // (STRATA_PF_FUSED_Q6K on HIP: below) 2564#endif 2565bool gu_covered(int t) { 2566 return t == T_IQ2_XXS || t == T_IQ2_XS || t == T_IQ2_S || t == T_IQ3_XXS || t == T_IQ3_S || t == T_IQ4_XS 2567#if !defined(__HIPCC__) 2568 || (t == T_Q2_0 && (dev_info().t75 || dev_info().q2n)) // Q2_0 gate/up: the Turing opt-in, or STRATA_PF_FUSED_Q2N=1 on sm_80+ 2569 || ((t == T_Q4_K || t == T_Q5_K) && dev_info().kq) // STRATA_PF_FUSED_KQ=1 and its kernels set up 2570 || (t == T_Q8_0 && dev_info().q8gu) // STRATA_PF_FUSED_Q8GU=1 and its kernels set up 2571 || (t == T_Q6_K && dev_info().q6k) // STRATA_PF_FUSED_Q6K=1 and its kernels set up 2572 || ((t == T_TQK6 || t == T_TQ2_T) && dev_info().tq) // the trellis kernels set up (default on sm_86 with 80+ SMs, or STRATA_PF_FUSED_TQ=1) 2573 || (t == T_NVFP4 && dev_info().nvfp4) // the NVFP4 kernels set up (native_supported: the NVFP4 / NVFP4 pair, its switches) 2574#endif 2575#if defined(__HIPCC__) 2576 || (t == T_Q2_0 && dev_info().w12) // gfx12: the native Q2_0 pack's gate/up on native_w11_kernel (64-value blocks) 2577 || ((t == T_Q4_K || t == T_Q5_K) && kq_on()) // the gfx11 kernels only 2578 || ((t == T_TQK6 || t == T_TQ2_T) && tq_hip_device(dev_info())) // STRATA_PF_FUSED_TQ=1 on gfx12; gfx1151 by fused_tq_hip.hpp 2579 || (t == T_Q8_0 && dev_info().w12 && q8gu_hip_requested()) // STRATA_PF_FUSED_Q8GU=1 on gfx12 2580 || (t == T_Q6_K && dev_info().w12 && q6k_hip_requested()) // STRATA_PF_FUSED_Q6K=1 on gfx12 2581#endif 2582 ; 2583} 2584bool d_covered(int t) { 2585 return t == T_Q2_0 || t == T_IQ4_NL 2586#if defined(__HIPCC__) 2587 || ((t == T_Q5_1 || t == T_Q8_0) && kq_on()) 2588#endif 2589 ; 2590} 2591// d_covered on this device: STRATA_PF_F16=1 also takes Q8_0 down on gfx11 without STRATA_PF_FUSED_KQ - the F16 kernel 2592// has no minimum term, and its output already differs from the int8 kernels' (UD-IQ4_XS: the five Q8_0-down layers 2593// would otherwise leave the fused path for MMQ). 2594// gfx1151: a Q8_0 down (UD-IQ4_XS and UD-Q4_K_XL keep five layers of it) takes the F16 matrix-core kernel without STRATA_PF_F16, 2595// whose output differs from the int8 kernels' only for that layer; every other format keeps the bits it has. Without it those 2596// layers leave the fused path for MMQ: UD-IQ4_XS 4K / 8K prompts 1,057 / 1,128 against 1,192 / 1,257 tok/s with it (Windows 2597// gfx1151, eng034 with STRATA_PF_F16=1, which also covers these layers; A/B in phaseA-tests/strix-u). STRATA_PF_Q8D_F16=0 turns it off. 2598bool q8d_f16_default(const DevInfo& d) { 2599#if defined(__HIPCC__) 2600 if (d.w12 || !d.g1151) return false; 2601 static const bool off = [] { const char* v = std::getenv("STRATA_PF_Q8D_F16"); return v != nullptr && v[0] == '0'; }(); 2602 return !off; 2603#else 2604 (void) d; 2605 return false; 2606#endif 2607} 2608// Q5_0 down (UD-IQ4_XS-v31: 33 of 48 layers, with IQ3_XXS or IQ4_XS gate/up). Round 0217 (Strix Halo): on gfx1151 under Windows these layers take the F16 2609// matrix-core kernel (native_f16_kernel, the K-quant experts' route: int8 codes q - 16 times the block scale, F16 products, FP16 down rows for the combine) instead 2610// of leaving the fused path for gather + FP16 dequantize + hipBLASLt (RESULTS-STRIX2.md: 24-29% of the prompt). It needs the F16 prompt set (STRATA_PF_F16, on by 2611// default there), so with STRATA_PF_F16=0 the layers keep the earlier route. STRATA_PF_FUSED_Q50=0 (or STRATA_GFX1151_DEFAULTS=0) turns it off; =1 turns it on 2612// for any gfx1151 build (Linux: opt-in). gfx12 (RDNA4) keeps its int8 kernel (native_w11_kernel, int8 codes q - 16: its WMMA layout), reached through the same 2613// switch. gfx12 under Linux: ON by default since 0.2.15 (owner 11-10; R9700, UD-IQ4_XS-v31: prompt x1.31 / x1.47 / x1.16 at 4K / 8K / 20K, first-token KL 2614// against the FP16 path level with the default's, the neutral-writer forced-token gate PASS, first-8K reads PASS in 64 starts); gfx12 under Windows: opt-in 2615// (not measured there). STRATA_PF_FUSED_Q50=0 keeps the layers on gather + FP16 dequantize + hipBLASLt (the earlier route, kept two releases). 2616// The switch means the same everywhere: 1 on, 0 off, unset = the platform's default. 2617bool q50_on(const DevInfo& d) { 2618#if defined(__HIPCC__) 2619 if (!d.w12 && !d.g1151) return false; 2620 static const int env = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q50"); return v == nullptr || v[0] == '\0' ? -1 : (v[0] == '1' ? 1 : 0); }(); 2621 if (env >= 0) return env == 1; 2622 if (d.w12) return strata::core::kGfx12PromptDefaultsOn; // gfx12: on by default under Linux, opt-in under Windows 2623#if defined(_WIN32) 2624 const char* off = std::getenv("STRATA_GFX1151_DEFAULTS"); 2625 return !(off != nullptr && off[0] == '0'); 2626#else 2627 return false; 2628#endif 2629#else 2630 (void) d; 2631 return false; 2632#endif 2633} 2634#if defined(__HIPCC__) 2635// STRATA_PF_FUSED_Q8D on HIP (phase 4, opt-in, gfx12 only): "1" lets a Q8_0 down behind an i-quant gate/up (the pinned UD-IQ4_XS's five Q8_0-down layers: 2636// IQ3_S / Q8_0 4, IQ4_XS / Q8_0 1) take native_w11_kernel's Q8_0 down instead of MMQ, without STRATA_PF_FUSED_KQ (whose meaning is the K-quant experts). 2637// The same switch as CUDA's. No new kernel: the gfx12 int8 Q8_0 down instances exist for the K-quant route. gfx11 keeps its own rules (q8d_f16_default, 2638// STRATA_PF_F16): not measured with this switch. 2639bool q8d_hip_requested() { 2640 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q8D"); return v != nullptr && v[0] == '1'; }(); 2641 return on; 2642} 2643// STRATA_PF_FUSED_TQ on HIP (phase 4, opt-in, gfx12 only): "1" lets Gyro-S's TQK6 / TQK7 and Gyro-M's TQ2_T / TQ2_T layers take native_w11_kernel's 2644// trellis instances instead of MMQ (the decode is CUDA's: 15-bit states, MMQ's int8 codebook tq_lut_i8, one scale a 128-value block; Hadamard-folded 2645// downs through the FP32-H epilogue and rot_quant_h_kernel, as on CUDA). The same switch as CUDA's. gfx1151: STRATA_PF_TQ_G1151 (below); other gfx11: keeps its path. 2646bool tq_hip_requested() { 2647 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_TQ"); return v != nullptr && v[0] == '1'; }(); 2648 return on; 2649} 2650// STRATA_PF_TQ_G1151 (internal): gfx1151 (Strix Halo) takes the trellis instances too, with STRATA_PF_FUSED_TQ=1 (a build without MMQ sets it): 1 on, any other value 2651// off, unset = fused_tq_hip::kG1151DefaultOn under Windows (STRATA_GFX1151_DEFAULTS=0 off). The rule: include/strata/prefill/fused_tq_hip.hpp, pinned by 2652// tests/prefill/fused_tq_hip_test.cpp. gfx12 is unchanged (STRATA_PF_FUSED_TQ=1 alone); every other HIP device keeps its path. 2653static int tq_g1151_switch() { 2654 static const int v = strata::prefill::fused_tq_hip::parse_switch(std::getenv("STRATA_PF_TQ_G1151")); 2655 return v; 2656} 2657static bool tq_g1151_defaults_off() { 2658 static const bool off = [] { const char* v = std::getenv("STRATA_GFX1151_DEFAULTS"); return v != nullptr && v[0] == '0'; }(); 2659 return off; 2660} 2661#if defined(_WIN32) 2662constexpr bool kTqWin = true; 2663#else 2664constexpr bool kTqWin = false; 2665#endif 2666bool tq_hip_device(const DevInfo& d) { 2667 return strata::prefill::fused_tq_hip::device_on(d.w12, d.g1151, kTqWin, tq_hip_requested(), tq_g1151_switch(), tq_g1151_defaults_off(), 2668 strata::prefill::fused_tq_hip::kG1151DefaultOn); 2669} 2670static bool tq_hip_pair(const DevInfo& d, bool gyro_s, bool gyro_m) { 2671 return strata::prefill::fused_tq_hip::pair_on(d.w12, d.g1151, kTqWin, tq_hip_requested(), tq_g1151_switch(), tq_g1151_defaults_off(), 2672 strata::prefill::fused_tq_hip::kG1151DefaultOn, gyro_s, gyro_m); 2673} 2674// STRATA_PF_FUSED_Q8GU on HIP (phase 4, opt-in, gfx12 only): "1" lets a Q8_0 gate/up with a Q8_0 down (the Q8_0 set's 48 layers, UD-Q6_K_XL's one Q8_0 2675// layer) take native_w11_kernel's Q8_0 gate/up instance instead of MMQ (32-value blocks, two a stage, like the Q8_0 down; the same decode as CUDA's). The 2676// same switch as CUDA's. gfx11: not measured, keeps MMQ. 2677bool q8gu_hip_requested() { 2678 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q8GU"); return v != nullptr && v[0] == '1'; }(); 2679 return on; 2680} 2681// STRATA_PF_FUSED_Q6K on HIP (phase 4, opt-in, gfx12 only): "1" lets a Q6_K gate/up with a Q8_0 down (UD-Q6_K_XL's 47 layers, UD-Q5_K_XL's one) take 2682// native_w11_kernel's Q6_K gate/up instance instead of MMQ (the K-quant library, STRATA_MMQ_KQ=1, on gfx12). 256-value superblocks, an int8 scale per 2683// 16 values (native_w11's per-16 path), the same decode as CUDA's. The same switch as CUDA's. gfx11: not measured, keeps its route. 2684bool q6k_hip_requested() { 2685 static const bool on = [] { const char* v = std::getenv("STRATA_PF_FUSED_Q6K"); return v != nullptr && v[0] == '1'; }(); 2686 return on; 2687} 2688#endif 2689bool d_covered_on(int t, const DevInfo& d) { 2690#if defined(__HIPCC__) 2691 if (t == T_Q5_0) return d.w12 ? q50_on(d) : q50_on(d) && f16::requested(); // gfx1151 has no int8 kernel for Q5_0 down (the F16 prompt set must be on); gfx12 has only the int8 one 2692 if (t == T_Q8_0 && !d.w12 && (f16::requested() || q8d_f16_default(d))) return true; 2693 if (t == T_Q8_0 && d.w12 && (q8d_hip_requested() || q8gu_hip_requested() || q6k_hip_requested())) return true; // (behind an i-quant gate/up with Q8D, a Q8_0 one with Q8GU, a Q6_K one with Q6K: native_supported) 2694 if (t == T_TQK7 || t == T_TQ2_T) return tq_hip_device(d); // STRATA_PF_FUSED_TQ=1 on gfx12, gfx1151 by fused_tq_hip.hpp (behind a trellis gate/up only: native_supported) 2695#else 2696 if (t == T_Q5_0) return d.q50; // STRATA_PF_FUSED_Q50=1 and its kernels set up (dev_info) 2697 if (t == T_Q5_1) return d.kq; // STRATA_PF_FUSED_KQ=1 (behind a K-quant gate/up only: native_supported) 2698 if (t == T_TQK7 || t == T_TQ2_T) return d.tq; // STRATA_PF_FUSED_TQ=1 (behind a trellis gate/up only: native_supported) 2699 if (t == T_NVFP4) return d.nvfp4; // (behind an NVFP4 gate/up only: native_supported) 2700 if (t == T_Q8_0) return d.kq8 || d.q8d || d.q8gu || d.q6k; // STRATA_PF_FUSED_KQ=1, A2 (behind a K-quant gate/up), STRATA_PF_FUSED_Q8D=1 (behind an i-quant one), _Q8GU=1 (behind a Q8_0 one) or _Q6K=1 (behind a Q6_K one): native_supported 2701#endif 2702 (void) d; 2703 return d_covered(t); 2704} 2705 2706#if !defined(__HIPCC__) 2707// Turing: whether the pair has set-up kernels here - the canonical Q2_0 pair, or an i-quant gate/up of kT75Formats (t75_set) with an IQ4_NL (t75_set) or Q2_0 down 2708bool t75_pair_ok(const DevInfo& d, int gu_type, int d_type) { 2709 if (gu_type == T_Q2_0) return d_type == T_Q2_0; 2710 if (t75_bit(gu_type, true) == 0 || (d.t75_set & t75_bit(gu_type, true)) == 0) return false; 2711 return d_type == T_Q2_0 || (t75_bit(d_type, false) != 0 && (d.t75_set & t75_bit(d_type, false)) != 0); 2712} 2713// Turing: the instances a launch can run. The 64-row (WW 4) instance of a format that did not fit 64 KB does not exist, so such a launch takes the 128-row one 2714int t75_ww(const DevInfo& d, const NativeGeom& g, int ww) { 2715 if (ww != 4) return ww; 2716 const bool gu4 = g.gu_type == T_Q2_0 || (d.t75_w4 & t75_bit(g.gu_type, true)) != 0; 2717 const bool d4 = g.d_type == T_Q2_0 || (d.t75_w4 & t75_bit(g.d_type, false)) != 0; 2718 return gu4 && d4 ? 4 : 2; 2719} 2720#endif 2721#if defined(__HIPCC__) 2722int host_astages() { return ASTAGES; } // (#2145 defines it for CUDA above; HIP keeps the one depth) 2723#endif 2724template <int T, bool GU> 2725void launch(int ww, unsigned grid, const Batch& b, const NativeGeom& g, const Tables& tb, const void* act, 2726 const int32_t* src, void* out, float* dm, cudaStream_t s) { 2727 const uint8_t* a = (const uint8_t*) act; 2728 uint8_t* o = (uint8_t*) out; 2729 if (ww == 4) native_kernel<T, GU, 4><<<grid, THREADS, smem_bytes(T, 4, host_astages(), THREADS, false, GU), s>>>(b, g, tb, a, src, o, dm); 2730 else native_kernel<T, GU, 2><<<grid, THREADS, smem_bytes(T, 2, host_astages(), THREADS, false, GU), s>>>(b, g, tb, a, src, o, dm); 2731} 2732// the NT = 256 kernels (WW 2), their F16 form (STRATA_PF_FUSED_F16) or the warp-specialized kernel (ws: 2733// STRATA_PF_FUSED_WS) 2734template <int T, bool GU> 2735void launch(bool ws, unsigned grid, const WideBatch& b, const NativeGeom& g, const Tables& tb, const void* act, 2736 const int32_t* src, void* out, float* dm, cudaStream_t s) { 2737 const uint8_t* a = (const uint8_t*) act; 2738 uint8_t* o = (uint8_t*) out; 2739#if !defined(__HIPCC__) // (native_ws_kernel and the F16 path are CUDA only; d.ws is never set on HIP) 2740 if (ws) 2741 native_ws_kernel<T, GU><<<grid, WS_THREADS, ws_smem_bytes(T), s>>>(b, g, tb, a, src, o, dm); 2742 else if (f16c_requested()) 2743 native_kernel<T, GU, 2, 256, true><<<grid, 256, smem_bytes(T, 2, host_astages(), 256, true, GU), s>>>(b, g, tb, a, src, o, dm); 2744 else 2745#else 2746 (void) ws; 2747#endif 2748 native_kernel<T, GU, 2, 256><<<grid, 256, smem_bytes(T, 2, host_astages(), 256, false, GU), s>>>(b, g, tb, a, src, o, dm); 2749} 2750 2751// The work item's shape for a layer of `n` routed rows over `n_expert` experts: 128 routed rows when an expert has 2752// about one such tile (56 to 112 rows on average), else 64. Measured on the RTX 5070 (tests/cuda/prefill_fused_iq_test, 2753// a layer of 2048 / 3584 / 8192 tokens, ms, 64 -> 128): IQ2_S 5.2 -> 6.7, 9.5 -> 9.3, 17.5 -> 19.5; IQ2_XXS 4.2 -> 2754// 5.8, 7.4 -> 7.6, 13.6 -> 16.5; IQ3_S 5.6 -> 6.6, 10.2 -> 8.8, 18.4 -> 19.0; IQ3_XXS 5.1 -> 6.3, 9.1 -> 8.3, 16.5 -> 2755// 17.5 (256 rows was slower still). STRATA_PF_FUSED_TILE=64|128 forces one. 2756#if !defined(__HIPCC__) 2757// (staging) what the weight copies assume of a launch: the expert blobs are device memory (the prompt path's expert cache slots and its stage ring), not a host-mapped 2758// pointer, and every row and offset is 2-byte aligned (the decode reads 16-bit words at the same offsets inside the shared-memory window). A launch that breaks it 2759// does not end the process: it takes the unstaged 64-row kernel of the same formats (same output bits), whatever STRATA_PF_FUSED_TILE says, and the first such launch of a 2760// process logs one line. Checked on the first 256 launches of a process only (a pointer query is a driver call); once one launch failed, every later launch of the process 2761// takes the unstaged kernel without asking (fused_tile::StagedGuard: the decision is a pure function, tests/prefill/fused_tile_rule_test.cpp). 2762std::mutex g_staged_mu; 2763strata::prefill::fused_tile::StagedGuard g_staged_guard; 2764int g_staged_fallbacks = 0; // launches that took the unstaged kernel for this reason (the GPU test reads it) 2765 2766bool staged_blobs_ok(const Batch& b, const NativeGeom& g) { 2767 namespace ft = strata::prefill::fused_tile; 2768 { 2769 std::lock_guard<std::mutex> lk(g_staged_mu); 2770 const ft::StagedGuard::Step st = g_staged_guard.next(); 2771 if (st == ft::StagedGuard::Step::kTrust) return true; 2772 if (st == ft::StagedGuard::Step::kUnstaged) { ++g_staged_fallbacks; return false; } 2773 } 2774 char why[256]; 2775 why[0] = 0; 2776 bool ok = ft::staged_geometry_ok(g.gu_row, g.d_row, g.up_off, g.down_off); 2777 if (!ok) { 2778 std::snprintf(why, sizeof why, "rows and offsets must be 2-byte aligned (gu_row %zu d_row %zu up_off %zu down_off %zu)", g.gu_row, g.d_row, g.up_off, g.down_off); 2779 } else { 2780 for (int e = b.e0; e < b.e1; ++e) { 2781 const uint8_t* p = b.blob[e - b.e0]; 2782 cudaPointerAttributes pa{}; 2783 ft::BlobMem m = ft::BlobMem::kUnknown; 2784 if (cudaPointerGetAttributes(&pa, p) != cudaSuccess) cudaGetLastError(); // (not a pointer the driver knows: the launch's own error says so) 2785 else m = pa.type == cudaMemoryTypeDevice ? ft::BlobMem::kDevice : pa.type == cudaMemoryTypeManaged ? ft::BlobMem::kManaged : pa.type == cudaMemoryTypeHost ? ft::BlobMem::kHost : ft::BlobMem::kUnregistered; 2786 if (!ft::staged_blob_ok(m, reinterpret_cast<uintptr_t>(p))) { 2787 std::snprintf(why, sizeof why, "expert blobs must be in device memory at 2-byte aligned addresses (expert %d: %p, memory type %d)", e, (const void*) p, (int) pa.type); 2788 ok = false; 2789 break; 2790 } 2791 } 2792 } 2793 std::lock_guard<std::mutex> lk(g_staged_mu); 2794 if (g_staged_guard.report(ok)) 2795 std::fprintf(stderr, "prefill fused experts (native): %s; this launch and every later one in this process take the unstaged kernel (same results, slower)\n", why); 2796 if (!ok) ++g_staged_fallbacks; 2797 return ok; 2798} 2799#endif 2800// `staged`: the launch's gate/up kernel is a weight-staging instance (sw_on); the rule then takes 128 rows from every size on the sm_86 / sm_120 cards under 80 SMs. 2801// `wide`: the launch's gate/up is Q6_K or Q8_0: on sm_86 cards with 40+ SMs the rule takes 128 rows above 112 rows an expert (fused_tile_rule.hpp). 2802// `kq`: 0, or the launch's pair is one of Flash-Next's K-quant pairs (fused_tile::kq_flash_kind: 1 a Q5_1 down, 2 a Q8_0 down): on sm_86-class cards with 80-99 SMs the rule takes 128 rows from 5.8 rows an expert up. 2803// STRATA_PF_FUSED_TILE=64|128 as a work item (4 / 2), 0 when the variable does not force one. Read once. 2804int forced_ww() { 2805 static const int forced = [] { 2806 const char* v = std::getenv("STRATA_PF_FUSED_TILE"); 2807 const int t = v ? std::atoi(v) : 0; 2808 return t == 64 ? 4 : t == 128 ? 2 : 0; 2809 }(); 2810 return forced; 2811} 2812int pick_ww(int64_t n, int n_expert, bool staged, bool wide, int kq) { 2813 const int forced = forced_ww(); 2814 if (forced) return forced; 2815 // the rule itself (the card classes, the thresholds and the measurements behind them) is include/strata/prefill/fused_tile_rule.hpp, a pure function the unit test 2816 // tests/prefill/fused_tile_rule_test.cpp pins for every measured card class. STRATA_PF_TILE_DEBUG=1 prints the choice of every launch of a class that has its own rule. 2817 const DevInfo& d = dev_info(); 2818 const strata::prefill::fused_tile::Pick pk = strata::prefill::fused_tile::pick({d.s120, d.s121, d.sms, d.s86}, n, n_expert, staged, wide, kq); 2819 using strata::prefill::fused_tile::Why; 2820 if (pk.why != Why::kBand) { 2821 static const bool dbg = [] { const char* v = std::getenv("STRATA_PF_TILE_DEBUG"); return v != nullptr && v[0] == '1'; }(); 2822 if (dbg) { 2823 const double avg = (double) n / std::max(n_expert, 1); 2824 const char* cls = pk.why == Why::kS121 ? "s121" : pk.why == Why::kS120Small ? "s120 small" : pk.why == Why::kStaged ? "staged" : pk.why == Why::kS86Wide ? "s86 wide" : pk.why == Why::kS86Kq ? "s86 kq" : pk.why == Why::kS120Kq ? "s120 kq" : "s120"; 2825 std::fprintf(stderr, "strata: fused tile: rows %lld experts %d avg %.1f %s yes -> ww %d\n", (long long) n, n_expert, avg, cls, pk.ww); 2826 } 2827 } 2828 return pk.ww; 2829} 2830 2831// The work item of one launch of the native kernels: pick_ww's, unless the launch has a weight-staging kernel (`staged`: its gate/up kernel is one; the down's is looked at here) and 2832// the staging check fails (staged_blobs_ok), in which case the unstaged 64-row instances run (fused_tile::kUnstagedWw), also when STRATA_PF_FUSED_TILE=128 forces the 128-row tile. 2833// Every call site that launches `launch<...>` with a ww from pick_ww on a batch of blobs takes its ww from here. 2834int launch_ww(const Batch& b, const NativeGeom& g, int64_t n, int n_expert, bool staged, bool wide, int kq = 0) { 2835#if !defined(__HIPCC__) 2836 { // Turing has no staging kernels (no cp.async: ASTAGES_DEV 2), so the blob check does not apply; only the instances that fit 64 KB exist. Its own tile rule 2837 // (fused_tile::t75_pick: an i-quant gate/up takes 128 rows from above 56 rows an expert, a Q2_0 gate/up keeps the band); STRATA_PF_FUSED_TILE still forces one. 2838 const DevInfo& dv = dev_info(); 2839 if (dv.t75) { 2840 const int f = forced_ww(); 2841 return t75_ww(dv, g, f != 0 ? f : strata::prefill::fused_tile::t75_pick(n, n_expert, g.gu_type == T_Q2_0)); 2842 } 2843 } 2844 if ((staged || sw_on(g.d_type, false)) && !staged_blobs_ok(b, g)) return strata::prefill::fused_tile::kUnstagedWw; 2845#else 2846 (void) b; 2847 (void) g; 2848#endif 2849 return pick_ww(n, n_expert, staged, wide, kq); 2850} 2851#endif 2852} // namespace 2853#if !defined(STRATA_FUSED_DECODE_ONLY) 2854 2855// Launches that took the unstaged kernel because the staging check failed (see staged_blobs_ok); 0 in a HIP build, which has no staging kernels. 2856int staged_blob_fallbacks() { 2857#if !defined(__HIPCC__) 2858 std::lock_guard<std::mutex> lk(g_staged_mu); 2859 return g_staged_fallbacks; 2860#else 2861 return 0; 2862#endif 2863} 2864 2865bool gfx12_iq2s_default() { 2866 static const bool on = [] { 2867 const char* v = std::getenv("STRATA_PF_FUSED_IQ2S"); 2868 const char* all = std::getenv("STRATA_PF_FUSED"); 2869 // ON by default on gfx12 (owner decision 11-01, amd-q RESULTS.md section 12; the quick quality suite lost 2 of 19 tasks with it on, the full suite 2870 // decided). STRATA_PF_FUSED_IQ2S=0 turns it off, as does STRATA_PF_FUSED=0. 2871 return strata::core::kGfx12PromptDefaultsOn && !(v != nullptr && v[0] == '0') && !(all != nullptr && all[0] == '0'); 2872 }(); 2873 return on; 2874} 2875 2876#if defined(__HIPCC__) 2877// IQ2_XXS / IQ2_XS gate/up on gfx12 (the IQ3_XXS pack's 19 layers that kept MMQ, and the IQ2_XS sets): ON by default under Linux since 0.2.15 (owner 11-10; 2878// R9700, IQ3_XXS: prompt x1.17 / x1.14 / x1.13 at 4K / 8K / 20K, first-token KL against the FP16 path level with the default's, the neutral-writer forced-token 2879// gate PASS, first-8K reads PASS). STRATA_PF_FUSED_IQ2X=0 keeps these layers on MMQ (the earlier route, kept two releases), as does STRATA_PF_FUSED=0. 2880// Windows gfx12: opt-in (STRATA_PF_FUSED=1), as before. Like gfx12_iq2s_default, native_supported applies it to gfx12 cards only (d.w12). 2881static bool gfx12_iq2x_default() { 2882 static const bool on = [] { 2883 const char* v = std::getenv("STRATA_PF_FUSED_IQ2X"); 2884 const char* all = std::getenv("STRATA_PF_FUSED"); 2885 return strata::core::kGfx12PromptDefaultsOn && !(v != nullptr && v[0] == '0') && !(all != nullptr && all[0] == '0'); 2886 }(); 2887 return on; 2888} 2889#endif 2890 2891bool native_supported(int gu_type, int d_type) { 2892 static const bool off = [] { // STRATA_PF_FUSED_NATIVE=0: the native packs keep MMQ under STRATA_PF_FUSED=1 (A/B) 2893 const char* v = std::getenv("STRATA_PF_FUSED_NATIVE"); 2894 return v != nullptr && v[0] == '0'; 2895 }(); 2896 // gfx12: fused::available() (the Q2_0 pack's expert_w11_kernel, not ported) stays false, so requested() does too; the 2897 // same opt-in (STRATA_PF_FUSED=1) is read here. Every other device: requested() as before. 2898 // unset: on by default on gfx12 for IQ3_S, IQ3_XXS and (148-amd) Q2_0 gate/up (R9700: prompt +13..+20%, first-token KL against the FP16 2899 // prompt path equal to MMQ's within noise, 96 prompts each; Q2_0: prompt 4K +16.7%, 20K +19.0%, teacher-forced KL 0.0004 / 0.0016, 2900 // tasks equal to MMQ's); STRATA_PF_FUSED=0 turns it off, =1 keeps the opt-in for every covered format 2901 static const int env_set = [] { 2902 const char* v = std::getenv("STRATA_PF_FUSED"); 2903 return v == nullptr || v[0] == '\0' ? -1 : (v[0] == '1' ? 1 : 0); 2904 }(); 2905 // the small-card prompt path (pf_fused_override.hpp) reads an unset STRATA_PF_FUSED as 1 2906 const int env = strata::prefill::pf_fused_env_effective(env_set, strata::prefill::pf_fused_override().load(std::memory_order_relaxed)); 2907 if (off || env == 0 || !gu_covered(gu_type)) return false; 2908 if (strata::prefill::pf_fused_override().load(std::memory_order_relaxed) == 0) return false; // the small-card prompt path (pf_fused_override.hpp) 2909#if !defined(__HIPCC__) 2910 // CUDA, a K-quant gate/up: a Q4_K / Q5_K gate/up with a Q5_1 (or, with the Q8_0 down kernels set up, Q8_0) down takes the fused 2911 // kernels on any sm_80+ card whose K-quant kernels were set up: by default on the cards of fused_tile::kq_takes (dev_info), with 2912 // STRATA_PF_FUSED_KQ=1 on every card, never with STRATA_PF_FUSED_KQ=0; other downs keep MMQ for now. A Q5_1 2913 // down needs the code sums only the K-quant gate/up epilogue writes, so it is never taken behind another gate/up. Not with the 2914 // NT = 256 / warp-specialized opt-ins (no K-quant form): MMQ. 2915 // CUDA, a Q8_0 down behind an i-quant gate/up (phase 4, opt-in): with STRATA_PF_FUSED_Q8D=1 such a layer takes the fused kernels on 2916 // any sm_80+ card whose Q8_0 down kernels were set up, whatever the 80-SM rule says (the pinned UD-IQ4_XS's five Q8_0-down layers; 2917 // its other 43 layers follow their own rule). Not behind the Q2_0 gate/up of STRATA_PF_FUSED_Q2N, not with the NT = 256 / 2918 // warp-specialized opt-ins (no Q8_0 form): MMQ. 2919 // A Q8_0 gate/up (phase 4, opt-in STRATA_PF_FUSED_Q8GU=1; gu_covered checked q8gu above): with a Q8_0 down only, same conditions. 2920 if (gu_type == T_Q8_0) { 2921 const DevInfo& d = dev_info(); 2922 return d_type == T_Q8_0 && d.ok && d.q8gu && !d.t75 && d.occ_nt <= 0 && !d.ws; 2923 } 2924 // A Q6_K gate/up (phase 4, opt-in STRATA_PF_FUSED_Q6K=1; gu_covered checked q6k above): with a Q8_0 down only, same conditions. 2925 if (gu_type == T_Q6_K) { 2926 const DevInfo& d = dev_info(); 2927 return d_type == T_Q8_0 && d.ok && d.q6k && !d.t75 && d.occ_nt <= 0 && !d.ws; 2928 } 2929 // The trellis pairs (phase 4; default on sm_86 with 80+ SMs, STRATA_PF_FUSED_TQ=1 on any sm_80+ card; d.tq): Gyro-S's TQK6 / TQK7 and Gyro-M's TQ2_T / TQ2_T only, same conditions. (A trellis 2930 // down behind any other gate/up, or the reverse: MMQ.) 2931 if (is_tq(gu_type) || is_tq(d_type)) { 2932 // the compiled geometry must suit the trellis path: whole 128-value blocks along both K, and (a folded down) the FP32 H of a row, 2933 // n_ff floats, inside the row's own n_embd-float down-output row (set_down_rotation's invariant); the geometry checks allow n_ff up to 2934 // 1,100 and n_embd down to 1,024, so a geometry that breaks it declines the trellis fused path (MMQ) instead of overrunning the row 2935 constexpr bool geo_ok = D_ROWS_K <= D_ROWS_N && D_ROWS_K % 128 == 0 && GU_ROWS_K % 128 == 0; 2936 const DevInfo& d = dev_info(); 2937 // Gyro-S's pair follows the default rule (d.tq); Gyro-M's TQ2_T / TQ2_T has no gate series yet: only with STRATA_PF_FUSED_TQ=1 (fused_tq::gyro_m_on) 2938 const bool pair = (gu_type == T_TQK6 && d_type == T_TQK7) || (gu_type == T_TQ2_T && d_type == T_TQ2_T && strata::prefill::fused_tq::gyro_m_on(tq_env())); 2939 return geo_ok && pair && d.ok && d.tq && !d.t75 && d.occ_nt <= 0 && !d.ws; 2940 } 2941 // NVFP4 (ModelOpt packs), the NVFP4 / NVFP4 pair only (a mixed pair keeps MMQ or the FP16 path): taken when STRATA_PF_FUSED is 1 AS SET (by the user, or by a build without MMQ, whose 2942 // start-up sets it) - so a build with MMQ keeps its MMQ w4a8 default unless the switch is given - and STRATA_PREFILL_NVFP4 is unset or w4a8 (fp16 asks for the exact reference 2943 // path, w4a4 for Blackwell's FP4 MMA: both stay as they are). The kernels' arithmetic is MMQ's w4a8 class (int8 products, FP32 scales, the weight_scale_2 values on the FP32 products). 2944 // Not with the NT = 256 / warp-specialized opt-ins (no NVFP4 form of theirs). 2945 if (gu_type == T_NVFP4 || d_type == T_NVFP4) { 2946 static const bool mode_ok = [] { const char* v = std::getenv("STRATA_PREFILL_NVFP4"); return v == nullptr || v[0] == '\0' || std::strcmp(v, "w4a8") == 0; }(); 2947 const DevInfo& d = dev_info(); 2948 return gu_type == T_NVFP4 && d_type == T_NVFP4 && env_set == 1 && mode_ok && d.ok && d.nvfp4 && !d.t75 && d.occ_nt <= 0 && !d.ws; 2949 } 2950 if (d_type == T_Q8_0 && gu_type != T_Q4_K && gu_type != T_Q5_K) { 2951 const DevInfo& d = dev_info(); 2952 return gu_type != T_Q2_0 && d.ok && d.q8d && !d.t75 && d.occ_nt <= 0 && !d.ws; 2953 } 2954 if (gu_type == T_Q4_K || gu_type == T_Q5_K || d_type == T_Q5_1 || d_type == T_Q8_0) { 2955 const DevInfo& d = dev_info(); 2956 return (gu_type == T_Q4_K || gu_type == T_Q5_K) && (d_type == T_Q5_1 || (d_type == T_Q8_0 && d.kq8)) && d.ok && d.kq && !d.t75 && 2957 d.occ_nt <= 0 && !d.ws; 2958 } 2959 // CUDA, a Q5_0 down (phase 4 step 1, opt-in): with STRATA_PF_FUSED_Q50=1 such a layer takes the fused kernels on any sm_80+ card 2960 // whose Q5_0 kernels were set up, whatever the 80-SM rule below says (STRATA_PF_FUSED=0 still turns every fused kernel off, above). 2961 // Not with the NT = 256 / warp-specialized opt-ins (their kernels have no Q5_0 form): those layers keep the FP16 path. 2962 if (d_type == T_Q5_0) { 2963 const DevInfo& d = dev_info(); 2964 return d.ok && d.q50 && !d.t75 && d.occ_nt <= 0 && !d.ws; 2965 } 2966 // CUDA: unset, on by default on a compute capability 12.0 card with 80+ SMs (sm120_series.hpp, STRATA_SM120_PERF=0 off) for the gate/up 2967 // formats measured there - the IQ3_S pack's IQ2_S, IQ3_XXS, IQ3_S and IQ4_XS layers with IQ4_NL / Q2_0 down (RTX PRO 5000: 2968 // prompt +7.0% 4K, +5.2% 20K, +3.4% 98K; teacher-forced KL against MMQ 0.0002 over 5998 positions, 0.0010 over 1760 long 2969 // ones; first-token KL against the FP16 prompt path 0.01181 vs MMQ's 0.01236 over 48 prompts; HumanEval, reasoning, 2970 // agent, needles, CJK and tool calls the same). The same on a compute capability 8.6 card with 80+ SMs (RTX A6000, 84 SMs, IQ3_S pack, 11-11: prompt +6.7% 4K, +3.6% 8K, 2971 // +1.0% 20K, 5/5 each; first-token KL against the FP16 prompt path at or below MMQ's at 2,048 / 4K / 8K / 20K; neutral-writer 2972 // forced-token gate PASS over 48 prompts; small chunks, agent, first 8K read, backstop as MMQ; STRATA_PF_FUSED_SM86=0 off). 2973 // IQ2_XXS / IQ2_XS gate/up (the IQ3_XXS and IQ2_XS packs) joined on 11-12 on the owner's word (RTX A6000, IQ3_XXS pack: prompt time fused / MMQ-mixed 0.874-0.976 from 300 to 8,193 2974 // tokens on the integration head, 5 rounds; teacher-forced KL vs the FP16 prompt path 0.965 / 1.062 / 1.072 / 1.008 at 512 / 1,000 / 2,048 / 8,192 ids; the rule: fused_tile_rule.hpp). 2975 // Every other CUDA card (and the 40-79 SM cards for every launch outside the staged class): opt-in as before. 2976 if (env < 0) { 2977 // the native Q2_0 pack's Q2_0 / Q2_0 layers: only with STRATA_PF_FUSED_Q2N=1 (d.q2n), on any sm_80+ card 2978 if (gu_type == T_Q2_0) { const DevInfo& d = dev_info(); return d.ok && d.q2n && d_type == T_Q2_0 && d_covered_on(d_type, d); } 2979 if (!strata::prefill::fused_tile::default_gate_format(gu_type)) return false; // IQ2_XXS / IQ2_XS joined 11-12 (owner), see fused_tile_rule.hpp 2980 const DevInfo& d = dev_info(); 2981 // 12.0 and 8.6 cards with 80+ SMs (RTX PRO 5000 / 6000, RTX 5080 / 5090; RTX A6000 / A40, RTX 3090 / 3090 Ti / 3080 Ti), and since 11-12 8.0 and 8.9 cards (A100; RTX 4090 / 4080 Super, RTX 6000 Ada: the same code, no card run) take every format of the default list; 2982 // on an RTX 5070 (48 SMs, IQ2_XS pack, experts partly in RAM) the fused kernels were 4.7% slower on 20K prompts (0/6 pairs), so the 40-79 SM cards take only the staged launches below 2983 // (the unstaged formats keep MMQ there unless STRATA_PF_FUSED=1) 2984 // The sm_86 / sm_120 cards with 40-79 SMs (RTX A4000, RTX 5070 class) also take the launches with a staged gate/up kernel (IQ3_XXS / IQ3_S / IQ4_XS) and an IQ4_NL / Q2_0 down, by default since 2985 // the owner's word of 11-12 (RTX A4000, IQ3_XXS set, end to end: prompt time MMQ / fused 1.000 / 1.003 / 1.028 / 1.012 at 2K / 4K / 8K / 20K, forced-token KL against the FP16 prompt path 2986 // 0.930 [0.881, 0.988] at 2,048 and 0.973 [0.898, 1.057] at 8,192 ids); STRATA_PF_FUSED_SM40=0 takes them back to MMQ (a diagnostic like STRATA_PF_FUSED_SM86=0). The rule itself is 2987 // fused_tile::default_takes_env (unit-tested over every card flag, SM count, switch state and launch kind). This whole branch is CUDA-only (the #if !defined(__HIPCC__) above): a HIP build never reads it. 2988 static const int sm40_env = [] { const char* v = std::getenv("STRATA_PF_FUSED_SM40"); return v == nullptr ? -1 : (v[0] == '0' ? 0 : 1); }(); 2989 return d.ok && strata::prefill::fused_tile::default_takes_env(env, {d.s120, d.s121, d.sms, d.s86}, sm40_env, sw_on(gu_type, true), d_type == T_IQ4_NL || d_type == T_Q2_0, gu_type) && !d.t75 && 2990 d_covered_on(d_type, d); 2991 } 2992 if (gu_type == T_Q2_0 && d_type != T_Q2_0) return false; // the opt-in (STRATA_PF_FUSED=1 as well) takes the Q2_0 / Q2_0 pair only 2993#endif 2994#if defined(__HIPCC__) 2995 // The trellis pairs on gfx12 (opt-in STRATA_PF_FUSED_TQ=1; gu_covered checked it above) and on gfx1151 (tq_hip_pair): Gyro-S's TQK6 / TQK7 and Gyro-M's TQ2_T / TQ2_T only, with 2996 // CUDA's geometry rule (whole 128-value blocks along both K; a folded down's FP32 H row inside the row's own n_embd-float output row). Any other 2997 // pairing with a trellis format: MMQ. 2998 if (is_tq(gu_type) || is_tq(d_type)) { 2999 constexpr bool geo_ok = D_ROWS_K <= D_ROWS_N && D_ROWS_K % 128 == 0 && GU_ROWS_K % 128 == 0; 3000 const DevInfo& d = dev_info(); 3001 // gfx1151 (fused_tq_hip.hpp): Gyro-S's pair by STRATA_PF_TQ_G1151 and its default, Gyro-M's only with STRATA_PF_TQ_G1151=1 3002 return geo_ok && d.ok && tq_hip_pair(d, gu_type == T_TQK6 && d_type == T_TQK7, gu_type == T_TQ2_T && d_type == T_TQ2_T); 3003 } 3004 // A Q8_0 gate/up on gfx12 (opt-in STRATA_PF_FUSED_Q8GU=1; gu_covered checked it above): with a Q8_0 down only. 3005 if (gu_type == T_Q8_0) { 3006 const DevInfo& d = dev_info(); 3007 return d_type == T_Q8_0 && d.ok && d.w12 && q8gu_hip_requested(); 3008 } 3009 // A Q6_K gate/up on gfx12 (opt-in STRATA_PF_FUSED_Q6K=1; gu_covered checked it above): with a Q8_0 down only. 3010 if (gu_type == T_Q6_K) { 3011 const DevInfo& d = dev_info(); 3012 return d_type == T_Q8_0 && d.ok && d.w12 && q6k_hip_requested(); 3013 } 3014#endif 3015 // default: only these three; the Q2_0 one (148-amd) is on Linux only: on Windows it is opt-in (STRATA_PF_FUSED=1) until tested there. 3016 // AMD-Q (owner decision 10-31): the IQ2_S and IQ4_XS gate/up layers too (the IQ3_S pack's 21 layers that kept MMQ), Linux only like the Q2_0 entry. 3017 // R9700 (p620, IQ3_S, serve, 3 interleaved pairs): prompt 2K +9.7%, 8K +12.2%, 32K +8.8%; first-token KL against the MMQ default 0.017 (29 prompts of 3018 // 1,025 tokens, top-1 28 of 29), the answers are not the MMQ bytes. STRATA_PF_FUSED_IQ2S=0 turns it off; STRATA_PF_FUSED=0 turns all fused kernels off. 3019 if (env < 0 && gu_type != T_IQ3_S && gu_type != T_IQ3_XXS && !(gu_type == T_Q2_0 && strata::core::kGfx12PromptDefaultsOn) && 3020 !(gfx12_iq2s_default() && (gu_type == T_IQ2_S || gu_type == T_IQ4_XS)) 3021#if defined(__HIPCC__) 3022 && !(gfx12_iq2x_default() && (gu_type == T_IQ2_XXS || gu_type == T_IQ2_XS)) // (0.2.15, Linux gfx12: above) 3023#endif 3024 ) return false; 3025 const DevInfo& d = dev_info(); 3026#if defined(__HIPCC__) 3027 // STRATA_PF_FUSED_Q8D=1 on gfx12 covers the Q8_0 down behind an i-quant gate/up only (as on CUDA): a Q2_0 gate/up with a Q8_0 down keeps MMQ there 3028 // unless STRATA_PF_FUSED_KQ=1 (the K-quant route's wider rule, unchanged; gfx11's rules unchanged) 3029 if (d.w12 && gu_type == T_Q2_0 && d_type == T_Q8_0 && !kq_on()) return false; 3030 // d_covered_on takes a Q8_0 down on gfx12 for STRATA_PF_FUSED_Q8GU=1 too (behind a Q8_0 gate/up, returned above): an i-quant / K-quant gate/up 3031 // keeps its own rule (STRATA_PF_FUSED_Q8D=1 or the K-quant route's) 3032 if (d.w12 && d_type == T_Q8_0 && !kq_on() && !q8d_hip_requested()) return false; 3033#endif 3034 if (!d.ok || !d_covered_on(d_type, d)) return false; 3035#if !defined(__HIPCC__) 3036 if (d.t75 && !t75_pair_ok(d, gu_type, d_type)) return false; 3037#endif 3038 if (env < 0) return d.w12; 3039 return requested() || d.w12; 3040} 3041 3042void quantize_act_native(const float* x, int64_t rows, int64_t cols, void* xa, void* stream) { 3043 if (rows <= 0) return; 3044 const int64_t nblk = rows * (cols / 64); 3045 quant_act_nat_kernel<<<blocks(nblk, 8), 256, 0, (cudaStream_t) stream>>>(x, nblk, (uint8_t*) xa); 3046 ck(cudaGetLastError(), "quantize_act_native"); 3047} 3048 3049// The down rows as FP16 (opt-in per call: the prompt path asks, STRATA_PF_DM16): dm_f16_used() tells which form the last call wrote. 3050// Per host thread, like g_rot_* below: on a layer split each stage reads its chunks on a thread of its own, at the same time as the 3051// others (prefill.cpp run_impl hands the next stage its chunk and goes on), and asks, launches and reads back on that thread. 3052static thread_local bool g_dm16_want = false, g_dm16_used = false; 3053void want_dm_f16(bool on) { g_dm16_want = on; } 3054bool dm_f16_used() { return g_dm16_used; } 3055 3056// STRATA_PF_P1A=1 (gfx1151, HIP; a default on Windows gfx1151, arch_defaults.cpp): the Q4_K gate/up + Q5_1 down layers on moe_p1a.cu's kernels (F16 activations from the 3057// layer's FP16 input rows, F16 H, FP16 down rows); STRATA_PF_P1A_L2=1 adds the Q5_K gate/up and Q8_0 down layers. Taken per call only 3058// when the prompt path handed over the FP16 input (p1a_input), the down rows go out as FP16 (want_dm_f16) and H has room for FP16 rows; 3059// every other call, and every call with the switch off, runs as before. 3060#if defined(__HIPCC__) 3061static thread_local const void* g_p1a_x16 = nullptr; // (per host thread: a stage's own device's FP16 rows, see g_dm16_want) 3062static thread_local size_t g_p1a_h_bytes = 0; 3063#endif 3064static thread_local bool g_p1a_used = false; 3065bool p1a_wanted() { 3066#if defined(__HIPCC__) 3067 static const bool on = [] { 3068 const char* v = std::getenv("STRATA_PF_P1A"); 3069 return v != nullptr && v[0] == '1' && v[1] == '\0' && dev_info().g1151; 3070 }(); 3071 return on; 3072#else 3073 return false; 3074#endif 3075} 3076[[maybe_unused]] static bool p1a_l2() { 3077#if defined(__HIPCC__) 3078 static const bool on = [] { const char* v = std::getenv("STRATA_PF_P1A_L2"); return v != nullptr && v[0] == '1' && v[1] == '\0'; }(); 3079 return on; 3080#else 3081 return false; // (the P1A kernels run on gfx1151 only: p1a_wanted) 3082#endif 3083} 3084void p1a_input(const void* x16, size_t h_bytes) { 3085#if defined(__HIPCC__) 3086 g_p1a_x16 = p1a_wanted() ? x16 : nullptr; 3087 g_p1a_h_bytes = g_p1a_x16 != nullptr ? h_bytes : 0; 3088#else 3089 (void) x16; (void) h_bytes; // (g_p1a_x16 / g_p1a_h_bytes are read in the HIP-only P1A launch only) 3090#endif 3091} 3092bool p1a_used() { return g_p1a_used; } 3093#if defined(__HIPCC__) 3094bool p1a_takes_pair(int gu_type, int d_type) { return p1a_wanted() && p1a::takes(gu_type, d_type, p1a_l2()); } 3095#else 3096bool p1a_takes_pair(int, int) { return false; } // (namespace p1a, moe_p1a.hpp, exists in HIP builds only) 3097#endif 3098 3099// (CUDA, STRATA_PF_FUSED_TQ=1) a Hadamard-folded down (set_down_rotation): on, and the down input's sign vector. 3100// Per host thread: the sign vector lives on the device of the stage that set it (hadamard_device_signs), and on a layer split the 3101// stages' threads set and clear it per layer at the same time; one process-wide copy let CUDA0's kernels read CUDA1's vector 3102// (an illegal address without peer access, another layer's signs with it). prefill.cpp sets, launches and clears on one thread. 3103static thread_local const float* g_rot_signs = nullptr; 3104static thread_local bool g_rot_on = false; 3105void set_down_rotation(bool on, const float* signs) { g_rot_on = on; g_rot_signs = on ? signs : nullptr; } 3106void hadamard128_rows(float* x, int64_t rows, int64_t cols, const float* signs, void* stream) { 3107 if (rows <= 0) return; 3108 if (cols % 128 != 0) { std::fprintf(stderr, "hadamard128_rows: %lld columns are not whole 128-value blocks\n", (long long) cols); std::exit(1); } 3109 const int64_t nblk = rows * (cols / 128); 3110 hadamard128_kernel<<<blocks(nblk, 8), 256, 0, (cudaStream_t) stream>>>(x, nblk, (int) cols, signs); 3111 ck(cudaGetLastError(), "hadamard128_rows"); 3112} 3113 3114void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n, const void* scratch, const void* xa, 3115 const int32_t* src, void* ha, float* dm, void* stream) { 3116 g_dm16_used = false; 3117 g_p1a_used = false; 3118 if (b.e1 <= b.e0 || n <= 0) return; 3119 const DevInfo& d = dev_info(); 3120#if !defined(__HIPCC__) 3121 const bool t75_missing = d.t75 && !t75_pair_ok(d, g.gu_type, g.d_type); // (Turing: an instance that was not set up would fail to launch) 3122#else 3123 constexpr bool t75_missing = false; 3124#endif 3125 if (!d.ok || !gu_covered(g.gu_type) || !d_covered_on(g.d_type, d) || t75_missing) { 3126 std::fprintf(stderr, "prefill fused experts (native): types %d / %d are not covered here\n", g.gu_type, g.d_type); 3127 std::exit(1); 3128 } 3129 const cudaStream_t s = (cudaStream_t) stream; 3130 const Tables tb = tables(const_cast<void*>(scratch), n_expert); 3131#if defined(__HIPCC__) 3132 { 3133 const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0); 3134 // STRATA_PF_P1A=1 (gfx1151; p1a_input set only then): gate/up and down on moe_p1a.cu's kernels, H as FP16 in `ha`, the down rows as FP16 in `dm` 3135 if (g_p1a_x16 != nullptr && g_dm16_want && !g_rot_on && d.g1151 && p1a::takes(g.gu_type, g.d_type, p1a_l2()) && tiles <= 65535 && 3136 (size_t) n * kMoeFF * 2 <= g_p1a_h_bytes) { 3137 p1a::gate_up(b, g, tb.ts, tb.tiles, tb.off, tiles, g_p1a_x16, src, ha, s); 3138 p1a::down(b, g, tb.ts, tb.tiles, tb.off, tiles, ha, dm, s); 3139 g_dm16_used = true; 3140 g_p1a_used = true; 3141 ck(cudaGetLastError(), "experts_native (P1A)"); 3142 return; 3143 } 3144 // W (X1): LDS alias of the H tile + waves_per_eu(8) (192 VGPRs) + 4 blocks per WGP, only for the types whose 3145 // kernels do not spill under the cap (measured: gate/up IQ2_*, IQ3_*, IQ4_XS 0-5 regs; down IQ4_NL 11, Q8_0 / Q2_0 0; 3146 // Q4_K / Q5_K gate/up and Q5_1 down spill 100+ and keep the old launch). 3147 // Those counts are from a clang before 22; from clang 22 nw_capped decides which of these kernels get the cap 3148 // (above NW_LB), and the launch is the same. 3149#ifdef STRATA_W_NO_OCC 3150 const bool occ_gu = false, occ_d = false; 3151#else 3152 // STRATA_PF_KQ_OCC=1 (a trial switch, off by default, HIP only): the K-quant kernels too (Q4_K / Q5_K gate/up, Q5_1 down) take the 3153 // occupancy launch above (the H tile on the weight buffers, occ_blocks per WGP; the VGPR cap only where nw_capped says so). 3154 static const bool kq_occ = [] { const char* v = std::getenv("STRATA_PF_KQ_OCC"); return v != nullptr && v[0] == '1'; }(); 3155 const bool occ_gu = g.gu_type == T_IQ2_XXS || g.gu_type == T_IQ2_XS || g.gu_type == T_IQ2_S || 3156 g.gu_type == T_IQ3_XXS || g.gu_type == T_IQ3_S || g.gu_type == T_IQ4_XS || g.gu_type == T_Q2_0 || 3157 (kq_occ && (g.gu_type == T_Q4_K || g.gu_type == T_Q5_K)); 3158 const bool occ_d = g.d_type == T_Q2_0 || g.d_type == T_Q8_0 || g.d_type == T_IQ4_NL || (kq_occ && g.d_type == T_Q5_1); 3159#endif 3160 if (d.w12 && (reinterpret_cast<uintptr_t>(dm) & 15) != 0) { // gfx12's down store: two float4 per lane 3161 std::fprintf(stderr, "prefill fused experts (native): the down rows (%p) must be 16-byte aligned\n", (void*) dm); 3162 std::exit(1); 3163 } 3164 const unsigned g_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / NW_ROWS), 3165 (int64_t) d.sms * (occ_gu ? occ_blocks(d.w12, g.gu_type, true) : wgp_blocks(d.occ))); 3166 const unsigned g_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / NW_ROWS), 3167 (int64_t) d.sms * (occ_d ? occ_blocks(d.w12, g.d_type, false) : wgp_blocks(d.occ))); 3168 const uint8_t* xa8 = (const uint8_t*) xa; 3169 uint8_t* ha8 = (uint8_t*) ha; 3170#define STRATA_NW_GU(T) do { if (occ_gu) native_w11_kernel<T, true, true, NW_LB><<<g_gu, NW_THREADS, 0, s>>>(b, g, tb, xa8, src, ha8, nullptr); else native_w11_kernel<T, true><<<g_gu, NW_THREADS, 0, s>>>(b, g, tb, xa8, src, ha8, nullptr); } while (0) 3171 // STRATA_PF_F16=1: gfx11's F16 matrix cores - the K-quants' kernels (moe_f16_kq.cu) and native_f16_kernel for 3172 // f16_gu / f16_d (gfx12's WMMA layout differs, so it keeps the int8 kernels) 3173 const bool f16 = !d.w12 && f16::requested(); 3174 const bool f16_q8d = !f16 && g.d_type == T_Q8_0 && q8d_f16_default(d); // (the Q8_0 down only) 3175 const bool f16_wide = f16::pick_bn(n, n_expert) == 2 * kTileRows; 3176 // STRATA_PF_DM16 (the prompt path asks per call): when this layer's down runs on an F16 kernel its rows go to `dm` as FP16 (moe_combine16 reads them) 3177 const bool d16 = g_dm16_want && ((f16 && (f16kq::covers_down(g.d_type) || f16_d(g.d_type))) || f16_q8d); 3178 g_dm16_used = d16; 3179#define STRATA_F16_LAUNCH(T, GU, NFB, ACT, OUT, DM) do { \ 3180 const unsigned grid_f = (unsigned) std::max<int64_t>(1, tiles * (NFB)); \ 3181 if (d16 && !(GU)) { \ 3182 if (f16_wide) native_f16_kernel<T, GU, 2 * kTileRows, !(GU)><<<grid_f, f16::THREADS, 0, s>>>(b, g, tb, ACT, src, OUT, DM); \ 3183 else native_f16_kernel<T, GU, kTileRows, !(GU)><<<grid_f, f16::THREADS, 0, s>>>(b, g, tb, ACT, src, OUT, DM); \ 3184 } else if (f16_wide) native_f16_kernel<T, GU, 2 * kTileRows><<<grid_f, f16::THREADS, 0, s>>>(b, g, tb, ACT, src, OUT, DM); \ 3185 else native_f16_kernel<T, GU, kTileRows><<<grid_f, f16::THREADS, 0, s>>>(b, g, tb, ACT, src, OUT, DM); \ 3186} while (0) 3187 if (f16 && f16kq::covers(g.gu_type)) f16kq::gate_up(b, g, tb.ts, tb.tiles, tb.off, n, n_expert, tiles, xa8, src, ha8, s); 3188 else if (f16 && f16_gu(g.gu_type)) { 3189#define STRATA_F16_GU(T) STRATA_F16_LAUNCH(T, true, f16::GU_NFB, xa8, ha8, nullptr) 3190 switch (g.gu_type) { 3191 case T_IQ2_XXS: STRATA_F16_GU(T_IQ2_XXS); break; 3192 case T_IQ2_XS: STRATA_F16_GU(T_IQ2_XS); break; 3193 case T_IQ2_S: STRATA_F16_GU(T_IQ2_S); break; 3194 case T_IQ3_XXS: STRATA_F16_GU(T_IQ3_XXS); break; 3195 case T_IQ3_S: STRATA_F16_GU(T_IQ3_S); break; 3196 default: STRATA_F16_GU(T_IQ4_XS); break; 3197 } 3198#undef STRATA_F16_GU 3199 } 3200 else switch (g.gu_type) { 3201 case T_IQ2_XXS: STRATA_NW_GU(T_IQ2_XXS); break; 3202 case T_IQ2_XS: STRATA_NW_GU(T_IQ2_XS); break; 3203 case T_IQ2_S: STRATA_NW_GU(T_IQ2_S); break; 3204 case T_IQ3_XXS: STRATA_NW_GU(T_IQ3_XXS); break; 3205 case T_IQ3_S: STRATA_NW_GU(T_IQ3_S); break; 3206 case T_Q2_0: STRATA_NW_GU(T_Q2_0); break; 3207 case T_Q4_K: STRATA_NW_GU(T_Q4_K); break; 3208 case T_Q5_K: STRATA_NW_GU(T_Q5_K); break; 3209 // (STRATA_PF_FUSED_TQ=1, gfx12) the trellis gate/up: the plain launch; a folded down (set_down_rotation) takes H as FP32 rows in dm 3210 // (STRATA_PF_FUSED_Q8GU=1, gfx12) the Q8_0 gate/up (a Q8_0 down behind it): the plain launch 3211 case T_Q8_0: native_w11_kernel<T_Q8_0, true><<<g_gu, NW_THREADS, 0, s>>>(b, g, tb, xa8, src, ha8, nullptr); break; 3212 // (STRATA_PF_FUSED_Q6K=1, gfx12) the Q6_K gate/up (a Q8_0 down behind it): the plain launch 3213 case T_Q6_K: native_w11_kernel<T_Q6_K, true><<<g_gu, NW_THREADS, 0, s>>>(b, g, tb, xa8, src, ha8, nullptr); break; 3214 case T_TQK6: native_w11_kernel<T_TQK6, true><<<g_gu, NW_THREADS, 0, s>>>(b, g, tb, xa8, src, ha8, g_rot_on ? dm : nullptr); break; 3215 case T_TQ2_T: native_w11_kernel<T_TQ2_T, true><<<g_gu, NW_THREADS, 0, s>>>(b, g, tb, xa8, src, ha8, g_rot_on ? dm : nullptr); break; 3216 default: STRATA_NW_GU(T_IQ4_XS); break; 3217 } 3218#undef STRATA_NW_GU 3219#define STRATA_NW_D(T) do { if (occ_d) native_w11_kernel<T, false, true, NW_LB><<<g_d, NW_THREADS, 0, s>>>(b, g, tb, ha8, src, nullptr, dm); else native_w11_kernel<T, false><<<g_d, NW_THREADS, 0, s>>>(b, g, tb, ha8, src, nullptr, dm); } while (0) 3220 if (f16 && f16kq::covers_down(g.d_type)) f16kq::down(b, g, tb.ts, tb.tiles, tb.off, n, n_expert, tiles, ha8, dm, s, d16); 3221 else if ((f16 && f16_d(g.d_type)) || f16_q8d) { 3222 if (g.d_type == T_Q2_0) STRATA_F16_LAUNCH(T_Q2_0, false, f16::D_NFB, ha8, nullptr, dm); 3223 else if (g.d_type == T_Q8_0) STRATA_F16_LAUNCH(T_Q8_0, false, f16::D_NFB, ha8, nullptr, dm); 3224 else if (g.d_type == T_Q5_0) STRATA_F16_LAUNCH(T_Q5_0, false, f16::D_NFB, ha8, nullptr, dm); 3225 else STRATA_F16_LAUNCH(T_IQ4_NL, false, f16::D_NFB, ha8, nullptr, dm); 3226 } 3227 else if (g.d_type == T_TQK7 || g.d_type == T_TQ2_T) { // (STRATA_PF_FUSED_TQ=1, gfx12; d_covered_on checked it above) 3228 if (g_rot_on) { // a folded down: the FP32 H rows of this call's experts (in dm), rotated and quantized into ha, as on CUDA 3229 const int64_t nw = n * (D_ROWS_K / 128); 3230 rot_quant_h_kernel<<<blocks(nw, 8), 256, 0, s>>>(dm, tb.off, b.e0, b.e1, n, g_rot_signs, ha8); 3231 } 3232 if (g.d_type == T_TQK7) native_w11_kernel<T_TQK7, false><<<g_d, NW_THREADS, 0, s>>>(b, g, tb, ha8, src, nullptr, dm); 3233 else native_w11_kernel<T_TQ2_T, false><<<g_d, NW_THREADS, 0, s>>>(b, g, tb, ha8, src, nullptr, dm); 3234 } 3235 else if (g.d_type == T_Q2_0) STRATA_NW_D(T_Q2_0); 3236 else if (g.d_type == T_Q5_0) STRATA_NW_D(T_Q5_0); 3237 else if (g.d_type == T_Q5_1) STRATA_NW_D(T_Q5_1); 3238 else if (g.d_type == T_Q8_0) STRATA_NW_D(T_Q8_0); 3239 else STRATA_NW_D(T_IQ4_NL); 3240#undef STRATA_NW_D 3241#undef STRATA_F16_LAUNCH 3242 ck(cudaGetLastError(), "experts_native"); 3243 return; 3244 } 3245#endif 3246 if (d.occ_nt > 0 || d.ws) { // STRATA_PF_FUSED_NT=256 / _F16 / _WS: the kernels that take a WideBatch 3247 WideBatch w; 3248 w.e0 = b.e0; 3249 w.e1 = b.e1; 3250 std::copy(b.blob, b.blob + (b.e1 - b.e0), w.blob); 3251 experts_native(w, g, n_expert, n, scratch, xa, src, ha, dm, stream); 3252 return; 3253 } 3254 // the launch's gate/up kernel is a staging instance: the tile rule then takes 128 rows (the gate/up is about two thirds of the layer; a down that is not staged, Q2_0 or Q8_0 or Q5_0, 3255 // still ran faster in the 128-row tile with it: IQ3_S / Q2_0 on the RTX A4000, RESULTS-P4-SM.md S1). Either kernel staged: the blob check. 3256 const bool staged = sw_on(g.gu_type, true), wide = g.gu_type == T_Q6_K || g.gu_type == T_Q8_0; // wide: the Q6_K / Q8_0 gate/up class of the sm_86 tile line (40+ SMs) 3257 const int ww = launch_ww(b, g, n, n_expert, staged, wide, strata::prefill::fused_tile::kq_flash_kind(g.gu_type, g.d_type)); // (the staging check inside: a launch that fails it takes the unstaged 64-row kernel; the last argument: Flash-Next's K-quant pairs) 3258 // the most 64-row tiles the batch can have (every row in it, plus a partial tile per expert) - the items of the 3259 // ones inside a larger item end at once 3260 const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0); 3261 const unsigned g_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ); 3262 const unsigned g_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ); 3263#if !defined(__HIPCC__) // (a HIP build returns above; no K-quant instance of native_kernel in its code objects) 3264 if (g.gu_type == T_NVFP4) { // the NVFP4 / NVFP4 pair (native_supported); its own occupancy; the per-expert scales are applied inside the kernels 3265 if (g.d_type != T_NVFP4) { 3266 std::fprintf(stderr, "prefill fused experts (native): NVFP4 gate/up with down type %d is not covered here\n", g.d_type); 3267 std::exit(1); 3268 } 3269 const unsigned n_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_nvfp4_gu); 3270 const unsigned n_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_nvfp4_d); 3271 launch<T_NVFP4, true>(ww, n_gu, b, g, tb, xa, src, ha, nullptr, s); 3272 launch<T_NVFP4, false>(ww, n_d, b, g, tb, ha, src, nullptr, dm, s); 3273 ck(cudaGetLastError(), "experts_native"); 3274 return; 3275 } 3276 if (g.gu_type == T_Q4_K || g.gu_type == T_Q5_K) { // STRATA_PF_FUSED_KQ=1 (gu_covered / d_covered_on checked d.kq above); their own occupancy 3277 const unsigned k_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq); 3278 const unsigned k_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq); 3279 if (g.d_type != T_Q5_1 && !(g.d_type == T_Q8_0 && d.kq8)) { 3280 std::fprintf(stderr, "prefill fused experts (native): K-quant gate/up with down type %d is not covered here\n", g.d_type); 3281 std::exit(1); 3282 } 3283 if (g.gu_type == T_Q4_K) launch<T_Q4_K, true>(ww, k_gu, b, g, tb, xa, src, ha, nullptr, s); 3284 else launch<T_Q5_K, true>(ww, k_gu, b, g, tb, xa, src, ha, nullptr, s); 3285 if (g.d_type == T_Q8_0) { // (A2) its own occupancy 3286 const unsigned k8 = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq8); 3287 launch<T_Q8_0, false>(ww, k8, b, g, tb, ha, src, nullptr, dm, s); 3288 } else launch<T_Q5_1, false>(ww, k_d, b, g, tb, ha, src, nullptr, dm, s); 3289 ck(cudaGetLastError(), "experts_native"); 3290 return; 3291 } 3292#endif 3293 switch (g.gu_type) { 3294 case T_IQ2_XXS: launch<T_IQ2_XXS, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3295 case T_IQ2_XS: launch<T_IQ2_XS, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3296 case T_IQ2_S: launch<T_IQ2_S, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3297 case T_IQ3_XXS: launch<T_IQ3_XXS, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3298 case T_IQ3_S: launch<T_IQ3_S, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3299 case T_Q2_0: launch<T_Q2_0, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3300#if !defined(__HIPCC__) // (STRATA_PF_FUSED_Q8GU=1, gu_covered checked d.q8gu above; a HIP build returns above and has no such instance) 3301 case T_Q8_0: 3302 launch<T_Q8_0, true>(ww, (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_q8gu), b, g, tb, 3303 xa, src, ha, nullptr, s); 3304 break; 3305 case T_Q6_K: // (STRATA_PF_FUSED_Q6K=1, gu_covered checked d.q6k above) 3306 launch<T_Q6_K, true>(ww, (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_q6k), b, g, tb, 3307 xa, src, ha, nullptr, s); 3308 break; 3309 case T_TQK6: // (STRATA_PF_FUSED_TQ=1, gu_covered checked d.tq above; native_supported pairs it with a TQK7 down) 3310 launch<T_TQK6, true>(ww, (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_tq_gu), b, g, tb, 3311 xa, src, ha, g_rot_on ? dm : nullptr, s); // (a folded down: the FP32 H rows into dm; else H as int8, as every format) 3312 break; 3313 case T_TQ2_T: // (the same; with a TQ2_T down) 3314 launch<T_TQ2_T, true>(ww, (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_tq_gu), b, g, tb, 3315 xa, src, ha, g_rot_on ? dm : nullptr, s); 3316 break; 3317#endif 3318 default: launch<T_IQ4_XS, true>(ww, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3319 } 3320 if (g.d_type == T_Q2_0) launch<T_Q2_0, false>(ww, g_d, b, g, tb, ha, src, nullptr, dm, s); 3321#if !defined(__HIPCC__) // (a HIP build returns above; no Q5_0 instance of native_kernel in its code objects) 3322 else if (g.d_type == T_Q5_0) { // STRATA_PF_FUSED_Q50=1 (d_covered_on checked d.q50 above); its own occupancy 3323 const unsigned g_q = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_q50); 3324 launch<T_Q5_0, false>(ww, g_q, b, g, tb, ha, src, nullptr, dm, s); 3325 } else if (g.d_type == T_Q8_0) { // STRATA_PF_FUSED_Q8D=1 (d_covered_on checked d.q8d above); A2's instances and occupancy 3326 const unsigned g8 = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq8); 3327 launch<T_Q8_0, false>(ww, g8, b, g, tb, ha, src, nullptr, dm, s); 3328 } else if (g.d_type == T_TQK7 || g.d_type == T_TQ2_T) { // STRATA_PF_FUSED_TQ=1 (d_covered_on checked d.tq above); their own occupancy 3329 if (g_rot_on) { // a folded down (set_down_rotation): the FP32 H rows of this call's experts (in dm), rotated and quantized into ha 3330 const int64_t nw = n * (D_ROWS_K / 128); 3331 rot_quant_h_kernel<<<blocks(nw, 8), 256, 0, s>>>(dm, tb.off, b.e0, b.e1, n, g_rot_signs, (uint8_t*) ha); 3332 } 3333 const unsigned gt = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_tq_d); 3334 if (g.d_type == T_TQK7) launch<T_TQK7, false>(ww, gt, b, g, tb, ha, src, nullptr, dm, s); 3335 else launch<T_TQ2_T, false>(ww, gt, b, g, tb, ha, src, nullptr, dm, s); 3336 } 3337#endif 3338 else launch<T_IQ4_NL, false>(ww, g_d, b, g, tb, ha, src, nullptr, dm, s); 3339 ck(cudaGetLastError(), "experts_native"); 3340} 3341 3342bool native_wide() { const DevInfo& d = dev_info(); return d.occ_nt > 0 || d.ws; } 3343 3344void experts_native(const WideBatch& b, const NativeGeom& g, int n_expert, int64_t n, const void* scratch, const void* xa, 3345 const int32_t* src, void* ha, float* dm, void* stream) { 3346 if (b.e1 <= b.e0 || n <= 0) return; 3347 const DevInfo& d = dev_info(); 3348#if !defined(__HIPCC__) 3349 // (no Q5_0 / K-quant / Q5_1 form of the NT = 256 kernels: native_supported keeps those layers off this path) 3350 const bool q50 = g.d_type == T_Q5_0 || g.gu_type == T_Q4_K || g.gu_type == T_Q5_K || g.d_type == T_Q5_1 || g.d_type == T_Q8_0 || 3351 is_tq(g.gu_type) || is_tq(g.d_type) || g.gu_type == T_NVFP4; 3352#else 3353 constexpr bool q50 = false; 3354#endif 3355 if ((d.occ_nt <= 0 && !d.ws) || !gu_covered(g.gu_type) || !d_covered_on(g.d_type, d) || q50) { 3356 std::fprintf(stderr, "prefill fused experts (native, NT = 256): types %d / %d are not covered here\n", g.gu_type, 3357 g.d_type); 3358 std::exit(1); 3359 } 3360 const cudaStream_t s = (cudaStream_t) stream; 3361 const Tables tb = tables(const_cast<void*>(scratch), n_expert); 3362 // WW 2 (128 x 64 items), as many blocks as are resident (the WS kernel: one a multiprocessor); the tiles as in 3363 // experts_native(const Batch&) 3364 const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0); 3365 const bool ws = d.ws; 3366 const int occ = ws ? 1 : d.occ_nt; 3367 const unsigned g_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(2)), (int64_t) d.sms * occ); 3368 const unsigned g_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(2)), (int64_t) d.sms * occ); 3369 switch (g.gu_type) { 3370 case T_IQ2_XXS: launch<T_IQ2_XXS, true>(ws, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3371 case T_IQ2_XS: launch<T_IQ2_XS, true>(ws, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3372 case T_IQ2_S: launch<T_IQ2_S, true>(ws, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3373 case T_IQ3_XXS: launch<T_IQ3_XXS, true>(ws, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3374 case T_IQ3_S: launch<T_IQ3_S, true>(ws, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3375 case T_Q2_0: launch<T_Q2_0, true>(ws, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3376 default: launch<T_IQ4_XS, true>(ws, g_gu, b, g, tb, xa, src, ha, nullptr, s); break; 3377 } 3378 if (g.d_type == T_Q2_0) launch<T_Q2_0, false>(ws, g_d, b, g, tb, ha, src, nullptr, dm, s); 3379 else launch<T_IQ4_NL, false>(ws, g_d, b, g, tb, ha, src, nullptr, dm, s); 3380 ck(cudaGetLastError(), "experts_native (NT = 256)"); 3381} 3382#endif 3383} // namespace strata::prefill::fused