okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1df3a3108
main
1#!/usr/bin/env python3 2"""Compile the production host guard with mock HIP calls; no GPU or HIP SDK needed. 3 4The helper and dispatch expression are extracted from their single production 5definition so this test cannot accidentally validate a copied implementation. 6Run from any directory with Python 3 and a C++17 compiler (CXX, default c++). 7""" 8import os 9from pathlib import Path 10import re 11import shlex 12import subprocess 13import tempfile 14 15 16ROOT = Path (__file__ ).resolve ().parents [1 ] 17source = (ROOT / "src/kernels/cuda/qsa_decode_attn.cu" ).read_text () 18start = source .index ("bool query_swizzle_gfx906_device() {" ) 19end = source .index ("\n}\n" ,start )+ 3 20guard = source [start :end ] 21dispatch = re .search (r"^ const bool use_query_swizzle = [^;]*;" ,source ,re .MULTILINE ) 22if dispatch is None or "if (use_query_swizzle) {" not in source : 23raise RuntimeError ("production query-swizzle dispatch changed; update the test" ) 24 25prefix = r''' 26#include "strata/kernels/gfx_arch.hpp" 27#include <cstdio> 28#include <cstdlib> 29#include <cstring> 30#include <initializer_list> 31#include <thread> 32using strata::kernels::gfx_arch_is; 33constexpr int hipSuccess = 0; 34struct hipDeviceProp_t { char gcnArchName[64]; int warpSize; }; 35struct Mock { 36int device = 0, device_calls = 0, property_calls = 0, clear_calls = 0; 37bool fail_device = false, fail_properties = false; 38}; 39thread_local Mock mock; 40int hipGetDevice(int* device) { 41++mock.device_calls; 42*device = mock.device; 43return mock.fail_device ? 1 : hipSuccess; 44} 45int hipGetDeviceProperties(hipDeviceProp_t* prop, int device) { 46++mock.property_calls; 47if (mock.fail_properties) return 1; 48static const char* names[] = { 49"gfx906", "gfx908", "gfx906:sramecc-:xnack-", "gfx9060", 50"gfx906", "", "gfx906-other", "gfx906" 51}; 52if (device < 0 || device >= 8) return 1; 53std::strcpy(prop->gcnArchName, names[device]); 54prop->warpSize = device == 4 ? 32 : 64; 55return hipSuccess; 56} 57int hipGetLastError() { ++mock.clear_calls; return hipSuccess; } 58int checks = 0; 59void require(bool condition, const char* message) { 60++checks; 61if (!condition) { std::fprintf(stderr, "FAIL: %s\n", message); std::exit(1); } 62} 63''' 64 65tests = r''' 66int main() { 67require(!active(false, false, 1), "opt-in off"); 68require(!active(true, true, 1), "lane-cell wins"); 69for (int mode : {0, 2, 3}) require(!active(true, false, mode), "other pool modes"); 70require(mock.device_calls == 0 && mock.property_calls == 0, "inactive dispatch makes no HIP queries"); 71require(active(true, false, 1), "gfx906 wave64 accepted"); 72require(active(true, false, 1), "same-device cache accepted"); 73require(mock.device_calls == 2 && mock.property_calls == 1, "ordinal checked each time, properties cached"); 74mock.device = 1; 75require(!active(true, false, 1), "gfx908 rejected after device switch"); 76require(!active(true, false, 1) && mock.property_calls == 2, "unsupported result cached"); 77mock.device = 0; 78require(active(true, false, 1) && mock.property_calls == 3, "switch back refreshes properties"); 79mock.device = 2; 80require(active(true, false, 1), "exact gfx906 name with feature suffix accepted"); 81for (int device : {3, 4, 5, 6}) { 82mock.device = device; 83require(!active(true, false, 1), "lookalike name, wave32, or empty name rejected"); 84} 85mock.device = 0; 86require(active(true, false, 1), "restore supported device"); 87int properties = mock.property_calls, clears = mock.clear_calls; 88mock.fail_device = true; 89require(!active(true, false, 1), "current-device API error fails closed"); 90require(mock.property_calls == properties && mock.clear_calls == clears + 1, "device error avoids property query"); 91mock.fail_device = false; 92require(active(true, false, 1) && mock.property_calls == properties + 1, "device error invalidates cache"); 93mock.device = -1; 94require(!active(true, false, 1), "negative ordinal fails closed"); 95mock.device = 0; 96require(active(true, false, 1) && mock.property_calls == properties + 2, "invalid ordinal invalidates cache"); 97mock.device = 7; 98mock.fail_properties = true; 99properties = mock.property_calls; 100clears = mock.clear_calls; 101require(!active(true, false, 1), "property API error cannot reuse another device's cached approval"); 102require(!active(true, false, 1), "repeated property failure stays closed"); 103require(mock.property_calls == properties + 2 && mock.clear_calls == clears + 2, "property errors remain retryable"); 104mock.fail_properties = false; 105require(active(true, false, 1) && mock.property_calls == properties + 3, "property query recovers"); 106mock.device = 0; 107require(active(true, false, 1), "parent thread caches ordinal zero"); 108std::thread other([] { 109require(active(true, false, 1) && mock.property_calls == 1, "new thread independently queries the same ordinal"); 110mock.device = 1; 111require(!active(true, false, 1), "other thread caches an unsupported ordinal"); 112}); 113other.join(); 114properties = mock.property_calls; 115require(active(true, false, 1) && mock.property_calls == properties, "other thread cannot replace parent cache"); 116std::printf("PASS query-swizzle host guard: %d checks (mock HIP, no GPU)\n", checks); 117} 118''' 119 120program = prefix + guard + "\nbool active(bool query_swizzle, bool lane_cell, int kv_mode) {\n" 121program += " const int kernel = -1; // the environment's choice (launch_chunks_batch)\n" + dispatch .group (0 )+ "\n return use_query_swizzle;\n}\n" + tests 122with tempfile .TemporaryDirectory (prefix = "qsa-swizzle-guard-" )as directory : 123cpp = Path (directory )/ "guard_test.cpp" 124binary = Path (directory )/ "guard_test" 125cpp .write_text (program ) 126command = shlex .split (os .environ .get ("CXX" ,"c++" )) 127command += ["-std=c++17" ,"-Wall" ,"-Wextra" ,"-Werror" ,"-pthread" ,"-I" ,str (ROOT / "include" ), 128str (cpp ),"-o" ,str (binary )] 129subprocess .run (command ,check = True ) 130subprocess .run ([str (binary )],check = True )