okami.mom/strata

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

Soren-ldRefactor architecture check for tuning compatibility12d6af092

main
5.4 KiB135 linesraw
1#pragma once
2
3#include <cstdint>
4#include <fstream>
5#include <limits>
6#include <sstream>
7#include <string>
8#include <utility>
9#include <vector>
10
11namespace strata::prefill::hipblaslt {
12
13enum class InputType : uint8_t { f16, bf16 };
14
15struct TuningRow {
16    InputType type = InputType::bf16;
17    int n = 0;
18    int k = 0;
19    int ldy = 0;
20    int t_bucket = 0;
21    int solution_id = -1;
22};
23
24class TuningTable {
25public:
26    bool load(const std::string& path, const std::string& expected_arch, int expected_version, std::string& err) {
27        std::ifstream input(path);
28        if (!input) {
29            err = "cannot open tuning file";
30            return false;
31        }
32
33        bool saw_header = false;
34        std::string line;
35        size_t line_number = 0;
36        while (std::getline(input, line)) {
37            ++line_number;
38            const auto first = line.find_first_not_of(" \t\r\n");
39            if (first == std::string::npos || line[first] == '#') continue;
40
41            std::istringstream row(line.substr(first));
42            if (!saw_header) {
43                std::string magic;
44                std::string arch;
45                int version = 0;
46                std::string extra;
47                if (!(row >> magic >> arch >> version) || (row >> extra) || magic != "STRATA_HIPBLASLT_TUNING_V1") {
48                    err = "invalid tuning header at line " + std::to_string(line_number);
49                    return false;
50                }
51                // A table's solution IDs are scoped to its hipBLASLt version and its architecture FAMILY:
52                // gfx1200/gfx1201 (RDNA4) share them, as gfx1100/gfx1101/gfx1102 (RDNA3) do.  A card can report
53                // its sibling - an R9700 (gfx1201) under HSA_OVERRIDE_GFX_VERSION=12.0.0 reports gfx1200 - and
54                // must still load its own calibration.  A solution ID that does not exist on the running card
55                // fails the per-solution gate in prefill/gemm.cu (getAlgosFromIndex / matmulIsAlgoSupported) and
56                // falls back to hipBLASEx, so the header gate does not need to be exact.
57                const auto is_rdna4 = [](const std::string& a) { return a == "gfx1200" || a == "gfx1201"; };
58                const auto is_rdna3 = [](const std::string& a) {
59                    return a == "gfx1100" || a == "gfx1101" || a == "gfx1102";
60                };
61                if (arch != expected_arch &&
62                    !(is_rdna4(arch) && is_rdna4(expected_arch)) &&
63                    !(is_rdna3(arch) && is_rdna3(expected_arch))) {
64                    err = "tuning architecture mismatch: file=" + arch + " runtime=" + expected_arch;
65                    return false;
66                }
67                if (version != expected_version) {
68                    err = "hipBLASLt version mismatch: file=" + std::to_string(version) +
69                          " runtime=" + std::to_string(expected_version);
70                    return false;
71                }
72                arch_ = std::move(arch);
73                version_ = version;
74                saw_header = true;
75                continue;
76            }
77
78            std::string type_name;
79            int n = 0, k = 0, ldy = 0, t_bucket = 0, solution_id = -1;
80            std::string extra;
81            if (!(row >> type_name >> n >> k >> ldy >> t_bucket >> solution_id) || (row >> extra) ||
82                (type_name != "f16" && type_name != "bf16") || n <= 0 || k <= 0 || ldy < n || t_bucket <= 0 ||
83                solution_id < 0) {
84                err = "invalid tuning row at line " + std::to_string(line_number);
85                return false;
86            }
87            TuningRow parsed;
88            parsed.type = type_name == "f16" ? InputType::f16 : InputType::bf16;
89            parsed.n = n;
90            parsed.k = k;
91            parsed.ldy = ldy;
92            parsed.t_bucket = t_bucket;
93            parsed.solution_id = solution_id;
94            for (const auto& existing : rows_) {
95                if (existing.type == parsed.type && existing.n == parsed.n && existing.k == parsed.k &&
96                    existing.ldy == parsed.ldy && existing.t_bucket == parsed.t_bucket) {
97                    err = "duplicate tuning key at line " + std::to_string(line_number);
98                    return false;
99                }
100            }
101            rows_.push_back(parsed);
102        }
103        if (!saw_header) {
104            err = "tuning file has no header";
105            return false;
106        }
107        return true;
108    }
109
110    const TuningRow* closest(InputType type, int n, int k, int ldy, int t) const {
111        const TuningRow* best = nullptr;
112        int64_t best_distance = std::numeric_limits<int64_t>::max();
113        for (const auto& row : rows_) {
114            if (row.type != type || row.n != n || row.k != k || row.ldy != ldy) continue;
115            const int64_t distance = row.t_bucket >= t ? int64_t(row.t_bucket) - t : int64_t(t) - row.t_bucket;
116            if (distance < best_distance ||
117                (distance == best_distance && (!best || row.t_bucket < best->t_bucket))) {
118                best = &row;
119                best_distance = distance;
120            }
121        }
122        return best;
123    }
124
125    const std::vector<TuningRow>& rows() const { return rows_; }
126    const std::string& arch() const { return arch_; }
127    int version() const { return version_; }
128
129private:
130    std::string arch_;
131    int version_ = 0;
132    std::vector<TuningRow> rows_;
133};
134
135}  // namespace strata::prefill::hipblaslt