From 8d4ea068eb25678d72682030bc3a12a29a37daeb Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Mon, 30 Sep 2019 15:41:28 -0700 Subject: [PATCH 01/19] Add pytest. Will fail --- ngraph_bridge/ngraph_builder.cc | 11 ++-- .../python/test_provenance_tags_attachment.py | 54 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 test/python/test_provenance_tags_attachment.py diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index 51adb9c12..ba8c43fce 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -5153,12 +5153,17 @@ Status Builder::TranslateGraph( return n->is_parameter() || is_result; }; + size_t num_tags = 0; for (auto n : ng_function->get_ordered_ops()) { // Results and Parameters are not expected to have provenance tags if (!check_if_result_or_parameter(n)) { - if (n->get_provenance_tags().size() == 0) { - return errors::Internal("Found ngraph node ", n->get_name(), - " which does not have provenance tag set"); + num_tags = n->get_provenance_tags().size(); + if (num_tags != 1) { + return errors::Internal( + "Found ngraph node ", n->get_name(), + " which has provenance tag set of size ", num_tags, + ". Expected all ngraph nodes created in TranslateGraph to have " + "exactly one provenance tag"); } } } diff --git a/test/python/test_provenance_tags_attachment.py b/test/python/test_provenance_tags_attachment.py new file mode 100644 index 000000000..2e7893109 --- /dev/null +++ b/test/python/test_provenance_tags_attachment.py @@ -0,0 +1,54 @@ +# ============================================================================== +# Copyright 2019 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""nGraph TensorFlow bridge prod operations test + +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import pytest +import numpy as np +import tensorflow as tf + +from common import NgraphTest + + +class TestProductOperations(NgraphTest): + + def test_resnet_like_block(self): + # Creates a network: y = x + |x| + # --------- + # / \ + # inp-----> + ---> out_node + # \ / + # ---abs---- + # The translation of the Add node, first broadcasts the 2 inputs + # it receives and then creates an ngraph Add node. + # Since the shapes of the inputs to the TF add node are same, + # the broadcast builder will return the exact same inputs (lhs and rhs) + # without creating new ng nodes + # If we do not take care, we could be adding a tag to the ng abs node + # when tagging the return of the broadcast builder + + # This test makes sure that TranslateGraph checks that the + # builder returned nodes are different from its inputs, + # and only in that case it adds provenance tags + + inp = tf.placeholder(tf.float64, shape=[1, 32, 32, 2], name='input') + out_node = tf.add(tf.math.abs(inp, name="abs"), inp, name="add") + self.with_ngraph(lambda sess: sess.run( + out_node, feed_dict={inp: np.ones([1, 32, 32, 2])})) From 39641a971b5a75cefacd2a3c8b3a73616d748b11 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Mon, 30 Sep 2019 16:07:10 -0700 Subject: [PATCH 02/19] Fix broadcast builder provenance attachment --- ngraph_bridge/ngraph_builder.cc | 16 ++++++++++++++-- ngraph_bridge/ngraph_builder.h | 4 ++++ test/python/test_provenance_tags_attachment.py | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index ba8c43fce..c52d582d9 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -346,6 +346,17 @@ Builder::TF_NGRAPH_CONST_MAP() { return the_map; } +static std::pair, std::shared_ptr> +PerformNgBroadcast(const string& prov_tag, std::shared_ptr ng_lhs, + std::shared_ptr ng_rhs) { + std::shared_ptr ng_lhs_new, ng_rhs_new; + std::tie(ng_lhs_new, ng_rhs_new) = + ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); + if (ng_lhs_new != ng_lhs) ng_lhs->add_provenance_tag(prov_tag); + if (ng_rhs_new != ng_rhs) ng_rhs->add_provenance_tag(prov_tag); + return make_pair(ng_lhs_new, ng_rhs_new); +} + // Helper function to translate a unary op. // // Parameters: @@ -434,10 +445,10 @@ static Status TranslateBinaryOp( std::shared_ptr ng_lhs, ng_rhs; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); + std::tie(ng_lhs, ng_rhs) = PerformNgBroadcast(op->name(), ng_lhs, ng_rhs); + std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); - ng_lhs->add_provenance_tag(op->name()); - ng_rhs->add_provenance_tag(op->name()); auto ng_node = create_binary_op(ng_lhs, ng_rhs); ng_node->add_provenance_tag(op->name()); @@ -2996,6 +3007,7 @@ static Status TranslateOneHotOp( // broadcast to make all tensors same shape, as required by ngraph select op std::tie(ng_onehot_bool, ng_on) = ng::builder::numpy_broadcast(std::make_pair(ng_onehot_bool, ng_on)); + ng_onehot_bool->add_provenance_tag(op->name()); ng_on->add_provenance_tag(op->name()); std::tie(ng_onehot_bool, ng_off) = diff --git a/ngraph_bridge/ngraph_builder.h b/ngraph_bridge/ngraph_builder.h index c453e990b..b39db598e 100644 --- a/ngraph_bridge/ngraph_builder.h +++ b/ngraph_bridge/ngraph_builder.h @@ -137,6 +137,10 @@ class Builder { NGRAPH_VLOG(3) << "ng_padding_above: " << ngraph::join(ng_padding_above); } + static std::pair, std::shared_ptr> + PerformNgBroadcast(const string&, std::shared_ptr, + std::shared_ptr); + static const std::map< DataType, std::pair Date: Mon, 30 Sep 2019 17:10:31 -0700 Subject: [PATCH 03/19] Replace other autobroadcasts with new function --- ngraph_bridge/ngraph_builder.cc | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index c52d582d9..e2fb9aa5e 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -447,9 +447,6 @@ static Status TranslateBinaryOp( std::tie(ng_lhs, ng_rhs) = PerformNgBroadcast(op->name(), ng_lhs, ng_rhs); - std::tie(ng_lhs, ng_rhs) = - ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); - auto ng_node = create_binary_op(ng_lhs, ng_rhs); ng_node->add_provenance_tag(op->name()); @@ -3006,14 +3003,9 @@ static Status TranslateOneHotOp( // broadcast to make all tensors same shape, as required by ngraph select op std::tie(ng_onehot_bool, ng_on) = - ng::builder::numpy_broadcast(std::make_pair(ng_onehot_bool, ng_on)); - - ng_onehot_bool->add_provenance_tag(op->name()); - ng_on->add_provenance_tag(op->name()); + PerformNgBroadcast(op->name(), ng_onehot_bool, ng_on); std::tie(ng_onehot_bool, ng_off) = - ng::builder::numpy_broadcast(std::make_pair(ng_onehot_bool, ng_off)); - ng_onehot_bool->add_provenance_tag(op->name()); - ng_off->add_provenance_tag(op->name()); + PerformNgBroadcast(op->name(), ng_onehot_bool, ng_off); auto ng_onehot = ConstructNgNode(op->name(), ng_onehot_bool, ng_on, ng_off); @@ -4863,14 +4855,10 @@ static Status TranslateSelectOp(const Node* op, op->name(), ng_input1, ng::AxisVector{0}, tmp_vector); } - std::tie(ng_input1, ng_input2) = ng::builder::numpy_broadcast( - std::make_pair(length != 0 ? ng_input_new : ng_input1, ng_input2)); - ng_input1->add_provenance_tag(op->name()); - ng_input2->add_provenance_tag(op->name()); + std::tie(ng_input1, ng_input2) = PerformNgBroadcast( + op->name(), (length != 0 ? ng_input_new : ng_input1), ng_input2); std::tie(ng_input2, ng_input3) = - ng::builder::numpy_broadcast(std::make_pair(ng_input2, ng_input3)); - ng_input2->add_provenance_tag(op->name()); - ng_input3->add_provenance_tag(op->name()); + PerformNgBroadcast(op->name(), ng_input2, ng_input3); ng_select = ConstructNgNode(op->name(), ng_input1, ng_input2, ng_input3); From 1186db1f837cb043dac780660750f8d1cc1d69e0 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Mon, 30 Sep 2019 17:40:18 -0700 Subject: [PATCH 04/19] Fix bug in PerformNgBroadcast --- ngraph_bridge/ngraph_builder.cc | 4 ++-- test/python/test_provenance_tags_attachment.py | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index e2fb9aa5e..1a7e8f97f 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -352,8 +352,8 @@ PerformNgBroadcast(const string& prov_tag, std::shared_ptr ng_lhs, std::shared_ptr ng_lhs_new, ng_rhs_new; std::tie(ng_lhs_new, ng_rhs_new) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); - if (ng_lhs_new != ng_lhs) ng_lhs->add_provenance_tag(prov_tag); - if (ng_rhs_new != ng_rhs) ng_rhs->add_provenance_tag(prov_tag); + if (ng_lhs_new != ng_lhs) ng_lhs_new->add_provenance_tag(prov_tag); + if (ng_rhs_new != ng_rhs) ng_rhs_new->add_provenance_tag(prov_tag); return make_pair(ng_lhs_new, ng_rhs_new); } diff --git a/test/python/test_provenance_tags_attachment.py b/test/python/test_provenance_tags_attachment.py index 2438e80c9..46c04eb14 100644 --- a/test/python/test_provenance_tags_attachment.py +++ b/test/python/test_provenance_tags_attachment.py @@ -29,7 +29,7 @@ class TestProductOperations(NgraphTest): - def test_resnet_like_block(self): + def test_provenance_for_no_effect_broadcast(self): # Creates a network: y = x + |x| # --------- # / \ @@ -52,3 +52,10 @@ def test_resnet_like_block(self): out_node = tf.add(tf.math.abs(inp, name="abs"), inp, name="add") self.with_ngraph(lambda sess: sess.run( out_node, feed_dict={inp: np.ones([1, 32, 32, 2])})) + + def test_provenance_for_broadcast_with_effect(self): + inp0 = tf.placeholder(tf.float64, shape=[2, 2], name='input0') + inp1 = tf.placeholder(tf.float64, shape=[2], name='input1') + out_node = inp0 / inp1 + self.with_ngraph(lambda sess: sess.run( + out_node, feed_dict={inp0: np.ones([2, 2]), inp1: np.ones([2])})) From 943d36b6a7c8e2d6a4ec9a4be0a00e65838b8569 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Mon, 30 Sep 2019 17:43:03 -0700 Subject: [PATCH 05/19] Style --- test/python/test_provenance_tags_attachment.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/python/test_provenance_tags_attachment.py b/test/python/test_provenance_tags_attachment.py index 46c04eb14..bd2373a3f 100644 --- a/test/python/test_provenance_tags_attachment.py +++ b/test/python/test_provenance_tags_attachment.py @@ -58,4 +58,7 @@ def test_provenance_for_broadcast_with_effect(self): inp1 = tf.placeholder(tf.float64, shape=[2], name='input1') out_node = inp0 / inp1 self.with_ngraph(lambda sess: sess.run( - out_node, feed_dict={inp0: np.ones([2, 2]), inp1: np.ones([2])})) + out_node, feed_dict={ + inp0: np.ones([2, 2]), + inp1: np.ones([2]) + })) From 019c8226b507dcec4f75b4f7df29e31d2a181904 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Mon, 30 Sep 2019 17:50:09 -0700 Subject: [PATCH 06/19] Expand test to cover lhs and rhs broadcasting --- test/python/test_provenance_tags_attachment.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/test/python/test_provenance_tags_attachment.py b/test/python/test_provenance_tags_attachment.py index bd2373a3f..e134e15eb 100644 --- a/test/python/test_provenance_tags_attachment.py +++ b/test/python/test_provenance_tags_attachment.py @@ -54,11 +54,16 @@ def test_provenance_for_no_effect_broadcast(self): out_node, feed_dict={inp: np.ones([1, 32, 32, 2])})) def test_provenance_for_broadcast_with_effect(self): + # In this test, the broadcast actually produces new ng nodes + # as opposed to test_provenance_for_no_effect_broadcast, + # which is a dummy broadcast + # so test that they are tagged appropriately inp0 = tf.placeholder(tf.float64, shape=[2, 2], name='input0') inp1 = tf.placeholder(tf.float64, shape=[2], name='input1') - out_node = inp0 / inp1 - self.with_ngraph(lambda sess: sess.run( - out_node, feed_dict={ - inp0: np.ones([2, 2]), - inp1: np.ones([2]) - })) + out_node0 = inp0 / inp1 + out_node1 = inp1 / inp0 + self.with_ngraph(lambda sess: sess.run([out_node0, out_node1], + feed_dict={ + inp0: np.ones([2, 2]), + inp1: np.ones([2]) + })) From 6af4dec559022fc2562a1b063938926a5b43d072 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Mon, 30 Sep 2019 17:51:59 -0700 Subject: [PATCH 07/19] Upgrade version --- README.md | 2 +- ngraph_bridge/version.cc | 2 +- python/setup.in.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c6a9dff82..416f93d8e 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Once TensorFlow's dependencies are installed, clone the `ngraph-bridge` repo: git clone https://github.com/tensorflow/ngraph-bridge.git cd ngraph-bridge - git checkout v0.19.0-rc4 + git checkout v0.19.0-rc5 Run the following Python script to build TensorFlow, nGraph, and the bridge. Use Python 3.5: diff --git a/ngraph_bridge/version.cc b/ngraph_bridge/version.cc index da3c6e649..84bb0704a 100644 --- a/ngraph_bridge/version.cc +++ b/ngraph_bridge/version.cc @@ -32,7 +32,7 @@ // candidate such as v0.7.0-rc0 // The code in master will always have the last released version number // with a suffix of '-master' -#define NG_TF_VERSION_SUFFIX "-rc4" +#define NG_TF_VERSION_SUFFIX "-rc5" #define VERSION_STR_HELPER(x) #x #define VERSION_STR(x) VERSION_STR_HELPER(x) diff --git a/python/setup.in.py b/python/setup.in.py index f71d432ad..fdbae3e51 100644 --- a/python/setup.in.py +++ b/python/setup.in.py @@ -59,7 +59,7 @@ def get_tag(self): setup( name='ngraph_tensorflow_bridge', - version='0.19.0rc4', + version='0.19.0rc5', description='Intel nGraph compiler and runtime for TensorFlow', long_description=long_description, long_description_content_type="text/markdown", From 87beb09559aa76877f0a96b03893f58cf00241c5 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Tue, 1 Oct 2019 11:02:12 -0700 Subject: [PATCH 08/19] Dump ngfunction when incorrect --- ngraph_bridge/ngraph_builder.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index 1a7e8f97f..0b773859c 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -5159,6 +5159,9 @@ Status Builder::TranslateGraph( if (!check_if_result_or_parameter(n)) { num_tags = n->get_provenance_tags().size(); if (num_tags != 1) { + NgraphSerialize( + "tf_function_error_" + ng_function->get_name() + ".json", + ng_function); return errors::Internal( "Found ngraph node ", n->get_name(), " which has provenance tag set of size ", num_tags, From 8504d2e07853246f92b05608d561ddb05e032e5b Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Tue, 1 Oct 2019 17:47:26 -0700 Subject: [PATCH 09/19] Dump ngraph json in case of errors --- ngraph_bridge/ngraph_builder.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index 0b773859c..e2736982b 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -5159,9 +5159,24 @@ Status Builder::TranslateGraph( if (!check_if_result_or_parameter(n)) { num_tags = n->get_provenance_tags().size(); if (num_tags != 1) { + char* original_provenance_flag_value = getenv("NGRAPH_PROVENANCE_ENABLE"); + // No need to free original_provenance_flag_value + + char enable_provenance[] = "NGRAPH_PROVENANCE_ENABLE=1"; + putenv(enable_provenance); NgraphSerialize( "tf_function_error_" + ng_function->get_name() + ".json", ng_function); + if (original_provenance_flag_value == NULL) { + unsetenv("NGRAPH_PROVENANCE_ENABLE"); + } else { + string provenance_flag_original_val{original_provenance_flag_value}; + char* reset = new char[26 + provenance_flag_original_val.size()]; + strcpy(reset, ("NGRAPH_PROVENANCE_ENABLE="+provenance_flag_original_val).c_str()); + putenv(reset); + delete[] reset; + } + return errors::Internal( "Found ngraph node ", n->get_name(), " which has provenance tag set of size ", num_tags, From f5e455a8e1166c13811418d7fffb8a39102b01ef Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Tue, 1 Oct 2019 18:22:23 -0700 Subject: [PATCH 10/19] Add provenance to helper functions correctly --- ngraph_bridge/ngraph_builder.cc | 116 ++++++++++------------------ ngraph_bridge/ngraph_conversions.cc | 12 ++- ngraph_bridge/ngraph_conversions.h | 8 +- test/conversions.cpp | 8 +- 4 files changed, 57 insertions(+), 87 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index e2736982b..cdb799cd0 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -387,7 +387,8 @@ static Status TranslateUnaryOp( shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); auto ng_node = create_unary_op(ng_input); - ng_node->add_provenance_tag(op->name()); + if (ng_node != ng_input) + ng_node->add_provenance_tag(op->name()); SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); } @@ -448,7 +449,8 @@ static Status TranslateBinaryOp( std::tie(ng_lhs, ng_rhs) = PerformNgBroadcast(op->name(), ng_lhs, ng_rhs); auto ng_node = create_binary_op(ng_lhs, ng_rhs); - ng_node->add_provenance_tag(op->name()); + if (ng_node != ng_lhs && ng_node != ng_rhs) + ng_node->add_provenance_tag(op->name()); SaveNgOp(ng_op_map, op->name(), ng_node); @@ -510,8 +512,7 @@ static Status TranslateQuantizedPoolOp(const Node* op, BatchedOpParamToNGraph(is_nhwc, ng_input->get_output_shape(0), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); @@ -543,8 +544,7 @@ static Status TranslateQuantizedPoolOp(const Node* op, } ng_quant_pool->add_provenance_tag(op->name()); - BatchToTensorflow(is_nhwc, ng_quant_pool); - ng_quant_pool->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_quant_pool); SaveNgOp(ng_op_map, op->name(), ng_quant_pool); // For QuantizedAvgPool and QuantizedMaxPool input min-max remains unchanged // and is just propagated along @@ -649,8 +649,7 @@ static Status TranslateAvgPoolOp(const Node* op, BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -669,8 +668,7 @@ static Status TranslateAvgPoolOp(const Node* op, op->name(), ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above, false); - BatchToTensorflow(is_nhwc, ng_avgpool); - ng_avgpool->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_avgpool); NGRAPH_VLOG(3) << "avgpool outshape: {" << ng::join(ng_avgpool->get_shape()) << "}"; @@ -720,8 +718,7 @@ static Status TranslateAvgPoolGradOp( ng::Shape ng_window_shape(2); BatchedOpParamReshape(is_nhwc, ng_orig_input_shape, ng_forward_arg_shape); - BatchToNGraph(is_nhwc, ng_grad); - ng_grad->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_grad); BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, ng_orig_input_shape, ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_window_shape); @@ -746,8 +743,7 @@ static Status TranslateAvgPoolGradOp( op->name(), ng_forward_arg_shape, ng_grad, ng_window_shape, ng_strides, ng_padding_below, ng_padding_above, false); - BatchToTensorflow(is_nhwc, ng_avgpool_backprop); - ng_avgpool_backprop->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_avgpool_backprop); NGRAPH_VLOG(3) << "avgpoolbackprop outshape: {" << ng::join(ng_avgpool_backprop->get_shape()) << "}"; @@ -1257,8 +1253,7 @@ static Status TranslateConv2DOp(const Node* op, BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); @@ -1283,8 +1278,7 @@ static Status TranslateConv2DOp(const Node* op, op->name(), ng_input, ng_filter, ng_strides, ng_dilations, ng_padding_below, ng_padding_above); - BatchToTensorflow(is_nhwc, ng_conv); - ng_conv->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_conv); SaveNgOp(ng_op_map, op->name(), ng_conv); return Status::OK(); } @@ -1355,8 +1349,7 @@ static Status TranslateConv2DBackpropFilterOp( // nGraph Padding Below [f] // nGraph Padding Above [f] // nGraph Dilation Stride [f] - BatchToNGraph(is_nhwc, ng_data_batch); - ng_data_batch->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_data_batch); // tf_filter shape : // [filter_height, filter_width, in_channels, out_channels] // reshape for nGraph @@ -1364,8 +1357,7 @@ static Status TranslateConv2DBackpropFilterOp( static_cast(tf_filter_sizes[2]), static_cast(tf_filter_sizes[0]), static_cast(tf_filter_sizes[1])}; - BatchToNGraph(is_nhwc, ng_output_delta); - ng_output_delta->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_output_delta); BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_window_movement_strides_forward); BatchedOpParamToNGraph(is_nhwc, tf_dilations, @@ -1462,8 +1454,7 @@ static Status TranslateConv2DBackpropInputOp( BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, tf_input_sizes, ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); - BatchToNGraph(is_nhwc, ng_out_backprop); - ng_out_backprop->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_out_backprop); if (is_nhwc) { ng_batch_shape = {static_cast(tf_input_sizes[0]), static_cast(tf_input_sizes[3]), @@ -1501,8 +1492,7 @@ static Status TranslateConv2DBackpropInputOp( ng_dilations, ng_padding_below, ng_padding_above, ng::Strides(ng_batch_shape.size() - 2, 1)); - BatchToTensorflow(is_nhwc, ng_data); - ng_data->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_data); SaveNgOp(ng_op_map, op->name(), ng_data); return Status::OK(); @@ -1553,8 +1543,7 @@ static Status TranslateConv3DOp(const Node* op, BatchedOpParam3DToNGraph(is_ndhwc, tf_strides, ng_strides); BatchedOpParam3DToNGraph(is_ndhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParam3DToNGraph(is_ndhwc, tf_dilations, ng_dilations); - BatchToNGraph3D(is_ndhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph3D(op->name(), is_ndhwc, ng_input); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); @@ -1580,8 +1569,7 @@ static Status TranslateConv3DOp(const Node* op, op->name(), ng_input, ng_filter, ng_strides, ng_dilations, ng_padding_below, ng_padding_above); - BatchToTensorflow3D(is_ndhwc, ng_conv); - ng_conv->add_provenance_tag(op->name()); + BatchToTensorflow3D(op->name(), is_ndhwc, ng_conv); SaveNgOp(ng_op_map, op->name(), ng_conv); return Status::OK(); } @@ -1782,8 +1770,7 @@ static Status TranslateDepthwiseConv2dNativeOp( BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); @@ -1838,8 +1825,7 @@ static Status TranslateDepthwiseConv2dNativeOp( std::shared_ptr ng_concat = ConstructNgNode( op->name(), ng_args, ng_concatenation_axis); - BatchToTensorflow(is_nhwc, ng_concat); - ng_concat->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_concat); SaveNgOp(ng_op_map, op->name(), ng_concat); return Status::OK(); } @@ -1968,8 +1954,7 @@ static Status TranslateFusedBatchNormOp( NGRAPH_VLOG(3) << "epsilon: " << tf_epsilon; - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); std::shared_ptr ng_batch_norm; @@ -1997,8 +1982,7 @@ static Status TranslateFusedBatchNormOp( auto variance = ConstructNgNode(op->name(), ng_variance, Bessel_scale); - BatchToTensorflow(is_nhwc, ng_y); - ng_y->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_y); SaveNgOp(ng_op_map, op->name(), ng_y); SaveNgOp(ng_op_map, op->name(), ng_mean); @@ -2022,8 +2006,7 @@ static Status TranslateFusedBatchNormOp( ng_batch_norm = ConstructNgNode( op->name(), tf_epsilon, ng_scale, ng_offset, ng_input, ng_mean, ng_variance); - BatchToTensorflow(is_nhwc, ng_batch_norm); - ng_batch_norm->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_batch_norm); SaveNgOp(ng_op_map, op->name(), ng_batch_norm); if (is_v3) { SaveNgOp(ng_op_map, op->name(), ng_mean); @@ -2102,10 +2085,8 @@ static Status TranslateFusedBatchNormGradOp(const Node* op, op->name(), ng_scale->get_element_type(), ng_scale->get_shape(), std::vector{ng::shape_size(ng_scale->get_shape()), "0"}); - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); - BatchToNGraph(is_nhwc, ng_delta); - ng_delta->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); + BatchToNGraph(op->name(), is_nhwc, ng_delta); std::shared_ptr ng_batch_norm_backprop; @@ -2123,8 +2104,7 @@ static Status TranslateFusedBatchNormGradOp(const Node* op, ConstructNgNode(op->name(), ng_batch_norm_backprop, 2); - BatchToTensorflow(is_nhwc, ng_input_delta_op); - ng_input_delta_op->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_input_delta_op); SaveNgOp(ng_op_map, op->name(), ng_input_delta_op); SaveNgOp(ng_op_map, op->name(), ng_scale_delta_op); @@ -2369,8 +2349,7 @@ static Status TranslateFusedConv2DOp(const Node* op, BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); @@ -2424,8 +2403,7 @@ static Status TranslateFusedConv2DOp(const Node* op, TF_RETURN_IF_ERROR(CreateNgConv(ng_input, ng_filter, ng_conv)); - BatchToTensorflow(is_nhwc, ng_conv); - ng_conv->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_conv); auto ng_conv_shape = ng_conv->get_shape(); auto ng_bias_shape = ng_bias->get_shape(); @@ -2486,8 +2464,7 @@ static Status TranslateFusedConv2DOp(const Node* op, op->name() + "_FusedConv2D_BatchNorm", tf_epsilon, ng_scale, ng_offset, ng_conv, ng_mean, ng_variance); - BatchToTensorflow(is_nhwc, ng_batch_norm); - ng_batch_norm->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_batch_norm); if (VecStrCmp(fused_ops, {"FusedBatchNorm", "Relu"})) { SaveNgOp(ng_op_map, op->name(), @@ -2646,8 +2623,7 @@ static Status TranslateMaxPoolOp(const Node* op, BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -2666,8 +2642,7 @@ static Status TranslateMaxPoolOp(const Node* op, op->name(), ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); - BatchToTensorflow(is_nhwc, ng_maxpool); - ng_maxpool->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_maxpool); NGRAPH_VLOG(3) << "maxpool outshape: {" << ng::join(ng_maxpool->get_shape()) << "}"; @@ -2710,8 +2685,7 @@ static Status TranslateMaxPool3DOp(const Node* op, BatchedOpParam3DToNGraph(is_ndhwc, tf_strides, ng_strides); BatchedOpParam3DToNGraph(is_ndhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParam3DToNGraph(is_ndhwc, tf_ksize, ng_kernel_shape); - BatchToNGraph3D(is_ndhwc, ng_input); - ng_input->add_provenance_tag(op->name()); + BatchToNGraph3D(op->name(), is_ndhwc, ng_input); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -2730,8 +2704,7 @@ static Status TranslateMaxPool3DOp(const Node* op, op->name(), ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); - BatchToTensorflow3D(is_ndhwc, ng_maxpool); - ng_maxpool->add_provenance_tag(op->name()); + BatchToTensorflow3D(op->name(), is_ndhwc, ng_maxpool); NGRAPH_VLOG(3) << "maxpool outshape: {" << ng::join(ng_maxpool->get_shape()) << "}"; @@ -2773,12 +2746,9 @@ static Status TranslateMaxPoolGradOp(const Node* op, BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); - BatchToNGraph(is_nhwc, ng_input); - ng_input->add_provenance_tag(op->name()); - BatchToNGraph(is_nhwc, ng_grad); - ng_grad->add_provenance_tag(op->name()); - BatchToNGraph(is_nhwc, ng_fwd); - ng_fwd->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, ng_input); + BatchToNGraph(op->name(), is_nhwc, ng_grad); + BatchToNGraph(op->name(), is_nhwc, ng_fwd); NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); @@ -2794,8 +2764,7 @@ static Status TranslateMaxPoolGradOp(const Node* op, ConstructNgNode( op->name(), ng_input, ng_grad, ng_fwd, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); - BatchToTensorflow(is_nhwc, ng_maxpool_backprop); - ng_maxpool_backprop->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_maxpool_backprop); NGRAPH_VLOG(3) << "maxpoolbackprop outshape: {" << ng::join(ng_maxpool_backprop->get_shape()) << "}"; SaveNgOp(ng_op_map, op->name(), ng_maxpool_backprop); @@ -3432,12 +3401,10 @@ static Status TranslateQuantizedConv( BatchedOpParamToNGraph(is_nhwc, node_inps[0]->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); // Generally, the mapping is: 0->input, 1->filter, 2->bias, 3->sum input - BatchToNGraph(is_nhwc, node_inps[0]); - node_inps[0]->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, node_inps[0]); // QconvBiasAdd variants if (num_node_inputs == 12) { - BatchToNGraph(is_nhwc, node_inps[9]); - node_inps[9]->add_provenance_tag(op->name()); + BatchToNGraph(op->name(), is_nhwc, node_inps[9]); } auto& ng_filter_shape = node_inps[1]->get_shape(); ng_kernel_shape[0] = ng_filter_shape[0]; @@ -3459,8 +3426,7 @@ static Status TranslateQuantizedConv( ng_data_dilations); ng_quant_conv_bias->add_provenance_tag(op->name()); - BatchToTensorflow(is_nhwc, ng_quant_conv_bias); - ng_quant_conv_bias->add_provenance_tag(op->name()); + BatchToTensorflow(op->name(), is_nhwc, ng_quant_conv_bias); SaveNgOp(ng_op_map, op->name(), ng_quant_conv_bias); // QconvBiasAdd variants have summand and its min/max as the last input // nodes @@ -5160,7 +5126,7 @@ Status Builder::TranslateGraph( num_tags = n->get_provenance_tags().size(); if (num_tags != 1) { char* original_provenance_flag_value = getenv("NGRAPH_PROVENANCE_ENABLE"); - // No need to free original_provenance_flag_value + // No need to free original_provenance_flag_value sccording to the standard char enable_provenance[] = "NGRAPH_PROVENANCE_ENABLE=1"; putenv(enable_provenance); diff --git a/ngraph_bridge/ngraph_conversions.cc b/ngraph_bridge/ngraph_conversions.cc index 9e23cc31e..f371b4e11 100644 --- a/ngraph_bridge/ngraph_conversions.cc +++ b/ngraph_bridge/ngraph_conversions.cc @@ -31,31 +31,35 @@ void NdhwcToNGraph(std::shared_ptr& ng_node) { } } // namespace detail -void BatchToNGraph(bool is_nhwc, std::shared_ptr& ng_input) { +void BatchToNGraph(const string& provenance_tag, bool is_nhwc, std::shared_ptr& ng_input) { if (is_nhwc) { detail::NhwcToNGraph(ng_input); + ng_input->add_provenance_tag(provenance_tag); } } -void BatchToNGraph3D(bool is_ndhwc, std::shared_ptr& ng_input) { +void BatchToNGraph3D(const string& provenance_tag, bool is_ndhwc, std::shared_ptr& ng_input) { if (is_ndhwc) { detail::NdhwcToNGraph(ng_input); + ng_input->add_provenance_tag(provenance_tag); } } -void BatchToTensorflow(bool is_nhwc, std::shared_ptr& ng_node) { +void BatchToTensorflow(const string& provenance_tag, bool is_nhwc, std::shared_ptr& ng_node) { if (!is_nhwc) { return; } Reshape<0, 2, 3, 1>(ng_node); + ng_node->add_provenance_tag(provenance_tag); } -void BatchToTensorflow3D(bool is_ndhwc, +void BatchToTensorflow3D(const string& provenance_tag, bool is_ndhwc, std::shared_ptr& ng_node) { if (!is_ndhwc) { return; } Reshape3D<0, 2, 3, 4, 1>(ng_node); + ng_node->add_provenance_tag(provenance_tag); } } // namespace ngraph_bridge diff --git a/ngraph_bridge/ngraph_conversions.h b/ngraph_bridge/ngraph_conversions.h index a03a99dce..d30189acf 100644 --- a/ngraph_bridge/ngraph_conversions.h +++ b/ngraph_bridge/ngraph_conversions.h @@ -103,9 +103,9 @@ void NdhwcToNcdhw(const std::vector& src, std::vector& dst) { } } -void BatchToNGraph(bool is_nhwc, std::shared_ptr& ng_input); +void BatchToNGraph(const string& provenance_tag, bool is_nhwc, std::shared_ptr& ng_input); -void BatchToNGraph3D(bool is_ndhwc, std::shared_ptr& ng_input); +void BatchToNGraph3D(const string& provenance_tag, bool is_ndhwc, std::shared_ptr& ng_input); template void BatchedOpParamToNGraph(bool is_nhwc, const std::vector& src, @@ -147,9 +147,9 @@ void BatchedOpParamReshape3D(bool is_ndhwc, const std::vector& src, } } -void BatchToTensorflow(bool is_nhwc, std::shared_ptr& ng_node); +void BatchToTensorflow(const string& provenance_tag, bool is_nhwc, std::shared_ptr& ng_node); -void BatchToTensorflow3D(bool is_ndhwc, std::shared_ptr& ng_node); +void BatchToTensorflow3D(const string& provenance_tag, bool is_ndhwc, std::shared_ptr& ng_node); } // namespace ngraph_bridge } // namespace tensorflow diff --git a/test/conversions.cpp b/test/conversions.cpp index 37000009f..7a6bbe046 100644 --- a/test/conversions.cpp +++ b/test/conversions.cpp @@ -37,7 +37,7 @@ TEST(conversions, batch_to_tensorflow_nchw) { auto shape = ng::Shape{2, 3, 4, 5}; std::shared_ptr ng_node = make_shared(ng::element::f32, shape); - BatchToTensorflow(false, ng_node); + BatchToTensorflow("tag", false, ng_node); ASSERT_EQ(ng_node->get_shape(), shape); } @@ -45,7 +45,7 @@ TEST(conversions, batch_to_tensorflow_nhwc) { auto shape = ng::Shape{2, 3, 4, 5}; std::shared_ptr ng_node = make_shared(ng::element::f32, shape); - BatchToTensorflow(true, ng_node); + BatchToTensorflow("tag", true, ng_node); ASSERT_EQ(ng_node->get_shape(), (ng::Shape{2, 4, 5, 3})); } @@ -53,7 +53,7 @@ TEST(conversions, batch_to_ngraph_nchw) { auto shape = ng::Shape{2, 3, 4, 5}; std::shared_ptr ng_node = make_shared(ng::element::f32, shape); - BatchToNGraph(false, ng_node); + BatchToNGraph("tag", false, ng_node); ASSERT_EQ(ng_node->get_shape(), shape); } @@ -69,7 +69,7 @@ TEST(conversions, batch_to_ngraph_nhwc) { auto shape = ng::Shape{2, 3, 4, 5}; std::shared_ptr ng_node = make_shared(ng::element::f32, shape); - BatchToNGraph(true, ng_node); + BatchToNGraph("tag", true, ng_node); ASSERT_EQ(ng_node->get_shape(), (ng::Shape{2, 5, 3, 4})); } From 802b174951d577264d3df6dc802611b78545b177 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Tue, 1 Oct 2019 18:23:19 -0700 Subject: [PATCH 11/19] Style --- ngraph_bridge/ngraph_builder.cc | 13 ++++++++----- ngraph_bridge/ngraph_conversions.cc | 9 ++++++--- ngraph_bridge/ngraph_conversions.h | 12 ++++++++---- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index cdb799cd0..30bdbe7d8 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -387,8 +387,7 @@ static Status TranslateUnaryOp( shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); auto ng_node = create_unary_op(ng_input); - if (ng_node != ng_input) - ng_node->add_provenance_tag(op->name()); + if (ng_node != ng_input) ng_node->add_provenance_tag(op->name()); SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); } @@ -5125,8 +5124,10 @@ Status Builder::TranslateGraph( if (!check_if_result_or_parameter(n)) { num_tags = n->get_provenance_tags().size(); if (num_tags != 1) { - char* original_provenance_flag_value = getenv("NGRAPH_PROVENANCE_ENABLE"); - // No need to free original_provenance_flag_value sccording to the standard + char* original_provenance_flag_value = + getenv("NGRAPH_PROVENANCE_ENABLE"); + // No need to free original_provenance_flag_value sccording to the + // standard char enable_provenance[] = "NGRAPH_PROVENANCE_ENABLE=1"; putenv(enable_provenance); @@ -5138,7 +5139,9 @@ Status Builder::TranslateGraph( } else { string provenance_flag_original_val{original_provenance_flag_value}; char* reset = new char[26 + provenance_flag_original_val.size()]; - strcpy(reset, ("NGRAPH_PROVENANCE_ENABLE="+provenance_flag_original_val).c_str()); + strcpy(reset, + ("NGRAPH_PROVENANCE_ENABLE=" + provenance_flag_original_val) + .c_str()); putenv(reset); delete[] reset; } diff --git a/ngraph_bridge/ngraph_conversions.cc b/ngraph_bridge/ngraph_conversions.cc index f371b4e11..9d43dddb9 100644 --- a/ngraph_bridge/ngraph_conversions.cc +++ b/ngraph_bridge/ngraph_conversions.cc @@ -31,21 +31,24 @@ void NdhwcToNGraph(std::shared_ptr& ng_node) { } } // namespace detail -void BatchToNGraph(const string& provenance_tag, bool is_nhwc, std::shared_ptr& ng_input) { +void BatchToNGraph(const string& provenance_tag, bool is_nhwc, + std::shared_ptr& ng_input) { if (is_nhwc) { detail::NhwcToNGraph(ng_input); ng_input->add_provenance_tag(provenance_tag); } } -void BatchToNGraph3D(const string& provenance_tag, bool is_ndhwc, std::shared_ptr& ng_input) { +void BatchToNGraph3D(const string& provenance_tag, bool is_ndhwc, + std::shared_ptr& ng_input) { if (is_ndhwc) { detail::NdhwcToNGraph(ng_input); ng_input->add_provenance_tag(provenance_tag); } } -void BatchToTensorflow(const string& provenance_tag, bool is_nhwc, std::shared_ptr& ng_node) { +void BatchToTensorflow(const string& provenance_tag, bool is_nhwc, + std::shared_ptr& ng_node) { if (!is_nhwc) { return; } diff --git a/ngraph_bridge/ngraph_conversions.h b/ngraph_bridge/ngraph_conversions.h index d30189acf..83045eaf5 100644 --- a/ngraph_bridge/ngraph_conversions.h +++ b/ngraph_bridge/ngraph_conversions.h @@ -103,9 +103,11 @@ void NdhwcToNcdhw(const std::vector& src, std::vector& dst) { } } -void BatchToNGraph(const string& provenance_tag, bool is_nhwc, std::shared_ptr& ng_input); +void BatchToNGraph(const string& provenance_tag, bool is_nhwc, + std::shared_ptr& ng_input); -void BatchToNGraph3D(const string& provenance_tag, bool is_ndhwc, std::shared_ptr& ng_input); +void BatchToNGraph3D(const string& provenance_tag, bool is_ndhwc, + std::shared_ptr& ng_input); template void BatchedOpParamToNGraph(bool is_nhwc, const std::vector& src, @@ -147,9 +149,11 @@ void BatchedOpParamReshape3D(bool is_ndhwc, const std::vector& src, } } -void BatchToTensorflow(const string& provenance_tag, bool is_nhwc, std::shared_ptr& ng_node); +void BatchToTensorflow(const string& provenance_tag, bool is_nhwc, + std::shared_ptr& ng_node); -void BatchToTensorflow3D(const string& provenance_tag, bool is_ndhwc, std::shared_ptr& ng_node); +void BatchToTensorflow3D(const string& provenance_tag, bool is_ndhwc, + std::shared_ptr& ng_node); } // namespace ngraph_bridge } // namespace tensorflow From 0306b2f1195f08f98f75a03c134bd99a8444cb6b Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Tue, 1 Oct 2019 18:26:39 -0700 Subject: [PATCH 12/19] Update ngcore to 0.25.1-rc8 --- CMakeLists.txt | 2 +- bazel/WORKSPACE | 8 ++++---- bazel/ngraph.BUILD | 4 ++-- build_ngtf.py | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fbb5232bb..15d4896db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -248,7 +248,7 @@ if (NOT USE_PRE_BUILT_NGRAPH) ExternalProject_Add( ext_ngraph GIT_REPOSITORY https://github.com/NervanaSystems/ngraph - GIT_TAG v0.25.1-rc.7 + GIT_TAG v0.25.1-rc.8 CMAKE_ARGS -DNGRAPH_DISTRIBUTED_ENABLE=${NGRAPH_DISTRIBUTED_ENABLE} -DNGRAPH_INSTALL_PREFIX=${NGRAPH_ARTIFACTS_DIR} diff --git a/bazel/WORKSPACE b/bazel/WORKSPACE index 8e9341462..0a26e0fa9 100644 --- a/bazel/WORKSPACE +++ b/bazel/WORKSPACE @@ -55,11 +55,11 @@ tf_workspace(path_prefix = "", tf_repo_name = "org_tensorflow") http_archive( name = "ngraph", build_file = "//:bazel/ngraph.BUILD", - sha256 = "345f0566c3ae4a968daea116c26ce9a90a94305148d6b8b56b6309448432492e", - strip_prefix = "ngraph-0.25.1-rc.7", + sha256 = "4cd939dcf07544dc71f46f422d82a175b210fe7b21417fa67436168d5c40c9dc", + strip_prefix = "ngraph-0.25.1-rc.8", urls = [ - "https://mirror.bazel.build/github.com/NervanaSystems/ngraph/archive/v0.25.1-rc.7.tar.gz", - "https://github.com/NervanaSystems/ngraph/archive/v0.25.1-rc.7.tar.gz" + "https://mirror.bazel.build/github.com/NervanaSystems/ngraph/archive/v0.25.1-rc.8.tar.gz", + "https://github.com/NervanaSystems/ngraph/archive/v0.25.1-rc.8.tar.gz" ], ) diff --git a/bazel/ngraph.BUILD b/bazel/ngraph.BUILD index 0b5b7aa84..67b6238cc 100644 --- a/bazel/ngraph.BUILD +++ b/bazel/ngraph.BUILD @@ -84,7 +84,7 @@ cc_library( "-fstack-protector-all", '-D SHARED_LIB_PREFIX=\\"lib\\"', '-D SHARED_LIB_SUFFIX=\\".so\\"', - '-D NGRAPH_VERSION=\\"v0.25.1-rc.7\\"', + '-D NGRAPH_VERSION=\\"v0.25.1-rc.8\\"', "-D NGRAPH_DEX_ONLY", '-D PROJECT_ROOT_DIR=\\"\\"', '-D NGRAPH_STATIC_LIB_ENABLE' @@ -117,7 +117,7 @@ cc_library( "-fstack-protector-all", '-D SHARED_LIB_PREFIX=\\"lib\\"', '-D SHARED_LIB_SUFFIX=\\".so\\"', - '-D NGRAPH_VERSION=\\"v0.25.1-rc.7\\"', + '-D NGRAPH_VERSION=\\"v0.25.1-rc.8\\"', "-D NGRAPH_DEX_ONLY", '-D PROJECT_ROOT_DIR=\\"\\"', ] + CXX_ABI, diff --git a/build_ngtf.py b/build_ngtf.py index f649dd326..9a9f46cee 100755 --- a/build_ngtf.py +++ b/build_ngtf.py @@ -53,7 +53,7 @@ def main(): ''' # Component versions - ngraph_version = "v0.25.1-rc.7" + ngraph_version = "v0.25.1-rc.8" tf_version = "v1.14.0" # Command line parser options From 145b24268811ef803133dd79c3ed78753b2cc266 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Wed, 2 Oct 2019 14:07:37 -0700 Subject: [PATCH 13/19] Add prov tags to nodes created by backend (non-core ops) --- ngraph_bridge/ngraph_builder.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index 30bdbe7d8..660d54909 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -1132,6 +1132,7 @@ static Status TranslateCombinedNonMaxSuppressionOp( op->name(), " backend could not return valid ngraph node"); } + ng_cnms->add_provenance_tag(op->name()); shared_ptr ng_nmsed_boxes = ConstructNgNode(op->name(), ng_cnms, 0); shared_ptr ng_nmsed_scores = @@ -2294,6 +2295,7 @@ static Status TranslateGatherV2Op( return errors::Internal("In translating GatherV2 op ", op->name(), " backend could not return valid ngraph node"); } + ng_gather->add_provenance_tag(op->name()); SaveNgOp(ng_op_map, op->name(), ng_gather); return Status::OK(); @@ -2834,6 +2836,7 @@ static Status TranslateNonMaxSuppressionV4Op( op->name(), " backend could not return valid ngraph node"); } + ng_nmsv4->add_provenance_tag(op->name()); shared_ptr ng_selected_indices = ConstructNgNode(op->name(), ng_nmsv4, 0); shared_ptr ng_valid_output = From 64c0d92c587dda3ac91c8277759cc004b185539f Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Thu, 3 Oct 2019 12:17:55 -0700 Subject: [PATCH 14/19] Add SetTracingInfo function --- ngraph_bridge/ngraph_builder.cc | 92 +++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index 660d54909..d5d23528c 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -92,15 +92,20 @@ static void SaveNgOp(Builder::OpMap& ng_op_map, const std::string& op_name, ng_op_map[op_name].push_back(output_node); } -template -std::shared_ptr ConstructNgNode(const std::string& op_name, - TArg&&... Args) { - auto ng_node = std::make_shared(std::forward(Args)...); +void SetTracingInfo(const std::string& op_name, + const shared_ptr ng_node) { ng_node->set_friendly_name(op_name); ng_node->add_provenance_tag(op_name); if (config::IsLoggingPlacement()) { cout << "TF_to_NG: " << op_name << " --> " << ng_node->get_name() << "\n"; } +} + +template +std::shared_ptr ConstructNgNode(const std::string& op_name, + TArg&&... Args) { + auto ng_node = std::make_shared(std::forward(Args)...); + SetTracingInfo(op_name, ng_node); return ng_node; } @@ -352,8 +357,12 @@ PerformNgBroadcast(const string& prov_tag, std::shared_ptr ng_lhs, std::shared_ptr ng_lhs_new, ng_rhs_new; std::tie(ng_lhs_new, ng_rhs_new) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); - if (ng_lhs_new != ng_lhs) ng_lhs_new->add_provenance_tag(prov_tag); - if (ng_rhs_new != ng_rhs) ng_rhs_new->add_provenance_tag(prov_tag); + if (ng_lhs_new != ng_lhs) { + SetTracingInfo(prov_tag, ng_lhs_new); + } + if (ng_rhs_new != ng_rhs) { + SetTracingInfo(prov_tag, ng_rhs_new); + } return make_pair(ng_lhs_new, ng_rhs_new); } @@ -387,7 +396,9 @@ static Status TranslateUnaryOp( shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); auto ng_node = create_unary_op(ng_input); - if (ng_node != ng_input) ng_node->add_provenance_tag(op->name()); + if (ng_node != ng_input) { + SetTracingInfo(op->name(), ng_node); + } SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); } @@ -448,8 +459,9 @@ static Status TranslateBinaryOp( std::tie(ng_lhs, ng_rhs) = PerformNgBroadcast(op->name(), ng_lhs, ng_rhs); auto ng_node = create_binary_op(ng_lhs, ng_rhs); - if (ng_node != ng_lhs && ng_node != ng_rhs) - ng_node->add_provenance_tag(op->name()); + if (ng_node != ng_lhs && ng_node != ng_rhs) { + SetTracingInfo(op->name(), ng_node); + } SaveNgOp(ng_op_map, op->name(), ng_node); @@ -541,7 +553,7 @@ static Status TranslateQuantizedPoolOp(const Node* op, ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above, dummy_min, dummy_max); } - ng_quant_pool->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_quant_pool); BatchToTensorflow(op->name(), is_nhwc, ng_quant_pool); SaveNgOp(ng_op_map, op->name(), ng_quant_pool); @@ -801,7 +813,7 @@ static Status TranslateBatchMatMulOp( ng_lhs_axes.push_back(n_dims - 1); ng_lhs_axes.push_back(n_dims - 2); ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng_lhs_axes); - ng_lhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_lhs); ng_lhs_shape = ng_lhs->get_shape(); } else { ng_lhs_axes.push_back(n_dims - 2); @@ -812,7 +824,7 @@ static Status TranslateBatchMatMulOp( ng_rhs_axes.push_back(n_dims - 1); ng_rhs_axes.push_back(n_dims - 2); ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng_rhs_axes); - ng_rhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_rhs); ng_rhs_shape = ng_rhs->get_shape(); } else { ng_rhs_axes.push_back(n_dims - 2); @@ -856,18 +868,18 @@ static Status TranslateBatchMatMulOp( ng_lhs_axes.push_back(n_dims - 1); ng_lhs_axes.push_back(n_dims - 2); ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng_lhs_axes); - ng_lhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_lhs); } if (tf_adj_y) { ng_rhs_axes.insert(ng_rhs_axes.begin(), n_dims - 2); ng_rhs_axes.insert(ng_rhs_axes.begin(), n_dims - 1); ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng_rhs_axes); - ng_rhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_rhs); } else { ng_rhs_axes.insert(ng_rhs_axes.begin(), n_dims - 1); ng_rhs_axes.insert(ng_rhs_axes.begin(), n_dims - 2); ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng_rhs_axes); - ng_rhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_rhs); } ng_lhs_shape = ng_lhs->get_shape(); @@ -1132,7 +1144,7 @@ static Status TranslateCombinedNonMaxSuppressionOp( op->name(), " backend could not return valid ngraph node"); } - ng_cnms->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_cnms); shared_ptr ng_nmsed_boxes = ConstructNgNode(op->name(), ng_cnms, 0); shared_ptr ng_nmsed_scores = @@ -1263,7 +1275,7 @@ static Status TranslateConv2DOp(const Node* op, ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - ng_filter->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1399,7 +1411,7 @@ static Status TranslateConv2DBackpropFilterOp( // Reshape the output to tf format : [filter_height, filter_width, // in_channels, out_channels] Reshape<2, 3, 1, 0>(ng_back_prop_filter); - ng_back_prop_filter->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_back_prop_filter); SaveNgOp(ng_op_map, op->name(), ng_back_prop_filter); return Status::OK(); @@ -1475,7 +1487,7 @@ static Status TranslateConv2DBackpropInputOp( ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - ng_filter->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1554,7 +1566,7 @@ static Status TranslateConv3DOp(const Node* op, ng_kernel_shape[1] = ng_filter_shape[1]; ng_kernel_shape[2] = ng_filter_shape[2]; Reshape3D<4, 3, 0, 1, 2>(ng_filter); - ng_filter->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1723,7 +1735,7 @@ static Status TranslateDepthToSpaceOp(const Node* op, auto transposed = ng::builder::numpy_transpose(reshaped, ng_transpose_permutation); - transposed->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), transposed); ng::AxisVector ng_axis_order_second_reshape(transposed->get_shape().size()); std::iota(ng_axis_order_second_reshape.begin(), @@ -1780,7 +1792,7 @@ static Status TranslateDepthwiseConv2dNativeOp( ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - ng_filter->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -2177,12 +2189,12 @@ static Status TranslateFusedMatMulOp(const Node* op, if (GetNodeAttr(op->attrs(), "transpose_a", &transpose_a) == Status::OK() && transpose_a) { ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng::AxisVector{1, 0}); - ng_lhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_lhs); } if (GetNodeAttr(op->attrs(), "transpose_b", &transpose_b) == Status::OK() && transpose_b) { ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng::AxisVector{1, 0}); - ng_rhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_rhs); } // The default axis count for nGraph's Dot op is 1, which is just what @@ -2295,7 +2307,7 @@ static Status TranslateGatherV2Op( return errors::Internal("In translating GatherV2 op ", op->name(), " backend could not return valid ngraph node"); } - ng_gather->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_gather); SaveNgOp(ng_op_map, op->name(), ng_gather); return Status::OK(); @@ -2360,7 +2372,7 @@ static Status TranslateFusedConv2DOp(const Node* op, ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - ng_filter->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -2575,12 +2587,12 @@ static Status TranslateMatMulOp(const Node* op, if (GetNodeAttr(op->attrs(), "transpose_a", &transpose_a) == Status::OK() && transpose_a) { ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng::AxisVector{1, 0}); - ng_lhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_lhs); } if (GetNodeAttr(op->attrs(), "transpose_b", &transpose_b) == Status::OK() && transpose_b) { ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng::AxisVector{1, 0}); - ng_rhs->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_rhs); } // The default axis count for nGraph's Dot op is 1, which is just what @@ -2836,7 +2848,7 @@ static Status TranslateNonMaxSuppressionV4Op( op->name(), " backend could not return valid ngraph node"); } - ng_nmsv4->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_nmsv4); shared_ptr ng_selected_indices = ConstructNgNode(op->name(), ng_nmsv4, 0); shared_ptr ng_valid_output = @@ -2877,7 +2889,7 @@ static Status TranslateReduceOp( std::shared_ptr ng_node = create_ng_node(ng_input, ng_reduction_axes); - ng_node->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_node); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. @@ -2909,7 +2921,7 @@ static Status TranslateMeanOp( ng::AxisSet ng_reduction_axes) { auto mean_node = ng::builder::mean(ng_input, ng_reduction_axes); - mean_node->add_provenance_tag(op_name); + SetTracingInfo(op_name, mean_node); return mean_node; }); } @@ -3351,7 +3363,7 @@ static Status TranslateQuantizedConcatOpHelper( auto ng_qconcat = ng::builder::ScaledQuantizedConcat( ng_args, size_t(concat_axis), ng_all_mins, ng_all_maxs); - ng_qconcat->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_qconcat); SaveNgOp(ng_op_map, op->name(), ng_qconcat); SaveNgOp(ng_op_map, op->name(), ng_min_of_mins); @@ -3412,7 +3424,7 @@ static Status TranslateQuantizedConv( ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(node_inps[1]); - node_inps[1]->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), node_inps[1]); ng::CoordinateDiff ng_padding_below{0, 0}; ng::CoordinateDiff ng_padding_above{0, 0}; Builder::MakePadding(tf_padding_type, ng_image_shape, ng_kernel_shape, @@ -3426,7 +3438,7 @@ static Status TranslateQuantizedConv( std::shared_ptr ng_quant_conv_bias = create_quantized_conv_node( node_inps, ng_strides, ng_dilations, ng_padding_below, ng_padding_above, ng_data_dilations); - ng_quant_conv_bias->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_quant_conv_bias); BatchToTensorflow(op->name(), is_nhwc, ng_quant_conv_bias); SaveNgOp(ng_op_map, op->name(), ng_quant_conv_bias); @@ -3454,7 +3466,7 @@ static Status TranslateQuantizedConv2DWithBiasMaybeReluAndRequantizeOp( ng_padding_below, ng_padding_above, ng_data_dilations, node_inps[3], node_inps[4], node_inps[5], node_inps[6], node_inps[7], node_inps[8], IsRelu); - ng_node->add_provenance_tag(op_name); + SetTracingInfo(op_name, ng_node); return ng_node; }; return TranslateQuantizedConv(op, ng_op_map, create_quantized_conv_node); @@ -3473,7 +3485,7 @@ static Status TranslateQuantizedConv2DWithBiasSumAndReluAndRequantizeOp( ng_dilations, ng_padding_below, ng_padding_above, ng_data_dilations, node_inps[3], node_inps[4], node_inps[5], node_inps[6], node_inps[7], node_inps[8], node_inps[10], node_inps[11], true); - ng_node->add_provenance_tag(op_name); + SetTracingInfo(op_name, ng_node); return ng_node; }; return TranslateQuantizedConv(op, ng_op_map, create_quantized_conv_node); @@ -3492,7 +3504,7 @@ static Status TranslateQuantizedConv2DWithBiasSignedSumAndReluAndRequantizeOp( ng_dilations, ng_padding_below, ng_padding_above, ng_data_dilations, node_inps[3], node_inps[4], node_inps[5], node_inps[6], node_inps[7], node_inps[8], node_inps[10], node_inps[11], true); - ng_node->add_provenance_tag(op_name); + SetTracingInfo(op_name, ng_node); return ng_node; }; return TranslateQuantizedConv(op, ng_op_map, create_quantized_conv_node); @@ -3524,7 +3536,7 @@ static Status TranslateQuantizeV2Op(const Node* op, auto ng_node = ng::builder::ScaledQuantize(ng_input, ng_min, ng_max, ng_et, ng::AxisSet(), ng_round_mode); - ng_node->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_min); SaveNgOp(ng_op_map, op->name(), ng_max); @@ -3541,7 +3553,7 @@ static Status TranslateDequantizeOp(const Node* op, // TF only dequantizes to fp32 auto ng_node = ng::builder::ScaledDequantize(ng_input, ng_min, ng_max, ng::element::f32, ng::AxisSet()); - ng_node->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); } @@ -4716,7 +4728,7 @@ static Status TranslateTransposeOp( NGRAPH_VLOG(3) << ng::join(ng_axis_order); auto ng_node = ng::builder::numpy_transpose(ng_input, ng_axis_order); - ng_node->add_provenance_tag(op->name()); + SetTracingInfo(op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); } From 8f4a238be0fff25e07615ac3dd2fa8780f01c2b4 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Thu, 3 Oct 2019 12:30:00 -0700 Subject: [PATCH 15/19] Add comments for SetTracingInfo and add code in conversions. Also fix full header path in conversions.cc --- ngraph_bridge/ngraph_builder.cc | 8 +++++++ ngraph_bridge/ngraph_conversions.cc | 37 ++++++++++++++++++++++------- ngraph_bridge/ngraph_conversions.h | 8 +++---- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index d5d23528c..1137c3410 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -94,6 +94,14 @@ static void SaveNgOp(Builder::OpMap& ng_op_map, const std::string& op_name, void SetTracingInfo(const std::string& op_name, const shared_ptr ng_node) { + // This function is used to trace whng node came from which tf node + // It does 3 things: + // 1. Attaches provenance tags. This is guaranteed to propagate the tag info + // to all nodes. + // The next 2 are not guaranteed to be present for all nodes. + // But when present they are correct and agree with provenance tags + // 2. Attaches friendly names. + // 3. Prints a log if NGRAPH_TF_LOG_PLACEMENT=1 ng_node->set_friendly_name(op_name); ng_node->add_provenance_tag(op_name); if (config::IsLoggingPlacement()) { diff --git a/ngraph_bridge/ngraph_conversions.cc b/ngraph_bridge/ngraph_conversions.cc index 9d43dddb9..0d503bcee 100644 --- a/ngraph_bridge/ngraph_conversions.cc +++ b/ngraph_bridge/ngraph_conversions.cc @@ -14,7 +14,8 @@ * limitations under the License. *******************************************************************************/ -#include "ngraph_conversions.h" +#include "ngraph_bridge/ngraph_conversions.h" +#include "ngraph_bridge/ngraph_api.h" namespace tensorflow { @@ -31,38 +32,56 @@ void NdhwcToNGraph(std::shared_ptr& ng_node) { } } // namespace detail -void BatchToNGraph(const string& provenance_tag, bool is_nhwc, +void BatchToNGraph(const string& op_name, bool is_nhwc, std::shared_ptr& ng_input) { if (is_nhwc) { detail::NhwcToNGraph(ng_input); - ng_input->add_provenance_tag(provenance_tag); + ng_input->set_friendly_name(op_name); + ng_input->add_provenance_tag(op_name); + if (config::IsLoggingPlacement()) { + cout << "TF_to_NG: " << op_name << " --> " << ng_input->get_name() + << "\n"; + } } } -void BatchToNGraph3D(const string& provenance_tag, bool is_ndhwc, +void BatchToNGraph3D(const string& op_name, bool is_ndhwc, std::shared_ptr& ng_input) { if (is_ndhwc) { detail::NdhwcToNGraph(ng_input); - ng_input->add_provenance_tag(provenance_tag); + ng_input->set_friendly_name(op_name); + ng_input->add_provenance_tag(op_name); + if (config::IsLoggingPlacement()) { + cout << "TF_to_NG: " << op_name << " --> " << ng_input->get_name() + << "\n"; + } } } -void BatchToTensorflow(const string& provenance_tag, bool is_nhwc, +void BatchToTensorflow(const string& op_name, bool is_nhwc, std::shared_ptr& ng_node) { if (!is_nhwc) { return; } Reshape<0, 2, 3, 1>(ng_node); - ng_node->add_provenance_tag(provenance_tag); + ng_node->set_friendly_name(op_name); + ng_node->add_provenance_tag(op_name); + if (config::IsLoggingPlacement()) { + cout << "TF_to_NG: " << op_name << " --> " << ng_node->get_name() << "\n"; + } } -void BatchToTensorflow3D(const string& provenance_tag, bool is_ndhwc, +void BatchToTensorflow3D(const string& op_name, bool is_ndhwc, std::shared_ptr& ng_node) { if (!is_ndhwc) { return; } Reshape3D<0, 2, 3, 4, 1>(ng_node); - ng_node->add_provenance_tag(provenance_tag); + ng_node->set_friendly_name(op_name); + ng_node->add_provenance_tag(op_name); + if (config::IsLoggingPlacement()) { + cout << "TF_to_NG: " << op_name << " --> " << ng_node->get_name() << "\n"; + } } } // namespace ngraph_bridge diff --git a/ngraph_bridge/ngraph_conversions.h b/ngraph_bridge/ngraph_conversions.h index 83045eaf5..c829391c4 100644 --- a/ngraph_bridge/ngraph_conversions.h +++ b/ngraph_bridge/ngraph_conversions.h @@ -103,10 +103,10 @@ void NdhwcToNcdhw(const std::vector& src, std::vector& dst) { } } -void BatchToNGraph(const string& provenance_tag, bool is_nhwc, +void BatchToNGraph(const string& op_name, bool is_nhwc, std::shared_ptr& ng_input); -void BatchToNGraph3D(const string& provenance_tag, bool is_ndhwc, +void BatchToNGraph3D(const string& op_name, bool is_ndhwc, std::shared_ptr& ng_input); template @@ -149,10 +149,10 @@ void BatchedOpParamReshape3D(bool is_ndhwc, const std::vector& src, } } -void BatchToTensorflow(const string& provenance_tag, bool is_nhwc, +void BatchToTensorflow(const string& op_name, bool is_nhwc, std::shared_ptr& ng_node); -void BatchToTensorflow3D(const string& provenance_tag, bool is_ndhwc, +void BatchToTensorflow3D(const string& op_name, bool is_ndhwc, std::shared_ptr& ng_node); } // namespace ngraph_bridge From d60a9888c9e082d98c81db48273a1e5302cd79b5 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Thu, 3 Oct 2019 13:37:33 -0700 Subject: [PATCH 16/19] Move SetTracingInfo to builder.h --- ngraph_bridge/ngraph_builder.cc | 87 ++++++++++++++--------------- ngraph_bridge/ngraph_builder.h | 11 ++++ ngraph_bridge/ngraph_conversions.cc | 27 ++------- 3 files changed, 56 insertions(+), 69 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index 1137c3410..d09e4e88b 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -92,16 +92,8 @@ static void SaveNgOp(Builder::OpMap& ng_op_map, const std::string& op_name, ng_op_map[op_name].push_back(output_node); } -void SetTracingInfo(const std::string& op_name, - const shared_ptr ng_node) { - // This function is used to trace whng node came from which tf node - // It does 3 things: - // 1. Attaches provenance tags. This is guaranteed to propagate the tag info - // to all nodes. - // The next 2 are not guaranteed to be present for all nodes. - // But when present they are correct and agree with provenance tags - // 2. Attaches friendly names. - // 3. Prints a log if NGRAPH_TF_LOG_PLACEMENT=1 +void Builder::SetTracingInfo(const std::string& op_name, + const shared_ptr ng_node) { ng_node->set_friendly_name(op_name); ng_node->add_provenance_tag(op_name); if (config::IsLoggingPlacement()) { @@ -113,7 +105,7 @@ template std::shared_ptr ConstructNgNode(const std::string& op_name, TArg&&... Args) { auto ng_node = std::make_shared(std::forward(Args)...); - SetTracingInfo(op_name, ng_node); + Builder::SetTracingInfo(op_name, ng_node); return ng_node; } @@ -362,14 +354,17 @@ Builder::TF_NGRAPH_CONST_MAP() { static std::pair, std::shared_ptr> PerformNgBroadcast(const string& prov_tag, std::shared_ptr ng_lhs, std::shared_ptr ng_rhs) { + // builder::numpy_broadcast is the only known builder that has the possibility + // that the output node is same as the input node + // So we take special care to check, before calling SetTracingInfo std::shared_ptr ng_lhs_new, ng_rhs_new; std::tie(ng_lhs_new, ng_rhs_new) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); if (ng_lhs_new != ng_lhs) { - SetTracingInfo(prov_tag, ng_lhs_new); + Builder::SetTracingInfo(prov_tag, ng_lhs_new); } if (ng_rhs_new != ng_rhs) { - SetTracingInfo(prov_tag, ng_rhs_new); + Builder::SetTracingInfo(prov_tag, ng_rhs_new); } return make_pair(ng_lhs_new, ng_rhs_new); } @@ -405,7 +400,7 @@ static Status TranslateUnaryOp( TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); auto ng_node = create_unary_op(ng_input); if (ng_node != ng_input) { - SetTracingInfo(op->name(), ng_node); + Builder::SetTracingInfo(op->name(), ng_node); } SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); @@ -468,7 +463,7 @@ static Status TranslateBinaryOp( auto ng_node = create_binary_op(ng_lhs, ng_rhs); if (ng_node != ng_lhs && ng_node != ng_rhs) { - SetTracingInfo(op->name(), ng_node); + Builder::SetTracingInfo(op->name(), ng_node); } SaveNgOp(ng_op_map, op->name(), ng_node); @@ -561,7 +556,7 @@ static Status TranslateQuantizedPoolOp(const Node* op, ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above, dummy_min, dummy_max); } - SetTracingInfo(op->name(), ng_quant_pool); + Builder::SetTracingInfo(op->name(), ng_quant_pool); BatchToTensorflow(op->name(), is_nhwc, ng_quant_pool); SaveNgOp(ng_op_map, op->name(), ng_quant_pool); @@ -821,7 +816,7 @@ static Status TranslateBatchMatMulOp( ng_lhs_axes.push_back(n_dims - 1); ng_lhs_axes.push_back(n_dims - 2); ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng_lhs_axes); - SetTracingInfo(op->name(), ng_lhs); + Builder::SetTracingInfo(op->name(), ng_lhs); ng_lhs_shape = ng_lhs->get_shape(); } else { ng_lhs_axes.push_back(n_dims - 2); @@ -832,7 +827,7 @@ static Status TranslateBatchMatMulOp( ng_rhs_axes.push_back(n_dims - 1); ng_rhs_axes.push_back(n_dims - 2); ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng_rhs_axes); - SetTracingInfo(op->name(), ng_rhs); + Builder::SetTracingInfo(op->name(), ng_rhs); ng_rhs_shape = ng_rhs->get_shape(); } else { ng_rhs_axes.push_back(n_dims - 2); @@ -876,18 +871,18 @@ static Status TranslateBatchMatMulOp( ng_lhs_axes.push_back(n_dims - 1); ng_lhs_axes.push_back(n_dims - 2); ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng_lhs_axes); - SetTracingInfo(op->name(), ng_lhs); + Builder::SetTracingInfo(op->name(), ng_lhs); } if (tf_adj_y) { ng_rhs_axes.insert(ng_rhs_axes.begin(), n_dims - 2); ng_rhs_axes.insert(ng_rhs_axes.begin(), n_dims - 1); ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng_rhs_axes); - SetTracingInfo(op->name(), ng_rhs); + Builder::SetTracingInfo(op->name(), ng_rhs); } else { ng_rhs_axes.insert(ng_rhs_axes.begin(), n_dims - 1); ng_rhs_axes.insert(ng_rhs_axes.begin(), n_dims - 2); ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng_rhs_axes); - SetTracingInfo(op->name(), ng_rhs); + Builder::SetTracingInfo(op->name(), ng_rhs); } ng_lhs_shape = ng_lhs->get_shape(); @@ -1152,7 +1147,7 @@ static Status TranslateCombinedNonMaxSuppressionOp( op->name(), " backend could not return valid ngraph node"); } - SetTracingInfo(op->name(), ng_cnms); + Builder::SetTracingInfo(op->name(), ng_cnms); shared_ptr ng_nmsed_boxes = ConstructNgNode(op->name(), ng_cnms, 0); shared_ptr ng_nmsed_scores = @@ -1283,7 +1278,7 @@ static Status TranslateConv2DOp(const Node* op, ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - SetTracingInfo(op->name(), ng_filter); + Builder::SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1419,7 +1414,7 @@ static Status TranslateConv2DBackpropFilterOp( // Reshape the output to tf format : [filter_height, filter_width, // in_channels, out_channels] Reshape<2, 3, 1, 0>(ng_back_prop_filter); - SetTracingInfo(op->name(), ng_back_prop_filter); + Builder::SetTracingInfo(op->name(), ng_back_prop_filter); SaveNgOp(ng_op_map, op->name(), ng_back_prop_filter); return Status::OK(); @@ -1495,7 +1490,7 @@ static Status TranslateConv2DBackpropInputOp( ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - SetTracingInfo(op->name(), ng_filter); + Builder::SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1574,7 +1569,7 @@ static Status TranslateConv3DOp(const Node* op, ng_kernel_shape[1] = ng_filter_shape[1]; ng_kernel_shape[2] = ng_filter_shape[2]; Reshape3D<4, 3, 0, 1, 2>(ng_filter); - SetTracingInfo(op->name(), ng_filter); + Builder::SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1743,7 +1738,7 @@ static Status TranslateDepthToSpaceOp(const Node* op, auto transposed = ng::builder::numpy_transpose(reshaped, ng_transpose_permutation); - SetTracingInfo(op->name(), transposed); + Builder::SetTracingInfo(op->name(), transposed); ng::AxisVector ng_axis_order_second_reshape(transposed->get_shape().size()); std::iota(ng_axis_order_second_reshape.begin(), @@ -1800,7 +1795,7 @@ static Status TranslateDepthwiseConv2dNativeOp( ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - SetTracingInfo(op->name(), ng_filter); + Builder::SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -2197,12 +2192,12 @@ static Status TranslateFusedMatMulOp(const Node* op, if (GetNodeAttr(op->attrs(), "transpose_a", &transpose_a) == Status::OK() && transpose_a) { ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng::AxisVector{1, 0}); - SetTracingInfo(op->name(), ng_lhs); + Builder::SetTracingInfo(op->name(), ng_lhs); } if (GetNodeAttr(op->attrs(), "transpose_b", &transpose_b) == Status::OK() && transpose_b) { ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng::AxisVector{1, 0}); - SetTracingInfo(op->name(), ng_rhs); + Builder::SetTracingInfo(op->name(), ng_rhs); } // The default axis count for nGraph's Dot op is 1, which is just what @@ -2315,7 +2310,7 @@ static Status TranslateGatherV2Op( return errors::Internal("In translating GatherV2 op ", op->name(), " backend could not return valid ngraph node"); } - SetTracingInfo(op->name(), ng_gather); + Builder::SetTracingInfo(op->name(), ng_gather); SaveNgOp(ng_op_map, op->name(), ng_gather); return Status::OK(); @@ -2380,7 +2375,7 @@ static Status TranslateFusedConv2DOp(const Node* op, ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - SetTracingInfo(op->name(), ng_filter); + Builder::SetTracingInfo(op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -2595,12 +2590,12 @@ static Status TranslateMatMulOp(const Node* op, if (GetNodeAttr(op->attrs(), "transpose_a", &transpose_a) == Status::OK() && transpose_a) { ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng::AxisVector{1, 0}); - SetTracingInfo(op->name(), ng_lhs); + Builder::SetTracingInfo(op->name(), ng_lhs); } if (GetNodeAttr(op->attrs(), "transpose_b", &transpose_b) == Status::OK() && transpose_b) { ng_rhs = ng::builder::numpy_transpose(ng_rhs, ng::AxisVector{1, 0}); - SetTracingInfo(op->name(), ng_rhs); + Builder::SetTracingInfo(op->name(), ng_rhs); } // The default axis count for nGraph's Dot op is 1, which is just what @@ -2856,7 +2851,7 @@ static Status TranslateNonMaxSuppressionV4Op( op->name(), " backend could not return valid ngraph node"); } - SetTracingInfo(op->name(), ng_nmsv4); + Builder::SetTracingInfo(op->name(), ng_nmsv4); shared_ptr ng_selected_indices = ConstructNgNode(op->name(), ng_nmsv4, 0); shared_ptr ng_valid_output = @@ -2897,7 +2892,7 @@ static Status TranslateReduceOp( std::shared_ptr ng_node = create_ng_node(ng_input, ng_reduction_axes); - SetTracingInfo(op->name(), ng_node); + Builder::SetTracingInfo(op->name(), ng_node); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. @@ -2929,7 +2924,7 @@ static Status TranslateMeanOp( ng::AxisSet ng_reduction_axes) { auto mean_node = ng::builder::mean(ng_input, ng_reduction_axes); - SetTracingInfo(op_name, mean_node); + Builder::SetTracingInfo(op_name, mean_node); return mean_node; }); } @@ -3371,7 +3366,7 @@ static Status TranslateQuantizedConcatOpHelper( auto ng_qconcat = ng::builder::ScaledQuantizedConcat( ng_args, size_t(concat_axis), ng_all_mins, ng_all_maxs); - SetTracingInfo(op->name(), ng_qconcat); + Builder::SetTracingInfo(op->name(), ng_qconcat); SaveNgOp(ng_op_map, op->name(), ng_qconcat); SaveNgOp(ng_op_map, op->name(), ng_min_of_mins); @@ -3432,7 +3427,7 @@ static Status TranslateQuantizedConv( ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(node_inps[1]); - SetTracingInfo(op->name(), node_inps[1]); + Builder::SetTracingInfo(op->name(), node_inps[1]); ng::CoordinateDiff ng_padding_below{0, 0}; ng::CoordinateDiff ng_padding_above{0, 0}; Builder::MakePadding(tf_padding_type, ng_image_shape, ng_kernel_shape, @@ -3446,7 +3441,7 @@ static Status TranslateQuantizedConv( std::shared_ptr ng_quant_conv_bias = create_quantized_conv_node( node_inps, ng_strides, ng_dilations, ng_padding_below, ng_padding_above, ng_data_dilations); - SetTracingInfo(op->name(), ng_quant_conv_bias); + Builder::SetTracingInfo(op->name(), ng_quant_conv_bias); BatchToTensorflow(op->name(), is_nhwc, ng_quant_conv_bias); SaveNgOp(ng_op_map, op->name(), ng_quant_conv_bias); @@ -3474,7 +3469,7 @@ static Status TranslateQuantizedConv2DWithBiasMaybeReluAndRequantizeOp( ng_padding_below, ng_padding_above, ng_data_dilations, node_inps[3], node_inps[4], node_inps[5], node_inps[6], node_inps[7], node_inps[8], IsRelu); - SetTracingInfo(op_name, ng_node); + Builder::SetTracingInfo(op_name, ng_node); return ng_node; }; return TranslateQuantizedConv(op, ng_op_map, create_quantized_conv_node); @@ -3493,7 +3488,7 @@ static Status TranslateQuantizedConv2DWithBiasSumAndReluAndRequantizeOp( ng_dilations, ng_padding_below, ng_padding_above, ng_data_dilations, node_inps[3], node_inps[4], node_inps[5], node_inps[6], node_inps[7], node_inps[8], node_inps[10], node_inps[11], true); - SetTracingInfo(op_name, ng_node); + Builder::SetTracingInfo(op_name, ng_node); return ng_node; }; return TranslateQuantizedConv(op, ng_op_map, create_quantized_conv_node); @@ -3512,7 +3507,7 @@ static Status TranslateQuantizedConv2DWithBiasSignedSumAndReluAndRequantizeOp( ng_dilations, ng_padding_below, ng_padding_above, ng_data_dilations, node_inps[3], node_inps[4], node_inps[5], node_inps[6], node_inps[7], node_inps[8], node_inps[10], node_inps[11], true); - SetTracingInfo(op_name, ng_node); + Builder::SetTracingInfo(op_name, ng_node); return ng_node; }; return TranslateQuantizedConv(op, ng_op_map, create_quantized_conv_node); @@ -3544,7 +3539,7 @@ static Status TranslateQuantizeV2Op(const Node* op, auto ng_node = ng::builder::ScaledQuantize(ng_input, ng_min, ng_max, ng_et, ng::AxisSet(), ng_round_mode); - SetTracingInfo(op->name(), ng_node); + Builder::SetTracingInfo(op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_min); SaveNgOp(ng_op_map, op->name(), ng_max); @@ -3561,7 +3556,7 @@ static Status TranslateDequantizeOp(const Node* op, // TF only dequantizes to fp32 auto ng_node = ng::builder::ScaledDequantize(ng_input, ng_min, ng_max, ng::element::f32, ng::AxisSet()); - SetTracingInfo(op->name(), ng_node); + Builder::SetTracingInfo(op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); } @@ -4736,7 +4731,7 @@ static Status TranslateTransposeOp( NGRAPH_VLOG(3) << ng::join(ng_axis_order); auto ng_node = ng::builder::numpy_transpose(ng_input, ng_axis_order); - SetTracingInfo(op->name(), ng_node); + Builder::SetTracingInfo(op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); } diff --git a/ngraph_bridge/ngraph_builder.h b/ngraph_bridge/ngraph_builder.h index b39db598e..cdaad09cf 100644 --- a/ngraph_bridge/ngraph_builder.h +++ b/ngraph_bridge/ngraph_builder.h @@ -148,6 +148,17 @@ class Builder { const ngraph::element::Type>>& TF_NGRAPH_CONST_MAP(); + // This function is used to trace which ng node came from which tf node + // It does 3 things: + // 1. Attaches provenance tags. This is guaranteed to propagate the tag info + // to all nodes. + // The next 2 are not guaranteed to be present for all nodes. + // But when present they are correct and agree with provenance tags + // 2. Attaches friendly names. + // 3. Prints a log if NGRAPH_TF_LOG_PLACEMENT=1 + static void SetTracingInfo(const std::string& op_name, + const std::shared_ptr ng_node); + private: static void ComputeScaleOffsetFolded(const uint& num_bits, const bool& unsigned_type, diff --git a/ngraph_bridge/ngraph_conversions.cc b/ngraph_bridge/ngraph_conversions.cc index 0d503bcee..31564f90a 100644 --- a/ngraph_bridge/ngraph_conversions.cc +++ b/ngraph_bridge/ngraph_conversions.cc @@ -36,12 +36,7 @@ void BatchToNGraph(const string& op_name, bool is_nhwc, std::shared_ptr& ng_input) { if (is_nhwc) { detail::NhwcToNGraph(ng_input); - ng_input->set_friendly_name(op_name); - ng_input->add_provenance_tag(op_name); - if (config::IsLoggingPlacement()) { - cout << "TF_to_NG: " << op_name << " --> " << ng_input->get_name() - << "\n"; - } + Builder::SetTracingInfo(op_name, ng_input); } } @@ -49,12 +44,7 @@ void BatchToNGraph3D(const string& op_name, bool is_ndhwc, std::shared_ptr& ng_input) { if (is_ndhwc) { detail::NdhwcToNGraph(ng_input); - ng_input->set_friendly_name(op_name); - ng_input->add_provenance_tag(op_name); - if (config::IsLoggingPlacement()) { - cout << "TF_to_NG: " << op_name << " --> " << ng_input->get_name() - << "\n"; - } + Builder::SetTracingInfo(op_name, ng_input); } } @@ -64,11 +54,7 @@ void BatchToTensorflow(const string& op_name, bool is_nhwc, return; } Reshape<0, 2, 3, 1>(ng_node); - ng_node->set_friendly_name(op_name); - ng_node->add_provenance_tag(op_name); - if (config::IsLoggingPlacement()) { - cout << "TF_to_NG: " << op_name << " --> " << ng_node->get_name() << "\n"; - } + Builder::SetTracingInfo(op_name, ng_node); } void BatchToTensorflow3D(const string& op_name, bool is_ndhwc, @@ -76,12 +62,7 @@ void BatchToTensorflow3D(const string& op_name, bool is_ndhwc, if (!is_ndhwc) { return; } - Reshape3D<0, 2, 3, 4, 1>(ng_node); - ng_node->set_friendly_name(op_name); - ng_node->add_provenance_tag(op_name); - if (config::IsLoggingPlacement()) { - cout << "TF_to_NG: " << op_name << " --> " << ng_node->get_name() << "\n"; - } + Builder::SetTracingInfo(op_name, ng_node); } } // namespace ngraph_bridge From f81092a680330c20494217c92cacaef62f52149b Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Thu, 3 Oct 2019 13:40:26 -0700 Subject: [PATCH 17/19] Fix PerformNgBroadcast --- ngraph_bridge/ngraph_builder.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index d09e4e88b..ad4c1a270 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -351,9 +351,10 @@ Builder::TF_NGRAPH_CONST_MAP() { return the_map; } -static std::pair, std::shared_ptr> -PerformNgBroadcast(const string& prov_tag, std::shared_ptr ng_lhs, - std::shared_ptr ng_rhs) { +std::pair, std::shared_ptr> +Builder::PerformNgBroadcast(const string& prov_tag, + std::shared_ptr ng_lhs, + std::shared_ptr ng_rhs) { // builder::numpy_broadcast is the only known builder that has the possibility // that the output node is same as the input node // So we take special care to check, before calling SetTracingInfo @@ -459,7 +460,8 @@ static Status TranslateBinaryOp( std::shared_ptr ng_lhs, ng_rhs; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); - std::tie(ng_lhs, ng_rhs) = PerformNgBroadcast(op->name(), ng_lhs, ng_rhs); + std::tie(ng_lhs, ng_rhs) = + Builder::PerformNgBroadcast(op->name(), ng_lhs, ng_rhs); auto ng_node = create_binary_op(ng_lhs, ng_rhs); if (ng_node != ng_lhs && ng_node != ng_rhs) { @@ -2989,9 +2991,9 @@ static Status TranslateOneHotOp( // broadcast to make all tensors same shape, as required by ngraph select op std::tie(ng_onehot_bool, ng_on) = - PerformNgBroadcast(op->name(), ng_onehot_bool, ng_on); + Builder::PerformNgBroadcast(op->name(), ng_onehot_bool, ng_on); std::tie(ng_onehot_bool, ng_off) = - PerformNgBroadcast(op->name(), ng_onehot_bool, ng_off); + Builder::PerformNgBroadcast(op->name(), ng_onehot_bool, ng_off); auto ng_onehot = ConstructNgNode(op->name(), ng_onehot_bool, ng_on, ng_off); @@ -4838,10 +4840,10 @@ static Status TranslateSelectOp(const Node* op, op->name(), ng_input1, ng::AxisVector{0}, tmp_vector); } - std::tie(ng_input1, ng_input2) = PerformNgBroadcast( + std::tie(ng_input1, ng_input2) = Builder::PerformNgBroadcast( op->name(), (length != 0 ? ng_input_new : ng_input1), ng_input2); std::tie(ng_input2, ng_input3) = - PerformNgBroadcast(op->name(), ng_input2, ng_input3); + Builder::PerformNgBroadcast(op->name(), ng_input2, ng_input3); ng_select = ConstructNgNode(op->name(), ng_input1, ng_input2, ng_input3); From c769a2455a4c552598882c81c9897a63b3296c90 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Thu, 3 Oct 2019 13:53:06 -0700 Subject: [PATCH 18/19] Addressing comments --- ngraph_bridge/ngraph_builder.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ngraph_bridge/ngraph_builder.cc b/ngraph_bridge/ngraph_builder.cc index ad4c1a270..035e663ae 100644 --- a/ngraph_bridge/ngraph_builder.cc +++ b/ngraph_bridge/ngraph_builder.cc @@ -5144,9 +5144,13 @@ Status Builder::TranslateGraph( if (!check_if_result_or_parameter(n)) { num_tags = n->get_provenance_tags().size(); if (num_tags != 1) { + // In case of an error (num_tags != 1), we dump the ngraph json + // However by default the json will not contain the provenance + // information. So enable NGRAPH_PROVENANCE_ENABLE, and then reset it + // back in the end after NgraphSerialize is done char* original_provenance_flag_value = getenv("NGRAPH_PROVENANCE_ENABLE"); - // No need to free original_provenance_flag_value sccording to the + // No need to free original_provenance_flag_value according to the // standard char enable_provenance[] = "NGRAPH_PROVENANCE_ENABLE=1"; From 69b42eee1ed44829425bd22a511d342209c8432b Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar Date: Thu, 3 Oct 2019 14:31:22 -0700 Subject: [PATCH 19/19] Add back unintentional deletion --- ngraph_bridge/ngraph_conversions.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/ngraph_bridge/ngraph_conversions.cc b/ngraph_bridge/ngraph_conversions.cc index 31564f90a..12ee4b365 100644 --- a/ngraph_bridge/ngraph_conversions.cc +++ b/ngraph_bridge/ngraph_conversions.cc @@ -62,6 +62,7 @@ void BatchToTensorflow3D(const string& op_name, bool is_ndhwc, if (!is_ndhwc) { return; } + Reshape3D<0, 2, 3, 4, 1>(ng_node); Builder::SetTracingInfo(op_name, ng_node); } } // namespace ngraph_bridge