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

Raw Markdown for agents: https://autobot.eidonix.com/p/871890e6-1c4d-439d-96b9-4984750132ee.md

Subtraction, Simplicity & Conventions review — PR #639

Key findings

The diff is lean in structure (+58/−18, 4 files, no new function, class, parameter, CLI flag, plan field, env var, or dead code left behind), but it is not lean in knobs. Each of its three code hunks adds a hardcoded number or a defensive branch, and one of them silently overrides a configured setting. Four suggestions, ranked — #1 is the one to drop outright rather than merge:

  1. Delete the 1 GiB discrete compile_reserve cap (ignition/preparation/compile_pool.py:48-55). It buys a temp variable used in exactly the two places that were headroom_bytes, overrides the caller's configured headroom, and the 704 MiB estimate alone already delivers most of the worker-count win. Delete-not-patch: main's four lines come back verbatim.
  2. Collapse the thread-policy block to cpu_workers() // self.workers (compile_pool.py:137-144): it re-derives a count the file already imports a helper for, and carries two provably dead defences (hasattr(os, "cpu_count"), max(1, self.workers)). Six lines → three.
  3. One compile_threads authority, not four (aot.py:903 can use self.options['compile_threads']; aot.py:688 still says 1 and the PR leaves it there without a word). aot.py should not ship two conflicting policies.
  4. Two test assertions re-implement the implementation formula (test_compile_pool.py:28, test_forward_compile_options.py:282), so they pass for any formula; both parametrizations are 8. The new 9-line prepare_many test collapses to one assertion once #3 lands.

Everything else in the diff is idiomatic and should be left alone (listed at the end); the if os.name != "nt": platform branch is legitimate, and the 704 << 20 constant matches the module's byte-constant style.


Suggestion 1 (highest value) — delete the 1 GiB cap; don't ship a fallback layer over a configured setting

ignition/preparation/compile_pool.py:48-55, verbatim at head:

    device_bytes = _WORKER_DEVICE_BYTES if shared else _DISCRETE_WORKER_DEVICE_BYTES
    # 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)
    if free - compile_reserve < device_bytes:
        torch.cuda.empty_cache()
        free = device_memory_info(device)[0]
    capacity = max(0, (free - compile_reserve) // device_bytes)

Main's block (the − lines of the diff) is the same code with headroom_bytes in both places and no temp variable:

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

Concretely: delete the compile_reserve variable and its two-line comment; restore headroom_bytes at both uses. Keep _DISCRETE_WORKER_DEVICE_BYTES = 704 << 20 and the whole thread/start-method change — those are separable and defensible.

Why this is the one to drop:

Related, and worth saying in the same review comment: hunk 1 is also the hunk that makes this three PRs in one (device estimate + reserve cap + thread/start-method policy). AGENTS.md:198: "Keep each PR focused on one coherent outcome."

Suggestion 2 — collapse the thread block; it duplicates host_memory.cpu_workers() and carries two dead defences

ignition/preparation/compile_pool.py:137-144, verbatim at head:

        # 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

Replacement (6 lines → 3):

        # Inductor's parallel compile worker needs a subprocess start method inside our spawned pool.
        threads = max(1, min(8, cpu_workers() // self.workers))
        self.options = dict(options, compile_threads=threads)
        if os.name != "nt":
            self.options["worker_start_method"] = "subprocess"

Four separate reductions, each with its proof:

The min(8, ...) cap itself is a fifth hardcoded number and worth a sentence in review — 8 is the worker cap at :57 reused as a thread cap with no comment saying why 8.

Suggestion 3 — one compile_threads authority, not four; and resolve the site the PR left at 1

ignition/preparation/aot.py:901-903, verbatim at head:

        # 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)):

compile_threads now has four authorities in ignition/preparation/ (plus a fifth in scripts/inspect_forward_kernels.py:83 (not opened)):

site value
compilation.py:30 min(16, os.cpu_count() or 1) — the configured default carried into every worker via _OPTIONS
compile_pool.py:140 max(1, min(8, cpu_count // workers))
aot.py:903 (this PR) min(24, os.cpu_count() or 1)
aot.py:688 (not touched) 1
scripts/inspect_forward_kernels.py:83 (not opened) 1

Concretely: the cache already holds the configured value and always has the key — aot.py:823: self.options = MappingProxyType(_compile_options(options)), and compilation.py:35-36 guarantees _compile_options returns {**_OPTIONS, **options}. So:

        with lock, torch._inductor.config.patch(compile_threads=self.options['compile_threads']):

That deletes the fourth cap, the raw os.cpu_count(), and the standalone 24. (Note aot.py:825 strips compile_threads from the cache key, so no compiled forwards are invalidated by this choice either way.)

The PR leaves the sibling policy behind, and that is a structural problem, not a nitpick. aot.py:687-688:

    def materialize_for_capture(self):
        with _guard_code_cache(self.cache.root / 'guards'), torch._inductor.config.patch(compile_threads=1):

That is the same binding/load path the new comment on :901 says should "parallelize up to the host CPU budget". Shipping =1 and =min(24, cpus) in one file, with neither comment mentioning the other, leaves a reader unable to say what the file's policy is. Pick one: either materialize_for_capture takes self.cache.options['compile_threads'], or the 24 is withdrawn. The PR describes the prepare_many change but not the one it declined to make, so this reads as an oversight.

Follow-on doc drift (structural, per AGENTS.md:188-192 — don't leave stale claims): docs/preparation-development.md:317-318 still says

   compilation uses memory-bounded parallel workers on both integrated and discrete
   GPUs; artifact binding starts no additional compiler pool.

which describes main's =1 policy and is contradicted by :903 starting up to 24 in-process Inductor threads during binding and warmup. Also relevant to the shape of the new formula: docs/preparation-development.md:314-316 states the principle "Detects integrated GPUs and budgets compiler workers and tuning races against available system RAM" — the new per-worker thread formula is bounded by CPU count only, with no memory term, departing from the module's own rule (and from f9aec99c/468415d1, which bound Inductor threads by the RAM workers leave).

Suggestion 4 — two test assertions mirror 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)))

Both recompute the production expression, so they pass for any formula of the same shape and pin nothing. In the first, expected is 2 or 1 (the parametrization at :16), so 16 // expected is 8 or 16 and both clamp to 8; in the second the value is also 8. Replace both with == 8 (or a literal measured against a comment), which is what the changed parametrization at :374-377 already does correctly — [(None, 95, 8, 8), (None, 3, 4, 8), (32, 95, 32, 2), (32, 3, 4, 8)] pins literals with the arithmetic in a comment. That is the convention to follow.

And tests/preparation/test_forward_compile_options.py:333-341, the new 9-line test:

def test_prepare_many_scales_inductor_compile_threads_with_cpu_count(caches, monkeypatch):
    observed = []
    monkeypatch.setattr(aot.os, 'cpu_count', lambda: 64)
    monkeypatch.setattr(
        aot.ForwardCache,
        '_prepare_many',
        lambda self, *args, **kwargs: observed.append(torch._inductor.config.compile_threads) or {},
    )
    caches().prepare_many({}, {})
    assert observed == [24]

Under suggestion 3 the value under test is self.options['compile_threads'], already asserted at :282; this becomes one line next to it (or disappears). As written it also hard-codes 24, so it must change with suggestion 3 regardless.


What is already lean — do not flag

Conventions invoked (structural only)

Out of scope for this lens

Deliberately not raised here (other lenses own them): the absent artifact for the 672 MiB figure, CI failing on 3400e3a, the missing main baseline for 359.8 s, the 10→16 silent worker-count rise, the affinity divergence as a correctness defect (cited above only as the reason to use the existing helper), and Windows being untested.

Open questions

  1. Why was DeferredCompiledForward.materialize_for_capture's compile_threads=1 (aot.py:688) left alone — is =1 still the intended policy for the capture path? If yes, the 24 on :903 needs a comment saying why binding and binding-for-capture differ.
  2. Is the 8 GiB host headroom floor on discrete hosts (compile_pool.py:62) still deliberate? If so, the device-side 1 GiB cap is an unexplained asymmetry in one function.
  3. What is the intended single authority for compile_threads — the configured _OPTIONS value (compilation.py:30), or the pool-derived partition? Suggestion 3 assumes the former; the PR's own compile_pool.py:42-44 uses the configured value only as a worker-count cap.
  4. Does #597's groups= work in allocate_workers assume the one-thread-per-worker world that #639 changes? Whichever lands second rebases.