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
96 lines
3.5 KiB
Python
96 lines
3.5 KiB
Python
# Copyright (c) ONNX Project Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
|
|
import onnx
|
|
from onnx import TensorProto, helper, numpy_helper, shape_inference
|
|
|
|
|
|
class TestTrainingTool:
|
|
def test_training_info_proto(self) -> None:
|
|
# Inference graph.
|
|
A_shape = [2, 2]
|
|
A_name = "A"
|
|
A = np.random.rand(*A_shape).astype(np.float32)
|
|
A_initializer = numpy_helper.from_array(A, name=A_name)
|
|
A_value_info = helper.make_tensor_value_info(A_name, TensorProto.FLOAT, A_shape)
|
|
|
|
B_shape = [2, 2]
|
|
B_name = "B"
|
|
B = np.random.rand(*B_shape).astype(np.float32)
|
|
B_initializer = numpy_helper.from_array(B, name=B_name)
|
|
B_value_info = helper.make_tensor_value_info(B_name, TensorProto.FLOAT, B_shape)
|
|
|
|
C_shape = [2, 2]
|
|
C_name = "C"
|
|
C_value_info = helper.make_tensor_value_info(C_name, TensorProto.FLOAT, C_shape)
|
|
|
|
inference_node = helper.make_node(
|
|
"MatMul", inputs=[A_name, B_name], outputs=[C_name]
|
|
)
|
|
|
|
inference_graph = helper.make_graph(
|
|
[inference_node],
|
|
"simple_inference",
|
|
[A_value_info, B_value_info],
|
|
[C_value_info],
|
|
[A_initializer, B_initializer],
|
|
)
|
|
|
|
# Training graph
|
|
X_shape = [2, 2]
|
|
X_name = "X"
|
|
X = np.random.rand(*X_shape).astype(np.float32)
|
|
X_initializer = numpy_helper.from_array(X, name=X_name)
|
|
X_value_info = helper.make_tensor_value_info(X_name, TensorProto.FLOAT, X_shape)
|
|
|
|
Y_shape = [2, 2]
|
|
Y_name = "Y"
|
|
Y_value_info = helper.make_tensor_value_info(Y_name, TensorProto.FLOAT, Y_shape)
|
|
|
|
node = helper.make_node(
|
|
"MatMul",
|
|
inputs=[X_name, C_name], # tensor "C" is from inference graph.
|
|
outputs=[Y_name],
|
|
)
|
|
|
|
training_graph = helper.make_graph(
|
|
[node], "simple_training", [X_value_info], [Y_value_info], [X_initializer]
|
|
)
|
|
|
|
# Capture assignment of B <--- Y.
|
|
training_info = helper.make_training_info(
|
|
training_graph, [(B_name, Y_name)], None, None
|
|
)
|
|
|
|
# Create a model with both inference and training information.
|
|
model = helper.make_model(inference_graph)
|
|
# Check if the inference-only part is correct.
|
|
onnx.checker.check_model(model)
|
|
# Insert training information.
|
|
new_training_info = model.training_info.add()
|
|
new_training_info.CopyFrom(training_info)
|
|
|
|
# Generate the actual training graph from training information so that
|
|
# we can run onnx checker to check if the full training graph is a valid
|
|
# graph. As defined in spec, full training graph forms by concatenating
|
|
# corresponding fields.
|
|
full_training_graph = helper.make_graph(
|
|
list(model.graph.node) + list(model.training_info[0].algorithm.node),
|
|
"full_training_graph",
|
|
list(model.graph.input) + list(model.training_info[0].algorithm.input),
|
|
list(model.graph.output) + list(model.training_info[0].algorithm.output),
|
|
list(model.graph.initializer)
|
|
+ list(model.training_info[0].algorithm.initializer),
|
|
)
|
|
|
|
# Wrap full training graph as a ModelProto so that we can run checker.
|
|
full_training_model = helper.make_model(full_training_graph)
|
|
full_training_model_with_shapes = shape_inference.infer_shapes(
|
|
full_training_model
|
|
)
|
|
onnx.checker.check_model(full_training_model_with_shapes)
|