onnx-opset-compatibility-audit
Audit ONNX graph validity, IR/opset domain boundaries, CPU ONNX Runtime session creation, known-output numerical assertions, and custom-op limits offline.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: onnx-opset-compatibility-auditdescription: "Audit ONNX graph validity, IR/opset domain boundaries, CPU ONNX Runtime session creation, known-output numerical assertions, and custom-op limits offline."category: datarisk: safesource: selfsource_type: selfdate_added: "2026-12-01"author: Koushik-Katkamtags: [onnx, onnxruntime, opset, compatibility, ml-inference, verification]ONNX Opset Compatibility Audit
Overview
Verify ONNX model files offline using pinned onnx==1.23.2 and onnxruntime==1.31.0 across three strictly separated stages: (1) ONNX structural graph validity (onnx.checker.check_model), (2) ONNX Runtime InferenceSession instantiation for concrete ir_version and opset_import domains, and (3) known-output numerical inference assertions (np.allclose). Explicitly exposes where a model passes onnx.checker yet fails runtime loading (such as unsupported opsets or unregistered custom operators), and never infers GPU/TensorRT/production compatibility from a passing CPUExecutionProvider fixture.
Distinction from existing ML serving and deployment skills:
model-serving-kubernetes: deploys KServe and Triton InferenceServer manifests on Kubernetes clusters rather than auditing ONNX IR/opset/session/numerical boundaries offline.transformers-js: guides client-side JavaScript inference with@huggingface/transformersand ONNX WebAssembly paths rather than running Pythononnx.checker+onnxruntime.InferenceSessionopset compatibility checks.ml-engineer: covers general PyTorch/TensorFlow training pipelines and serving architecture without executable ONNX opset/IR compatibility tests.
When to Use
- Upgrading ONNX exporters (
torch.onnx.export,onnxscript, converters) or pinning a targetonnxruntimerelease. - Checking whether a model's
ir_versionand per-domainopset_importversions load and execute accurately on the pinned CPU ONNX Runtime. - Distinguishing protobuf parse errors (
INVALID_INPUT), structural ONNX graph errors, runtime opset/custom-op rejections, and wrong numerical outputs. - Do not use this skill to load untrusted third-party/downloaded models, register native custom-operator shared libraries, or deploy models to clusters.
Prerequisites
- Python
3.12+with pinned ONNX, ONNX Runtime, NumPy, and pytest packages (numpy==2.5.3requires>=3.12;onnxruntime==1.31.0requires>=3.11; tested with Python3.12.10,onnx==1.23.2,onnxruntime==1.31.0,numpy==2.5.3,pytest==9.1.1). - Synthetic or trusted in-memory
.onnxprotobuf models within bounded size (16 MiBmax; models referencingTensorProto.EXTERNALtensor files are rejected asUNSUPPORTED). - No GPU hardware, CUDA drivers, external model downloads, or network access required.
How It Works
1. Inspect IR version, opset imports, external data, and graph validity
- Parse bounded serialized bytes with
onnx.load_from_string(INVALID_INPUTon corrupt protobuf bytes). - Reject model-local functions and training sections as outside this harness's inference-only scope. Recursively scan supported graph tensor containers (
graph.initializer,graph.sparse_initializer, node attributest/tensors/sparse_tensor/sparse_tensors, and nested subgraphg/graphsattributes up to bounded depth16) and reject anyTensorProto.EXTERNAL/external_datareference (EXTERNAL_DATA_UNSUPPORTED->UNSUPPORTED, orSUBGRAPH_DEPTH_EXCEEDED->UNSUPPORTEDbeyond depth16) before invokingonnx.checkerorInferenceSession, so no external tensor data files are opened outside synthetic/in-memory payloads. - Extract
model.ir_versionand{imp.domain or "ai.onnx": imp.version for imp in model.opset_import}. - Run
onnx.checker.check_model(model)separately so structural graph errors (ValidationError) are recorded asONNX_CHECKER_FAILEDwithout conflating them with runtime opset limits.
2. Instantiate pinned ONNX Runtime session and separate inference/numerical status
Run the local verification suite in ONNX opset compatibility harness:
- Session creation (
CPUExecutionProvider): Attemptonnxruntime.InferenceSession(model_bytes, providers=["CPUExecutionProvider"]). Observe thatir_version=10, opset=99passesonnx.checkeryet failsInferenceSession, proving graph validity is not runtime compatibility. - Unsupported IR & custom operator domain checks: Reject
ir_version=99and custom domains (com.example.custom) that fail session creation unless registered; classify non-default execution provider demands separately. - Inference execution & dense-tensor numerical verification: Validate
sample_inputs/expected_outputsstructures, catchsession.run(...)failures as structuredINFERENCE_FAILEDerrors rather than uncaught exceptions, require exact key equality across all model output names (flagging both missing and unexpected extra keys), classify non-np.ndarrayruntime outputs (such as ONNXSequencelists orMapdicts) as structuredUNSUPPORTED(NON_NDARRAY_OUTPUT) before reading.shape, and explicitly reportnumerical_status="NOT_TESTED"when numerical assertions were not run.
Examples
Run the self-contained verification suite in ONNX opset compatibility harness:
User request: "Verify our exported Relu+Linear ONNX graph passes checker, loads in ONNX Runtime 1.31.0, and matches expected values."
Inputs: Synthetic in-memory ONNX ModelProto bytes with explicit ir_version and opset_import.
Expected result: Compatible fixture (ir_version=10, ai.onnx opset=21) passes all 3 stages on CPUExecutionProvider;
opset=99 passes checker but fails ORT session creation; ir_version=99 fails both; wrong bias fails numerical stage only.
Limitations
- Tests strictly on
CPUExecutionProvider(onnxruntime==1.31.0); a passing CPU fixture never provesCUDAExecutionProvider,TensorrtExecutionProvider,CoreMLExecutionProvider, quantized NPU, or production throughput compatibility. - Custom operators (
com.microsoft, custom domains) and external-weight models (> 2 GiBprotobuf split.onnx_data) are rejected or require explicit trusted custom-op libraries outside this offline audit. - Small synthetic input tensors do not establish full-domain numerical stability across edge shapes (
batch=0, dynamic sequence lengths, FP16 overflow).
Safety and Side Effects
- Strictly offline and in-memory over small synthetic ONNX protobuf graphs (
16 MiBlimit). - Never downloads untrusted external checkpoints, loads custom native
.dll/.sooperator extensions, or deploys endpoints.