- Notifications
You must be signed in to change notification settings - Fork0
Add command line to replace constants in a model#87
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
77863a6
example
xadupreace236c
Merge branch 'main' of https://github.com/sdpython/onnx-array-api
xadupre32fc52e
Merge branch 'main' of https://github.com/sdpython/onnx-array-api
xadupre79f0f16
Add command line to replace constant
xadupre9b64d5f
doc
xadupreafdc879
ut
xadupre8cfddd3
doc
xadupreFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsCHANGELOGS.rst
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
5 changes: 5 additions & 0 deletions_doc/api/tools.rst
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
160 changes: 160 additions & 0 deletions_unittests/ut_tools/test_replace_constants.py
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,160 @@ | ||
import unittest | ||
import numpy as np | ||
import onnx | ||
import onnx.helper as oh | ||
import onnx.numpy_helper as onh | ||
from onnx import TensorProto | ||
from onnx_array_api.ext_test_case import ExtTestCase | ||
from onnx_array_api.reference import ( | ||
ExtendedReferenceEvaluator as ReferenceEvaluator, | ||
) | ||
from onnx_array_api.tools.replace_constants import ( | ||
replace_initializer_by_constant_of_shape, | ||
) | ||
class TestReplaceConstants(ExtTestCase): | ||
def test_replace_initializer(self): | ||
dtype = np.float32 | ||
value = np.random.randn(2, 100).astype(dtype) | ||
A = onh.from_array(value, name="A") | ||
value = np.array([1], dtype=dtype) | ||
C = onh.from_array(value, name="C") | ||
X = oh.make_tensor_value_info("X", TensorProto.FLOAT, [None, None]) | ||
Y = oh.make_tensor_value_info("Y", TensorProto.FLOAT, [None]) | ||
node1 = oh.make_node("MatMul", ["X", "A"], ["AX"]) | ||
node2 = oh.make_node("Sub", ["AX", "C"], ["Y"]) | ||
graph = oh.make_graph([node1, node2], "lr", [X], [Y], [A, C]) | ||
model_def = oh.make_model(graph) | ||
x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2)) | ||
oinf1 = ReferenceEvaluator(model_def) | ||
y1 = oinf1.run(None, {"X": x})[0] # type: ignore[index] | ||
repl = replace_initializer_by_constant_of_shape(model_def) | ||
node_types = {n.op_type for n in repl.graph.node} | ||
self.assertIn("ConstantOfShape", node_types) | ||
oinf2 = ReferenceEvaluator(repl) | ||
y1[:, :] = 3.5 | ||
y1[0, :] = 0.5 | ||
y2 = oinf2.run(None, {"X": x})[0] # type: ignore[index] | ||
self.assertEqualArray(y1, y2) | ||
def test_replace_constant(self): | ||
dtype = np.float32 | ||
value = np.random.randn(2, 10).astype(dtype) | ||
A = onh.from_array(value, name="A") | ||
value = np.array([1], dtype=dtype) | ||
C = onh.from_array(value, name="C") | ||
X = oh.make_tensor_value_info("X", TensorProto.FLOAT, [None, None]) | ||
Y = oh.make_tensor_value_info("Y", TensorProto.FLOAT, [None]) | ||
node0 = oh.make_node("Constant", [], ["A"], value=A) | ||
node1 = oh.make_node("MatMul", ["X", "A"], ["AX"]) | ||
node2 = oh.make_node("Sub", ["AX", "C"], ["Y"]) | ||
graph = oh.make_graph([node0, node1, node2], "lr", [X], [Y], [C]) | ||
model_def = oh.make_model(graph) | ||
x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2)) | ||
oinf1 = ReferenceEvaluator(model_def) | ||
y1 = oinf1.run(None, {"X": x})[0] # type: ignore[index] | ||
repl = replace_initializer_by_constant_of_shape(model_def, threshold=0) | ||
node_types = {n.op_type for n in repl.graph.node} | ||
self.assertIn("ConstantOfShape", node_types) | ||
oinf2 = ReferenceEvaluator(repl) | ||
y1[:, :] = 4 | ||
y1[0, :] = 1 | ||
y2 = oinf2.run(None, {"X": x})[0] # type: ignore[index] | ||
self.assertEqualArray(y1, y2) | ||
def test_replace_constant_function(self): | ||
dtype = np.float32 | ||
value = np.random.randn(2, 100).astype(dtype) | ||
A = onh.from_array(value, name="A") | ||
value = np.array([1], dtype=dtype) | ||
C = onh.from_array(value, name="C") | ||
X = oh.make_tensor_value_info("X", TensorProto.FLOAT, [None, None]) | ||
Y = oh.make_tensor_value_info("Y", TensorProto.FLOAT, [None]) | ||
nodeC = oh.make_node("Constant", [], ["C"], value=C) | ||
node0 = oh.make_node("Constant", [], ["A"], value=A) | ||
node1 = oh.make_node("MatMul", ["X", "A"], ["AX"]) | ||
node2 = oh.make_node("Sub", ["AX", "C"], ["Y"]) | ||
opset_imports = [ | ||
oh.make_opsetid("", onnx.defs.onnx_opset_version()), | ||
oh.make_opsetid("custom", 1), | ||
] | ||
fct = oh.make_function( | ||
"custom", | ||
"unittest", | ||
["X"], | ||
["Y"], | ||
[nodeC, node0, node1, node2], | ||
opset_imports, | ||
) | ||
node = oh.make_node("unittest", ["X"], ["Y"], domain="custom") | ||
graph = oh.make_graph([node], "lr", [X], [Y], [C]) | ||
model_def = oh.make_model(graph, functions=[fct], opset_imports=opset_imports) | ||
x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2)) | ||
oinf1 = ReferenceEvaluator(model_def) | ||
y1 = oinf1.run(None, {"X": x})[0] # type: ignore[index] | ||
repl = replace_initializer_by_constant_of_shape(model_def) | ||
node_types = {n.op_type for n in repl.functions[0].node} | ||
self.assertIn("ConstantOfShape", node_types) | ||
oinf2 = ReferenceEvaluator(repl) | ||
y1[:, :] = 3.5 | ||
y1[0, :] = 0.5 | ||
y2 = oinf2.run(None, {"X": x})[0] # type: ignore[index] | ||
self.assertEqualArray(y1, y2) | ||
def test_replace_constant_graph(self): | ||
value = np.array([0], dtype=np.float32) | ||
zero = onh.from_array(value, name="zero") | ||
X = oh.make_tensor_value_info("X", onnx.TensorProto.FLOAT, [None, None]) | ||
Y = oh.make_tensor_value_info("Y", onnx.TensorProto.FLOAT, [None]) | ||
rsum = oh.make_node("ReduceSum", ["X"], ["rsum"]) | ||
cond = oh.make_node("Greater", ["rsum", "zero"], ["cond"]) | ||
then_out = oh.make_tensor_value_info("then_out", onnx.TensorProto.FLOAT, None) | ||
then_cst = onh.from_array(np.array([1] * 129).astype(np.float32)) | ||
then_const_node = oh.make_node( | ||
"Constant", inputs=[], outputs=["then_out"], value=then_cst, name="cst1" | ||
) | ||
then_body = oh.make_graph([then_const_node], "then_body", [], [then_out]) | ||
else_out = oh.make_tensor_value_info("else_out", onnx.TensorProto.FLOAT, None) | ||
else_cst = onh.from_array(np.array([-1] * 129).astype(np.float32)) | ||
else_const_node = oh.make_node( | ||
"Constant", inputs=[], outputs=["else_out"], value=else_cst, name="cst2" | ||
) | ||
else_body = oh.make_graph([else_const_node], "else_body", [], [else_out]) | ||
if_node = oh.make_node( | ||
"If", ["cond"], ["Y"], then_branch=then_body, else_branch=else_body | ||
) | ||
graph = oh.make_graph([rsum, cond, if_node], "if", [X], [Y], [zero]) | ||
onnx_model = oh.make_model( | ||
graph, opset_imports=[oh.make_opsetid("", onnx.defs.onnx_opset_version())] | ||
) | ||
self.assertNotIn("ConstantOfShape", str(onnx_model)) | ||
x = np.ones((3, 2), dtype=np.float32) | ||
oinf1 = ReferenceEvaluator(onnx_model) | ||
y1 = oinf1.run(None, {"X": x})[0] # type: ignore[index] | ||
repl = replace_initializer_by_constant_of_shape(onnx_model) | ||
self.assertIn("ConstantOfShape", str(repl)) | ||
oinf2 = ReferenceEvaluator(repl) | ||
y2 = oinf2.run(None, {"X": x})[0] # type: ignore[index] | ||
y1 = y1.copy() | ||
y1[:] = 0.5 | ||
self.assertEqualArray(y1, y2) | ||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) |
8 changes: 8 additions & 0 deletions_unittests/ut_xrun_doc/test_command_lines1.py
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
80 changes: 76 additions & 4 deletionsonnx_array_api/_command_lines_parser.py
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
5 changes: 2 additions & 3 deletionsonnx_array_api/array_api/_onnx_common.py
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
3 changes: 2 additions & 1 deletiononnx_array_api/npx/npx_functions.py
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
1 change: 1 addition & 0 deletionsonnx_array_api/tools/__init__.py
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 @@ | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.