okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
12d6af092
main
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 { 16InputType type = InputType ::bf16 ; 17int n = 0 ; 18int k = 0 ; 19int ldy = 0 ; 20int t_bucket = 0 ; 21int solution_id = -1 ; 22}; 23 24class TuningTable { 25public : 26bool load (const std::string & path ,const std::string & expected_arch ,int expected_version , std::string & err ) { 27 std::ifstream input (path ); 28if (!input ) { 29err = "cannot open tuning file" ; 30return false; 31 } 32 33bool saw_header = false; 34 std::string line ; 35size_t line_number = 0 ; 36while (std::getline (input ,line )) { 37++ line_number ; 38const auto first = line .find_first_not_of (" \t\r\n" ); 39if (first == std::string::npos || line [first ]== '#' )continue ; 40 41 std::istringstream row (line .substr (first )); 42if (!saw_header ) { 43 std::string magic ; 44 std::string arch ; 45int version = 0 ; 46 std::string extra ; 47if (!(row >>magic >>arch >>version )|| (row >>extra )|| magic != "STRATA_HIPBLASLT_TUNING_V1" ) { 48err = "invalid tuning header at line " + std::to_string (line_number ); 49return 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. 57const auto is_rdna4 = [](const std::string & a ) {return a == "gfx1200" || a == "gfx1201" ; }; 58const auto is_rdna3 = [](const std::string & a ) { 59return a == "gfx1100" || a == "gfx1101" || a == "gfx1102" ; 60 }; 61if (arch != expected_arch && 62 !(is_rdna4 (arch )&& is_rdna4 (expected_arch ))&& 63 !(is_rdna3 (arch )&& is_rdna3 (expected_arch ))) { 64err = "tuning architecture mismatch: file=" + arch + " runtime=" + expected_arch ; 65return false; 66 } 67if (version != expected_version ) { 68err = "hipBLASLt version mismatch: file=" + std::to_string (version )+ 69" runtime=" + std::to_string (expected_version ); 70return false; 71 } 72arch_ = std::move (arch ); 73version_ = version ; 74saw_header = true; 75continue ; 76 } 77 78 std::string type_name ; 79int n = 0 ,k = 0 ,ldy = 0 ,t_bucket = 0 ,solution_id = -1 ; 80 std::string extra ; 81if (!(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 || 83solution_id < 0 ) { 84err = "invalid tuning row at line " + std::to_string (line_number ); 85return false; 86 } 87TuningRow parsed ; 88parsed .type = type_name == "f16" ?InputType ::f16 :InputType ::bf16 ; 89parsed .n = n ; 90parsed .k = k ; 91parsed .ldy = ldy ; 92parsed .t_bucket = t_bucket ; 93parsed .solution_id = solution_id ; 94for (const auto & existing :rows_ ) { 95if (existing .type == parsed .type && existing .n == parsed .n && existing .k == parsed .k && 96existing .ldy == parsed .ldy && existing .t_bucket == parsed .t_bucket ) { 97err = "duplicate tuning key at line " + std::to_string (line_number ); 98return false; 99 } 100 } 101rows_ .push_back (parsed ); 102 } 103if (!saw_header ) { 104err = "tuning file has no header" ; 105return false; 106 } 107return true; 108 } 109 110const TuningRow * closest (InputType type ,int n ,int k ,int ldy ,int t )const { 111const TuningRow * best = nullptr ; 112int64_t best_distance = std::numeric_limits < int64_t > ::max (); 113for (const auto & row :rows_ ) { 114if (row .type != type || row .n != n || row .k != k || row .ldy != ldy )continue ; 115const int64_t distance = row .t_bucket >=t ?int64_t (row .t_bucket )- t :int64_t (t )- row .t_bucket ; 116if (distance < best_distance || 117 (distance == best_distance && (!best || row .t_bucket < best -> t_bucket ))) { 118best = & row ; 119best_distance = distance ; 120 } 121 } 122return best ; 123 } 124 125const std::vector < TuningRow >& rows ()const {return rows_ ; } 126const std::string & arch ()const {return arch_ ; } 127int version ()const {return version_ ; } 128 129private : 130 std::string arch_ ; 131int version_ = 0 ; 132 std::vector < TuningRow > rows_ ; 133}; 134 135}// namespace strata::prefill::hipblaslt