Files
Strata/include/strata/quant/act.hpp
T
Niko1221 8a2b0f5f00 quant layer: type table, CPU probe, Q8_0/Q8_1/Q8_K/Q4_0 reference quantizers, Generic-contract dots for every pool type, self-checks, ggml oracle test
New static library strata_quant (include/strata/quant/, src/quant/), no ggml dependency: block layouts and codebooks come from the quant header.
- types.hpp: ggml's type ids and block geometry (type_info, row_bytes), activation type per weight type; unknown ids answer nullptr.
- isa.hpp: Isa (hardware) and Arith (arithmetic contract: Generic, Avx, Avx2, Neon), Caps probe with the STRATA_FORCE_ISA cap, default_arith().
- act.hpp / dot.hpp: kernel tables with best-kernel lookup per (type, contract, hardware); references: ggml's _ref quantizers and _generic dots
  ported to C++ (22 dot types: Q2_0, Q4_0/1, Q5_0/1, Q8_0, IQ4_NL, MXFP4, NVFP4, Q2_K..Q6_K, IQ1_S/M, IQ2_XXS/XS/S, IQ3_XXS/S, IQ4_XS).
- selfcheck.hpp: every kernel against the reference on exact synthetic data (powers-of-two scales, small activations), once per process.
- tests/quant/quant_parity.cpp (the only program that links ggml-cpu) compares the layer with ggml-cpu bit for bit; run_oracle.sh builds it in the
  three flavours (portable AVX2, STRATA_ISA_FLOOR=avx, none).  Generic tier vs the none flavour: 13825 comparisons, 0 differences
  (Q4_0 excluded there: ggml's SSE4.2 flavour has an SSSE3 branch that is not the generic C code).
2026-11-09 04:11:27 +01:00

39 lines
1.8 KiB
C++

// include/strata/quant/act.hpp - activation quantizers (float row -> Q8_0 / Q8_1 / Q8_K blocks), own-stack phase 2.
//
// An activation is quantized once per token and layer and every weight row's dot reads it, so the quantizer's rounding is part of the
// arithmetic contract (isa.hpp): ggml's AVX / AVX2 Q8_0 and Q8_1 quantizers round half to even (`_mm256_round_ps(NEAREST)`), its generic
// `_ref` code rounds half away from zero (`roundf`), and the two differ on exact ties. Q8_K is ggml's scalar `quantize_row_q8_K_ref` on
// every x86 flavour (the Arm flavour has its own). Each contract's quantizer below reproduces that flavour's bytes.
#pragma once
#include <cstdint>
#include "strata/quant/isa.hpp"
#include "strata/quant/types.hpp"
namespace strata::quant {
/// x (n floats; n a whole number of blocks: 32 for Q8_0 / Q8_1, 256 for Q8_K) -> blocks y.
using ActQuantFn = void (*)(const float* x, void* y, int64_t n);
struct ActKernel {
ActQuantFn fn = nullptr;
Type act = Type::Q8_0;
Arith arith = Arith::Generic;
Isa isa = Isa::Scalar;
const char* name = "";
};
/// The best quantizer of contract `a` for the activation format `act` that runs on a CPU of level `max_isa` (default: this CPU), or nullptr.
const ActKernel* act_kernel(Type act, Arith a, Isa max_isa) noexcept;
const ActKernel* act_kernel(Type act, Arith a) noexcept;
/// The reference quantizers (the Generic contract: ggml's `_ref` functions).
void quantize_q8_0_ref(const float* x, void* y, int64_t n);
void quantize_q8_1_ref(const float* x, void* y, int64_t n);
void quantize_q8_K_ref(const float* x, void* y, int64_t n);
/// Q4_0 from floats, ggml's `quantize_row_q4_0_ref` (the `--mtp-q4` requantizer's output format).
void quantize_q4_0_ref(const float* x, void* y, int64_t n);
} // namespace strata::quant