okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
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 { 23int n = 0 ; 24int k = 0 ; 25int ldy = 0 ; 26int t_bucket = 0 ; 27bool tuned = false;// false: the default kernel measured best at this bucket (the row bounds its neighbours' reach) 28int 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. 34bool load (const std::string & path ,const std::string & expected_arch ,const std::string & expected_version , 35 std::string & err ) { 36 std::ifstream input (path ); 37if (!input ) { 38err = "cannot open rocBLAS tuning file" ; 39return false; 40 } 41bool saw_header = false; 42 std::string line ; 43size_t line_number = 0 ; 44while (std::getline (input ,line )) { 45++ line_number ; 46if (const auto hash = line .find ('#' );hash != std::string::npos )line .resize (hash ); 47const auto first = line .find_first_not_of (" \t\r\n" ); 48if (first == std::string::npos )continue ; 49 std::istringstream row (line .substr (first )); 50if (!saw_header ) { 51 std::string magic ,arch ,version ,extra ; 52if (!(row >>magic >>arch >>version )|| (row >>extra )|| magic != "STRATA_ROCBLAS_TUNING_V1" ) { 53err = "invalid rocBLAS tuning header at line " + std::to_string (line_number ); 54return false; 55 } 56if (arch != expected_arch ) { 57err = "rocBLAS tuning architecture mismatch: file=" + arch + " runtime=" + expected_arch ; 58return false; 59 } 60if (version != expected_version ) { 61err = "rocBLAS version mismatch: file=" + version + " runtime=" + expected_version ; 62return false; 63 } 64arch_ = std::move (arch ); 65version_ = std::move (version ); 66saw_header = true; 67continue ; 68 } 69 std::string type ,solution ,extra ; 70Row parsed ; 71if (!(row >>type >>parsed .n >>parsed .k >>parsed .ldy >>parsed .t_bucket >>solution )|| (row >>extra )|| 72type != "f16o" || parsed .n <=0 || parsed .k <=0 || parsed .ldy < parsed .n || parsed .t_bucket <=0 ) { 73err = "invalid rocBLAS tuning row at line " + std::to_string (line_number ); 74return false; 75 } 76if (solution != "default" ) { 77char * end = nullptr ; 78const long value = std::strtol (solution .c_str (),& end ,10 ); 79if (!end || * end || value < INT_MIN || value > INT_MAX ) { 80err = "invalid rocBLAS solution index at line " + std::to_string (line_number ); 81return false; 82 } 83parsed .tuned = true; 84parsed .solution = (int )value ; 85 } 86for (const auto & existing :rows_ ) { 87if (existing .n == parsed .n && existing .k == parsed .k && existing .ldy == parsed .ldy && 88existing .t_bucket == parsed .t_bucket ) { 89err = "duplicate rocBLAS tuning key at line " + std::to_string (line_number ); 90return false; 91 } 92 } 93rows_ .push_back (parsed ); 94 } 95if (!saw_header ) { 96err = "rocBLAS tuning file has no header" ; 97return false; 98 } 99return 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. 105const Row * closest (int n ,int k ,int ldy ,int t )const { 106const Row * best = nullptr ; 107int64_t best_distance = std::numeric_limits < int64_t > ::max (); 108for (const auto & row :rows_ ) { 109if (row .n != n || row .k != k || row .ldy != ldy )continue ; 110const int64_t distance = row .t_bucket >=t ?int64_t (row .t_bucket )- t :int64_t (t )- row .t_bucket ; 111if (distance < best_distance || (distance == best_distance && row .t_bucket < best -> t_bucket )) { 112best = & row ; 113best_distance = distance ; 114 } 115 } 116return best ; 117 } 118 119const std::vector < Row >& rows ()const {return rows_ ; } 120const std::string & arch ()const {return arch_ ; } 121const 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