Metal DeepSeek engine: IQ1_S expert kernels (decode GEMV, window, prompt tile) and a Q6_K dense dequant for the Unsloth sets
This commit is contained in:
1 parent
7851a8dab6
commit
1b6c67122a
10 files changed
+158
-14
No files matched your search
@@ -712,6 +712,35 @@ template <> struct W8<T_IQ1_M> {
|
||||
}
|
||||
};
|
||||
|
||||
// IQ1_S (Unsloth's UD-IQ1_S / UD-IQ1_M experts), ggml's dequantize_row_iq1_s: f16 d, qs[32], qh[8] u16; a 32-value sub-block has
|
||||
// one scale dl = d (2 s + 1) (qh bits 12-14), a +-0.125 delta (qh bit 15) and four 8-value groups whose grid index is
|
||||
// qs[4 ib + g] | 3 bits of qh << 8. Same float arithmetic as ggml (dl (grid + delta)), so the half4 is what the CPU reference rounds to.
|
||||
template <typename P>
|
||||
static inline half4 iq1s_w4(P row, int k) {
|
||||
const P bl = row + (k >> 8) * 50;
|
||||
const int ib = (k & 255) >> 5, g = (k & 31) >> 3, hh = (k & 7) >> 2;
|
||||
const uint qh = uint(bl[34 + 2 * ib]) | (uint(bl[35 + 2 * ib]) << 8);
|
||||
const float dl = float(as_type<half>(ushort(uint(bl[0]) | (uint(bl[1]) << 8)))) * float(2u * ((qh >> 12) & 7u) + 1u);
|
||||
const float delta = (qh & 0x8000u) != 0u ? -0.125f : 0.125f;
|
||||
const uint idx = uint(bl[2 + 4 * ib + g]) | (((qh >> (3u * uint(g))) & 7u) << 8);
|
||||
const ulong gr = iq1s_grid[idx];
|
||||
float v[4];
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const uint by = uint((gr >> (8u * uint(4 * hh + j))) & 0xffu);
|
||||
const int c = (by & 0x80u) != 0u ? int(by) - 256 : int(by);
|
||||
v[j] = dl * (float(c) + delta);
|
||||
}
|
||||
return half4(v[0], v[1], v[2], v[3]);
|
||||
}
|
||||
template <> struct W8<T_IQ1_S> {
|
||||
template <typename P>
|
||||
static inline void run(P row, int k, threadgroup const uint* tab, thread half4& a, thread half4& b) {
|
||||
(void) tab;
|
||||
a = iq1s_w4(row, k);
|
||||
b = iq1s_w4(row, k + 16);
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct W8<T_IQ3_S> {
|
||||
template <typename P>
|
||||
static inline void run(P row, int k, threadgroup const uint* tab, thread half4& a, thread half4& b) {
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#define T_IQ3_XXS 18
|
||||
#define T_IQ4_NL 20
|
||||
#define T_IQ3_S 21
|
||||
#define T_IQ1_S 19 // Unsloth UD-IQ1_S / UD-IQ1_M experts (50-byte blocks)
|
||||
#define T_IQ1_M 29 // the 2.00 bpw pack's 1.75-bit type
|
||||
#define T_IQ2_S 22
|
||||
#define T_IQ4_XS 23
|
||||
@@ -41,6 +42,7 @@ template <> struct fmt<T_Q2_0R> { enum { QK = 64, BB = 18, UNIT = 16, TAB =
|
||||
template <> struct fmt<T_IQ3_XXS> { enum { QK = 256, BB = 98, UNIT = 16, TAB = 256 + 32 }; };
|
||||
template <> struct fmt<T_IQ2_XXS> { enum { QK = 256, BB = 66, UNIT = 16, TAB = 512 + 32 }; };
|
||||
template <> struct fmt<T_IQ1_M> { enum { QK = 256, BB = 56, UNIT = 16, TAB = 1 }; };
|
||||
template <> struct fmt<T_IQ1_S> { enum { QK = 256, BB = 50, UNIT = 16, TAB = 1 }; };
|
||||
template <> struct fmt<T_IQ2_XS> { enum { QK = 256, BB = 74, UNIT = 16, TAB = 1024 + 32 }; };
|
||||
template <> struct fmt<T_IQ2_S> { enum { QK = 256, BB = 82, UNIT = 16, TAB = 2048 + 32 }; };
|
||||
template <> struct fmt<T_IQ3_S> { enum { QK = 256, BB = 110, UNIT = 16, TAB = 512 + 32 }; };
|
||||
@@ -393,6 +395,7 @@ template <> inline void load_tab<T_IQ2_XS>(threadgroup uint* tab, ushort tid, us
|
||||
}
|
||||
// IQ1_M: no threadgroup table - the tile reads iq1s_grid from the constants (the w4 helper in moe_mm.metal)
|
||||
template <> inline void load_tab<T_IQ1_M>(threadgroup uint*, ushort, ushort) {}
|
||||
template <> inline void load_tab<T_IQ1_S>(threadgroup uint*, ushort, ushort) {} // (same: iq1s_grid from the constants)
|
||||
template <> inline void load_tab<T_IQ2_S>(threadgroup uint* tab, ushort tid, ushort nth) {
|
||||
if (ctab) return;
|
||||
for (int i = tid; i < 1024; i += nth) { tab[2 * i] = uint(iq2s_grid[i]); tab[2 * i + 1] = uint(iq2s_grid[i] >> 32); }
|
||||
|
||||
@@ -750,6 +750,7 @@ bool iq_mv(const void* jobs, int nj) {
|
||||
case 17: tn = "ds4_iq2xs_mvn"; break;
|
||||
case 18: tn = "ds4_iq3xxs_mvn"; break;
|
||||
case 22: tn = "ds4_iq2s_mvn"; break;
|
||||
case 19: tn = "ds4_iq1s_mvn"; break; // (Unsloth UD-IQ1_S / UD-IQ1_M experts: ds4_w32_iq1s)
|
||||
case 29: tn = "ds4_iq1m_mvn"; break; // (the 2.00 bpw pack's IQ1_M: ds4_iq.metal's ds4_w32_iq1m)
|
||||
default: break;
|
||||
}
|
||||
@@ -2328,13 +2329,13 @@ bool rows_f16(const float* x, int ldx, const int* idx, int nrows, int ncols, uin
|
||||
return true;
|
||||
}
|
||||
|
||||
// kind: 0 BF16 values, 8 Q8_0, 13 Q5_K. n rows of k values, ld the row stride (bytes; values for BF16), out half rows.
|
||||
// kind: 0 BF16 values, 8 Q8_0, 13 Q5_K, 14 Q6_K. n rows of k values, ld the row stride (bytes; values for BF16), out half rows.
|
||||
bool dequant_rows(int kind, const void* raw, size_t ld, int k, int n, uint16_t* out) {
|
||||
if (n <= 0 || k <= 0) return true;
|
||||
if (!ensure()) return false;
|
||||
const char* fn = kind == 13 ? "ds4_dq_q5k" : kind == 8 ? "ds4_dq_q8" : kind == 0 ? "ds4_dq_bf16" : nullptr;
|
||||
const char* fn = kind == 13 ? "ds4_dq_q5k" : kind == 14 ? "ds4_dq_q6k" : kind == 8 ? "ds4_dq_q8" : kind == 0 ? "ds4_dq_bf16" : nullptr;
|
||||
if (!fn) return false;
|
||||
if ((kind == 13 && k % 256 != 0) || (kind == 8 && k % 32 != 0)) return false;
|
||||
if (((kind == 13 || kind == 14) && k % 256 != 0) || (kind == 8 && k % 32 != 0)) return false;
|
||||
void* hw = strata::gpu::host_view((void*) raw);
|
||||
void* ho = strata::gpu::host_view(out);
|
||||
if (!hw || !ho) return false;
|
||||
@@ -2345,7 +2346,7 @@ bool dequant_rows(int kind, const void* raw, size_t ld, int k, int n, uint16_t*
|
||||
return g_cx->wrap((void*) base, wrap_len(base, end - base));
|
||||
};
|
||||
auto off = [&](void* host) { return (size_t) ((uintptr_t) host - (((uintptr_t) host) & ~(uintptr_t) (page - 1))); };
|
||||
const size_t wbytes = (size_t) (n - 1) * ld * (kind == 0 ? 2 : 1) + (kind == 0 ? (size_t) k * 2 : (kind == 13 ? (size_t) (k / 256) * 176 : (size_t) (k / 32) * 34));
|
||||
const size_t wbytes = (size_t) (n - 1) * ld * (kind == 0 ? 2 : 1) + (kind == 0 ? (size_t) k * 2 : (kind == 13 ? (size_t) (k / 256) * 176 : kind == 14 ? (size_t) (k / 256) * 210 : (size_t) (k / 32) * 34));
|
||||
if (!std::getenv("DS4_NO_TOUCH")) { // (the matrix may be file-backed pages not read yet: the GPU does not page them in, it stalls)
|
||||
volatile unsigned char sink = 0;
|
||||
for (size_t o = 0; o < wbytes; o += 4096) sink = sink + ((const volatile unsigned char*) hw)[o];
|
||||
@@ -2356,7 +2357,7 @@ bool dequant_rows(int kind, const void* raw, size_t ld, int k, int n, uint16_t*
|
||||
if (!bw.handle || !bo.handle) return false;
|
||||
mtl::Pipeline pl;
|
||||
try { pl = g_cx->pipeline(g_lib, fn); } catch (...) { return false; }
|
||||
size_t items = kind == 13 ? (size_t) n * (k / 256) * 8 : kind == 8 ? (size_t) n * (k / 32) : (size_t) n * k;
|
||||
size_t items = kind == 13 ? (size_t) n * (k / 256) * 8 : kind == 14 ? (size_t) n * (k / 256) * 64 : kind == 8 ? (size_t) n * (k / 32) : (size_t) n * k;
|
||||
mtl::Dispatch d;
|
||||
d.p = &pl;
|
||||
d.args = {mtl::Arg::B(bw, off(hw)), mtl::Arg::V((unsigned long) ld), mtl::Arg::V((unsigned) k), mtl::Arg::V((unsigned) n),
|
||||
@@ -2454,7 +2455,7 @@ int g_lib_moe = -2;
|
||||
mtl::Buffer g_mm_xs, g_mm_hs, g_mm_ys; // grow-only scratch: the sorted rows' half activations; the gate/up's swiglu rows (the down's input)
|
||||
bool g_mm_hs_ok = false;
|
||||
// bytes of a 256-value block per type: the tile kernels are instantiated for exactly these (DS4_MM_INST)
|
||||
size_t iq_bs(int type) { return type == 16 ? 66 : type == 17 ? 74 : type == 22 ? 82 : type == 18 ? 98 : type == 29 ? 56 : 0; }
|
||||
size_t iq_bs(int type) { return type == 16 ? 66 : type == 17 ? 74 : type == 22 ? 82 : type == 18 ? 98 : type == 29 ? 56 : type == 19 ? 50 : 0; }
|
||||
struct DsMmArgsH {
|
||||
unsigned long gate_off, up_off, down_off, stride;
|
||||
unsigned gu_row, d_row, n_embd, n_ff;
|
||||
|
||||
@@ -2706,6 +2706,36 @@ kernel void ds4_dq_q5k(device const uchar* W [[buffer(0)]], constant ulong& ld [
|
||||
}
|
||||
}
|
||||
|
||||
// Q6_K (210-byte blocks: ql[128], qh[64], int8 scales[16], f16 d; the Unsloth sets' attention, shared-expert, head and engram_kv
|
||||
// matrices): a thread four values of a block (two 128-value halves x 32 lanes), ggml's dequantize_row_q6_K arithmetic
|
||||
// ((d * scale) * (q - 32), in this order) -> out[n][k] half
|
||||
kernel void ds4_dq_q6k(device const uchar* W [[buffer(0)]], constant ulong& ld [[buffer(1)]], constant uint& k [[buffer(2)]],
|
||||
constant uint& n [[buffer(3)]], device half* out [[buffer(4)]], uint gid [[thread_position_in_grid]]) {
|
||||
const uint nb = k / 256u;
|
||||
const ulong total = (ulong) n * nb * 64ul;
|
||||
if (ulong(gid) >= total) return;
|
||||
const uint l = gid & 31u;
|
||||
const uint hf = (gid >> 5) & 1u;
|
||||
const uint blk = (gid >> 6) % nb;
|
||||
const uint row = uint(ulong(gid) / (64ul * nb));
|
||||
const device uchar* p = W + (ulong) row * ld + (ulong) blk * 210ul;
|
||||
const float d = float(as_type<half>(*(const device ushort*) (p + 208)));
|
||||
const device uchar* ql = p + 64u * hf;
|
||||
const device uchar* qh = p + 128u + 32u * hf;
|
||||
const device char* sc = (const device char*) (p + 192u + 8u * hf);
|
||||
const uint is = l >> 4;
|
||||
const uint qlo = uint(ql[l]), qhi = uint(ql[l + 32u]), h = uint(qh[l]);
|
||||
const int q1 = int((qlo & 0xfu) | (((h >> 0) & 3u) << 4)) - 32;
|
||||
const int q2 = int((qhi & 0xfu) | (((h >> 2) & 3u) << 4)) - 32;
|
||||
const int q3 = int((qlo >> 4) | (((h >> 4) & 3u) << 4)) - 32;
|
||||
const int q4 = int((qhi >> 4) | (((h >> 6) & 3u) << 4)) - 32;
|
||||
device half* o = out + (ulong) row * k + (ulong) blk * 256ul + 128ul * hf + l;
|
||||
o[0] = half(d * float(sc[is + 0u]) * float(q1));
|
||||
o[32] = half(d * float(sc[is + 2u]) * float(q2));
|
||||
o[64] = half(d * float(sc[is + 4u]) * float(q3));
|
||||
o[96] = half(d * float(sc[is + 6u]) * float(q4));
|
||||
}
|
||||
|
||||
// Q8_0: 34-byte blocks (fp16 d, 32 int8)
|
||||
kernel void ds4_dq_q8(device const uchar* W [[buffer(0)]], constant ulong& ld [[buffer(1)]], constant uint& k [[buffer(2)]],
|
||||
constant uint& n [[buffer(3)]], device half* out [[buffer(4)]], uint gid [[thread_position_in_grid]]) {
|
||||
|
||||
@@ -264,9 +264,13 @@ struct Ds4IqSub {
|
||||
|
||||
// the block bytes of the types these kernels take: 16 IQ2_XXS, 17 IQ2_XS, 18 IQ3_XXS, 22 IQ2_S (ds4_iq.cu's Ty<T>::B)
|
||||
static inline uint ds4_iq_bs(int type) {
|
||||
return type == 16 ? 66u : type == 17 ? 74u : type == 18 ? 98u : 82u;
|
||||
return type == 16 ? 66u : type == 17 ? 74u : type == 18 ? 98u : type == 19 ? 50u : type == 29 ? 56u : 82u;
|
||||
}
|
||||
|
||||
// the 1-bit types' sub-block decoders (defined with the multi-column matvec below): 32 codes as 8*grid +- 1 (int8) and the two halves' scales
|
||||
static inline void ds4_w32_iq1s(const device uchar* p, uint ib, thread int (&w)[32], thread float& db0, thread float& db1);
|
||||
static inline void ds4_w32_iq1m(const device uchar* p, uint ib, thread int (&w)[32], thread float& db0, thread float& db1);
|
||||
|
||||
// ds4_iq.cu:139 decode<T>: the block's 8 sub-blocks, kb's is the (kb & 7)'th; the grids are ggml-common.h's
|
||||
static inline Ds4IqSub ds4_iq_sub(device const uchar* row, uint bs, int kb, int type) {
|
||||
Ds4IqSub s;
|
||||
@@ -275,6 +279,15 @@ static inline Ds4IqSub ds4_iq_sub(device const uchar* row, uint bs, int kb, int
|
||||
const float d = float(as_type<half>(*(const device ushort*) p));
|
||||
uint idx[4], sm[4];
|
||||
float db0, db1;
|
||||
if (type == 19 || type == 29) { // IQ1_S (50 B) / IQ1_M (56 B): the integer form 8*code +- 1, the scale / 8 in db
|
||||
int w[32];
|
||||
if (type == 19) ds4_w32_iq1s(p, uint(ib), w, db0, db1); else ds4_w32_iq1m(p, uint(ib), w, db0, db1);
|
||||
for (uint t = 0; t < 8u; ++t)
|
||||
s.q[t] = (w[4u * t] & 0xff) | ((w[4u * t + 1u] & 0xff) << 8) | ((w[4u * t + 2u] & 0xff) << 16) | ((w[4u * t + 3u] & 0xff) << 24);
|
||||
s.s0 = db0;
|
||||
s.s1 = db1;
|
||||
return s;
|
||||
}
|
||||
if (type == 18) { // IQ3_XXS: d, qs[64] grid bytes (4 a sub-block), scales[8] (the top nibble the group's s)
|
||||
const device uchar* qs = p + 2;
|
||||
const uint w0 = uint(qs[8 * ib]) | (uint(qs[8 * ib + 1]) << 8) | (uint(qs[8 * ib + 2]) << 16) |
|
||||
@@ -1011,6 +1024,27 @@ static inline void ds4_w32_iq3xxs(const device uchar* p, uint ib, thread int (&w
|
||||
}
|
||||
|
||||
|
||||
// IQ1_S (UD-IQ1_S / UD-IQ1_M experts; 50-byte blocks): f16 d, qs[32] (4 grid bytes a 32-value sub-block), qh[8] u16 (3 bits a
|
||||
// group of 8 more grid index, bits 12-14 the sub-block's 3-bit scale, bit 15 the sign of the +-0.125 delta). ggml's
|
||||
// dequantize_row_iq1_s: dl = d (2 s + 1), w = dl (grid + delta). The integer form here (8 grid +- 1, scale / 8) is the same
|
||||
// number with no rounding difference.
|
||||
static inline void ds4_w32_iq1s(const device uchar* p, uint ib, thread int (&w)[32], thread float& db0, thread float& db1) {
|
||||
const float d = float(as_type<half>(*(const device ushort*) p));
|
||||
const uint qh = uint(p[34u + 2u * ib]) | (uint(p[35u + 2u * ib]) << 8);
|
||||
db0 = d * float(2u * ((qh >> 12) & 7u) + 1u) * 0.125f;
|
||||
db1 = db0;
|
||||
const int dlt = (qh & 0x8000u) != 0u ? -1 : 1;
|
||||
for (uint l = 0; l < 4u; ++l) {
|
||||
const uint idx = uint(p[2u + 4u * ib + l]) | (((qh >> (3u * l)) & 7u) << 8);
|
||||
const ulong g = iq1s_grid[idx];
|
||||
for (uint j = 0; j < 8u; ++j) {
|
||||
const uint code = uint((g >> (8u * j)) & 0xffu);
|
||||
const int c8 = (code & 0x80u) != 0u ? int(code) - 256 : int(code); // -1 / 0 / +1
|
||||
w[l * 8u + j] = 8 * c8 + dlt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IQ1_M (the 2.00 bpw pack's 1.75-bit type): the block has no d field - its F16 scale is the top nibbles of its four
|
||||
// u16 scales - and each 32-value sub-block splits into two 16-value halves with their own 3-bit scale (ggml's
|
||||
// dequantize_row_iq1_m: dl1 for the first two 8-value groups, dl2 for the last two, and a +-0.125 delta per group).
|
||||
@@ -1048,7 +1082,7 @@ kernel void ds4_iqg_mvn(device const uchar* jobs [[buffer(0)]], uint2 tgp [[thre
|
||||
const uint row = tgp.x * 8u + (tid >> 5);
|
||||
if (int(row) >= b.n) return;
|
||||
const uint nblk = uint(b.k) / 256u, nsb = uint(b.k) / 32u;
|
||||
constexpr uint bs = TYPE == 16 ? 66u : (TYPE == 17 ? 74u : (TYPE == 18 ? 98u : (TYPE == 29 ? 56u : 82u)));
|
||||
constexpr uint bs = TYPE == 16 ? 66u : (TYPE == 17 ? 74u : (TYPE == 18 ? 98u : (TYPE == 29 ? 56u : (TYPE == 19 ? 50u : 82u))));
|
||||
const device uchar* w0p = b.w + (size_t) row * b.ld;
|
||||
const uint ib = lane & 7u;
|
||||
const uint bsel = lane >> 3;
|
||||
@@ -1062,6 +1096,7 @@ kernel void ds4_iqg_mvn(device const uchar* jobs [[buffer(0)]], uint2 tgp [[thre
|
||||
else if (TYPE == 17) ds4_w32_iq2xs(p, ib, w, db0, db1);
|
||||
else if (TYPE == 22) ds4_w32_iq2s(p, ib, w, db0, db1);
|
||||
else if (TYPE == 29) ds4_w32_iq1m(p, ib, w, db0, db1);
|
||||
else if (TYPE == 19) ds4_w32_iq1s(p, ib, w, db0, db1);
|
||||
else { ds4_w32_iq3xxs(p, ib, w, db0); db1 = db0; }
|
||||
for (uint c = 0; c < NC; ++c) {
|
||||
const device uint4* xq4 = (const device uint4*) (b.xq + (size_t) c * b.k + (size_t) blk * 256u + 32u * ib);
|
||||
@@ -1085,7 +1120,8 @@ kernel void ds4_iqg_mvn(device const uchar* jobs [[buffer(0)]], uint2 tgp [[thre
|
||||
template [[host_name("ds4_iq2xs_mvn" #NC)]] kernel void ds4_iqg_mvn<NC, 17>(device const uchar*, uint2, uint, ushort); \
|
||||
template [[host_name("ds4_iq2s_mvn" #NC)]] kernel void ds4_iqg_mvn<NC, 22>(device const uchar*, uint2, uint, ushort); \
|
||||
template [[host_name("ds4_iq3xxs_mvn" #NC)]] kernel void ds4_iqg_mvn<NC, 18>(device const uchar*, uint2, uint, ushort); \
|
||||
template [[host_name("ds4_iq1m_mvn" #NC)]] kernel void ds4_iqg_mvn<NC, 29>(device const uchar*, uint2, uint, ushort);
|
||||
template [[host_name("ds4_iq1m_mvn" #NC)]] kernel void ds4_iqg_mvn<NC, 29>(device const uchar*, uint2, uint, ushort); \
|
||||
template [[host_name("ds4_iq1s_mvn" #NC)]] kernel void ds4_iqg_mvn<NC, 19>(device const uchar*, uint2, uint, ushort);
|
||||
DS4_MVN_INST(1)
|
||||
DS4_MVN_INST(2)
|
||||
DS4_MVN_INST(4)
|
||||
|
||||
@@ -132,6 +132,7 @@ kernel void ds4_mm_dn(device const uchar* W [[buffer(0)]], device half* YEH [[bu
|
||||
|
||||
#define DS4_MM_INST(F, T) template [[host_name("ds4_mm_gu_" #T)]] kernel decltype(ds4_mm_gu<F, 32, 32, 32, 4>) ds4_mm_gu<F, 32, 32, 32, 4>; template [[host_name("ds4_mm_dn_" #T)]] kernel decltype(ds4_mm_dn<F, 32, 32, 32, 4>) ds4_mm_dn<F, 32, 32, 32, 4>;
|
||||
DS4_MM_INST(T_IQ1_M, 29)
|
||||
DS4_MM_INST(T_IQ1_S, 19)
|
||||
DS4_MM_INST(T_IQ2_XXS, 16)
|
||||
DS4_MM_INST(T_IQ2_XS, 17)
|
||||
DS4_MM_INST(T_IQ2_S, 22)
|
||||
|
||||
+46
-2
@@ -24,7 +24,7 @@ const char* type_name(int t) {
|
||||
case Q3_K: return "Q3_K"; case Q4_K: return "Q4_K"; case Q5_K: return "Q5_K"; case Q6_K: return "Q6_K";
|
||||
case IQ2_XXS: return "IQ2_XXS"; case IQ2_XS: return "IQ2_XS"; case IQ2_S: return "IQ2_S";
|
||||
case IQ3_XXS: return "IQ3_XXS"; case IQ3_S: return "IQ3_S"; case IQ4_NL: return "IQ4_NL";
|
||||
case IQ4_XS: return "IQ4_XS"; case Q2_0: return "Q2_0";
|
||||
case IQ4_XS: return "IQ4_XS"; case Q2_0: return "Q2_0"; case IQ1_S: return "IQ1_S"; case IQ1_M: return "IQ1_M";
|
||||
case TQ2_T: return "TQ2_T"; case TQK6: return "TQK6"; case TQK7: return "TQK7";
|
||||
}
|
||||
return "?";
|
||||
@@ -46,7 +46,7 @@ int block_bytes(int t) {
|
||||
case Q4_0: return 18; case Q5_0: return 22; case Q5_1: return 24; case Q8_0: return 34; case IQ4_NL: return 18; case Q2_0: return 18;
|
||||
case Q3_K: return 110; case Q4_K: return 144; case Q5_K: return 176; case Q6_K: return 210;
|
||||
case IQ2_XXS: return 66; case IQ2_XS: return 74; case IQ2_S: return 82; case IQ3_XXS: return 98;
|
||||
case IQ3_S: return 110; case IQ4_XS: return 136;
|
||||
case IQ3_S: return 110; case IQ4_XS: return 136; case IQ1_S: return 50; case IQ1_M: return 56;
|
||||
case TQ2_T: return 34; case TQK6: return 26; case TQK7: return 30;
|
||||
}
|
||||
std::fprintf(stderr, "ref: unknown type %d\n", t);
|
||||
@@ -292,6 +292,48 @@ static void deq_block(int t, const uint8_t* b, float* y) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
case IQ1_S: { // ggml dequantize_row_iq1_s: dl = d (2 s + 1), y = dl (grid + delta)
|
||||
const float d = f16_to_f32(rd16(b));
|
||||
const uint8_t* qs = b + 2;
|
||||
for (int ib = 0; ib < 8; ++ib) {
|
||||
const uint16_t qh = rd16(b + 34 + 2 * ib);
|
||||
const float dl = d * (float)(2 * ((qh >> 12) & 7) + 1);
|
||||
const float delta = qh & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
|
||||
for (int l = 0; l < 4; ++l) {
|
||||
const int8_t* grid = (const int8_t*)(iq1s_grid + (qs[l] | (((qh >> 3 * l) & 7) << 8)));
|
||||
for (int j = 0; j < 8; ++j) y[j] = dl * (grid[j] + delta);
|
||||
y += 8;
|
||||
}
|
||||
qs += 4;
|
||||
}
|
||||
return;
|
||||
}
|
||||
case IQ1_M: { // ggml dequantize_row_iq1_m: the f16 scale in the top nibbles of the four u16 scales
|
||||
const uint8_t* qs = b;
|
||||
const uint8_t* qh = b + 32;
|
||||
const uint16_t* sc = (const uint16_t*)(b + 48);
|
||||
uint16_t s16 = (uint16_t)((sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000));
|
||||
const float d = f16_to_f32(s16);
|
||||
for (int ib = 0; ib < 8; ++ib) {
|
||||
const float dl1 = d * (float)(2 * ((sc[ib / 2] >> (6 * (ib % 2) + 0)) & 7) + 1);
|
||||
const float dl2 = d * (float)(2 * ((sc[ib / 2] >> (6 * (ib % 2) + 3)) & 7) + 1);
|
||||
uint16_t idx[4];
|
||||
idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
|
||||
idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
|
||||
idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
|
||||
idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
|
||||
const float delta[4] = {qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA, qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA,
|
||||
qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA, qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA};
|
||||
for (int l = 0; l < 4; ++l) {
|
||||
const int8_t* grid = (const int8_t*)(iq1s_grid + idx[l]);
|
||||
for (int j = 0; j < 8; ++j) y[j] = (l < 2 ? dl1 : dl2) * (grid[j] + delta[l]);
|
||||
y += 8;
|
||||
}
|
||||
qs += 4;
|
||||
qh += 2;
|
||||
}
|
||||
return;
|
||||
}
|
||||
case IQ2_XXS: {
|
||||
const float d = f16_to_f32(rd16(b));
|
||||
const uint8_t* qs = b + 2;
|
||||
@@ -462,6 +504,8 @@ void random_blocks(int t, uint8_t* dst, int rows, int cols, uint64_t seed, float
|
||||
case Q6_K: put16(b + 208, f32_to_f16(scale * u / 32 / 64)); break;
|
||||
case Q3_K: put16(b + 108, f32_to_f16(scale * u / 32 / 2)); break;
|
||||
case IQ2_XXS: case IQ2_XS: case IQ2_S: put16(b, f32_to_f16(scale * u / 4)); break;
|
||||
case IQ1_S: put16(b, f32_to_f16(scale * u / 2)); break;
|
||||
case IQ1_M: { uint16_t h = f32_to_f16(scale * u / 2); for (int j = 0; j < 4; ++j) { uint16_t w = (uint16_t)(b[48 + 2 * j] | (b[49 + 2 * j] << 8)); w = (uint16_t)((w & 0x0fff) | (((h >> (4 * j)) & 0xf) << 12)); b[48 + 2 * j] = (uint8_t)w; b[49 + 2 * j] = (uint8_t)(w >> 8); } break; }
|
||||
case IQ3_XXS: put16(b, f32_to_f16(scale * u / 8)); break;
|
||||
case IQ3_S: put16(b, f32_to_f16(scale * u / 64)); break;
|
||||
case TQ2_T: case TQK6: case TQK7: put16(b, f32_to_f16(scale * u / 1.5f)); break; // codebook |point| ~ 1
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace ref {
|
||||
// GGML type ids (stable, as in native_mmvq.hpp)
|
||||
enum : int {
|
||||
F32 = 0, F16 = 1, Q4_0 = 2, Q5_0 = 6, Q5_1 = 7, Q8_0 = 8, Q3_K = 11, Q4_K = 12, Q5_K = 13, Q6_K = 14,
|
||||
IQ2_XXS = 16, IQ2_XS = 17, IQ3_XXS = 18, IQ3_S = 21, IQ2_S = 22, IQ4_NL = 20, IQ4_XS = 23, BF16 = 30, Q2_0 = 42,
|
||||
IQ2_XXS = 16, IQ2_XS = 17, IQ3_XXS = 18, IQ1_S = 19, IQ1_M = 29, IQ3_S = 21, IQ2_S = 22, IQ4_NL = 20, IQ4_XS = 23, BF16 = 30, Q2_0 = 42,
|
||||
TQ2_T = 144, TQK6 = 145, TQK7 = 146, // agentionai's trellis types (Gyro): 128 values a block, 34 / 26 / 30 bytes
|
||||
};
|
||||
|
||||
|
||||
@@ -1087,7 +1087,7 @@ bool type_on_chunk(int type) {
|
||||
#if defined(STRATA_METAL)
|
||||
// ds4_mm.metal instantiates the tile GEMM for IQ2_XXS / IQ2_XS / IQ2_S only; IQ1_M (the 2.00 bpw pack's type) has
|
||||
// the matvec kernel but no tile one, so a chunked prompt would stop in "kernel launch with no Metal dispatch".
|
||||
if (type == iq::kIQ1_S || type == iq::kIQ4_NL) return false; // (IQ1_M: the tile kernels since DS4_MM_INST(29) and iq_bs)
|
||||
if (type == iq::kIQ4_NL) return false; // (IQ1_M and IQ1_S: the tile kernels, DS4_MM_INST(29) / (19) and iq_bs)
|
||||
return type_on_device(type);
|
||||
#else
|
||||
(void) type;
|
||||
|
||||
+1
-1
@@ -1457,7 +1457,7 @@ bool supported(int t) {
|
||||
// The MSL kernels (metal/src/engine/msl/ds4_iq.metal) implement the 2.0-bit types plus IQ1_M; IQ1_S and IQ4_NL
|
||||
// have CUDA kernels here but no Metal ones. Saying so makes those layers "cpu only" (ds4_model.cu, expert_layout),
|
||||
// so their experts stay on the CPU lane instead of stopping in "kernel launch with no Metal dispatch".
|
||||
if (t == kIQ1_S || t == kIQ4_NL) return false;
|
||||
if (t == kIQ4_NL) return false; // (IQ1_S: ds4_iq1s_mvn + the tile kernel since the Unsloth sets)
|
||||
#endif
|
||||
// the types the kernels below are instantiated for (DS4_IQ_SWITCH + the trellis ones + the dense K-quants)
|
||||
switch (t) {
|
||||
|
||||
Reference in new issue
Block a user