From f5ab356abd5a1ac2dc645d28ea52483cfed496f0 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 6 Oct 2026 13:19:16 +0200 Subject: [PATCH] hip gfx1151: keep the sign of Q2_0's negative zero; warn when the auto cache outgrows an APU's host memory (PR #895) Two pieces of PR #895 that 0.1.40 did not have; the rest of it is in 0.1.40 (gfx1151 in the arch gate, sudot4, setup and the Strix Halo defaults) or is not taken (the ROCm pin bump for every AMD arch). - dq_q2_0: the compiler on gfx1151 (ROCm 7.13) folds the half path's negative scale times +0 to +0, as HIP 5.7 did on gfx1012. The sign-preserving path now covers gfx1151 too. Changes bits on gfx1151 only (the same value the FP32 and CPU paths give); nothing else is touched. - Linux HIP, --expert-cache auto: an APU's "VRAM" is system RAM, and the device-free figure counts the whole GPU-addressable pool, so a cache sized from it can ask the OOM killer for nearly all RAM. The PR capped the cache at the host's free memory less 4 GiB; here it only prints a warning with both numbers and leaves the cache as asked (recommend, never force). --expert-cache N sets it by hand. Compile check only on HIP; the run needs a gfx1151 box. Co-authored-by: mini --- src/kernels/cuda/iq_kernels.cu | 6 +++--- src/program/generate.cpp | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu index 7a9e4301..1c20986d 100644 --- a/src/kernels/cuda/iq_kernels.cu +++ b/src/kernels/cuda/iq_kernels.cu @@ -1708,9 +1708,9 @@ __device__ void dq_q2_0(const void* vx, int64_t ibs, dst_t* yy, int tid) { for (int j = 0; j < 8; ++j) { const int i = part * 8 + j; const int code = (x[b].qs[i / 4] >> ((i % 4) * 2)) & 3; -#if defined(__HIPCC__) && defined(__gfx1012__) && HIP_VERSION_MAJOR < 7 - // HIP 5.7 on RDNA1 folds the half path's negative scale times +0 - // to +0. Preserve the scale's sign, as the FP32/CPU paths do. +#if defined(__HIPCC__) && ((defined(__gfx1012__) && HIP_VERSION_MAJOR < 7) || defined(__gfx1151__)) + // HIP 5.7 on RDNA1 and the HIP compiler on gfx1151 (PR #895, ROCm 7.13) fold the half path's negative scale + // times +0 to +0. Preserve the scale's sign, as the FP32/CPU paths do. if constexpr (std::is_same_v) { if (code == 1) { yy[b * 64 + i] = __ushort_as_half(__half_as_ushort(x[b].d) & 0x8000u); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 3b551f3a..003bf891 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -3841,6 +3841,26 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: expert cache auto: %.2f GiB free, %d MiB reserved (+%lld MiB for the " "draft head) -> %d slots\n", (double) free_b / 1073741824.0, o.vram_reserve_mib, (long long) (mtp_bind >> 20), o.expert_cache); +#if defined(STRATA_USE_HIP) && !defined(_WIN32) + // An APU's "VRAM" is system RAM: the device-free figure above counts the whole GPU-addressable pool and does not + // subtract ordinary CPU allocations (the host expert arena), so a cache sized from it alone can ask the OOM killer + // for nearly all RAM (PR #895). A warning, not a cap: the cache stays as asked, and --expert-cache N chooses. + { + cudaDeviceProp apu_prop{}; + int apu_dev = 0; + strata::core::detail::HostMemory hm{}; + if (cudaGetDevice(&apu_dev) == cudaSuccess && cudaGetDeviceProperties(&apu_prop, apu_dev) == cudaSuccess && + apu_prop.integrated && strata::core::detail::host_available_memory(hm)) { + const int64_t host_headroom = 4ll << 30; // the OS and request-time CPU work + const int64_t cache_b = (int64_t) o.expert_cache * blob; + if (cache_b > (int64_t) hm.available - host_headroom) + std::fprintf(stderr, "strata generate: WARNING: this GPU shares system RAM, and the cache above (%.2f GiB) is " + "more than the host memory available now (%.2f GiB) less 4 GiB for the OS: the system may " + "run out of memory; --expert-cache N sets the cache by hand\n", + (double) cache_b / 1073741824.0, (double) hm.available / 1073741824.0); + } + } +#endif // #496: the verify window cannot start without a cache (#174), and a cache too small to lend the prompt path // a 256-token chunk's buffers (plus the 128 slots a loan leaves; one slot without --prefill) makes it // allocate its own on top - more than the reserve. When the default reserve leaves less than that (a 6 GB