interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-metal / ggml-metal-tuning.h

75 lines · 2586 bytes Raw Blame History
1#pragma once
2
3#include "ggml.h"
4
5#include <cstdint>
6#include <vector>
7
8namespace ggml_metal_tuning {
9
10// FA vec selection buckets. ne01 (query rows) splits decode (==1) from batch (>=2), the
11// batch side refined into {2,3,4,5}: Q>1 reuses one K/V load across rows, so it only pays
12// off once ne01 aligns with Q. ne11 (KV length) is bucketed too, as the Q>1 crossover is
13// head-size dependent (small dk crosses late, large dk wins even at short KV).
14constexpr int FA_VEC_NE11_BUCKETS[] = { 1024, 4096, 16384 };
15constexpr int FA_VEC_NE01_BUCKETS[] = { 2, 3, 4, 5 };
16
17int fa_vec_ne11_bucket(int64_t ne11);
18int fa_vec_ne01_bucket(int64_t ne01);
19
20// NE baked into each (dk,dv) baseline instantiation in kernels/fa_vec_*.metal.
21// Hand-maintained mirror; keep in sync with those instantiations.
22// The Metal test slice covers every legal config for dk=128 and dk=576.
23int fa_vec_baseline_ne(int dk, int dv);
24
25// Tuned table has two row kinds. Exact rows key a (ne11_b, ne01_b) bucket. Default rows
26// collapse ne11 over one ne01 domain: ne11_b == FA_VEC_NE11_DEFAULT and ne01_b holds the
27// domain. fa_vec_pick tries exact bucket -> domain default -> baseline; short KV
28// (ne11 < FA_VEC_NE11_BUCKETS[0]) always uses baseline.
29constexpr int8_t FA_VEC_NE11_DEFAULT = -1;
30constexpr int8_t FA_VEC_DOMAIN_DECODE = 0; // ne01 == 1
31constexpr int8_t FA_VEC_DOMAIN_BATCH = 1; // ne01 >= 2
32
33struct fa_vec_key_t {
34 int8_t family;
35 int8_t dtype;
36 int16_t dk;
37 int16_t dv;
38 int8_t ne11_b;
39 int8_t ne01_b;
40};
41
42static_assert(sizeof(fa_vec_key_t) == 8, "fa_vec_key_t must be tightly packed for memcmp");
43
44struct fa_vec_cfg_t {
45 int8_t Q;
46 int8_t NE;
47};
48
49struct fa_vec_entry_t {
50 fa_vec_key_t key;
51 fa_vec_cfg_t cfg;
52};
53
54// legal NE values for a (dk,dv): NL = 32/NE, require (dk/4)%NL==0 && (dv/4)%NL==0.
55// single source shared by the offline tuner and test-backend-ops.
56inline std::vector<int> fa_vec_legal_ne(int dk, int dv) {
57 std::vector<int> r;
58 for (int ne : { 1, 2, 4 }) {
59 const int nl = 32 / ne;
60 if ((dk / 4) % nl == 0 && (dv / 4) % nl == 0) {
61 r.push_back(ne);
62 }
63 }
64 return r;
65}
66
67// test/tune-only override; when set, fa_vec_pick returns it directly.
68void fa_vec_set_override(fa_vec_cfg_t cfg);
69void fa_vec_clear_override();
70fa_vec_cfg_t fa_vec_baseline_cfg(int dk, int dv);
71
72// Keyed by Apple GPU family; an untuned family matches no row and gets the baseline.
73fa_vec_cfg_t fa_vec_pick(int gpu_family, int dtype, int dk, int dv, int64_t ne11, int64_t ne01);
74
75} // namespace ggml_metal_tuning