-
Notifications
You must be signed in to change notification settings - Fork 89
Add utility function for converting model protos to function proto #2655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chapman73
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
chapman73:convert-model-to-funcproto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| import onnx | ||
| import onnx_ir | ||
| from onnx import helper | ||
|
|
||
|
|
||
| def _initializers_to_constants(model: onnx.ModelProto) -> onnx.ModelProto: | ||
| graph = model.graph | ||
| new_nodes = [] | ||
|
|
||
| # Keep track of names to remove from inputs | ||
| init_names = {init.name for init in graph.initializer} | ||
|
|
||
| for init in graph.initializer: | ||
| # Convert initializer to Constant node | ||
| const_node = helper.make_node( | ||
| "Constant", | ||
| inputs=[], | ||
| outputs=[init.name], | ||
| value=init, # Directly use TensorProto | ||
| ) | ||
| new_nodes.append(const_node) | ||
|
|
||
| # Filter out initializer names from graph inputs | ||
| filtered_inputs = [i for i in graph.input if i.name not in init_names] | ||
| graph.ClearField("input") | ||
| graph.input.extend(filtered_inputs) | ||
|
|
||
| # Add new Constant nodes at the beginning | ||
| all_nodes = new_nodes + list(graph.node) | ||
| graph.ClearField("node") | ||
| graph.node.extend(all_nodes) | ||
|
|
||
| # Clear initializers (since we replaced them) | ||
| graph.ClearField("initializer") | ||
|
|
||
| return model | ||
|
|
||
|
|
||
| def convert_model_proto_to_function_proto( | ||
| model: onnx.ModelProto, domain: str, name: str | ||
| ) -> onnx.FunctionProto: | ||
| """Converts an arbitrary ModelProto to a FunctionProto. | ||
|
|
||
| Since function protos don't support initializers (or rather it does not make sense in the context of a function) | ||
| we need to convert them to constants first. | ||
| """ | ||
| model = _initializers_to_constants(model) | ||
| model_ir = onnx_ir.serde.deserialize_model(model) | ||
| doc_string = model.doc_string if model.doc_string else "" | ||
| graph = model_ir.graph | ||
|
|
||
| function_ir = onnx_ir.Function( | ||
| domain=domain, name=name, graph=graph, attributes={} | ||
| ) | ||
|
|
||
| # set metadata | ||
| function_ir.doc_string(doc_string) | ||
| function_ir.metadata_props = graph.metadata_props # I believe the docs suggest directly modifying the attr? | ||
| # function_ir.value_infos(graph.value_infos) # theres no setter defined? | ||
| return onnx_ir.to_proto(function_ir) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
| import onnxruntime as ort | ||
|
|
||
| from onnxscript import script | ||
| from onnxscript.onnx_opset import opset15 as op | ||
| from onnxscript.onnx_types import FLOAT | ||
| from onnxscript.utils.model_proto_to_function_proto import ( | ||
| convert_model_proto_to_function_proto, | ||
| ) | ||
| from onnxscript.values import Opset | ||
|
|
||
|
|
||
| class TestModelProtoToFunctionProto(unittest.TestCase): | ||
| def setUp(self): | ||
| """Set up test fixtures.""" | ||
| # Create a fresh custom opset for each test | ||
| self.local = Opset("local", 1) | ||
|
|
||
| # Define test functions | ||
| @script(self.local, default_opset=op) | ||
| def diff_square(x, y): | ||
| diff = x - y | ||
| return diff * diff | ||
|
|
||
| @script(self.local) | ||
| def sum_func(z): | ||
| return op.ReduceSum(z, keepdims=1) | ||
|
|
||
| @script() | ||
| def l2norm_with_functions(x: FLOAT["N"], y: FLOAT["N"]) -> FLOAT[1]: # noqa: F821 | ||
| return op.Sqrt(sum_func(diff_square(x, y))) | ||
|
|
||
| self.diff_square = diff_square | ||
| self.sum_func = sum_func | ||
| self.l2norm_with_functions = l2norm_with_functions | ||
|
|
||
| def test_multiple_functions_in_model_proto(self): | ||
| """Test that multiple functions can be included in a single model proto.""" | ||
| # Add sum function to opset | ||
| sum_model = self.sum_func.to_model_proto() | ||
| sum_function_proto = convert_model_proto_to_function_proto( | ||
| sum_model, "local", "sum_func" | ||
| ) | ||
|
|
||
| model = self.l2norm_with_functions.to_model_proto( | ||
| functions=[sum_function_proto, self.diff_square] | ||
| ) | ||
|
|
||
| # Test execution | ||
| session = ort.InferenceSession(model.SerializeToString()) | ||
| result = session.run( | ||
| None, | ||
| { | ||
| "x": np.array([1.0, 2.0, 3.0]).astype(np.float32), | ||
| "y": np.array([4.0, 5.0, 6.0]).astype(np.float32), | ||
| }, | ||
| ) | ||
|
|
||
| # Verify result | ||
| self.assertEqual(len(result), 1) | ||
| self.assertAlmostEqual(np.sqrt(27.0), result[0][0], places=5) # L2 norm of [3, 3, 3] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / lintrunner
RUFF-FORMAT/format