okami.mom/strata

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

Niko1221Windows AMD: the HIP runtime next to strata.exe, not an AMD driver's System32 copy (#468, #461)a2edcb12f

main
6.0 KiB122 linesraw
1// src/core/device_main.cpp - `strata-device`: report the GPU, the plan, and exercise the arena.
2//
3// This is P2.S1's "startup prints the memory plan vs actual cudaMemGetInfo" bullet, on its own so it can run
4// without the model.  It is also the run-time half of the sm_120 policy: CMake refuses to COMPILE for another
5// architecture, and this refuses to RUN on one.
6#include "strata/core/device.hpp"
7#include "strata/plan/plan.hpp"
8
9#include <cstdio>
10#include <cstring>
11#include <string>
12#if defined(STRATA_USE_HIP) && defined(_WIN32)
13#ifndef NOMINMAX
14#define NOMINMAX
15#endif
16#include <windows.h>
17#endif
18
19static std::string human(uint64_t b) {
20    char buf[64];
21    std::snprintf(buf, sizeof(buf), "%.3f GiB (%llu B)", (double) b / (1024.0 * 1024 * 1024),
22                  (unsigned long long) b);
23    return buf;
24}
25
26int main(int argc, char** argv) {
27    bool selftest = false;
28    bool list_devices = false;
29    for (int i = 1; i < argc; ++i) {
30        if (std::strcmp(argv[i], "--selftest") == 0) selftest = true;
31        else if (std::strcmp(argv[i], "--list-devices") == 0) list_devices = true;
32        else if (std::strcmp(argv[i], "--help") == 0 || std::strcmp(argv[i], "-h") == 0) {
33            std::printf("usage: strata-device [--selftest] [--list-devices]\n"
34                        "  --list-devices  every GPU the runtime enumerates, numbered as HIP_VISIBLE_DEVICES /\n"
35                        "                  CUDA_VISIBLE_DEVICES number them, and whether this binary can run it\n");
36            return 0;
37        } else {
38            std::fprintf(stderr, "unknown argument: %s\n", argv[i]);
39            return 2;
40        }
41    }
42
43    // The runtime's numbering, which setup needs on Windows: there an integrated Radeon is HIP device 0 and pushes the
44    // discrete card to 1, while setup finds the cards in the display-adapter order (#325).  No arch check here - the
45    // cards this binary has no code for are part of the answer.  Format (setup.py's hip_devices parses it):
46    //   device N: <name>
47    //     arch gfx1201, 15.9 GiB, wave32          (CUDA: compute capability 12.0, 11.9 GiB)
48    //     cannot run: <why>                       (only for a card this binary cannot run)
49    if (list_devices) {
50        const int count = strata::core::device_count();
51        if (count == 0) std::printf("(no GPU device)\n");
52        for (int ordinal = 0; ordinal < count; ++ordinal) {
53            std::string name, detail;
54            if (!strata::core::device_summary(ordinal, name, detail)) {
55                std::printf("device %d: (the runtime cannot describe it)\n", ordinal);
56                continue;
57            }
58            std::printf("device %d: %s\n  %s\n", ordinal, name.c_str(), detail.c_str());
59            if (const std::string why = strata::core::gpu_arch_problem(ordinal); !why.empty())
60                std::printf("  cannot run: %s\n", why.c_str());
61        }
62        return 0;
63    }
64
65    try {
66        const strata::core::DeviceInfo d = strata::core::device_info(0);
67        std::printf("device %d: %s\n", d.ordinal, d.name.c_str());
68#if defined(STRATA_USE_HIP)
69        std::printf("  HIP arch            %s wave32 (compiled for %s)\n", d.arch.c_str(),
70                    strata::core::compiled_gpu_archs());
71#else
72        std::printf("  compute capability  %d.%d   (sm_%d%d)\n", d.cc_major, d.cc_minor, d.cc_major, d.cc_minor);
73#endif
74        std::printf("  multiprocessors     %d\n", d.multi_processor_count);
75        std::printf("  VRAM total / free   %s / %s\n", human(d.total_bytes).c_str(), human(d.free_bytes).c_str());
76        std::printf("  driver / runtime    %d / %d\n", d.driver_version, d.runtime_version);
77#if defined(STRATA_USE_HIP) && defined(_WIN32)
78        // #468 #461: which HIP runtime this process loaded - the one beside the exe, or an AMD driver's System32 copy
79        if (HMODULE h = GetModuleHandleA("amdhip64_7.dll")) {
80            char path[MAX_PATH] = {};
81            if (GetModuleFileNameA(h, path, MAX_PATH) > 0) std::printf("  HIP runtime         %s\n", path);
82        }
83#endif
84
85        // The planner's view against the card's.  A plan that does not fit in what is actually FREE is the
86        // failure this print exists to make visible at startup rather than at token 4000.
87        const auto plan = strata::plan::make_plan(20480, strata::plan::Geometry{}, strata::plan::Costs{});
88        std::printf("\n%s", strata::plan::to_string(plan).c_str());
89        std::printf("  card free           %s\n", human(d.free_bytes).c_str());
90        std::printf("  plan + KV vs free   %s\n",
91                    plan.vram_budget <= d.free_bytes ? "FITS" : "*** DOES NOT FIT ***");
92
93        if (selftest) {
94            // Exercise the arena for real: allocate, write from the host, read back, and check the poison
95            // path leaves NaNs rather than zeros.  A GPU test that only asks the driver for its name does not
96            // test the runtime this file exists to provide.
97            const uint64_t bytes = 64ull << 20;      // 64 MiB, small enough to be safe on any card
98            strata::core::DeviceArena arena(bytes, 0, /*poison=*/true);
99            void* a = arena.alloc(1 << 20, 256);
100            void* b = arena.alloc(1 << 20, 4096);
101            if (((uintptr_t) a % 256) || ((uintptr_t) b % 4096)) {
102                std::fprintf(stderr, "selftest: alignment not honoured\n");
103                return 1;
104            }
105            std::printf("\nselftest: arena %s, used %s after two 1 MiB allocations\n", human(arena.capacity()).c_str(),
106                        human(arena.used()).c_str());
107            // and the arena must REFUSE rather than wrap
108            try {
109                arena.alloc(bytes * 2);
110                std::fprintf(stderr, "selftest: over-allocation did NOT throw\n");
111                return 1;
112            } catch (const strata::core::CudaError&) {
113                std::printf("selftest: over-allocation refused as required\n");
114            }
115            std::printf("strata-device selftest OK\n");
116        }
117        return 0;
118    } catch (const std::exception& e) {
119        std::fprintf(stderr, "strata-device: %s\n", e.what());
120        return 1;
121    }
122}