okami.mom/strata

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

Niko1221Merge f-022-int (68864c564) into f-022-gyrocuda: release notes keep both sides1df3a3108

main
5.9 KiB130 linesraw
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:
23    raise 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 {
36    int device = 0, device_calls = 0, property_calls = 0, clear_calls = 0;
37    bool fail_device = false, fail_properties = false;
38};
39thread_local Mock mock;
40int hipGetDevice(int* device) {
41    ++mock.device_calls;
42    *device = mock.device;
43    return mock.fail_device ? 1 : hipSuccess;
44}
45int hipGetDeviceProperties(hipDeviceProp_t* prop, int device) {
46    ++mock.property_calls;
47    if (mock.fail_properties) return 1;
48    static const char* names[] = {
49        "gfx906", "gfx908", "gfx906:sramecc-:xnack-", "gfx9060",
50        "gfx906", "", "gfx906-other", "gfx906"
51    };
52    if (device < 0 || device >= 8) return 1;
53    std::strcpy(prop->gcnArchName, names[device]);
54    prop->warpSize = device == 4 ? 32 : 64;
55    return hipSuccess;
56}
57int hipGetLastError() { ++mock.clear_calls; return hipSuccess; }
58int checks = 0;
59void require(bool condition, const char* message) {
60    ++checks;
61    if (!condition) { std::fprintf(stderr, "FAIL: %s\n", message); std::exit(1); }
62}
63'''
64
65tests = r'''
66int main() {
67    require(!active(false, false, 1), "opt-in off");
68    require(!active(true, true, 1), "lane-cell wins");
69    for (int mode : {0, 2, 3}) require(!active(true, false, mode), "other pool modes");
70    require(mock.device_calls == 0 && mock.property_calls == 0, "inactive dispatch makes no HIP queries");
71    require(active(true, false, 1), "gfx906 wave64 accepted");
72    require(active(true, false, 1), "same-device cache accepted");
73    require(mock.device_calls == 2 && mock.property_calls == 1, "ordinal checked each time, properties cached");
74    mock.device = 1;
75    require(!active(true, false, 1), "gfx908 rejected after device switch");
76    require(!active(true, false, 1) && mock.property_calls == 2, "unsupported result cached");
77    mock.device = 0;
78    require(active(true, false, 1) && mock.property_calls == 3, "switch back refreshes properties");
79    mock.device = 2;
80    require(active(true, false, 1), "exact gfx906 name with feature suffix accepted");
81    for (int device : {3, 4, 5, 6}) {
82        mock.device = device;
83        require(!active(true, false, 1), "lookalike name, wave32, or empty name rejected");
84    }
85    mock.device = 0;
86    require(active(true, false, 1), "restore supported device");
87    int properties = mock.property_calls, clears = mock.clear_calls;
88    mock.fail_device = true;
89    require(!active(true, false, 1), "current-device API error fails closed");
90    require(mock.property_calls == properties && mock.clear_calls == clears + 1, "device error avoids property query");
91    mock.fail_device = false;
92    require(active(true, false, 1) && mock.property_calls == properties + 1, "device error invalidates cache");
93    mock.device = -1;
94    require(!active(true, false, 1), "negative ordinal fails closed");
95    mock.device = 0;
96    require(active(true, false, 1) && mock.property_calls == properties + 2, "invalid ordinal invalidates cache");
97    mock.device = 7;
98    mock.fail_properties = true;
99    properties = mock.property_calls;
100    clears = mock.clear_calls;
101    require(!active(true, false, 1), "property API error cannot reuse another device's cached approval");
102    require(!active(true, false, 1), "repeated property failure stays closed");
103    require(mock.property_calls == properties + 2 && mock.clear_calls == clears + 2, "property errors remain retryable");
104    mock.fail_properties = false;
105    require(active(true, false, 1) && mock.property_calls == properties + 3, "property query recovers");
106    mock.device = 0;
107    require(active(true, false, 1), "parent thread caches ordinal zero");
108    std::thread other([] {
109        require(active(true, false, 1) && mock.property_calls == 1, "new thread independently queries the same ordinal");
110        mock.device = 1;
111        require(!active(true, false, 1), "other thread caches an unsupported ordinal");
112    });
113    other.join();
114    properties = mock.property_calls;
115    require(active(true, false, 1) && mock.property_calls == properties, "other thread cannot replace parent cache");
116    std::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:
123    cpp = Path(directory) / "guard_test.cpp"
124    binary = Path(directory) / "guard_test"
125    cpp.write_text(program)
126    command = shlex.split(os.environ.get("CXX", "c++"))
127    command += ["-std=c++17", "-Wall", "-Wextra", "-Werror", "-pthread", "-I", str(ROOT / "include"),
128                str(cpp), "-o", str(binary)]
129    subprocess.run(command, check=True)
130    subprocess.run([str(binary)], check=True)