mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
pool_quant.hpp (include/strata/kernels/cpu/) is the one place that knows both worlds: Tr (a weight type's dot, its activation format, an activation type's quantizer), traits(), row_size_of(), blck_size_of(), flavour_avx2() / flavour_f16c() / flavour_avx512(), to_float(), fp16_bits(). With STRATA_OWN_CPU_QUANT they are the quant layer's (each kernel passes its self-check once before it is handed out; the contract is default_arith(): Avx2 on any CPU with AVX2+FMA+F16C, i.e. what the shipped ggml-cpu gives, AVX-512 CPUs included); without it they are ggml-cpu's type traits, exactly as before. - src/kernels/cpu/native_expert.cpp: the traits calls, ggml_row_size / ggml_blck_size, ggml_cpu_has_*, ggml_cpu_init and the six first-use checks of the own rows now go through pool_quant.hpp (the checks compare with the layer's dot instead of ggml's). Hot loops call Tr::dot (a function pointer). - src/dsv41/ds4_gg_cpu.cpp (the DeepSeek lane, also compiled into the Metal host): the same, plus to_float for the embedding rows. Types 10-14, 19, 20, 23 and every activation quantizer there come from the layer on x86. - strata/quant/fp16.hpp: the scalar fp16 conversions, public (internal.hpp used to hold them). - CMake: option STRATA_OWN_CPU_QUANT; strata_kernels_cpu links strata_quant and defines it PUBLIC. ggml-cpu / ggml-base stay linked for the tests that use them as the oracle; with the option on, the pool calls no ggml-cpu function, so a static link pulls none of its objects (nm check in the gates). - iq_avx1_parity calls ggml_cpu_init itself now (native_fmt used to do it for the oracle's fp16 table). - native_fmt's two error strings name the library: "native experts: <ggml-cpu | the quant layer> has no dot product for type N" (unchanged text in the ggml build). cpu-check-native preset, p2006 (AVX-512 host), own mode: 101 tests, 0 failures (6 skipped by design); boston (AVX2): 101 tests, 0 failures in both the own and the ggml build.
53 lines
2.0 KiB
C++
53 lines
2.0 KiB
C++
// include/strata/quant/fp16.hpp - IEEE half <-> float, scalar and exact (own-stack phase 2).
|
|
//
|
|
// The same values as F16C's VCVTPH2PS / VCVTPS2PH and ggml's table for every finite input (fp16 -> fp32 is exact; fp32 -> fp16 rounds to nearest even; a NaN
|
|
// comes out as 0x7E00). quant_parity checks both over every fp16 value and 4M random floats.
|
|
#pragma once
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
|
|
namespace strata::quant {
|
|
|
|
inline float f32_from_bits(uint32_t w) {
|
|
float f;
|
|
std::memcpy(&f, &w, sizeof f);
|
|
return f;
|
|
}
|
|
inline uint32_t f32_to_bits(float f) {
|
|
uint32_t w;
|
|
std::memcpy(&w, &f, sizeof w);
|
|
return w;
|
|
}
|
|
|
|
inline float fp16_to_fp32(uint16_t h) {
|
|
const uint32_t w = (uint32_t) h << 16;
|
|
const uint32_t sign = w & 0x80000000u;
|
|
const uint32_t two_w = w + w;
|
|
const float exp_scale = f32_from_bits(0x7800000u); // 2^-112
|
|
const float normalized = f32_from_bits((two_w >> 4) + (0xE0u << 23)) * exp_scale;
|
|
const float denormalized = f32_from_bits((two_w >> 17) | (126u << 23)) - 0.5f;
|
|
const uint32_t result = sign | (two_w < (1u << 27) ? f32_to_bits(denormalized) : f32_to_bits(normalized));
|
|
return f32_from_bits(result);
|
|
}
|
|
|
|
inline uint16_t fp32_to_fp16(float f) {
|
|
const float scale_to_inf = f32_from_bits(0x77800000u); // 2^112
|
|
const float scale_to_zero = f32_from_bits(0x08800000u); // 2^-110
|
|
float base = (std::fabs(f) * scale_to_inf) * scale_to_zero;
|
|
const uint32_t w = f32_to_bits(f);
|
|
const uint32_t shl1_w = w + w;
|
|
const uint32_t sign = w & 0x80000000u;
|
|
uint32_t bias = shl1_w & 0xFF000000u;
|
|
if (bias < 0x71000000u) bias = 0x71000000u;
|
|
base = f32_from_bits((bias >> 1) + 0x07800000u) + base;
|
|
const uint32_t bits = f32_to_bits(base);
|
|
const uint32_t exp_bits = (bits >> 13) & 0x00007C00u;
|
|
const uint32_t mantissa_bits = bits & 0x00000FFFu;
|
|
const uint32_t nonsign = exp_bits + mantissa_bits;
|
|
return (uint16_t) ((sign >> 16) | (shl1_w > 0xFF000000u ? 0x7E00u : nonsign));
|
|
}
|
|
|
|
} // namespace strata::quant
|