expert cache: STRATA_VRAM_FREE=device reads the whole card's free VRAM through NVML, Windows + NVIDIA, opt-in (#1911)

Under WDDM cudaMemGetInfo answers for the calling process: memory another process holds on the card (the image encoder,
a browser, another engine) is not in its free figure. The reporter measured, on an RTX 5090, 30,994 MiB free from CUDA
where NVML read 24,348 with 6 GiB held elsewhere, so --expert-cache auto sized the cache too large and shrank it by
guesses.

device_free_bytes() now takes min(CUDA's figure, NVML's figure for the whole card) when STRATA_VRAM_FREE=device is set.
nvml.dll is loaded from the system directory at first use (LoadLibraryExW with LOAD_LIBRARY_SEARCH_SYSTEM32), the card
is matched by its PCI bus id, and a missing nvml.dll, a missing export or a failed nvmlInit leaves CUDA's figure with
one line on stderr. The serve check of "VRAM free with everything loaded" uses the same function. The value can only get
smaller, so the switch can only shrink the expert cache.

Unset (the default), nothing changes: the NVML code is compiled only for _WIN32 and not for HIP, Metal or the no-VMM
build, and the env variable is read once. The serve check's min() is the same value elsewhere (on a unified-memory
system device_free_bytes() is never below cudaMemGetInfo's figure). Added to the PR: the NVML block is also left out
of a SYCL build (SYCL_LANGUAGE_VERSION), whose CUDA names are a compatibility layer. docs/DETAILS.md: one row in the
measured-and-left-off table instead of the PR's long paragraph.

Tests: builds on Linux CUDA (the Windows-only block is skipped), SYCL (P1006) and on Windows MSVC (owner PC build of
the final tree). Not covered: no run with a model; the Windows gate is written down.
This commit is contained in:
Xiaomi2008 authored and Niko1221 committed 2026-10-26 18:47:52 +01:00
1 parent 2251053b42
commit 3100ca05ed
4 files changed
+84 -1

No files matched your search

+78
View File
@@ -22,8 +22,65 @@
#include <utility>
#include <cstring>
// (not in a SYCL build: its CUDA names are a compatibility layer without the PCI ids NVML is matched by)
#if defined(_WIN32) && !defined(STRATA_EC_NO_VMM) && !defined(SYCL_LANGUAGE_VERSION)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#define STRATA_EC_NVML_FREE 1
#endif
namespace strata::core {
#if defined(STRATA_EC_NVML_FREE)
namespace {
// STRATA_VRAM_FREE=device (opt-in, Windows + NVIDIA). Under WDDM cudaMemGetInfo answers for THIS process: memory
// another process holds on the card (the image encoder, a browser, another engine) is not in its free figure.
// Measured on an RTX 5090, driver 610.88: with 6 GiB written by another process, cudaMemGetInfo in a second process
// still read 30,994 MiB free where NVML read 24,348. NVML (nvml.dll, installed with every driver) counts the whole
// card. The library is loaded from the system directory only, at first use; false when it cannot answer.
bool nvml_device_free(size_t& out) {
struct Memory { unsigned long long total, free, used; };
typedef int (*InitFn)(void);
typedef int (*ByBusFn)(const char*, void**);
typedef int (*MemFn)(void*, Memory*);
struct Lib {
ByBusFn by_bus = nullptr;
MemFn mem = nullptr;
Lib() {
HMODULE h = LoadLibraryExW(L"nvml.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (h == nullptr) return;
const auto init = (InitFn) (void*) GetProcAddress(h, "nvmlInit_v2");
const auto bus = (ByBusFn) (void*) GetProcAddress(h, "nvmlDeviceGetHandleByPciBusId_v2");
const auto m = (MemFn) (void*) GetProcAddress(h, "nvmlDeviceGetMemoryInfo");
if (init == nullptr || bus == nullptr || m == nullptr || init() != 0) return;
by_bus = bus;
mem = m;
}
};
static const Lib lib;
if (lib.mem == nullptr) return false;
int dev = 0;
cudaDeviceProp prop{};
if (cudaGetDevice(&dev) != cudaSuccess || cudaGetDeviceProperties(&prop, dev) != cudaSuccess) return false;
char bus[32];
std::snprintf(bus, sizeof bus, "%08X:%02X:%02X.0", (unsigned) prop.pciDomainID, (unsigned) prop.pciBusID,
(unsigned) prop.pciDeviceID);
void* handle = nullptr;
Memory m{};
if (lib.by_bus(bus, &handle) != 0 || lib.mem(handle, &m) != 0) return false;
out = (size_t) m.free;
return true;
}
} // namespace
#endif
// See the header. NVIDIA's DGX Spark Porting Guide (section 5.5, "Memory reporting on UMA systems") recommends the same:
// not to rely on cudaMemGetInfo alone but to count the memory the OS can reclaim. Swap is not counted here (unlike
// NVIDIA's reference snippet): an expert cache that pushes the system into swap would be far slower than a smaller one.
@@ -67,6 +124,27 @@ size_t device_free_bytes() {
if (avail > head && avail - head > free_b) free_b = (size_t) (avail - head);
}
}
#endif
#if defined(STRATA_EC_NVML_FREE)
static const bool device_wide = [] {
const char* v = std::getenv("STRATA_VRAM_FREE");
return v != nullptr && std::strcmp(v, "device") == 0;
}();
if (device_wide) {
size_t nvml_b = 0;
const bool got = nvml_device_free(nvml_b);
static std::atomic<bool> said{false};
if (!said.exchange(true)) {
if (got)
std::fprintf(stderr, "strata generate: STRATA_VRAM_FREE=device: free VRAM is read for the whole card "
"(NVML: %lld MiB now; CUDA's figure for this process alone: %lld MiB)\n",
(long long) (nvml_b >> 20), (long long) (free_b >> 20));
else
std::fprintf(stderr, "strata generate: STRATA_VRAM_FREE=device: NVML did not answer (nvml.dll); free "
"VRAM stays CUDA's figure for this process\n");
}
if (got) free_b = std::min(free_b, nvml_b);
}
#endif
return free_b;
}