Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Onnx Onnx Check Model Path

From Leeroopedia


Knowledge Sources
Domains External_Data, Validation
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for path-based validation and shape inference of large ONNX models with external data, wrapping C++ implementations.

Description

For models exceeding 2GB, checker.check_model accepts a file path (str or PathLike) instead of a ModelProto. When a path is provided, it dispatches to the C++ check_model_path function which reads the model directly from disk. Similarly, shape_inference.infer_shapes_path performs shape inference on disk-based models without loading them entirely into memory.

Both functions are Python wrappers around C++ implementations exposed via nanobind (onnx_cpp2py_export.checker.check_model_path and onnx_cpp2py_export.shape_inference.infer_shapes_path).

Usage

Use path-based check_model (by passing a string path instead of a ModelProto) and infer_shapes_path when verifying large models with external data. These functions are required when the total model data exceeds 2GB.

Code Reference

Source Location

  • Repository: onnx
  • File: onnx/checker.py:121-169 (check_model, path branch at L147-153), onnx/shape_inference.py:73-108 (infer_shapes_path)

Signature

# Path-based check_model (same function, different input type)
def check_model(
    model: str | os.PathLike,  # pass path for >2GB models
    full_check: bool = False,
    skip_opset_compatibility_check: bool = False,
    check_custom_domain: bool = False,
) -> None:
    """When model is a path, uses C++ check_model_path for >2GB support."""

def infer_shapes_path(
    model_path: str | os.PathLike,
    output_path: str | os.PathLike = "",
    check_type: bool = False,
    strict_mode: bool = False,
    data_prop: bool = False,
) -> None:
    """Path-based shape inference for >2GB models.

    Args:
        model_path: Input model file path.
        output_path: Output path (default: overwrite input).
        check_type: Check type equality.
        strict_mode: Throw errors on failure.
        data_prop: Enable data propagation.
    """

Import

from onnx import checker, shape_inference

I/O Contract

Inputs

Name Type Required Description
model (check_model) str or PathLike Yes File path to the ONNX model
model_path (infer_shapes_path) str or PathLike Yes File path to the ONNX model
output_path str or PathLike No Output path for infer_shapes_path (default: overwrite)
full_check bool No Also run shape inference (default: False)

Outputs

Name Type Description
check_model return None Raises ValidationError on failure
infer_shapes_path None Writes shape-inferred model to output_path

Usage Examples

Verify Large Model with External Data

from onnx import checker, shape_inference

# Validate the model structure and external data references
checker.check_model("large_model.onnx", full_check=True)

# Run shape inference and save result
shape_inference.infer_shapes_path(
    "large_model.onnx",
    output_path="large_model_inferred.onnx",
)

Complete External Data Verification Pipeline

import onnx
from onnx import checker, shape_inference

# Step 1: Save model with external data
onnx.save_model(model, "output/model.onnx", save_as_external_data=True,
                location="weights.bin")

# Step 2: Verify the saved model
checker.check_model("output/model.onnx")

# Step 3: Run shape inference on the saved model
shape_inference.infer_shapes_path(
    "output/model.onnx",
    output_path="output/model_inferred.onnx",
)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment