--vram-cap: the blocker is not "used by others"; the clamp subtracted it twice

vram_info computed used = total - free after the blocker of total - cap had been allocated, so the blocker
was in `used` AND already out of `free`: room = cap - used = 2*cap - total - used_other, negative at
--vram-cap 5722 on the 1080 Ti and ~4,800 instead of ~7,900 MiB at 8192 (review finding 1).  The blocker's
bytes are remembered and taken out of `used`.  The cap line now also prints what a card of the cap would show
free (cap - used_before), and the start refuses if the clamped figure is above it or more than 64 MiB below
(the driver rounds the blocker up by a few MiB).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017tPVMg2wGSNR1KANEiUtwr
This commit is contained in:
Robert KarlandClaude Fable 5.1 committed 2026-10-11 11:19:58 -07:00
1 parent fa3a819405
commit 2b4fcf1f8f
1 file changed
+19 -3
+19 -3
View File
@@ -124,10 +124,14 @@ bool under_wddm() {
// would size it), and a blocker of `total - cap` is allocated at start (main, before the weight arena), so the driver // would size it), and a blocker of `total - cap` is allocated at start (main, before the weight arena), so the driver
// is under the pressure the real card would be under. 0 = no cap. // is under the pressure the real card would be under. 0 = no cap.
int64_t g_vram_cap_bytes = 0; int64_t g_vram_cap_bytes = 0;
uint64_t g_vram_blocker_bytes = 0; // the blocker's own allocation, which `used` below must not count twice
cudaError_t vram_info(size_t* free_b, size_t* total_b) { cudaError_t vram_info(size_t* free_b, size_t* total_b) {
const cudaError_t e = cudaMemGetInfo(free_b, total_b); const cudaError_t e = cudaMemGetInfo(free_b, total_b);
if (e == cudaSuccess && g_vram_cap_bytes > 0 && *total_b > 0) { if (e == cudaSuccess && g_vram_cap_bytes > 0 && *total_b > 0) {
const uint64_t used = *total_b > *free_b ? *total_b - *free_b : 0; // this process AND every other one // this process AND every other one, minus the blocker: the blocker already took `total - cap` out of
// `free`, so counting it in `used` as well subtracted it twice (room went negative at --vram-cap 5722)
const uint64_t all = *total_b > *free_b ? *total_b - *free_b : 0;
const uint64_t used = all > g_vram_blocker_bytes ? all - g_vram_blocker_bytes : 0;
const uint64_t room = (uint64_t) g_vram_cap_bytes > used ? (uint64_t) g_vram_cap_bytes - used : 0; const uint64_t room = (uint64_t) g_vram_cap_bytes > used ? (uint64_t) g_vram_cap_bytes - used : 0;
if (*free_b > room) *free_b = (size_t) room; if (*free_b > room) *free_b = (size_t) room;
} }
@@ -2132,12 +2136,24 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
g_vram_cap_bytes = (int64_t) cap; g_vram_cap_bytes = (int64_t) cap;
g_vram_blocker_bytes = hold;
size_t f2 = 0, t2 = 0; size_t f2 = 0, t2 = 0;
vram_info(&f2, &t2); vram_info(&f2, &t2);
// what a card of `cap` would show free with the same other programs on it; the driver may round the
// blocker up by a few MiB, so the figure is a bound, not an identity
const uint64_t expect = cap > used ? cap - used : 0;
std::fprintf(stderr, "strata generate: --vram-cap %lld MiB: %llu MiB of this %llu MiB card held back; %llu MiB " std::fprintf(stderr, "strata generate: --vram-cap %lld MiB: %llu MiB of this %llu MiB card held back; %llu MiB "
"free to this engine (the other %llu MiB were in use before it started)\n", "free to this engine (the other %llu MiB were in use before it started; a %llu MiB card "
"would show %llu)\n",
(long long) o.vram_cap_mib, (unsigned long long) (hold >> 20), (unsigned long long) (total_b >> 20), (long long) o.vram_cap_mib, (unsigned long long) (hold >> 20), (unsigned long long) (total_b >> 20),
(unsigned long long) (f2 >> 20), (unsigned long long) (used >> 20)); (unsigned long long) (f2 >> 20), (unsigned long long) (used >> 20), (unsigned long long) (cap >> 20),
(unsigned long long) (expect >> 20));
if (f2 > expect || expect - f2 > (64ull << 20)) {
std::fprintf(stderr, "strata generate: --vram-cap: the free figure after the blocker is %llu MiB, not the "
"%llu MiB a card of the cap would have: the clamp is wrong\n",
(unsigned long long) (f2 >> 20), (unsigned long long) (expect >> 20));
return 1;
}
} }
} }
// --ram-cap: the RAM tier (--resident-*) and the pinned arena see at most this much host memory // --ram-cap: the RAM tier (--resident-*) and the pinned arena see at most this much host memory