Skip to content

Commit

Permalink
[Frontend][Tensorflow] Fix TF 1.15 conv2d_transpose parsing (apache#6589
Browse files Browse the repository at this point in the history
)

* Fix conv2d_transpose parsing in Tensorflow frontend for TF 1.15

* Add comments and convolution tests without AddShapesToGraphDef
  • Loading branch information
jtuyls authored and trevor-m committed Oct 19, 2020
1 parent e5e5079 commit c7fef34
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
14 changes: 12 additions & 2 deletions python/tvm/relay/frontend/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,12 @@ def _impl(inputs, attr, params, mod):
)
attr["data_format"] = "NCHW"

if opname == "conv_transpose" and len(attr["_output_shapes"]) > 0:
# Check whether output shapes attribute is set and not None
if (
opname == "conv_transpose"
and len(attr["_output_shapes"]) > 0
and attr["_output_shapes"][0]
):
tmp_shape = attr["_output_shapes"][0]
tmp_shape = [tmp_shape[ii] for ii in (0, 3, 1, 2)]
attr["_output_shapes"][0] = tmp_shape
Expand Down Expand Up @@ -386,7 +391,12 @@ def _impl(inputs, attr, params, mod):
kernel_h, kernel_w = attr["kernel_shape"]

pdata_shape = input_shape
if opname == "conv_transpose" and len(attr["_output_shapes"]) > 0:
# Check whether output shapes attribute is set and not None
if (
opname == "conv_transpose"
and len(attr["_output_shapes"]) > 0
and attr["_output_shapes"][0]
):
pdata_shape = attr["_output_shapes"][0]

if attr["data_format"] == "NHWC":
Expand Down
84 changes: 81 additions & 3 deletions tests/python/frontend/tensorflow/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def compare_tf_with_tvm(
opt_level=3,
mode="graph_runtime",
cuda_layout="NCHW",
add_shapes_to_graph_def=True,
):
"""Generic function to generate and compare tensorflow and TVM output"""

Expand All @@ -221,7 +222,11 @@ def name_without_num(name):
with tf.Session() as sess:
if init_global_variables:
sess.run(variables.global_variables_initializer())
final_graph_def = tf_testing.AddShapesToGraphDef(sess, out_node)
final_graph_def = (
tf_testing.AddShapesToGraphDef(sess, out_node)
if add_shapes_to_graph_def
else tf.get_default_graph().as_graph_def()
)

tf_output = run_tf_graph(sess, in_data, in_name, out_name)

Expand Down Expand Up @@ -422,6 +427,7 @@ def _test_convolution(
padding,
data_format,
deconv_output_shape=[],
add_shapes_to_graph_def=True,
):
""" One iteration of convolution with given shapes and attributes """

Expand Down Expand Up @@ -456,6 +462,7 @@ def _test_convolution(
np.reshape(data_array, tensor_in_sizes).astype("float32"),
"Placeholder:0",
"Conv2D:0",
add_shapes_to_graph_def=add_shapes_to_graph_def,
)
elif opname == "conv_transpose":
nn_ops.conv2d_transpose(
Expand All @@ -471,6 +478,7 @@ def _test_convolution(
np.reshape(data_array, tensor_in_sizes).astype("float32"),
"Placeholder:0",
"conv2d_transpose:0",
add_shapes_to_graph_def=add_shapes_to_graph_def,
)
else:
nn_ops.depthwise_conv2d_native(
Expand All @@ -486,6 +494,7 @@ def _test_convolution(
np.reshape(data_array, tensor_in_sizes).astype("float32"),
"Placeholder:0",
"DepthwiseConv2dNative:0",
add_shapes_to_graph_def=add_shapes_to_graph_def,
)


Expand Down Expand Up @@ -648,11 +657,32 @@ def test_forward_convolution():
_test_convolution("conv", [4, 17, 17, 19], [3, 3, 19, 19], [1, 1], [2, 2], "VALID", "NHWC")
_test_convolution("conv", [4, 17, 17, 124], [1, 1, 124, 19], [1, 1], [1, 1], "SAME", "NHWC")
_test_convolution("conv", [4, 17, 17, 12], [3, 3, 12, 32], [1, 1], [2, 2], "VALID", "NHWC")
_test_convolution(
"conv",
[4, 17, 17, 12],
[3, 3, 12, 32],
[1, 1],
[2, 2],
"VALID",
"NHWC",
add_shapes_to_graph_def=False,
)
_test_convolution("depthwise", [4, 8, 8, 176], [1, 1, 176, 1], [1, 1], [1, 1], "SAME", "NHWC")
_test_convolution("depthwise", [4, 17, 17, 19], [3, 3, 19, 1], [1, 1], [2, 2], "VALID", "NHWC")
_test_convolution("depthwise", [4, 17, 17, 124], [1, 1, 124, 1], [1, 1], [1, 1], "SAME", "NHWC")
_test_convolution("depthwise", [4, 17, 17, 12], [3, 3, 12, 1], [1, 1], [2, 2], "VALID", "NHWC")
_test_convolution("depthwise", [4, 17, 17, 12], [3, 3, 12, 2], [1, 1], [2, 2], "VALID", "NHWC")
_test_convolution(
"depthwise",
[4, 17, 17, 12],
[3, 3, 12, 2],
[1, 1],
[2, 2],
"VALID",
"NHWC",
add_shapes_to_graph_def=False,
)

_test_convolution(
"conv_transpose",
[4, 8, 8, 32],
Expand Down Expand Up @@ -785,6 +815,18 @@ def test_forward_convolution():
"NHWC",
[1, 8, 8, 1],
)
# Test without adding shapes to graph def
_test_convolution(
"conv_transpose",
[4, 8, 8, 32],
[1, 1, 176, 32],
[1, 1],
[1, 1],
"SAME",
"NHWC",
[4, 8, 8, 176],
add_shapes_to_graph_def=False,
)


#######################################################################
Expand All @@ -801,6 +843,7 @@ def _test_convolution3d(
padding,
data_format,
deconv_output_shape=[],
add_shapes_to_graph_def=True,
):
""" One iteration of 3D convolution with given shapes and attributes """

Expand Down Expand Up @@ -836,6 +879,7 @@ def _test_convolution3d(
"Placeholder:0",
"Conv3D:0",
cuda_layout="NCDHW",
add_shapes_to_graph_def=add_shapes_to_graph_def,
)


Expand Down Expand Up @@ -866,6 +910,17 @@ def test_forward_convolution3d():
_test_convolution3d(
"conv", [4, 17, 17, 17, 12], [3, 3, 3, 12, 32], [1, 1, 1], [2, 2, 2], "VALID", "NDHWC"
)
# Test without adding shapes to graph def
_test_convolution3d(
"conv",
[4, 17, 17, 17, 12],
[3, 3, 3, 12, 32],
[1, 1, 1],
[2, 2, 2],
"VALID",
"NDHWC",
add_shapes_to_graph_def=False,
)


#######################################################################
Expand All @@ -874,7 +929,13 @@ def test_forward_convolution3d():


def _test_convolution3d_transpose(
data_shape, filter_shape, strides, padding, output_shape, data_format="NCDHW"
data_shape,
filter_shape,
strides,
padding,
output_shape,
data_format="NCDHW",
add_shapes_to_graph_def=True,
):
""" One iteration of 3D convolution transpose with given shapes and attributes """

Expand All @@ -899,7 +960,13 @@ def _test_convolution3d_transpose(
data_format=data_format,
)

compare_tf_with_tvm(data_array, "Placeholder:0", "conv3d_transpose:0", cuda_layout="NDHWC")
compare_tf_with_tvm(
data_array,
"Placeholder:0",
"conv3d_transpose:0",
cuda_layout="NDHWC",
add_shapes_to_graph_def=add_shapes_to_graph_def,
)


@tvm.testing.uses_gpu
Expand Down Expand Up @@ -973,6 +1040,17 @@ def test_forward_convolution3d_transpose():
data_format="NDHWC",
)

# Test without adding shapes to graph def
_test_convolution3d_transpose(
data_shape=[1, 8, 8, 8, 16],
filter_shape=[3, 3, 3, 6, 16],
strides=[3, 3, 3],
padding="VALID",
output_shape=[1, 24, 24, 24, 6],
data_format="NDHWC",
add_shapes_to_graph_def=False,
)


#######################################################################
# BiasAdd
Expand Down

0 comments on commit c7fef34

Please sign in to comment.