5cbd3f29e3
Fuzz / Run fuzz harnesses (${{ github.event_name == 'schedule' && 'nightly' || 'smoke' }}) (push) Has been cancelled
Create Releases / call-mac (push) Has been cancelled
Create Releases / call-linux (push) Has been cancelled
Create Releases / call-sdist (push) Has been cancelled
Create Releases / call-win (push) Has been cancelled
Create Releases / call-pyodide (push) Has been cancelled
Windows_No_Exception_CI / build (x64, 3.10) (push) Has been cancelled
Check URLs / build (push) Has been cancelled
Create Releases / Attest CI build artifacts (push) Has been cancelled
Create Releases / Check for Publish release build to pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Check for Publish release build to test.pypi (rc-candidates) (push) Has been cancelled
Create Releases / Publish release build to test.pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish release build to pypi (push) Has been cancelled
Create Releases / test source distribution (push) Has been cancelled
clang-tidy / clang-tidy (push) Has been cancelled
Lint / Validate SBOM (push) Has been cancelled
Lint / Enforce style (push) Has been cancelled
CI / Test windows-2022, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=1, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=1, onnx_ml=1, autogen=1 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=0, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
Pixi CI / Install and lint (ubuntu-24.04-arm) (push) Has been cancelled
Pixi CI / Install and lint (windows-2022) (push) Has been cancelled
Pixi CI / Xcode generator build (push) Has been cancelled
Pixi CI / Install and test (macos-latest, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, default) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, default) (push) Has been cancelled
Pixi CI / Install and test (macos-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, oldies) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
Generate and publish ONNX docs / build (push) Has been cancelled
Generate and publish ONNX docs / deploy (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
182 lines
5.8 KiB
Python
182 lines
5.8 KiB
Python
# Copyright (c) ONNX Project Contributors
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
"""onnx shape inference. Shape inference is not guaranteed to be
|
|
complete.
|
|
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import TYPE_CHECKING
|
|
|
|
import onnx
|
|
import onnx.onnx_cpp2py_export.shape_inference as C # noqa: N812
|
|
from onnx.onnx_pb import (
|
|
IR_VERSION,
|
|
AttributeProto,
|
|
FunctionProto,
|
|
ModelProto,
|
|
TypeProto,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Sequence
|
|
|
|
GraphInferencer = C.GraphInferencer
|
|
InferenceContext = C.InferenceContext
|
|
|
|
|
|
def infer_shapes(
|
|
model: ModelProto | bytes,
|
|
check_type: bool = False,
|
|
strict_mode: bool = False,
|
|
data_prop: bool = False,
|
|
) -> ModelProto:
|
|
"""Apply shape inference to the provided ModelProto.
|
|
|
|
Inferred shapes are added to the value_info field of the graph.
|
|
|
|
If the inferred values conflict with values already provided in the
|
|
graph, that means that the provided values are invalid (or there is a
|
|
bug in shape inference), and the result is unspecified.
|
|
|
|
Arguments:
|
|
model: ModelProto.
|
|
check_type: Checks the type-equality for input and output.
|
|
strict_mode: Stricter shape inference, it will throw errors if any;
|
|
Otherwise, simply stop if any error.
|
|
data_prop: Enables data propagation for limited operators to perform shape computation.
|
|
|
|
Returns:
|
|
(ModelProto) model with inferred shape information
|
|
"""
|
|
if isinstance(model, (ModelProto, bytes)):
|
|
model_str = model if isinstance(model, bytes) else model.SerializeToString()
|
|
inferred_model_str = C.infer_shapes(
|
|
model_str, check_type, strict_mode, data_prop
|
|
)
|
|
return onnx.load_from_string(inferred_model_str)
|
|
if isinstance(model, (str, os.PathLike)):
|
|
raise TypeError(
|
|
"infer_shapes only accepts ModelProto or bytes,"
|
|
" For Model paths (str or os.PathLike), use infer_shapes_path()."
|
|
)
|
|
|
|
raise TypeError(
|
|
f"infer_shapes only accepts ModelProto or bytes, incorrect type: {type(model)}"
|
|
)
|
|
|
|
|
|
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:
|
|
"""Take model path for shape_inference.
|
|
|
|
This function is the same as :func:`infer_shape` but supports >2GB models.
|
|
The function outputs the inferred model to the `output_path`. The original model path
|
|
is used if not specified.
|
|
"""
|
|
if isinstance(model_path, ModelProto):
|
|
raise TypeError(
|
|
"infer_shapes_path only accepts model Path (String),"
|
|
"you can use infer_shapes for the ModelProto."
|
|
)
|
|
try:
|
|
model_path = os.fspath(model_path)
|
|
except TypeError as exp:
|
|
raise TypeError(
|
|
"infer_shapes_path only accepts model path as a string or PathLike, "
|
|
f"incorrect model path type: {type(model_path)}"
|
|
) from exp
|
|
try:
|
|
output_path = os.fspath(output_path)
|
|
except TypeError as exp:
|
|
raise TypeError(
|
|
"infer_shapes_path only accepts output path as a string or PathLike, "
|
|
f"incorrect output path type: {type(output_path)}"
|
|
) from exp
|
|
|
|
if output_path == "":
|
|
output_path = model_path
|
|
C.infer_shapes_path(model_path, output_path, check_type, strict_mode, data_prop)
|
|
|
|
|
|
def infer_node_outputs(
|
|
schema: onnx.defs.OpSchema,
|
|
node: onnx.NodeProto,
|
|
input_types: dict[str, onnx.TypeProto],
|
|
input_data: dict[str, onnx.TensorProto] | None = None,
|
|
input_sparse_data: dict[str, onnx.SparseTensorProto] | None = None,
|
|
opset_imports: list[onnx.OperatorSetIdProto] | None = None,
|
|
ir_version: int = IR_VERSION,
|
|
) -> dict[str, onnx.TypeProto]:
|
|
if input_data is None:
|
|
input_data = {}
|
|
if input_sparse_data is None:
|
|
input_sparse_data = {}
|
|
if opset_imports is None:
|
|
passed_opset_imports = {}
|
|
else:
|
|
passed_opset_imports = {opset.domain: opset.version for opset in opset_imports}
|
|
|
|
# catch KeyError if node's input does not exist in input_types
|
|
passed_input_types = {
|
|
key: input_types[key].SerializeToString() for key in node.input if key != ""
|
|
}
|
|
# input_types will also be used as outer_scope_value_types so do not filter by node's input here
|
|
for key, value in input_types.items():
|
|
if key not in passed_input_types:
|
|
passed_input_types[key] = value.SerializeToString()
|
|
passed_input_data = {
|
|
key: input_data[key].SerializeToString()
|
|
for key in node.input
|
|
if key in input_data
|
|
}
|
|
passed_sparse_input_data = {
|
|
key: input_sparse_data[key].SerializeToString()
|
|
for key in node.input
|
|
if key in input_sparse_data
|
|
}
|
|
|
|
outputs = schema._infer_node_outputs(
|
|
node.SerializeToString(),
|
|
passed_input_types,
|
|
passed_input_data,
|
|
passed_sparse_input_data,
|
|
passed_opset_imports,
|
|
ir_version,
|
|
) # type: ignore[call-arg]
|
|
return {key: onnx.TypeProto.FromString(out) for key, out in outputs.items()}
|
|
|
|
|
|
def infer_function_output_types(
|
|
function: FunctionProto,
|
|
input_types: Sequence[TypeProto],
|
|
attributes: Sequence[AttributeProto],
|
|
) -> list[TypeProto]:
|
|
"""Apply type-and-shape-inference to given function body, with given input types
|
|
and given input attribute values.
|
|
"""
|
|
result = C.infer_function_output_types(
|
|
function.SerializeToString(),
|
|
[x.SerializeToString() for x in input_types],
|
|
[x.SerializeToString() for x in attributes],
|
|
)
|
|
|
|
def to_type_proto(x) -> TypeProto:
|
|
type_proto = onnx.TypeProto()
|
|
type_proto.ParseFromString(x)
|
|
return type_proto
|
|
|
|
return [to_type_proto(x) for x in result]
|
|
|
|
|
|
InferenceError = C.InferenceError
|