Simplicity scratchpad · NVIDIA/hyperflare-inference #581

Raw Markdown for agents: https://autobot.eidonix.com/p/533976f2-0f81-4272-b6a1-e08c58ab95b7.md

#1 [high] should fix: instantiation_flags kwarg is an unused configuration switch (two of three modes have no caller)

PR 581, ignition/preparation/runtime_core.py:208-243 (head 61f64f1).

record_export_graph(..., instantiation_flags=CUDA_GRAPH_INSTANTIATE_FLAG_USE_NODE_PRIORITY) has three modes:

Callers in this repo: exactly one, ignition/preparation/capture.py:193 record_export_graph(graph, warmup_storage, owners=(function,)) -- no instantiation_flags, so always default. (search_code for record_export_graph at pr/581 head: runtime_core.py:208 def, capture.py:193, tests only; tests/preparation/test_capture_rank_readiness.py:61 replaces the function wholesale.)

So the None and 0 branches plus the input validation at lines 224-229 exist only to be tested (tests/preparation/test_capture_graph_metadata.py:222-255). The PR description justifies None as "keeps executable-derived flags for startup tuning graphs", but startup tuning graphs never call record_export_graph (they go through capture_graph()/instantiate_graph(), runtime_core.py:246-269), and voipmonitor's review item 3 already says that sentence is inaccurate.

Rule: AGENTS.md "Contribution scope and rejection criteria" -> "Reject unsupported abstractions and churn. Do not add frameworks, extension points, speculative compatibility, fallback layers, or configuration switches without a concrete requirement." Same section lists "Flags, enums, callbacks, optional paths" as things that cannot be used to dodge boundaries. docs/design-review.md principle 15 repeats it.

Smaller version:

def record_export_graph(graph, storage, *, owners=()):
    """Retain a captured template, its executable and external export resources.

    Owners must not reference the graph: Torch's destruction finalizer retains
    them independently. Native replay callers must drain before resetting graphs.
    Exported flags combine the executable's permitted flags with node priority so
    Helios honors captured stream priorities when it instantiates the core.
    """
    pools = (storage.pool, storage.graph_pool)
    owned = {pool.id for pool in pools}
    if not set(graph.pools()) <= owned:
        raise ValueError("export graph uses a CUDA pool without an external owner")
    graph.retain_object((pools, tuple(owners)), synchronize_before_release=True)
    instantiate_graph(graph)
    graph._flare_instantiation_flags = _executable_instantiation_flags(graph) | 8
    return graph

That deletes the kwarg, lines 224-229, the if instantiation_flags is not None: wrapper, the 4-line docstring paragraph, and the prior-flags guard at 240-241 (the same check already runs on every export in _graphs, runtime_core.py:282-287 -- needs the shared constant there instead of the hardcoded ~9).

What would settle it: a caller outside this repo (hyperflare-recipes) passing instantiation_flags would keep the kwarg honest, but no such caller is shown in the PR and the description names none.

#3 [high] should fix: duplicate flag validation (record-side guard is subsumed by _graphs, constant left unadopted)

PR 581, ignition/preparation/runtime_core.py:238-241 and :282-287.

Two checks reject the same thing with the same message:

The record-side guard cannot catch anything _graphs misses: it stores prior_flags | instantiation_flags, so any unsupported bit in prior_flags is still set when _graphs re-checks, and _graphs runs on every export of that graph (RuntimeCore.write / export_model both call _graphs, runtime_core.py:122 and :397). So the second guard only changes the message and the moment of failure (capture vs export) — one of the two should go.

Also PERMITTED_CUDA_GRAPH_INSTANTIATE_FLAGS (lines 192-197) is introduced but _graphs still hardcodes ~9 and repeats the error string verbatim, so the module now has the mask in two forms and one message in two copies. Main already had the ~9; this PR added the constant without adopting it. (voipmonitor's review nit 4 makes the same ~9 point.)

Fix: keep one check. Either (a) delete lines 240-241 and let _graphs reject, using PERMITTED_CUDA_GRAPH_INSTANTIATE_FLAGS there instead of 9; or (b) keep the early record-side check and change _graphs to the constant, dropping the duplicated string.

Cross-language copies (heliopause/src/export/capture.rs:2823 and 589, helios/src/runtime_core/format.rs:495) are pre-existing and out of scope.

#7 [high] Research summary

Reviewed PR 581 through lens D (simplicity). The functional change is small and mostly sound — node priority ends up in the recorded bitmask and Helios already accepts 9 — but the patch ships more surface than the single caller needs, and it adds a second copy of a validation both record_export_graph and the pre-existing _graphs already perform.

Findings, all recorded on the scratchpad:

  1. should fix — instantiation_flags on record_export_graph (runtime_core.py:208-243) is a new optional path whose None and 0 modes have no caller in the repository; the only production caller (capture.py:193) takes the default. The PR justifies None with "startup tuning graphs", which never call record_export_graph at all. That is exactly the "optional paths / configuration switches without a concrete requirement" that AGENTS.md and design-review principle 15 say to reject; the smaller version drops the kwarg and the three-branch body.
  2. should fix — the new record-side prior_flags guard (lines 240-241) is subsumed by the existing _graphs check (lines 282-287), which runs on every export and sees the same ORed value. The new PERMITTED_CUDA_GRAPH_INSTANTIATE_FLAGS constant is introduced but _graphs still hardcodes ~9 with the identical error string, so the module now has the mask in two forms and one message in two copies. Keep one check, use the constant in it.
  3. nit — the new _Cuda() per graph in _executable_instantiation_flags reopens libcuda per exported graph; the storage argument already owns a handle.
  4. nit — the added four-line docstring paragraph paraphrases prior | requested; one sentence naming the actual constraint (Helios accepts only bits 1 and 8) is shorter and more useful.
  5. nit — the new test test_capture_forwards_export_sets_node_priority_instantiation_flag re-implements the whole capture_forwards mock surface to re-assert a value already asserted directly, and the None/0 test cases cover modes with no caller; they should go with the kwarg.

No correctness defects found in the lens; the GPU-qualification gap is voipmonitor's area.

#8 [high] Rule citations for the lens-D findings (charter, AGENTS.md, design-review principle 15, prose skill)

Citations the report must carry (PR 581, lens D):

#9 [high] Verified context facts: the change works end-to-end; smaller version stays compatible

Verified at PR 581 head (61f64f1), needed so the report can say the simplifications are behaviour-preserving:

#2 [medium] nit: permitted-flag mask and error text duplicated inside runtime_core (_graphs still hardcodes ~9)

PR 581, ignition/preparation/runtime_core.py:192-197 (constants), 224-229 (check), 282-287 (_graphs).

Head has two copies of the permitted mask and two copies of one error string in one module:

Main already had the _graphs hardcode (main runtime_core.py:245-249), so the PR adds the constant without adopting it. Also the third copy of 9 lives in heliopause/src/export/capture.rs:2823 and helios/src/runtime_core/format.rs:495 (cross-language, out of scope).

Smaller version: _graphs uses flags & ~PERMITTED_CUDA_GRAPH_INSTANTIATE_FLAGS and one module-level message constant, or the record-side check is dropped entirely (it duplicates what _graphs rejects on every export) so only one copy remains.

Note: voipmonitor's review nit 4 raises the same ~9 point; keep the finding short and reference it.

#4 [medium] nit: new tests cover kwarg modes with no caller and duplicate capture_forwards mocking

PR 581, tests/preparation/test_capture_graph_metadata.py:222-255, 258-302; tests/preparation/test_recorded_export_graph_cuda.py:85-87.

Findings about test value under the simplicity lens:

  1. test_capture_forwards_export_sets_node_priority_instantiation_flag (lines 258-302) re-implements the whole capture_forwards mock surface (torch.cuda stream/graph/memory fakes, _warmup_stream, _seed_warmup_workspaces, an ExportGraph subclass with pools()) only to re-assert the same value already asserted directly at lines 215-218 (expected_combined). Its distinct content is that capture.py:193 calls record_export_graph without instantiation_flags — i.e. it pins the default value of the new kwarg. If the kwarg goes (entry 1), this test goes with it. It also duplicates the file's existing stream-mocking test (test_shared_capture_records_after_all_warmups_without_reinstantiation, lines 165-194) with no new stream/isolation property.

  2. Lines 222-235 (no_autofree, preserved_only) and 237-242 (plain) exist to cover the instantiation_flags=None and instantiation_flags=0 modes; those modes have no production caller (entry 1). no_autofree (exec_flags=0 -> flags 8) is the only case that adds a distinct checked combination (executable without AUTO_FREE), and it could be folded into the first graph's assertion by parametrizing exec_flags.

  3. test_recorded_export_graph_cuda.py:85-87 builds flags_by_name (a dict comprehension) to assert {"decode": 9, "tune": 1} and then all(... node_count > 0 ...). The dict form saves nothing over the original per-item assertions: the old code asserted graphs[0].flags == 1 and node_count > 0; the new code asserts tune=1 and decode=9, which is the intended new property, so this change is fine and not a finding — only note that the all() call widened node_count checking from graph 0 to both graphs, which is harmless.

  4. test_export_instantiates_and_retains_executable lines 250-255 (bad, instantiation_flags=2) checks the up-front rejection at lines 224-229 leaves no retained objects and no instantiation. Worth keeping only while the parameter (and its validation) exists; otherwise delete with it. Also tests/preparation/test_runtime_core.py:31-37 already covers rejection of unsupported flags on the _graphs side.

None of these is a correctness problem; they are extra surface that mirrors code which itself has no caller.

#5 [medium] nit: _Cuda() reopened per graph instead of reusing the storage handle

PR 581, ignition/preparation/runtime_core.py:200-205.

_executable_instantiation_flags creates a fresh _Cuda() per graph. _Cuda.__init__ does C.CDLL("libcuda.so.1") / WinDLL("nvcuda.dll"), so every exported graph adds a dlopen handle. Elsewhere _Cuda instances are created once per storage object (core_storage.py:76, virtual_storage.py:27), not per call.

Smaller version: build _Cuda() once per record_export_graph call at most, or accept a cuda argument from the caller that already owns one (warmup_storage.cuda exists at core_storage.py:76, but record_export_graph(graph, storage, ...) receives the storage object already, so storage.cuda.call(...) would reuse the live handle). That removes the extra _Cuda() construction entirely and keeps one driver handle per prepared model.

Also the error path names nothing: _Cuda.check raises f"{name}: CUDA {status}" so a failure reads "cuGraphExecGetFlags: CUDA 1", which is acceptable for the existing style but means the new call adds no context about which graph failed. Minor; the caller already wraps capture names in the progress reporter.

Note voipmonitor's review nit 4 raises the same caching point.

#6 [low] nit: added docstring paragraph restates the assignment

PR 581, ignition/preparation/runtime_core.py:215-223 (docstring).

The added docstring paragraph is 4 lines restating what the two-line code does, and part of it describes a mode with no caller:

"When instantiation_flags is set, export combines those flags with any permitted flags already carried by the instantiated executable (such as CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH) so Helios instantiates the graph with matching flags while honoring per-node stream priorities."

graph._flare_instantiation_flags = prior_flags | instantiation_flags already says "prior | requested". Per .agents/skills/repo-prose/SKILL.md ("State the substantive fact, requirement, assumption, or limitation directly. Do not add commentary about why a statement is present"), the durable fact worth one sentence is the invariant that the recorded bitmask must stay within what Helios accepts (9, format.rs:495) and that node priority is what makes captured stream priorities take effect. Suggested one sentence:

"""Retain a captured template, its executable and external export resources.

Owners must not reference the graph: Torch's destruction finalizer retains them independently. Native replay callers must drain before resetting graphs. Exported flags keep the executable's permitted bits and add node priority, the only combination Helios accepts when it instantiates the core. """

That is shorter than the added paragraph and names the actual constraint (Helios's validator) instead of paraphrasing the assignment.