Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions app/mem_admit.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,29 @@ constexpr uint64_t kMiB = 1ull << 20, kGiB = 1ull << 30;
// backstop cannot disagree about where the wall is.
constexpr uint64_t kGttReserveDefaultMiB = 16384;

inline uint64_t gtt_reserve_bytes() {
// PRECEDENCE, stated once so a future change knows what it must match: the explicit knob (GGML_HRX_GTT_RESERVE_MB),
// then the 16 GiB cap, then a quarter of the pool. The fork's copy of this function carries the same three rules and
// must keep carrying them -- the planner refuses a load the allocation-time backstop would allow, or the reverse, if
// the two disagree.
//
// engine#2986: the 16 GiB default was sized on a 120 GiB GTT box (#680) and is right there, but it is a *fixed* number:
// on a machine whose GTT is 16 GiB (a 32 GB box) it leaves a ceiling of zero, so HRX refuses every model -- and on a 16 GB
// box (about 8 GiB GTT) it refuses before arithmetic. The reserve therefore scales with the pool it protects: a quarter of
// GTT, capped at the measured 16 GiB. For GTT >= 64 GiB that is exactly the old number (so #680's headroom is unchanged,
// bit for bit); below it the box keeps three quarters of its pool usable instead of losing all of it.
inline uint64_t gtt_reserve_bytes(uint64_t gtt_total) {
if (const char* e = std::getenv("GGML_HRX_GTT_RESERVE_MB")) {
const long long mb = std::atoll(e);
if (mb >= 0) return uint64_t(mb) * kMiB;
}
return kGttReserveDefaultMiB * kMiB;
// engine#2995 review: `gtt_total == 0` means the pool could not be READ, not that it is empty. Without this line
// `quarter` is 0, `0 < cap` is true, and the function returns no reserve at all -- failing open on exactly the input
// where the answer is unknown, so a 32 GB box whose GTT read fails would be admitted like a box with an infinite pool.
// The measured-safe value is the right answer to "I cannot tell".
if (gtt_total == 0) return kGttReserveDefaultMiB * kMiB;
const uint64_t quarter = gtt_total / 4;
const uint64_t cap = kGttReserveDefaultMiB * kMiB;
return quarter < cap ? quarter : cap;
}

// The metadata a size estimate needs: integer scalars, integer arrays and general.architecture.
Expand Down Expand Up @@ -363,7 +380,7 @@ inline Verdict judge(const Need& need, const sysinfo::Memory& mem, uint64_t rese
const uint64_t gtt_free = mem.gtt_total > mem.gtt_used ? mem.gtt_total - mem.gtt_used : 0;
v.free = std::min(v.free, gtt_free);
v.box = "GTT " + gib(gtt_free) + " of " + gib(mem.gtt_total) + " GiB free, " + v.box;
const uint64_t gtt_reserve = gtt_reserve_bytes();
const uint64_t gtt_reserve = gtt_reserve_bytes(mem.gtt_total);
v.ceiling = mem.gtt_total > gtt_reserve ? mem.gtt_total - gtt_reserve : 0;
v.projected = mem.gtt_used + need.total;
v.over_ceiling = v.projected > v.ceiling;
Expand Down
46 changes: 44 additions & 2 deletions tests/mem_admit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,36 @@ int main() {
expect("12 GiB on the CPU: GTT does not count, MemAvailable 50 GiB does", v.fits && v.free == box.mem_available);
need.total = 60ull << 30;
expect("60 GiB on the CPU: refused", !admit::judge(need, box).fits);

// engine#2986: the reserve was a fixed 16 GiB, sized on this 120 GiB box, so a machine whose GTT is 16 GiB had a
// ceiling of zero and HRX refused every model. It now scales as a quarter of GTT, capped at 16 GiB -- which is
// exactly the old number from 64 GiB up (#680's measured headroom unchanged) and leaves three quarters usable below.
{
auto ceiling_of = [&](uint64_t gtt_total) {
sysinfo::Memory m = box;
m.gtt_total = gtt_total;
m.gtt_used = 0;
return admit::judge(need, m);
};
const uint64_t gib = 1ull << 30;
expect("120 GiB of GTT keeps the measured 16 GiB reserve (104 GiB ceiling)",
admit::gtt_reserve_bytes(120 * gib) == 16 * gib);
expect("64 GiB of GTT is the crossover: still a 16 GiB reserve",
admit::gtt_reserve_bytes(64 * gib) == 16 * gib);
expect("32 GiB of GTT gets an 8 GiB reserve, not 16",
admit::gtt_reserve_bytes(32 * gib) == 8 * gib);
expect("16 GiB of GTT (a 32 GB machine) gets a 4 GiB reserve, so the ceiling is not zero",
admit::gtt_reserve_bytes(16 * gib) == 4 * gib);
expect("8 GiB of GTT (a 16 GB machine) keeps a 2 GiB reserve and a usable ceiling",
admit::gtt_reserve_bytes(8 * gib) == 2 * gib);
// engine#2995 review: an UNREADABLE pool is not an empty one. `gtt_total == 0` must keep the measured reserve,
// because the alternative is failing open on the input where the answer is unknown.
expect("an unreadable pool keeps the measured 16 GiB reserve, not zero",
admit::gtt_reserve_bytes(0) == 16 * gib);

expect("a 16 GiB-GTT box now admits a small model where the fixed reserve refused everything",
ceiling_of(16 * gib).fits || !ceiling_of(16 * gib).over_ceiling);
}
expect("a box with no /proc/meminfo: not checked", !admit::judge(need, sysinfo::memory(d + "/nowhere")).checked);

// The program-cache warning now needs MemAvailable to be the binding limit: under the ceiling there is
Expand Down Expand Up @@ -284,11 +314,23 @@ int main() {
need.total = 1ull << 30; // 61 GiB after the load: inside the ceiling, and above #315's zone only warns
v = admit::judge(need, box);
expect("1 GiB more: fits, no ceiling note", v.fits && v.ceiling_note.empty());
// engine#2986 restated this invariant rather than dropping it: a pool smaller than the *fixed* 16 GiB reserve is no
// longer a zero ceiling, because the reserve is now a quarter of the pool. What still has to hold is that the ceiling
// leaves the reserve free -- and that a pool small enough to be a sixteenth of the load is still refused by it.
need.gpu = true; // the arms below are about the GTT ceiling, and an earlier arm leaves this cleared
need.total = 60ull << 30;
sysinfo::Memory small = box;
small.gtt_total = 8ull << 30; // smaller than the 16 GiB reserve: no ceiling to fit under
small.gtt_total = 8ull << 30; // was "smaller than the reserve": now 2 GiB reserved, 6 GiB ceiling
small.gtt_used = 0;
v = admit::judge(need, small);
expect("a pool smaller than the reserve: refused at any size", !v.fits && v.over_ceiling && v.ceiling == 0);
expect("an 8 GiB pool: the ceiling is the pool minus a quarter, not zero",
v.ceiling == 6ull * (1ull << 30) && v.over_ceiling);
sysinfo::Memory tiny = box;
tiny.gtt_total = 1ull << 30; // 256 MiB reserved, 768 MiB ceiling: still far under a 60 GiB load
tiny.gtt_used = 0;
v = admit::judge(need, tiny);
expect("a 1 GiB pool is still refused: the ceiling cannot grow a load that large",
!v.fits && v.over_ceiling && v.ceiling == 768ull * (1ull << 20));

fs::remove_all(dir);
if (failures) std::printf("%d FAILED\n", failures);
Expand Down