okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5b adfcf9028 into f-own-int (round 16: the strata-image test program and its parts, DiT runner, sampler, noise, PNG writer; hooked into no build)e4309aaf3

main
9.5 KiB199 linesraw
1// src/image/te/te_dequant.cu - GGUF block formats -> f16 rows on CUDA / HIP (include/strata/image/te_dequant.hpp).
2//
3// One thread decodes 8 consecutive values (a quarter of a 32-value group; a K-quant / IQ4_XS super-block has 8 groups,
4// a Q8_0 block is one) and writes them with one 16-byte store, so a warp writes 512 contiguous bytes.  Every multiply and subtract is __fmul_rn / __fsub_rn: the compilers would
5// otherwise fuse "d1 * q - m1" into one FMA, which rounds once instead of twice and gives other f16 bits than ggml's
6// (and dequant.hpp's) f32 values.  The f16 rounding is f16_bits.hpp's (round to nearest even), not __float2half.
7#include "strata/image/te_dequant.hpp"
8
9#include "strata/kernels/bf16_bits.hpp"
10#include "strata/kernels/f16_bits.hpp"
11
12#if defined(__HIP__) || defined(__HIPCC__) || defined(__HIP_PLATFORM_AMD__)
13#include "strata/hip_compat/cuda_runtime.h"
14#else
15#include <cuda_runtime.h>
16#endif
17
18namespace strata::image {
19namespace {
20
21using strata::kernels::f16_from_f32;
22
23__constant__ int8_t kIq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
24
25__device__ __forceinline__ float h2f_at(const uint8_t* p) {
26    return strata::kernels::f32_from_f16((uint16_t) (p[0] | (p[1] << 8)));
27}
28
29// get_scale_min_k4 (ggml-quants.c), j = 0..7
30__device__ __forceinline__ void scale_min_k4(int j, const uint8_t* q, int& sc, int& m) {
31    if (j < 4) {
32        sc = q[j] & 63;
33        m = q[j + 4] & 63;
34    } else {
35        sc = (q[j + 4] & 0x0F) | ((q[j - 4] >> 6) << 4);
36        m = (q[j + 4] >> 4) | ((q[j] >> 6) << 4);
37    }
38}
39
40__device__ __forceinline__ void put8(uint16_t* out, const float* v) {     // 16-byte store: consecutive threads, consecutive 16 B
41    uint4 o;
42    o.x = (uint32_t) f16_from_f32(v[0]) | ((uint32_t) f16_from_f32(v[1]) << 16);
43    o.y = (uint32_t) f16_from_f32(v[2]) | ((uint32_t) f16_from_f32(v[3]) << 16);
44    o.z = (uint32_t) f16_from_f32(v[4]) | ((uint32_t) f16_from_f32(v[5]) << 16);
45    o.w = (uint32_t) f16_from_f32(v[6]) | ((uint32_t) f16_from_f32(v[7]) << 16);
46    *reinterpret_cast<uint4*>(out) = o;
47}
48
49template <uint32_t TYPE>
50__device__ __forceinline__ void decode8(const uint8_t* row, int g, int l0, float* v) {   // values l0 .. l0+7 of group g
51    if constexpr (TYPE == TE_Q4_K) {                    // d dmin scales[12] qs[128]; group g: 32 values
52        const uint8_t* b = row + (size_t) (g >> 3) * 144;
53        const int sub = g & 7, j64 = sub >> 1, hi = sub & 1;
54        int sc, m;
55        scale_min_k4(sub, b + 4, sc, m);
56        const float d1 = __fmul_rn(h2f_at(b), (float) sc), m1 = __fmul_rn(h2f_at(b + 2), (float) m);
57        const uint8_t* q = b + 16 + 32 * j64;
58#pragma unroll
59        for (int i = 0; i < 8; ++i) { const int l = l0 + i; v[i] = __fsub_rn(__fmul_rn(d1, (float) (hi ? (q[l] >> 4) : (q[l] & 0x0F))), m1); }
60    } else if constexpr (TYPE == TE_Q5_K) {             // d dmin scales[12] qh[32] qs[128]
61        const uint8_t* b = row + (size_t) (g >> 3) * 176;
62        const int sub = g & 7, j64 = sub >> 1, hi = sub & 1;
63        int sc, m;
64        scale_min_k4(sub, b + 4, sc, m);
65        const float d1 = __fmul_rn(h2f_at(b), (float) sc), m1 = __fmul_rn(h2f_at(b + 2), (float) m);
66        const uint8_t* qh = b + 16;
67        const uint8_t* ql = b + 48 + 32 * j64;
68        const int u = 1 << sub;                         // u1 = 1, 4, 16, 64 and u2 = 2, 8, 32, 128
69#pragma unroll
70        for (int i = 0; i < 8; ++i) {
71            const int l = l0 + i;
72            const int q = (hi ? (ql[l] >> 4) : (ql[l] & 0x0F)) + ((qh[l] & u) ? 16 : 0);
73            v[i] = __fsub_rn(__fmul_rn(d1, (float) q), m1);
74        }
75    } else if constexpr (TYPE == TE_Q6_K) {             // ql[128] qh[64] scales[16] (int8) d
76        const uint8_t* b = row + (size_t) (g >> 3) * 210;
77        const int sub = g & 7, n = sub >> 2, k = sub & 3;   // values 128 n + 32 k + l
78        const uint8_t* ql = b + 64 * n;
79        const uint8_t* qh = b + 128 + 32 * n;
80        const int8_t* sc = reinterpret_cast<const int8_t*>(b + 192) + 8 * n;
81        const float d = h2f_at(b + 208);
82        const uint8_t* L = ql + ((k & 1) ? 32 : 0);
83        const int shift_l = (k & 2) ? 4 : 0, shift_h = 2 * k;
84#pragma unroll
85        for (int i = 0; i < 8; ++i) {
86            const int l = l0 + i;
87            const int q = (int) (((L[l] >> shift_l) & 0x0F) | (((qh[l] >> shift_h) & 3) << 4)) - 32;
88            v[i] = __fmul_rn(__fmul_rn(d, (float) sc[(l >> 4) + 2 * k]), (float) q);
89        }
90    } else if constexpr (TYPE == TE_IQ4_XS) {           // d scales_h scales_l[4] qs[128]
91        const uint8_t* b = row + (size_t) (g >> 3) * 136;
92        const int ib = g & 7;
93        const int sh = b[2] | (b[3] << 8);
94        const int ls = ((b[4 + ib / 2] >> (4 * (ib % 2))) & 0x0F) | (((sh >> (2 * ib)) & 3) << 4);
95        const float dl = __fmul_rn(h2f_at(b), (float) (ls - 32));
96        const uint8_t* qs = b + 8 + 16 * ib;
97#pragma unroll
98        for (int i = 0; i < 8; ++i) {                 // values 0-15: low nibbles of qs[0..15], 16-31: high nibbles
99            const int l = l0 + i;
100            v[i] = __fmul_rn(dl, (float) kIq4nl[l < 16 ? (qs[l] & 0x0F) : (qs[l - 16] >> 4)]);
101        }
102    } else if constexpr (TYPE == TE_Q8_0) {             // d qs[32]
103        const uint8_t* b = row + (size_t) g * 34;
104        const float d = h2f_at(b);
105        const int8_t* q = reinterpret_cast<const int8_t*>(b + 2);
106#pragma unroll
107        for (int i = 0; i < 8; ++i) v[i] = __fmul_rn((float) q[l0 + i], d);
108    } else if constexpr (TYPE == TE_F32) {
109        const float* p = reinterpret_cast<const float*>(row) + 32 * (size_t) g;
110#pragma unroll
111        for (int i = 0; i < 8; ++i) v[i] = p[l0 + i];
112    } else if constexpr (TYPE == TE_BF16) {
113        const uint8_t* p = row + 64 * (size_t) g;
114#pragma unroll
115        for (int i = 0; i < 8; ++i) v[i] = strata::kernels::f32_from_bf16((uint16_t) (p[2 * (l0 + i)] | (p[2 * (l0 + i) + 1] << 8)));
116    }
117}
118
119template <uint32_t TYPE>
120__global__ void dequant_kernel(const uint8_t* __restrict__ src, size_t row_bytes, int64_t ne0, int64_t rows,
121                               uint16_t* __restrict__ dst) {
122    const int64_t ppr = ne0 / 8;                          // pieces of 8 values per row
123    const int64_t total = ppr * rows;
124    for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < total; i += (int64_t) gridDim.x * blockDim.x) {
125        const int64_t r = i / ppr, pc = i % ppr;
126        float v[8];
127        decode8<TYPE>(src + (size_t) r * row_bytes, (int) (pc >> 2), (int) (pc & 3) * 8, v);
128        put8(dst + r * ne0 + pc * 8, v);
129    }
130}
131
132__global__ void copy_f16_kernel(const uint16_t* __restrict__ src, size_t n, uint16_t* __restrict__ dst) {
133    for (size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t) gridDim.x * blockDim.x) dst[i] = src[i];
134}
135
136template <uint32_t TYPE>
137std::string launch(const void* src, size_t rb, int64_t ne0, int64_t rows, uint16_t* dst, cudaStream_t s) {
138    const int64_t total = ne0 / 8 * rows;
139    const int threads = 256;
140    const int64_t want = (total + threads - 1) / threads;
141    const int blocks = (int) (want < 65535 * 8 ? want : 65535 * 8);
142    dequant_kernel<TYPE><<<blocks, threads, 0, s>>>(static_cast<const uint8_t*>(src), rb, ne0, rows, dst);
143    const cudaError_t e = cudaGetLastError();
144    return e == cudaSuccess ? std::string() : std::string("te_dequant: launch: ") + cudaGetErrorString(e);
145}
146
147struct Geo { int elems, bytes; };
148bool geo(uint32_t type, Geo& g) {
149    switch (type) {
150    case TE_F32: g = {1, 4}; return true;
151    case TE_F16: g = {1, 2}; return true;
152    case TE_BF16: g = {1, 2}; return true;
153    case TE_Q8_0: g = {32, 34}; return true;
154    case TE_Q4_K: g = {256, 144}; return true;
155    case TE_Q5_K: g = {256, 176}; return true;
156    case TE_Q6_K: g = {256, 210}; return true;
157    case TE_IQ4_XS: g = {256, 136}; return true;
158    default: return false;
159    }
160}
161
162}  // namespace
163
164bool te_type_supported(uint32_t type) {
165    Geo g;
166    return geo(type, g);
167}
168
169size_t te_row_bytes(uint32_t type, int64_t ne0) {
170    Geo g;
171    if (!geo(type, g) || ne0 <= 0 || ne0 % 32 != 0 || ne0 % g.elems != 0) return 0;   // the device path decodes groups of 32
172    return (size_t) (ne0 / g.elems) * (size_t) g.bytes;
173}
174
175std::string te_dequant_f16(uint32_t type, const void* d_src, int64_t ne0, int64_t rows, uint16_t* d_dst, void* stream) {
176    const size_t rb = te_row_bytes(type, ne0);
177    if (rb == 0) return "te_dequant: type " + std::to_string(type) + " with rows of " + std::to_string(ne0) + " values is not supported";
178    if (rows <= 0) return std::string();
179    cudaStream_t s = static_cast<cudaStream_t>(stream);
180    switch (type) {
181    case TE_Q4_K: return launch<TE_Q4_K>(d_src, rb, ne0, rows, d_dst, s);
182    case TE_Q5_K: return launch<TE_Q5_K>(d_src, rb, ne0, rows, d_dst, s);
183    case TE_Q6_K: return launch<TE_Q6_K>(d_src, rb, ne0, rows, d_dst, s);
184    case TE_IQ4_XS: return launch<TE_IQ4_XS>(d_src, rb, ne0, rows, d_dst, s);
185    case TE_Q8_0: return launch<TE_Q8_0>(d_src, rb, ne0, rows, d_dst, s);
186    case TE_F32: return launch<TE_F32>(d_src, rb, ne0, rows, d_dst, s);
187    case TE_BF16: return launch<TE_BF16>(d_src, rb, ne0, rows, d_dst, s);
188    case TE_F16: {
189        const size_t n = (size_t) ne0 * (size_t) rows;
190        copy_f16_kernel<<<(unsigned) ((n + 255) / 256 < 65535 * 8 ? (n + 255) / 256 : 65535 * 8), 256, 0, s>>>(
191            static_cast<const uint16_t*>(d_src), n, d_dst);
192        const cudaError_t e = cudaGetLastError();
193        return e == cudaSuccess ? std::string() : std::string("te_dequant: launch: ") + cudaGetErrorString(e);
194    }
195    default: return "te_dequant: type " + std::to_string(type) + " is not supported";
196    }
197}
198
199}  // namespace strata::image