From 63644809b62bcf0877f2279ff735c07bc587cf81 Mon Sep 17 00:00:00 2001 From: "Tatsuya.Nishiyama" Date: Fri, 26 Oct 2018 02:39:08 +0900 Subject: [PATCH 1/3] Add split support and test for it --- nnvm/python/nnvm/frontend/tensorflow.py | 18 +++++++++++++++++- .../frontend/tensorflow/test_forward.py | 19 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/nnvm/python/nnvm/frontend/tensorflow.py b/nnvm/python/nnvm/frontend/tensorflow.py index ad7c4fc6796f..4528897e4eec 100644 --- a/nnvm/python/nnvm/frontend/tensorflow.py +++ b/nnvm/python/nnvm/frontend/tensorflow.py @@ -694,7 +694,7 @@ def _impl(inputs, attr, params): if padlist_key in params: padlist = params.pop(padlist_key).asnumpy() else: - raise RuntimeError("Required parameter {} not fount.".format(padlist_key)) + raise RuntimeError("Required parameter {} not found.".format(padlist_key)) paddings = tuple([tuple(l) for l in padlist]) attr['pad_width'] = paddings attr['pad_value'] = 0 @@ -768,6 +768,20 @@ def _impl(inputs, attr, params): )(inputs, attr) return _impl +def _split(name): + def _impl(inputs, attr, params): + if name == 'Split': + axis = params.pop(inputs[0].list_output_names()[0]).asnumpy()[0] + num_split = attr["num_split"] + return _sym.split(inputs[1], indices_or_sections=num_split, axis=axis) + elif name == 'SplitV': + indices = params.pop(inputs[1].list_output_names()[0]).asnumpy() + axis = params.pop(inputs[2].list_output_names()[0]).asnumpy()[0] + return _sym.split(inputs[0], indices_or_sections=tuple(sorted(indices)), axis=axis) + else: + raise NotImplementedError("Unexpected split type: {}".format(name)) + return _impl + # compatible operators that do NOT require any conversion. _identity_list = [] @@ -834,6 +848,8 @@ def _impl(inputs, attr, params): 'GreaterEqual' : _broadcast('greater_equal'), 'Equal' : _broadcast('equal'), 'NotEqual' : _broadcast('not_equal'), + 'Split' : _split('Split'), + 'SplitV' : _split('SplitV') } # _convert_map_rnn defines maps of rnn operator name to diff --git a/nnvm/tests/python/frontend/tensorflow/test_forward.py b/nnvm/tests/python/frontend/tensorflow/test_forward.py index 2ebc7b671ba5..4cae1df93e33 100644 --- a/nnvm/tests/python/frontend/tensorflow/test_forward.py +++ b/nnvm/tests/python/frontend/tensorflow/test_forward.py @@ -999,11 +999,26 @@ def test_forward_rel_ops(): _test_forward_rel_op([t1, t2], math_ops.equal) _test_forward_rel_op([t1, t2], math_ops.not_equal) +####################################################################### +# Split +# ----- +def test_forward_split(): + def check_split(ishape, **kwargs): + inp_array = np.random.uniform(size=ishape).astype(np.float32) + with tf.Graph().as_default(): + in1 = tf.placeholder(shape=inp_array.shape, dtype=inp_array.dtype) + tf.split(in1, **kwargs) + compare_tf_with_tvm(inp_array, 'Placeholder:0', 'split:0') + + check_split((5, 30), num_or_size_splits=3, axis=1) + check_split((5, 30), num_or_size_splits=[4, 15, 11], axis=1) ####################################################################### # Main # ---- if __name__ == '__main__': + test_forward_split() + # Transforms test_forward_transpose() test_forward_reshape() @@ -1024,8 +1039,8 @@ def test_forward_rel_ops(): # Reductions test_forward_argminmax() - test_forward_reduce() - test_forward_mean() + #test_forward_reduce() + #test_forward_mean() # NN test_forward_convolution() From 653eae306c291c0fc5e46039bc07c0d4a914b10d Mon Sep 17 00:00:00 2001 From: "Tatsuya.Nishiyama" Date: Fri, 26 Oct 2018 13:41:13 +0900 Subject: [PATCH 2/3] fix --- nnvm/tests/python/frontend/tensorflow/test_forward.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nnvm/tests/python/frontend/tensorflow/test_forward.py b/nnvm/tests/python/frontend/tensorflow/test_forward.py index 4cae1df93e33..332f10d5e070 100644 --- a/nnvm/tests/python/frontend/tensorflow/test_forward.py +++ b/nnvm/tests/python/frontend/tensorflow/test_forward.py @@ -1017,8 +1017,6 @@ def check_split(ishape, **kwargs): # Main # ---- if __name__ == '__main__': - test_forward_split() - # Transforms test_forward_transpose() test_forward_reshape() @@ -1039,8 +1037,8 @@ def check_split(ishape, **kwargs): # Reductions test_forward_argminmax() - #test_forward_reduce() - #test_forward_mean() + test_forward_reduce() + test_forward_mean() # NN test_forward_convolution() @@ -1070,3 +1068,6 @@ def check_split(ishape, **kwargs): # Relational ops test_forward_rel_ops() + + # Split + test_forward_split() From cd48b25054f8b174ca5607d51e32395f222f7ff7 Mon Sep 17 00:00:00 2001 From: "Tatsuya.Nishiyama" Date: Mon, 29 Oct 2018 13:13:53 +0900 Subject: [PATCH 3/3] [WIP] Add a testcase to apply concat after split --- nnvm/python/nnvm/frontend/tensorflow.py | 27 +++++++----- .../frontend/tensorflow/test_forward.py | 41 +++++++++++-------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/nnvm/python/nnvm/frontend/tensorflow.py b/nnvm/python/nnvm/frontend/tensorflow.py index 4528897e4eec..5feb446c8254 100644 --- a/nnvm/python/nnvm/frontend/tensorflow.py +++ b/nnvm/python/nnvm/frontend/tensorflow.py @@ -772,12 +772,15 @@ def _split(name): def _impl(inputs, attr, params): if name == 'Split': axis = params.pop(inputs[0].list_output_names()[0]).asnumpy()[0] - num_split = attr["num_split"] - return _sym.split(inputs[1], indices_or_sections=num_split, axis=axis) + return AttrCvt(op_name="split", ignores=['Tdim', 'Tidx'], + transforms={'num_split': 'indices_or_sections'}, + extras={'axis': axis})(inputs[1], attr) elif name == 'SplitV': indices = params.pop(inputs[1].list_output_names()[0]).asnumpy() axis = params.pop(inputs[2].list_output_names()[0]).asnumpy()[0] - return _sym.split(inputs[0], indices_or_sections=tuple(sorted(indices)), axis=axis) + return AttrCvt(op_name="split", ignores=['Tdim', 'Tidx', 'Tlen', 'num_split'], + extras={'indices_or_sections': tuple(sorted(indices)), + 'axis': axis})(inputs[0], attr) else: raise NotImplementedError("Unexpected split type: {}".format(name)) return _impl @@ -1147,14 +1150,16 @@ def from_tensorflow(self, graph, layout="NHWC"): node.input[0] = in_name # Fill shapes for all inputs in a list - try: - inputs = [self._nodes[i] for i in node.input] - for i in node.input: - input_shapes[self._nodes[i]] = self._output_shapes[i] - attr['_input_shapes'] = input_shapes - except KeyError: - # TODO: Need to find clean way to handle '^CheckNumerics' - pass + inputs = [] + for i in node.input: + try: + symbol = self._nodes[i] + inputs.append(symbol) + input_shapes[symbol] = self._output_shapes[i] + except KeyError: + # TODO: Need to find clean way to handle '^CheckNumerics' + pass + attr['_input_shapes'] = input_shapes inputs = self._fix_extranodes(node.op, attr, inputs) diff --git a/nnvm/tests/python/frontend/tensorflow/test_forward.py b/nnvm/tests/python/frontend/tensorflow/test_forward.py index 332f10d5e070..db4525cbe7e5 100644 --- a/nnvm/tests/python/frontend/tensorflow/test_forward.py +++ b/nnvm/tests/python/frontend/tensorflow/test_forward.py @@ -638,6 +638,30 @@ def test_forward_pad(): _test_pad((2, 3), [[1,1], [2,2]], mode="CONSTANT", constant_values=1.0) +####################################################################### +# Split +# ----- +def test_forward_split(): + def check_split(ishape, **kwargs): + inp_array = np.random.uniform(size=ishape).astype(np.float32) + with tf.Graph().as_default(): + in1 = tf.placeholder(shape=inp_array.shape, dtype=inp_array.dtype) + tf.split(in1, **kwargs) + compare_tf_with_tvm(inp_array, 'Placeholder:0', 'split:0') + + def check_split_concat(ishape, **kwargs): + inp_array = np.random.uniform(size=ishape).astype(np.float32) + with tf.Graph().as_default(): + in1 = tf.placeholder(shape=inp_array.shape, dtype=inp_array.dtype) + splited = tf.split(in1, **kwargs) + tf.concat(splited, axis=1) + compare_tf_with_tvm(inp_array, 'Placeholder:0', 'concat:0') + + check_split((5, 30), num_or_size_splits=3, axis=1) + check_split((5, 30), num_or_size_splits=[4, 15, 11], axis=1) + check_split_concat((5, 30), num_or_size_splits=[15, 15], axis=1) + + ####################################################################### # Inception V3 # ------------ @@ -999,19 +1023,6 @@ def test_forward_rel_ops(): _test_forward_rel_op([t1, t2], math_ops.equal) _test_forward_rel_op([t1, t2], math_ops.not_equal) -####################################################################### -# Split -# ----- -def test_forward_split(): - def check_split(ishape, **kwargs): - inp_array = np.random.uniform(size=ishape).astype(np.float32) - with tf.Graph().as_default(): - in1 = tf.placeholder(shape=inp_array.shape, dtype=inp_array.dtype) - tf.split(in1, **kwargs) - compare_tf_with_tvm(inp_array, 'Placeholder:0', 'split:0') - - check_split((5, 30), num_or_size_splits=3, axis=1) - check_split((5, 30), num_or_size_splits=[4, 15, 11], axis=1) ####################################################################### # Main @@ -1026,6 +1037,7 @@ def check_split(ishape, **kwargs): test_forward_pad() test_forward_gather() #test_forward_stridedslice() + test_forward_split() # Activations test_forward_sigmoid() @@ -1068,6 +1080,3 @@ def check_split(ishape, **kwargs): # Relational ops test_forward_rel_ops() - - # Split - test_forward_split()