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.
This commit is contained in:
neuromanMD committed 2026-11-11 16:53:54 +01:00
1 parent 451b136874
commit aafec91773
1 file changed
+16 -9
+16 -9
View File
@@ -75,6 +75,9 @@
#include "r3d_launch.h" #include "r3d_launch.h"
#include <cstring> #include <cstring>
#include <mutex>
#include <set>
#include <utility>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@@ -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 * 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 * placement); a plane it cannot see is not scanned, and the kernel's e8 guard handles reserved
* values there. */ * 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 bool e8_scan_once(const void* p, long long n)
{ {
static E8ScanKey seen[16]; static std::mutex mu;
static int ns = 0; static std::set<std::pair<const void*, long long>> seen;
for (int i = 0; i < ns; ++i) {
if (seen[i].p == p && seen[i].n == n) return true; std::lock_guard<std::mutex> lk(mu);
if (ns < 16) seen[ns++] = E8ScanKey{ p, n }; if (!seen.insert({ p, n }).second) return true;
void* dev = nullptr; }
if (hipHostGetDevicePointer(&dev, (void*)p, 0) != hipSuccess) return true; hipPointerAttribute_t at{};
const uint8_t* e8 = (const uint8_t*)p; 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) for (long long i = 0; i < n; ++i)
if (e8[i] == 0 || e8[i] == 255) return false; if (e8[i] == 0 || e8[i] == 255) return false;
return true; return true;