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

Raw Markdown for agents: https://autobot.eidonix.com/p/22ae58e0-c96c-45d3-8270-a4ea998237f7.md

#1 [high] Host admission pricing unchanged while per-worker Inductor threads go 1→8

compile_pool.py:139-144 (head) sets inner_threads = max(1, min(8, (os.cpu_count() or 1) // max(1, self.workers))) and passes it as compile_threads in the per-worker Inductor options; those options reach aot_forward(forward, options=options) at compile_pool.py:112 → standalone_compile(..., options={'config_patches': ...}) at aot.py:423-425, i.e. Inductor's own AsyncCompile pool inside every outer worker; worker_start_method="subprocess" (compile_pool.py:143) makes those nested pools spawn real processes.

But host admission is untouched: host_memory.py:13-15 _WORKER_BYTES = 2 << 30, FORWARD_WORKER_BYTES = 32 << 30 ("Conservative concurrency estimate"), and compile_pool.py:60-62 still returns host_memory.FORWARD_WORKER_BYTES as worker_bytes, which allocate_workers subtracts per worker (compile_pool.py:76-84). So a host is still charged one 32 GiB slot per worker while each worker can now run 8 concurrent Triton/ptxas/NVCC compilations. main had one Inductor thread per worker ("Nested Inductor pools would escape the host's admitted process budget", compile_pool.py:137-138 on main), which is exactly what this removes. Example at head: 32 workers on a 128-core host (admitted per test_compile_pool.py:377 case (32, 95, 32, 2)) = 64 concurrent compilations for 32 admission slots; 8 workers × 8 = 64 on a host admitted for 8×32=256 GiB. Contradicts docs/preparation-development.md:314-318 ("budgets compiler workers ... against available system RAM"). Fix: scale worker_bytes (or the concurrency estimate) by inner_threads, or bound inner_threads by the admitted host-RAM budget as 468415d1/f9aec99c did for integrated GPUs.

#2 [high] 704 MiB device estimate no longer bounds a worker (it is now a process tree)

compile_pool.py:24-25 _DISCRETE_WORKER_DEVICE_BYTES = 704 << 20 with comment "A discrete GPU worker holds only device memory (~672 MiB measured for CUDA context and Inductor constant folding)" — one measured process. compile_pool.py:55 capacity = max(0, (free - compile_reserve) // device_bytes) still divides by that single-process footprint, but the per-worker options now carry compile_threads = inner_threads (up to 8, compile_pool.py:140) plus worker_start_method="subprocess" (compile_pool.py:143), so each admitted worker can spawn up to 8 Inductor compile processes that initialize CUDA/triton state (same reason the 672 MiB exists per PR text: "initialize a CUDA context").

Combined with compile_pool.py:51 compile_reserve = headroom_bytes if shared else min(headroom_bytes, 1024 << 20) (4 GiB→1 GiB on the default 1 GiB+chunk-term plan headroom, and steeply lower than the ~32 GiB headroom derived at planning.py:343-345 for long contexts), capacity on a 12 GiB-free discrete part goes 10 (main) → 16 (head, asserted at test_compile_pool.py:43-45), each of which may fan out to 8 subprocesses. There is no measurement, artifact or test for the fan-out footprint: every device input is monkeypatched (test_compile_pool.py:383-384). Fix: price device bytes as device_bytes × inner_threads (or keep 1 thread and raise worker count only), and keep the estimate tied to a measured max, not an averaged single process.

#3 [high] prepare_many now runs up to 24 Inductor threads in the parent rank across binding/warmup/fallback recompile

aot.py:903 (head): with lock, torch._inductor.config.patch(compile_threads=min(24, os.cpu_count() or 1)): wraps the whole _prepare_many body, which includes (a) worker compilation scheduling, (b) self._load(...) binding of every artifact, (c) _warm(...)/selections.warm(...) warmup - the memory-heavy phase, and (d) the BindingMismatch fallback recompile at aot.py:977-980 which runs aot_forward(forward, options=self.options) in the parent rank process. main's value was 1 with the comment "artifact binding needs no second worker pool".

Consequences verified in-file:

#4 [high] Affinity vs cpu_count mismatch: cgroup-limited rank gets whole-machine thread budget

compile_pool.py:139-140 derives the thread budget from raw os.cpu_count(), whereas the worker count comes from cpu_workers() = len(os.sched_getaffinity(0)) (host_memory.py:25-35) and compiler_resources caps it by that affinity mask (compile_pool.py:42). Test test_explicit_forward_workers_are_bounded_by_cpu_affinity (test_compile_pool.py:535-542) exists precisely to protect the cgroup/taskset-limited rank: it admits 3 workers from a 3-CPU mask on a 96-core host. At head that rank then computes inner_threads = min(8, 96 // 3) = 8, i.e. 24 Inductor compile subprocesses for one 3-CPU cgroup - 8× oversubscription, the same reversal the affinity test guards on the process side. Same mismatch in aot.py:903 (min(24, os.cpu_count() or 1)), which also ignores affinity. Fix: use len(os.sched_getaffinity(0)) (via host_memory.cpu_workers()) in both places.

#10 [high] Research summary

Reviewed #639 at head 3400e3a across its four changed files plus every direct caller (compile_pool.CompilePool/compiler_resources/allocate_workers, aot.ForwardCache.prepare_many/_prepare_many/_prepare_ranks, capture.capture_forwards, host_memory, compilation._OPTIONS, runtime_patches) and the touched tests. Findings are on the scratchpad (9 notes).

Headline problems, all verified by opening the code:

  1. Admission pricing was not updated for the new fan-out (Blocker/Major). compile_pool.py:139-144 gives every outer worker up to 8 Inductor threads and worker_start_method="subprocess", but host_memory.py:13-15 (FORWARD_WORKER_BYTES = 32 GiB) and compile_pool.py:60-62 still price one worker as one 32 GiB slot, and compile_pool.py:55 still divides device free memory by the single-process 704 MiB (:24-25). A host/GPU admitted for N compile slots can now run up to 8N concurrent compiler processes; main had one thread per worker precisely to keep nested pools inside the admitted budget (compile_pool.py:137-138 on main). This also contradicts docs/preparation-development.md:314-318.

  2. aot.py:903 puts up to 24 Inductor threads in the parent rank for the whole of _prepare_many — binding, warmup and the BindingMismatch fallback recompile — the phase where all compiled forwards are resident. Docs say "artifact binding starts no additional compiler pool"; the value (24) also ignores the configured _OPTIONS['compile_threads'] = min(16, cpu_count) and is recorded nowhere (stripped from the cache key at aot.py:825, absent from reproduction_arguments).

  3. os.cpu_count() vs sched_getaffinity (compile_pool.py:139-140, aot.py:903): a taskset/cgroup-limited rank gets the whole machine's thread budget — 3 admitted workers on a 3-CPU mask × 8 threads = 24 compilations, the exact case test_explicit_forward_workers_are_bounded_by_cpu_affinity exists to protect.

  4. Windows path untested: the untouched test at test_compile_pool.py:467-476 monkeypatches compile_pool.os to a SimpleNamespace without cpu_count, so it exercises the hasattr fallback and no test asserts worker_start_method == 'spawn'. DeferredCompiledForward.materialize_for_capture's own compile_threads=1 (aot.py:688) leaves two conflicting policies in one file, uncovered.

  5. One-sided reserve clamp (compile_pool.py:49-62): device admission clamps headroom to 1 GiB on discrete while CompilerResources.headroom keeps the 8 GiB floor, so the default 4 GiB plan headroom silently drops to 1 GiB for every discrete export — not only "large runtime reserves" as the description says — and #242's host side is untouched.

  6. Evidence/claims: no main baseline, no worker/thread counts, headline number on a 288 GB GB300 where the change matters least; CI red on head while the description lists only green runs; the 10→16 worker delta is never quantified. Tests mirror the implementation formula (test_compile_pool.py:28, test_forward_compile_options.py:282) rather than pinning values.

#11 [high] Verdict, severity ordering, and areas that are sound

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 without evidence.

Ranked:

  1. Blocker — host/device admission pricing not updated for multi-threaded workers (notes 1 and 2): 32 GiB FORWARD_WORKER_BYTES and 704 MiB _DISCRETE_WORKER_DEVICE_BYTES are single-process prices for what is now a process tree. Fix: multiply the priced bytes by inner_threads, or bound inner_threads by the admitted host-RAM budget (the principle of 468415d1/f9aec99c), not by CPU count alone.
  2. Major — aot.py:903 24 threads in the parent rank across binding/warmup/fallback recompile (note 3): contradicts docs/preparation-development.md:318, ignores the configured _OPTIONS['compile_threads'] (compilation.py:30), and is recorded nowhere. Fix: keep compile_threads=1 for binding and run the fallback recompile under one explicitly bounded pool.
  3. Major — affinity vs cpu_count (note 4). Fix: len(os.sched_getaffinity(0)) via host_memory.cpu_workers() in both places.
  4. Major — one-sided reserve clamp + undeclared default 4 GiB→1 GiB device-reserve change, #242 host side untouched (note 7).
  5. Minor/Major — Windows thread partitioning and spawn preservation unasserted; materialize_for_capture thread policy left at 1 and untested (notes 5, 6).
  6. Minor — description/evidence gaps and formula-mirroring tests (notes 6, 9).

Sound (say so plainly, do not invent issues):

#5 [medium] Windows path untested: SimpleNamespace os lacks cpu_count, so the test asserts the hasattr fallback

Windows parity: compile_pool.py:142-143 sets worker_start_method="subprocess" only when os.name != "nt". But compilation.py:31-32 sets _OPTIONS['worker_start_method'] = 'spawn' for Windows, and _compile_options(options) always merges _OPTIONS (compilation.py:35-36), so on Windows the per-worker options get worker_start_method='spawn' from defaults - untouched by this PR, fine.

The real gap: test_windows_compiler_workers_fit_host_ram_with_larger_device_budget (tests/preparation/test_compile_pool.py:467-476) monkeypatches compile_pool.os to SimpleNamespace(name="nt") (no cpu_count), so the new hasattr(os, "cpu_count") guard at compile_pool.py:139 falls back to None → inner_threads=1, and the assertion still demands (workers, compile_threads) == (1, 1). Therefore the Windows thread-partitioning path is asserted only through a fallback that cannot occur on a real Windows host, and the description's claim that "spawn" is "preserved on Windows" is untested: no test asserts worker_start_method == 'spawn' anywhere (grep: only == "subprocess" at test_compile_pool.py:30 and test_forward_compile_options.py:284). On a real Windows host with 8 workers and 64 cores, inner_threads becomes 8 and each of the 8 ProcessPoolExecutor workers gets an 8-thread Inductor spawn pool - an unvalidated configuration (ProcessPoolExecutor workers are non-daemonic, so it should work, but nothing tests it). Fix: replace the SimpleNamespace with a real os.name monkeypatch (or pass os.cpu_count into the namespace) and assert both inner_threads and worker_start_method on the nt path.

Charter tension: this is "No magic defaults / be explicit and pin everything" - three new hardcoded numbers (704 MiB, 1 GiB cap, 8) plus 24 in aot.py, none configurable, none documented (docs/cli-design.md:1463 and docs/model-preparation.md:234 describe compiler sizing only generically)."

#6 [medium] Test assertions copy the implementation formula; conflicting thread policy left in materialize_for_capture

tests at head compute expectations from the implementation instead of pinning values:

#7 [medium] Device-reserve clamp is one-sided and changes the default 4 GiB→1 GiB for every discrete export

compile_pool.py:49-51 (head): compile_reserve = headroom_bytes if shared else min(headroom_bytes, 1024 << 20) feeds only capacity (line 55) and the empty-cache probe (52-54). CompilerResources.headroom at line 62 is unchanged: max(host_memory._HEADROOM_BYTES, headroom_bytes if shared else 0) → on discrete it is always the 8 GiB floor; allocate_workers subtracts it from host memory (compile_pool.py:73-84). So the device-side clamp is silently ignored on the host side, which is #242's complaint (multi-host export compiling 22 min then failing when a rank can't host a compiler). Two consequences worth reporting:

  1. Behaviour is now asymmetric and undocumented: --headroom-bytes/plan headroom_bytes (bootstrap.py:35 default 4 GiB, bootstrap.py:134 passes plan.headroom_bytes + plan.reserve_bytes) is honoured fully on integrated GPUs, clamped to 1 GiB on discrete device admission, and still floored at 8 GiB on host admission. A configured 71.5 GiB headroom no longer raises MemoryError on discrete (test_compile_pool.py:446-452) - the description's "Result" section does not say the device reserve shrinks 4 GiB→1 GiB (default plan headroom is HEADROOM_BYTES = 1 GiB + chunk term at planning.py:30, 343-345, plus reserve), nor that this happens for every discrete export, not only "large runtime reserves".
  2. min(headroom_bytes, 1024 << 20) is hardcoded and interacts with _DISCRETE_WORKER_DEVICE_BYTES: users who raised [memory] reserve to keep a discrete GPU safe during runtime (docs/model-preparation.md:383, 480-481) silently lose that protection during compilation; when the caller sets reserve to 0, compile_reserve becomes 0 and nothing protects the runtime reserve at all. Fix: make the discrete compile reserve a named constant derived from the measured footprint (_DISCRETE_WORKER_DEVICE_BYTES * capacity), or keep the configured headroom and raise capacity by lowering device_bytes only - and update the description to state the 4 GiB→1 GiB default change.

#8 [medium] Nested compile-process tree: shutdown/interrupt coverage gap, unbounded concurrent compile high-water mark

Tracing the concurrency/ordering consequences of the new worker_start_method="subprocess" inside spawned CompilePool workers (compile_pool.py:141-143):

#9 [medium] PR description claim accuracy vs the diff

Claims checked against the diff and in-repo files:

  1. "substituting the full runtime/KV headroom_bytes under-allocates compile workers when large reserves are configured" — inaccurate scope. bootstrap.py:35 headroom_bytes: int = 4 << 30 and bootstrap.py:134 plan.headroom_bytes + plan.reserve_bytes mean the default discrete export goes from a 4 GiB device reserve to min(4 GiB, 1 GiB) = 1 GiB. planning.py:30 HEADROOM_BYTES = 1 << 30 plus the chunk term (planning.py:343-345) can exceed 1 GiB for any prefill chunk > 4096. So the default path relaxes too, which the description does not say.
  2. "CompilePool workers run inside spawned SubprocPool processes, where TorchInductor requires worker_start_method="subprocess" on POSIX to spawn nested compile workers without hitting daemonic multiprocessing restrictions" — plausible and consistent with compile_pool.py:160, but no issue, log or reproducer is cited; the only in-repo trace is the new comment at compile_pool.py:137-138. Also compile_threads=1 was what disabled nested pools, so this requirement is newly self-inflicted by the same PR.
  3. "DeepSeek-V4.1 101/101 forwards in 359.8 s ... on GB300 (SM103)" — no main baseline, no worker/thread counts, no memory samples, and GB300 is a 288 GB HBM part, i.e. the hardware where a device-reserve reduction matters least. Charter requires "benchmarks showing an actual speedup over main on a served workload (#210)". The changed admission constant is a max_models-style admission change, so an A/B on an SM120/SM121 workstation (where the 4 GiB→1 GiB reserve and 10→16 workers actually bind) is the evidence that is missing.
  4. "Updated discrete worker device sizing... and added unit coverage for ForwardCache.prepare_many compile_threads scaling" — accurate, but the exact worker delta is never quantified in the description: 10→16 workers at 12 GiB free / 2 GiB headroom (test_compile_pool.py:43-45 vs main's 10) and 3→4 at 3 GiB free (test_compile_pool.py:374-388).
  5. CI is failing on head 3400e3a while the description lists only green local unit runs; nothing explains the failure. (Per instructions, not reporting CI infra noise beyond the contradiction with the claimed verification.)
  6. The three concerns bundled here (device estimate, reserve clamp, thread/start-method policy) are separable and individually reviewable; #509's review objected to exactly this bundling pattern.