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

[Frontend][Tensorflow] Support SAME padding for dynamic h, w when stride == 1 #7885

Merged
merged 3 commits into from
Apr 21, 2021
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
7 changes: 6 additions & 1 deletion python/tvm/relay/frontend/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ def list_shape_of(tensor, ndim):


def _get_pad_pair(input1d, kernel1d, stride1d):
if input1d % stride1d == 0:
if isinstance(input1d, tvm.tir.Any) and stride1d != 1:
raise tvm.error.OpAttributeUnImplemented(
"SAME padding is not supported in combination with dynamic height or width when stride"
" is not 1."
)
if stride1d == 1 or input1d % stride1d == 0:
pad = max(kernel1d - stride1d, 0)
else:
pad = max(kernel1d - (input1d % stride1d), 0)
Expand Down
28 changes: 28 additions & 0 deletions tests/python/frontend/tensorflow/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def compare_tf_with_tvm(
cuda_layout="NCHW",
add_shapes_to_graph_def=True,
targets=None,
ignore_in_shape=False,
):
"""Generic function to generate and compare tensorflow and TVM output"""

Expand Down Expand Up @@ -259,6 +260,7 @@ def name_without_num(name):
opt_level=opt_level,
mode=mode,
cuda_layout=cuda_layout,
ignore_in_shape=ignore_in_shape,
)
# since the names from tensorflow and relay runs are not exactly same,
# first len(tf_output) will be compared
Expand Down Expand Up @@ -314,6 +316,22 @@ def _test_pooling(input_shape, **kwargs):
_test_pooling_iteration(input_shape, **kwargs)


def _test_pooling_dynamic(input_shape, np_shape, **kwargs):
""" Pooling with dynamic height and width dimensions. """
x = -np.arange(np.prod(np_shape), dtype=np.float32).reshape(np_shape) - 1

with tf.Graph().as_default():
in_data = array_ops.placeholder(shape=input_shape, dtype="float32")
nn_ops.pool(in_data, **kwargs)

if kwargs["pooling_type"] == "MAX":
out_name = "max_pool:0"
else:
out_name = "avg_pool:0"

compare_tf_with_tvm(x, "Placeholder:0", out_name, mode="vm", ignore_in_shape=True)


@tvm.testing.uses_gpu
def test_forward_pooling():
""" Pooling """
Expand Down Expand Up @@ -347,6 +365,16 @@ def test_forward_pooling():
strides=[2, 2, 2],
)

_test_pooling_dynamic(
input_shape=[1, None, None, 3],
np_shape=[1, 32, 32, 3],
window_shape=[2, 2],
padding="SAME",
pooling_type=pool_type,
dilation_rate=[1, 1],
strides=[1, 1],
)

# test cases for max_pool3d & avg_pool3d with layout NCDHW
# TensorFlow pool3d doesn't support NCDHW on cpu
if is_gpu_available():
Expand Down