Files
onnx--onnx/onnx/test/function_test.py
T
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:41:19 +08:00

222 lines
8.3 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# Copyright (c) ONNX Project Contributors
from __future__ import annotations
import onnx
from onnx import checker, utils
class TestFunction:
def _verify_function_set(self, extracted_model, function_set, func_domain):
checker.check_model(extracted_model)
assert len(extracted_model.functions) == len(function_set)
for function in function_set:
assert (
next(
(
f
for f in extracted_model.functions
if f.name == function and f.domain == func_domain
),
None,
)
is not None
)
def test_extract_model_with_local_function(self) -> None:
r"""# 1. build a model with graph below. extract models with output combinations
# 2. validate extracted models' local functions
#
# model graph:
# i0 i1 i2
# | __________________|__________________/_________
# | | | | / |
# | | | | / |
# func_add func_identity add identity
# | ___\___________\____________________|_________ |
# | | \ \ | _______|___|
# | | \ \ | | | |
# add function_nested_identity_add add function_nested_identity_add
# | | | |
# | | | |
# o_func_add o_all_func0 o_no_func o_all_func1
#
# where function_nested_identity_add is a function that is defined with functions:
# a b
# | |
# func_identity func_identity
# \ /
# func_add
# |
# c
#
"""
# function common
func_domain = "local"
func_opset_imports = [onnx.helper.make_opsetid("", 14)]
func_nested_opset_imports = [
onnx.helper.make_opsetid("", 14),
onnx.helper.make_opsetid(func_domain, 1),
]
# add function
func_add_name = "func_add"
func_add_inputs = ["a", "b"]
func_add_outputs = ["c"]
func_add_nodes = [onnx.helper.make_node("Add", ["a", "b"], ["c"])]
func_add = onnx.helper.make_function(
func_domain,
func_add_name,
func_add_inputs,
func_add_outputs,
func_add_nodes,
func_opset_imports,
)
# identity function
func_identity_name = "func_identity"
func_identity_inputs = ["a"]
func_identity_outputs = ["b"]
func_identity_nodes = [onnx.helper.make_node("Identity", ["a"], ["b"])]
func_identity = onnx.helper.make_function(
func_domain,
func_identity_name,
func_identity_inputs,
func_identity_outputs,
func_identity_nodes,
func_opset_imports,
)
# nested identity/add function
func_nested_identity_add_name = "func_nested_identity_add"
func_nested_identity_add_inputs = ["a", "b"]
func_nested_identity_add_outputs = ["c"]
func_nested_identity_add_nodes = [
onnx.helper.make_node("func_identity", ["a"], ["a1"], domain=func_domain),
onnx.helper.make_node("func_identity", ["b"], ["b1"], domain=func_domain),
onnx.helper.make_node("func_add", ["a1", "b1"], ["c"], domain=func_domain),
]
func_nested_identity_add = onnx.helper.make_function(
func_domain,
func_nested_identity_add_name,
func_nested_identity_add_inputs,
func_nested_identity_add_outputs,
func_nested_identity_add_nodes,
func_nested_opset_imports,
)
# create graph nodes
node_func_add = onnx.helper.make_node(
func_add_name, ["i0", "i1"], ["t0"], domain=func_domain
)
node_add0 = onnx.helper.make_node("Add", ["i1", "i2"], ["t2"])
node_add1 = onnx.helper.make_node("Add", ["t0", "t2"], ["o_func_add"])
node_func_identity = onnx.helper.make_node(
func_identity_name, ["i1"], ["t1"], domain=func_domain
)
node_identity = onnx.helper.make_node("Identity", ["i1"], ["t3"])
node_add2 = onnx.helper.make_node("Add", ["t3", "t2"], ["o_no_func"])
node_func_nested0 = onnx.helper.make_node(
func_nested_identity_add_name,
["t0", "t1"],
["o_all_func0"],
domain=func_domain,
)
node_func_nested1 = onnx.helper.make_node(
func_nested_identity_add_name,
["t3", "t2"],
["o_all_func1"],
domain=func_domain,
)
graph_name = "graph_with_imbedded_functions"
ir_version = 8
opset_imports = [
onnx.helper.make_opsetid("", 14),
onnx.helper.make_opsetid("local", 1),
]
tensor_type_proto = onnx.helper.make_tensor_type_proto(elem_type=2, shape=[5])
graph = onnx.helper.make_graph(
[
node_func_add,
node_add0,
node_add1,
node_func_identity,
node_identity,
node_func_nested0,
node_func_nested1,
node_add2,
],
graph_name,
[
onnx.helper.make_value_info(name="i0", type_proto=tensor_type_proto),
onnx.helper.make_value_info(name="i1", type_proto=tensor_type_proto),
onnx.helper.make_value_info(name="i2", type_proto=tensor_type_proto),
],
[
onnx.helper.make_value_info(
name="o_no_func", type_proto=tensor_type_proto
),
onnx.helper.make_value_info(
name="o_func_add", type_proto=tensor_type_proto
),
onnx.helper.make_value_info(
name="o_all_func0", type_proto=tensor_type_proto
),
onnx.helper.make_value_info(
name="o_all_func1", type_proto=tensor_type_proto
),
],
)
meta = {
"ir_version": ir_version,
"opset_imports": opset_imports,
"producer_name": "test_extract_model_with_local_function",
"functions": [func_identity, func_add, func_nested_identity_add],
}
model = onnx.helper.make_model(graph, **meta)
checker.check_model(model)
extracted_with_no_function = utils.Extractor(model).extract_model(
["i0", "i1", "i2"], ["o_no_func"]
)
self._verify_function_set(extracted_with_no_function, {}, func_domain)
extracted_with_add_function = utils.Extractor(model).extract_model(
["i0", "i1", "i2"], ["o_func_add"]
)
self._verify_function_set(
extracted_with_add_function, {func_add_name}, func_domain
)
extracted_with_o_all_function0 = utils.Extractor(model).extract_model(
["i0", "i1", "i2"], ["o_all_func0"]
)
self._verify_function_set(
extracted_with_o_all_function0,
{func_add_name, func_identity_name, func_nested_identity_add_name},
func_domain,
)
extracted_with_o_all_function1 = utils.Extractor(model).extract_model(
["i0", "i1", "i2"], ["o_all_func1"]
)
self._verify_function_set(
extracted_with_o_all_function1,
{func_add_name, func_identity_name, func_nested_identity_add_name},
func_domain,
)
extracted_with_o_all_function2 = utils.Extractor(model).extract_model(
["i0", "i1", "i2"],
["o_no_func", "o_func_add", "o_all_func0", "o_all_func1"],
)
self._verify_function_set(
extracted_with_o_all_function2,
{func_add_name, func_identity_name, func_nested_identity_add_name},
func_domain,
)