compile --allow-partial: do not cache an artifact when nothing was compiled

Merged
TO
14h ago
+731-79

What was wrong

linml compile --provider trt --allow-partial on mobilenetv2-7 reported success: true, unsupported: true ("no nodes compiled; passthrough artifact") and published a 495-byte file into the cache. That file has no EPContext node. It holds fused TRTKernel_graph_main_... nodes, which no ONNX Runtime can resolve. Every later linml run --provider trt then picked it up through TryFindCompiledArtifact, logged Compiled artifact ... rejected (... is not a registered function/op); falling back to original model, and still passed. Both model-zoo matrices (#73, harness in #81) show this on every TensorRT warm run.

Cause: with partial compile allowed, OnnxCompiler decided unsupported from the absence of EPContext but still File.Moved the temp file to outputPath.

The fix

Do not publish an artifact when nothing was compiled. Chosen over "publish only one that loads" because a load check means creating a session per compile (slow, and a TensorRT session is a ~25 s engine build), and because a file with no EPContext node has no value to keep: it compiled nothing.

  • OnnxCompiler: in partial mode, a temp file without an EPContext operator is deleted and not moved. The outcome is Success=false, Unsupported=true, ArtifactBytes=0. For the built-in cpu, cuda and trt providers that is the same result strict mode gives. For a plugin EP strict mode reports a plain failure (unsupported:false, since the EP may simply have claimed no node), while --allow-partial has always reported unsupported:true and still does; I kept that so the frozen harness classifies those cells as before. A pre-existing file at outputPath is never touched (as before). This applies to an explicit --output too, since a TensorRT artifact there is just as unloadable.
  • The EPContext check also looks inside control-flow subgraphs (If/Loop bodies, depth-capped at 32), because ORT partitions those too and a real artifact must not be classified as a passthrough. OnnxProtobufReader.Parse still reports main-graph nodes by default, so inspect output is unchanged.
  • CompileCommand: detail text is now no nodes compiled; no artifact written (the old text claimed a passthrough artifact). --allow-partial help text updated.

Artifacts already cached by earlier versions

The cache key does not include --allow-partial, so a poisoned file from before this fix sits at exactly the path a recompile targets, and warm runs would keep rejecting it. Two measures:

  1. OnnxSessionFactory ignores a cached artifact that parses as ONNX and has no EPContext node (Information log, no deletion; a file that cannot be parsed is left for ORT to decide). Existing caches become inert with no action by the user, and no valid artifact can be lost to a transient load failure, because nothing is deleted on a rejection.
  2. A cache-keyed linml compile --allow-partial (no --output) that again compiles nothing removes a stale artifact (and its sidecar) at its own cache key, only if that file is a no-EPContext passthrough. A real artifact is left alone. Only the mode that used to write these files cleans them up: a strict compile, or a compile with --output, leaves the cache untouched. linml cache clear --provider <name> --yes still works for anyone who does not recompile.

Opt-in compile --verify

"Has an EPContext node" only stands in for "loads". With --verify, the freshly compiled artifact is loaded into a throwaway session for the same provider (set up exactly as session creation sets it up) before it replaces anything at the output path. An artifact ONNX Runtime rejects is not written, an existing file at the output path is left alone, and the row reports the load error with success:false (exit 1). The JSON row gains a verified field, true only when the check ran and passed. It is off by default because it costs a session creation: on trt-rtx with mobilenetv2-7 the compile went from about 2 s to about 10 s, the engine build. The post-compile publish step moved into OnnxCompiler.FinishCompile so this decision is testable without a GPU.

What changes for users

beforeafter
compile --allow-partial, trt/cpu/cuda (and any provider that compiles zero nodes)exit 0, success:true, unsupported:true, file cachedexit 1, success:false, unsupported:true, artifactBytes:0, nothing cached
matrix mode (no --provider) with --allow-partialexit 0 even if nothing compiled anywhere (cpu "succeeded")exit 1 only when no provider really compiled; exit 0 if another provider succeeded
strict mode, probe, compile with --outputunchangedunchanged
compile that really produces EPContext (trt-rtx etc.)unchangedunchanged

CPU and CUDA passthrough artifacts do change. They loaded, but they were plain re-serialized copies of the model: for mobilenetv2-7 the cpu and cuda files are byte-identical to each other (14,248,257 bytes), the original is 14,246,826 bytes, and linml inspect shows the same 155 nodes for both. They are no longer written, so a warm run loads the original model, with the same graph and the same output. The only thing lost is skipping a re-parse of an identical graph.

I checked linml cache, docs/ and compile's exit code and JSON for anything that depended on the artifact existing: ProbeCommand runs strict (unaffected) and only reads success/unsupported/detail; linml cache list just lists whatever is on disk; the README compile section (duplicated in the file, both copies updated), docs/compatibility.md and CHANGELOG.md are updated. The JSON row gains one field, verified (see --verify); otherwise the field set is unchanged (artifactBytes is now 0 and detail changes in this case).

Verification

Tests written first and seen failing for the right reason (Success was true / exit was 0 / file still present) before the change:

  • OnnxCompilerTests: partial mode with nothing compiled publishes no artifact, leaves no temp file, and leaves an existing file at outputPath byte-identical. An EPContext node inside a control-flow subgraph is found, other subgraph operators are not, 5000-deep nesting does not overflow, and Parse still reports main-graph nodes only by default. Runs on CPU, no GPU needed.
  • CompileCommandTests: --allow-partial on cpu exits 1, reports success:false/unsupported:true/artifactBytes:0, writes no file. CompileStaleArtifactTests (WslProbe collection, as it redirects XDG_CACHE_HOME): a pre-fix passthrough artifact and sidecar at the cache key are removed by an --allow-partial compile and a neighbouring entry survives; a strict compile and a compile with --output leave the cached artifact in place.
  • OnnxSessionFactoryArtifactTests: a no-EPContext artifact is ignored but not deleted, one with EPContext is kept, an unparseable one is left to ORT.
  • --verify: when the load check fails nothing is published and an existing output file is untouched; when it passes the artifact is published and reported verified; it does not run when nothing was compiled; without it nothing is claimed as verified. A real ORT session load rejects an unloadable model and accepts a loadable one, and the CLI flag reports verified:false when nothing compiled. The failure path is covered by these unit tests only: no real provider here produces an artifact ORT rejects, so I could not exercise a rejection end to end on a GPU.

Full suite in WSL Ubuntu (RTX 5070): dotnet test LinML.slnx -c Release passed with 0 failed and 0 skipped (Runtime.Onnx 79, Policy 25, Catalog 177, Integration 20, Cli 237 = 538), no build warnings. The fresh-cache TensorRT rows below were re-run after the review changes with the same result (--verify was added afterwards and is off by default); the poisoned-cache row was measured before the review changes, which do not touch that path. A real compile --provider trt-rtx --verify on mobilenetv2-7 published the artifact with verified:true, and the following run used it with no rejection.

TensorRT repro, mobilenetv2-7, fresh XDG_CACHE_HOME per run, before (origin/main) and after:

beforeafter
compile --provider trt --allow-partial --jsonsuccess:true, unsupported:true, 495-byte .compiled.onnx + sidecar, exit 0success:false, unsupported:true, artifactBytes:0, exit 1
ep-context/ after compile, cache list1 entryempty, count: 0
run --provider trt --require-providerexit 0, Compiled artifact ... rejected (... TRTKernel_graph_... is not a registered function/op); falling back to original modelexit 0, no rejection line
new binary on the cache the old build poisonedn/arun: no rejection (file ignored); compile --allow-partial: removes it ("Note: removed a stale artifact ..."), cache empty

Verification program (#73)

  • corpus.json, thresholds.json and zoo.py are untouched (hashes in freeze.json still match).
  • No gate currently flags a warm run whose artifact was rejected. artifactUse: rejected is recorded in the run-warm cell verdict but nothing in GateEvaluator reads it, so these cells passed. Adding such a rule changes zoo.py/thresholds.json and needs a maintainer re-freeze; I did not do it here.
  • Effect of this change on the frozen harness: interpret_compile previously mapped success && unsupported to outcome passthrough, which counts as "artifact produced", so run-warm ran. Now the compile-partial row is unsupported (same cell status as before), run-warm is recorded as not run ("no compile leg produced an artifact") for cpu/cuda/trt, and the passthrough outcome is unreachable with this LinML. No gate reads cacheEntriesAfter, and check_compile_truth only consults the strict leg, so I expect no gate to trip. I have not re-run the matrix. verification/model-zoo/README.md still describes the passthrough outcome; it is not hash-pinned and I left it for the maintainers.

Known limit

"Contains an EPContext node" stands in for "loads". By default an artifact mixing EPContext nodes with unregistered fused nodes would still be published; --verify closes that for anyone who wants the guarantee. No current provider produces one: built-in TensorRT writes no EPContext node at all (so it is never published), and a trt-rtx artifact for mobilenetv2-7 contains only EPContext and loads. I could not check openvino, qnn or migraphx on this host, nor a trt-rtx model it claims only in part. Whether the built-in TensorRT EP can emit EPContext through its own ep-context options is a separate question and not addressed here.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • linml compile --verify can check whether a compiled artifact loads before publishing it. Verification is off by default, and results indicate whether it passed.
  • Bug Fixes
    • linml compile --allow-partial reports unsupported and exits with failure when no nodes are compiled, without writing an artifact. Matrix runs fail only when no provider succeeds.
    • Unusable cached artifacts are ignored when creating a session and can be removed by a matching partial compile or linml cache clear. CPU and CUDA passthrough copies are no longer cached.
  • Documentation
    • Updated the README and compatibility guide with these behaviors.