okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/cuda/dequant_bf16.cu - see include/strata/kernels/dequant_bf16.hpp. 2// 3// Arithmetic transcribed from ggml/src/ggml-quants.c at the pinned llama.cpp (MIT License, Copyright (c) 2023-2026 4// The ggml authors): dequantize_row_q2_0/q4_0/q5_0/q8_0/q3_K/q4_K/q5_K/q6_K/iq4_nl/iq4_xs. 5#include "strata/kernels/dequant_bf16.hpp" 6#include "strata/core/tc_sm75.hpp" 7#include "strata/kernels/iq_kernels.hpp" 8#include "strata/core/defaults_026.hpp" 9#include "strata/core/sm120_series.hpp" 10 11#include <cuda_fp16.h> 12#include <cuda_runtime.h> 13 14#include <cstdint> 15#include <cstdio> 16#include <cstdlib> 17 18namespace strata::kernels { 19namespace { 20 21__device__ __forceinline__ float h2f(const uint8_t* p) { 22 return __half2float(__ushort_as_half((uint16_t) (p[0] | (p[1] << 8)))); 23} 24__device__ __forceinline__ uint16_t f2bf(float f) { 25 uint32_t u = __float_as_uint(f); 26 // a NaN (a NaN scale in the block) stays a quiet NaN, as in ggml_compute_fp32_to_bf16 and `bf16_from_f32`: 27 // the rounding add below would carry it into -0 or inf 28 if ((u & 0x7fffffffu) > 0x7f800000u) return (uint16_t) ((u >> 16) | 64u); 29 u += 0x7fffu + ((u >> 16) & 1u); // round to nearest even 30 return (uint16_t) (u >> 16); 31} 32__device__ __forceinline__ void put(uint16_t* o, int i, float v) { o[i] = f2bf(v); } 33struct H16 { uint16_t v; }; 34__device__ __forceinline__ void put(H16* o, int i, float v) { o[i].v = __half_as_ushort(__float2half_rn(v)); } 35__device__ __forceinline__ void put(float* o, int i, float v) { o[i] = v; } 36 37__constant__ int8_t kv_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113}; 38 39__device__ __forceinline__ void scale_min_k4(int j, const uint8_t* q, int& d, int& m) { 40 if (j < 4) { d = q[j] & 63; m = q[j + 4] & 63; } 41 else { d = (q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4); m = (q[j + 4] >> 4) | ((q[j] >> 6) << 4); } 42} 43 44// One 32-element group `g` (row-major over the whole slice); `out` points at that group's 32 outputs. 45template <int TYPE, typename T> 46__device__ __forceinline__ void group32(const uint8_t* row_blocks, int gi_in_row, T* out) { 47 if constexpr (TYPE == 42) { // Q2_0: 64 per block of 18 B 48 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 2) * 18; 49 const float d = h2f(b); 50 const int e0 = (gi_in_row % 2) * 32; 51#pragma unroll 52 for (int j = 0; j < 32; ++j) { 53 const int e = e0 + j; 54 const int q = (b[2 + e / 4] >> ((e % 4) * 2)) & 3; 55 put(out, j, (float) (q - 1) * d); 56 } 57 } else if constexpr (TYPE == 2) { // Q4_0 58 const uint8_t* b = row_blocks + (size_t) gi_in_row * 18; 59 const float d = h2f(b); 60 for (int j = 0; j < 16; ++j) { 61 put(out, j, (float) ((b[2 + j] & 0x0F) - 8) * d); 62 put(out, j + 16, (float) ((b[2 + j] >> 4) - 8) * d); 63 } 64 } else if constexpr (TYPE == 6) { // Q5_0 65 const uint8_t* b = row_blocks + (size_t) gi_in_row * 22; 66 const float d = h2f(b); 67 const uint32_t qh = (uint32_t) b[2] | ((uint32_t) b[3] << 8) | ((uint32_t) b[4] << 16) | ((uint32_t) b[5] << 24); 68 for (int j = 0; j < 16; ++j) { 69 const int xh0 = ((qh >> j) << 4) & 0x10; 70 const int xh1 = (qh >> (j + 12)) & 0x10; 71 put(out, j, (float) (((b[6 + j] & 0x0F) | xh0) - 16) * d); 72 put(out, j + 16, (float) (((b[6 + j] >> 4) | xh1) - 16) * d); 73 } 74 } else if constexpr (TYPE == 7) { // Q5_1 75 const uint8_t* b = row_blocks + (size_t) gi_in_row * 24; 76 const float d = h2f(b), m = h2f(b + 2); 77 const uint32_t qh = (uint32_t) b[4] | ((uint32_t) b[5] << 8) | 78 ((uint32_t) b[6] << 16) | ((uint32_t) b[7] << 24); 79 for (int j = 0; j < 16; ++j) { 80 const int xh0 = ((qh >> j) & 1) << 4; 81 const int xh1 = ((qh >> (j + 16)) & 1) << 4; 82 put(out, j, (float) ((b[8 + j] & 15) | xh0) * d + m); 83 put(out, j + 16, (float) ((b[8 + j] >> 4) | xh1) * d + m); 84 } 85 } else if constexpr (TYPE == 8) { // Q8_0 86 const uint8_t* b = row_blocks + (size_t) gi_in_row * 34; 87 const float d = h2f(b); 88 for (int j = 0; j < 32; ++j) put(out, j, (float) (int8_t) b[2 + j] * d); 89 } else if constexpr (TYPE == 20) { // IQ4_NL 90 const uint8_t* b = row_blocks + (size_t) gi_in_row * 18; 91 const float d = h2f(b); 92 for (int j = 0; j < 16; ++j) { 93 put(out, j, d * (float) kv_iq4nl[b[2 + j] & 0xf]); 94 put(out, j + 16, d * (float) kv_iq4nl[b[2 + j] >> 4]); 95 } 96 } else if constexpr (TYPE == 11) { // Q3_K: hmask[32] qs[64] scales[12] d 97 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 110; 98 const int gi = gi_in_row % 8, n = gi / 4, jj = gi % 4; 99 const uint8_t* hm = b; 100 const uint8_t* q = b + 32 + n * 32; 101 const uint8_t* sc = b + 96; 102 const float d_all = h2f(b + 108); 103 uint32_t aux[4]; 104 memcpy(aux, sc, 12); 105 const uint32_t kmask1 = 0x03030303u, kmask2 = 0x0f0f0f0fu, tmp = aux[2]; 106 aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4); 107 aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4); 108 aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4); 109 aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4); 110 const int8_t* scales = reinterpret_cast<const int8_t*>(aux); 111 const int shift = 2 * jj; 112 const uint8_t m = (uint8_t) (1u << (n * 4 + jj)); 113 for (int t = 0; t < 32; ++t) { 114 const int is = n * 8 + jj * 2 + (t >= 16 ? 1 : 0); 115 const float dl = d_all * (float) (scales[is] - 32); 116 put(out, t, dl * (float) ((int) ((q[t] >> shift) & 3) - ((hm[t] & m) ? 0 : 4))); 117 } 118 } else if constexpr (TYPE == 12) { // Q4_K: d dmin scales[12] qs[128] 119 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 144; 120 const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2; 121 const float d = h2f(b), dmin = h2f(b + 2); 122 int sc, m; 123 scale_min_k4(gi, b + 4, sc, m); 124 const float d1 = d * (float) sc, m1 = dmin * (float) m; 125 const uint8_t* q = b + 16 + 32 * j64; 126 for (int l = 0; l < 32; ++l) put(out, l, d1 * (float) (hi ? (q[l] >> 4) : (q[l] & 0xF)) - m1); 127 } else if constexpr (TYPE == 13) { // Q5_K: d dmin scales[12] qh[32] qs[128] 128 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 176; 129 const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2; 130 const float d = h2f(b), dmin = h2f(b + 2); 131 int sc, m; 132 scale_min_k4(gi, b + 4, sc, m); 133 const float d1 = d * (float) sc, m1 = dmin * (float) m; 134 const uint8_t* qh = b + 16; 135 const uint8_t* ql = b + 48 + 32 * j64; 136 const uint8_t u = (uint8_t) (1u << (2 * j64 + hi)); 137 for (int l = 0; l < 32; ++l) { 138 const int nib = hi ? (ql[l] >> 4) : (ql[l] & 0xF); 139 put(out, l, d1 * (float) (nib + ((qh[l] & u) ? 16 : 0)) - m1); 140 } 141 } else if constexpr (TYPE == 14) { // Q6_K: ql[128] qh[64] scales[16] d 142 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 210; 143 const int gi = gi_in_row % 8, n = gi / 4, qu = gi % 4; 144 const uint8_t* ql = b + 64 * n; 145 const uint8_t* qh = b + 128 + 32 * n; 146 const int8_t* sc = reinterpret_cast<const int8_t*>(b + 192) + 8 * n; 147 const float d = h2f(b + 208); 148 for (int l = 0; l < 32; ++l) { 149 const int is = l / 16; 150 int q; 151 if (qu == 0) q = (ql[l] & 0xF) | (((qh[l] >> 0) & 3) << 4); 152 else if (qu == 1) q = (ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4); 153 else if (qu == 2) q = (ql[l] >> 4) | (((qh[l] >> 4) & 3) << 4); 154 else q = (ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4); 155 put(out, l, d * (float) sc[is + 2 * qu] * (float) (q - 32)); 156 } 157 } else if constexpr (TYPE == 23) { // IQ4_XS: d scales_h scales_l[4] qs[128] 158 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 136; 159 const int ib = gi_in_row % 8; 160 const float d = h2f(b); 161 const uint16_t scales_h = (uint16_t) (b[2] | (b[3] << 8)); 162 const int ls = ((b[4 + ib / 2] >> (4 * (ib % 2))) & 0xf) | (((scales_h >> (2 * ib)) & 3) << 4); 163 const float dl = d * (float) (ls - 32); 164 const uint8_t* qs = b + 8 + 16 * ib; 165 for (int j = 0; j < 16; ++j) { 166 put(out, j, dl * (float) kv_iq4nl[qs[j] & 0xf]); 167 put(out, j + 16, dl * (float) kv_iq4nl[qs[j] >> 4]); 168 } 169 } 170} 171 172/// `VEC` (fork, Daily be4cf1c, sm_75 only): the group is dequantised into a register buffer and flushed with 16-byte 173/// stores. The plain form writes each element where it is computed, which for a warp means 32 store instructions 174/// whose lanes are 64 bytes apart - 32 two-byte transactions per instruction. **The values are identical** (the 175/// same arithmetic, the same order), only the stores are wider; the Turing build measured this kernel at 18% of the 176/// row's bandwidth. (0.1.42's Q8_0 kernel below already writes coalesced and is left as it is.) 177template <int TYPE, typename T, bool VEC> 178__global__ void dequant_kernel(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t row0, int64_t rows, 179 int64_t groups_per_row, int64_t ld, T* __restrict__ out) { 180 const int64_t g = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 181 if (g >= rows * groups_per_row) return; 182 const int64_t r = g / groups_per_row, gi = g % groups_per_row; 183 T* dst = out + r * ld + gi * 32; 184 if constexpr (!VEC) { 185 group32<TYPE>(blocks + (row0 + r) * row_bytes, (int) gi, dst); 186 } else { 187 __align__(16) T buf[32]; // the uint4 flush below reads it as 16-byte vectors 188 group32<TYPE>(blocks + (row0 + r) * row_bytes, (int) gi, buf); 189 // 32 elements: 64 B for the 16-bit forms (4 x uint4), 128 B for the float one (8 x uint4) 190 constexpr int CHUNKS = (sizeof(T) == 2 ? 4 : 8); 191#pragma unroll 192 for (int c = 0; c < CHUNKS; ++c) reinterpret_cast<uint4*>(dst)[c] = reinterpret_cast<const uint4*>(buf)[c]; 193 } 194} 195 196// ---- the wide kernels: a thread per 8 values (Pascal, sm_61) -------------------------------------------------------- 197// dequant_kernel's thread reads its 32-value group a byte at a time and writes 32 two-byte values one by one, 64 bytes 198// from its neighbour's. On a Tesla P40 (sm_61) that is 7.2 ms for a 10240 x 2560 Q6_K weight (10 GB/s), and the IQ3_S 199// pack's dense weights cost every prompt chunk ~0.5 s of it, whatever the chunk's length (a fifth of a 560-token prompt). 200// Here a thread takes 8 consecutive values: its code bytes as 16-bit loads (the GGUF blocks are 2-byte aligned) and 201// one 16-byte store (two for FP32), so a warp writes 512 consecutive bytes. The arithmetic per value is group32's - 202// the same expression on the same operands - so the output is bitwise the same (dequant_wide_parity). On the P40: 203// Q6_K 7.2 -> 0.33 ms (227 GB/s), Q4_K 1.67 -> 0.32 ms, Q5_K 1.24 -> 0.33 ms, IQ4_XS 1.21 -> 0.34 ms for that shape. 204// Q4_K, Q5_K, Q6_K and IQ4_XS. On by default on compute capability 6.1, where it was measured; 205// STRATA_DEQUANT_WIDE=0 / =1 turns it off / on for every card. 206__device__ __forceinline__ uint64_t ld8(const uint8_t* p) { // 8 bytes at a 2-byte aligned address 207 const uint16_t* q = reinterpret_cast<const uint16_t*>(p); 208 return (uint64_t) q[0] | ((uint64_t) q[1] << 16) | ((uint64_t) q[2] << 32) | ((uint64_t) q[3] << 48); 209} 210// values [8 piece, 8 piece + 8) of the 32-element group `gi_in_row`, as group32 computes them 211template <int TYPE, typename T> 212__device__ __forceinline__ void wide8(const uint8_t* row_blocks, int gi_in_row, int piece, T* v) { 213 const int l0 = 8 * piece; 214 if constexpr (TYPE == 12) { // Q4_K: d dmin scales[12] qs[128] 215 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 144; 216 const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2; 217 const float d = h2f(b), dmin = h2f(b + 2); 218 int sc, m; 219 scale_min_k4(gi, b + 4, sc, m); 220 const float d1 = d * (float) sc, m1 = dmin * (float) m; 221 const uint64_t q = ld8(b + 16 + 32 * j64 + l0); 222#pragma unroll 223 for (int k = 0; k < 8; ++k) { 224 const int lb = (int) (q >> (8 * k)) & 0xFF; 225 put(v, k, d1 * (float) (hi ? (lb >> 4) : (lb & 0xF)) - m1); 226 } 227 } else if constexpr (TYPE == 13) { // Q5_K: d dmin scales[12] qh[32] qs[128] 228 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 176; 229 const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2; 230 const float d = h2f(b), dmin = h2f(b + 2); 231 int sc, m; 232 scale_min_k4(gi, b + 4, sc, m); 233 const float d1 = d * (float) sc, m1 = dmin * (float) m; 234 const uint64_t qh = ld8(b + 16 + l0), ql = ld8(b + 48 + 32 * j64 + l0); 235 const int ub = 2 * j64 + hi; 236#pragma unroll 237 for (int k = 0; k < 8; ++k) { 238 const int lb = (int) (ql >> (8 * k)) & 0xFF, hb = (int) (qh >> (8 * k)) & 0xFF; 239 const int nib = hi ? (lb >> 4) : (lb & 0xF); 240 put(v, k, d1 * (float) (nib + (((hb >> ub) & 1) ? 16 : 0)) - m1); 241 } 242 } else if constexpr (TYPE == 14) { // Q6_K: ql[128] qh[64] scales[16] d 243 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 210; 244 const int gi = gi_in_row % 8, n = gi / 4, qu = gi % 4; 245 const uint64_t ql = ld8(b + 64 * n + ((qu & 1) ? 32 : 0) + l0); 246 const uint64_t qh = ld8(b + 128 + 32 * n + l0); 247 const int8_t* sc = reinterpret_cast<const int8_t*>(b + 192) + 8 * n; 248 const float d = h2f(b + 208); 249 const float ds = d * (float) sc[l0 / 16 + 2 * qu]; 250#pragma unroll 251 for (int k = 0; k < 8; ++k) { 252 const int lb = (int) (ql >> (8 * k)) & 0xFF, hb = (int) (qh >> (8 * k)) & 0xFF; 253 const int q = ((qu >= 2) ? (lb >> 4) : (lb & 0xF)) | (((hb >> (2 * qu)) & 3) << 4); 254 put(v, k, ds * (float) (q - 32)); 255 } 256 } else if constexpr (TYPE == 23) { // IQ4_XS: d scales_h scales_l[4] qs[128] 257 const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 136; 258 const int ib = gi_in_row % 8; 259 const float d = h2f(b); 260 const uint16_t scales_h = (uint16_t) (b[2] | (b[3] << 8)); 261 const int ls = ((b[4 + ib / 2] >> (4 * (ib % 2))) & 0xf) | (((scales_h >> (2 * ib)) & 3) << 4); 262 const float dl = d * (float) (ls - 32); 263 const uint64_t qs = ld8(b + 8 + 16 * ib + 8 * (piece & 1)); // pieces 0, 1: the low nibbles; 2, 3: the high ones 264#pragma unroll 265 for (int k = 0; k < 8; ++k) { 266 const int lb = (int) (qs >> (8 * k)) & 0xFF; 267 put(v, k, dl * (float) kv_iq4nl[piece >= 2 ? (lb >> 4) : (lb & 0xf)]); 268 } 269 } 270} 271template <int TYPE, typename T> 272__global__ void dequant_wide_kernel(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t row0, uint32_t rows, 273 uint32_t groups_per_row, int64_t ld, T* __restrict__ out) { 274 const uint32_t i = blockIdx.x * blockDim.x + threadIdx.x; // launch(): 4 * rows * groups_per_row < 2^31 275 const uint32_t g = i >> 2, r = g / groups_per_row, gi = g - r * groups_per_row; 276 if (r >= rows) return; 277 const int piece = (int) (i & 3); 278 __align__(16) T v[8]; 279 wide8<TYPE>(blocks + (row0 + (int64_t) r) * row_bytes, (int) gi, piece, v); 280 uint4* o = (uint4*) (out + (int64_t) r * ld + (int64_t) gi * 32 + 8 * piece); 281#pragma unroll 282 for (int k = 0; k < (int) (8 * sizeof(T) / 16); ++k) o[k] = ((const uint4*) v)[k]; 283} 284bool wide_type(int type) { return type == 12 || type == 13 || type == 14 || type == 23; } 285int g_dequant_wide = -1; // dequant_set_wide (the parity test); -1 = STRATA_DEQUANT_WIDE, else the card's default 286bool dequant_wide_on() { 287 static const int env = [] { const char* v = std::getenv("STRATA_DEQUANT_WIDE"); return v != nullptr && v[0] != '\0' ? (std::atoi(v) != 0 ? 1 : 0) : -1; }(); 288 if (g_dequant_wide >= 0) return g_dequant_wide != 0; 289 if (env >= 0) return env != 0; 290 // 0 unknown, 1 no, 2 yes: sm_61 (measured on a Tesla P40), sm_86 (RTX 3060 / A4000, 0.1.45: short-prompt TTFT -12..-17%, 291 // bitwise) and sm_80 (CMP 170HX: a 3,259-token prompt 857 -> 792 ms of GPU time, prompts +4-7%, bitwise) 292 static int per_dev[64]; 293 int dev = 0; 294 if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { (void) cudaGetLastError(); return false; } 295 if (!per_dev[dev]) { 296 int major = 0, minor = 0; 297 if (cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess || 298 cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev) != cudaSuccess) { (void) cudaGetLastError(); return false; } 299 // sm_80 is the 0.2.7 addition (#3779): STRATA_DEFAULTS=026 gives it the per-group kernels again, as 0.2.6 had 300 per_dev[dev] = (major == 6 && minor == 1) || (major == 8 && minor == 6) || 301 (major == 8 && minor == 0 && strata::sm80_default_on(nullptr, strata::qwen_defaults_026())) ? 2 : 1; 302 } 303 return per_dev[dev] == 2; 304} 305 306// Q8_0 with four threads per block, 8 values each: the warp reads 8 consecutive blocks and writes 512 consecutive 307// bytes (a thread per block wrote 2-byte values 64 bytes apart: ~110 GB/s, a quarter of a prompt projection's time) 308template <typename T> 309__global__ void dequant_q8_0_kernel(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t row0, int64_t rows, 310 int64_t groups_per_row, T* __restrict__ out) { 311 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 312 if (i >= rows * groups_per_row * 4) return; 313 const int64_t g = i >> 2, r = g / groups_per_row, gi = g % groups_per_row; 314 const int piece = (int) (i & 3); 315 const uint8_t* b = blocks + (row0 + r) * row_bytes + gi * 34; 316 const float d = h2f(b); 317 const uint16_t* q = (const uint16_t*) (b + 2 + 8 * piece); // blocks are 2-byte aligned 318 __align__(16) T v[8]; 319#pragma unroll 320 for (int k = 0; k < 4; ++k) { 321 const uint16_t w = q[k]; 322 put(v, 2 * k, (float) (int8_t) (w & 0xFF) * d); 323 put(v, 2 * k + 1, (float) (int8_t) (w >> 8) * d); 324 } 325 uint4* o = (uint4*) (out + g * 32 + 8 * piece); 326#pragma unroll 327 for (int k = 0; k < (int) (8 * sizeof(T) / 16); ++k) o[k] = ((const uint4*) v)[k]; 328} 329 330bool geometry(int type, int& block_elems, int& block_bytes) { 331 switch (type) { 332 case 2: block_elems = 32; block_bytes = 18; return true; 333 case 6: block_elems = 32; block_bytes = 22; return true; 334 case 7: block_elems = 32; block_bytes = 24; return true; 335 case 8: block_elems = 32; block_bytes = 34; return true; 336 case 20: block_elems = 32; block_bytes = 18; return true; 337 case 11: block_elems = 256; block_bytes = 110; return true; 338 case 12: block_elems = 256; block_bytes = 144; return true; 339 case 13: block_elems = 256; block_bytes = 176; return true; 340 case 14: block_elems = 256; block_bytes = 210; return true; 341 case 23: block_elems = 256; block_bytes = 136; return true; 342 case 42: block_elems = 64; block_bytes = 18; return true; 343 default: return false; 344 } 345} 346 347template <typename T> 348void launch(int type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, T* out, void* stream, 349 int64_t ld = 0) { 350 int be = 0, bb = 0; 351 if (!geometry(type, be, bb) || cols % be != 0 || rows <= 0) { 352 std::fprintf(stderr, "dequant: unsupported type %d or shape %lld x %lld\n", type, (long long) rows, 353 (long long) cols); 354 std::exit(1); 355 } 356 const int64_t row_bytes = cols / be * bb, gpr = cols / 32, total = rows * gpr; 357 if (ld <= 0) ld = cols; 358 const unsigned grid = (unsigned) ((total + 255) / 256); 359 const uint8_t* p = (const uint8_t*) blocks; 360 cudaStream_t st = (cudaStream_t) stream; 361 if (type == 8 && ld == cols && (reinterpret_cast<uintptr_t>(blocks) & 1) == 0 && 362 (reinterpret_cast<uintptr_t>(out) & 15) == 0) { // contiguous rows only (the kernel writes out + g * 32) 363 dequant_q8_0_kernel<T><<<(unsigned) ((4 * total + 255) / 256), 256, 0, st>>>(p, row_bytes, row0, rows, gpr, out); 364 const cudaError_t e = cudaGetLastError(); 365 if (e != cudaSuccess) { std::fprintf(stderr, "dequant launch: %s\n", cudaGetErrorString(e)); std::exit(1); } 366 return; 367 } 368 // the wide kernels: even block addresses, 16-byte stores at every row (contiguous or a stride of whole stores) 369 if (wide_type(type) && (reinterpret_cast<uintptr_t>(blocks) & 1) == 0 && (reinterpret_cast<uintptr_t>(out) & 15) == 0 && 370 ((size_t) ld * sizeof(T)) % 16 == 0 && 4 * total < (1ll << 31) && dequant_wide_on()) { 371 const unsigned wgrid = (unsigned) ((4 * total + 255) / 256); 372#define STRATA_DQW(TY) dequant_wide_kernel<TY, T><<<wgrid, 256, 0, st>>>(p, row_bytes, row0, (uint32_t) rows, (uint32_t) gpr, ld, out); break 373 switch (type) { 374 case 12: STRATA_DQW(12); 375 case 13: STRATA_DQW(13); 376 case 14: STRATA_DQW(14); 377 case 23: STRATA_DQW(23); 378 } 379#undef STRATA_DQW 380 const cudaError_t e = cudaGetLastError(); 381 if (e != cudaSuccess) { std::fprintf(stderr, "dequant launch: %s\n", cudaGetErrorString(e)); std::exit(1); } 382 return; 383 } 384 // sm_75 (and only cc 7.5) takes the vectorised-store form when every group's 32 outputs start on 16 bytes (an 385 // aligned `out` and a row stride of whole 16-byte units); every other SM model keeps the plain kernel 386 int dev = 0, cmaj = 0, cmin = 0; 387 const bool vec = (reinterpret_cast<uintptr_t>(out) & 15) == 0 && ((uint64_t) ld * sizeof(T)) % 16 == 0 && 388 cudaGetDevice(&dev) == cudaSuccess && 389 cudaDeviceGetAttribute(&cmaj, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess && 390 cudaDeviceGetAttribute(&cmin, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess && 391 strata::core::tc_sm75_enabled(cmaj, cmin); 392 if (!vec) cudaGetLastError(); 393#define STRATA_DQ(TY) \ 394 if (vec) dequant_kernel<TY, T, true><<<grid, 256, 0, st>>>(p, row_bytes, row0, rows, gpr, ld, out); \ 395 else dequant_kernel<TY, T, false><<<grid, 256, 0, st>>>(p, row_bytes, row0, rows, gpr, ld, out); \ 396 break 397 switch (type) { 398 case 2: STRATA_DQ(2); 399 case 6: STRATA_DQ(6); 400 case 7: STRATA_DQ(7); 401 case 8: STRATA_DQ(8); 402 case 11: STRATA_DQ(11); 403 case 12: STRATA_DQ(12); 404 case 13: STRATA_DQ(13); 405 case 14: STRATA_DQ(14); 406 case 20: STRATA_DQ(20); 407 case 23: STRATA_DQ(23); 408 case 42: STRATA_DQ(42); 409 } 410#undef STRATA_DQ 411 const cudaError_t e = cudaGetLastError(); 412 if (e != cudaSuccess) { std::fprintf(stderr, "dequant launch: %s\n", cudaGetErrorString(e)); std::exit(1); } 413} 414 415} // namespace 416 417void dequant_set_wide(int mode) { g_dequant_wide = mode; } 418 419bool dequant_bf16_supported(int ggml_type) noexcept { 420 int a, b; 421 return geometry(ggml_type, a, b); 422} 423 424namespace { 425// plan v0.3 P6: the i-quant formats (llama.cpp's dequantizers, iq_kernels.cu) 426bool iq_only(int t) { return t == 16 || t == 17 || t == 18 || t == 21 || t == 22 || t == 29; } 427} // namespace 428 429void dequant_bf16(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, uint16_t* out, 430 void* stream) { 431 launch<uint16_t>(ggml_type, blocks, row0, rows, cols, out, stream); 432} 433 434void dequant_f16(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, uint16_t* out, 435 void* stream) { 436 if (iq_only(ggml_type)) { 437 iq_dequant_f16(ggml_type, (const uint8_t*) blocks + (size_t) row0 * iq_row_bytes(ggml_type, cols), rows * cols, 438 out, stream); 439 return; 440 } 441 launch<H16>(ggml_type, blocks, row0, rows, cols, reinterpret_cast<H16*>(out), stream); 442} 443 444bool dequant_f16_ld(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, int64_t ld, 445 uint16_t* out, void* stream) { 446 int be = 0, bb = 0; 447 if (iq_only(ggml_type) || !geometry(ggml_type, be, bb) || cols % be != 0 || rows <= 0 || ld < cols || ld % 8 != 0) 448 return false; 449 launch<H16>(ggml_type, blocks, row0, rows, cols, reinterpret_cast<H16*>(out), stream, ld); 450 return true; 451} 452 453void dequant_f32(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, float* out, void* stream) { 454 if (iq_only(ggml_type)) { 455 iq_dequant_f32(ggml_type, (const uint8_t*) blocks + (size_t) row0 * iq_row_bytes(ggml_type, cols), rows * cols, 456 out, stream); 457 return; 458 } 459 launch<float>(ggml_type, blocks, row0, rows, cols, out, stream); 460} 461 462} // namespace strata::kernels