okami.mom/strata

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

Niko1221Merge branch 'f-133-winamd' into rel-0.1.346edbe9d6e

main
5.1 KiB108 linesraw
1// Is a hipHostGetDevicePointer alias usable on this stack? (#325)
2//
3// tests/hip/handoff.cpp fails on gfx1201/Windows with "separate copy/ring timeout": it does a
4// hipMemcpyAsync(..., hipMemcpyDeviceToDevice) into a hipHostGetDevicePointer alias, then waits for
5// a doorbell that copy should have published. This test pins down the properties that decide it.
6//
7// On Linux the alias is a device address of the pinned host memory, a D2D copy lands, the host sees
8// it, and a kernel reads it. On Windows (#325, RX 9070 XT) the runtime hands back the HOST pointer
9// itself (unified addressing; this test prints both addresses), and a D2D copy into it does not
10// publish - hip_handoff's timeout there. The engine itself runs on Windows (the reporter's serving
11// run gave correct answers), so kernels reading mapped memory through that address work; only the
12// copy-engine path does not. src/core/native_head.cpp keeps the token embedding in VRAM when the
13// alias is refused outright.
14//
15// Reporting, not gating: a Windows host is EXPECTED to fail some checks below, so this test exits 0
16// whenever it ran at all, and 77 (skipped) without a device. When the alias IS the host pointer the
17// D2D copy is not attempted (it may fault rather than report); the kernel read is, since that is
18// what the engine relies on.
19
20#include "strata/kernels/elementwise.hpp"
21
22#include <hip/hip_runtime.h>
23
24#include <cstdio>
25#include <vector>
26
27namespace {
28
29int not_held = 0;
30
31void report(const char* what, bool holds) {
32    std::printf("  %-44s %s\n", what, holds ? "holds" : "DOES NOT HOLD");
33    if (!holds) ++not_held;
34}
35
36// A kernel reads `n` floats through `alias` into VRAM; the result is copied back and compared.
37bool kernel_reads(const float* alias, const std::vector<float>& want) {
38    const int64_t n = (int64_t) want.size();
39    float* out = nullptr;
40    if (hipMalloc(reinterpret_cast<void**>(&out), want.size() * sizeof(float)) != hipSuccess) return false;
41    strata::kernels::copy_from_mapped(out, alias, n, nullptr);
42    std::vector<float> got(want.size(), -1.0f);
43    const bool ran = hipDeviceSynchronize() == hipSuccess &&
44                     hipMemcpy(got.data(), out, want.size() * sizeof(float), hipMemcpyDeviceToHost) == hipSuccess;
45    hipFree(out);
46    hipGetLastError();
47    return ran && got == want;
48}
49
50}  // namespace
51
52int main() {
53    int runtime = 0;
54    if (hipRuntimeGetVersion(&runtime) != hipSuccess) return 1;
55    int count = 0;
56    if (hipGetDeviceCount(&count) != hipSuccess || count < 1) {
57        std::printf("no HIP device\n");
58        return 77;  // ctest "skipped"
59    }
60    std::printf("HIP runtime %d, %d device(s)\n", runtime, count);
61
62    const int64_t n = 4096;                       // enough elements for copy_from_mapped's float4 path
63    const size_t bytes = size_t(n) * sizeof(float);
64    std::vector<float> pattern(size_t(n), 0.0f);
65    for (int64_t i = 0; i < n; ++i) pattern[size_t(i)] = float(i + 1);
66
67    float* host = nullptr;
68    if (hipHostMalloc(reinterpret_cast<void**>(&host), bytes, hipHostMallocMapped) != hipSuccess) return 1;
69    void* alias = nullptr;
70    if (hipHostGetDevicePointer(&alias, host, 0) != hipSuccess || alias == nullptr) {
71        std::printf("hipHostGetDevicePointer failed: native_head.cpp keeps the embedding in VRAM here\n");
72        hipGetLastError();
73        hipHostFree(host);
74        return 0;
75    }
76    const bool distinct = alias != reinterpret_cast<void*>(host);
77    std::printf("\n  host  %p\n  alias %p\n  -> %s\n\n", static_cast<void*>(host), alias,
78                distinct ? "a distinct device address" : "the host pointer itself (unified addressing)");
79    report("the alias is a distinct device address", distinct);
80
81    // What the engine relies on: a kernel reading mapped host memory through the alias.
82    for (int64_t i = 0; i < n; ++i) host[i] = pattern[size_t(i)];
83    report("a kernel reads the alias correctly", kernel_reads(reinterpret_cast<const float*>(alias), pattern));
84
85    // What tests/hip/handoff.cpp attempts: a DeviceToDevice copy INTO the alias, seen by the host.
86    if (distinct) {
87        float* device = nullptr;
88        std::vector<float> twice(pattern);
89        for (float& v : twice) v *= 2.0f;
90        if (hipMalloc(reinterpret_cast<void**>(&device), bytes) == hipSuccess &&
91            hipMemcpy(device, twice.data(), bytes, hipMemcpyHostToDevice) == hipSuccess) {
92            const hipError_t copy = hipMemcpy(alias, device, bytes, hipMemcpyDeviceToDevice);
93            report("a D2D copy into the alias succeeds", copy == hipSuccess);
94            bool seen = hipDeviceSynchronize() == hipSuccess;
95            for (int64_t i = 0; seen && i < n; ++i) seen = host[i] == twice[size_t(i)];
96            report("the host sees a D2D copy into the alias", seen);
97        }
98        if (device) hipFree(device);
99        hipGetLastError();
100    } else {
101        std::printf("  the D2D copy into the alias is not attempted: with alias == host it may fault rather than\n"
102                    "  report (tests/hip/handoff's timeout on Windows)\n");
103    }
104
105    std::printf("\n%d check(s) did not hold (a Windows host is expected to fail some)\n", not_held);
106    hipHostFree(host);
107    return 0;
108}