okami.mom/strata

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

Niko1221Merge f-own-3c 117473f64 into f-own-intd3abf7330

main
8.6 KiB168 linesraw
1// tools/vision/own/cpu_kernels_sse41.cpp - the SSE4.1 tier (6 x 8 register tile: 12 accumulators of the 16 xmm registers), for x86 CPUs without AVX
2// (Nehalem / Westmere, the Pentium and Celeron parts without AVX, Silvermont / Goldmont).  Compiled with -msse4.1 (MSVC x64: no flag needed); only called
3// when quant::isa_ok(Sse41).
4//
5// No FMA on these CPUs: a product term is a multiply then an add (two roundings), in the same k order as every tier, so the products are bit-identical to
6// the scalar tier's on x86 (whose struct V also multiplies then adds) and differ from the AVX2 tier's FMA by the second rounding (within rule 1).
7// F16 weights are converted without F16C (bit exact, integer code), exp is the C library's expf per lane, as in the scalar tier.
8#include "cpu_tier.h"      // only this and the intrinsics headers: no standard-library code may be compiled with this file's ISA flags (cpu_tier.h)
9
10#if defined(__x86_64__) || defined(_M_X64)
11#include <emmintrin.h>
12#include <smmintrin.h>
13
14namespace strata_vit {
15namespace {
16namespace kn {
17
18template <class T> inline T kmin(T a, T b) { return b < a ? b : a; }
19template <class T> inline T kmax(T a, T b) { return a < b ? b : a; }
20inline void kcopy(float* d, const float* s, int n) { for (int i = 0; i < n; ++i) d[i] = s[i]; }
21inline void kzero(float* d, int n) { for (int i = 0; i < n; ++i) d[i] = 0.0f; }
22
23#define STRATA_KN_RAW 1
24constexpr int VW = 4;
25typedef __m128 VR;
26inline VR vr_load(const float* p) { return _mm_loadu_ps(p); }
27inline void vr_store(float* p, VR v) { _mm_storeu_ps(p, v); }
28inline VR vr_set1(float f) { return _mm_set1_ps(f); }
29inline VR vr_zero() { return _mm_setzero_ps(); }
30inline VR vr_add(VR a, VR b) { return _mm_add_ps(a, b); }
31inline VR vr_sub(VR a, VR b) { return _mm_sub_ps(a, b); }
32inline VR vr_mul(VR a, VR b) { return _mm_mul_ps(a, b); }
33inline VR vr_div(VR a, VR b) { return _mm_div_ps(a, b); }
34inline VR vr_fma(VR a, VR b, VR c) { return _mm_add_ps(_mm_mul_ps(a, b), c); }      // a*b + c with two roundings (no FMA on these CPUs)
35inline VR vr_max(VR a, VR b) { return _mm_max_ps(a, b); }
36inline float vr_sqrt1(float x) { return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(x))); }
37// (x0 + x2) + (x1 + x3): the scalar tier's order
38inline float vr_hadd(VR a) {
39    const __m128 s = _mm_add_ps(a, _mm_movehl_ps(a, a));
40    return _mm_cvtss_f32(_mm_add_ss(s, _mm_shuffle_ps(s, s, 1)));
41}
42inline float vr_hmax(VR a) {
43    const __m128 s = _mm_max_ps(a, _mm_movehl_ps(a, a));
44    return _mm_cvtss_f32(_mm_max_ss(s, _mm_shuffle_ps(s, s, 1)));
45}
46
47// The product's micro-kernel: the generic micro of cpu_kernels_impl.h on raw __m128, multiply then add.
48#define STRATA_KN_HAS_MICRO 1
49template <int MR>
50inline void micro(const float* A, size_t lda, const float* B, size_t ldb, int K, float* C, size_t ldc, int init, const float* bias) {
51    __m128 acc[MR][2];
52    for (int r = 0; r < MR; ++r)
53        for (int c = 0; c < 2; ++c) {
54            __m128 v = _mm_setzero_ps();
55            if (init != 0) v = _mm_loadu_ps(C + (size_t) r * ldc + c * 4);
56            if (init != 1 && bias) v = _mm_add_ps(v, _mm_loadu_ps(bias + c * 4));
57            acc[r][c] = v;
58        }
59    for (int k = 0; k < K; ++k) {
60        const __m128 b0 = _mm_loadu_ps(B + (size_t) k * ldb), b1 = _mm_loadu_ps(B + (size_t) k * ldb + 4);
61        for (int r = 0; r < MR; ++r) {
62            const __m128 a = _mm_set1_ps(A[(size_t) r * lda + k]);
63            acc[r][0] = _mm_add_ps(_mm_mul_ps(a, b0), acc[r][0]);
64            acc[r][1] = _mm_add_ps(_mm_mul_ps(a, b1), acc[r][1]);
65        }
66    }
67    for (int r = 0; r < MR; ++r)
68        for (int c = 0; c < 2; ++c) _mm_storeu_ps(C + (size_t) r * ldc + c * 4, acc[r][c]);
69}
70
71// expf of the C library per lane (an out-of-line call per element), with the scalar tier's clamp [-87, 88]: the scalar tier's exp, so this tier's output is
72// byte-identical to the scalar tier's (a polynomial exp here gave Qwen3.8 code4217 a cosine of 0.998928 against llama.cpp's reader, under the 0.999 bar)
73inline VR vr_exp(VR xv) {
74    alignas(16) float t[4];
75    _mm_store_ps(t, xv);
76    for (int i = 0; i < 4; ++i) t[i] = cpu_tier_expf(t[i] < -87.0f ? -87.0f : t[i] > 88.0f ? 88.0f : t[i]);
77    return _mm_load_ps(t);
78}
79
80// 4 floats rounded to the 16-bit type and back, bit for bit as the scalar tier's round_one: BF16 round to nearest even (a NaN stays), F16 round to nearest
81// even to 11 significant bits (|x| >= 65520, inf and NaN stay as they are, as in the scalar tier), F16 subnormals to multiples of 2^-24
82inline __m128 round4(__m128 x, int kind) {
83    const __m128i u = _mm_castps_si128(x);
84    const __m128i absu = _mm_and_si128(u, _mm_set1_epi32(0x7fffffff));
85    if (kind == 1) {
86        const __m128i lsb = _mm_and_si128(_mm_srli_epi32(u, 16), _mm_set1_epi32(1));
87        const __m128i r = _mm_and_si128(_mm_add_epi32(u, _mm_add_epi32(_mm_set1_epi32(0x7fff), lsb)), _mm_set1_epi32((int) 0xffff0000u));
88        const __m128i nan = _mm_cmpgt_epi32(absu, _mm_set1_epi32(0x7f800000));
89        return _mm_castsi128_ps(_mm_blendv_epi8(r, u, nan));
90    }
91    const __m128 a = _mm_castsi128_ps(absu);
92    const __m128i sign = _mm_and_si128(u, _mm_set1_epi32((int) 0x80000000u));
93    // normal: drop 13 mantissa bits, round to nearest even
94    const __m128i lsb = _mm_and_si128(_mm_srli_epi32(absu, 13), _mm_set1_epi32(1));
95    const __m128i nrm = _mm_and_si128(_mm_add_epi32(absu, _mm_add_epi32(_mm_set1_epi32(0xfff), lsb)), _mm_set1_epi32((int) 0xffffe000u));
96    // subnormal: (a + 0.5) - 0.5 rounds a to a multiple of 2^-24 (the ulp of 0.5), ties to even, exactly
97    const __m128 sub = _mm_sub_ps(_mm_add_ps(a, _mm_set1_ps(0.5f)), _mm_set1_ps(0.5f));
98    const __m128 is_sub = _mm_cmplt_ps(a, _mm_set1_ps(6.103515625e-05f));
99    __m128 q = _mm_blendv_ps(_mm_castsi128_ps(nrm), sub, is_sub);
100    q = _mm_or_ps(q, _mm_castsi128_ps(sign));
101    const __m128 keep = _mm_cmplt_ps(a, _mm_set1_ps(65520.0f));      // false for NaN and |x| >= 65520: those stay
102    return _mm_blendv_ps(x, q, keep);
103}
104inline void round_buf(float* p, size_t n, int kind) {
105    size_t i = 0;
106    for (; i + 4 <= n; i += 4) _mm_storeu_ps(p + i, round4(_mm_loadu_ps(p + i), kind));
107    if (i < n) {
108        float t[4] = {0};
109        for (size_t j = i; j < n; ++j) t[j - i] = p[j];
110        _mm_storeu_ps(t, round4(_mm_loadu_ps(t), kind));
111        for (size_t j = i; j < n; ++j) p[j] = t[j - i];
112    }
113}
114
115// 4 halves (zero-extended to 32 bits) to float, bit exact without F16C: (exponent and mantissa << 13) as a float times 2^112 for normal and subnormal
116// values (an exact scaling), inf / NaN by bits; the sign put back
117inline __m128 h2f4(__m128i h) {
118    const __m128i em = _mm_and_si128(h, _mm_set1_epi32(0x7fff));
119    const __m128i sign = _mm_slli_epi32(_mm_and_si128(h, _mm_set1_epi32(0x8000)), 16);
120    const __m128i sh = _mm_slli_epi32(em, 13);
121    const __m128 fin = _mm_mul_ps(_mm_castsi128_ps(sh), _mm_castsi128_ps(_mm_set1_epi32(0x77800000)));      // * 2^112
122    const __m128i inf = _mm_or_si128(sh, _mm_set1_epi32(0x7f800000));
123    const __m128i big = _mm_cmpgt_epi32(em, _mm_set1_epi32(0x7bff));
124    const __m128i r = _mm_blendv_epi8(_mm_castps_si128(fin), inf, big);
125    return _mm_castsi128_ps(_mm_or_si128(r, sign));
126}
127inline float f16_one(uint16_t h) { return _mm_cvtss_f32(h2f4(_mm_cvtsi32_si128((int) h))); }
128inline float bf16_one(uint16_t h) { return _mm_cvtss_f32(_mm_castsi128_ps(_mm_cvtsi32_si128((int) ((uint32_t) h << 16)))); }
129
130inline void conv16(const void* Wm, int kind, size_t off, int n, float* out) {
131    const uint16_t* p = (const uint16_t*) Wm + off;
132    int i = 0;
133    if (kind == 2) {
134        for (; i + 8 <= n; i += 8) {
135            const __m128i w = _mm_loadu_si128((const __m128i*) (p + i));
136            _mm_storeu_ps(out + i, h2f4(_mm_cvtepu16_epi32(w)));
137            _mm_storeu_ps(out + i + 4, h2f4(_mm_cvtepu16_epi32(_mm_srli_si128(w, 8))));
138        }
139        for (; i < n; ++i) out[i] = f16_one(p[i]);
140    } else {
141        const __m128i z = _mm_setzero_si128();
142        for (; i + 8 <= n; i += 8) {
143            const __m128i w = _mm_loadu_si128((const __m128i*) (p + i));
144            _mm_storeu_ps(out + i, _mm_castsi128_ps(_mm_unpacklo_epi16(z, w)));
145            _mm_storeu_ps(out + i + 4, _mm_castsi128_ps(_mm_unpackhi_epi16(z, w)));
146        }
147        for (; i < n; ++i) out[i] = bf16_one(p[i]);
148    }
149}
150
151}  // namespace kn
152}  // namespace
153}  // namespace strata_vit
154
155#define STRATA_KN_MR 6
156#define KN_MIN(a, b) kmin(a, b)
157#define KN_MAX(a, b) kmax(a, b)
158#define KN_COPY(d, s, n) kcopy((d), (s), (n))
159#define KN_ZERO(d, n) kzero((d), (n))
160#define STRATA_KN_NAME "sse41"
161#define STRATA_KN_GETTER cpu_ops_sse41
162#include "cpu_kernels_impl.h"
163
164#else   // not x86-64: the tier does not exist, the dispatcher never asks for it
165namespace strata_vit {
166const CpuOps& cpu_ops_sse41() { return cpu_ops_scalar(); }
167}
168#endif