#1 [high] PR #641 subtraction lens — finding A: widened flush trigger is extra scope; delete it
HEAD ignition/preparation/compile_pool.py:46-64 (verbatim block cites 48-63): 46 shared = torch.cuda.get_device_properties(device).is_integrated 47 device_bytes = _WORKER_DEVICE_BYTES if shared else _DISCRETE_WORKER_DEVICE_BYTES 48 worker_bytes = host_memory.FORWARD_WORKER_BYTES + (device_bytes if shared else 0) 49 headroom = max(host_memory._HEADROOM_BYTES, headroom_bytes if shared else 0) 50 max_workers = limit if compile_workers is not None else min(limit, _DEFAULT_MAX_WORKERS) 51 available = host_memory.available_bytes() 52 free = device_memory_info(device)[0] 53 if shared: 54 available = min(available, free) 55 if (available - headroom < max_workers * worker_bytes if shared 56 else free - headroom_bytes < max_workers * device_bytes): 57 torch.cuda.empty_cache() 58 free = device_memory_info(device)[0] 59 if shared: 60 available = min(host_memory.available_bytes(), free)
Main change vs main (main:45-56): old trigger if free - headroom_bytes < device_bytes (ONE 2 GiB worker). New trigger = whole worker budget.
ARITHMETIC (the reported failure does NOT need the widening):
- Reported before: "8435 MiB available host memory, 8192 MiB headroom", GB10 unified/POSIX.
- On POSIX integrated, host_memory.py:107-109 makes free = min(total, available_bytes()) = 8435 MiB.
- OLD trigger: 8435-8192 = 243 MiB < 2048 MiB (_WORKER_DEVICE_BYTES) => empty_cache() ALREADY fired. So the whole bug was the missing resample of
available, nothing else. - NEW trigger on GB10: max_workers = min(cpu_workers≈20, compile_threads=min(16,cores)=16, 8) = 8; worker_bytes = 32 GiB (FORWARD_WORKER_BYTES, host_memory.py:15) + 2 GiB = 34 GiB; threshold = 8*34 = 272 GiB > 121.7 GiB total => condition ALWAYS true on every GB10 preparation. Empty_cache() goes from "rare" to unconditional.
- Discrete: threshold = max_workers * 1 GiB; with compile_workers=32 (tests/preparation/test_compile_pool.py:39-41, 12 GiB free / 2 GiB headroom) threshold 32 GiB > 10 GiB => that pre-existing test now flushes where it never did (test still passes, so the path change is silent).
- Cost: CompilePool.init (compile_pool.py:133) and aot.py:1076 both call compiler_resources, and aot.py:1070 adds its own flush BEFORE the barrier, so a GB10 rank now does empty_cache() twice per preparation (once aot.py:1070, once compile_pool.py:57 immediately after the barrier). No cost measurement in the PR description.
SUGGESTED REPLACEMENT (narrow trigger, keeps the resample fix):
if (available - headroom < device_bytes if shared
else free - headroom_bytes < device_bytes):
torch.cuda.empty_cache()
...
Deletes line 50 (max_workers) entirely. Keeps _DEFAULT_MAX_WORKERS (still used line 63). Keeps the Windows host-RAM-driven decision (that is why shared uses available, not free). Reported case still fires (243 < 2048 MiB).
CONSEQUENCE FOR TESTS (purely additive +147 lines, tests/preparation/test_compile_pool.py:538-681):
- 569-592 (3 shared threshold params) and 605-611 param (9, 4, 12, 13) exist ONLY to pin the widened trigger; the (9,...) case needs explicit compile_workers=12 > 8 to fire. 626-642 "skips" test also only meaningful for the wide trigger. Deleting the widening removes ~4 of the 5 new tests, leaving the resample assertion (591) and the aot.py ordering test (645-682).
- AGENTS.md:188-193 "Reject unsupported abstractions and churn ... Refactoring must directly support the requested behavior or remove a demonstrated source of defect or cost." Widening is an unmeasured behaviour change beyond the reported defect.
#2 [high] PR #641 finding B: shared sample/clamp expression duplicated 3× and re-reads /proc on POSIX
ignition/preparation/compile_pool.py:51-60 (head): 51 available = host_memory.available_bytes() 52 free = device_memory_info(device)[0] 53 if shared: 54 available = min(available, free) ... 58 free = device_memory_info(device)[0] 59 if shared: 60 available = min(host_memory.available_bytes(), free)
- On POSIX integrated, host_memory.py:103-110:
free, total = torch.cuda.mem_get_info(device)
if os.name != "nt" and torch.cuda.get_device_properties(device).is_integrated:
free = min(total, available_bytes())
so free <= available_bytes() already;
min(available_bytes(), free)at :60 is alwaysfreeon POSIX (all live evidence), andmin(available, free)at :54 is likewisefree. The clamp is load-bearing only on Windows (os.name == "nt" skips the fold) — whose only coverage is a mock. - available_bytes() cost: host_memory.py:82-100 reads /proc/meminfo AND calls _cgroup_available_bytes() (host_memory.py:51-79, reads /proc/self/cgroup + /proc/self/mountinfo per ancestor). Each call = 3 file reads.
- main: 2 calls (compile_pool.py:45 direct + the one inside device_memory_info at :46).
- head: 4 calls when the flush fires (:51, inside :52, inside :58, :60) — and per finding A the flush is the normal path on GB10. The extra pair recomputes a value device_memory_info already folded into
free.
- Asymmetry: after a discrete flush only
freeis resampled (:58) whileavailablekeeps the pre-flush sample — the two numbers allocate_workers multiplies together (compile_pool.py:75-86). Either resample both or neither; the current split is an undocumented special case.
SUGGESTED REPLACEMENT (collapse the three if shared branches into one sample helper; 10 lines -> 6):
def sample():
free = device_memory_info(device)[0]
return free, (min(host_memory.available_bytes(), free) if shared
else host_memory.available_bytes())
free, available = sample()
if :
torch.cuda.empty_cache()
free, available = sample()
This removes :53-54, :59-60, the duplicated device_memory_info(...)[0] call, and the discrete asymmetry in one move (one if shared instead of three). Behaviour on discrete changes only by making available fresh (more conservative admission), which is the direction the PR argues for elsewhere.
#3 [high] PR #641 subtraction lens — findings C/D and what is already lean
-
Trigger duplicates admission arithmetic that already lives in allocate_workers (compile_pool.py:67-86): for _ in range(limit): able = [rank ... if workers[rank] < resources[rank].capacity and resources[rank].worker_bytes <= remaining]
remaining = available - headroom(large) is decremented per worker;limit = min(jobs, min(limit))where in _prepare_ranksjobs= missing forwards (aot.py:1080 demand/1086 counts, 1087 allocate_workers(..., counts, ...)). The new pre-check at :55-56 replacesjobs/capacity/the hostmin-across-ranks with a flatmax_workers * worker_byteson one rank's reading. Host with 1 missing forward: admission needs 34 GiB, pre-check demands 8*34 = 272 GiB (> total on every GB10). So the trigger is a second, less accurate copy of the admission rule; drop it rather than maintain two. -
_DEFAULT_MAX_WORKERS(compile_pool.py:26) is used at :50 and :63. If the widening goes, only :63 uses it -> single-use constant extracted from main's literalmin(8, capacity)(main:54). Note as a consequence of finding A, not a separate suggestion (AGENTS.md:188-193 discourages churn); keeping the name is harmless, but the extraction is only justified by the widening. -
Minimum-diff alternative for the trigger (verbatim main:49 which already fires in the reported case): if free - headroom_bytes < device_bytes: Reported case: POSIX integrated -> host_memory.py:108-109 folds free = min(total, available_bytes()) = 8435 MiB; 8435-8192 = 243 MiB < 2048 MiB -> fires. So restoring this line + keeping the resample at :60 fixes the reported bug entirely. If the author wants Windows unified memory covered by the same check, use
available - headroom < device_bytesinstead, in which case tests/preparation/test_compile_pool.py:595-602 must change (available=10, headroom=8 gives 2 GiB, not < 2 GiB, so it would no longer fire). -
Tests to delete with the widening (all purely additive, +147): 569-592 (3 shared threshold params; only param 3 (8,44,34,2) also claims "a second worker", which the PR's own evidence contradicts), 605-623 (discrete capacity recovery, incl.
(9, 4, 12, 13)needing compile_workers=12), 626-642 (no-flush skip test, whose name is literally "…already_covers_max_workers"). Keep: the single resample assertion (591), the Windows clamp test, and 645-682 ordering test. Mock-only concern: helper at 538-566 monkeypatches torch.cuda.empty_cache / host_memory.available_bytes / mem_get_info / get_device_properties; AGENTS.md:194-199 "mocks that bypass the behavior cannot establish runtime correctness". Pinningstate["flushes"] == 1/== 0for a trigger that is about to be re-derived is scaffolding, not a contract. -
Already lean / should not be touched:
- ignition/preparation/aot.py:1067-1074 (+2/-1): 1070
torch.cuda.empty_cache()inside the existing try/except, beforedist.barrier(group=group)at 1074. Necessary for cross-rank consistency (peers share a host and sample at compiler_resources, aot.py:1076); a failure folds intoerrorand is reported via the failures list at 1083-1085, so barrier progress is preserved. No wrapper, no flag. The one comment reword at 1073 is a direct consequence, not comment churn. - Hoisting
worker_bytes(:48) andheadroom(:49) out of the return tuple (main:57-59) is justified: both are now consumed twice (trigger + return). No duplication. - No new config key, env var, dependency, flag, or abstraction introduced;
CompilerResourcesshape unchanged (compile_pool.py:29-35). - PYTHONPATH-independent: no docs/schema touched, nothing becomes wrong.
- ignition/preparation/aot.py:1067-1074 (+2/-1): 1070
#4 [high] PR #641 finding A — arithmetic addendum (272 GiB > 121.7 GiB ⇒ flush always fires)
Precision addendum to entry 1 (verify before quoting):
- "Always fires on GB10" is exact, not approximate: on unified memory the trigger is
available - headroom < max_workers * worker_bytes= 272 GiB, whileavailable <= total = 121.7 GiB, andheadroom >= 0, soavailable - headroom < 0 < 272 GiBholds for every possible headroom_bytes. Hence empty_cache() runs on every GB10 preparation regardless of recipe headroom. - max_workers arithmetic on GB10: compilation.py:30
compile_threads = min(16, os.cpu_count() or 1); compile_pool.py:43-45 gives limit = min(cpu_workers(None), cpu_workers(), compile_threads); compile_pool.py:50 then min(limit, 8) = 8 (GB10 has >=16-20 visible CPUs). worker_bytes = host_memory.FORWARD_WORKER_BYTES (32 GiB, host_memory.py:15) + 2 GiB = 34 GiB. 8 * 34 = 272 GiB. - Discrete: threshold = max_workers * 1 GiB (compile_pool.py:56, _DISCRETE_WORKER_DEVICE_BYTES at :25), i.e. free < 8 GiB + headroom_bytes at defaults, versus main's free < 1 GiB + headroom_bytes (main:49).
- Headroom is recipe-dependent: bootstrap.py:134
"headroom_bytes": plan.headroom_bytes + plan.reserve_bytes, default 4 GiB at bootstrap.py:35, floored to 8 GiB by host_memory._HEADROOM_BYTES (host_memory.py:16) via compile_pool.py:49. The PR's cited run used 8192 MiB. - Old trigger fires in the reported case because POSIX integrated folds host RAM into
free: host_memory.py:107-109free, total = torch.cuda.mem_get_info(device); if os.name != "nt" and ...is_integrated: free = min(total, available_bytes())⇒ free = 8435 MiB; 8435 - 8192 = 243 MiB < 2048 MiB (_WORKER_DEVICE_BYTES, compile_pool.py:23). So the widening at :55-56 is not needed for the reported defect — only the resample at :59-60 is.
#5 [high] PR #641 finding E: widened trigger double-flushes each rank
Chain: ignition/preparation/aot.py:1067-1077 1067 resources = None 1068 try: 1069 release_unused() 1070 torch.cuda.empty_cache() # added by this PR 1071 except Exception as failure: 1072 error = error or f"{type(failure).name}: {failure}" 1073 # Include every rank's retained cache-restoration owners and flushed CUDA caches ... 1074 dist.barrier(group=group) 1075 try: 1076 resources = compiler_resources(self.device, headroom_bytes=self.headroom_bytes, ... The barrier at :1074 exists so every rank's cache is released before any rank samples. With the widened trigger (compile_pool.py:55-56) the flush threshold is 272 GiB on GB10, so compiler_resources at :1076 flushes a SECOND time on the same rank, immediately after :1070 → two torch.cuda.empty_cache() calls per rank per preparation, the second one redundant with the first and with the barrier already guaranteeing freshness.
Consequence for the review: the single highest-value simplification is deleting the widened trigger; it (a) removes a behaviour change with no measurement, (b) makes the new aot.py:1070 flush meaningful instead of immediately redundant, (c) deletes ~4 of 5 new mocked tests, and (d) keeps the actual reported fix (available resampled after the flush) intact.
Also: aot.py:1070's flush is correct and cheap to keep because it is the one that makes the barrier ordering work; the PR's stated bug ("peer ranks sample before other ranks return cached segments") is solved entirely by :1070 + resample. So aot.py needs no change beyond what it has — the diff there is lean (+2/-1, no wrapper, failure folded into error and surfaced through the failures list at aot.py:1082-1085).
#6 [high] Research summary
I reviewed PR #641 through the subtraction/simplicity/conventions lens: the two production files at head and on main, host_memory.py, compilation.py, bootstrap.py, memory_inventory.py, tuning.py, the surrounding tests, and the relevant AGENTS.md and docs/preparation-development.md rules.
The diff is small and contains no new flag, config key, env var, dependency, abstraction or artifact change; aot.py (+2/−1) is lean and correct in shape. The one high-value subtraction is in compiler_resources(): the PR widens the empty_cache() trigger from "one worker does not fit" to "the whole worker budget does not fit" (compile_pool.py:55-56), which on unified memory thresholds 8 × 34 GiB = 272 GiB against a 121.7 GiB GB10 — so the flush becomes unconditional on every GB10 preparation and newly fires on discrete GPUs too, with no cost measurement, and the reported failure did not need it (the old trigger already fired at 243 MiB < 2048 MiB, because POSIX device_memory_info folds host RAM into free). Removing the widening leaves the actual fix — resampling available after the flush — untouched, removes a redundant second empty_cache() per rank (the PR's own new aot.py:1070 flush already precedes the barrier), and makes ~4 of the 5 new mocked tests unnecessary.
Second suggestion: the shared-memory sample/clamp is written three times (compile_pool.py:51, 53-54, 58-60), re-reads /proc/meminfo four times instead of two, is a no-op on POSIX where all live evidence was gathered, and leaves available stale on discrete GPUs while capacity grows. One small sample() closure collapses ten lines to six and fixes the asymmetry.
I also noted that _DEFAULT_MAX_WORKERS only earns its extraction from the widening, and listed what should explicitly not be touched. Details and verbatim citations are in my scratchpad notes (entries 1-5).
#7 [high] PR #641 — report skeleton: verdict, both suggestions, verbatim quotes, and what is lean
PR #641 (nero-, head d82fcfc, base 0a8c20f, +163/−10, 3 files) — Subtraction/Simplicity/Conventions
VERDICT: The diff is small, contains no new flag/config key/env var/dependency/abstraction/artifact change, and ignition/preparation/aot.py (+2/−1) is lean. TWO high-value subtractions, both in compiler_resources(); everything else is a note.
S1 (highest value) — Delete the widened flush trigger at compile_pool.py:55-56; it is unmeasured extra scope and not needed for the reported bug.
Head, ignition/preparation/compile_pool.py:46-63 (verbatim):
shared = torch.cuda.get_device_properties(device).is_integrated
device_bytes = _WORKER_DEVICE_BYTES if shared else _DISCRETE_WORKER_DEVICE_BYTES
worker_bytes = host_memory.FORWARD_WORKER_BYTES + (device_bytes if shared else 0)
headroom = max(host_memory._HEADROOM_BYTES, headroom_bytes if shared else 0)
max_workers = limit if compile_workers is not None else min(limit, _DEFAULT_MAX_WORKERS)
available = host_memory.available_bytes()
free = device_memory_info(device)[0]
if shared:
available = min(available, free)
if (available - headroom < max_workers * worker_bytes if shared
else free - headroom_bytes < max_workers * device_bytes):
torch.cuda.empty_cache()
free = device_memory_info(device)[0]
if shared:
available = min(host_memory.available_bytes(), free)
capacity = max(0, (free - headroom_bytes) // device_bytes)
Main, compile_pool.py:49-52 (the line being replaced):
if free - headroom_bytes < device_bytes:
torch.cuda.empty_cache()
free = device_memory_info(device)[0]
Replace :55-56 with:
if (available - headroom < device_bytes if shared
else free - headroom_bytes < device_bytes):
and delete line 50 (max_workers = ...) entirely.
Why (4 points):
- Not needed for the reported defect. POSIX integrated folds host RAM into
free— host_memory.py:107-109free, total = torch.cuda.mem_get_info(device); if os.name != "nt" and torch.cuda.get_device_properties(device).is_integrated: free = min(total, available_bytes())— so the reported "8435 MiB available, 8192 MiB headroom" gives 8435−8192 = 243 MiB < 2048 MiB (_WORKER_DEVICE_BYTES, compile_pool.py:23) and the OLD trigger already fired. The entire bug was the missing resample ofavailable, which:59-60fixes and the narrow trigger preserves. - Unconditional on the target hardware. max_workers = 8 (compilation.py:30
'compile_threads': min(16, os.cpu_count() or 1); compile_pool.py:43-45 then :50 → min(...,8)); worker_bytes = FORWARD_WORKER_BYTES 32 GiB (host_memory.py:15) + 2 GiB = 34 GiB; threshold = 272 GiB vs 121.7 GiB total on GB10 ⇒available - headroom < 272 GiBholds for ANY headroom ≥ 0. So empty_cache() runs on every GB10 preparation. - Newly flushes discrete GPU hosts. Threshold becomes max_workers × 1 GiB (
_DISCRETE_WORKER_DEVICE_BYTES, :25) vs main's 1 GiB + headroom — e.g. the pre-existing test at tests/preparation/test_compile_pool.py:39-41 (12 GiB free, 2 GiB headroom, compile_workers=32) now flushes where it never did, silently (test still passes). No cost measurement anywhere in the PR description. - Double-flushes every rank. aot.py:1069-1074 (
release_unused(); torch.cuda.empty_cache()thendist.barrier(group=group)) is the PR's own new pre-barrier flush; with the 272 GiB threshold,compiler_resourcesat aot.py:1076 flushes a second time on the same rank immediately afterward. The barrier-ordering bug the PR describes is solved entirely by aot.py:1070 + the resample.
AGENTS.md:188-193: "Reject unsupported abstractions and churn. … Refactoring must directly support the requested behavior or remove a demonstrated source of defect or cost." The widening is a behaviour change beyond the reported defect, on both hardware classes, with no measurement.
Tests to delete with it (all purely additive, +147, tests/preparation/test_compile_pool.py:538-681):
:569-5923 shared threshold params — the third(8, 44, 34, 2)is the only survivor candidate (it claims a second worker).:605-623discrete capacity recovery — param(9, 4, 12, 13)needscompile_workers=12 > 8purely to fire the wide trigger.:626-642the "skips empty_cache when memory already covers max workers" test — its name is the wide trigger. Keep: the single resample assertion (:591), the Windows clamp test (:595-602), the ordering test (:645-682). Mock note (AGENTS.md:194-199: "mocks that bypass the behavior cannot establish runtime correctness"): helper_patch_compiler_memory(:538-566) monkeypatches empty_cache/available_bytes/mem_get_info/get_device_properties, soassert state["flushes"] == 1/0pins arithmetic of a trigger about to be re-derived, not a contract.
S2 — Collapse the three shared-memory branches in the same function (:51, :53-54, :58-60) into one sample helper.
Verbatim duplication (compile_pool.py:51-60):
available = host_memory.available_bytes()
free = device_memory_info(device)[0]
if shared:
available = min(available, free)
if (...):
torch.cuda.empty_cache()
free = device_memory_info(device)[0]
if shared:
available = min(host_memory.available_bytes(), free)
Replace with:
def sample():
free = device_memory_info(device)[0]
return free, (min(host_memory.available_bytes(), free) if shared
else host_memory.available_bytes())
free, available = sample()
if (...):
torch.cuda.empty_cache()
free, available = sample()
Three concrete gains, no behaviour change on POSIX/Windows-integrated:
min(available_bytes(), free)is alwaysfreeon POSIX integrated (host_memory.py:108-109 already folded it), i.e. the clamp is load-bearing only on the Windows path (os.name == "nt"skips the fold) — the one path whose sole coverage is a mock. Comparingfree - headroomon POSIX andavailable - headroomon Windows is the honest form; the current code expresses it twice.- One
if sharedinstead of three, and onedevice_memory_info(...)[0]call site instead of two. - Fixes the asymmetry: after a discrete flush only
freeis resampled (:58) whileavailablestays stale — the two numbersallocate_workersmultiplies together (compile_pool.py:75-86). The helper resamples both.
Cost evidence for the extra reads: available_bytes() (host_memory.py:82-100) reads /proc/meminfo and calls _cgroup_available_bytes() (host_memory.py:51-79: /proc/self/cgroup + /proc/self/mountinfo per ancestor) ⇒ 3 file reads/call. main: 2 calls (compile_pool.py:45 + the one inside device_memory_info at :46). head when the flush fires: 4 calls (:51, inside :52, inside :58, :60) — and per S1 the flush is the normal path on GB10.
Minor notes (not blockers)
_DEFAULT_MAX_WORKERS(compile_pool.py:26) is used at :50 and :63; if S1 removes :50 it has a single use at :63, i.e. it is extracted from main's literalmin(8, capacity)(main:54) solely to serve the widening. Consequence of S1, not a separate ask.- No repo-wide "flush then resample" helper exists to reuse:
memory_inventory.py:244only samples;tuning.py:598-603flushes device +empty_host_cache()for the tuning subsystem;core_storage.py:254-292flushes at pool retirement;direct/executor.py:114is unrelated. So S2 is intra-function collapse, NOT duplication of an existing abstraction — do not overclaim reuse. state["host_samples"]is asserted only in the discrete test (:622), so "resampled exactly once" is not actually pinned on the shared path.
Already lean — do not touch
- aot.py:1067-1074 (+2/−1): the flush sits inside the existing try/except, folds a failure into
errorwhich is reported through the failures list at aot.py:1082-1085, so barrier progress is preserved; the:1073comment reword is a direct consequence, not comment churn. No wrapper, no flag, no flag needed. - Hoisting
worker_bytes(:48) andheadroom(:49) out of the return tuple (main:57-59) is justified — both are now consumed twice. CompilerResourcesshape unchanged (:29-35);allocate_workersuntouched (:67-98); no docs/schema/config surface touched, so nothing becomes wrong.
Conventions check (structural only)
- AGENTS.md:188-193 — the only violation is S1's unmeasured behaviour widening.
- AGENTS.md:194-199 — mock-only Windows/discrete coverage, and tests pinning a trigger instead of the behaviour; the described reclamation is exercised by no test.
- docs/preparation-development.md:305-313 ("Account for … cached allocator segments") and :314-322 ("budgets compiler workers … against available system RAM") are what the resample fix implements — aligned; the widening is not required by either.
- No docs file touched, while #597 and #122 both edited docs;
docs/model-preparation.md:489-495(DGX Spark) is the natural home for "cached allocator segments are reclaimed before admission". Optional, consistent with S1's "keep it minimal" direction.