diff --git a/CMakeLists.txt b/CMakeLists.txt index 8661ccce..ca8c10b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -421,6 +421,15 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) add_test(NAME hip_handoff COMMAND hip_handoff) list(APPEND _strata_hip_tests hip_handoff) endif() + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/hip/mapped_alias.cpp") + # #325: reports what a hipHostGetDevicePointer alias is on this stack (on Windows: the host pointer, and a + # D2D copy into it does not land - hip_handoff's timeout there). A diagnosis: exits 0 whenever it ran. + add_executable(hip_mapped_alias tests/hip/mapped_alias.cpp) + target_link_libraries(hip_mapped_alias PRIVATE strata_kernels strata_hip_runtime) + add_test(NAME hip_mapped_alias COMMAND hip_mapped_alias) + set_tests_properties(hip_mapped_alias PROPERTIES SKIP_RETURN_CODE 77) + list(APPEND _strata_hip_tests hip_mapped_alias) + endif() if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/hip/native_qsa_score.cpp") add_executable(hip_native_qsa_score tests/hip/native_qsa_score.cpp) target_link_libraries(hip_native_qsa_score PRIVATE strata_kernels strata_hip_runtime) diff --git a/src/core/native_head.cpp b/src/core/native_head.cpp index e47dfe14..acb4f81f 100644 --- a/src/core/native_head.cpp +++ b/src/core/native_head.cpp @@ -150,10 +150,31 @@ bool NativeEmbed::load(const std::vector& shards, int64_t n_embd, i } else { std::memcpy(host_, gguf.tensor_data(*t), bytes_); void* d = nullptr; +#if defined(STRATA_USE_HIP) + // #325: the Windows HIP stack can refuse the device alias of a mapped allocation (and, when it gives + // one, it is the host address itself - unified addressing; kernels read it correctly there, a + // device-to-device copy into it does not land: tests/hip/mapped_alias). The table is only gathered from, + // so without an alias it goes into VRAM like the unpinnable case above, instead of failing the start. + if (cudaHostGetDevicePointer(&d, host_, 0) != cudaSuccess || d == nullptr) { + cudaGetLastError(); + d = nullptr; + if (cudaMalloc(&d, bytes_) != cudaSuccess || + cudaMemcpy(d, gguf.tensor_data(*t), bytes_, cudaMemcpyHostToDevice) != cudaSuccess) { + if (d) cudaFree(d); + cudaGetLastError(); + err = "native embedding: no device alias for the mapped table, and no VRAM to copy it into"; + return false; + } + cudaFreeHost(host_); // the destructor frees dev_ when host_ is null + host_ = nullptr; + std::fprintf(stderr, "strata: native embedding: no device alias for the mapped table, kept in VRAM\n"); + } +#else if (cudaHostGetDevicePointer(&d, host_, 0) != cudaSuccess) { err = "native embedding: no device alias for the mapped table"; return false; } +#endif dev_ = d; } type_ = (int) t->type; diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu index 328c8ae4..bb6ef045 100644 --- a/src/prefill/gemm.cu +++ b/src/prefill/gemm.cu @@ -42,6 +42,23 @@ void ck(cublasStatus_t s, const char* what) { } } +// #247/#325: on Windows (seen on gfx1201), hipBLAS can return success with the correct BF16/FP16 product for some +// shapes (hc up once T >= 96, the router) and still leave hipErrorInvalidValue set, which the next kernel's error +// check turns into an exit. The multiply has finished, so that one stale error is cleared after a GEMM that succeeded; +// any other error still stops the engine. Windows only: on Linux a stale hipErrorInvalidValue is a real error from +// an earlier call and keeps being reported. A no-op everywhere else (CUDA compiles none of it). +#if defined(__HIPCC__) && defined(_WIN32) +void absorb_hipblas_sticky(const char* what) { + const hipError_t sticky = hipGetLastError(); + if (sticky == hipSuccess || sticky == hipErrorInvalidValue) return; + std::fprintf(stderr, "prefill gemm: %s left %s\n", what, hipGetErrorString(sticky)); + std::exit(1); +} +#define STRATA_ABSORB_HIPBLAS_STICKY(what) absorb_hipblas_sticky(what) +#else +#define STRATA_ABSORB_HIPBLAS_STICKY(what) ((void) 0) +#endif + // A setup call whose failure the engine survives (the handle keeps its defaults), as before #240 - but said. void note(cublasStatus_t s, const char* what) { if (s != CUBLAS_STATUS_SUCCESS) std::fprintf(stderr, "prefill gemm: %s: cuBLAS status %d (continuing)\n", what, (int) s); @@ -363,6 +380,7 @@ void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64 #if defined(__HIPCC__) && defined(STRATA_HIPBLASLT_AVAILABLE) if (try_hipblaslt(hipblaslt_state_, strata::prefill::hipblaslt::InputType::bf16, X, W, Y, T, N, K, ldy, beta, stream_)) { + STRATA_ABSORB_HIPBLAS_STICKY("hipBLASLt bf16"); return; } #endif @@ -371,6 +389,7 @@ void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64 CUDA_R_16BF, (int) K, X, CUDA_R_16BF, (int) K, &beta, Y, CUDA_R_32F, (int) ldy, CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT), "cublasGemmEx"); + STRATA_ABSORB_HIPBLAS_STICKY("cublasGemmEx"); } void Gemm::f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy, @@ -381,6 +400,7 @@ void Gemm::f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_ #if defined(__HIPCC__) && defined(STRATA_HIPBLASLT_AVAILABLE) if (try_hipblaslt(hipblaslt_state_, strata::prefill::hipblaslt::InputType::f16, X, W, Y, T, N, K, ldy, beta, stream_)) { + STRATA_ABSORB_HIPBLAS_STICKY("hipBLASLt f16"); return; } #endif @@ -388,6 +408,7 @@ void Gemm::f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_ CUDA_R_16F, (int) K, X, CUDA_R_16F, (int) K, &beta, Y, CUDA_R_32F, (int) ldy, CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT), "cublasGemmEx f16"); + STRATA_ABSORB_HIPBLAS_STICKY("cublasGemmEx f16"); } void Gemm::native(const uint16_t* X, int ggml_type, const void* W_blocks, float* Y, int64_t T, int64_t N, int64_t K, diff --git a/tests/hip/mapped_alias.cpp b/tests/hip/mapped_alias.cpp new file mode 100644 index 00000000..0507bb87 --- /dev/null +++ b/tests/hip/mapped_alias.cpp @@ -0,0 +1,108 @@ +// Is a hipHostGetDevicePointer alias usable on this stack? (#325) +// +// tests/hip/handoff.cpp fails on gfx1201/Windows with "separate copy/ring timeout": it does a +// hipMemcpyAsync(..., hipMemcpyDeviceToDevice) into a hipHostGetDevicePointer alias, then waits for +// a doorbell that copy should have published. This test pins down the properties that decide it. +// +// On Linux the alias is a device address of the pinned host memory, a D2D copy lands, the host sees +// it, and a kernel reads it. On Windows (#325, RX 9070 XT) the runtime hands back the HOST pointer +// itself (unified addressing; this test prints both addresses), and a D2D copy into it does not +// publish - hip_handoff's timeout there. The engine itself runs on Windows (the reporter's serving +// run gave correct answers), so kernels reading mapped memory through that address work; only the +// copy-engine path does not. src/core/native_head.cpp keeps the token embedding in VRAM when the +// alias is refused outright. +// +// Reporting, not gating: a Windows host is EXPECTED to fail some checks below, so this test exits 0 +// whenever it ran at all, and 77 (skipped) without a device. When the alias IS the host pointer the +// D2D copy is not attempted (it may fault rather than report); the kernel read is, since that is +// what the engine relies on. + +#include "strata/kernels/elementwise.hpp" + +#include + +#include +#include + +namespace { + +int not_held = 0; + +void report(const char* what, bool holds) { + std::printf(" %-44s %s\n", what, holds ? "holds" : "DOES NOT HOLD"); + if (!holds) ++not_held; +} + +// A kernel reads `n` floats through `alias` into VRAM; the result is copied back and compared. +bool kernel_reads(const float* alias, const std::vector& want) { + const int64_t n = (int64_t) want.size(); + float* out = nullptr; + if (hipMalloc(reinterpret_cast(&out), want.size() * sizeof(float)) != hipSuccess) return false; + strata::kernels::copy_from_mapped(out, alias, n, nullptr); + std::vector got(want.size(), -1.0f); + const bool ran = hipDeviceSynchronize() == hipSuccess && + hipMemcpy(got.data(), out, want.size() * sizeof(float), hipMemcpyDeviceToHost) == hipSuccess; + hipFree(out); + hipGetLastError(); + return ran && got == want; +} + +} // namespace + +int main() { + int runtime = 0; + if (hipRuntimeGetVersion(&runtime) != hipSuccess) return 1; + int count = 0; + if (hipGetDeviceCount(&count) != hipSuccess || count < 1) { + std::printf("no HIP device\n"); + return 77; // ctest "skipped" + } + std::printf("HIP runtime %d, %d device(s)\n", runtime, count); + + const int64_t n = 4096; // enough elements for copy_from_mapped's float4 path + const size_t bytes = size_t(n) * sizeof(float); + std::vector pattern(size_t(n), 0.0f); + for (int64_t i = 0; i < n; ++i) pattern[size_t(i)] = float(i + 1); + + float* host = nullptr; + if (hipHostMalloc(reinterpret_cast(&host), bytes, hipHostMallocMapped) != hipSuccess) return 1; + void* alias = nullptr; + if (hipHostGetDevicePointer(&alias, host, 0) != hipSuccess || alias == nullptr) { + std::printf("hipHostGetDevicePointer failed: native_head.cpp keeps the embedding in VRAM here\n"); + hipGetLastError(); + hipHostFree(host); + return 0; + } + const bool distinct = alias != reinterpret_cast(host); + std::printf("\n host %p\n alias %p\n -> %s\n\n", static_cast(host), alias, + distinct ? "a distinct device address" : "the host pointer itself (unified addressing)"); + report("the alias is a distinct device address", distinct); + + // What the engine relies on: a kernel reading mapped host memory through the alias. + for (int64_t i = 0; i < n; ++i) host[i] = pattern[size_t(i)]; + report("a kernel reads the alias correctly", kernel_reads(reinterpret_cast(alias), pattern)); + + // What tests/hip/handoff.cpp attempts: a DeviceToDevice copy INTO the alias, seen by the host. + if (distinct) { + float* device = nullptr; + std::vector twice(pattern); + for (float& v : twice) v *= 2.0f; + if (hipMalloc(reinterpret_cast(&device), bytes) == hipSuccess && + hipMemcpy(device, twice.data(), bytes, hipMemcpyHostToDevice) == hipSuccess) { + const hipError_t copy = hipMemcpy(alias, device, bytes, hipMemcpyDeviceToDevice); + report("a D2D copy into the alias succeeds", copy == hipSuccess); + bool seen = hipDeviceSynchronize() == hipSuccess; + for (int64_t i = 0; seen && i < n; ++i) seen = host[i] == twice[size_t(i)]; + report("the host sees a D2D copy into the alias", seen); + } + if (device) hipFree(device); + hipGetLastError(); + } else { + std::printf(" the D2D copy into the alias is not attempted: with alias == host it may fault rather than\n" + " report (tests/hip/handoff's timeout on Windows)\n"); + } + + std::printf("\n%d check(s) did not hold (a Windows host is expected to fail some)\n", not_held); + hipHostFree(host); + return 0; +}