okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
5.4 KiB136 linesraw
1// Strata. The trellis routed experts (ds4_iq.cuh's is_tq types) on the CPU lane of the DeepSeek-V4.1 engine path: Strata's
2// own decode-and-dot kernels (include/strata/kernels/cpu/tq_cpu.hpp: AVX-512 / AVX2 / scalar, one result whatever the ISA)
3// over the file's rows read in place.  The activations are the device's (ds4_gguf.cu's q8_quant: int8 with an F32 scale a 32
4// values), so an expert gives the same sums on either lane up to the order of the blocks' float sums.
5#include "ds4_gguf_cpu.hpp"
6
7#include <algorithm>
8#include <cmath>
9#include <cstdint>
10#include <cstring>
11#include <string>
12#include <vector>
13
14#if defined(STRATA_DSV41_TQ_CPU)
15#include "strata/kernels/cpu/tq_cpu.hpp"
16#endif
17
18namespace strata::ds4::gguf {
19
20#if defined(STRATA_DSV41_TQ_CPU)
21namespace kc = strata::kernels::cpu;
22namespace {
23
24int kbits_of(int type) {
25    switch (type) {
26        case 145: return 6;
27        case 146: return 7;
28        case 224: case 226: return 5;
29        case 225: case 227: return 4;
30        default: return 0;
31    }
32}
33constexpr int kActTq = 100;   // IqCpu::gu_act / d_act of these types: the layout below
34
35// an activation of k values: k / 32 F32 scales, then k int8
36size_t act_bytes_of(int k) { return (size_t) k / 32 * 4 + (size_t) k; }
37
38void unpack(const void* act, int k, kc::tq_detail::TqAct& a) {
39    const uint8_t* p = (const uint8_t*) act;
40    std::memcpy(a.d, p, (size_t) k / 32 * 4);
41    const int8_t* q = (const int8_t*) (p + (size_t) k / 32 * 4);
42    for (int i = 0; i < k; ++i) a.q[i] = q[i];
43}
44
45// dots of rows [r0, r1) of one matrix against nt activations: out[t][r]
46void dots(int type, const uint8_t* w, size_t rb, int n, const void* const* act, int nt, float* const* out, int r0, int r1) {
47    static thread_local std::vector<kc::tq_detail::TqAct> unp(kc::kTqMaxTokens);
48    const kc::tq_detail::TqAct* ap[kc::kTqMaxTokens];
49    for (int t = 0; t < nt; ++t) {
50        unpack(act[t], n, unp[(size_t) t]);
51        ap[t] = &unp[(size_t) t];
52    }
53    const int kb = kc::tq_kbits(type);
54    const int16_t* tab = kc::tq_state_table_for(type);
55    switch (kc::tq_isa()) {
56        case kc::TqIsa::avx512: kc::tq_detail::tq_rows_avx512(kb, tab, w, rb, n, ap, nt, out, r0, r1); break;
57        case kc::TqIsa::avx2: kc::tq_detail::tq_rows_avx2(kb, tab, w, rb, n, ap, nt, out, r0, r1); break;
58        default: kc::tq_detail::tq_rows_scalar(kb, tab, w, rb, n, ap, nt, out, r0, r1); break;
59    }
60}
61}  // namespace
62
63bool tq_cpu_fmt(int tg, int td, int n_embd, int n_ff, IqCpu& f, std::string& err) {
64    if (kbits_of(tg) == 0 || kbits_of(td) == 0) {
65        err = "not trellis types";
66        return false;
67    }
68    if (n_embd % 128 != 0 || n_ff % 128 != 0 || n_embd > kc::kTqMaxN || n_ff > kc::kTqMaxN) {
69        err = "the trellis CPU kernels take widths of 128 blocks up to " + std::to_string(kc::kTqMaxN);
70        return false;
71    }
72    f.tg = tg;
73    f.td = td;
74    f.n_embd = n_embd;
75    f.n_ff = n_ff;
76    f.gu_act = f.d_act = kActTq;
77    f.gu_row = (size_t) (n_embd / 128) * (2 + 4 * kbits_of(tg));
78    f.d_row = (size_t) (n_ff / 128) * (2 + 4 * kbits_of(td));
79    f.act_bytes = act_bytes_of(n_embd);
80    f.h_bytes = act_bytes_of(n_ff);
81    return true;
82}
83
84// x (k values) -> the layout above (the device's q8_quant: sc = max|x| / 127, q = round-half-even(x / sc))
85void tq_cpu_quant(const float* x, int k, void* dst) {
86    float* d = (float*) dst;
87    int8_t* q = (int8_t*) ((uint8_t*) dst + (size_t) k / 32 * 4);
88    for (int b = 0; b < k / 32; ++b) {
89        const float* xb = x + b * 32;
90        float am = 0.0f;
91        for (int j = 0; j < 32; ++j) am = std::max(am, std::fabs(xb[j]));
92        const float sc = am / 127.0f;
93        d[b] = sc;
94        for (int j = 0; j < 32; ++j) q[b * 32 + j] = (int8_t) (am > 0.0f ? (int) std::nearbyint(xb[j] / sc) : 0);
95    }
96}
97
98void tq_cpu_gate_up(const IqCpu& f, const uint8_t* gate, const uint8_t* up, const void* const* act, int nt, float* const* ff,
99                    int r0, int r1, float limit) {
100    static thread_local std::vector<float> tmp[kc::kTqMaxTokens];
101    float* up_out[kc::kTqMaxTokens];
102    for (int t = 0; t < nt; ++t) {
103        tmp[t].resize((size_t) r1);
104        up_out[t] = tmp[t].data();
105    }
106    dots(f.tg, gate, f.gu_row, f.n_embd, act, nt, ff, r0, r1);
107    dots(f.tg, up, f.gu_row, f.n_embd, act, nt, up_out, r0, r1);
108    for (int t = 0; t < nt; ++t)
109        for (int r = r0; r < r1; ++r) {
110            float g = ff[t][r], u = up_out[t][r];
111            if (limit > 0.0f) {
112                g = std::min(g, limit);
113                u = std::min(std::max(u, -limit), limit);
114            }
115            ff[t][r] = g / (1.0f + std::exp(-g)) * u;
116        }
117}
118
119void tq_cpu_down(const IqCpu& f, const uint8_t* down, const void* const* hq, int nt, float* const* out, int r0, int r1) {
120    dots(f.td, down, f.d_row, f.n_ff, hq, nt, out, r0, r1);
121}
122bool tq_cpu_type(int type) { return kbits_of(type) > 0; }
123int tq_cpu_act() { return kActTq; }
124#else
125bool tq_cpu_fmt(int, int, int, int, IqCpu&, std::string& err) {
126    err = "this build has no CPU trellis kernels (STRATA_NATIVE_EXPERTS=OFF)";
127    return false;
128}
129void tq_cpu_quant(const float*, int, void*) {}
130void tq_cpu_gate_up(const IqCpu&, const uint8_t*, const uint8_t*, const void* const*, int, float* const*, int, int, float) {}
131void tq_cpu_down(const IqCpu&, const uint8_t*, const void* const*, int, float* const*, int, int) {}
132bool tq_cpu_type(int) { return false; }
133int tq_cpu_act() { return 100; }
134#endif
135
136}  // namespace strata::ds4::gguf