Skip to content
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
28 changes: 28 additions & 0 deletions tests/test_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,34 @@ def test_transpose_pad11(self, input_shape, output_shape, pads, perm_input, perm
self.run_transpose_compare(["res"], {"X": np.random.randn(*input_shape).astype(np.float32)},
model_proto, remaining_transpose_num=0)

@parameterized.expand([
((1, 3, 4, 5), (2, 6, 4, 8), [1, 0, 1, 3, 0, 0, 2, 0], [0, 2, 3, 1], [0, 3, 1, 2]),
((1, 3, 4, 5, 6), (2, 5, 6, 8, 10), [1, 0, 1, 3, 1, 0, 2, 2, 1, 1], [0, 2, 3, 4, 1], [0, 4, 1, 2, 3]),
])
@check_opset_min_version(11, "pad")
def test_transpose_pad11_non_const_pads(self, input_shape, output_shape, pads, perm_input, perm_output):

pads_val = np.array(pads, dtype=np.int64)
node0 = helper.make_node("Transpose", ["X"], ["Y"], perm=perm_input, name="trans_1")
node1 = helper.make_node("Pad", ["Y", "Pads"], ["Z"], name="pad")
node2 = helper.make_node("Transpose", ["Z"], ["res"], perm=perm_output, name="trans_2")

graph = helper.make_graph(
[node0, node1, node2],
"transpose-pad-test",
[helper.make_tensor_value_info("X", TensorProto.FLOAT, input_shape),
helper.make_tensor_value_info("Pads", TensorProto.INT64, pads_val.shape)],
[helper.make_tensor_value_info("res", TensorProto.FLOAT, output_shape)],
)

model_proto = self.make_model(graph, producer_name="onnx-tests")
self.run_transpose_compare(["res"],
{
"X": np.random.randn(*input_shape).astype(np.float32),
"Pads": pads_val
},
model_proto, remaining_transpose_num=0)

@parameterized.expand([
((1, 3, 4, 5), (1, 3, 1, 1), [0, 2, 3, 1], [0, 3, 1, 2]),
((1, 3, 4, 5, 6), (1, 3, 1, 1, 1), [0, 2, 3, 4, 1], [0, 4, 1, 2, 3]),
Expand Down
16 changes: 15 additions & 1 deletion tf2onnx/optimizer/transpose_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,21 @@ def _pad_handler(self, trans, node):
input1.set_tensor_value(new_pads)
input1.data_format = "NCHW"
return self._switch_transpose_and_node(node, trans)
return False
# when the second input is not a constant, let's shuffle it with Split followed by Concat
# there are examples of models, where this non-constant input
# gets constant folded anyway by a framework.
split = self._g.make_node("Split", inputs=[node.input[1]], attr={}, output_count=trans_rank * 2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice. When the pad is non-const, it is often constructed by concating some tensors together. This optimization might introduce a concat -> split -> concat sequence that can be further optimized. Still, it is worth it for removing a transpose and the concat -> split -> concat will be operating on very small tensors. We might want to add a new optimizer for it in the future though.

pads = split.output
if trans_rank == 4:
new_pads = self._g.make_node("Concat", [pads[0], pads[3], pads[1], pads[2],
pads[4], pads[7], pads[5], pads[6]],
{'axis': 0})
else:
new_pads = self._g.make_node("Concat", [pads[0], pads[4], pads[1], pads[2], pads[3],
pads[5], pads[9], pads[6], pads[7], pads[8]],
{'axis': 0})
self._g.replace_input(node, node.input[1], new_pads.output[0], 1)
return self._switch_transpose_and_node(node, trans)

def _reducemean_handler(self, trans, node):
axes = node.get_attr("axes").ints
Expand Down