okami.mom/strata

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

Niko1221Merge f-mac-rel (Mac Metal support) into f-144-rel6a74320ca

main
3.3 KiB66 linesraw
1// metal/src/engine/mmvq_host.cpp - the host-side helpers of src/kernels/cuda/native_mmvq.cu the shared host code reads
2// (which GGUF types the dense projections take, their byte sizes).  Copied from native_mmvq.cu; keep in step.  The
3// kernels are not ported: on Metal the decode window runs mac-kern's GEMVs (metal/src/engine/window.cpp).
4#include "strata/kernels/iq_kernels.hpp"
5#include "strata/kernels/native_mmvq.hpp"
6
7#include <cstddef>
8#include <cstdlib>
9#include <limits>
10#include <stdexcept>
11
12namespace strata::kernels {
13namespace {
14constexpr int Q8K = 32;
15constexpr std::size_t kQ81Block = 4 + 32;   // half2 ds + int8 qs[32]
16void validate_shape(int n_in, int ncols, int block_elems = Q8K) {
17    if (n_in <= 0 || n_in % block_elems != 0)
18        throw std::invalid_argument("native MMVQ requires n_in > 0 and divisible by its block element count");
19    if (ncols < 1 || ncols > 8) throw std::invalid_argument("native MMVQ requires 1 <= ncols <= 8");
20}
21}  // namespace
22
23bool native_mmvq_supported(int ggml_type) noexcept {
24    return ggml_type == 2 || ggml_type == 6 || ggml_type == 7 || ggml_type == 8 || ggml_type == 11 ||
25           ggml_type == 12 || ggml_type == 13 || ggml_type == 14 || ggml_type == 20 ||
26           ggml_type == 23 || ggml_type == 42 || ggml_type == 16 || ggml_type == 17 || ggml_type == 18 ||
27           ggml_type == 21 || ggml_type == 22 || ggml_type == 29;
28}
29
30std::size_t native_mmvq_weight_bytes(int ggml_type, int n_in, int n_out) {
31    int block_elems, block_bytes;
32    switch (ggml_type) {
33        case 2: block_elems = 32; block_bytes = 18; break;
34        case 6: block_elems = 32; block_bytes = 22; break;
35        case 7: block_elems = 32; block_bytes = 24; break;
36        case 8: block_elems = 32; block_bytes = 34; break;
37        case 20: block_elems = 32; block_bytes = 18; break;
38        case 11: block_elems = 256; block_bytes = 110; break;
39        case 12: block_elems = 256; block_bytes = 144; break;
40        case 13: block_elems = 256; block_bytes = 176; break;
41        case 14: block_elems = 256; block_bytes = 210; break;
42        case 23: block_elems = 256; block_bytes = 136; break;
43        case 42: block_elems = 64; block_bytes = 18; break;
44        case 16: case 17: case 18: case 21: case 22: case 29:
45            block_elems = 256; block_bytes = (int) iq_row_bytes(ggml_type, 256); break;
46        default: throw std::invalid_argument("unsupported native MMVQ GGML type");
47    }
48    validate_shape(n_in, 1, block_elems);
49    if (n_out <= 0) throw std::invalid_argument("native MMVQ requires n_out > 0");
50    const std::size_t row_bytes = std::size_t(n_in / block_elems) * block_bytes;
51    if (row_bytes > std::numeric_limits<std::size_t>::max() / std::size_t(n_out))
52        throw std::length_error("native MMVQ weight byte count overflows size_t");
53    return row_bytes * std::size_t(n_out);
54}
55
56std::size_t native_q8_1_bytes(int n_in, int ncols) {
57    validate_shape(n_in, ncols);
58    return std::size_t(ncols) * std::size_t(n_in / Q8K) * kQ81Block;
59}
60std::size_t native_q8_1_il_bytes(int n_in, int ncols) { return native_q8_1_bytes(n_in, ncols) * 2; }
61bool native_mmvq_il_supported(int, int, int) { return false; }
62bool native_q8_0_packed_eligible(int, int) { return false; }
63bool native_q8_0_packed_enabled() { return false; }
64bool native_q6_k_packed_enabled() { return false; }
65
66}  // namespace strata::kernels