compile --allow-partial: do not cache an artifact when nothing was compiled
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 anEPContextoperator is deleted and not moved. The outcome isSuccess=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-partialhas always reportedunsupported:trueand still does; I kept that so the frozen harness classifies those cells as before. A pre-existing file atoutputPathis never touched (as before). This applies to an explicit--outputtoo, since a TensorRT artifact there is just as unloadable.- The
EPContextcheck also looks inside control-flow subgraphs (If/Loopbodies, depth-capped at 32), because ORT partitions those too and a real artifact must not be classified as a passthrough.OnnxProtobufReader.Parsestill reports main-graph nodes by default, soinspectoutput is unchanged. CompileCommand: detail text is nowno nodes compiled; no artifact written(the old text claimed a passthrough artifact).--allow-partialhelp 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:
OnnxSessionFactoryignores a cached artifact that parses as ONNX and has noEPContextnode (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.- 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-EPContextpassthrough. 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> --yesstill 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
| before | after | |
|---|---|---|
compile --allow-partial, trt/cpu/cuda (and any provider that compiles zero nodes) | exit 0, success:true, unsupported:true, file cached | exit 1, success:false, unsupported:true, artifactBytes:0, nothing cached |
matrix mode (no --provider) with --allow-partial | exit 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 --output | unchanged | unchanged |
compile that really produces EPContext (trt-rtx etc.) | unchanged | unchanged |
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 atoutputPathbyte-identical. AnEPContextnode inside a control-flow subgraph is found, other subgraph operators are not, 5000-deep nesting does not overflow, andParsestill reports main-graph nodes only by default. Runs on CPU, no GPU needed.CompileCommandTests:--allow-partialon cpu exits 1, reportssuccess:false/unsupported:true/artifactBytes:0, writes no file.CompileStaleArtifactTests(WslProbe collection, as it redirectsXDG_CACHE_HOME): a pre-fix passthrough artifact and sidecar at the cache key are removed by an--allow-partialcompile and a neighbouring entry survives; a strict compile and a compile with--outputleave the cached artifact in place.OnnxSessionFactoryArtifactTests: a no-EPContextartifact is ignored but not deleted, one withEPContextis 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 reportsverified:falsewhen 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:
| before | after | |
|---|---|---|
compile --provider trt --allow-partial --json | success:true, unsupported:true, 495-byte .compiled.onnx + sidecar, exit 0 | success:false, unsupported:true, artifactBytes:0, exit 1 |
ep-context/ after compile, cache list | 1 entry | empty, count: 0 |
run --provider trt --require-provider | exit 0, Compiled artifact ... rejected (... TRTKernel_graph_... is not a registered function/op); falling back to original model | exit 0, no rejection line |
| new binary on the cache the old build poisoned | n/a | run: no rejection (file ignored); compile --allow-partial: removes it ("Note: removed a stale artifact ..."), cache empty |
Verification program (#73)
corpus.json,thresholds.jsonandzoo.pyare untouched (hashes infreeze.jsonstill match).- No gate currently flags a warm run whose artifact was rejected.
artifactUse: rejectedis recorded in therun-warmcell verdict but nothing inGateEvaluatorreads it, so these cells passed. Adding such a rule changeszoo.py/thresholds.jsonand needs a maintainer re-freeze; I did not do it here. - Effect of this change on the frozen harness:
interpret_compilepreviously mappedsuccess && unsupportedto outcomepassthrough, which counts as "artifact produced", sorun-warmran. Now the compile-partial row isunsupported(same cell status as before),run-warmis recorded as not run ("no compile leg produced an artifact") for cpu/cuda/trt, and thepassthroughoutcome is unreachable with this LinML. No gate readscacheEntriesAfter, andcheck_compile_truthonly consults the strict leg, so I expect no gate to trip. I have not re-run the matrix.verification/model-zoo/README.mdstill describes thepassthroughoutcome; 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 --verifycan 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-partialreports 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.