Skip to content
Snippets Groups Projects
Commit 903472bf authored by Tobi-Alonso's avatar Tobi-Alonso
Browse files

[TEST] Add test to verify move scalar add/mul past matmul only operates on linear segments

parent 6463cacb
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import numpy as np
import pytest
import onnx.helper as oh
from onnx import TensorProto
......@@ -99,3 +100,56 @@ def test_move_scalar_add_past_matmul():
assert new_model.graph.node[0].op_type == "MatMul"
assert new_model.graph.node[1].op_type == "Add"
assert new_model.graph.node[0].output[0] == new_model.graph.node[1].input[0]
@pytest.mark.parametrize(
"test_args",
[("Add", MoveScalarAddPastMatMul()), ("Mul", MoveScalarMulPastMatMul())],
)
def test_move_scalar_past_matmul_only_if_linear(test_args):
scalar_op = test_args[0]
transf_fxn = test_args[1]
input_shape = [1, 2]
matmul_shape = [2, 2]
top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, input_shape)
top_out = oh.make_tensor_value_info("top_out", TensorProto.FLOAT, input_shape)
p1 = oh.make_tensor_value_info("p1", TensorProto.FLOAT, [1, 1])
p2 = oh.make_tensor_value_info("p2", TensorProto.FLOAT, matmul_shape)
p3 = oh.make_tensor_value_info("p3", TensorProto.FLOAT, matmul_shape)
p4 = oh.make_tensor_value_info("p4", TensorProto.FLOAT, matmul_shape)
modelproto = oh.make_model(
oh.make_graph(
name="test",
inputs=[top_in],
outputs=[top_out],
value_info=[p1, p2, p3, p4],
nodes=[
oh.make_node(scalar_op, ["top_in", "p1"], ["t1"]),
oh.make_node("MatMul", ["t1", "p2"], ["fork"]),
oh.make_node("MatMul", ["fork", "p3"], ["t3"]),
oh.make_node(scalar_op, ["t3", "fork"], ["t4"]),
oh.make_node("MatMul", ["t4", "p4"], ["top_out"]),
],
)
)
model = ModelWrapper(modelproto)
model = model.transform(InferShapes())
np.random.seed(0)
model.set_initializer("p1", np.random.rand(1, 1).astype(np.float32))
model.set_initializer("p2", np.random.rand(*matmul_shape).astype(np.float32))
model.set_initializer("p3", np.random.rand(*matmul_shape).astype(np.float32))
model.set_initializer("p4", np.random.rand(*matmul_shape).astype(np.float32))
# Transform
new_model = model.transform(transf_fxn)
# Test
inp_dict = {"top_in": np.random.rand(*input_shape).astype(np.float32)}
assert ox.compare_execution(model, new_model, inp_dict)
assert new_model.graph.node[0].op_type == "MatMul"
assert new_model.graph.node[1].op_type == scalar_op
assert new_model.graph.node[2].op_type == "MatMul"
assert new_model.graph.node[3].op_type == scalar_op
assert new_model.graph.node[4].op_type == "MatMul"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment