Files
onnx--onnx/onnx/test/model_container_refeval_test.py
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

137 lines
4.8 KiB
Python

# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import os
import tempfile
import numpy as np
import numpy.testing as npt
import pytest
import onnx
import onnx.helper
import onnx.model_container
import onnx.numpy_helper
import onnx.reference
def _linear_regression():
X = onnx.helper.make_tensor_value_info("X", onnx.TensorProto.FLOAT, [None, None])
Y = onnx.helper.make_tensor_value_info("Y", onnx.TensorProto.FLOAT, [None])
graph = onnx.helper.make_graph(
[
onnx.helper.make_node("MatMul", ["X", "A"], ["XA"]),
onnx.helper.make_node("MatMul", ["XA", "B"], ["XB"]),
onnx.helper.make_node("MatMul", ["XB", "C"], ["Y"]),
],
"mm",
[X],
[Y],
[
onnx.numpy_helper.from_array(
np.arange(9).astype(np.float32).reshape((-1, 3)), name="A"
),
onnx.numpy_helper.from_array(
(np.arange(9) * 100).astype(np.float32).reshape((-1, 3)),
name="B",
),
onnx.numpy_helper.from_array(
(np.arange(9) + 10).astype(np.float32).reshape((-1, 3)),
name="C",
),
],
)
onnx_model = onnx.helper.make_model(graph)
onnx.checker.check_model(onnx_model)
return onnx_model
def _large_linear_regression():
X = onnx.helper.make_tensor_value_info("X", onnx.TensorProto.FLOAT, [None, None])
Y = onnx.helper.make_tensor_value_info("Y", onnx.TensorProto.FLOAT, [None])
graph = onnx.helper.make_graph(
[
onnx.helper.make_node("MatMul", ["X", "A"], ["XA"]),
onnx.helper.make_node("MatMul", ["XA", "B"], ["XB"]),
onnx.helper.make_node("MatMul", ["XB", "C"], ["Y"]),
],
"mm",
[X],
[Y],
[
onnx.model_container.make_large_tensor_proto(
"#loc0", "A", onnx.TensorProto.FLOAT, (3, 3)
),
onnx.numpy_helper.from_array(
np.arange(9).astype(np.float32).reshape((-1, 3)), name="B"
),
onnx.model_container.make_large_tensor_proto(
"#loc1", "C", onnx.TensorProto.FLOAT, (3, 3)
),
],
)
onnx_model = onnx.helper.make_model(graph)
large_model = onnx.model_container.make_large_model(
onnx_model.graph,
{
"#loc0": (np.arange(9) * 100).astype(np.float32).reshape((-1, 3)),
"#loc1": (np.arange(9) + 10).astype(np.float32).reshape((-1, 3)),
},
)
large_model.check_model()
return large_model
class TestLargeOnnxReferenceEvaluator:
def common_check_reference_evaluator(self, container):
X = np.arange(9).astype(np.float32).reshape((-1, 3))
ref = onnx.reference.ReferenceEvaluator(container)
got = ref.run(None, {"X": X})
expected = np.array(
[
[945000, 1015200, 1085400],
[2905200, 3121200, 3337200],
[4865400, 5227200, 5589000],
],
dtype=np.float32,
)
npt.assert_allclose(got[0], expected)
def test_large_onnx_no_large_initializer(self):
model_proto = _linear_regression()
large_model = onnx.model_container.make_large_model(model_proto.graph)
self.common_check_reference_evaluator(large_model)
with pytest.raises(ValueError):
large_model["#anymissingkey"]
with tempfile.TemporaryDirectory() as temp:
filename = os.path.join(temp, "model.onnx")
large_model.save(filename)
copy = onnx.model_container.ModelContainer()
copy.load(filename)
self.common_check_reference_evaluator(copy)
def test_large_one_weight_file(self):
large_model = _large_linear_regression()
self.common_check_reference_evaluator(large_model)
with tempfile.TemporaryDirectory() as temp:
filename = os.path.join(temp, "model.onnx")
large_model.save(filename, True)
copy = onnx.model_container.ModelContainer()
copy.load(filename)
loaded_model = onnx.load_model(filename, load_external_data=True)
self.common_check_reference_evaluator(loaded_model)
def test_large_multi_files(self):
large_model = _large_linear_regression()
self.common_check_reference_evaluator(large_model)
with tempfile.TemporaryDirectory() as temp:
filename = os.path.join(temp, "model.onnx")
large_model.save(filename, False)
copy = onnx.load_model(filename)
self.common_check_reference_evaluator(copy)
loaded_model = onnx.load_model(filename, load_external_data=True)
self.common_check_reference_evaluator(loaded_model)