okami.mom/strata

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

Niko1221Merge f-own-2n (ead5a32ae) into f-own-int: the Arm NEON tier of the quant layer0a9381336

main
3.1 KiB94 linesraw
1// arm_act.inc - ggml's Arm NEON Q8_0 and Q8_1 activation quantizers (arch/arm/quants.c at the pin; MIT, The ggml authors), same namespace as arm_dots.inc.
2// Round to nearest even (vcvtnq_s32_f32).  Q8_K is ggml's scalar _ref on Arm too ("placeholder implementation for Apple targets").
3
4void quantize_q8_0(const float* STRATA_RESTRICT x, void* STRATA_RESTRICT vy, int64_t k) {
5    assert(QK8_0 == 32);
6    assert(k % QK8_0 == 0);
7    const int nb = k / QK8_0;
8
9    block_q8_0* STRATA_RESTRICT y = (block_q8_0*) vy;
10
11#if defined(__ARM_NEON)
12    for (int i = 0; i < nb; i++) {
13        float32x4_t srcv [8];
14        float32x4_t asrcv[8];
15        float32x4_t amaxv[8];
16
17        for (int j = 0; j < 8; j++) srcv[j]  = vld1q_f32(x + i*32 + 4*j);
18        for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
19
20        for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
21        for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
22        for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
23
24        const float amax = vmaxvq_f32(amaxv[0]);
25
26        const float d = amax / ((1 << 7) - 1);
27        const float id = d ? 1.0f/d : 0.0f;
28
29        y[i].d = f2h(d);
30
31        for (int j = 0; j < 8; j++) {
32            const float32x4_t v  = vmulq_n_f32(srcv[j], id);
33            const int32x4_t   vi = vcvtnq_s32_f32(v);
34
35            y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
36            y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
37            y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
38            y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
39        }
40    }
41#else
42    UNUSED(nb);
43    // scalar
44    quantize_q8_0_ref(x, y, k);
45#endif
46}
47
48void quantize_q8_1(const float* STRATA_RESTRICT x, void* STRATA_RESTRICT vy, int64_t k) {
49    assert(k % QK8_1 == 0);
50    const int nb = k / QK8_1;
51
52    block_q8_1* STRATA_RESTRICT y = (block_q8_1*) vy;
53#if defined(__ARM_NEON)
54    for (int i = 0; i < nb; i++) {
55        float32x4_t srcv [8];
56        float32x4_t asrcv[8];
57        float32x4_t amaxv[8];
58
59        for (int j = 0; j < 8; j++) srcv[j]  = vld1q_f32(x + i*32 + 4*j);
60        for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
61
62        for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
63        for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
64        for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
65
66        const float amax = vmaxvq_f32(amaxv[0]);
67
68        const float d = amax / ((1 << 7) - 1);
69        const float id = d ? 1.0f/d : 0.0f;
70
71        y[i].d = f2h(d);
72
73        int32x4_t accv = vdupq_n_s32(0);
74
75        for (int j = 0; j < 8; j++) {
76            const float32x4_t v  = vmulq_n_f32(srcv[j], id);
77            const int32x4_t   vi = vcvtnq_s32_f32(v);
78
79            y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
80            y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
81            y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
82            y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
83
84            accv = vaddq_s32(accv, vi);
85        }
86
87        y[i].s = f2h(d * vaddvq_s32(accv));
88    }
89#else
90    UNUSED(nb);
91    // scalar
92    quantize_q8_1_ref(x, y, k);
93#endif
94}