okami.mom/strata

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

Niko1221Merge f-q4-kquant (d19ab7302): Metal and SYCL take Q5_K/Q6_K (Metal also Q3_K/Q4_K/IQ4_XS/IQ3_S) down experts for UD files at expert widths 512/1024; additive, today's sets unchanged (Qwen4 prep)99807e8da

main
4.9 KiB99 linesraw
1// metal/src/engine/iq_host.cpp - the host-side helpers of src/kernels/cuda/iq_kernels.cu the shared host code reads
2// (format tables, row sizes, the native expert blob layout).  The kernels themselves are not ported: on Metal the
3// decode window runs mac-kern's chain (metal/src/engine/window.cpp).  Copied from iq_kernels.cu; keep in step.
4#include "strata/kernels/iq_kernels.hpp"
5
6#define GGML_COMMON_DECL_CPP
7#include "ggml-common.h"
8#include "ggml-common-tq.h"   // block_tq2_t / block_tqk6 / block_tqk7
9
10#include <cstddef>
11#include <cstdint>
12
13namespace strata::kernels {
14namespace {
15// agentionai's trellis types (Gyro): TQ2_T (144), TQK6 (145), TQK7 (146); 128 weights a block
16bool is_tq(int t) { return t == 144 || t == 145 || t == 146; }
17bool is_iq(int t) {
18    return t == 16 || t == 17 || t == 18 || t == 20 || t == 21 || t == 22 || t == 23 || t == 29 || t == 42 || t == 11 ||
19           t == 12 || t == 13 || t == 14 || t == 7 || t == 6 || t == 2 || t == 3 || t == 8;   // 14: Q6_K (Unsloth's Q5/Q6 gate/up)
20}
21// values per block of the types the grouped expert kernels take (0 = none): STRATA_GU_FMTS / STRATA_D_FMTS
22int gu_qk(int t) {
23    switch (t) {
24        case 16: case 17: case 18: case 21: case 22: case 23: case 29: case 12: case 13: case 14: return 256;
25        case 42: return 64;
26        case 144: case 145: return 128;   // TQ2_T / TQK6 gate/up (the CUDA engine's STRATA_GU_FMTS)
27        case 8: return 32;   // Q8_0 (the UD-Q5/Q6_K_XL packs); Q5_0 / Q4_0 / Q4_1 gate/up have no Metal kernel (moe.metal GU_SET)
28        default: return 0;
29    }
30}
31int d_qk(int t) {
32    switch (t) {
33        case 23: return 256;
34        case 21: case 11: case 12: case 13: case 14: return 256;   // IQ3_S, Q3_K, Q4_K, Q5_K, Q6_K down (UD files at expert widths divisible by 256: Q4-KQ; moe.metal DNK_SET / DN_SET, moe_mm.metal MM3_DN / MM6_DN1 / MM_DN2_KQ)
35        case 42: return 64;
36        case 144: case 146: return 128;   // TQ2_T / TQK7 down
37        case 20: case 7: case 6: case 8: return 32;   // IQ4_NL, Q5_1, Q5_0 (moe.metal DN_SET since 0.2.11 UD sets), Q8_0; Q4_0 / Q4_1 down have no Metal kernel
38        default: return 0;
39    }
40}
41}  // namespace
42
43bool iq_supported(int t) noexcept { return is_iq(t); }
44bool embed_type_supported(int t) noexcept { return is_iq(t) || t == 30; }
45
46size_t iq_row_bytes(int t, int64_t n) noexcept {
47    switch (t) {
48        case 16: return (size_t) (n / 256) * sizeof(block_iq2_xxs);
49        case 17: return (size_t) (n / 256) * sizeof(block_iq2_xs);
50        case 18: return (size_t) (n / 256) * sizeof(block_iq3_xxs);
51        case 20: return (size_t) (n / 32) * sizeof(block_iq4_nl);
52        case 21: return (size_t) (n / 256) * sizeof(block_iq3_s);
53        case 22: return (size_t) (n / 256) * sizeof(block_iq2_s);
54        case 29: return (size_t) (n / 256) * sizeof(block_iq1_m);
55        case 23: return (size_t) (n / 256) * sizeof(block_iq4_xs);
56        case 11: return (size_t) (n / 256) * sizeof(block_q3_K);
57        case 42: return (size_t) (n / 64) * sizeof(block_q2_0);
58        case 12: return (size_t) (n / 256) * sizeof(block_q4_K);
59        case 13: return (size_t) (n / 256) * sizeof(block_q5_K);
60        case 14: return (size_t) (n / 256) * sizeof(block_q6_K);
61        case 7: return (size_t) (n / 32) * sizeof(block_q5_1);
62        case 6: return (size_t) (n / 32) * sizeof(block_q5_0);
63        case 2: return (size_t) (n / 32) * sizeof(block_q4_0);
64        case 3: return (size_t) (n / 32) * sizeof(block_q4_1);
65        case 8: return (size_t) (n / 32) * sizeof(block_q8_0);
66        case 144: return (size_t) (n / 128) * sizeof(block_tq2_t);
67        case 145: return (size_t) (n / 128) * sizeof(block_tqk6);
68        case 146: return (size_t) (n / 128) * sizeof(block_tqk7);
69        case 30: return (size_t) n * 2;
70        default: return 0;
71    }
72}
73
74bool native_expert_supported(int gu_type, int d_type, int64_t n_embd, int64_t n_ff) noexcept {
75    const int qg = gu_qk(gu_type), qd = d_qk(d_type);
76    return qg > 0 && qd > 0 && (is_iq(gu_type) || is_tq(gu_type)) && (is_iq(d_type) || is_tq(d_type)) && n_embd % qg == 0 && n_ff % qd == 0 &&
77           n_embd % 256 == 0 && (n_ff * n_embd) % 256 == 0;   // (the trellis rows: 128-value blocks, 2560 = 20, 640 = 5)
78}
79
80NativeExpertLayout native_expert_layout(int gu_type, int d_type, int64_t n_embd, int64_t n_ff) {
81    NativeExpertLayout L;
82    L.gu_type = gu_type;
83    L.d_type = d_type;
84    L.n_embd = n_embd;
85    L.n_ff = n_ff;
86    L.gu_row = iq_row_bytes(gu_type, n_embd);
87    L.d_row = iq_row_bytes(d_type, n_ff);
88    L.up_off = (size_t) n_ff * L.gu_row;
89    L.down_off = 2 * L.up_off;
90    L.bytes = L.down_off + (size_t) n_embd * L.d_row;
91    return L;
92}
93
94size_t native_expert_scratch_bytes(int64_t cap, int64_t n_ff) {
95    const size_t f = (size_t) cap * (size_t) n_ff * sizeof(float);
96    return 3 * ((f + 255) & ~(size_t) 255) + (((size_t) cap * (size_t) (n_ff / 32) * sizeof(block_q8_1) + 255) & ~(size_t) 255);
97}
98
99}  // namespace strata::kernels