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.
110 lines
5.2 KiB
C++
110 lines
5.2 KiB
C++
// include/strata/kernels/cpu/pool_quant.hpp - what the CPU expert pool (native_expert.cpp) and the DeepSeek CPU lane (dsv41/ds4_gg_cpu.cpp) ask of a quant type:
|
|
// its dot product, the activation format that dot takes, the activation quantizer, row geometry.
|
|
//
|
|
// With STRATA_OWN_CPU_QUANT (the default build) these come from the Strata quant layer (src/quant): no ggml-cpu function is called by the pool. Without it
|
|
// they are ggml-cpu's type traits, exactly as before (kept selectable for two releases). The two builds expose the same names, so the callers do not care.
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
|
|
#if defined(STRATA_OWN_CPU_QUANT)
|
|
#include "strata/quant/act.hpp"
|
|
#include "strata/quant/dequant.hpp"
|
|
#include "strata/quant/dot.hpp"
|
|
#include "strata/quant/fp16.hpp"
|
|
#include "strata/quant/isa.hpp"
|
|
#include "strata/quant/selfcheck.hpp"
|
|
#include "strata/quant/types.hpp"
|
|
#define STRATA_POOL_LIB "the quant layer"
|
|
#else
|
|
#include "ggml-cpu.h"
|
|
#include "ggml.h"
|
|
#define STRATA_POOL_LIB "ggml-cpu"
|
|
#endif
|
|
|
|
namespace strata::kernels::cpu::pq {
|
|
|
|
// ggml's type ids of the formats the pool names
|
|
constexpr int kTQ5_0 = 6, kTIq4nl = 20, kTQ8_0 = 8, kTQ8_K = 15;
|
|
|
|
#if defined(STRATA_OWN_CPU_QUANT)
|
|
namespace sq = strata::quant;
|
|
/// ggml's ggml_type_traits_cpu in the layer's terms: the weight type's dot of the default arithmetic contract, the activation format that dot takes, and, for an
|
|
/// activation format, its quantizer. Every kernel passes the layer's self-check once before it is handed out.
|
|
struct Tr {
|
|
const sq::DotKernel* k = nullptr;
|
|
sq::ActQuantFn q = nullptr;
|
|
int vec_dot_type = -1;
|
|
bool has_dot() const { return k != nullptr; }
|
|
bool has_from_float() const { return q != nullptr; }
|
|
void dot(int n, float* s, const void* w, const void* a) const { k->fn(n, s, w, a); }
|
|
void from_float(const float* x, void* y, int64_t n) const { q(x, y, n); }
|
|
};
|
|
inline Tr traits(int type) {
|
|
constexpr int kN = 64; // ggml's ids below the trellis types; those are tq_cpu.hpp's and never asked here
|
|
static Tr cache[kN];
|
|
static std::once_flag once[kN];
|
|
if (type < 0 || type >= kN) return Tr{};
|
|
std::call_once(once[type], [type] {
|
|
const sq::TypeInfo* ti = sq::type_info(type);
|
|
if (ti == nullptr) return;
|
|
Tr t;
|
|
if (ti->has_dot) {
|
|
t.k = sq::checked_dot_kernel(ti->type, sq::default_arith());
|
|
t.vec_dot_type = (int) ti->act;
|
|
}
|
|
if (ti->is_act) {
|
|
const sq::ActKernel* a = sq::checked_act_kernel(ti->type, sq::default_arith());
|
|
t.q = a ? a->fn : nullptr;
|
|
}
|
|
cache[type] = t;
|
|
});
|
|
return cache[type];
|
|
}
|
|
inline size_t row_size_of(int type, int64_t n) { return sq::row_bytes(type, n); }
|
|
inline size_t type_size_of(int type) { const sq::TypeInfo* t = sq::type_info(type); return t ? t->bytes : 0; }
|
|
inline int blck_size_of(int type) { const sq::TypeInfo* t = sq::type_info(type); return t ? t->blck : 0; }
|
|
/// "this build's ggml-cpu is the AVX2 flavour": the layer's default contract is AVX2's whenever the CPU has AVX2 + FMA + F16C, which is what the shipped
|
|
/// ggml-cpu (STRATA_PORTABLE) gives on every CPU, an AVX-512 one included; ggml's own AVX-512 flavour does not exist here.
|
|
inline bool flavour_avx2() { return sq::default_arith() == sq::Arith::Avx2; }
|
|
inline bool flavour_f16c() { return sq::caps().avx2; }
|
|
inline bool flavour_avx512() { return false; }
|
|
inline void layer_init() {}
|
|
/// A row of blocks of `type` as floats (the DeepSeek embedding reader); false for a type the layer does not dequantize.
|
|
inline bool to_float(int type, const void* src, float* dst, int64_t n) { return sq::dequant_row((sq::Type) type, src, dst, n); }
|
|
inline uint16_t fp16_bits(float f) { return sq::fp32_to_fp16(f); }
|
|
#else
|
|
struct Tr {
|
|
const ggml_type_traits_cpu* t = nullptr;
|
|
int vec_dot_type = -1;
|
|
bool has_dot() const { return t != nullptr && t->vec_dot != nullptr; }
|
|
bool has_from_float() const { return t != nullptr && t->from_float != nullptr; }
|
|
void dot(int n, float* s, const void* w, const void* a) const { t->vec_dot(n, s, 0, w, 0, a, 0, 1); }
|
|
void from_float(const float* x, void* y, int64_t n) const { t->from_float(x, y, n); }
|
|
};
|
|
inline Tr traits(int type) {
|
|
Tr r;
|
|
r.t = ggml_get_type_traits_cpu((ggml_type) type);
|
|
r.vec_dot_type = r.t ? (int) r.t->vec_dot_type : -1;
|
|
return r;
|
|
}
|
|
inline size_t row_size_of(int type, int64_t n) { return ggml_row_size((ggml_type) type, n); }
|
|
inline size_t type_size_of(int type) { return ggml_type_size((ggml_type) type); }
|
|
inline int blck_size_of(int type) { return (int) ggml_blck_size((ggml_type) type); }
|
|
inline bool flavour_avx2() { return ggml_cpu_has_avx2() && ggml_cpu_has_fma(); }
|
|
inline bool flavour_f16c() { return ggml_cpu_has_f16c(); }
|
|
inline bool flavour_avx512() { return ggml_cpu_has_avx512(); }
|
|
inline void layer_init() { ggml_cpu_init(); }
|
|
inline bool to_float(int type, const void* src, float* dst, int64_t n) {
|
|
const ggml_type_traits* t = ggml_get_type_traits((ggml_type) type);
|
|
if (t == nullptr || t->to_float == nullptr) return false;
|
|
t->to_float(src, dst, n);
|
|
return true;
|
|
}
|
|
inline uint16_t fp16_bits(float f) { return ggml_fp32_to_fp16(f); }
|
|
#endif
|
|
|
|
} // namespace strata::kernels::cpu::pq
|