okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
62db0bf10
main
1// src/kernels/cuda/quantize_act.cu - P2.S2: quantize an activation the way ggml converts src1. 2// 3// WHY THIS EXISTS AT ALL. `ggml_mul_mat` converts the ACTIVATION (src1) to the weight's `vec_dot_type` 4// before the dot product - the rule established over rounds 119-136 and written into `ref/quant.py` as 5// `apply_src1`. The CPU expert path already honours it (`bench/micro/cpu_s2.cpp` quantizes to int8 and uses 6// `vpdpbusd`), and its P0.T2 parity passes at 1.461e-06. The GPU kernels added in rounds 166-177 take FP16 7// activations instead. **Left that way, the two paths would compute different numbers for different experts 8// of the SAME token** - not a tolerance question, an inconsistency inside one forward pass. 9// 10// Q2_0 IS THE CASE THAT MATTERS: the routed experts are all Q2_0, `Q2_0 -> Q8_0`, and that is 31.64 GiB of the 11// 38 GiB pack. Q8_K (for the K-quants and IQ4_XS) is the other half of the table and is not here yet. 12// 13// THE THREE SUBTLETIES, each of which was paid for once already and is transcribed rather than recalled: 14// 15// 1. `d32 = amax / 127.0f` is computed in FP32, and the QUANTIZED INTEGERS divide by **d32**, not by the 16// fp16-rounded value that gets stored. Conflating the two shifts every quant near a rounding boundary - 17// measured against ggml's own bytes, 18 of 80 blocks differed, and because Q2_0 experts convert to Q8_0 18// the error landed in all 48 MoE blocks and made the end-to-end KL WORSE. 19// 2. The reference divides in FLOAT64 (`blk.astype(np.float64) / float(np.float32(d32))`) and rounds with 20// `rint` - half to even. This kernel therefore divides in double too: an FP32 division followed by 21// `rintf` differs from the reference wherever the f32 quotient rounds across a .5 boundary. 22// 3. The DEQUANTIZED value is `q * d16`, the fp16 scale, not `q * d32`. The block stores fp16 and that is 23// what a reader multiplies by. 24#include "strata/kernels/quantize_act.hpp" 25#include "strata/kernels/quantize_act_dev.cuh" 26#include "strata/kernels/swiglu.cuh" 27#include "strata/kernels/bf16_bits.hpp" 28#include "strata/kernels/f16_bits.hpp" 29 30#include <cuda_runtime.h> 31 32#include <cmath> 33#include <cstdio> 34#include <cstdlib> 35 36namespace strata::kernels { 37namespace { 38 39constexpr int QK8_0 = 32; 40 41// THE fp16 CONVERSION LIVES IN `strata/kernels/f16_bits.hpp`, and this file used to carry its own copy. 42// 43// Round 198 found the private copy wrong in a way no fixture here could see: it tested `if (exp >= 31)` to 44// detect an out-of-range exponent, which conflates an f32 INF/NAN (raw exponent 255) with a FINITE value too 45// large for fp16. Every finite overflow - 1e30, 65536, 1e45 - came back as a NaN instead of saturating to 46// inf, and this file's fixture is O(1) throughout, so it passed. A numpy-generated oracle caught it on its 47// first run. Three copies of a converter whose failure mode is silent wrong bits was two copies too many. 48 49 50__global__ void quantize_q8_0_kernel(const float* __restrict__ x, uint8_t* __restrict__ blocks, 51 long long n_blocks) { 52 const long long b = (long long) blockIdx.x * blockDim.x + threadIdx.x; 53 if (b >= n_blocks) return; 54 quantize_q8_0_block(x + b * QK8_0, blocks + b * 34); 55} 56 57/// **THE HIT PATH'S QUANTIZER, AND IT EXISTS TO REPRODUCE `act_quant_q8_1` EXACTLY (R4.2h, round 331).** 58/// 59/// The engine computes every routed expert twice. MISSES go through `cpu/expert.cpp:138 act_quant_q8_1` 60/// into the VNNI kernel; HITS went through `quantize_q8_0` above. **Those are not the same quantization**, 61/// and round 330 measured the difference with both real implementations linked 62/// (`bench/micro/act_quant_parity.cu`): 63/// 64/// int8 activations differing : 0 of 2560 <- this rule's ties are measure-zero 65/// chunks whose SCALE differs : 80 of 80, max rel 4.761e-04 <- 2^-11: fp32 vs the block's fp16 66/// 67/// The scale is the difference that matters: `ActQ::scale` is `float` and the CPU kernel multiplies by it 68/// (`expert.cpp:92`), while `row_dot_s2_q8` read `f16_at(xb)` out of the `block_q8_0`. So this variant 69/// writes the SAME 34-byte blocks (the kernel's indexing is unchanged) **and** a parallel fp32 scale array 70/// carrying `d32` unrounded. 71/// 72/// It also adopts the CPU's ROUNDING RULE - multiply by the reciprocal, round half AWAY FROM ZERO - rather 73/// than this file's `rint`. That is deliberate and it is the opposite of the direction the comment above 74/// argues for. `quantize_q8_0` was tuned to match **ggml's Q8_0 bytes**, which is right for the pack and for 75/// `moe_hit_parity`. This function's job is different: it must match **this engine's own CPU reference**, 76/// because a hit and a miss for the same expert on the same layer have to produce the same number. The CPU 77/// path is the reference - C1 passes on it - so the hit path is brought to it, not the reverse. 78__global__ void quantize_q8_0_scaled_thread_kernel(const float* __restrict__ x, uint8_t* __restrict__ blocks, 79 float* __restrict__ scales, long long n_blocks) { 80 const long long b = (long long) blockIdx.x * blockDim.x + threadIdx.x; 81 if (b >= n_blocks) return; 82 quantize_q8_0_scaled_block(x + b * QK8_0, blocks + b * 34, scales + b); 83} 84 85// A warp a block, lane i on value i: the block's |max| is a max over the same values (0 for none above it, as the 86// serial scan from 0 through fmaxf), so every byte is the per-thread loop's. 87__global__ void quantize_q8_0_scaled_kernel(const float* __restrict__ x, uint8_t* __restrict__ blocks, 88 float* __restrict__ scales, long long n_blocks) { 89 const long long b = ((long long) blockIdx.x * blockDim.x + threadIdx.x) >> 5; 90 const int i = (int) (threadIdx.x & 31); 91 if (b >= n_blocks) return; 92 const float xi = x[b * QK8_0 + i]; 93 uint8_t* out = blocks + b * 34; 94 95 float amax = fmaxf(0.0f, fabsf(xi)); 96 for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o)); 97 // VERBATIM from `cpu/expert.cpp:144-145`, including the `amax > 0` guard, so the fp32 value written here 98 // is bit-identical to the `s` the CPU path used. 99 const float s = amax > 0.f ? amax / 127.f : 0.f; 100 const float inv = s > 0.f ? 1.f / s : 0.f; 101 if (i == 0) { 102 scales[b] = s; 103 const uint16_t d16bits = f16_from_f32(s); 104 out[0] = (uint8_t) (d16bits & 0xFF); 105 out[1] = (uint8_t) (d16bits >> 8); 106 } 107 // VERBATIM from `cpu/expert.cpp:159-162`: reciprocal multiply, then `t + copysign(0.5, t)` truncated 108 // toward zero, which is `lround`'s rule - round half away from zero. 109 const float t = xi * inv; 110 const float r = t + (t >= 0.f ? 0.5f : -0.5f); 111 int v = (int) r; 112 v = v < -127 ? -127 : (v > 127 ? 127 : v); 113 out[2 + i] = (uint8_t) (int8_t) v; 114} 115 116__global__ void dequant_q8_0_kernel(const uint8_t* __restrict__ blocks, float* __restrict__ x, 117 long long n_blocks) { 118 const long long b = (long long) blockIdx.x * blockDim.x + threadIdx.x; 119 if (b >= n_blocks) return; 120 const uint8_t* blk = blocks + b * 34; 121 const uint16_t dbits = (uint16_t) (blk[0] | (blk[1] << 8)); 122 const float d = f32_from_f16(dbits); 123 float* out = x + b * QK8_0; 124 for (int i = 0; i < QK8_0; ++i) out[i] = (float) (int8_t) blk[2 + i] * d; 125} 126 127// ===================== Q8_K ===================== 128// 129// `block_q8_K` = { float d ; int8_t qs[256] ; int16_t bsums[16] } = 292 bytes, no padding 130// (`static_assert` in ggml-common.h). QK_K = 256. 131 132constexpr int QK_K = 256; 133constexpr int Q8K_BYTES = 292; 134 135// `nearest_int_dev` and the per-block quantizers live in `quantize_act_dev.cuh` (shared with the fused 136// kernels); the comment that used to justify this copy is there. 137 138__global__ void quantize_q8_K_kernel(const float* __restrict__ x, uint8_t* __restrict__ blocks, 139 long long n_blocks) { 140 const long long b = (long long) blockIdx.x * blockDim.x + threadIdx.x; 141 if (b >= n_blocks) return; 142 quantize_q8_K_block(x + b * QK_K, blocks + b * Q8K_BYTES); 143} 144 145__global__ void dequant_q8_K_kernel(const uint8_t* __restrict__ blocks, float* __restrict__ x, 146 long long n_blocks) { 147 const long long b = (long long) blockIdx.x * blockDim.x + threadIdx.x; 148 if (b >= n_blocks) return; 149 const uint8_t* blk = blocks + b * Q8K_BYTES; 150 float d; 151 memcpy(&d, blk, 4); 152 const int8_t* qs = (const int8_t*) (blk + 4); 153 float* out = x + b * QK_K; 154 for (int i = 0; i < QK_K; ++i) out[i] = (float) qs[i] * d; 155} 156 157// ===================== fused silu(gate) * up + quantize ===================== 158// 159// ONE kernel where two ran: `swiglu_kernel` wrote the products and the quantizer read them back. The 160// products land in `gate_out` exactly as the standalone kernels stored them and the blocks are the same 161// bytes the standalone quantizers produced - the quantizer bodies (`quantize_act_dev.cuh`) run verbatim over 162// the shared products. The swilu is PARALLEL (one lane per element): the first version ran 32 exp calls 163// serially per thread and measured SLOWER than the two kernels it replaced (M=8: +22.9%). 164// 165// `SwiluKind` selects the expression because the engine really has three (see `swiglu.cuh`); each is the 166// verbatim expression of the standalone kernel that this replaces. 167 168template <int KIND> 169__device__ __forceinline__ float swilu_apply(float g, float u) { 170 if constexpr (KIND == 0) return swilu_legacy(g, u); 171 else if constexpr (KIND == 1) return swilu_fast(g, u); 172 else return swilu_native(g, u); 173} 174 175// One WARP per 32-element block of pairs: lane j computes pair j (parallel exp), the products stage through 176// shared memory, then the quantizer body runs on them exactly as `quantize_q8_0_kernel` did. 177template <int KIND, bool SCALED> 178__global__ void swilu_quantize_q8_0_kernel(const float* __restrict__ gate, const float* __restrict__ up, 179 float* __restrict__ gate_out, uint8_t* __restrict__ blocks, 180 float* __restrict__ scales, long long n_blocks) { 181 __shared__ float p[4][Q8_0_BLOCK]; // one row per warp (4 warps per block) 182 const int warps_per_block = (int) (blockDim.x >> 5); 183 const long long b = (long long) blockIdx.x * warps_per_block + (threadIdx.x >> 5); 184 const int lane = threadIdx.x & 31; 185 const int warp = threadIdx.x >> 5; 186 if (b >= n_blocks) return; 187 const long long i0 = b * Q8_0_BLOCK; 188 const float v = swilu_apply<KIND>(gate[i0 + lane], up[i0 + lane]); 189 gate_out[i0 + lane] = v; 190 p[warp][lane] = v; 191 __syncwarp(); 192 if (lane == 0) { 193 if constexpr (SCALED) quantize_q8_0_scaled_block(p[warp], blocks + b * 34, scales + b); 194 else quantize_q8_0_block(p[warp], blocks + b * 34); 195 } 196} 197 198// The same for the Q8_K contract (the shared expert's down projection): one warp per 256-element block, 199// lane j taking pairs j, j+32, ... so the 256 exp calls spread over the warp. 200template <int KIND> 201__global__ void swilu_quantize_q8_K_kernel(const float* __restrict__ gate, const float* __restrict__ up, 202 float* __restrict__ gate_out, uint8_t* __restrict__ blocks, 203 long long n_blocks) { 204 __shared__ float p[2][QK_K]; // one row per warp (2 warps per block) 205 const int warps_per_block = (int) (blockDim.x >> 5); 206 const long long b = (long long) blockIdx.x * warps_per_block + (threadIdx.x >> 5); 207 const int lane = threadIdx.x & 31; 208 const int warp = threadIdx.x >> 5; 209 if (b >= n_blocks) return; 210 const long long i0 = b * QK_K; 211 for (int j = lane; j < QK_K; j += 32) { 212 const float v = swilu_apply<KIND>(gate[i0 + j], up[i0 + j]); 213 gate_out[i0 + j] = v; 214 p[warp][j] = v; 215 } 216 __syncwarp(); 217 if (lane == 0) quantize_q8_K_block(p[warp], blocks + b * Q8K_BYTES); 218} 219 220template <int KIND> 221void swilu_quantize_q8_0_launch(const float* gate, const float* up, float* gate_out, int64_t n, uint8_t* blocks, 222 float* scales, void* stream) { 223 const long long nb = n / QK8_0; 224 const int threads = 128; // 4 warps = 4 blocks of 32 pairs 225 const unsigned grid = (unsigned) ((nb + 3) / 4); 226 if (scales != nullptr) 227 swilu_quantize_q8_0_kernel<KIND, true><<<grid, threads, 0, (cudaStream_t) stream>>>( 228 gate, up, gate_out, blocks, scales, nb); 229 else 230 swilu_quantize_q8_0_kernel<KIND, false><<<grid, threads, 0, (cudaStream_t) stream>>>( 231 gate, up, gate_out, blocks, nullptr, nb); 232 const cudaError_t e = cudaGetLastError(); 233 if (e != cudaSuccess) { 234 std::fprintf(stderr, "swilu_quantize_q8_0 launch: %s\n", cudaGetErrorString(e)); 235 std::exit(1); 236 } 237 if (stream == nullptr) cudaDeviceSynchronize(); 238} 239 240// ===================== every activation image of x in ONE pass ===================== 241// 242// The layer start quantized `x` with two or three kernels (Q8_0, Q8_K, bf16) - three reads of the same 243// buffer and three launches, per layer. One kernel here produces whichever images the caller asks for; 244// every image's bytes are what its standalone kernel produced (the per-block math is shared, and each block 245// reads the same `x` values). Null pointers skip that image. 246__global__ void quantize_images_kernel(const float* __restrict__ x, uint8_t* __restrict__ b80, 247 uint8_t* __restrict__ bK, uint16_t* __restrict__ b16, long long n) { 248 const long long i = (long long) blockIdx.x * blockDim.x + threadIdx.x; 249 const long long nb80 = n / Q8_0_BLOCK; // 32-element blocks 250 if (i < nb80) { 251 if (b80 != nullptr) quantize_q8_0_block(x + i * Q8_0_BLOCK, b80 + i * Q8_0_BLOCK_BYTES); 252 if (b16 != nullptr) 253 for (int j = 0; j < Q8_0_BLOCK; ++j) b16[i * Q8_0_BLOCK + j] = bf16_from_f32(x[i * Q8_0_BLOCK + j]); 254 } 255 // q8_K blocks are 256 wide: the thread whose 32-block starts a super-block computes it (256 values in 256 // one thread, exactly as `quantize_q8_K_kernel` did). 257 if (bK != nullptr && (i % (Q8_K_BLOCK / Q8_0_BLOCK)) == 0) { 258 const long long k = i / (Q8_K_BLOCK / Q8_0_BLOCK); 259 if (i < nb80 && k < n / Q8_K_BLOCK) quantize_q8_K_block(x + k * Q8_K_BLOCK, bK + k * Q8_K_BLOCK_BYTES); 260 } 261} 262 263} // namespace 264 265void swilu_quantize_q8_0(const float* gate, const float* up, float* gate_out, int64_t n, int swilu_kind, 266 uint8_t* blocks, void* stream) { 267 if (n <= 0) return; 268 if (n % QK8_0 != 0) { 269 std::fprintf(stderr, "swilu_quantize_q8_0: n %lld is not a multiple of %d\n", (long long) n, QK8_0); 270 std::exit(1); 271 } 272 switch (swilu_kind) { 273 case 0: swilu_quantize_q8_0_launch<0>(gate, up, gate_out, n, blocks, nullptr, stream); break; 274 case 1: swilu_quantize_q8_0_launch<1>(gate, up, gate_out, n, blocks, nullptr, stream); break; 275 default: swilu_quantize_q8_0_launch<2>(gate, up, gate_out, n, blocks, nullptr, stream); break; 276 } 277} 278 279void swilu_quantize_q8_0_scaled(const float* gate, const float* up, float* gate_out, int64_t n, int swilu_kind, 280 uint8_t* blocks, float* scales, void* stream) { 281 if (n <= 0) return; 282 if (n % QK8_0 != 0) { 283 std::fprintf(stderr, "swilu_quantize_q8_0_scaled: n %lld is not a multiple of %d\n", (long long) n, QK8_0); 284 std::exit(1); 285 } 286 if (scales == nullptr) { 287 std::fprintf(stderr, "swilu_quantize_q8_0_scaled: scales is null\n"); 288 std::exit(1); 289 } 290 switch (swilu_kind) { 291 case 0: swilu_quantize_q8_0_launch<0>(gate, up, gate_out, n, blocks, scales, stream); break; 292 case 1: swilu_quantize_q8_0_launch<1>(gate, up, gate_out, n, blocks, scales, stream); break; 293 default: swilu_quantize_q8_0_launch<2>(gate, up, gate_out, n, blocks, scales, stream); break; 294 } 295} 296 297void swilu_quantize_q8_K(const float* gate, const float* up, float* gate_out, int64_t n, int swilu_kind, 298 uint8_t* blocks, void* stream) { 299 if (n <= 0) return; 300 if (n % QK_K != 0) { 301 std::fprintf(stderr, "swilu_quantize_q8_K: n %lld is not a multiple of %d\n", (long long) n, QK_K); 302 std::exit(1); 303 } 304 const long long nb = n / QK_K; 305 const int threads = 64; // 2 warps = 2 blocks of 256 pairs 306 const unsigned grid = (unsigned) ((nb + 1) / 2); 307 switch (swilu_kind) { 308 case 0: swilu_quantize_q8_K_kernel<0><<<grid, threads, 0, (cudaStream_t) stream>>>(gate, up, gate_out, blocks, nb); break; 309 case 1: swilu_quantize_q8_K_kernel<1><<<grid, threads, 0, (cudaStream_t) stream>>>(gate, up, gate_out, blocks, nb); break; 310 default: swilu_quantize_q8_K_kernel<2><<<grid, threads, 0, (cudaStream_t) stream>>>(gate, up, gate_out, blocks, nb); break; 311 } 312 const cudaError_t e = cudaGetLastError(); 313 if (e != cudaSuccess) { 314 std::fprintf(stderr, "swilu_quantize_q8_K launch: %s\n", cudaGetErrorString(e)); 315 std::exit(1); 316 } 317 if (stream == nullptr) cudaDeviceSynchronize(); 318} 319 320void quantize_q8_0(const float* x, uint8_t* blocks, int64_t n, void* stream) { 321 if (n <= 0) return; 322 if (n % QK8_0 != 0) { 323 std::fprintf(stderr, "quantize_q8_0: n %lld is not a multiple of %d\n", (long long) n, QK8_0); 324 std::exit(1); 325 } 326 const long long nb = n / QK8_0; 327 const int threads = 128; 328 const unsigned grid = (unsigned) ((nb + threads - 1) / threads); 329 quantize_q8_0_kernel<<<grid, threads, 0, (cudaStream_t) stream>>>(x, blocks, nb); 330 const cudaError_t e = cudaGetLastError(); 331 if (e != cudaSuccess) { 332 std::fprintf(stderr, "quantize_q8_0 launch: %s\n", cudaGetErrorString(e)); 333 std::exit(1); 334 } 335 if (stream == nullptr) cudaDeviceSynchronize(); 336} 337 338/// See `quantize_q8_0_scaled_kernel`. Writes the same 34-byte `block_q8_0` layout as `quantize_q8_0`, plus 339/// `scales[n/32]` carrying the fp32 `s` the CPU path used - so a hit can be computed with the CPU's 340/// multiplier instead of the block's fp16 `d`. `scales` must not be null. 341void quantize_q8_0_scaled(const float* x, uint8_t* blocks, float* scales, int64_t n, void* stream) { 342 if (n <= 0) return; 343 if (n % QK8_0 != 0) { 344 std::fprintf(stderr, "quantize_q8_0_scaled: n %lld is not a multiple of %d\n", (long long) n, QK8_0); 345 std::exit(1); 346 } 347 if (scales == nullptr) { 348 std::fprintf(stderr, "quantize_q8_0_scaled: scales is null\n"); 349 std::exit(1); 350 } 351 const long long nb = n / QK8_0; 352 const int threads = 128; 353 static const bool old_q80 = [] { 354 const char* e = std::getenv("STRATA_OLD_Q80_SCALED"); 355 return e != nullptr && e[0] == '1'; 356 }(); 357 if (old_q80) { 358 const unsigned grid = (unsigned) ((nb + threads - 1) / threads); 359 quantize_q8_0_scaled_thread_kernel<<<grid, threads, 0, (cudaStream_t) stream>>>(x, blocks, scales, nb); 360 } else { 361 const unsigned grid = (unsigned) ((nb + threads / 32 - 1) / (threads / 32)); // a warp a block 362 quantize_q8_0_scaled_kernel<<<grid, threads, 0, (cudaStream_t) stream>>>(x, blocks, scales, nb); 363 } 364 const cudaError_t e = cudaGetLastError(); 365 if (e != cudaSuccess) { 366 std::fprintf(stderr, "quantize_q8_0_scaled launch: %s\n", cudaGetErrorString(e)); 367 std::exit(1); 368 } 369 if (stream == nullptr) cudaDeviceSynchronize(); 370} 371 372void dequant_q8_0(const uint8_t* blocks, float* x, int64_t n, void* stream) { 373 if (n <= 0) return; 374 const long long nb = n / QK8_0; 375 const int threads = 128; 376 const unsigned grid = (unsigned) ((nb + threads - 1) / threads); 377 dequant_q8_0_kernel<<<grid, threads, 0, (cudaStream_t) stream>>>(blocks, x, nb); 378 const cudaError_t e = cudaGetLastError(); 379 if (e != cudaSuccess) { 380 std::fprintf(stderr, "dequant_q8_0 launch: %s\n", cudaGetErrorString(e)); 381 std::exit(1); 382 } 383 if (stream == nullptr) cudaDeviceSynchronize(); 384} 385 386void quantize_q8_K(const float* x, uint8_t* blocks, int64_t n, void* stream) { 387 if (n <= 0) return; 388 if (n % QK_K != 0) { 389 std::fprintf(stderr, "quantize_q8_K: n %lld is not a multiple of %d\n", (long long) n, QK_K); 390 std::exit(1); 391 } 392 const long long nb = n / QK_K; 393 const int threads = 64; // one block per thread, and a block is 256 elements 394 const unsigned grid = (unsigned) ((nb + threads - 1) / threads); 395 quantize_q8_K_kernel<<<grid, threads, 0, (cudaStream_t) stream>>>(x, blocks, nb); 396 const cudaError_t e = cudaGetLastError(); 397 if (e != cudaSuccess) { 398 std::fprintf(stderr, "quantize_q8_K launch: %s\n", cudaGetErrorString(e)); 399 std::exit(1); 400 } 401 if (stream == nullptr) cudaDeviceSynchronize(); 402} 403 404void dequant_q8_K(const uint8_t* blocks, float* x, int64_t n, void* stream) { 405 if (n <= 0) return; 406 const long long nb = n / QK_K; 407 const int threads = 64; 408 const unsigned grid = (unsigned) ((nb + threads - 1) / threads); 409 dequant_q8_K_kernel<<<grid, threads, 0, (cudaStream_t) stream>>>(blocks, x, nb); 410 const cudaError_t e = cudaGetLastError(); 411 if (e != cudaSuccess) { 412 std::fprintf(stderr, "dequant_q8_K launch: %s\n", cudaGetErrorString(e)); 413 std::exit(1); 414 } 415 if (stream == nullptr) cudaDeviceSynchronize(); 416} 417 418void quantize_act_images(const float* x, int64_t n, uint8_t* q8_0_blocks, uint8_t* q8_K_blocks, 419 uint16_t* bf16, void* stream) { 420 if (n <= 0) return; 421 if (q8_0_blocks == nullptr && q8_K_blocks == nullptr && bf16 == nullptr) return; 422 if (n % Q8_0_BLOCK != 0 || (q8_K_blocks != nullptr && n % QK_K != 0)) { 423 std::fprintf(stderr, "quantize_act_images: n %lld must be a multiple of %d (and %d for Q8_K)\n", 424 (long long) n, Q8_0_BLOCK, QK_K); 425 std::exit(1); 426 } 427 const long long nb = n / Q8_0_BLOCK; 428 const int threads = 128; 429 const unsigned grid = (unsigned) ((nb + threads - 1) / threads); 430 quantize_images_kernel<<<grid, threads, 0, (cudaStream_t) stream>>>(x, q8_0_blocks, q8_K_blocks, bf16, n); 431 const cudaError_t e = cudaGetLastError(); 432 if (e != cudaSuccess) { 433 std::fprintf(stderr, "quantize_act_images launch: %s\n", cudaGetErrorString(e)); 434 std::exit(1); 435 } 436 if (stream == nullptr) cudaDeviceSynchronize(); 437} 438 439} // namespace strata::kernels