Files
Niko1221 ce52657a48 integrate P-3: artifact/dequant.hpp from the clean-room header
The old header is replaced (guard renamed), the transition comparison test and the one-off build scripts that compared old and new are removed.
2026-12-01 02:19:19 +01:00

278 lines
11 KiB
C++

// Scalar float32 reference dequantisers for the GGUF block formats Strata reads.
//
// Written from the block-format tables of phaseA-tests/hy4gguf/FORMAT-SPEC.md (sections 2-5, 9, 12-16) only.
// Every function takes one block and writes that block's elements in element order. Rounding rule: each
// operator rounds to float32 on its own, left to right as written in the formulas below, and no product may
// be fused into a following add or subtract (keep the translation unit's -ffp-contract setting as it is;
// the clang pragma below only makes that explicit where the compiler honours it).
//
#ifndef STRATA_ARTIFACT_DEQUANT_HPP
#define STRATA_ARTIFACT_DEQUANT_HPP
#ifndef STRATA_GGUF_MAIN_DISABLED
#define STRATA_GGUF_MAIN_DISABLED
#endif
#include "strata/artifact/gguf_reader.hpp"
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#if defined(__clang__)
#pragma STDC FP_CONTRACT OFF
#endif
namespace strata {
// ---- scalar conversions ---------------------------------------------------------------------------------
// little-endian 16-bit word at p (no alignment assumed)
inline uint16_t read_u16(const uint8_t* p) { return (uint16_t)(p[0] | (p[1] << 8)); }
namespace dq_detail {
inline uint32_t read_u32(const uint8_t* p) {
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}
inline float bits_to_float(uint32_t u) {
float f;
std::memcpy(&f, &u, sizeof f);
return f;
}
} // namespace dq_detail
// IEEE binary16 -> binary32. Exact for every finite half; subnormal halves stay subnormal-derived values
// (never flushed), infinities map to infinities, a NaN keeps its payload shifted left by 13.
inline float fp16_to_fp32(uint16_t h) {
const uint32_t sign = (uint32_t)(h & 0x8000u) << 16;
const uint32_t exp5 = (h >> 10) & 0x1Fu;
const uint32_t man = h & 0x3FFu;
if (exp5 == 0x1Fu) return dq_detail::bits_to_float(sign | 0x7F800000u | (man << 13));
if (exp5 == 0) {
// a subnormal half is man * 2^-24: man (< 1024) is exact in float32 and a power-of-two scale is exact
const float mag = (float)man * dq_detail::bits_to_float(0x33800000u); // 2^-24
return sign ? -mag : mag;
}
return dq_detail::bits_to_float(sign | ((exp5 + 112u) << 23) | (man << 13));
}
// bfloat16 -> binary32: the 16 bits become the high half of the float.
inline float bf16_to_fp32(uint16_t b) { return dq_detail::bits_to_float((uint32_t)b << 16); }
// ---- tables ---------------------------------------------------------------------------------------------
// the 4-bit non-linear codebook of IQ4_NL / IQ4_XS (FORMAT-SPEC section 14)
static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
// The 6-bit scale d and 6-bit minimum m of sub-block j (0..7) of a Q4_K / Q5_K block; q = the block's
// scales[12] bytes (FORMAT-SPEC section 3).
static inline void get_scale_min_k4(int j, const uint8_t* q, uint8_t& d, uint8_t& m) {
if (j < 4) {
d = q[j] & 63;
m = q[j + 4] & 63;
} else {
d = (uint8_t)((q[j + 4] & 15) | ((q[j - 4] >> 6) << 4));
m = (uint8_t)((q[j + 4] >> 4) | ((q[j] >> 6) << 4));
}
}
// ---- 32 / 64-element formats (FORMAT-SPEC sections 2 and 12) ---------------------------------------------
// Q2_0: 64 elements, 18 bytes: d (half), qs[16] with four 2-bit codes per byte, first code in the low bits.
// A code c stands for c - 1.
inline void dequantize_q2_0(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
for (int j = 0; j < 64; ++j) {
const int c = (b[2 + (j >> 2)] >> (2 * (j & 3))) & 3;
out[j] = (float)(c - 1) * d;
}
}
// Q8_0: 32 elements, 34 bytes: d (half), qs[32] int8.
inline void dequantize_q8_0(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
for (int j = 0; j < 32; ++j) out[j] = (float)(int8_t)b[2 + j] * d;
}
// Q4_0: 32 elements, 18 bytes: d, qs[16]; low nibble of qs[j] is element j, high nibble element j + 16.
inline void dequantize_q4_0(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
for (int j = 0; j < 16; ++j) {
out[j] = (float)((b[2 + j] & 15) - 8) * d;
out[j + 16] = (float)((b[2 + j] >> 4) - 8) * d;
}
}
// Q5_0: 32 elements, 22 bytes: d, qh[4] (32 fifth bits, bit i for element i), qs[16].
inline void dequantize_q5_0(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
const uint32_t qh = dq_detail::read_u32(b + 2);
for (int j = 0; j < 16; ++j) {
const int lo = (b[6 + j] & 15) | (int)(((qh >> j) & 1u) << 4);
const int hi = (b[6 + j] >> 4) | (int)(((qh >> (j + 16)) & 1u) << 4);
out[j] = (float)(lo - 16) * d;
out[j + 16] = (float)(hi - 16) * d;
}
}
// Q5_1: 32 elements, 24 bytes: d, m (halves), qh[4], qs[16]. y = q5 * d + m (product first, then the sum:
// the two roundings must stay separate).
inline void dequantize_q5_1(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
const float m = fp16_to_fp32(read_u16(b + 2));
const uint32_t qh = dq_detail::read_u32(b + 4);
for (int j = 0; j < 16; ++j) {
const int lo = (b[8 + j] & 15) | (int)(((qh >> j) & 1u) << 4);
const int hi = (b[8 + j] >> 4) | (int)(((qh >> (j + 16)) & 1u) << 4);
const float pl = (float)lo * d;
const float ph = (float)hi * d;
out[j] = pl + m;
out[j + 16] = ph + m;
}
}
// IQ4_NL: 32 elements, 18 bytes: d, qs[16]; y = d * kvalues_iq4nl[nibble].
inline void dequantize_iq4_nl(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
for (int j = 0; j < 16; ++j) {
out[j] = d * (float)kvalues_iq4nl[b[2 + j] & 15];
out[j + 16] = d * (float)kvalues_iq4nl[b[2 + j] >> 4];
}
}
// ---- 256-element K-quants (FORMAT-SPEC sections 3, 4, 9, 13, 14) -----------------------------------------
// Q6_K: 210 bytes: ql[128], qh[64], scales[16] int8, d (half). y = (d * scale) * q, q = (low4 + 16*high2) - 32.
inline void dequantize_q6_K(const uint8_t* b, float* out) {
const uint8_t* ql = b;
const uint8_t* qh = b + 128;
const int8_t* sc = (const int8_t*)(b + 192);
const float d = fp16_to_fp32(read_u16(b + 208));
for (int n = 0; n < 2; ++n) {
for (int l = 0; l < 32; ++l) {
const uint8_t h = qh[32 * n + l];
const uint8_t a = ql[64 * n + l];
const uint8_t c = ql[64 * n + 32 + l];
const int q0 = ((a & 15) | (((h >> 0) & 3) << 4)) - 32;
const int q1 = ((c & 15) | (((h >> 2) & 3) << 4)) - 32;
const int q2 = ((a >> 4) | (((h >> 4) & 3) << 4)) - 32;
const int q3 = ((c >> 4) | (((h >> 6) & 3) << 4)) - 32;
const int s = 8 * n + (l >> 4);
float* o = out + 128 * n + l;
o[0] = (d * (float)sc[s + 0]) * (float)q0;
o[32] = (d * (float)sc[s + 2]) * (float)q1;
o[64] = (d * (float)sc[s + 4]) * (float)q2;
o[96] = (d * (float)sc[s + 6]) * (float)q3;
}
}
}
// Q4_K: 144 bytes: d, dmin, scales[12], qs[128]. y = ((d * sc) * q) - (dmin * m).
inline void dequantize_q4_K(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
const float dmin = fp16_to_fp32(read_u16(b + 2));
const uint8_t* scales = b + 4;
const uint8_t* qs = b + 16;
for (int g = 0; g < 4; ++g) {
uint8_t sc0, m0, sc1, m1;
get_scale_min_k4(2 * g, scales, sc0, m0);
get_scale_min_k4(2 * g + 1, scales, sc1, m1);
const float d0 = d * (float)sc0, n0 = dmin * (float)m0;
const float d1 = d * (float)sc1, n1 = dmin * (float)m1;
for (int l = 0; l < 32; ++l) {
const uint8_t byte = qs[32 * g + l];
const float lo = d0 * (float)(byte & 15);
const float hi = d1 * (float)(byte >> 4);
out[64 * g + l] = lo - n0;
out[64 * g + 32 + l] = hi - n1;
}
}
}
// Q5_K: 176 bytes: d, dmin, scales[12], qh[32], qs[128]. q = nibble + 16 * (bit j of qh[l]).
inline void dequantize_q5_K(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
const float dmin = fp16_to_fp32(read_u16(b + 2));
const uint8_t* scales = b + 4;
const uint8_t* qh = b + 16;
const uint8_t* qs = b + 48;
for (int g = 0; g < 4; ++g) {
uint8_t sc0, m0, sc1, m1;
get_scale_min_k4(2 * g, scales, sc0, m0);
get_scale_min_k4(2 * g + 1, scales, sc1, m1);
const float d0 = d * (float)sc0, n0 = dmin * (float)m0;
const float d1 = d * (float)sc1, n1 = dmin * (float)m1;
for (int l = 0; l < 32; ++l) {
const uint8_t byte = qs[32 * g + l];
const int q0 = (byte & 15) | (((qh[l] >> (2 * g)) & 1) << 4);
const int q1 = (byte >> 4) | (((qh[l] >> (2 * g + 1)) & 1) << 4);
const float lo = d0 * (float)q0;
const float hi = d1 * (float)q1;
out[64 * g + l] = lo - n0;
out[64 * g + 32 + l] = hi - n1;
}
}
}
// Q3_K: 110 bytes: hmask[32], qs[64], scales[12], d (half, may be negative).
// y = (d * (u_t - 32)) * q, q = lo if the element's hmask bit is set else lo - 4.
inline void dequantize_q3_K(const uint8_t* b, float* out) {
const uint8_t* hmask = b;
const uint8_t* qs = b + 32;
const uint8_t* sc = b + 96;
const float d = fp16_to_fp32(read_u16(b + 108));
int u[16];
for (int t = 0; t < 16; ++t) {
const int low4 = (t < 8) ? (sc[t] & 15) : (sc[t - 8] >> 4);
const int high2 = (sc[8 + (t & 3)] >> (2 * (t >> 2))) & 3;
u[t] = low4 + 16 * high2;
}
for (int h = 0; h < 2; ++h) {
for (int s = 0; s < 4; ++s) {
for (int l = 0; l < 32; ++l) {
const int e = 128 * h + 32 * s + l;
const int lo = (qs[32 * h + l] >> (2 * s)) & 3;
const int hb = (hmask[l] >> (4 * h + s)) & 1;
const int q = hb ? lo : lo - 4;
const float dl = d * (float)(u[e >> 4] - 32);
out[e] = dl * (float)q;
}
}
}
}
// IQ4_XS: 136 bytes: d, scales_h (u16), scales_l[4], qs[128]. y = (d * (ls - 32)) * kvalue.
inline void dequantize_iq4_xs(const uint8_t* b, float* out) {
const float d = fp16_to_fp32(read_u16(b));
const uint16_t sh = read_u16(b + 2);
const uint8_t* sl = b + 4;
const uint8_t* qs = b + 8;
for (int ib = 0; ib < 8; ++ib) {
const int ls = ((sl[ib >> 1] >> (4 * (ib & 1))) & 15) | (((sh >> (2 * ib)) & 3) << 4);
const float dl = d * (float)(ls - 32);
for (int j = 0; j < 16; ++j) {
const uint8_t q = qs[16 * ib + j];
out[32 * ib + j] = dl * (float)kvalues_iq4nl[q & 15];
out[32 * ib + 16 + j] = dl * (float)kvalues_iq4nl[q >> 4];
}
}
}
// ---- whole-array copies / conversions ---------------------------------------------------------------------
inline void dequantize_f32(const uint8_t* p, float* out, int n) { std::memcpy(out, p, (size_t)n * sizeof(float)); }
inline void dequantize_f16(const uint8_t* p, float* out, int n) {
for (int i = 0; i < n; ++i) out[i] = fp16_to_fp32(read_u16(p + 2 * (size_t)i));
}
inline void dequantize_bf16(const uint8_t* p, float* out, int n) {
for (int i = 0; i < n; ++i) out[i] = bf16_to_fp32(read_u16(p + 2 * (size_t)i));
}
} // namespace strata
#endif