Files
Strata/include/strata/quant/dot.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

38 lines
1.6 KiB
C++

// include/strata/quant/dot.hpp - one weight row times one quantized activation row, own-stack phase 2.
//
// The contract (isa.hpp): `dot_kernel(w, a, isa)->fn` returns the bits ggml-cpu's `vec_dot` of flavour `a` returns for the same row pair.
// `dot_ref` is the Generic scalar reference of every format. A kernel is only returned when the CPU can run it.
#pragma once
#include <cstdint>
#include "strata/quant/isa.hpp"
#include "strata/quant/types.hpp"
namespace strata::quant {
/// *s = dot(weight row w of n elements, activation row a of n elements). n is a whole number of the weight type's blocks (and of the
/// activation type's: 32 for the 32-element formats, 256 for Q8_K). The activation format is `TypeInfo::act` of the weight type.
using DotFn = void (*)(int n, float* s, const void* w, const void* a);
struct DotKernel {
DotFn fn = nullptr;
Type weight = Type::F32;
Type act = Type::Q8_0;
Arith arith = Arith::Generic;
Isa isa = Isa::Scalar;
const char* name = "";
};
/// The best kernel of contract `a` for weight type `w` that runs on a CPU of level `max_isa` (the second form: this CPU), or nullptr.
const DotKernel* dot_kernel(Type w, Arith a, Isa max_isa) noexcept;
const DotKernel* dot_kernel(Type w, Arith a) noexcept;
/// Every kernel the layer has for (w, a), best first, whether or not this CPU can run it (tests, benches, the oracle tool).
int dot_kernels(Type w, Arith a, const DotKernel** out, int cap) noexcept;
/// The Generic scalar reference. Aborts for a type without one.
void dot_ref(Type w, int n, float* s, const void* wrow, const void* act);
} // namespace strata::quant