okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-2a (7c09c4a75) into f-own-int: the Strata quant layer and the x86 CPU rows9c143c403

main
1.9 KiB41 linesraw
1// src/quant/x86/tier_common.hpp - what every x86 tier file of the layer needs before it includes ggml's ported x86 code (x86_dots.inc, x86_act.inc).
2//
3// The tier files (dot_avx.cpp, dot_avx2.cpp, act_avx.cpp, act_avx2.cpp) are compiled with their own instruction-set flags (cmake/backends/quant.cmake),
4// so the #if __AVX2__ / __AVX__ branches of the ported code select exactly what ggml-cpu selects for that flavour.  Never include this header from a
5// file compiled without those flags.
6#pragma once
7
8#define STRATA_QUANT_TABLES
9#include "../internal.hpp"
10
11#include <immintrin.h>
12
13#include <cassert>
14#include <cstdlib>
15
16#define UNUSED(x) (void) (x)
17
18// Keeps a float product a separate rounded value, so the compiler cannot fuse it with the add that follows (where ggml-cpu's build does not).
19#if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
20#define STRATA_NOFUSE(v) __asm__("" : "+x"(v))
21#else
22#define STRATA_NOFUSE(v) (void) (v)   // MSVC: checked by the Windows run of tests/quant/quant_parity (RESULTS-OWN-2A.md)
23#endif
24
25// ggml's own choice: F16C hardware conversions where the flavour has them (same values as the software ones for every finite input), software otherwise.
26#if defined(__F16C__) || (defined(_MSC_VER) && defined(__AVX2__))
27namespace strata::quant {
28#if defined(_MSC_VER)   // MSVC has no scalar _cvtsh_ss / _cvtss_sh: the packed ones on one lane (the same VCVTPH2PS / VCVTPS2PH)
29inline float h2f(uint16_t h) { return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) h))); }
30inline uint16_t f2h(float f) { return (uint16_t) _mm_cvtsi128_si32(_mm_cvtps_ph(_mm_set_ss(f), 0)); }
31#else
32inline float h2f(uint16_t h) { return _cvtsh_ss(h); }
33inline uint16_t f2h(float f) { return _cvtss_sh(f, 0); }
34#endif
35}  // namespace strata::quant
36#else
37namespace strata::quant {
38inline float h2f(uint16_t h) { return fp16_to_fp32(h); }
39inline uint16_t f2h(float f) { return fp32_to_fp16(f); }
40}  // namespace strata::quant
41#endif