Merge f-mac-kern b6480437 (IQ4_XS expert kernels: IQ3_S on Metal)

This commit is contained in:
Niko1221 committed 2026-10-12 19:42:48 +02:00
commit b6a2dacd86
8 files changed
+130 -15

No files matched your search

+75 -3
View File
@@ -16,6 +16,10 @@ struct GemvArgs {
};
constant int NTOK [[function_constant(0)]]; // tokens of the window (1..8): the token loops are specialised
// a bench probe (function constant 9): 1 = tokens past the first reuse token 0's x (no load; wrong values),
// 2 = tokens past the first skip the combine (wrong values)
constant int GEMV_PROBE [[function_constant(9)]];
constant int gemv_probe = is_function_constant_defined(GEMV_PROBE) ? GEMV_PROBE : 0;
template <int F, int LPR, int NR, int SG, int KS>
kernel void gemv(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]],
@@ -39,14 +43,29 @@ kernel void gemv(device const uchar* W [[buffer(0)]], device const float* xp [[b
device const uchar* rp[NR];
for (int r = 0; r < NR; ++r) rp[r] = W + (ulong)min(row0 + r, int(a.n_out) - 1) * a.row_bytes;
const int s16n = int(a.n_in) / 16;
if (gemv_probe == 3) {
// rows outer, tokens inner: one row's decoded unit in registers at a time (x re-read per row from L1)
for (int u = u0 + li; u < u1; u += LPR)
for (int r = 0; r < NR; ++r) {
float q[UNIT];
float4 sc;
Dec<F>::run(rp[r], u, q, sc, tab);
for (int t = 0; t < NTOK; ++t) {
float xs[UNIT], xsum[2];
X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
acc[r][t] += unit_combine<UNIT>(q, sc, xs, xsum);
}
}
} else
for (int u = u0 + li; u < u1; u += LPR) {
float q[NR][UNIT];
float4 sc[NR];
for (int r = 0; r < NR; ++r) Dec<F>::run(rp[r], u, q[r], sc[r], tab);
float xs[UNIT], xsum[2];
for (int t = 0; t < NTOK; ++t) {
float xs[UNIT], xsum[2];
X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);
if (gemv_probe != 1 || t == 0) X<F>::load(xp + t * int(a.x_stride), s16 + t * s16n, u, xs, xsum);
if (gemv_probe != 2 || t == 0)
for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);
}
}
constexpr int RPT = RS * RG * NR;
@@ -189,3 +208,56 @@ kernel void gemv_cat(device const uchar* W [[buffer(0)]], device const float* xp
#define GVC_(F, L, R, S) template [[host_name("gemvc_" #F "_" #L "_" #R "_" #S)]] kernel decltype(gemv_cat<F, L, R, S>) gemv_cat<F, L, R, S>;
#define GVC(F, L, R, S) GVC_(F, L, R, S)
GVC(T_BF16, 32, 1, 4) GVC(T_BF16, 32, 1, 8) GVC(T_BF16, 16, 1, 4) GVC(T_BF16, 32, 2, 4)
// gemv_x: gemv (KS = 1) with the window's prepared x (the format's plane, NTOK rows, and its per-16 sums) staged in
// threadgroup memory once per threadgroup, so the per-unit x reads of every token come from threadgroup memory
// instead of L1 (on the M4 the x traffic through L1 - 4 B a weight a token, shared by the rows in a simdgroup - is
// what a window token costs). Same tile meaning and the same arithmetic as gemv_<F>_<LPR>_<NR>_<SG>_1: bitwise.
// Dynamic threadgroup memory 0: 4 * NTOK * (n_in + n_in / 16) bytes.
template <int F, int LPR, int NR, int SG>
kernel void gemv_x(device const uchar* W [[buffer(0)]], device const float* xp [[buffer(1)]],
device const float* s16 [[buffer(2)]], device float* y [[buffer(3)]], constant GemvArgs& a [[buffer(4)]],
threadgroup float* xt [[threadgroup(0)]],
uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]],
ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) {
constexpr int UNIT = fmt<F>::UNIT, RG = 32 / LPR;
threadgroup uint tab[fmt<F>::TAB];
load_tab<F>(tab, tid, SG * 32);
const int n = int(a.n_in), s16n = n / 16;
threadgroup float* st = xt + NTOK * n;
for (int t = 0; t < NTOK; ++t) {
device const float4* src = (device const float4*)(xp + t * int(a.x_stride));
threadgroup float4* dst = (threadgroup float4*)(xt + t * n);
for (int i = tid; i < n / 4; i += SG * 32) dst[i] = src[i];
for (int i = tid; i < s16n; i += SG * 32) st[t * s16n + i] = s16[t * s16n + i];
}
threadgroup_barrier(mem_flags::mem_threadgroup);
const int li = lane % LPR;
const int row0 = int(tg.x) * SG * RG * NR + (sg * RG + lane / LPR) * NR;
const int U = n / UNIT;
float acc[NR][8];
for (int r = 0; r < NR; ++r)
for (int t = 0; t < NTOK; ++t) acc[r][t] = 0.0f;
device const uchar* rp[NR];
for (int r = 0; r < NR; ++r) rp[r] = W + (ulong)min(row0 + r, int(a.n_out) - 1) * a.row_bytes;
for (int u = li; u < U; u += LPR) {
float q[NR][UNIT];
float4 sc[NR];
for (int r = 0; r < NR; ++r) Dec<F>::run(rp[r], u, q[r], sc[r], tab);
for (int t = 0; t < NTOK; ++t) {
float xs[UNIT], xsum[2];
X<F>::load((threadgroup const float*)(xt + t * n), (threadgroup const float*)(st + t * s16n), u, xs, xsum);
for (int r = 0; r < NR; ++r) acc[r][t] += unit_combine<UNIT>(q[r], sc[r], xs, xsum);
}
}
for (int r = 0; r < NR; ++r)
for (int t = 0; t < NTOK; ++t) {
const float v = lane_sum<LPR>(acc[r][t]);
if (li == 0 && row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = v;
}
}
#define GVX(F, L, R, S) template [[host_name("gemvx_" #F "_" #L "_" #R "_" #S)]] kernel decltype(gemv_x<F, L, R, S>) gemv_x<F, L, R, S>;
#define GVX_SET(F) GVX(F, 32, 1, 4) GVX(F, 32, 2, 4) GVX(F, 16, 1, 4) GVX(F, 16, 2, 4) GVX(F, 8, 1, 4) GVX(F, 8, 2, 4) \
GVX(F, 4, 2, 4) GVX(F, 16, 2, 8) GVX(F, 8, 2, 8) GVX(F, 16, 4, 4) GVX(F, 32, 2, 8) GVX(F, 2, 2, 4)
GVX_SET(T_BF16) GVX_SET(T_Q8_0) GVX_SET(T_Q4_0) GVX_SET(T_Q5_0) GVX_SET(T_IQ4_NL) GVX_SET(T_IQ4_XS) GVX_SET(T_Q2_0)
GVX_SET(T_IQ3_S) GVX_SET(T_Q3_K) GVX_SET(T_Q4_K) GVX_SET(T_Q5_K) GVX_SET(T_Q6_K)
+1
View File
@@ -322,6 +322,7 @@ GU_SET(T_IQ2_XXS)
GU_SET(T_IQ2_XS)
GU_SET(T_IQ2_S)
GU_SET(T_IQ3_S)
GU_SET(T_IQ4_XS) // the IQ3_S pack's gate/up in one layer
DN_SET(T_Q2_0)
DN_SET(T_Q2_0R)
DN_SET(T_IQ4_NL)
+20 -1
View File
@@ -382,6 +382,9 @@ kernel void mm_down2(device const uchar* W [[buffer(0)]], device const half* HS
#define MMDN2(F, M, N, K, S) MMDN2_(F, M, N, K, S)
#define MM_GU2_SET(F) MMGU2(F, 32, 64, 32, 2) MMGU2(F, 32, 32, 32, 2) MMGU2(F, 32, 64, 16, 2) MMGU2(F, 64, 32, 16, 2) MMGU2(F, 16, 32, 32, 2) MMGU2(F, 16, 64, 16, 2) MMGU2(F, 16, 128, 32, 4) MMGU2(F, 32, 64, 16, 4) MMGU2(F, 16, 32, 32, 1) MMGU2(F, 16, 64, 32, 4) MMGU2(F, 16, 64, 64, 2) MMGU2(F, 32, 64, 64, 4) MMGU2(F, 64, 64, 64, 4) MMGU2(F, 16, 32, 64, 2) MMGU2(F, 32, 64, 32, 4) MMGU2(F, 64, 64, 32, 4) MMGU2(F, 16, 64, 32, 2)
#define MM_DN2_SET(F) MMDN2(F, 32, 64, 32, 2) MMDN2(F, 32, 32, 32, 2) MMDN2(F, 32, 64, 16, 2) MMDN2(F, 64, 32, 16, 2) MMDN2(F, 16, 32, 32, 2) MMDN2(F, 16, 64, 16, 2) MMDN2(F, 16, 128, 32, 4) MMDN2(F, 32, 64, 16, 4) MMDN2(F, 16, 32, 32, 1) MMDN2(F, 16, 64, 32, 4) MMDN2(F, 16, 64, 64, 2) MMDN2(F, 32, 64, 64, 4) MMDN2(F, 64, 64, 64, 4) MMDN2(F, 16, 32, 64, 2) MMDN2(F, 32, 64, 32, 4) MMDN2(F, 64, 64, 32, 4) MMDN2(F, 16, 64, 32, 2)
// IQ4_XS (UNIT 32): the K chunk must be >= 32
MMGU2(T_IQ4_XS, 16, 64, 32, 2) MMGU2(T_IQ4_XS, 16, 64, 64, 2) MMGU2(T_IQ4_XS, 32, 64, 32, 2) MMGU2(T_IQ4_XS, 16, 32, 32, 2)
MMGU2(T_IQ4_XS, 64, 64, 32, 4)
MM_GU2_SET(T_Q2_0) MM_GU2_SET(T_IQ3_XXS) MM_GU2_SET(T_IQ2_XXS) MM_GU2_SET(T_IQ2_XS) MM_GU2_SET(T_IQ2_S) MM_GU2_SET(T_IQ3_S)
MM_DN2_SET(T_Q2_0)
MMDN2(T_IQ4_NL, 16, 64, 32, 2) MMDN2(T_IQ4_NL, 16, 64, 64, 2) MMDN2(T_IQ4_NL, 16, 32, 32, 2) MMDN2(T_IQ4_NL, 32, 32, 32, 2) MMDN2(T_IQ4_NL, 16, 128, 32, 4)
@@ -474,6 +477,22 @@ template <> struct W4<T_IQ3_S> {
return w4_signed(tab[i], hh ? s >> 4 : s, sc);
}
};
template <> struct W4<T_IQ4_XS> { // 256 a block: d, scales_h, scales_l[4], qs[128]; 32-groups as IQ4_NL's
static inline half4 run(device const uchar* row, int k, threadgroup const uint* tab) {
device const uchar* b = row + (k >> 8) * 136;
const int j = k & 255, ib = j >> 5, o = j & 15, hi = (j >> 4) & 1;
const uint sh = *(device const ushort*)(b + 2);
const int ls = int((b[4 + (ib >> 1)] >> (4 * (ib & 1))) & 15) | int(((sh >> (2 * ib)) & 3) << 4);
const float s = rdh(b) * float(ls - 32);
device const uchar* qs = b + 8 + 16 * ib;
float4 q;
for (int c = 0; c < 4; ++c) {
const half2 w = as_type<half2>(tab[qs[o + c]]);
q[c] = float(hi ? w.y : w.x);
}
return half4(q * s);
}
};
template <> struct W4<T_IQ4_NL> { // 32 a block: d, 16 bytes; low nibbles are elements 0..15, high 16..31
static inline half4 run(device const uchar* row, int k, threadgroup const uint* tab) {
device const uchar* b = row + (k >> 5) * 18;
@@ -702,5 +721,5 @@ kernel void mm_down3(device const uchar* W [[buffer(0)]], device const half* HS
MMGU3(F, 16, 32, 16, 2) MMGU3(F, 32, 32, 32, 2) MMGU3(F, 16, 64, 32, 1) MMGU3(F, 16, 64, 32, 4) MMGU3(F, 16, 32, 16, 4) MMGU3(F, 16, 32, 32, 5) MMGU3(F, 16, 32, 16, 5) MMGU3(F, 16, 16, 32, 4) MMGU3(F, 16, 16, 32, 8) MMGU3(F, 16, 16, 16, 8)
#define MM3_DN(F) MMDN3(F, 16, 64, 32, 2) MMDN3(F, 16, 32, 32, 4) MMDN3(F, 32, 64, 32, 2) MMDN3(F, 16, 64, 64, 2) \
MMDN3(F, 16, 32, 16, 2) MMDN3(F, 32, 32, 32, 2) MMDN3(F, 16, 64, 32, 1) MMDN3(F, 16, 64, 32, 4) MMDN3(F, 16, 64, 16, 2) MMDN3(F, 16, 32, 16, 4) MMDN3(F, 16, 64, 32, 5) MMDN3(F, 16, 64, 16, 4)
MM3_GU(T_Q2_0) MM3_GU(T_IQ3_XXS) MM3_GU(T_IQ2_XXS) MM3_GU(T_IQ2_XS) MM3_GU(T_IQ2_S) MM3_GU(T_IQ3_S)
MM3_GU(T_IQ4_XS) MM3_GU(T_Q2_0) MM3_GU(T_IQ3_XXS) MM3_GU(T_IQ2_XXS) MM3_GU(T_IQ2_XS) MM3_GU(T_IQ2_S) MM3_GU(T_IQ3_S)
MM3_DN(T_Q2_0) MM3_DN(T_IQ4_NL)
+17 -6
View File
@@ -6,6 +6,17 @@
// (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) {
@@ -36,14 +47,14 @@ template <> struct XKind<T_Q3_K> { enum { K = XK_P2 }; };
// X<F>::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 <int F> struct X {
static inline void load(device const float* xp, device const float*, int u, thread float* xs, thread float* xsum) {
template <typename P> static inline void load(P xp, P, int u, thread float* xs, thread float* xsum) {
constexpr int UNIT = fmt<F>::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<T_Q2_0> {
static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {
template <typename P> 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;
@@ -51,7 +62,7 @@ template <> struct X<T_Q2_0> {
};
// Q4_K / Q5_K: unit (block b, chunk c, half m): run0 at 256b + 64c + 16m, run1 = run0 + 32
template <> struct X<T_Q4_K> {
static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {
template <typename P> 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);
@@ -60,13 +71,13 @@ template <> struct X<T_Q4_K> {
}
};
template <> struct X<T_Q5_K> {
static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {
template <typename P> static inline void load(P xp, P s16, int u, thread float* xs, thread float* xsum) {
X<T_Q4_K>::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<T_Q6_K> {
static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {
template <typename P> 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);
@@ -76,7 +87,7 @@ template <> struct X<T_Q6_K> {
};
// Q3_K: 32 contiguous, sums per 16
template <> struct X<T_Q3_K> {
static inline void load(device const float* xp, device const float* s16, int u, thread float* xs, thread float* xsum) {
template <typename P> 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];
+1 -1
View File
@@ -36,7 +36,7 @@ struct MoeArgs {
};
int type_of(const std::string& s) {
for (int t : {ref::Q2_0, ref::IQ3_XXS, ref::IQ2_XXS, ref::IQ2_XS, ref::IQ2_S, ref::IQ3_S, ref::IQ4_NL})
for (int t : {ref::Q2_0, ref::IQ3_XXS, ref::IQ2_XXS, ref::IQ2_XS, ref::IQ2_S, ref::IQ3_S, ref::IQ4_NL, ref::IQ4_XS})
if (s == ref::type_name(t)) return t;
std::fprintf(stderr, "unknown type %s\n", s.c_str());
std::exit(2);
+12 -2
View File
@@ -287,10 +287,19 @@ int main(int argc, char** argv) {
for (int nt : ntoks) {
double best = 1e9;
Cfg bc{};
for (Cfg c : kCfgs) {
const bool gx = std::getenv("GEMV_X") != nullptr; // gemvx_* (x staged in threadgroup memory)
const uint32_t tgm = (uint32_t)(4 * nt * (s.n_in + s.n_in / 16) + 15) / 16 * 16;
for (Cfg c : gx ? std::vector<Cfg>{{32, 1, 4, 1}, {32, 2, 4, 1}, {16, 1, 4, 1}, {16, 2, 4, 1}, {8, 1, 4, 1},
{8, 2, 4, 1}, {4, 2, 4, 1}, {16, 2, 8, 1}, {8, 2, 8, 1}, {16, 4, 4, 1},
{32, 2, 8, 1}, {2, 2, 4, 1}}
: kCfgs) {
if (!cfg_ok(c, s.type, s.n_in)) continue;
if (!all && !(c.K == 1 || s.n_in >= 6144)) continue;
mtl::Pipeline p = cx.pipeline(lib, kname(s.type, c), mtl::Constants().i(0, nt));
if (gx && tgm > 32768) continue;
const std::string kn = gx ? "gemvx_" + std::to_string(s.type) + "_" + std::to_string(c.L) + "_" + std::to_string(c.R) +
"_" + std::to_string(c.S)
: kname(s.type, c);
mtl::Pipeline p = cx.pipeline(lib, kn, mtl::Constants().i(0, nt).i(9, std::getenv("GEMV_PROBE") ? std::atoi(std::getenv("GEMV_PROBE")) : 0));
GemvArgs a{(uint32_t)s.n_in, (uint32_t)s.n_out, (uint32_t)nt, (uint32_t)rb, (uint32_t)s.n_in,
(uint32_t)s.n_out};
std::vector<mtl::Dispatch> ds;
@@ -302,6 +311,7 @@ int main(int argc, char** argv) {
mtl::Arg::B(xp, plane * 4), mtl::Arg::B(y), mtl::Arg::V(a)};
d.grid[0] = (s.n_out + rows_per_tg(c) - 1) / rows_per_tg(c);
d.tg[0] = c.S * 32;
if (gx) d.tg_mem = tgm;
ds.push_back(d);
}
cx.warm(ds, 0.05);
+1 -1
View File
@@ -23,7 +23,7 @@ struct MmPlanArgs { uint32_t n_ent, topk, n_expert, tm, n_blocks; };
struct MmArgs { uint64_t gate_off, up_off, down_off, gu_stride, d_stride; uint32_t gu_row, d_row, n_embd, n_ff; };
double median(std::vector<double> v) { std::sort(v.begin(), v.end()); return v[v.size() / 2]; }
int type_of(const std::string& s) {
for (int t : {ref::Q2_0, ref::IQ3_XXS, ref::IQ2_XXS, ref::IQ2_XS, ref::IQ2_S, ref::IQ3_S, ref::IQ4_NL})
for (int t : {ref::Q2_0, ref::IQ3_XXS, ref::IQ2_XXS, ref::IQ2_XS, ref::IQ2_S, ref::IQ3_S, ref::IQ4_NL, ref::IQ4_XS})
if (s == ref::type_name(t)) return t;
std::exit(2);
}
+3 -1
View File
@@ -49,6 +49,7 @@ struct MmArgs { uint64_t gate_off, up_off, down_off, gu_stride, d_stride; uint32
struct ArgArgs { uint32_t n_vocab, parts; };
double median(std::vector<double> v) { std::sort(v.begin(), v.end()); return v[v.size() / 2]; }
bool fmt_unit32(int t) { return t == ref::IQ4_XS || t == ref::IQ4_NL; } // mm_gu2 needs a K chunk >= 32
int unit_of(int t) { return t == ref::Q2_0 || t == ref::IQ3_S || t == ref::BF16 ? 16 : 32; }
} // namespace
@@ -433,7 +434,8 @@ int main(int argc, char** argv) {
push(g, "experts");
mtl::Dispatch gu;
gu.p = gu_v3 ? P(lib, "mm_gu3_" + std::to_string(eg.type) + "_" + gu3_for(eg.type), mtl::Constants().b(7, k4))
: P(lib, "mm_gu2_" + std::to_string(eg.type) + "_16_64_16_2", mtl::Constants().i(5, 0));
: P(lib, "mm_gu2_" + std::to_string(eg.type) + (fmt_unit32(eg.type) ? "_16_64_32_2" : "_16_64_16_2"),
mtl::Constants().i(5, 0));
gu.args = {mtl::Arg::B(W), mtl::Arg::B(XS), mtl::Arg::B(HS), mtl::Arg::B(tiles), mtl::Arg::V(ma)};
gu.tg[0] = gu_v3 ? 32 * gu3[3] : 64;
gu.indirect = &meta;