From 8b04baabf0f261eadfe69dc81a49a31bc15a2ae0 Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 22 Oct 2019 10:21:44 -0700 Subject: [PATCH 1/2] [Relay][Frontend][TF] Fix Size operator --- python/tvm/relay/frontend/common.py | 2 +- python/tvm/relay/frontend/tensorflow.py | 9 +- .../frontend/tensorflow/test_forward.py | 225 +++++++++--------- 3 files changed, 123 insertions(+), 113 deletions(-) diff --git a/python/tvm/relay/frontend/common.py b/python/tvm/relay/frontend/common.py index 637e1f0860da..d4b9162d6f3d 100644 --- a/python/tvm/relay/frontend/common.py +++ b/python/tvm/relay/frontend/common.py @@ -259,7 +259,7 @@ def get_relay_op(op_name): op = None else: # try search op in various modules - for candidate in (_op, _op.nn, _op.image, _op.vision): + for candidate in (_op, _op.nn, _op.image, _op.vision, _op.contrib): op = getattr(candidate, op_name, None) if op is not None: break diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index eb67cf24b81e..95e0008b3332 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -1305,6 +1305,13 @@ def _impl(inputs, attr, params): return _op.multiply(difference, difference) return _impl +def _size(): + def _impl(inputs, attr, params): + new_attr = attr + new_attr['out_type'] = attr['out_type'].name + return AttrCvt('ndarray_size', transforms={'out_type' : 'dtype'})(inputs, new_attr) + return _impl + # compatible operators that do NOT require any conversion. _identity_list = [] @@ -1410,7 +1417,7 @@ def _impl(inputs, attr, params): 'Shape' : _shape(), 'Sigmoid' : AttrCvt('sigmoid'), 'Sign' : AttrCvt('sign'), - 'Size' : AttrCvt('ndarray_size'), + 'Size' : _size(), 'Slice' : _slice(), 'Softmax' : _softmax(), 'Softplus' : _softplus(), diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 420bcb72a4a2..50c2b7ee851c 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -2184,15 +2184,18 @@ def check_mean(ishape, **kwargs): def test_forward_size(): def check_size(ishape): np_input = np.random.uniform(size=ishape).astype(np.float32) + + # if all dimensions are constant, TF will optimize away size operator into constant + tf_input_shape = list(np_input.shape) + tf_input_shape[0] = None + with tf.Graph().as_default(): - input = tf.placeholder(shape=np_input.shape, dtype=np_input.dtype, name='input') + input = tf.placeholder(shape=tf_input_shape, dtype=np_input.dtype, name='input') tf.size(input, name='size') compare_tf_with_tvm([np_input], ['input:0'], 'size:0') - if tf.__version__ < LooseVersion('1.1'): - check_size((10, 8, 16, 32)) - check_size((10,)) - check_size(()) + check_size((10, 8, 16, 32)) + check_size((10,)) ####################################################################### # All, Max, Min @@ -2361,112 +2364,112 @@ def test_forward_one_hot(): if __name__ == '__main__': # Transforms - test_forward_transpose() - test_forward_reshape() - test_forward_depthtospace() - test_forward_spacetodepth() - test_forward_squeeze() - test_forward_pack() + # test_forward_transpose() + # test_forward_reshape() + # test_forward_depthtospace() + # test_forward_spacetodepth() + # test_forward_squeeze() + # test_forward_pack() test_forward_size() - test_forward_broadcast_to() - test_forward_fill() - test_forward_crop() - test_forward_resize() - test_forward_crop_and_resize() - test_forward_pad() - test_forward_unpack() - test_forward_gather() - test_forward_gather_nd() - test_forward_stridedslice() - test_forward_split() - test_forward_unstack() - test_forward_tile() - test_forward_top_k_v2() - test_forward_clip_by_value() - test_forward_maximum() - test_forward_minimum() - test_forward_range() - test_forward_right_shift() - test_forward_left_shift() - test_forward_truncatemod() - test_forward_one_hot() - - # Activations - test_forward_sigmoid() - test_forward_relu() - test_forward_leaky_relu() - test_forward_elu() - test_forward_selu() - test_forward_tanh() - - # Tensor - test_forward_round() - test_forward_reverse_v2() - test_forward_pow_exp() - test_forward_sign() - test_forward_log() - test_forward_log1p() - test_forward_cos() - test_forward_sin() - test_forward_negative() - test_forward_divide() - test_forward_abs() - test_forward_softplus() - test_forward_sqrt() - test_forward_rsqrt() - test_forward_expand_dims() - test_forward_square() - test_forward_softmax() - test_forward_log_softmax() - test_forward_bias_add() - test_forward_zeros_like() - test_forward_erf() - test_forward_squared_difference() - - # Reductions - test_forward_argminmax() - test_forward_reduce() - test_forward_mean() - test_forward_reduce_prod() - test_forward_reduce_all() - test_forward_reduce_max() - test_forward_reduce_min() - - # General - test_forward_multi_input() - test_forward_multi_output() - test_forward_variable() - test_placeholder() - - # NN - test_forward_convolution() - test_forward_pooling() - test_forward_concat_v2() - test_forward_lrn() - test_forward_l2_normalize() - test_forward_space_to_batch_nd() - test_forward_batch_to_space_nd() - - # End to End - test_forward_inception_v3() - test_forward_inception_v1() - test_forward_mobilenet() - test_forward_resnetv2() - test_forward_placeholder() - test_forward_ptb() - - # RNN - test_forward_lstm() - - # Elementwise - test_forward_ceil() - test_forward_floor() - - # Relational ops - test_forward_rel_ops() - test_forward_logical() - test_forward_where() - test_forward_matmul() - test_forward_batch_matmul() + # test_forward_broadcast_to() + # test_forward_fill() + # test_forward_crop() + # test_forward_resize() + # test_forward_crop_and_resize() + # test_forward_pad() + # test_forward_unpack() + # test_forward_gather() + # test_forward_gather_nd() + # test_forward_stridedslice() + # test_forward_split() + # test_forward_unstack() + # test_forward_tile() + # test_forward_top_k_v2() + # test_forward_clip_by_value() + # test_forward_maximum() + # test_forward_minimum() + # test_forward_range() + # test_forward_right_shift() + # test_forward_left_shift() + # test_forward_truncatemod() + # test_forward_one_hot() + + # # Activations + # test_forward_sigmoid() + # test_forward_relu() + # test_forward_leaky_relu() + # test_forward_elu() + # test_forward_selu() + # test_forward_tanh() + + # # Tensor + # test_forward_round() + # test_forward_reverse_v2() + # test_forward_pow_exp() + # test_forward_sign() + # test_forward_log() + # test_forward_log1p() + # test_forward_cos() + # test_forward_sin() + # test_forward_negative() + # test_forward_divide() + # test_forward_abs() + # test_forward_softplus() + # test_forward_sqrt() + # test_forward_rsqrt() + # test_forward_expand_dims() + # test_forward_square() + # test_forward_softmax() + # test_forward_log_softmax() + # test_forward_bias_add() + # test_forward_zeros_like() + # test_forward_erf() + # test_forward_squared_difference() + + # # Reductions + # test_forward_argminmax() + # test_forward_reduce() + # test_forward_mean() + # test_forward_reduce_prod() + # test_forward_reduce_all() + # test_forward_reduce_max() + # test_forward_reduce_min() + + # # General + # test_forward_multi_input() + # test_forward_multi_output() + # test_forward_variable() + # test_placeholder() + + # # NN + # test_forward_convolution() + # test_forward_pooling() + # test_forward_concat_v2() + # test_forward_lrn() + # test_forward_l2_normalize() + # test_forward_space_to_batch_nd() + # test_forward_batch_to_space_nd() + + # # End to End + # test_forward_inception_v3() + # test_forward_inception_v1() + # test_forward_mobilenet() + # test_forward_resnetv2() + # test_forward_placeholder() + # test_forward_ptb() + + # # RNN + # test_forward_lstm() + + # # Elementwise + # test_forward_ceil() + # test_forward_floor() + + # # Relational ops + # test_forward_rel_ops() + # test_forward_logical() + # test_forward_where() + # test_forward_matmul() + # test_forward_batch_matmul() # TODO missing tests: rank From 18e839d31ca21df6ef08b64513ad6ebfc55f622b Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 22 Oct 2019 10:23:58 -0700 Subject: [PATCH 2/2] Uncomment tests --- .../frontend/tensorflow/test_forward.py | 212 +++++++++--------- 1 file changed, 106 insertions(+), 106 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 50c2b7ee851c..11c6a7befca6 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -2364,112 +2364,112 @@ def test_forward_one_hot(): if __name__ == '__main__': # Transforms - # test_forward_transpose() - # test_forward_reshape() - # test_forward_depthtospace() - # test_forward_spacetodepth() - # test_forward_squeeze() - # test_forward_pack() + test_forward_transpose() + test_forward_reshape() + test_forward_depthtospace() + test_forward_spacetodepth() + test_forward_squeeze() + test_forward_pack() test_forward_size() - # test_forward_broadcast_to() - # test_forward_fill() - # test_forward_crop() - # test_forward_resize() - # test_forward_crop_and_resize() - # test_forward_pad() - # test_forward_unpack() - # test_forward_gather() - # test_forward_gather_nd() - # test_forward_stridedslice() - # test_forward_split() - # test_forward_unstack() - # test_forward_tile() - # test_forward_top_k_v2() - # test_forward_clip_by_value() - # test_forward_maximum() - # test_forward_minimum() - # test_forward_range() - # test_forward_right_shift() - # test_forward_left_shift() - # test_forward_truncatemod() - # test_forward_one_hot() - - # # Activations - # test_forward_sigmoid() - # test_forward_relu() - # test_forward_leaky_relu() - # test_forward_elu() - # test_forward_selu() - # test_forward_tanh() - - # # Tensor - # test_forward_round() - # test_forward_reverse_v2() - # test_forward_pow_exp() - # test_forward_sign() - # test_forward_log() - # test_forward_log1p() - # test_forward_cos() - # test_forward_sin() - # test_forward_negative() - # test_forward_divide() - # test_forward_abs() - # test_forward_softplus() - # test_forward_sqrt() - # test_forward_rsqrt() - # test_forward_expand_dims() - # test_forward_square() - # test_forward_softmax() - # test_forward_log_softmax() - # test_forward_bias_add() - # test_forward_zeros_like() - # test_forward_erf() - # test_forward_squared_difference() - - # # Reductions - # test_forward_argminmax() - # test_forward_reduce() - # test_forward_mean() - # test_forward_reduce_prod() - # test_forward_reduce_all() - # test_forward_reduce_max() - # test_forward_reduce_min() - - # # General - # test_forward_multi_input() - # test_forward_multi_output() - # test_forward_variable() - # test_placeholder() - - # # NN - # test_forward_convolution() - # test_forward_pooling() - # test_forward_concat_v2() - # test_forward_lrn() - # test_forward_l2_normalize() - # test_forward_space_to_batch_nd() - # test_forward_batch_to_space_nd() - - # # End to End - # test_forward_inception_v3() - # test_forward_inception_v1() - # test_forward_mobilenet() - # test_forward_resnetv2() - # test_forward_placeholder() - # test_forward_ptb() - - # # RNN - # test_forward_lstm() - - # # Elementwise - # test_forward_ceil() - # test_forward_floor() - - # # Relational ops - # test_forward_rel_ops() - # test_forward_logical() - # test_forward_where() - # test_forward_matmul() - # test_forward_batch_matmul() + test_forward_broadcast_to() + test_forward_fill() + test_forward_crop() + test_forward_resize() + test_forward_crop_and_resize() + test_forward_pad() + test_forward_unpack() + test_forward_gather() + test_forward_gather_nd() + test_forward_stridedslice() + test_forward_split() + test_forward_unstack() + test_forward_tile() + test_forward_top_k_v2() + test_forward_clip_by_value() + test_forward_maximum() + test_forward_minimum() + test_forward_range() + test_forward_right_shift() + test_forward_left_shift() + test_forward_truncatemod() + test_forward_one_hot() + + # Activations + test_forward_sigmoid() + test_forward_relu() + test_forward_leaky_relu() + test_forward_elu() + test_forward_selu() + test_forward_tanh() + + # Tensor + test_forward_round() + test_forward_reverse_v2() + test_forward_pow_exp() + test_forward_sign() + test_forward_log() + test_forward_log1p() + test_forward_cos() + test_forward_sin() + test_forward_negative() + test_forward_divide() + test_forward_abs() + test_forward_softplus() + test_forward_sqrt() + test_forward_rsqrt() + test_forward_expand_dims() + test_forward_square() + test_forward_softmax() + test_forward_log_softmax() + test_forward_bias_add() + test_forward_zeros_like() + test_forward_erf() + test_forward_squared_difference() + + # Reductions + test_forward_argminmax() + test_forward_reduce() + test_forward_mean() + test_forward_reduce_prod() + test_forward_reduce_all() + test_forward_reduce_max() + test_forward_reduce_min() + + # General + test_forward_multi_input() + test_forward_multi_output() + test_forward_variable() + test_placeholder() + + # NN + test_forward_convolution() + test_forward_pooling() + test_forward_concat_v2() + test_forward_lrn() + test_forward_l2_normalize() + test_forward_space_to_batch_nd() + test_forward_batch_to_space_nd() + + # End to End + test_forward_inception_v3() + test_forward_inception_v1() + test_forward_mobilenet() + test_forward_resnetv2() + test_forward_placeholder() + test_forward_ptb() + + # RNN + test_forward_lstm() + + # Elementwise + test_forward_ceil() + test_forward_floor() + + # Relational ops + test_forward_rel_ops() + test_forward_logical() + test_forward_where() + test_forward_matmul() + test_forward_batch_matmul() # TODO missing tests: rank