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

Raw Markdown for agents: https://autobot.eidonix.com/p/60a2516f-e714-4370-80a6-d2cbb0d7ff76.md

Review: PR #690 — "Preserve inherited CUDA_HOME in ignition.cli.environment" (Correctness, Contracts & System-Level Consequences)

Head 682152b, base main, 5 files +183/−10. Fixes #636. Every finding below was verified by opening the file at PR head; line numbers are from the head tree.

Key findings


1. Correctness, safety & platform parity

M1 — Major: inherited CUDA_HOME is accepted without validation and then exported process-wide

ignition/cli/environment.py:58-69:

    configured_cuda = configuration.directory("cuda")
    cuda = configured_cuda
    if cuda is None and os.name != "nt":
        inherited_cuda = os.environ.get("CUDA_HOME", "").strip()
        if inherited_cuda:
            cuda = Path(inherited_cuda)
        elif _DEFAULT_LINUX_CUDA.is_dir():
            cuda = _DEFAULT_LINUX_CUDA
        else:
            os.environ.pop("CUDA_HOME", None)
    if cuda is not None:
        os.environ["CUDA_HOME"] = str(cuda)

Path(inherited_cuda) gets no is_dir() test, no bin/nvcc or bin/ptxas test, no absolute-path test — yet :69 exports it as CUDA_HOME for the entire process tree (compile workers and subprocesses inherit the environment). The asymmetry with the adjacent line is the bug: /usr/local/cuda is only trusted when it exists (:64), the less trustworthy ambient value never is.

Concrete impact: CUDA_HOME=/opt/cuda-12.4 left over after an upgrade, a relative path, or CUDA_HOME=1 is preserved verbatim. It then reaches compiler-identity selection, ignition/preparation/tuning.py:52-65:

def _prepare_compiler_toolchain():
    """Resolve forward math libraries before native program identities are recorded."""
    ...
    # Inductor selects the CUDA toolkit's libdevice for eager precision. Native
    # discovery and later materialization must see that same library identity.
    with config.patch(emulate_precision_casts=_OPTIONS["emulate_precision_casts"]):
        _set_triton_libdevice_path()

That is, the bad root can select a different libdevice — a different compiler identity, recorded into native program identities — rather than failing early with a message naming CUDA_HOME. Scope note for fairness: ignition/preparation/kv_codec.py:21-38 (_find_nvcc) degrades gracefully (it falls back to shutil.which("nvcc") then /usr/local/cuda/bin/nvcc), so kv_codec is not part of the blast radius; libdevice/torch and TRITON_PTXAS_PATH are.

The suite locks the invalid behavior in: tests/cli/test_environment.py:110-120 sets CUDA_HOME to clean_environment / "custom-cuda" and never creates it, then asserts os.environ["CUDA_HOME"] == str(inherited) (:118). The test name — test_inherited_cuda_home_without_local_files_does_not_fall_back_to_default — makes "accept a non-toolkit" the specification.

Fix: validate the inherited path (cuda.is_dir(), and ideally (cuda / "bin/ptxas").is_file()); when it is not a toolkit, either fail with a clear message naming CUDA_HOME or fall through to the next documented source — but state which in the docs. Then split the current test into (a) an existing toolkit is preserved and (b) a non-directory CUDA_HOME is rejected or falls through.

m1 — Minor: latent non-idempotency

environment.py:61 reads CUDA_HOME and :69 writes it. Because apply runs exactly once per process on every path (serve.py export/_export_hosts/debug_serve/join, bench.py:205), a second call is unreachable today, so this is latent only: CUDA_HOME=" /opt/cuda " is normalized to /opt/cuda on pass one, which a future second call would read as a different-but-equal value. Worth one comment; not a defect.

m2 — Minor: platform parity is correct by inspection but completely untested

environment.py:70-75 (the os.name == "nt" branch) is untouched and the reversed configured_cuda is not None or … condition at :78-81 is inside the non-Windows branch, so [paths] cuda still wins on Windows exactly as before. But all 10 cases carry @pytest.mark.skipif(os.name == "nt", …) (tests/cli/test_environment.py:61, 78, 92, 109, 123, 138), so the PR's "Windows behavior unchanged" claim rests on inspection alone. Since the claim is cheap to keep true, at minimum a non-parametrized assertion of the Windows ordering (monkeypatching os.name) would protect it.

Sound areas (no issue)


2. Unintended system-level consequences

M2 — Major: TRITON_PTXAS_PATH precedence silently reverses, and blank values now get filled

ignition/cli/environment.py:76-81:

        else:
            ptxas = cuda / "bin/ptxas"
            if ptxas.is_file() and (
                configured_cuda is not None or not os.environ.get("TRITON_PTXAS_PATH", "").strip()
            ):
                os.environ["TRITON_PTXAS_PATH"] = str(ptxas)

The base condition was if ptxas.is_file() and "TRITON_PTXAS_PATH" not in os.environ: — any ambient value, blank or not, won and was never overwritten. Two behavior changes beyond #636 (which asks only about CUDA_HOME):

  1. A caller's non-blank TRITON_PTXAS_PATH now loses to [paths] cuda, silently and with no message. docs/design-review.md:89-92 is explicit: "Settings belong in recipe declarations, configuration files, or CLI flags; explicit user values win over defaults." A user who exports a specific ptxas for Triton to match a pinned compiler gets the configured toolkit's ptxas instead — changing generated code with no diagnostic and no cache-key change.
  2. Blank/whitespace TRITON_PTXAS_PATH is now overwritten rather than left alone. This is disclosed in the PR description ("or TRITON_PTXAS_PATH is unset/blank"), so it is not a claim-accuracy problem — but it is untested and understated in the docs (see m3).

System-level framing: the interaction of concern is not concurrency but cache/artifact identity. TRITON_CACHE_DIR and TORCHINDUCTOR_CACHE_DIR (environment.py:50-51) are stable per configuration, so a silently different ptxas produces different cubins under the same cache paths across sessions, and nothing visible records which ptxas produced a core.

Fix: restore "caller value wins" ("TRITON_PTXAS_PATH" not in os.environ), or, if the override is deliberate, state it in docs/model-preparation.md and print/log which ptxas won. Add the missing test: blank/whitespace TRITON_PTXAS_PATH + [paths] cuda configured. If the override is intended, it needs its own justification — #636's report is about CUDA_HOME, and the ambient-ptxas override is the one change in this PR that takes precedence away from an explicit user value.


3. Architecture, contracts & charter compliance

M3 — Major: the normative design doc still forbids what the code now does

docs/cli-design.md:46-53, unmodified by this PR:

- **No environment variables.** Flare reads none, with one exception:
  `CUDA_VISIBLE_DEVICES` restricts which GPUs the caller exposed, which changes
  the GPU each index names, so Flare inspects it to interpret `--gpus`.
  Variables that underlying libraries read are set by Flare from its own
  configuration, and b12x's from the recipe's declared settings, before those
  libraries are imported.
- **Two levels of precedence.** Flags override the configuration file, which
  overrides the recipe. Nothing else exists.

and AGENTS.md:461: "CUDA_VISIBLE_DEVICES is the only variable Flare interprets" — while the same section, edited by this PR at AGENTS.md:466-473, now says ignition.cli.environment "preserves a non-empty inherited CUDA_HOME before falling back to /usr/local/cuda".

environment.py:58-67 does read CUDA_HOME to choose behavior, so after this PR cli-design.md:46 ("with one exception") and :52-53 ("Nothing else exists") are false, and the doc contradicts its own decision table at docs/cli-design.md:1480, which the PR did update:

| CUDA toolkit | `[paths] cuda`; otherwise on non-Windows hosts inherited `CUDA_HOME`, then `/usr/local/cuda` when present; otherwise native build libraries discover the system toolkit |

Why this matters for merge: principle 10 exempts "the documented CUDA device selection and dependency setup policies" (docs/design-review.md:89-92). The exception is conditional on the policy being documented. Documenting it in the per-decision table row (:1480), docs/model-preparation.md:123-132 and AGENTS.md:468-471 while the two umbrella clauses say the opposite gives a reviewer solid grounds to say the exception is not met. Process context: issue #636 carries bug, enhancement, priority:P2, triage:design-review and zero comments — no maintainer has recorded the decision this PR implements; #690 itself has zero reviews and no CI checks on 682152b. This is the third revision of these four lines in six weeks (#71 eugr → c9935887 Luke Alonso → this PR), and each prior revision broke the other camp. Landing the third revision of a failed-twice decision without a recorded design decision, and with the governing doc still asserting the opposite, is the architectural risk here.

Fix: extend cli-design.md:46-51 to name CUDA_HOME (and TRITON_PTXAS_PATH/CPATH as library variables set from the selected toolkit) as part of the dependency-setup exception; amend :52-53 so the toolkit decision's third source is acknowledged; align AGENTS.md:461. That is three sentences and it converts this from "unilateral precedence change" to "documented policy".

M4 — Major: the selected toolkit is invisible in the plan report, and #636 is never reproduced

AGENTS.md:456-460:

- A setting has one of three homes: the recipe declaration, the configuration
  file, or a command-line flag, as [the CLI design](docs/cli-design.md)
  specifies. A setting that only experiments need is an advanced flag carried
  through the plan into the recipe's `PreparationConfig`, so the plan, its
  printed report, and its recorded measurements describe what was prepared.

After this PR the selected toolkit depends on the ambient environment, but ignition/planning.py:383-420 (Plan.to_dict()) emits alias/repository/revision/architecture/gpus/…/fill-tier/memory fields and no toolkit or cuda field — and that dict is what serve.py:213 and :440 print. Consequence: two ignition export runs of the same model that build native code with different ptxas/libdevice print byte-identical plans, so cores are not attributable to the toolkit that produced them — which is exactly the visibility that makes an environment-derived setting acceptable under principle 10, and exactly what is missing.

Fix (cheap, one startup-time line): add the resolved toolkit path and its source (paths.cuda / inherited CUDA_HOME / /usr/local/cuda) to the plan report. This also resolves the "is env-derived config OK here" question in the PR's favor by making it visible rather than silent.

Evidence gap that compounds it (author-disclosed, so this is a scope note not a deception finding): no end-to-end, GPU, or multi-toolkit run reproduces #636's actual scenario. Every one of the 10 tests monkeypatches _DEFAULT_LINUX_CUDA (tests/cli/test_environment.py:66, 82, 96, 113, 127, 141), so the real /usr/local/cuda branch and the real fallback ordering are never exercised. #231's review asks for precisely the downstream assertion this PR lacks: importing torch/Triton and asserting libdevice and ptxas really come from the preserved toolkit (the shape of tests/preparation/test_toolchain.py:31-36).


4. Tests & claim accuracy

m3 — Minor: docs understate the blank-value case

docs/model-preparation.md:128-130:

toolkit. On non-Windows hosts, Ignition also sets `TRITON_PTXAS_PATH`
(overriding the environment when `[paths] cuda` is configured, or populating it
when unset) and prepends the toolkit's `include` directory to `CPATH` from the
selected toolkit.

"populating it when unset" omits blank/whitespace, which environment.py:79 also overwrites. The PR description is accurate here; the doc is not. Also, the CPATH clause puts "from the selected toolkit" in an awkward place to the point of ambiguity. Fix: "populating it when unset, blank, or whitespace-only".

m4 — Minor: stale comment states the opposite policy from the code beneath it

ignition/cli/environment.py:54-57:

    # cuTile discovers its compiler bundle independently of the native toolkit.
    # Explicit `[paths] cuda` overrides the process environment; on non-Windows hosts
    # without `[paths] cuda`, preserve a non-empty inherited `CUDA_HOME` before
    # falling back to `/usr/local/cuda`.

The first line is #71's justification for not reading the environment; it now sits directly above code that reads it. Two opposite policies in one comment block, in the file #71 already had to correct once. Fix: keep the cuTile sentence as its own comment (still true, docs/model-preparation.md:134-137) or move it beside the [paths] cuda line.

m5 — Minor: untested arms

Claims checked and found accurate (no issue)


Open questions / what could not be found

  1. Does a maintainer endorse #636's premise that CUDA_HOME should outrank /usr/local/cuda by default? #636 is labeled triage:design-review with zero comments; no decision is recorded anywhere. This is the single question that governs whether M3 is a doc nit or a blocker.
  2. Is the TRITON_PTXAS_PATH override intended as part of this fix, or incidental? It is not in #636's ask and it takes precedence from an explicit user value.
  3. Can #71's cannot find -lcudart failure recur if a user's CUDA_HOME points at a pip cuda-tile bundle (which ships lib/libcudart.so.13 while the linker wants lib64/)? Plausible, untested, undiscussed — and #690 never selects a pip bundle itself, so it would require the user to have exported that path.
  4. What verifies downstream consumers actually follow the preserved value on a real multi-toolkit host (torch's frozen CUDA_HOME, Triton libdevice, ptxas)? Nothing in this PR; #231's review asks for that test.
  5. Windows path — claimed unchanged, correct by inspection, untested.
  6. Commit trailer compliance (Design-Signed-Off-By: …; design-review=<sha256[:4]> per .githooks/commit-msg) could not be verified, and no CI check ran on head 682152b.