okami.mom/strata

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

waveringanaserve: restore the DeepSeek V4.1 server paths, tools and headers removed upstream, keeping the native picture tower firstba8745e13

main
17.8 KiB324 linesraw
1// umaprobe (HIP, Windows): what a unified-memory APU (Strix Halo, gfx1151) lets the GPU use, and how fast.
2//   umaprobe info                       device figures, hipMemGetInfo, the DXGI video memory budget, system RAM
3//   umaprobe alloc <GiB cap>            hipMalloc 1 GiB chunks until failure or the cap: how much, where RAM goes, GPU read GB/s per region
4//   umaprobe reg <GiB> [pagesMB]        hipHostRegister of GiB of VirtualAlloc'd memory: works?, time, GPU read GB/s through it
5//   umaprobe regchunk <GiB> <chunk GiB>  register in pieces
6//   umaprobe hostalloc <GiB>            hipHostMalloc (mapped) of GiB: works?, GPU read GB/s
7//   umaprobe copy                       hipMemcpy H2D / D2H pageable and pinned
8// Build: tools\dsv41\win\build_umaprobe.bat (ROCm clang, gfx1151).  Internal tool, not shipped.
9#include <hip/hip_runtime.h>
10#include <windows.h>
11#include <psapi.h>
12#include <dxgi1_4.h>
13#include <chrono>
14#include <cstdint>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <string>
19#include <vector>
20
21#define CK(x) do { hipError_t e_ = (x); if (e_ != hipSuccess) { std::printf("%s:%d %s -> %s\n", __FILE__, __LINE__, #x, hipGetErrorString(e_)); std::exit(1); } } while (0)
22static double now() { return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count(); }
23static double ram_avail_gib() { MEMORYSTATUSEX m{}; m.dwLength = sizeof m; GlobalMemoryStatusEx(&m); return m.ullAvailPhys / 1073741824.0; }
24static double ram_total_gib() { MEMORYSTATUSEX m{}; m.dwLength = sizeof m; GlobalMemoryStatusEx(&m); return m.ullTotalPhys / 1073741824.0; }
25
26static void dxgi_line(const char* tag) {
27    static IDXGIAdapter3* ad = nullptr;
28    if (!ad) {
29        hipDeviceProp_t p{}; hipGetDeviceProperties(&p, 0);
30        LUID luid; std::memcpy(&luid, p.luid, sizeof luid);
31        IDXGIFactory4* f = nullptr; CreateDXGIFactory1(__uuidof(IDXGIFactory4), (void**) &f);
32        if (f) { f->EnumAdapterByLuid(luid, __uuidof(IDXGIAdapter3), (void**) &ad); f->Release(); }
33    }
34    if (!ad) { std::printf("%s dxgi: no adapter\n", tag); return; }
35    DXGI_QUERY_VIDEO_MEMORY_INFO l{}, n{};
36    ad->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &l);
37    ad->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &n);
38    std::printf("%s dxgi local budget %.1f GiB usage %.1f | non-local budget %.1f GiB usage %.1f | RAM avail %.1f / %.1f GiB\n", tag,
39                l.Budget / 1073741824.0, l.CurrentUsage / 1073741824.0, n.Budget / 1073741824.0, n.CurrentUsage / 1073741824.0, ram_avail_gib(), ram_total_gib());
40}
41
42__global__ void read_kernel(const uint4* __restrict__ p, size_t n, uint32_t* out) {
43    uint32_t acc = 0;
44    for (size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t) gridDim.x * blockDim.x) {
45        const uint4 v = p[i];
46        acc += v.x ^ v.y ^ v.z ^ v.w;
47    }
48    if (acc == 0x12345678u) out[0] = acc;
49}
50
51static double gpu_read_gbps(const void* p, size_t bytes, int reps = 5) {
52    uint32_t* o; CK(hipMalloc(&o, 64));
53    const size_t n = bytes / 16;
54    const int blocks = 4096, threads = 256;
55    read_kernel<<<blocks, threads>>>((const uint4*) p, n, o);
56    CK(hipDeviceSynchronize());
57    double best = 0;
58    for (int r = 0; r < reps; ++r) {
59        const double t0 = now();
60        read_kernel<<<blocks, threads>>>((const uint4*) p, n, o);
61        CK(hipDeviceSynchronize());
62        const double g = bytes / 1e9 / (now() - t0);
63        if (g > best) best = g;
64    }
65    hipFree(o);
66    return best;
67}
68
69static void info() {
70    int nd = 0; CK(hipGetDeviceCount(&nd));
71    for (int d = 0; d < nd; ++d) {
72        hipDeviceProp_t p{}; CK(hipGetDeviceProperties(&p, d));
73        std::printf("dev %d %s arch %s CUs %d totalGlobalMem %.2f GiB integrated %d canMapHostMemory %d managed %d pageableMemoryAccess %d unifiedAddr %d\n", d, p.name, p.gcnArchName,
74                    p.multiProcessorCount, p.totalGlobalMem / 1073741824.0, p.integrated, p.canMapHostMemory, p.managedMemory, p.pageableMemoryAccess, p.unifiedAddressing);
75    }
76    size_t f, t; CK(hipMemGetInfo(&f, &t));
77    std::printf("hipMemGetInfo free %.2f total %.2f GiB\n", f / 1073741824.0, t / 1073741824.0);
78    dxgi_line("info");
79    {   // the adapter's own split: the BIOS carve-out (dedicated) and the shared (system RAM) part
80        hipDeviceProp_t p{}; hipGetDeviceProperties(&p, 0);
81        LUID luid; std::memcpy(&luid, p.luid, sizeof luid);
82        IDXGIFactory4* f = nullptr; CreateDXGIFactory1(__uuidof(IDXGIFactory4), (void**) &f);
83        IDXGIAdapter1* a = nullptr;
84        if (f && SUCCEEDED(f->EnumAdapterByLuid(luid, __uuidof(IDXGIAdapter1), (void**) &a))) {
85            DXGI_ADAPTER_DESC1 d{}; a->GetDesc1(&d);
86            std::printf("adapter dedicated video %.1f GiB, dedicated system %.1f GiB, shared system %.1f GiB\n", d.DedicatedVideoMemory / 1073741824.0,
87                        d.DedicatedSystemMemory / 1073741824.0, d.SharedSystemMemory / 1073741824.0);
88            a->Release();
89        }
90        if (f) f->Release();
91    }
92}
93
94static void alloc(int cap) {
95    info();
96    std::vector<void*> ch;
97    const size_t C = 1ull << 30;
98    for (int i = 0; i < cap; ++i) {
99        void* p = nullptr;
100        const double t0 = now();
101        if (hipMalloc(&p, C) != hipSuccess) { (void) hipGetLastError(); std::printf("hipMalloc failed at chunk %d (%d GiB allocated)\n", i, i); break; }
102        CK(hipMemset(p, 1, C));
103        ch.push_back(p);
104        if ((i + 1) % 8 == 0 || i == 0) {
105            size_t f, t; hipMemGetInfo(&f, &t);
106            char tag[64]; std::snprintf(tag, sizeof tag, "after %d GiB (%.2fs/chunk) memfree %.1f", i + 1, now() - t0, f / 1073741824.0);
107            dxgi_line(tag);
108        }
109    }
110    std::printf("allocated %zu GiB\n", ch.size());
111    // read bandwidth per 4 GiB window: the first (carve-out?) and the last (shared?)
112    for (size_t w : {size_t(0), ch.size() / 2, ch.size() >= 4 ? ch.size() - 1 : 0}) {
113        if (ch.empty()) break;
114        std::printf("GPU read chunk %zu (1 GiB): %.1f GB/s\n", w, gpu_read_gbps(ch[w], C));
115    }
116    for (void* p : ch) hipFree(p);
117}
118
119static void reg(int gib, int pages_mb) {
120    info();
121    const size_t n = (size_t) gib << 30;
122    double t0 = now();
123    void* p = VirtualAlloc(nullptr, n, MEM_COMMIT | MEM_RESERVE | (pages_mb > 0 ? MEM_LARGE_PAGES : 0), PAGE_READWRITE);
124    if (!p) { std::printf("VirtualAlloc %d GiB failed %lu\n", gib, GetLastError()); return; }
125    std::memset(p, 7, n);
126    std::printf("committed+touched %d GiB in %.1f s\n", gib, now() - t0);
127    dxgi_line("committed");
128    t0 = now();
129    hipError_t e = hipHostRegister(p, n, hipHostRegisterPortable | hipHostRegisterMapped);
130    std::printf("hipHostRegister(%d GiB, mapped|portable): %s in %.2f s\n", gib, hipGetErrorString(e), now() - t0);
131    if (e != hipSuccess) { (void) hipGetLastError(); e = hipHostRegister(p, n, hipHostRegisterPortable); std::printf("hipHostRegister(portable only): %s\n", hipGetErrorString(e)); (void) hipGetLastError(); }
132    dxgi_line("registered");
133    if (e == hipSuccess) {
134        void* dp = nullptr;
135        if (hipHostGetDevicePointer(&dp, p, 0) != hipSuccess) { (void) hipGetLastError(); dp = p; }
136        const size_t w = std::min<size_t>(n, 4ull << 30);
137        std::printf("GPU read of registered host memory (%zu GiB window): %.1f GB/s%s\n", w >> 30, gpu_read_gbps(dp, w), dp == p ? " (same ptr)" : " (device ptr)");
138        t0 = now();
139        for (size_t o = 0; o + (1ull << 30) <= n; o += (1ull << 30)) { /* touch pattern: sequential 1 GiB reads */ }
140        void* d; if (hipMalloc(&d, 1ull << 30) == hipSuccess) {
141            t0 = now(); for (int i = 0; i < 4; ++i) CK(hipMemcpy(d, p, 1ull << 30, hipMemcpyHostToDevice));
142            std::printf("hipMemcpy H2D from registered memory: %.1f GB/s\n", 4.0 / (now() - t0));
143            hipFree(d);
144        }
145        hipHostUnregister(p);
146    }
147    VirtualFree(p, 0, MEM_RELEASE);
148}
149
150static void regchunk(int gib, int chunk) {
151    info();
152    const size_t C = (size_t) chunk << 30;
153    std::vector<void*> v;
154    double t0 = now();
155    for (int o = 0; o < gib; o += chunk) {
156        void* p = VirtualAlloc(nullptr, C, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
157        if (!p) { std::printf("VirtualAlloc failed at %d GiB\n", o); break; }
158        std::memset(p, 5, C);
159        const double t1 = now();
160        const hipError_t e = hipHostRegister(p, C, hipHostRegisterPortable | hipHostRegisterMapped);
161        std::printf("register chunk at %d GiB (%d GiB): %s in %.2f s\n", o, chunk, hipGetErrorString(e), now() - t1);
162        std::fflush(stdout);
163        if (e != hipSuccess) { (void) hipGetLastError(); break; }
164        v.push_back(p);
165    }
166    std::printf("registered %zu chunks in %.1f s\n", v.size(), now() - t0);
167    dxgi_line("regchunk");
168    for (size_t i = 0; i < v.size(); i += std::max<size_t>(1, v.size() / 3)) {
169        void* dp = nullptr; if (hipHostGetDevicePointer(&dp, v[i], 0) != hipSuccess) { (void) hipGetLastError(); dp = v[i]; }
170        std::printf("GPU read chunk %zu: %.1f GB/s\n", i, gpu_read_gbps(dp, std::min<size_t>(C, 4ull << 30)));
171    }
172    for (void* p : v) { hipHostUnregister(p); VirtualFree(p, 0, MEM_RELEASE); }
173}
174
175
176// regsub: the engine's store pattern: one big VirtualAlloc(MEM_RESERVE), committed in chunks of N MiB (untouched or touched), each registered mapped
177static void regsub(int chunk_mib, int nchunks, int touch) {
178    info();
179    const size_t C = (size_t) chunk_mib << 20;
180    uint8_t* base = (uint8_t*) VirtualAlloc(nullptr, C * (size_t) nchunks, MEM_RESERVE, PAGE_READWRITE);
181    if (!base) { std::printf("reserve failed\n"); return; }
182    std::vector<void*> dev;
183    for (int i = 0; i < nchunks; ++i) {
184        uint8_t* p = base + C * (size_t) i;
185        if (!VirtualAlloc(p, C, MEM_COMMIT, PAGE_READWRITE)) { std::printf("commit failed at %d\n", i); break; }
186        if (touch) std::memset(p, 7, C);
187        hipError_t e = hipHostRegister(p, C, hipHostRegisterPortable | hipHostRegisterMapped);
188        void* dp = nullptr;
189        if (e == hipSuccess) e = hipHostGetDevicePointer(&dp, p, 0);
190        std::printf("chunk %d: register+devptr %s, dev %p host %p\n", i, hipGetErrorString(e), dp, (void*) p);
191        if (e != hipSuccess) { (void) hipGetLastError(); break; }
192        dev.push_back(dp);
193        if (!touch) std::memset(p, 7, C);   // (the store's reads fill it after the registration)
194    }
195    for (size_t i = 0; i < dev.size(); ++i) {
196        std::printf("GPU read chunk %zu: %.1f GB/s\n", i, gpu_read_gbps(dev[i], C));
197        std::fflush(stdout);
198    }
199}
200
201static void hostalloc(int gib) {
202    info();
203    void* p = nullptr;
204    const size_t n = (size_t) gib << 30;
205    const double t0 = now();
206    hipError_t e = hipHostMalloc(&p, n, hipHostMallocMapped | hipHostMallocPortable);
207    std::printf("hipHostMalloc(%d GiB, mapped): %s in %.2f s\n", gib, hipGetErrorString(e), now() - t0);
208    if (e != hipSuccess) return;
209    std::memset(p, 3, n);
210    dxgi_line("hostmalloc");
211    void* dp = nullptr; if (hipHostGetDevicePointer(&dp, p, 0) != hipSuccess) { (void) hipGetLastError(); dp = p; }
212    std::printf("GPU read of hipHostMalloc memory: %.1f GB/s\n", gpu_read_gbps(dp, std::min<size_t>(n, 4ull << 30)));
213    hipHostFree(p);
214}
215
216static void copy() {
217    for (size_t mib : {16, 64, 256, 1024}) {
218        const size_t n = mib << 20; void *d, *hp, *hg; hipStream_t s; CK(hipStreamCreate(&s)); CK(hipMalloc(&d, n));
219        CK(hipHostMalloc(&hp, n, hipHostMallocDefault)); hg = _aligned_malloc(n, 4096); std::memset(hp, 1, n); std::memset(hg, 1, n);
220        for (int pinned = 1; pinned >= 0; --pinned) {
221            void* src = pinned ? hp : hg;
222            for (int w = 0; w < 2; ++w) CK(hipMemcpyAsync(d, src, n, hipMemcpyHostToDevice, s));
223            CK(hipStreamSynchronize(s));
224            const int reps = (int) std::max<size_t>(4, 2048 / mib); double t0 = now();
225            for (int i = 0; i < reps; ++i) CK(hipMemcpyAsync(d, src, n, hipMemcpyHostToDevice, s));
226            CK(hipStreamSynchronize(s));
227            std::printf("H2D %s %5zu MiB: %.1f GB/s\n", pinned ? "pinned  " : "pageable", mib, (double) n * reps / 1e9 / (now() - t0));
228        }
229        hipFree(d); hipHostFree(hp); _aligned_free(hg); hipStreamDestroy(s);
230    }
231}
232
233// ver: which amdhip64 this process really loaded (path, file version), the HIP runtime / driver versions
234static void ver() {
235    int rv = 0, dv = 0; hipRuntimeGetVersion(&rv); hipDriverGetVersion(&dv);
236    std::printf("hipRuntimeGetVersion %d, hipDriverGetVersion %d\n", rv, dv);
237    HMODULE h = GetModuleHandleA("amdhip64_7.dll");
238    if (!h) h = GetModuleHandleA("amdhip64.dll");
239    char path[MAX_PATH] = {0};
240    if (h) GetModuleFileNameA(h, path, MAX_PATH);
241    std::printf("loaded amdhip64: %s\n", path);
242    DWORD hd = 0, sz = path[0] ? GetFileVersionInfoSizeA(path, &hd) : 0;
243    if (sz) {
244        std::vector<char> b(sz);
245        VS_FIXEDFILEINFO* fi = nullptr; UINT n = 0;
246        if (GetFileVersionInfoA(path, 0, sz, b.data()) && VerQueryValueA(b.data(), "\\", (void**) &fi, &n) && fi)
247            std::printf("file version %u.%u.%u.%u\n", HIWORD(fi->dwFileVersionMS), LOWORD(fi->dwFileVersionMS), HIWORD(fi->dwFileVersionLS), LOWORD(fi->dwFileVersionLS));
248    }
249    hipDeviceProp_t p{}; hipGetDeviceProperties(&p, 0);
250    std::printf("device %s %s\n", p.name, p.gcnArchName);
251    info();
252}
253
254__global__ void sum_kernel(const unsigned long long* __restrict__ p, size_t n, unsigned long long* out) {
255    unsigned long long acc = 0;
256    for (size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t) gridDim.x * blockDim.x) acc += p[i];
257    atomicAdd(out, acc);
258}
259static void m1_line(const char* what, int seg, double secs, double avail0, size_t reg_total) {
260    PROCESS_MEMORY_COUNTERS_EX pm{}; pm.cb = sizeof pm; GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*) &pm, sizeof pm);
261    const double avail = ram_avail_gib();
262    std::printf("M1 %s seg %d: %.2fs | registered %.1f GiB | avail %.1f GiB (fell %.1f = %.2fx the registered) | private %.1f GiB ws %.1f GiB faults %lu\n", what, seg, secs,
263                reg_total / 1073741824.0, avail, avail0 - avail, reg_total ? (avail0 - avail) / (reg_total / 1073741824.0) : 0.0, pm.PrivateUsage / 1073741824.0,
264                pm.WorkingSetSize / 1073741824.0, pm.PageFaultCount);
265    dxgi_line("   ");
266    std::fflush(stdout);
267}
268// m1 <segment MiB> <total GiB> <mapped|coarse|plain>: reserve total, then per segment commit + touch (a known pattern), register (flags per mode),
269// device pointer, a checksum kernel that reads every page and compares with the pattern's sum; every step logs DXGI, RAM, commit, faults.
270// 'coarse' = hipHostRegisterMapped | hipExtHostRegisterCoarseGrained; 'plain' = portable only (no map).  A fresh process per arm, a watchdog outside.
271static void m1(int seg_mib, int total_gib, const std::string& mode) {
272    ver();
273    const size_t S = (size_t) seg_mib << 20, T = (size_t) total_gib << 30;
274    const int nseg = (int) (T / S);
275    uint8_t* base = (uint8_t*) VirtualAlloc(nullptr, T, MEM_RESERVE, PAGE_READWRITE);
276    if (!base) { std::printf("reserve failed\n"); return; }
277    unsigned flags = hipHostRegisterPortable;
278    if (mode == "mapped") flags |= hipHostRegisterMapped;
279    if (mode == "coarse") flags |= hipHostRegisterMapped | hipExtHostRegisterCoarseGrained;
280    unsigned long long* dsum = nullptr; CK(hipMalloc(&dsum, 8));
281    const double avail0 = ram_avail_gib();
282    m1_line("start", 0, 0, avail0, 0);
283    size_t reg = 0;
284    for (int i = 0; i < nseg; ++i) {
285        uint8_t* p = base + S * (size_t) i;
286        double t0 = now();
287        if (!VirtualAlloc(p, S, MEM_COMMIT, PAGE_READWRITE)) { std::printf("commit failed at segment %d\n", i); break; }
288        unsigned long long* w = (unsigned long long*) p; const size_t nw = S / 8;
289        unsigned long long expect = 0;
290        for (size_t k = 0; k < nw; ++k) { w[k] = (unsigned long long) i * 1000003ull + k; expect += w[k]; }
291        const double tt = now() - t0;
292        t0 = now();
293        hipError_t e = hipHostRegister(p, S, flags);
294        void* dp = p;
295        if (e == hipSuccess && (flags & hipHostRegisterMapped)) { e = hipHostGetDevicePointer(&dp, p, 0); }
296        const double tr = now() - t0;
297        if (e != hipSuccess) { std::printf("M1 register segment %d FAILED: %s (after %.2fs)\n", i, hipGetErrorString(e), tr); (void) hipGetLastError(); m1_line("failed", i, tr, avail0, reg); break; }
298        reg += S;
299        unsigned long long zero = 0; CK(hipMemcpy(dsum, &zero, 8, hipMemcpyHostToDevice));
300        t0 = now();
301        sum_kernel<<<2048, 256>>>((const unsigned long long*) dp, nw, dsum);
302        hipError_t ek = hipDeviceSynchronize();
303        const double tk = now() - t0;
304        unsigned long long got = 0; hipMemcpy(&got, dsum, 8, hipMemcpyDeviceToHost);
305        std::printf("M1 segment %d: touch %.2fs register %.2fs read %.3fs (%.0f GB/s) checksum %s\n", i, tt, tr, tk, S / 1e9 / (std::max)(tk, 1e-9), ek != hipSuccess ? hipGetErrorString(ek) : (got == expect ? "OK" : "MISMATCH"));
306        if (ek != hipSuccess) { m1_line("kernel fault", i, tk, avail0, reg); break; }
307        if (i % (std::max)(1, nseg / 8) == 0 || i == nseg - 1) m1_line("step", i, tr, avail0, reg);
308    }
309    m1_line("end", nseg, 0, avail0, reg);
310}
311
312int main(int argc, char** argv) {
313    const std::string m = argc > 1 ? argv[1] : "";
314    if (m == "ver") { ver(); return 0; }
315    if (m == "m1") { m1(argc > 2 ? std::atoi(argv[2]) : 1024, argc > 3 ? std::atoi(argv[3]) : 8, argc > 4 ? argv[4] : "mapped"); return 0; }
316    if (m == "info") info();
317    else if (m == "alloc") alloc(argc > 2 ? std::atoi(argv[2]) : 80);
318    else if (m == "reg") reg(argc > 2 ? std::atoi(argv[2]) : 8, argc > 3 ? std::atoi(argv[3]) : 0);
319    else if (m == "regchunk") regchunk(argc > 2 ? std::atoi(argv[2]) : 32, argc > 3 ? std::atoi(argv[3]) : 4);
320    else if (m == "regsub") regsub(argc > 2 ? std::atoi(argv[2]) : 660, argc > 3 ? std::atoi(argv[3]) : 8, argc > 4 ? std::atoi(argv[4]) : 0);
321    else if (m == "hostalloc") hostalloc(argc > 2 ? std::atoi(argv[2]) : 8);
322    else if (m == "copy") copy();
323    else std::printf("usage: umaprobe info|alloc N|reg GiB|hostalloc GiB|copy\n");
324}