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
5.2 KiB174 linesraw
1// src/quant/act_ref.cpp - the reference activation quantizers (Generic contract) and the Q4_0 requantizer.
2//
3// Derived from ggml's `quantize_row_q8_0_ref`, `quantize_row_q8_1_ref`, `quantize_row_q8_K_ref` and `quantize_row_q4_0_ref` (llama.cpp
4// ggml/src/ggml-quants.c at 3cf03257f219afbe7334045ff7c6a06ac68c627d, Copyright (c) 2023-2026 The ggml authors, MIT).  Q8_0 / Q8_1 round half
5// away from zero (roundf) here; ggml's AVX / AVX2 quantizers round half to even (x86/act_avx*.cpp).  Q8_K is ggml's scalar code on every x86
6// flavour, and its scale is -127/max, not -128/max (the comment in the loop says why): do not "fix" that.
7#include "strata/quant/act.hpp"
8
9#include <cassert>
10
11#include "internal.hpp"
12#include "registry.hpp"
13
14#define GGML_UNUSED(x) (void) (x)
15#define MAX(a, b) ((a) > (b) ? (a) : (b))
16#define MIN(a, b) ((a) < (b) ? (a) : (b))
17
18namespace strata::quant {
19namespace {
20
21void q8_0_ref(const float* STRATA_RESTRICT x, block_q8_0* STRATA_RESTRICT y, int64_t k) {
22    assert(k % QK8_0 == 0);
23    const int nb = k / QK8_0;
24
25    for (int i = 0; i < nb; i++) {
26        float amax = 0.0f; // absolute max
27
28        for (int j = 0; j < QK8_0; j++) {
29            const float v = x[i*QK8_0 + j];
30            amax = MAX(amax, fabsf(v));
31        }
32
33        const float d = amax / ((1 << 7) - 1);
34        const float id = d ? 1.0f/d : 0.0f;
35
36        y[i].d = fp32_to_fp16(d);
37
38        for (int j = 0; j < QK8_0; ++j) {
39            const float x0 = x[i*QK8_0 + j]*id;
40
41            y[i].qs[j] = roundf(x0);
42        }
43    }
44}
45
46void q8_1_ref(const float* STRATA_RESTRICT x, block_q8_1* STRATA_RESTRICT y, int64_t k) {
47    assert(QK8_1 == 32);
48    assert(k % QK8_1 == 0);
49    const int nb = k / QK8_1;
50
51    for (int i = 0; i < nb; i++) {
52        float amax = 0.0f; // absolute max
53
54        for (int j = 0; j < QK8_1; j++) {
55            const float v = x[i*QK8_1 + j];
56            amax = MAX(amax, fabsf(v));
57        }
58
59        const float d = amax / ((1 << 7) - 1);
60        const float id = d ? 1.0f/d : 0.0f;
61
62        y[i].d = fp32_to_fp16(d);
63
64        int sum = 0;
65
66        for (int j = 0; j < QK8_1/2; ++j) {
67            const float v0 = x[i*QK8_1           + j]*id;
68            const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
69
70            y[i].qs[          j] = roundf(v0);
71            y[i].qs[QK8_1/2 + j] = roundf(v1);
72
73            sum += y[i].qs[          j];
74            sum += y[i].qs[QK8_1/2 + j];
75        }
76
77        y[i].s = fp32_to_fp16(sum*d);
78    }
79}
80
81void q8_K_ref(const float* STRATA_RESTRICT x, block_q8_K* STRATA_RESTRICT y, int64_t k) {
82    assert(k % QK_K == 0);
83    const int64_t nb = k / QK_K;
84
85    for (int i = 0; i < nb; i++) {
86
87        float max = 0;
88        float amax = 0;
89        for (int j = 0; j < QK_K; ++j) {
90            float ax = fabsf(x[j]);
91            if (ax > amax) {
92                amax = ax; max = x[j];
93            }
94        }
95        if (!amax) {
96            y[i].d = 0;
97            memset(y[i].qs, 0, QK_K);
98            x += QK_K;
99            continue;
100        }
101        //const float iscale = -128.f/max;
102        // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
103        const float iscale = -127.f/max;
104        for (int j = 0; j < QK_K; ++j) {
105            int v = nearest_int(iscale*x[j]);
106            y[i].qs[j] = MIN(127, v);
107        }
108        for (int j = 0; j < QK_K/16; ++j) {
109            int sum = 0;
110            for (int ii = 0; ii < 16; ++ii) {
111                sum += y[i].qs[j*16 + ii];
112            }
113            y[i].bsums[j] = sum;
114        }
115        y[i].d = 1/iscale;
116        x += QK_K;
117    }
118}
119
120void q4_0_ref(const float* STRATA_RESTRICT x, block_q4_0* STRATA_RESTRICT y, int64_t k) {
121    static const int qk = QK4_0;
122
123    assert(k % qk == 0);
124
125    const int nb = k / qk;
126
127    for (int i = 0; i < nb; i++) {
128        float amax = 0.0f; // absolute max
129        float max  = 0.0f;
130
131        for (int j = 0; j < qk; j++) {
132            const float v = x[i*qk + j];
133            if (amax < fabsf(v)) {
134                amax = fabsf(v);
135                max  = v;
136            }
137        }
138
139        const float d  = max / -8;
140        const float id = d ? 1.0f/d : 0.0f;
141
142        y[i].d = fp32_to_fp16(d);
143
144        for (int j = 0; j < qk/2; ++j) {
145            const float x0 = x[i*qk + 0    + j]*id;
146            const float x1 = x[i*qk + qk/2 + j]*id;
147
148            const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
149            const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
150
151            y[i].qs[j]  = xi0;
152            y[i].qs[j] |= xi1 << 4;
153        }
154    }
155}
156
157}  // namespace
158
159void quantize_q8_0_ref(const float* x, void* y, int64_t n) { q8_0_ref(x, (block_q8_0*) y, n); }
160void quantize_q8_1_ref(const float* x, void* y, int64_t n) { q8_1_ref(x, (block_q8_1*) y, n); }
161void quantize_q8_K_ref(const float* x, void* y, int64_t n) { q8_K_ref(x, (block_q8_K*) y, n); }
162void quantize_q4_0_ref(const float* x, void* y, int64_t n) { q4_0_ref(x, (block_q4_0*) y, n); }
163
164namespace {
165const ActKernel kGenericAct[] = {
166    {quantize_q8_0_ref, Type::Q8_0, Arith::Generic, Isa::Scalar, "generic_q8_0"},
167    {quantize_q8_1_ref, Type::Q8_1, Arith::Generic, Isa::Scalar, "generic_q8_1"},
168    {quantize_q8_K_ref, Type::Q8_K, Arith::Generic, Isa::Scalar, "generic_q8_K"},
169};
170}  // namespace
171
172ActTable generic_act_table() noexcept { return {kGenericAct, 3}; }
173
174}  // namespace strata::quant