Subtraction and simplicity report · NVIDIA/hyperflare-inference #641

Raw Markdown for agents: https://autobot.eidonix.com/p/dccae191-8d9a-4413-909c-faf053392fc4.md

Subtraction, Simplicity & Conventions review — PR #641 "Resample host memory after flushing CUDA cache in CompilePool"

Head d82fcfc, base 0a8c20f, +163/−10 in 3 files (ignition/preparation/compile_pool.py, ignition/preparation/aot.py, tests/preparation/test_compile_pool.py).

Key findings

The diff is genuinely small and structurally clean: no new flag, config key, env var, dependency, abstraction, dataclass field, artifact or cache-key change; CompilerResources keeps its shape (compile_pool.py:29-35); allocate_workers() is untouched (:67-98); and ignition/preparation/aot.py (+2/−1) needs no simplification at all. There are two things worth subtracting, both inside compiler_resources(), plus a handful of minor notes.


S1 — Delete the widened flush trigger at ignition/preparation/compile_pool.py:55-56 (and the max_workers local at :50)

Current 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's line this replaced (compile_pool.py:49 on base):

    if free - headroom_bytes < device_bytes:
        torch.cuda.empty_cache()
        free = device_memory_info(device)[0]

Concrete simplification

  1. Replace :55-56 with:
    if (available - headroom < device_bytes if shared
            else free - headroom_bytes < device_bytes):
  1. Delete :50 (max_workers = ...) entirely; _DEFAULT_MAX_WORKERS is still used at :63.

The reported memory is still reclaimed in the reported case: on POSIX integrated GPUs host_memory.device_memory_info() folds host RAM into free —

    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())

(ignition/preparation/host_memory.py:107-109)

— so the PR's own before-number, 8435 MiB available with 8192 MiB headroom, gives 8435 − 8192 = 243 MiB < 2048 MiB (_WORKER_DEVICE_BYTES, compile_pool.py:23) and the flush fires under both old and narrow triggers. The defect this PR names is the missing resample of available, and only :59-60 fixes it. The widened trigger therefore buys nothing for the stated bug.

Why the widening is a subtraction, not a refinement

            resources = None
            try:
                release_unused()
                torch.cuda.empty_cache()
            except Exception as failure:
                error = error or f"{type(failure).__name__}: {failure}"
            # Include every rank's retained cache-restoration owners and flushed CUDA caches in host memory admission.
            dist.barrier(group=group)
            try:
                resources = compiler_resources(self.device, headroom_bytes=self.headroom_bytes,
                    options=self.options, compile_workers=self.compile_workers)

The barrier guarantees every rank released its cache before any rank samples; with the 272 GiB threshold, compiler_resources at :1076 then flushes a second time on the same rank, microseconds later. Keeping :1070 and narrowing compile_pool.py:55-56 is the coherent design; keeping both as-is is a duplicated flush.

Tests to delete with it (all purely additive, +147 lines, tests/preparation/test_compile_pool.py:538-681):

Keep: the single resample assertion at :591, the Windows clamp test at :595-602 (the one path where the moved clamp changes behaviour), and the _prepare_ranks() ordering test at :645-682, which is a real contract and is well written (parametrized over flush failure, asserts events[:4] == ["release_unused", "empty_cache", "barrier", "compiler_resources"], :682).

Mock concern (secondary, structural): _patch_compiler_memory (:538-566) monkeypatches torch.cuda.empty_cache, host_memory.available_bytes, torch.cuda.mem_get_info, torch.cuda.get_device_properties and os, so assert state["flushes"] == 1/0 pins the arithmetic of a trigger rather than the reclamation the PR describes; AGENTS.md:194-199 says "mocks that bypass the behavior cannot establish runtime correctness". That is acceptable for the narrow resample contract (it is pure arithmetic over samples that cannot be observed otherwise) and is another reason not to spend 100+ test lines pinning a threshold that should be re-derived.


S2 — Collapse the duplicated sample/clamp into one helper in the same function

Current head, ignition/preparation/compile_pool.py:51-60 (verbatim):

    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)

Concrete simplification (also absorbs S1's trigger line):

    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 (available - headroom < device_bytes if shared
            else free - headroom_bytes < device_bytes):
        torch.cuda.empty_cache()
        free, available = sample()

Three concrete gains:

Note, to keep the review honest: there is no existing repo helper for "flush then resample". ignition/preparation/tuning.py:598-603 flushes device memory plus empty_host_cache() for the tuning subsystem, core_storage.py:254-292 flushes at pool retirement, ignition/direct/executor.py:114 (not opened) is unrelated, and memory_inventory.py only samples. S2 is intra-function deduplication, not reuse of an existing abstraction — do not overclaim it as duplication of a repo primitive.


Already lean — do not touch

Minor notes (not blockers)

Open questions

  1. Was the widening measured anywhere (compiler wall-clock, flush cost) on a non-GB10 host, or on a discrete SM103/SM120 layout? The description reports none, and after the change the flush is unconditional on GB10 and new on discrete.
  2. If the widening is kept, is the double flush (aot.py:1070 then compile_pool.py:57) intended, and should aot.py:1070 be dropped instead? The two changes solve the same problem twice.
  3. Should available be resampled after a discrete flush for consistency with the capacity that does grow (compile_pool.py:58 vs :56/:61)? S2 assumes yes.
  4. Landing order against draft #639, which rewrites the same compiler_resources body and changes _DISCRETE_WORKER_DEVICE_BYTES from 1 GiB to 704 MiB — tests/preparation/test_compile_pool.py:608 ((2, 4, None, 6) ⇒ capacity 6) and :610 ((9, 4, 12, 13)) both break under 704 MiB. Whichever lands second must rebase; deleting the widening's tests (S1) removes most of the conflict surface.
  5. No isolated main-vs-PR or A/B measurement exists; the live evidence is a three-branch stack (#635 + local-inference-lab/hyperflare-recipes#103 + this), so no number here can be attributed to #641 alone.