okami.mom/strata

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

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
4.0 KiB108 linesraw
1// src/kernels/cpu/tq_states.hpp - the 32 trellis states of one TQ2_T / TQK6 / TQK7 block in plain C++ (all three
2// kernels: scalar code beat byte-shuffle versions on AVX2 and AVX-512).  Internal to src/kernels/cpu/tq_*.cpp.
3//
4// TQ2_T (K = 8): state t = qs[(t + 31) % 32] << 8 | qs[t], masked to 15 bits.
5// TQK (K = 6, 7): the 16 stream bits from bit (31 - t)*K, LSB first, circular modulo 32*K bits, masked to 15 bits.
6// Every window is one unaligned 32-bit little-endian read inside the block's 4*K bytes, except the windows that run
7// past the end (t = 0 and 1), which read a 64-bit view of the last four bytes followed by the first four.
8#pragma once
9
10#include <cstdint>
11#include <cstring>
12
13namespace strata::kernels::cpu::tq_detail {
14
15inline constexpr uint32_t kTqStateMask = 0x7fffu;
16
17inline uint32_t tq_ld32(const uint8_t* p) {
18    uint32_t v;
19    std::memcpy(&v, p, 4);
20    return v;   // x86 (and every target Strata builds for) is little-endian
21}
22
23/// State t alone (t a constant after unrolling); `wrap` = tq_wrap<KB>(qs) for the TQK windows past the end.
24template <int KB>
25inline uint64_t tq_wrap(const uint8_t* qs) {
26    if constexpr (KB == 8) return 0;
27    else return (uint64_t) tq_ld32(qs + 4 * KB - 4) | ((uint64_t) tq_ld32(qs) << 32);
28}
29template <int KB>
30inline uint32_t tq_state(const uint8_t* qs, uint64_t wrap, int t) {
31    if constexpr (KB == 8) {
32        return (((uint32_t) qs[(t + 31) & 31] << 8) | qs[t]) & kTqStateMask;
33    } else {
34        constexpr int NB = 4 * KB;
35        const int off = (31 - t) * KB, b = off >> 3;
36        const uint32_t v = b + 4 <= NB ? tq_ld32(qs + b) >> (off & 7) : (uint32_t) (wrap >> (off - 8 * (NB - 4)));
37        return v & kTqStateMask;
38    }
39}
40
41template <int KB>
42inline void tq_states(const uint8_t* qs, uint32_t* st) {
43    if constexpr (KB == 8) {
44        st[0] = (((uint32_t) qs[31] << 8) | qs[0]) & kTqStateMask;
45#if defined(__GNUC__)
46#pragma GCC unroll 31
47#endif
48        for (int t = 1; t < 32; ++t) st[t] = (((uint32_t) qs[t - 1] << 8) | qs[t]) & kTqStateMask;
49    } else {
50        constexpr int NB = 4 * KB;
51        const uint64_t wrap = (uint64_t) tq_ld32(qs + NB - 4) | ((uint64_t) tq_ld32(qs) << 32);
52#if defined(__GNUC__)
53#pragma GCC unroll 32
54#endif
55        for (int t = 0; t < 32; ++t) {
56            const int off = (31 - t) * KB, b = off >> 3;
57            const uint32_t v = b + 4 <= NB ? tq_ld32(qs + b) >> (off & 7) : (uint32_t) (wrap >> (off - 8 * (NB - 4)));
58            st[t] = v & kTqStateMask;
59        }
60    }
61}
62
63/// TQP (Gyro-L): 64 pair-trellis steps; state t = the 16 stream bits from bit (63 - t)*KB, LSB first,
64/// circular over 8*KB bytes (tqp_state in the fork's ggml-common.h).
65template <int KB>
66inline uint32_t tq_state_tqp(const uint8_t* qs, int t) {
67    constexpr int NB = 8 * KB;
68    const uint32_t off = (uint32_t) (63 - t) * (uint32_t) KB;
69    const uint32_t i0 = off >> 3;
70    const uint32_t i1 = (i0 + 1) % (uint32_t) NB;
71    const uint32_t i2 = (i0 + 2) % (uint32_t) NB;
72    const uint32_t w = (uint32_t) qs[i0] | ((uint32_t) qs[i1] << 8) | ((uint32_t) qs[i2] << 16);
73    return (w >> (off & 7)) & kTqStateMask;
74}
75template <int KB>
76inline void tq_states_tqp(const uint8_t* qs, uint32_t* st) {
77#if defined(__GNUC__)
78#pragma GCC unroll 64
79#endif
80    for (int t = 0; t < 64; ++t) st[t] = tq_state_tqp<KB>(qs, t);
81}
82
83/// fp16 bits -> float, exact (the block scales and the Q8_0 activation scales).
84inline float tq_half_to_float(uint16_t h) {
85    const uint32_t sign = (uint32_t) (h & 0x8000u) << 16;
86    uint32_t exp = (h >> 10) & 0x1fu, man = h & 0x3ffu, bits;
87    if (exp == 0x1fu) {
88        bits = sign | 0x7f800000u | (man << 13);
89    } else if (exp != 0) {
90        bits = sign | ((exp + 112u) << 23) | (man << 13);
91    } else if (man == 0) {
92        bits = sign;
93    } else {   // subnormal: man * 2^-24, exact in float
94        const float m = (float) man * (1.0f / 16777216.0f);
95        return sign ? -m : m;
96    }
97    float f;
98    std::memcpy(&f, &bits, 4);
99    return f;
100}
101
102inline uint16_t tq_ld16(const uint8_t* p) {
103    uint16_t v;
104    std::memcpy(&v, p, 2);
105    return v;
106}
107
108}  // namespace strata::kernels::cpu::tq_detail