okami.mom/strata

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

Niko1221Merge branch 'f-split-int' into f-split-vision7c5e84612

main
5.5 KiB129 linesraw
1// src/prefill/rocblas_tuning.hpp - the rocBLAS solution table of the prompt path's FP16-out GEMMs (HIP, gfx103x).
2//
3// rocBLAS picks a kernel per GEMM shape from its own table, and on gfx1030 that pick is up to 6x slower than the best
4// kernel the library holds for the same shape (N=10240 K=2560 below T=1152: tools/hip/tune_rocblas.cpp).  A table
5// made by that tool names, per shape and token bucket, the solution index to run instead; Gemm::f16_inplace reads it
6// through STRATA_ROCBLAS_TUNING.  Indices are valid for one architecture and one rocBLAS build (its full version
7// string), and the table carries both; any other pair is refused and the default kernels run.
8#pragma once
9
10#include <climits>
11#include <cstdint>
12#include <cstdlib>
13#include <fstream>
14#include <limits>
15#include <sstream>
16#include <string>
17#include <utility>
18#include <vector>
19
20namespace strata::prefill::rocblas_tuning {
21
22struct Row {
23    int n = 0;
24    int k = 0;
25    int ldy = 0;
26    int t_bucket = 0;
27    bool tuned = false;   // false: the default kernel measured best at this bucket (the row bounds its neighbours' reach)
28    int solution = 0;     // rocBLAS solution indices can be negative
29};
30
31class Table {
32public:
33    /// `expected_version`: rocblas_get_version_string() of the library the engine runs with.
34    bool load(const std::string& path, const std::string& expected_arch, const std::string& expected_version,
35              std::string& err) {
36        std::ifstream input(path);
37        if (!input) {
38            err = "cannot open rocBLAS tuning file";
39            return false;
40        }
41        bool saw_header = false;
42        std::string line;
43        size_t line_number = 0;
44        while (std::getline(input, line)) {
45            ++line_number;
46            if (const auto hash = line.find('#'); hash != std::string::npos) line.resize(hash);
47            const auto first = line.find_first_not_of(" \t\r\n");
48            if (first == std::string::npos) continue;
49            std::istringstream row(line.substr(first));
50            if (!saw_header) {
51                std::string magic, arch, version, extra;
52                if (!(row >> magic >> arch >> version) || (row >> extra) || magic != "STRATA_ROCBLAS_TUNING_V1") {
53                    err = "invalid rocBLAS tuning header at line " + std::to_string(line_number);
54                    return false;
55                }
56                if (arch != expected_arch) {
57                    err = "rocBLAS tuning architecture mismatch: file=" + arch + " runtime=" + expected_arch;
58                    return false;
59                }
60                if (version != expected_version) {
61                    err = "rocBLAS version mismatch: file=" + version + " runtime=" + expected_version;
62                    return false;
63                }
64                arch_ = std::move(arch);
65                version_ = std::move(version);
66                saw_header = true;
67                continue;
68            }
69            std::string type, solution, extra;
70            Row parsed;
71            if (!(row >> type >> parsed.n >> parsed.k >> parsed.ldy >> parsed.t_bucket >> solution) || (row >> extra) ||
72                type != "f16o" || parsed.n <= 0 || parsed.k <= 0 || parsed.ldy < parsed.n || parsed.t_bucket <= 0) {
73                err = "invalid rocBLAS tuning row at line " + std::to_string(line_number);
74                return false;
75            }
76            if (solution != "default") {
77                char* end = nullptr;
78                const long value = std::strtol(solution.c_str(), &end, 10);
79                if (!end || *end || value < INT_MIN || value > INT_MAX) {
80                    err = "invalid rocBLAS solution index at line " + std::to_string(line_number);
81                    return false;
82                }
83                parsed.tuned = true;
84                parsed.solution = (int) value;
85            }
86            for (const auto& existing : rows_) {
87                if (existing.n == parsed.n && existing.k == parsed.k && existing.ldy == parsed.ldy &&
88                    existing.t_bucket == parsed.t_bucket) {
89                    err = "duplicate rocBLAS tuning key at line " + std::to_string(line_number);
90                    return false;
91                }
92            }
93            rows_.push_back(parsed);
94        }
95        if (!saw_header) {
96            err = "rocBLAS tuning file has no header";
97            return false;
98        }
99        return true;
100    }
101
102    /// The row of this shape whose token bucket is nearest to t (the smaller bucket on a tie); null when the shape
103    /// has no row at all (never measured).  A `default` row says the default kernel measured best there, so a tuned
104    /// neighbour's solution reaches no further than halfway to it.
105    const Row* closest(int n, int k, int ldy, int t) const {
106        const Row* best = nullptr;
107        int64_t best_distance = std::numeric_limits<int64_t>::max();
108        for (const auto& row : rows_) {
109            if (row.n != n || row.k != k || row.ldy != ldy) continue;
110            const int64_t distance = row.t_bucket >= t ? int64_t(row.t_bucket) - t : int64_t(t) - row.t_bucket;
111            if (distance < best_distance || (distance == best_distance && row.t_bucket < best->t_bucket)) {
112                best = &row;
113                best_distance = distance;
114            }
115        }
116        return best;
117    }
118
119    const std::vector<Row>& rows() const { return rows_; }
120    const std::string& arch() const { return arch_; }
121    const std::string& version() const { return version_; }
122
123private:
124    std::string arch_;
125    std::string version_;
126    std::vector<Row> rows_;
127};
128
129}  // namespace strata::prefill::rocblas_tuning