From e01f0d41ddbe67956c389781cb7c3f547386a304 Mon Sep 17 00:00:00 2001 From: lisiyuan Date: Tue, 19 Nov 2019 14:39:10 +0800 Subject: [PATCH 1/2] fix slice bug when input is param --- python/tvm/relay/frontend/tensorflow.py | 10 ++++++++-- tests/python/frontend/tensorflow/test_forward.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 2decc2180e48..b71bb95c7011 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -626,8 +626,14 @@ def _impl(inputs, attr, params): def _slice(): def _impl(inputs, attr, params): - begin = _get_list_param(params, inputs[1]) - size = _get_list_param(params, inputs[2]) + try: + begin = _get_list_param(params, inputs[1]) + except: + begin = _infer_value_simulated(inputs[1], params).asnumpy()[0] + try: + size = _get_list_param(params, inputs[2]) + except: + size = _infer_value_simulated(inputs[2], params).asnumpy()[0] data_shape = attr['_input_shapes'][inputs[0]] data_dim = len(data_shape) end = size diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index db19ed4e851d..d25c586e60ba 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -2188,6 +2188,20 @@ def test_forward_transpose(): _test_forward_tranapose_axes_input((2, 3, 4, 5), (3, 0, 1, 2)) +def _test_forward_slice_operation_input(input_value, begin_value, size_value): + input_data = np.array(input_value, dtype=np.float32) + with tf.Graph().as_default(): + input_tensor = tf.placeholder( + shape=input_data.shape, dtype=input_data.dtype, name="input") + begin_tensor = tf.expand_dims(begin_value, axis=0) + size_tensor = tf.expand_dims(size_value, axis=0) + slice_tensor = tf.slice(input_tensor, begin_tensor, size_tensor, name='slice_output') + compare_tf_with_tvm([input_data], ['input:0'], 'slice_output:0') + + +def test_forward_slice(): + _test_forward_slice_operation_input([1, 1], 0, 2) + def test_forward_ceil(): ishape = (1, 3, 10, 10) inp_array = np.random.uniform(size=ishape).astype(np.float32) @@ -2762,6 +2776,7 @@ def test_forward_add_n(): if __name__ == '__main__': # Transforms + test_forward_slice() test_forward_transpose() test_forward_reshape() test_forward_depthtospace() From 4d5c0158a4e00bc84b1d3ca004301a7899c2d8d1 Mon Sep 17 00:00:00 2001 From: lisiyuan Date: Thu, 21 Nov 2019 09:51:13 +0800 Subject: [PATCH 2/2] use _infer_value rather than _infer_value_simulated --- python/tvm/relay/frontend/tensorflow.py | 8 ++++---- tests/python/frontend/tensorflow/test_forward.py | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index b71bb95c7011..7c1d34f3fd2c 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -628,12 +628,12 @@ def _slice(): def _impl(inputs, attr, params): try: begin = _get_list_param(params, inputs[1]) - except: - begin = _infer_value_simulated(inputs[1], params).asnumpy()[0] + except (IndexError, KeyError, AttributeError): + begin = _infer_value(inputs[1], params).asnumpy().tolist()[0] try: size = _get_list_param(params, inputs[2]) - except: - size = _infer_value_simulated(inputs[2], params).asnumpy()[0] + except (IndexError, KeyError, AttributeError): + size = _infer_value(inputs[2], params).asnumpy().tolist()[0] data_shape = attr['_input_shapes'][inputs[0]] data_dim = len(data_shape) end = size diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index d25c586e60ba..4ec8abdfb336 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -2774,7 +2774,6 @@ def test_forward_add_n(): # Main # ---- if __name__ == '__main__': - # Transforms test_forward_slice() test_forward_transpose()