Correctness and contracts report · NVIDIA/hyperflare-inference #639

Raw Markdown for agents: https://autobot.eidonix.com/p/01569fe1-e27a-4ecd-9cbe-bff6e409f9bd.md

Correctness, Contracts & System-Level Consequences review — #639 "ignition: tune discrete CompilePool sizing and scale Inductor compile threads"

Head 3400e3a, base main, +58/−18 in 4 files (ignition/preparation/compile_pool.py, ignition/preparation/aot.py, tests/preparation/test_compile_pool.py, tests/preparation/test_forward_compile_options.py). Verdict: request changes. The thread/start-method half is a real fix, but it is priced by nothing; the sizing half silently relaxes the discrete compile reserve on the default path and raises worker counts with no main baseline.

Key findings

  1. Blocker — admission pricing was not updated for the new fan-out. compile_pool.py:139-143 gives every outer compiler worker up to 8 Inductor threads and worker_start_method="subprocess" (i.e. real nested processes), but the host is still charged one FORWARD_WORKER_BYTES = 32 GiB slot per worker (host_memory.py:15, compile_pool.py:60-61) and the device is still charged one 704 MiB single-process footprint per worker (compile_pool.py:24-25, :55). A host/GPU admitted for N slots can now run up to 8N compiler processes — exactly what main's one-thread policy existed to prevent.
  2. Major — aot.py:903 puts up to 24 Inductor threads in the parent rank for the whole _prepare_many body: binding, warmup, and the BindingMismatch fallback recompile, i.e. the phase where all compiled forwards are resident. Contradicts docs/preparation-development.md:318 ("artifact binding starts no additional compiler pool"), ignores the configured _OPTIONS['compile_threads'] (compilation.py:30), and is recorded nowhere (stripped from the cache key at aot.py:825).
  3. Major — affinity vs cpu_count mismatch. Thread budgets come from os.cpu_count() (compile_pool.py:139-140, aot.py:903) while worker counts come from len(os.sched_getaffinity(0)) (host_memory.py:31). A 3-CPU cgroup/taskset rank that admits 3 workers gets 8 threads each = 24 compilations on 3 CPUs — the exact case test_explicit_forward_workers_are_bounded_by_cpu_affinity protects on the process side.
  4. Major — one-sided reserve clamp silently changes the default path. compile_pool.py:51 clamps the device reserve to 1 GiB on discrete while CompilerResources.headroom keeps the 8 GiB host floor (:62). With the default plan headroom of 4 GiB (bootstrap.py:35), every discrete export's compile-time device reserve drops 4 GiB → 1 GiB, not just "large runtime reserves" as the description frames it.
  5. Minor/Major — Windows thread partitioning and spawn preservation are unasserted, and DeferredCompiledForward.materialize_for_capture keeps compile_threads=1 (aot.py:688), leaving two conflicting thread policies in one file.
  6. Minor — tests mirror the implementation formula rather than pinning values, and the description's evidence has no main baseline, no worker/thread counts, and is on a 288 GB GB300 where the reserve change matters least.

Areas confirmed sound (not overstating)


1. Correctness, Safety & Platform Parity

Finding 1 — Blocker: workers are priced as one process but now run a process tree (host side)

ignition/preparation/compile_pool.py:137-144:

# Partition CPU compile threads across admitted workers and use Inductor's subprocess
# pool on POSIX because CompilePool workers run inside spawned SubprocPool processes.
cpu_count = os.cpu_count() if hasattr(os, "cpu_count") else None
inner_threads = max(1, min(8, (cpu_count or 1) // max(1, self.workers)))
worker_options = dict(options, compile_threads=inner_threads)
if os.name != "nt":
    worker_options["worker_start_method"] = "subprocess"
self.options = worker_options

self.options is passed verbatim to every _compile_forward (compile_pool.py:161-162), hence to aot_forward(forward, options=options) (:112) and torch._inductor.standalone_compile(..., options={'config_patches': ...}) (aot.py:423-425). Host admission, however, is untouched:

ignition/preparation/host_memory.py:13-16:

_WORKER_BYTES = 2 << 30
# Conservative concurrency estimate; complete-forward tracing can outgrow kernel tuning workers.
FORWARD_WORKER_BYTES = 32 << 30
_HEADROOM_BYTES = 8 << 30

compile_pool.py:60-61 still returns host_memory.FORWARD_WORKER_BYTES as worker_bytes, and allocate_workers subtracts exactly one of those per worker (compile_pool.py:76-84). Main's rationale is deleted by this PR (compile_pool.py:137-138 on main: "Nested Inductor pools would escape the host's admitted process budget") — the mechanism it guarded is now reinstated without the price changing.

Impact: at head, 32 admitted workers on a 128-core host (test_compile_pool.py:377 case (32, 95, 32, 2)) may run 64 concurrent Triton/ptxas/NVCC compilations against 32×32 GiB of priced slots; 8 workers × 8 threads = 64 compilations on a host admitted for 8 slots. Peak RSS during preparation is now a function of scheduling luck, not of available_bytes(). That is the failure #242 reports (a rank that cannot host a compiler dying mid-export), amplified. Also contradicts docs/preparation-development.md:314-318 ("budgets compiler workers ... against available system RAM").

Fix: either multiply worker_bytes by inner_threads in compiler_resources, or bound inner_threads by the admitted host-RAM budget instead of by CPU count alone — the principle used by 468415d1 and f9aec99c for integrated GPUs.

Finding 2 — Major: affinity-limited ranks get the whole machine's thread budget

compile_pool.py:139-140 uses raw os.cpu_count(), while the worker budget is affinity-derived:

host_memory.py:31:

available = len(os.sched_getaffinity(0)) if hasattr(os, "sched_getaffinity") else os.cpu_count() or 1

and compile_pool.py:42 caps workers by cpu_workers().

tests/preparation/test_compile_pool.py:535-542:

def test_explicit_forward_workers_are_bounded_by_cpu_affinity(monkeypatch):
    ...
    monkeypatch.setattr(host_memory.os, "sched_getaffinity", lambda _: set(range(3)))
    ...
    assert compile_pool.CompilePool("cuda:0", compile_workers=32).workers == 3

That rank then computes inner_threads = min(8, 96 // 3) = 8 on a 96-core machine → up to 24 Inductor compile processes inside a 3-CPU cgroup, an 8× oversubscription and a direct reversal of the invariant that test was written to hold. Same bug in aot.py:903 (min(24, os.cpu_count() or 1) ignores affinity entirely).

Fix: derive both from host_memory.cpu_workers() (which already closes over sched_getaffinity) instead of os.cpu_count().

Finding 3 — Minor: Windows thread partitioning and "spawn" preservation are untested, and the test that "covers" Windows exercises a fallback that cannot occur on Windows

tests/preparation/test_compile_pool.py:467-476:

def test_windows_compiler_workers_fit_host_ram_with_larger_device_budget(monkeypatch):
    ...
    monkeypatch.setattr(compile_pool, "os", SimpleNamespace(name="nt"))
    ...
    pool = compile_pool.CompilePool("cuda:0", headroom_bytes=8 << 30)
    assert (pool.workers, pool.options["compile_threads"]) == (1, 1)

SimpleNamespace(name="nt") has no cpu_count, so the new hasattr(os, "cpu_count") guard at compile_pool.py:139 resolves to None → inner_threads = 1. The assertion therefore pins the fallback, not Windows behaviour. Grep for worker_start_method in tests finds only == "subprocess" assertions (test_compile_pool.py:30, test_forward_compile_options.py:284); nothing asserts 'spawn' survives on the nt path.

Impact: the description claims "spawn" is "preserved on Windows", and a real 64-core Windows host with 8 workers will now run 8-thread Inductor spawn pools inside ProcessPoolExecutor spawn workers (compile_pool.py:157-158) — never exercised in CI or by any test. Fix: monkeypatch os.name on the real module (or add cpu_count to the namespace) and assert both inner_threads and worker_start_method on the nt path.

Finding 4 — Minor: two conflicting thread policies left in aot.py, one uncovered

ignition/preparation/aot.py:688:

def materialize_for_capture(self):
    with _guard_code_cache(self.cache.root / 'guards'), torch._inductor.config.patch(compile_threads=1):
        bound = self.cache._load(self.owner, self.path, required=True, before_run=self.before_run)

The streaming-capture path (capture.py:175-182 via materialize_for_capture) binds at 1 thread while _prepare_many now binds and warms at up to 24. Either the difference is deliberate (then it needs a comment and a line in the description) or it was missed. No test covers it. Fix: state the rationale or make the policies uniform; add coverage.

Finding 5 — Minor: nested-process shutdown/blast radius is now two-level and untested

compile_pool.py:167-169 only calls self.pool.shutdown(). With per-worker Inductor subprocess pools, a single failed or slow forward now leaves up to inner_threads grandchildren per worker. test_cli_interrupt_leaves_no_compiler_processes (tests/preparation/test_native_processes.py:123-162) still builds a bare SubprocPool(1) with default options and therefore does not cover the tree this PR creates. Fix: add a SIGINT-during-compile test that asserts no orphaned compile processes when a CompilePool worker holds nested Inductor workers.

2. Unintended System-Level Consequences

Finding 6 — Blocker (same defect, device side): 704 MiB no longer bounds a worker

ignition/preparation/compile_pool.py:22-25:

# Device memory one compiler worker can hold: its CUDA context plus Inductor's constant folding.
_WORKER_DEVICE_BYTES = 2 << 30
# A discrete GPU worker holds only device memory (~672 MiB measured for CUDA context and Inductor constant folding).
_DISCRETE_WORKER_DEVICE_BYTES = 704 << 20

compile_pool.py:52-55:

compile_reserve = headroom_bytes if shared else min(headroom_bytes, 1024 << 20)
if free - compile_reserve < device_bytes:
    torch.cuda.empty_cache()
    free = device_memory_info(device)[0]
capacity = max(0, (free - compile_reserve) // device_bytes)

The 672 MiB measurement is of one process ("initialize a CUDA context", per the PR's own Why). Each admitted worker can now spawn up to 8 Inductor compile processes that each initialize CUDA/triton state, while capacity still divides free memory by one 704 MiB unit. Combined with finding 7 (reserve 4 GiB → 1 GiB), a 12 GiB-free discrete part goes from 10 workers on main to 16 at head (test_compile_pool.py:43-45), each potentially fanning out 8×. No test, artifact or measurement covers the fan-out footprint — every device input is monkeypatched (test_compile_pool.py:383-384).

Impact: on SM120/SM121 workstations and on multi-rank hosts sharing one discrete GPU, the new admission math can OOM during preparation, which is worse than under-admitting: the export dies after minutes of work. Fix: price device bytes as device_bytes × inner_threads, or keep one Inductor thread per worker and only raise the worker count; tie the constant to a measured maximum, not an average single process.

Finding 7 — Major: the reserve clamp is one-sided, undocumented, and changes the default for every discrete export

compile_pool.py:49-62:

# Integrated GPUs share physical memory with the host; discrete compile workers exit before
# runtime allocations, so cap their device-side reserve at 1 GiB.
compile_reserve = headroom_bytes if shared else min(headroom_bytes, 1024 << 20)
...
return CompilerResources(capacity, available, limit,
    host_memory.FORWARD_WORKER_BYTES + (device_bytes if shared else 0),
    max(host_memory._HEADROOM_BYTES, headroom_bytes if shared else 0))

The clamp feeds only device capacity and the empty-cache probe; CompilerResources.headroom — which allocate_workers subtracts from host memory (:73-84) — still floors at 8 GiB on discrete. So the same configured number is now honoured in full on integrated GPUs, clamped to 1 GiB for device admission on discrete, and floored at 8 GiB for host admission. bootstrap.py:35 sets headroom_bytes: int = 4 << 30, so the default discrete export loses 3 GiB of compile-time device protection, not only hosts with "large runtime reserves". The behaviour change is visible only in a test expectation change (test_compile_pool.py:446-452: a 71.5 GiB headroom no longer raises MemoryError on discrete but still does on integrated) and is never stated in the description's Result section.

Impact: (a) users who raised [memory] reserve to keep a discrete GPU safe at runtime silently lose that protection during compilation — with reserve = 0, compile_reserve becomes 0 and nothing is reserved at all; (b) #242's host-RAM side (CompilerResources.headroom, the 8 GiB floor) is untouched, so this looks adjacent to that bug without addressing it. Fix: make the discrete compile reserve a named constant derived from the measured footprint (_DISCRETE_WORKER_DEVICE_BYTES * capacity), keep configured headroom behaviour explicit, and document the default 4 GiB → 1 GiB change in the PR.

Finding 8 — Major: up to 24 Inductor threads in the parent rank across binding/warmup/fallback recompile

ignition/preparation/aot.py:901-906:

# Allow fallback/binding compilation to parallelize up to the host CPU budget.
lock = nullcontext() if _locked else self._publication_lock(coordinator)
with lock, torch._inductor.config.patch(compile_threads=min(24, os.cpu_count() or 1)):
    return self._prepare_many(functions, signatures, progress=progress,
                              coordinator=coordinator, materialize=materialize, warmup=warmup,
                              before_run=before_run, stream=stream)

The patch spans the entire _prepare_many: worker scheduling, self._load(...) binding of every artifact, _warm(...) warmup, and the fallback recompile at aot.py:977-980 (bound = BoundForward(aot_forward(forward, options=self.options), forward, False)), which runs in the parent rank. That is the phase in which loaded forwards are kept live while the rest are warmed (aot.py:965-1005, with release_unused(generation=0) between items). Main's value was 1, with the comment "Compiler workers own parallel compilation; artifact binding needs no second worker pool." Consequences:

Fix: keep compile_threads=1 for binding, and run only the fallback recompile under one explicitly bounded pool derived from host_memory.cpu_workers()/_OPTIONS['compile_threads']; record the value in the plan/reproduction arguments.

Finding 9 — Minor: unbounded concurrent-compile high-water mark; head-of-line blocking unchanged

_prepare_many gates on worker count only (aot.py:948-949: if len(pending) >= compiler.workers: finish()), so a slow worker holding 8 nested compiles still occupies exactly one slot while the GPU/CPU high-water mark depends on which workers happen to have live nested compiles. Not a regression in the gate, but there is now no admission check on the true process count. Fix: if nested threading stays, gate on workers × inner_threads against the same budget.

3. Architecture, Contracts & Charter Compliance

Layer placement itself is fine: shared preparation code, no per-model branch, no new hardware target, no wire/proto/captured-graph contract touched.

4. Tests & Claim Accuracy

Finding 10 — Minor: assertions copy the implementation formula

tests/preparation/test_compile_pool.py:28:

assert pool.options["compile_threads"] == min(8, 16 // expected)

tests/preparation/test_forward_compile_options.py:282:

assert pool.options['compile_threads'] == max(1, min(8, (os.cpu_count() or 1) // max(1, pool.workers)))

These mirror compile_pool.py:140 verbatim, so a wrong partition passes as long as implementation and test are edited together. The new test_prepare_many_scales_inductor_compile_threads_with_cpu_count (test_forward_compile_options.py:333-342) monkeypatches aot.ForwardCache._prepare_many out and only proves the number reaches config.patch — not that the scope should cover binding/warmup, nor that 24 threads are safe with forwards resident. Fix: pin literal expected values per case (as the parametrized case at :377 already does for workers) and assert the policy scope.

Finding 11 — Minor: description claims vs the diff

Open questions

  1. Why is CI red on 3400e3a, and does the failure touch these files?
  2. What compile_workers / headroom_bytes did the DeepSeek-V4.1 and Inkling-2-Small exports use, and what does the same export take on main? Without that pair the sizing half has no evidence.
  3. Is 704 MiB safe for the largest shipped forward (the replaced comment named "Kimi K3's 93-layer forwards"), and specifically per nested Inductor process? Where is the 672 MiB measurement?
  4. Is the 8 GiB host-RAM headroom floor on discrete (compile_pool.py:62) intentional while the device reserve is clamped (#242's remaining half)?
  5. Was materialize_for_capture's compile_threads=1 (aot.py:688) left alone deliberately?
  6. Which of #597 and #639 lands first, and does #597's groups= work assume the 8-worker/1-thread world?