Skip to content
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

Enhance Transpose optimizer "Mul" handler #978

Merged
merged 2 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def test_trans_can_be_replaced_with_reshape2(self):
model_proto, remaining_transpose_num=0)

def test_two_transposes_switch_with_mul(self):
const_node = self._make_onnx_const(np.array(10, dtype=np.float32), "const_10")
const_node = self._make_onnx_const(np.array(np.random.random(6), dtype=np.float32), "const_10")
node0 = helper.make_node("Transpose", ["u1"], ["v1"], perm=[0, 2, 3, 1], name="trans_0")
node1 = helper.make_node("Transpose", ["u2"], ["v2"], perm=[0, 2, 3, 1], name="trans_1")

Expand Down
17 changes: 15 additions & 2 deletions tf2onnx/optimizer/transpose_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,21 @@ def _mul_handler(self, trans, node):
self._g.remove_node(node.name)
return True

# if the shape is () or (1), we just move transpose after the mul
if not multiplier.shape or (len(multiplier.shape) == 1 and multiplier.shape[0] == 1):
# if the shape is (), we just move transpose after the mul
if not multiplier.shape:
return self._switch_transpose_and_node(node, trans)

# if multiplier is 1-D
if len(multiplier.shape) == 1:
if multiplier.shape[0] == 1:
Copy link
Collaborator

@RandySheriffH RandySheriffH Jun 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for "if..." at line 491 be merged to 486 like:
"if not multiplier.shape or multipier.shape == [1]"
Plus shall we have any UT for the enhancement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated unit test to see if 1D const tensors can be jumped over.

For the 'if' merging, it seems cleaner to separate handling of scalars and 1D tensors into 2 blocks (mostly for readability)

# shape is (1)
return self._switch_transpose_and_node(node, trans)

# shape is (N). reshape so that trans(shape) = 1,1,...,N
perm = list(trans.get_attr('perm').ints)
new_shape = np.ones(len(perm), dtype=np.int32)
new_shape[perm[-1]] = multiplier.shape[0]
multiplier_input_node.set_tensor_value(multiplier.reshape(new_shape))
return self._switch_transpose_and_node(node, trans)

return False
Expand Down