// STRA-169 candidate source: native GGUF blocks -> FP16 dequantization of the Strata prompt path. // // Origin: engine/strata/src/kernels/cuda/dequant_bf16.cu (group32, dequant_kernel, the H16 output of // dequant_f16), itself transcribed from ggml/src/ggml-quants.c at the pinned llama.cpp (MIT License, // Copyright (c) 2023-2026 The ggml authors): dequantize_row_q8_0/q4_K/q5_K/q6_K/iq4_nl/iq4_xs. // Only the GGUF types the GDN projections (attn_qkv, attn_gate, ssm_out) can carry are instantiated: // Q8_0 = 8, Q4_K = 12, Q5_K = 13, Q6_K = 14, IQ4_NL = 20, IQ4_XS = 23. // The group32 bodies are verbatim; the host wrapper's geometry/launch selection is reproduced in // gdn_native_batch.rs (one thread per 32-element group, 256 threads per block, rows addressed from `row0`). // The FP16 GEMM that consumes this scratch is the closed cuBLAS adapter (not part of this file). // // Offline only; compiler policy of the original (default nvcc Release, no --use_fast_math): // nvcc -ptx -O3 -arch=sm_120 offline_native_dequant_f16_kernel.cu -o offline_native_dequant_f16.ptx #include #include #include namespace { __device__ __forceinline__ float h2f(const uint8_t* p) { return __half2float(__ushort_as_half((uint16_t) (p[0] | (p[1] << 8)))); } struct H16 { uint16_t v; }; __device__ __forceinline__ void put(H16* o, int i, float v) { o[i].v = __half_as_ushort(__float2half_rn(v)); } __constant__ int8_t kv_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113}; __device__ __forceinline__ void scale_min_k4(int j, const uint8_t* q, int& d, int& m) { if (j < 4) { d = q[j] & 63; m = q[j + 4] & 63; } else { d = (q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4); m = (q[j + 4] >> 4) | ((q[j] >> 6) << 4); } } // One 32-element group `g` (row-major over the whole slice); `out` points at that group's 32 outputs. template __device__ __forceinline__ void group32(const uint8_t* row_blocks, int gi_in_row, T* out) { if constexpr (TYPE == 8) { // Q8_0 const uint8_t* b = row_blocks + (size_t) gi_in_row * 34; const float d = h2f(b); for (int j = 0; j < 32; ++j) put(out, j, (float) (int8_t) b[2 + j] * d); } else if constexpr (TYPE == 20) { // IQ4_NL const uint8_t* b = row_blocks + (size_t) gi_in_row * 18; const float d = h2f(b); for (int j = 0; j < 16; ++j) { put(out, j, d * (float) kv_iq4nl[b[2 + j] & 0xf]); put(out, j + 16, d * (float) kv_iq4nl[b[2 + j] >> 4]); } } else if constexpr (TYPE == 12) { // Q4_K: d dmin scales[12] qs[128] const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 144; const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2; const float d = h2f(b), dmin = h2f(b + 2); int sc, m; scale_min_k4(gi, b + 4, sc, m); const float d1 = d * (float) sc, m1 = dmin * (float) m; const uint8_t* q = b + 16 + 32 * j64; for (int l = 0; l < 32; ++l) put(out, l, d1 * (float) (hi ? (q[l] >> 4) : (q[l] & 0xF)) - m1); } else if constexpr (TYPE == 13) { // Q5_K: d dmin scales[12] qh[32] qs[128] const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 176; const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2; const float d = h2f(b), dmin = h2f(b + 2); int sc, m; scale_min_k4(gi, b + 4, sc, m); const float d1 = d * (float) sc, m1 = dmin * (float) m; const uint8_t* qh = b + 16; const uint8_t* ql = b + 48 + 32 * j64; const uint8_t u = (uint8_t) (1u << (2 * j64 + hi)); for (int l = 0; l < 32; ++l) { const int nib = hi ? (ql[l] >> 4) : (ql[l] & 0xF); put(out, l, d1 * (float) (nib + ((qh[l] & u) ? 16 : 0)) - m1); } } else if constexpr (TYPE == 14) { // Q6_K: ql[128] qh[64] scales[16] d const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 210; const int gi = gi_in_row % 8, n = gi / 4, qu = gi % 4; const uint8_t* ql = b + 64 * n; const uint8_t* qh = b + 128 + 32 * n; const int8_t* sc = reinterpret_cast(b + 192) + 8 * n; const float d = h2f(b + 208); for (int l = 0; l < 32; ++l) { const int is = l / 16; int q; if (qu == 0) q = (ql[l] & 0xF) | (((qh[l] >> 0) & 3) << 4); else if (qu == 1) q = (ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4); else if (qu == 2) q = (ql[l] >> 4) | (((qh[l] >> 4) & 3) << 4); else q = (ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4); put(out, l, d * (float) sc[is + 2 * qu] * (float) (q - 32)); } } else if constexpr (TYPE == 23) { // IQ4_XS: d scales_h scales_l[4] qs[128] const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 136; const int ib = gi_in_row % 8; const float d = h2f(b); const uint16_t scales_h = (uint16_t) (b[2] | (b[3] << 8)); const int ls = ((b[4 + ib / 2] >> (4 * (ib % 2))) & 0xf) | (((scales_h >> (2 * ib)) & 3) << 4); const float dl = d * (float) (ls - 32); const uint8_t* qs = b + 8 + 16 * ib; for (int j = 0; j < 16; ++j) { put(out, j, dl * (float) kv_iq4nl[qs[j] & 0xf]); put(out, j + 16, dl * (float) kv_iq4nl[qs[j] >> 4]); } } } template __device__ __forceinline__ void dequant_body(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t row0, int64_t rows, int64_t groups_per_row, T* __restrict__ out) { const int64_t g = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; if (g >= rows * groups_per_row) return; const int64_t r = g / groups_per_row, gi = g % groups_per_row; group32(blocks + (row0 + r) * row_bytes, (int) gi, out + r * groups_per_row * 32 + gi * 32); } } // namespace #define LELEKA_DEQUANT_F16(NAME, TYPE) \ extern "C" __global__ void NAME(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t row0, \ int64_t rows, int64_t groups_per_row, uint16_t* __restrict__ out) { \ dequant_body(blocks, row_bytes, row0, rows, groups_per_row, reinterpret_cast(out)); \ } LELEKA_DEQUANT_F16(leleka_native_dequant_f16_q8_0, 8) LELEKA_DEQUANT_F16(leleka_native_dequant_f16_q4_k, 12) LELEKA_DEQUANT_F16(leleka_native_dequant_f16_q5_k, 13) LELEKA_DEQUANT_F16(leleka_native_dequant_f16_q6_k, 14) LELEKA_DEQUANT_F16(leleka_native_dequant_f16_iq4_nl, 20) LELEKA_DEQUANT_F16(leleka_native_dequant_f16_iq4_xs, 23)