// metal/kernels/units.metal - the dot of one unit with the window's tokens (after quant.metal). // // X::load(xrow, u, xs, xsum) prepares unit u's x slice of one token (xrow = the token's row start), once per unit // and shared by every weight row the lane reads: the UNIT x values in the order the decoder produces weights, and // up to two partial sums (for the formats with an offset or a min). Where the codes are read with the mask trick // (an AND keeps a field in place, so the code arrives multiplied by 2^k), the x values are pre-scaled by 2^-k here: // exact in float. // the threadgroup-memory form (x staged once per threadgroup; the same values) static inline void load16(threadgroup const float* p, thread float* xs) { threadgroup const float4* v = (threadgroup const float4*)p; for (int j = 0; j < 4; ++j) { const float4 a = v[j]; xs[4 * j] = a.x; xs[4 * j + 1] = a.y; xs[4 * j + 2] = a.z; xs[4 * j + 3] = a.w; } } static inline void load16(device const float* p, thread float* xs) { device const float4* v = (device const float4*)p; for (int j = 0; j < 4; ++j) { const float4 a = v[j]; xs[4 * j] = a.x; xs[4 * j + 1] = a.y; xs[4 * j + 2] = a.z; xs[4 * j + 3] = a.w; } } // ---- prepared x (xprep): XP holds, per token row of n floats, the kinds // XK_RAW x (every format without the mask trick) // XK_Q2 x * 4^-(i % 8) (Q2_0: 2-bit fields in place in a 16-bit word) // XK_P4 x * 2^-(8 (i % 4)) (Q4_K, Q5_K: byte fields in place in a 32-bit word) // XK_P2 x * 2^-(8 (i % 2)) (Q6_K, Q3_K: byte fields in place in a 16-bit word) // and S16 = the sum of every 16 consecutive raw x (the formats with an offset or a min). #define XK_RAW 0 #define XK_Q2 1 #define XK_P4 2 #define XK_P2 3 template struct XKind { enum { K = XK_RAW }; }; template <> struct XKind { enum { K = XK_Q2 }; }; template <> struct XKind { enum { K = XK_P4 }; }; template <> struct XKind { enum { K = XK_P4 }; }; template <> struct XKind { enum { K = XK_P2 }; }; template <> struct XKind { enum { K = XK_P2 }; }; // X::load(xp, s16, u, xs, xsum): unit u's prepared x (xp = the token's row of the format's kind, s16 = its sums) template struct X { template static inline void load(P xp, P, int u, thread float* xs, thread float* xsum) { constexpr int UNIT = fmt::UNIT; for (int k = 0; k < UNIT; k += 16) load16(xp + u * UNIT + k, xs + k); xsum[0] = xsum[1] = 0.0f; } }; template <> struct X { template static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) { load16(xp + 16 * u, xs); xsum[0] = s16[u]; xsum[1] = 0.0f; } }; // Q4_K / Q5_K: unit (block b, chunk c, half m): run0 at 256b + 64c + 16m, run1 = run0 + 32 template <> struct X { template static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) { const int p0 = 256 * (u >> 3) + 64 * ((u >> 1) & 3) + 16 * (u & 1); load16(xp + p0, xs); load16(xp + p0 + 32, xs + 16); xsum[0] = s16[p0 >> 4]; xsum[1] = s16[(p0 >> 4) + 2]; } }; template <> struct X { template static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) { X::load(xp, s16, u, xs, xsum); } }; // Q6_K: unit (block b, half n, m, p): run0 at 256b + 128n + 32p + 16m, run1 = run0 + 64 template <> struct X { template static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) { const int p0 = 256 * (u >> 3) + 128 * ((u >> 2) & 1) + 32 * (u & 1) + 16 * ((u >> 1) & 1); load16(xp + p0, xs); load16(xp + p0 + 64, xs + 16); xsum[0] = s16[p0 >> 4]; xsum[1] = s16[(p0 >> 4) + 4]; } }; // Q5_1: 32 contiguous raw x, the two per-16 sums formed from them (the GEMVs that read Q5_1 - the routed down - get // h without prepared sums) template <> struct X { template static inline void load(P xp, P, int u, thread float* xs, thread float* xsum) { load16(xp + 32 * u, xs); load16(xp + 32 * u + 16, xs + 16); float a = 0.0f, b = 0.0f; for (int j = 0; j < 16; ++j) { a += xs[j]; b += xs[16 + j]; } xsum[0] = a; xsum[1] = b; } }; // Q3_K: 32 contiguous, sums per 16 template <> struct X { template static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) { load16(xp + 32 * u, xs); load16(xp + 32 * u + 16, xs + 16); xsum[0] = s16[2 * u]; xsum[1] = s16[2 * u + 1]; } }; // xprep: one thread per 16 consecutive values of one token row; XP = [4 kinds][n_tok][n] then S16 [n_tok][n / 16] kernel void xprep(device const float* x [[buffer(0)]], device float* xp [[buffer(1)]], constant uint& n [[buffer(2)]], constant uint& n_tok [[buffer(3)]], uint gid [[thread_position_in_grid]]) { const uint per = n / 16; if (gid >= per * n_tok) return; const uint t = gid / per, i = gid % per; device const float* src = x + t * n + 16 * i; const uint plane = n_tok * n; device float* raw = xp + t * n + 16 * i; float v[16]; load16(src, v); float s0 = 0, s1 = 0; for (int j = 0; j < 16; j += 2) { s0 += v[j]; s1 += v[j + 1]; } const float q2s[8] = {1.0f, 0.25f, 0.0625f, 0.015625f, 0.00390625f, 0.0009765625f, 0.000244140625f, 0.00006103515625f}; const float p4s[4] = {1.0f, 0.00390625f, 1.52587890625e-05f, 5.9604644775390625e-08f}; for (int j = 0; j < 16; ++j) { raw[j] = v[j]; raw[plane + j] = v[j] * q2s[j & 7]; raw[2 * plane + j] = v[j] * p4s[j & 3]; raw[3 * plane + j] = (j & 1) ? v[j] * 0.00390625f : v[j]; if (xh16) put_x16(xp, plane, t * n + 16 * i + j, j, v[j]); } xp[4 * plane + t * per + i] = s0 + s1; } // ---- decoders: Dec::run(row, u, q, sc) decodes unit u of a row into q[UNIT] and sc = (d1, d2, m1, m2), so that // for any token: dot = d1 * sum_{j < UNIT/2} q[j] xs[j] + d2 * sum_{j >= UNIT/2} q[j] xs[j] - (m1 xsum0 + m2 xsum1) // with xs, xsum from X::load. A unit's weights are decoded once per row and applied to every token. template struct Dec { static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint* tab) { constexpr int UNIT = fmt::UNIT, UPB = fmt::QK / UNIT; const float s = deq(row + (u / UPB) * fmt::BB, u % UPB, q, tab); sc = float4(s, s, 0.0f, 0.0f); } }; template <> struct Dec { static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 2) * 18; device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3)); const ushort a = p[0], c = p[1]; for (int j = 0; j < 8; ++j) { q[j] = float(a & ushort(3u << (2 * j))); q[8 + j] = float(c & ushort(3u << (2 * j))); } const float d = rdh(b); sc = float4(d, d, d, 0.0f); } }; // Q2_0 with raw x: q = c - 1 (shift, and, convert, subtract), one scale template <> struct Dec { static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 2) * 18; device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3)); const ushort a = p[0], c = p[1]; for (int j = 0; j < 8; ++j) { q[j] = float((a >> (2 * j)) & 3) - 1.0f; q[8 + j] = float((c >> (2 * j)) & 3) - 1.0f; } const float d = rdh(b); sc = float4(d, d, 0.0f, 0.0f); } }; static inline void scale_min_k4(int j, device const uchar* q, thread float& d, thread float& m) { if (j < 4) { d = float(q[j] & 63); m = float(q[j + 4] & 63); } else { d = float((q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4)); m = float((q[j + 4] >> 4) | ((q[j] >> 6) << 4)); } } static inline void bytes4(uint w, thread float* q) { q[0] = float(w & 0xFFu); q[1] = float(w & 0xFF00u); q[2] = float(w & 0xFF0000u); q[3] = float(w & 0xFF000000u); } static inline void bytes2(ushort w, thread float* q) { q[0] = float(w & 0xFF); q[1] = float(w & 0xFF00); } template <> struct Dec { // w = d q + m: sum = d (q . x) + m (sum x) static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint* tab) { device const uchar* b = row + u * 24; const float d = deq(b, 0, q, tab), m = rdh(b + 2); sc = float4(d, d, -m, -m); } }; template <> struct Dec { static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 3) * 144; const int c = (u >> 1) & 3, m = u & 1; const float d = rdh(b), dm = rdh(b + 2); float s1, m1, s2, m2; scale_min_k4(2 * c, b + 4, s1, m1); scale_min_k4(2 * c + 1, b + 4, s2, m2); device const uint* qp = (device const uint*)(b + 16 + 32 * c + 16 * m); for (int w = 0; w < 4; ++w) { const uint v = qp[w]; bytes4(v & 0x0F0F0F0Fu, q + 4 * w); bytes4((v >> 4) & 0x0F0F0F0Fu, q + 16 + 4 * w); } sc = float4(d * s1, d * s2, dm * m1, dm * m2); } }; template <> struct Dec { static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 3) * 176; const int c = (u >> 1) & 3, m = u & 1; const float d = rdh(b), dm = rdh(b + 2); float s1, m1, s2, m2; scale_min_k4(2 * c, b + 4, s1, m1); scale_min_k4(2 * c + 1, b + 4, s2, m2); device const uint* qp = (device const uint*)(b + 48 + 32 * c + 16 * m); device const uint* hp = (device const uint*)(b + 16 + 16 * m); for (int w = 0; w < 4; ++w) { const uint v = qp[w], h = hp[w]; bytes4((v & 0x0F0F0F0Fu) | (((h >> (2 * c)) & 0x01010101u) << 4), q + 4 * w); bytes4(((v >> 4) & 0x0F0F0F0Fu) | (((h >> (2 * c + 1)) & 0x01010101u) << 4), q + 16 + 4 * w); } sc = float4(d * s1, d * s2, dm * m1, dm * m2); } }; template <> struct Dec { static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 3) * 210; const int n = (u >> 2) & 1, m = (u >> 1) & 1, p = u & 1; device const ushort* ql = (device const ushort*)(b + 64 * n + 32 * p + 16 * m); device const ushort* qh = (device const ushort*)(b + 128 + 32 * n + 16 * m); device const char* scs = (device const char*)(b + 192); const float d = rdh(b + 208); for (int w = 0; w < 8; ++w) { const ushort l = ql[w], h = qh[w]; bytes2((l & 0x0F0F) | (((h >> (2 * p)) & 0x0303) << 4), q + 2 * w); bytes2(((l >> 4) & 0x0F0F) | (((h >> (2 * p + 4)) & 0x0303) << 4), q + 16 + 2 * w); } const float d1 = d * float(scs[8 * n + m + 2 * p]), d2 = d * float(scs[8 * n + m + 2 * p + 4]); sc = float4(d1, d2, 32.0f * d1, 32.0f * d2); } }; template <> struct Dec { static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 3) * 110; const int n = (u >> 2) & 1, j = u & 3; // qs and hmask as packed (2-byte aligned) vectors: blocks are 110 bytes, so odd blocks are only 2-aligned device const packed_ushort4* qv = (device const packed_ushort4*)(b + 32 + 32 * n); device const packed_ushort4* hv = (device const packed_ushort4*)b; ushort qq[16], hm[16]; for (int v = 0; v < 4; ++v) { const ushort4 a4 = ushort4(qv[v]), h4 = ushort4(hv[v]); qq[4 * v] = a4.x; qq[4 * v + 1] = a4.y; qq[4 * v + 2] = a4.z; qq[4 * v + 3] = a4.w; hm[4 * v] = h4.x; hm[4 * v + 1] = h4.y; hm[4 * v + 2] = h4.z; hm[4 * v + 3] = h4.w; } device const uchar* s = b + 96; const int i1 = 8 * n + 2 * j, i2 = i1 + 1; const int l1 = (i1 < 8 ? s[i1] & 0xF : s[i1 - 8] >> 4) | (((s[8 + (i1 & 3)] >> (2 * (i1 >> 2))) & 3) << 4); const int l2 = (i2 < 8 ? s[i2] & 0xF : s[i2 - 8] >> 4) | (((s[8 + (i2 & 3)] >> (2 * (i2 >> 2))) & 3) << 4); const float d = rdh(b + 108); const int hs = 4 * n + j; for (int w = 0; w < 8; ++w) { bytes2(((qq[w] >> (2 * j)) & 0x0303) | (((hm[w] >> hs) & 0x0101) << 2), q + 2 * w); bytes2(((qq[w + 8] >> (2 * j)) & 0x0303) | (((hm[w + 8] >> hs) & 0x0101) << 2), q + 16 + 2 * w); } const float d1 = d * float(l1 - 32), d2 = d * float(l2 - 32); sc = float4(d1, d2, 4.0f * d1, 4.0f * d2); } }; template <> struct Dec { // 16 floats a unit (the Unsloth files' router / alpha / beta / inject rows) static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) { device const float4* p = (device const float4*)(row + 64 * u); for (int k = 0; k < 4; ++k) { const float4 v = p[k]; q[4 * k] = v.x; q[4 * k + 1] = v.y; q[4 * k + 2] = v.z; q[4 * k + 3] = v.w; } sc = float4(1.0f, 1.0f, 0.0f, 0.0f); } }; template <> struct Dec { static inline void run(device const uchar* row, int u, thread float* q, thread float4& sc, threadgroup const uint*) { device const uint4* p = (device const uint4*)(row + 32 * u); const uint4 a = p[0], c = p[1]; const uint v[8] = {a.x, a.y, a.z, a.w, c.x, c.y, c.z, c.w}; for (int k = 0; k < 8; ++k) { q[2 * k] = as_type(v[k] << 16); q[2 * k + 1] = as_type(v[k] & 0xFFFF0000u); } sc = float4(1.0f, 1.0f, 0.0f, 0.0f); } }; // acc += unit dot of decoded q / sc with one token's prepared slice template static inline float unit_combine(thread const float* q, float4 sc, thread const float* xs, thread const float* xsum) { constexpr int H = UNIT / 2; float s0 = 0, s1 = 0, s2 = 0, s3 = 0; for (int j = 0; j < H; j += 2) { s0 = fma(q[j], xs[j], s0); s1 = fma(q[j + 1], xs[j + 1], s1); s2 = fma(q[H + j], xs[H + j], s2); s3 = fma(q[H + j + 1], xs[H + j + 1], s3); } return sc.x * (s0 + s1) + sc.y * (s2 + s3) - (sc.z * xsum[0] + sc.w * xsum[1]); } #ifdef STRATA_METAL_F16 // window.cpp compiles it in only under STRATA_METAL_F16=1 // ---- the fp16 window path (opt-in, STRATA_METAL_F16): the same decode into half, the window's x as a half plane, the // products of a half unit (16 weights, 8 for Q2_0) summed in half, the scales and the sum across units in float. // Unguarded: a product or a half unit's sum past 65504 is inf (IQ4_XS from |x| ~516, Q6_K ~1040, Q5_K ~2113, Q2_0 // ~21835; up to 8x lower for same-sign runs), and |x| below ~2^-6 (2^-8 kinds) or ~2^-8 (Q2_0's 4^-3) is subnormal. static inline void bytes2h(ushort w, thread half* q) { q[0] = half(w & 0xFF); q[1] = half(w & 0xFF00); } template struct Dec16; template <> struct Dec16 { static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 3) * 110; const int n = (u >> 2) & 1, j = u & 3; device const packed_ushort4* qv = (device const packed_ushort4*)(b + 32 + 32 * n); device const packed_ushort4* hv = (device const packed_ushort4*)b; ushort qq[16], hm[16]; for (int v = 0; v < 4; ++v) { const ushort4 a4 = ushort4(qv[v]), h4 = ushort4(hv[v]); qq[4 * v] = a4.x; qq[4 * v + 1] = a4.y; qq[4 * v + 2] = a4.z; qq[4 * v + 3] = a4.w; hm[4 * v] = h4.x; hm[4 * v + 1] = h4.y; hm[4 * v + 2] = h4.z; hm[4 * v + 3] = h4.w; } device const uchar* s = b + 96; const int i1 = 8 * n + 2 * j, i2 = i1 + 1; const int l1 = (i1 < 8 ? s[i1] & 0xF : s[i1 - 8] >> 4) | (((s[8 + (i1 & 3)] >> (2 * (i1 >> 2))) & 3) << 4); const int l2 = (i2 < 8 ? s[i2] & 0xF : s[i2 - 8] >> 4) | (((s[8 + (i2 & 3)] >> (2 * (i2 >> 2))) & 3) << 4); const float d = rdh(b + 108); const int hs = 4 * n + j; for (int w = 0; w < 8; ++w) { bytes2h(((qq[w] >> (2 * j)) & 0x0303) | (((hm[w] >> hs) & 0x0101) << 2), q + 2 * w); bytes2h(((qq[w + 8] >> (2 * j)) & 0x0303) | (((hm[w + 8] >> hs) & 0x0101) << 2), q + 16 + 2 * w); } const float d1 = d * float(l1 - 32), d2 = d * float(l2 - 32); sc = float4(d1, d2, 4.0f * d1, 4.0f * d2); } }; // Q3_K's prepared x (32 contiguous of the 2^-8 plane, sums per 16 in float) from the half plane template struct X16; template <> struct X16 { static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) { device const half4* v = (device const half4*)(xp + 32 * u); for (int k = 0; k < 8; ++k) { const half4 a = v[k]; xs[4 * k] = a.x; xs[4 * k + 1] = a.y; xs[4 * k + 2] = a.z; xs[4 * k + 3] = a.w; } xsum[0] = s16[2 * u]; xsum[1] = s16[2 * u + 1]; } }; template static inline float unit_combine16(thread const half* q, float4 sc, thread const half* xs, thread const float* xsum) { constexpr int H = UNIT / 2; half s0 = 0, s1 = 0, s2 = 0, s3 = 0; for (int j = 0; j < H; j += 2) { s0 = fma(q[j], xs[j], s0); s1 = fma(q[j + 1], xs[j + 1], s1); s2 = fma(q[H + j], xs[H + j], s2); s3 = fma(q[H + j + 1], xs[H + j + 1], s3); } return sc.x * float(s0 + s1) + sc.y * float(s2 + s3) - (sc.z * xsum[0] + sc.w * xsum[1]); } // a 32-bit word of nibble bytes (the K-quants' mask trick) split into two 16-bit halves: the bytes come out with the // factors 1, 2^8, 1, 2^8 - the 2^-8 plane, which half holds (values <= 31 * 256) static inline void bytes4h(uint w, thread half* q) { bytes2h(ushort(w & 0xFFFFu), q); bytes2h(ushort(w >> 16), q + 2); } template <> struct Dec16 { static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 3) * 144; const int c = (u >> 1) & 3, m = u & 1; const float d = rdh(b), dm = rdh(b + 2); float s1, m1, s2, m2; scale_min_k4(2 * c, b + 4, s1, m1); scale_min_k4(2 * c + 1, b + 4, s2, m2); device const uint* qp = (device const uint*)(b + 16 + 32 * c + 16 * m); for (int w = 0; w < 4; ++w) { const uint v = qp[w]; bytes4h(v & 0x0F0F0F0Fu, q + 4 * w); bytes4h((v >> 4) & 0x0F0F0F0Fu, q + 16 + 4 * w); } sc = float4(d * s1, d * s2, dm * m1, dm * m2); } }; template <> struct Dec16 { static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 3) * 176; const int c = (u >> 1) & 3, m = u & 1; const float d = rdh(b), dm = rdh(b + 2); float s1, m1, s2, m2; scale_min_k4(2 * c, b + 4, s1, m1); scale_min_k4(2 * c + 1, b + 4, s2, m2); device const uint* qp = (device const uint*)(b + 48 + 32 * c + 16 * m); device const uint* hp = (device const uint*)(b + 16 + 16 * m); for (int w = 0; w < 4; ++w) { const uint v = qp[w], h = hp[w]; bytes4h((v & 0x0F0F0F0Fu) | (((h >> (2 * c)) & 0x01010101u) << 4), q + 4 * w); bytes4h(((v >> 4) & 0x0F0F0F0Fu) | (((h >> (2 * c + 1)) & 0x01010101u) << 4), q + 16 + 4 * w); } sc = float4(d * s1, d * s2, dm * m1, dm * m2); } }; template <> struct Dec16 { static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 3) * 210; const int n = (u >> 2) & 1, m = (u >> 1) & 1, p = u & 1; device const ushort* ql = (device const ushort*)(b + 64 * n + 32 * p + 16 * m); device const ushort* qh = (device const ushort*)(b + 128 + 32 * n + 16 * m); device const char* scs = (device const char*)(b + 192); const float d = rdh(b + 208); for (int w = 0; w < 8; ++w) { const ushort l = ql[w], h = qh[w]; bytes2h((l & 0x0F0F) | (((h >> (2 * p)) & 0x0303) << 4), q + 2 * w); bytes2h(((l >> 4) & 0x0F0F) | (((h >> (2 * p + 4)) & 0x0303) << 4), q + 16 + 2 * w); } const float d1 = d * float(scs[8 * n + m + 2 * p]), d2 = d * float(scs[8 * n + m + 2 * p + 4]); sc = float4(d1, d2, 32.0f * d1, 32.0f * d2); } }; // IQ4_XS: the grid's values are half already (load_tab), no conversion template <> struct Dec16 { static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint* tab) { device const uchar* b = row + (u >> 3) * 136; const int ui = u & 7; device const ushort* p = (device const ushort*)(b + 8 + 16 * ui); for (int j = 0; j < 8; ++j) { const ushort v = p[j]; const half2 w0 = as_type(tab[v & 255]), w1 = as_type(tab[v >> 8]); q[2 * j] = w0.x; q[2 * j + 16] = w0.y; q[2 * j + 1] = w1.x; q[2 * j + 17] = w1.y; } const uint sh = *(device const ushort*)(b + 2); const int ls = int((b[4 + (ui >> 1)] >> (4 * (ui & 1))) & 15) | int(((sh >> (2 * ui)) & 3) << 4); const float s = rdh(b) * float(ls - 32); sc = float4(s, s, 0.0f, 0.0f); } }; // Q2_0 (units of 16): each 16-bit word split into bytes, the code masked in place within its byte - factors 4^(i % 4), // so the half plane is x * 4^-(i % 4) (no smaller than 1/64) template <> struct Dec16 { static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 2) * 18; device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3)); const ushort a = p[0], c = p[1]; const ushort a1 = a >> 8, c1 = c >> 8; for (int j = 0; j < 4; ++j) { const ushort mk = ushort(3u << (2 * j)); q[j] = half(a & mk); q[4 + j] = half(a1 & mk); q[8 + j] = half(c & mk); q[12 + j] = half(c1 & mk); } const float d = rdh(b); sc = float4(d, d, d, 0.0f); } }; static inline void load16h(device const half* p, thread half* xs) { device const half4* v = (device const half4*)p; for (int k = 0; k < 4; ++k) { const half4 a = v[k]; xs[4 * k] = a.x; xs[4 * k + 1] = a.y; xs[4 * k + 2] = a.z; xs[4 * k + 3] = a.w; } } template <> struct X16 { static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) { const int p0 = 256 * (u >> 3) + 64 * ((u >> 1) & 3) + 16 * (u & 1); load16h(xp + p0, xs); load16h(xp + p0 + 32, xs + 16); xsum[0] = s16[p0 >> 4]; xsum[1] = s16[(p0 >> 4) + 2]; } }; template <> struct X16 { static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) { X16::load(xp, s16, u, xs, xsum); } }; template <> struct X16 { static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) { const int p0 = 256 * (u >> 3) + 128 * ((u >> 2) & 1) + 32 * (u & 1) + 16 * ((u >> 1) & 1); load16h(xp + p0, xs); load16h(xp + p0 + 64, xs + 16); xsum[0] = s16[p0 >> 4]; xsum[1] = s16[(p0 >> 4) + 4]; } }; template <> struct X16 { static inline void load(device const half* xp, device const float*, int u, thread half* xs, thread float* xsum) { load16h(xp + 32 * u, xs); load16h(xp + 32 * u + 16, xs + 16); xsum[0] = xsum[1] = 0.0f; } }; template <> struct X16 { static inline void load(device const half* xp, device const float* s16, int u, thread half* xs, thread float* xsum) { load16h(xp + 16 * u, xs); xsum[0] = s16[u]; xsum[1] = 0.0f; } }; // Q2_0 with raw x (the routed down over h): q = code - 1 in half template <> struct Dec16 { static inline void run(device const uchar* row, int u, thread half* q, thread float4& sc, threadgroup const uint*) { device const uchar* b = row + (u >> 2) * 18; device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3)); const ushort a = p[0], c = p[1]; for (int j = 0; j < 8; ++j) { q[j] = half((a >> (2 * j)) & 3) - 1.0h; q[8 + j] = half((c >> (2 * j)) & 3) - 1.0h; } const float d = rdh(b); sc = float4(d, d, 0.0f, 0.0f); } }; template <> struct X16 { static inline void load(device const half* xp, device const float*, int u, thread half* xs, thread float* xsum) { load16h(xp + 16 * u, xs); xsum[0] = xsum[1] = 0.0f; } }; #endif // STRATA_METAL_F16