char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Hongqiang Wangopencl: general flash attention decode performance optimizations (#25366)6f8895fee

char/aliceai-t5-moe
3.8 KiB92 linesraw
1#pragma once
2
3// Flash-attention per-(dk,dv) tile tuning for the Adreno OpenCL backend.
4// Isolated from ggml-opencl.cpp so the tuning numbers are easy to find and
5// edit; the FA dispatch and kernel-compile logic stay in the main file.
6// This header is a file section — it is #included exactly once, at the point
7// in ggml-opencl.cpp where the ggml logging macros are already in scope.
8
9// Per-(dk, dv) FA config; shared by dispatch and supports_op.
10struct ggml_opencl_fa_dim {
11    int dk; int dv; int bm; int bn; int n_split; int nkv_split_threshold;
12};
13
14// Split variant fires when n_kv >= threshold (threshold=0 -> always split).
15// Default tuning covers Adreno 7xx/8xx mobile and X1-series laptop GPUs.
16static const ggml_opencl_fa_dim g_fa_dims_adreno_default[] = {
17    { 40,  40, 64, 32, 1, 0}, { 64,  64, 64, 32, 2, 64},
18    { 80,  80, 64, 32, 2, 64}, { 96,  96, 64, 32, 2, 64},
19    {112, 112, 64, 32, 2, 64}, {128, 128, 64, 32, 2, 64},
20    {192, 128, 16, 16, 1, 0},
21    {192, 192, 16, 16, 1, 0},
22    {256, 256, 16, 16, 16, 0},
23    {512, 512,  8, 16, 64, 0},
24};
25
26struct ggml_opencl_fa_dim_table {
27    const ggml_opencl_fa_dim * data;
28    size_t                     count;
29
30    const ggml_opencl_fa_dim * begin() const { return data; }
31    const ggml_opencl_fa_dim * end()   const { return data + count; }
32};
33
34// Mutable copy of the active table; GGML_OPENCL_FA_TUNE patches entries here
35// at backend init without touching the const source table.
36static ggml_opencl_fa_dim g_fa_dims_runtime[
37    sizeof(g_fa_dims_adreno_default) / sizeof(g_fa_dims_adreno_default[0])];
38
39static ggml_opencl_fa_dim_table g_opencl_fa_dims = {
40    g_fa_dims_adreno_default,
41    sizeof(g_fa_dims_adreno_default) / sizeof(g_fa_dims_adreno_default[0]),
42};
43
44// GGML_OPENCL_FA_TUNE=dk:dv:bm:bn:nsplit:thr[,…] — patches matching entries
45// in the active table at backend init, before the first FA kernel compiles.
46// Unmatched (dk,dv) pairs are warned and ignored.
47static void ggml_opencl_fa_apply_env_overrides() {
48    const char * e = std::getenv("GGML_OPENCL_FA_TUNE");
49    if (!e || !e[0]) {
50        return;
51    }
52
53    std::string s = e;
54    size_t pos = 0;
55    while (pos < s.size()) {
56        size_t comma = s.find(',', pos);
57        std::string entry = s.substr(pos, comma == std::string::npos ? std::string::npos : comma - pos);
58        int dk, dv, bm, bn, nsplit, thr;
59        if (std::sscanf(entry.c_str(), "%d:%d:%d:%d:%d:%d", &dk, &dv, &bm, &bn, &nsplit, &thr) == 6) {
60            bool patched = false;
61            for (size_t i = 0; i < g_opencl_fa_dims.count; ++i) {
62                ggml_opencl_fa_dim & d = g_fa_dims_runtime[i];
63                if (d.dk == dk && d.dv == dv) {
64                    d.bm = bm; d.bn = bn; d.n_split = nsplit; d.nkv_split_threshold = thr;
65                    GGML_LOG_INFO("ggml_opencl: FA tune override DK=%d DV=%d -> bm=%d bn=%d n_split=%d thr=%d\n",
66                                  dk, dv, bm, bn, nsplit, thr);
67                    patched = true;
68                    break;
69                }
70            }
71            if (!patched) {
72                GGML_LOG_WARN("ggml_opencl: FA tune override DK=%d DV=%d ignored (no matching dim)\n", dk, dv);
73            }
74        } else {
75            GGML_LOG_WARN("ggml_opencl: FA tune override entry malformed: '%s'\n", entry.c_str());
76        }
77        if (comma == std::string::npos) break;
78        pos = comma + 1;
79    }
80}
81
82// Copy the default table into the mutable runtime buffer and apply any
83// GGML_OPENCL_FA_TUNE overrides. A per-generation table can be added here
84// once it has been tuned on hardware.
85static void ggml_cl_init_fa_dims_table() {
86    const size_t count = sizeof(g_fa_dims_adreno_default) / sizeof(g_fa_dims_adreno_default[0]);
87    for (size_t i = 0; i < count; ++i) {
88        g_fa_dims_runtime[i] = g_fa_dims_adreno_default[i];
89    }
90    g_opencl_fa_dims = { g_fa_dims_runtime, count };
91    ggml_opencl_fa_apply_env_overrides();
92}