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
6.6 KiB151 linesraw
1// src/kernels/cuda/q8_1_il.cuh - the interleaved copy of 2..8 q8_1 activation columns (native_mmvq.hpp) as the
2// multi-column dot products read it, for native_mmvq.cu and iq_kernels.cu.
3#pragma once
4
5#include "strata/kernels/native_mmvq.hpp"
6
7#include <cuda_fp16.h>
8#include <cuda_runtime.h>
9
10#include <cstddef>
11#include <cstdint>
12
13namespace strata::kernels {
14namespace {
15
16// NC columns of the block-major (PM false) or position-major copy: u(b, p, o) = int p of q8_1 block b of every column,
17// scales(b, o) = their scales
18template <int NC, bool PM> struct Q81Il {
19    static constexpr int CP = native_q8_1_il_cp(NC);
20    const int* __restrict__ q;
21    const float* __restrict__ d;
22    int nb;   // blocks a column
23    __device__ __forceinline__ void u(int b, int p, int (&o)[NC]) const {
24        const int* s = q + std::size_t(PM ? p * nb + b : b * 8 + p) * CP;
25        if constexpr (CP == 2) {
26            const int2 a = *reinterpret_cast<const int2*>(s);
27            o[0] = a.x;
28            o[1] = a.y;
29        } else {
30            const int4 a = *reinterpret_cast<const int4*>(s);
31            o[0] = a.x;
32            if constexpr (NC > 1) o[1] = a.y;
33            if constexpr (NC > 2) o[2] = a.z;
34            if constexpr (NC > 3) o[3] = a.w;
35            if constexpr (NC > 4) {
36                const int4 e = *reinterpret_cast<const int4*>(s + 4);
37                o[4] = e.x;
38                if constexpr (NC > 5) o[5] = e.y;
39                if constexpr (NC > 6) o[6] = e.z;
40                if constexpr (NC > 7) o[7] = e.w;
41            }
42        }
43    }
44    // P1 (native_mmvq_il_pf_kernel): the same loads from a pointer the caller strength-reduces - qat(qbase(b, p) + k * QB)
45    // is u(b + k, p), dat(dbase(b) + k * DB) is scales(b + k)
46    static constexpr int QB = PM ? CP : 8 * CP, DB = CP;
47    __device__ __forceinline__ int qp(int dp) const { return PM ? dp * nb * CP : dp * CP; }   // ints from p to p + dp
48    __device__ __forceinline__ const int* qbase(int b, int p) const { return q + std::size_t(PM ? p * nb + b : b * 8 + p) * CP; }
49    __device__ __forceinline__ const float* dbase(int b) const { return d + std::size_t(b) * CP; }
50    __device__ __forceinline__ void qat(const int* s, int (&o)[NC]) const {   // __ldg: s comes from an asm (madw), so the
51        if constexpr (CP == 2) {                                                // front end no longer knows it is global
52            const int2 a = __ldg(reinterpret_cast<const int2*>(s));
53            o[0] = a.x;
54            o[1] = a.y;
55        } else {
56            const int4 a = __ldg(reinterpret_cast<const int4*>(s));
57            o[0] = a.x;
58            if constexpr (NC > 1) o[1] = a.y;
59            if constexpr (NC > 2) o[2] = a.z;
60            if constexpr (NC > 3) o[3] = a.w;
61            if constexpr (NC > 4) {
62                const int4 e = __ldg(reinterpret_cast<const int4*>(s + 4));
63                o[4] = e.x;
64                if constexpr (NC > 5) o[5] = e.y;
65                if constexpr (NC > 6) o[6] = e.z;
66                if constexpr (NC > 7) o[7] = e.w;
67            }
68        }
69    }
70    __device__ __forceinline__ void dat(const float* s, float (&o)[NC]) const {
71        if constexpr (CP == 2) {
72            const float2 a = __ldg(reinterpret_cast<const float2*>(s));
73            o[0] = a.x;
74            o[1] = a.y;
75        } else {
76            const float4 a = __ldg(reinterpret_cast<const float4*>(s));
77            o[0] = a.x;
78            if constexpr (NC > 1) o[1] = a.y;
79            if constexpr (NC > 2) o[2] = a.z;
80            if constexpr (NC > 3) o[3] = a.w;
81            if constexpr (NC > 4) {
82                const float4 e = __ldg(reinterpret_cast<const float4*>(s + 4));
83                o[4] = e.x;
84                if constexpr (NC > 5) o[5] = e.y;
85                if constexpr (NC > 6) o[6] = e.z;
86                if constexpr (NC > 7) o[7] = e.w;
87            }
88        }
89    }
90    __device__ __forceinline__ void scales(int b, float (&o)[NC]) const {
91        const float* s = d + std::size_t(b) * CP;
92        if constexpr (CP == 2) {
93            const float2 a = *reinterpret_cast<const float2*>(s);
94            o[0] = a.x;
95            o[1] = a.y;
96        } else {
97            const float4 a = *reinterpret_cast<const float4*>(s);
98            o[0] = a.x;
99            if constexpr (NC > 1) o[1] = a.y;
100            if constexpr (NC > 2) o[2] = a.z;
101            if constexpr (NC > 3) o[3] = a.w;
102            if constexpr (NC > 4) {
103                const float4 e = *reinterpret_cast<const float4*>(s + 4);
104                o[4] = e.x;
105                if constexpr (NC > 5) o[5] = e.y;
106                if constexpr (NC > 6) o[6] = e.z;
107                if constexpr (NC > 7) o[7] = e.w;
108            }
109        }
110    }
111};
112
113// One column's plain q8_1 blocks (36 bytes: the half2 scale and sum, then 32 codes) behind Q81Il's interface
114struct Q81One {
115    const uint8_t* __restrict__ x;
116    __device__ __forceinline__ void u(int b, int p, int (&o)[1]) const {
117        o[0] = reinterpret_cast<const int*>(x + std::size_t(b) * 36 + 4)[p];
118    }
119    __device__ __forceinline__ void scales(int b, float (&o)[1]) const {
120        o[0] = __low2float(*reinterpret_cast<const half2*>(x + std::size_t(b) * 36));
121    }
122    static constexpr int QB = 9, DB = 9;   // a 36-byte block in ints / floats
123    __device__ __forceinline__ int qp(int dp) const { return dp; }
124    __device__ __forceinline__ const int* qbase(int b, int p) const { return reinterpret_cast<const int*>(x + std::size_t(b) * 36 + 4) + p; }
125    __device__ __forceinline__ const float* dbase(int b) const { return reinterpret_cast<const float*>(x + std::size_t(b) * 36); }
126    __device__ __forceinline__ void qat(const int* s, int (&o)[1]) const { o[0] = __ldg(s); }
127    __device__ __forceinline__ void dat(const float* s, float (&o)[1]) const { o[0] = __low2float(__ldg(reinterpret_cast<const half2*>(s))); }
128};
129
130// The activations a kernel for NC columns reads: one column's q8_1 blocks, 2+ columns' interleaved copy (nb blocks a
131// column)
132template <int NC, bool PM>
133__device__ __forceinline__ auto q8_1_cols(const void* xq, const float* xd, int nb) {
134    if constexpr (NC == 1) return Q81One{static_cast<const uint8_t*>(xq)};
135    else return Q81Il<NC, PM>{static_cast<const int*>(xq), xd, nb};
136}
137
138// The copy's parts inside the buffer native_q8_1_il_bytes sizes: bm, pm (int32) and d (float).
139struct Q81IlParts {
140    const int* bm;
141    const int* pm;
142    const float* d;
143};
144inline Q81IlParts q8_1_il_parts(const void* x_il, int n_in, int ncols) {
145    const std::size_t words = std::size_t(n_in / 32) * 8 * std::size_t(native_q8_1_il_cp(ncols));
146    const int* bm = static_cast<const int*>(x_il);
147    return {bm, bm + words, reinterpret_cast<const float*>(bm + 2 * words)};
148}
149
150}  // namespace
151}  // namespace strata::kernels