okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
31da8f5dc
main
1// #477: --expert-profile-save's profile - rank_learned_profile's order (resident first, then the counted routing, then 2// the profile the engine started from) and write_expert_profile's file, byte for byte tools/make_profile.py's format, 3// read back by read_expert_profile. CPU only: no device is touched. 4#include "strata/core/expert_cache.hpp" 5 6#include <cstdio> 7#include <cstring> 8#include <filesystem> 9#include <string> 10#include <utility> 11#include <vector> 12 13namespace { 14int fails = 0 ; 15void check (bool ok ,const char * what ) { 16if (!ok ) { 17 std::fprintf (stderr ,"FAIL: %s\n" ,what ); 18++ fails ; 19 } 20} 21}// namespace 22 23int main () { 24using Pair = std::pair < int32_t ,int32_t > ; 25const int64_t L = 3 ,E = 4 ;// 12 pairs, index = layer * 4 + expert 26 std::vector < uint8_t > resident (12 ,0 ); 27resident [5 ]= 1 ;// (1, 1) 28resident [2 ]= 1 ;// (0, 2) 29 std::vector < double > heat (12 ,0.0 ); 30heat [2 ]= 1.0 ; 31heat [5 ]= 7.0 ; 32heat [11 ]= 9.0 ;// (2, 3): the hottest, but not resident 33heat [0 ]= 3.0 ; 34const std::vector < Pair > prior = {{2 ,0 }, {1 ,3 }, {0 ,1 }};// a profile that ranks three pairs 35const std::vector < Pair > r = strata::core::rank_learned_profile (L ,E ,resident ,heat ,prior ); 36check (r .size ()== 12 ,"every pair is ranked" ); 37const std::vector < Pair > head = {{1 ,1 }, {0 ,2 }, {2 ,3 }, {0 ,0 }, {2 ,0 }, {1 ,3 }, {0 ,1 }, {0 ,3 }}; 38for (size_t i = 0 ;i < head .size ()&& i < r .size ();++ i ) 39check (r [i ]== head [i ],"resident by heat, then the rest by heat, then the prior, then the index" ); 40// no counts at all: the resident ones, then the prior's order 41const std::vector < Pair > r0 = strata::core::rank_learned_profile (L ,E ,resident , {},prior ); 42check (r0 .size ()== 12 && r0 [0 ]== Pair (0 ,2 )&& r0 [1 ]== Pair (1 ,1 )&& r0 [2 ]== Pair (2 ,0 )&& 43r0 [3 ]== Pair (1 ,3 )&& r0 [4 ]== Pair (0 ,1 )&& r0 [5 ]== Pair (0 ,0 ),"without heat: resident, then prior" ); 44 45const std::filesystem::path dir = std::filesystem::temp_directory_path () /"strata_profile_save_test" ; 46 std::filesystem::create_directories (dir ); 47const std::string path = (dir /"learned.bin" ).string (); 48 std::string err ; 49check (strata::core::write_expert_profile (path ,L ,E ,r ,err ),"written" ); 50check (!std::filesystem::exists (path + ".tmp" ),"no temporary file left" ); 51// the bytes make_profile.write_profile would write for the same ranking 52 std::vector < uint8_t > want (4 ); 53 std::memcpy (want .data (),"STRP" ,4 ); 54auto u32 = [& ](uint32_t v ) {for (int b = 0 ;b < 4 ;++ b )want .push_back ((uint8_t ) (v >> (8 * b ))); }; 55auto u16 = [& ](uint32_t v ) {want .push_back ((uint8_t )v );want .push_back ((uint8_t ) (v >>8 )); }; 56for (uint32_t v : {1u ,3u ,4u ,12u ,12u })u32 (v ); 57for (const Pair & p :r ) {u16 ((uint32_t )p .first );u16 ((uint32_t )p .second ); } 58 std::vector < int32_t > table (12 ,-1 ); 59for (size_t i = 0 ;i < r .size ();++ i )table [(size_t ) (r [i ].first * E + r [i ].second )]= (int32_t )i ; 60for (int32_t v :table )u32 ((uint32_t )v ); 61 std::vector < uint8_t > got (want .size ()+ 1 ); 62if (std::FILE * f = std::fopen (path .c_str (),"rb" )) { 63got .resize (std::fread (got .data (),1 ,got .size (),f )); 64 std::fclose (f ); 65 } 66check (got == want ,"the file is make_profile.py's format, byte for byte" ); 67 std::vector < Pair > back ; 68int64_t slots = 0 ; 69check (strata::core::read_expert_profile (path ,L ,E ,back ,slots ,err )&& back == r && slots == 12 , 70"read back by the loader" ); 71// a second save replaces the first (the rename over an existing file) 72check (strata::core::write_expert_profile (path ,L ,E ,r0 ,err ),"written again" ); 73check (strata::core::read_expert_profile (path ,L ,E ,back ,slots ,err )&& back == r0 ,"replaced" ); 74check (!strata::core::write_expert_profile (path ,L ,E , {{3 ,0 }},err ),"a pair out of range is refused" ); 75check (strata::core::read_expert_profile (path ,L ,E ,back ,slots ,err )&& back == r0 , 76"a refused save keeps the file" ); 77 std::filesystem::remove_all (dir ); 78if (fails == 0 ) std::puts ("expert_profile_save_test: OK" ); 79return fails == 0 ?0 :1 ; 80}