From aafec91773e059ffdfe81c6553c458fc1c688165 Mon Sep 17 00:00:00 2001 From: neuromanMD Date: Wed, 11 Nov 2026 16:53:54 +0100 Subject: [PATCH] libr3d: mxfp4a8 -- do not scan a VRAM e8 plane from the host e8_scan_once used hipHostGetDevicePointer to decide whether the host can read the e8 plane. It succeeds for device memory on a large-BAR discrete GPU, so the host byte-scanned VRAM over PCIe (~12 MB/s measured on a Radeon Pro W7900: 8 s for a 34816 x 5120 plane), and the 16-entry cache was smaller than a DFlash2 drafter's ~36 MXFP4 planes, so every issued pass rescanned most of them -- 70-120 s first requests and ~1 tok/s decode at long context. Scan only host memory (hipPointerGetAttributes), unbounded seen-set; the kernel's e8 guard already gives reserved values the scale zero in device planes. --- libr3d/r3d_gemm_mxfp4a8.hip | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/libr3d/r3d_gemm_mxfp4a8.hip b/libr3d/r3d_gemm_mxfp4a8.hip index a95533e6..4ad0cefa 100644 --- a/libr3d/r3d_gemm_mxfp4a8.hip +++ b/libr3d/r3d_gemm_mxfp4a8.hip @@ -75,6 +75,9 @@ #include "r3d_launch.h" #include +#include +#include +#include #include #include @@ -530,17 +533,21 @@ extern "C" int r3d_gemm_nt_q_mxfp4a8_tiled(long a, long a_scale, long b_codes, l * weight at a stable address. The host must be able to read the plane (the mapped tier's * placement); a plane it cannot see is not scanned, and the kernel's e8 guard handles reserved * values there. */ -struct E8ScanKey { const void* p; long long n; }; +/* VRAM is not scanned: hipHostGetDevicePointer succeeds for device memory on a large-BAR dGPU, which made + * the host loop read the plane byte by byte over PCIe (multi-second issue stalls), and a 16-entry cache was + * smaller than a DFlash2 drafter's planes. The kernel's e8 guard maps reserved values to scale 0. */ static bool e8_scan_once(const void* p, long long n) { - static E8ScanKey seen[16]; - static int ns = 0; - for (int i = 0; i < ns; ++i) - if (seen[i].p == p && seen[i].n == n) return true; - if (ns < 16) seen[ns++] = E8ScanKey{ p, n }; - void* dev = nullptr; - if (hipHostGetDevicePointer(&dev, (void*)p, 0) != hipSuccess) return true; - const uint8_t* e8 = (const uint8_t*)p; + static std::mutex mu; + static std::set> seen; + { + std::lock_guard lk(mu); + if (!seen.insert({ p, n }).second) return true; + } + hipPointerAttribute_t at{}; + if (hipPointerGetAttributes(&at, p) != hipSuccess) { (void)hipGetLastError(); return true; } + if (at.type != hipMemoryTypeHost) return true; /* device / managed: not scanned */ + const uint8_t* e8 = (const uint8_t*)(at.hostPointer ? at.hostPointer : p); for (long long i = 0; i < n; ++i) if (e8[i] == 0 || e8[i] == 255) return false; return true;