From 029f07665c40ee9d49485b353feb0c74db45ed57 Mon Sep 17 00:00:00 2001 From: shresthamalik Date: Fri, 25 Jan 2019 20:56:30 -0800 Subject: [PATCH 1/9] Chnages in translate function. does not compile --- src/ngraph_builder.cc | 513 +++++++++++++++++++++++++++--------------- 1 file changed, 335 insertions(+), 178 deletions(-) diff --git a/src/ngraph_builder.cc b/src/ngraph_builder.cc index 2e1af9af..1a783c5c 100644 --- a/src/ngraph_builder.cc +++ b/src/ngraph_builder.cc @@ -345,13 +345,14 @@ Builder::TF_NGRAPH_CONST_MAP() { // } static Status TranslateUnaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map, std::function(std::shared_ptr)> create_unary_op) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); - SaveNgOp(ng_op_map, op->name(), create_unary_op(ng_input)); - + auto result = create_unary_op(ng_input); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } @@ -368,7 +369,7 @@ static Status TranslateUnaryOp( template static Status TranslateUnaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { return TranslateUnaryOp( op, static_input_map, ng_op_map, [](std::shared_ptr n) { return make_shared(n); }); @@ -400,7 +401,7 @@ static Status TranslateUnaryOp( static Status TranslateBinaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map, std::function(std::shared_ptr, std::shared_ptr)> create_binary_op) { @@ -409,8 +410,9 @@ static Status TranslateBinaryOp( std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); - - SaveNgOp(ng_op_map, op->name(), create_binary_op(ng_lhs, ng_rhs)); + auto result = create_binary_op(ng_lhs, ng_rhs); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } @@ -428,9 +430,9 @@ static Status TranslateBinaryOp( template static Status TranslateBinaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { return TranslateBinaryOp( - op, static_input_map, ng_op_map, + op, static_input_map, ng_op_map, first_ng_op_map, [](std::shared_ptr ng_lhs, std::shared_ptr ng_rhs) { return make_shared(ng_lhs, ng_rhs); }); @@ -438,23 +440,26 @@ static Status TranslateBinaryOp( static Status TranslateAllreduceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); - SaveNgOp(ng_op_map, op->name(), make_shared(ng_input)); + auto result = make_shared(ng_input); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateAddNOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { std::vector> ng_arg_vec(op->num_inputs()); for (int inp_idx = 0; inp_idx < op->num_inputs(); inp_idx++) TF_RETURN_IF_ERROR( GetInputNode(ng_op_map, op, inp_idx, &ng_arg_vec[inp_idx])); + // TODO (malikshr): find out the first node here SaveNgOp(ng_op_map, op->name(), std::accumulate(std::next(ng_arg_vec.begin()), ng_arg_vec.end(), ng_arg_vec.at(0))); // accumulation: start with @@ -466,7 +471,7 @@ static Status TranslateAddNOp( template static Status TranslateLogicalReduction( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_axes_op; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_axes_op)); @@ -494,7 +499,7 @@ static Status TranslateLogicalReduction( shared_ptr ng_all_or_any = make_shared(ng_input, ng_reduction_axes); - + SaveNgOp(first_ng_op_map, op->name(), ng_all_or_any); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -517,7 +522,7 @@ static Status TranslateLogicalReduction( static Status TranslateArgMaxOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_dim; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_dim)); @@ -546,14 +551,14 @@ static Status TranslateArgMaxOp( TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); auto ng_argmax = make_shared(ng_input, input_dims, ng_et); - + SaveNgOp(first_ng_op_map, op->name(), ng_argmax); SaveNgOp(ng_op_map, op->name(), ng_argmax); return Status::OK(); } static Status TranslateArgMinOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_dim; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_dim)); @@ -582,14 +587,14 @@ static Status TranslateArgMinOp( TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); auto ng_argmin = make_shared(ng_input, input_dims, ng_et); - + SaveNgOp(first_ng_op_map, op->name(), ng_argmin); SaveNgOp(ng_op_map, op->name(), ng_argmin); return Status::OK(); } static Status TranslateAvgPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -621,6 +626,9 @@ static Status TranslateAvgPoolOp( BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); BatchToNGraph(is_nhwc, ng_input); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), 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); @@ -638,7 +646,9 @@ static Status TranslateAvgPoolOp( std::shared_ptr ng_avgpool = make_shared(ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above, false); - + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_avgpool); + } BatchToTensorflow(is_nhwc, ng_avgpool); NGRAPH_VLOG(3) << "avgpool outshape: {" << ng::join(ng_avgpool->get_shape()) << "}"; @@ -649,7 +659,7 @@ static Status TranslateAvgPoolOp( static Status TranslateAvgPoolGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_grad; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, nullptr, &ng_grad)); @@ -690,6 +700,10 @@ static Status TranslateAvgPoolGradOp( BatchedOpParamReshape(is_nhwc, ng_orig_input_shape, ng_forward_arg_shape); BatchToNGraph(is_nhwc, ng_grad); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), 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); @@ -713,7 +727,9 @@ static Status TranslateAvgPoolGradOp( make_shared( ng_forward_arg_shape, ng_grad, ng_window_shape, ng_strides, ng_padding_below, ng_padding_above, false); - + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_avgpool_backprop); + } BatchToTensorflow(is_nhwc, ng_avgpool_backprop); NGRAPH_VLOG(3) << "avgpoolbackprop outshape: {" @@ -726,7 +742,7 @@ static Status TranslateAvgPoolGradOp( static Status TranslateBatchMatMulOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_lhs, ng_rhs; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); @@ -785,12 +801,16 @@ static Status TranslateBatchMatMulOp( "should have the same size"); } if (n_dims == 2) { - SaveNgOp(ng_op_map, op->name(), - make_shared(ng_lhs, ng_rhs)); + auto dot_output = make_shared(ng_lhs, ng_rhs); + SaveNgOp(first_ng_op_map, op->name(), dot_output); + SaveNgOp(ng_op_map, op->name(), dot_output); + } else { auto output_shape = ng_lhs_shape; output_shape[n_dims - 1] = ng_rhs_shape[1]; auto dot_output = make_shared(ng_lhs, ng_rhs); + SaveNgOp(ng_op_map, op->name(), dot_output); + size_t compound_size = 1; for (int i = 0; i < out_axes.size(); i++) { compound_size *= output_shape[i]; @@ -836,7 +856,7 @@ static Status TranslateBatchMatMulOp( static Status TranslateBiasAddOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_bias; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_bias)); @@ -876,6 +896,7 @@ static Status TranslateBiasAddOp( auto ng_bias_broadcasted = make_shared( ng_bias, ng_input_shape, ng_broadcast_axes); + SaveNgOp(first_ng_op_map, op->name(), ng_bias_broadcasted); auto ng_add = ng_input + ng_bias_broadcasted; SaveNgOp(ng_op_map, op->name(), ng_add); @@ -884,7 +905,7 @@ static Status TranslateBiasAddOp( static Status TranslateBiasAddGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -926,14 +947,14 @@ static Status TranslateBiasAddGradOp( } ng_biasadd_backprop = make_shared(ng_input, reduction_axes); - + SaveNgOp(first_ng_op_map, op->name(), ng_biasadd_backprop); SaveNgOp(ng_op_map, op->name(), ng_biasadd_backprop); return Status::OK(); } static Status TranslateCastOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -944,8 +965,9 @@ static Status TranslateCastOp( TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); try { - SaveNgOp(ng_op_map, op->name(), - make_shared(ng_input, ng_et)); + auto result = make_shared(ng_input, ng_et); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); } catch (const std::out_of_range&) { return errors::Unimplemented("Unsupported TensorFlow data type: ", DataType_Name(dtype)); @@ -955,7 +977,7 @@ static Status TranslateCastOp( static Status TranslateConcatV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCountMin(op, 2)); std::vector tf_concat_axis_vec; @@ -979,14 +1001,15 @@ static Status TranslateConcatV2Op( ng_args.push_back(ng_arg); } - SaveNgOp(ng_op_map, op->name(), - make_shared(ng_args, size_t(concat_axis))); + auto result = make_shared(ng_args, size_t(concat_axis)); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateConstOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { DataType dtype; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "dtype", &dtype)); @@ -1009,14 +1032,14 @@ static Status TranslateConstOp( return errors::Unimplemented("Unsupported TensorFlow data type: ", DataType_Name(dtype)); } - + SaveNgOp(first_ng_op_map, op->name(), ng_node); SaveNgOp(ng_op_map, op->name(), ng_node); return Status::OK(); } static Status TranslateConv2DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_filter; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_filter)); @@ -1058,7 +1081,9 @@ static Status TranslateConv2DOp( BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); BatchToNGraph(is_nhwc, ng_input); - + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); @@ -1067,6 +1092,7 @@ static Status TranslateConv2DOp( ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); + SaveNgOp(first_ng_op_map, op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1080,6 +1106,9 @@ static Status TranslateConv2DOp( std::shared_ptr ng_conv = make_shared( ng_input, ng_filter, ng_strides, ng_dilations, ng_padding_below, ng_padding_above); + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_conv); + } BatchToTensorflow(is_nhwc, ng_conv); SaveNgOp(ng_op_map, op->name(), ng_conv); @@ -1088,7 +1117,7 @@ static Status TranslateConv2DOp( static Status TranslateConv2DBackpropFilterOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_data_batch, ng_output_delta; TF_RETURN_IF_ERROR( GetInputNodes(ng_op_map, op, &ng_data_batch, nullptr, &ng_output_delta)); @@ -1153,6 +1182,9 @@ static Status TranslateConv2DBackpropFilterOp( // nGraph Padding Above [f] // nGraph Dilation Stride [f] BatchToNGraph(is_nhwc, ng_data_batch); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_data_batch); + } // tf_filter shape : // [filter_height, filter_width, in_channels, out_channels] // reshape for nGraph @@ -1198,7 +1230,9 @@ static Status TranslateConv2DBackpropFilterOp( ng_window_movement_strides_forward, ng_window_dilation_strides_forward, ng_padding_below_forward, ng_padding_above_forward, ng_data_dilation_strides_forward); - + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_back_prop_filter); + } // Reshape the output to tf format : [filter_height, filter_width, // in_channels, out_channels] Reshape<2, 3, 1, 0>(ng_back_prop_filter); @@ -1209,7 +1243,7 @@ static Status TranslateConv2DBackpropFilterOp( static Status TranslateConv2DBackpropInputOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_filter, ng_out_backprop; TF_RETURN_IF_ERROR( GetInputNodes(ng_op_map, op, nullptr, &ng_filter, &ng_out_backprop)); @@ -1258,6 +1292,7 @@ static Status TranslateConv2DBackpropInputOp( BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); BatchToNGraph(is_nhwc, ng_out_backprop); if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_out_backprop); ng_batch_shape = {static_cast(tf_input_sizes[0]), static_cast(tf_input_sizes[3]), static_cast(tf_input_sizes[1]), @@ -1277,6 +1312,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); + SaveNgOp(first_ng_op_map, op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1292,6 +1328,9 @@ static Status TranslateConv2DBackpropInputOp( ng_batch_shape, ng_filter, ng_out_backprop, ng_strides, ng_dilations, ng_padding_below, ng_padding_above, ng::Strides(ng_batch_shape.size() - 2, 1)); + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_data); + } BatchToTensorflow(is_nhwc, ng_data); @@ -1302,7 +1341,7 @@ static Status TranslateConv2DBackpropInputOp( // Translate Conv3D Op static Status TranslateConv3DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_filter; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_filter)); @@ -1345,7 +1384,9 @@ static Status TranslateConv3DOp( BatchedOpParam3DToNGraph(is_ndhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParam3DToNGraph(is_ndhwc, tf_dilations, ng_dilations); BatchToNGraph3D(is_ndhwc, ng_input); - + if (is_ndhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); @@ -1355,6 +1396,7 @@ static Status TranslateConv3DOp( ng_kernel_shape[1] = ng_filter_shape[1]; ng_kernel_shape[2] = ng_filter_shape[2]; Reshape3D<4, 3, 0, 1, 2>(ng_filter); + SaveNgOp(first_ng_op_map, op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1368,7 +1410,9 @@ static Status TranslateConv3DOp( std::shared_ptr ng_conv = make_shared( ng_input, ng_filter, ng_strides, ng_dilations, ng_padding_below, ng_padding_above); - + if (!is_ndhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_conv); + } BatchToTensorflow3D(is_ndhwc, ng_conv); SaveNgOp(ng_op_map, op->name(), ng_conv); return Status::OK(); @@ -1377,7 +1421,7 @@ static Status TranslateConv3DOp( // Translate DepthToSpace op static Status TranslateDepthToSpaceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -1516,6 +1560,7 @@ static Status TranslateDepthToSpaceOp( std::iota(ng_axis_order.begin(), ng_axis_order.end(), 0); auto reshaped = make_shared(ng_input, ng_axis_order, ng_reshape_shape); + SaveNgOp(first_ng_op_map, op->name(), reshaped); auto transposed = ng::builder::numpy_transpose(reshaped, ng_transpose_permutation); @@ -1532,7 +1577,7 @@ static Status TranslateDepthToSpaceOp( static Status TranslateDepthwiseConv2dNativeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_filter; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_filter)); @@ -1566,6 +1611,9 @@ static Status TranslateDepthwiseConv2dNativeOp( BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); BatchToNGraph(is_nhwc, ng_input); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); @@ -1575,7 +1623,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); - + SaveNgOp(first_ng_op_map, op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); ng::CoordinateDiff ng_padding_below{0, 0}; @@ -1597,7 +1645,9 @@ static Status TranslateDepthwiseConv2dNativeOp( input_shape[3]}; auto ng_sliced_input = make_shared(ng_input, lower_bound, upper_bound); - + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } const std::vector f_lower_bound{0, i, 0, 0}; const std::vector f_upper_bound{filter_shape[0], i + 1, filter_shape[2], filter_shape[3]}; @@ -1626,7 +1676,7 @@ static Status TranslateDepthwiseConv2dNativeOp( static Status TranslateExpandDimsOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_dim; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_dim)); @@ -1651,14 +1701,14 @@ static Status TranslateExpandDimsOp( std::iota(shape_dimensions.begin(), shape_dimensions.end(), 0); std::shared_ptr ng_expand_dim = make_shared(ng_input, shape_dimensions, out_shape); - + SaveNgOp(ng_op_map, op->name(), ng_expand_dim); SaveNgOp(ng_op_map, op->name(), ng_expand_dim); return Status::OK(); } static Status TranslateFillOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_value; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, nullptr, &ng_value)); @@ -1671,38 +1721,47 @@ static Status TranslateFillOp( ng_output_shape[i] = dims_vec[i]; ng_axis_set.insert(i); } - SaveNgOp(ng_op_map, op->name(), make_shared( - ng_value, ng_output_shape, ng_axis_set)); + + auto ng_broadcast = + make_shared(ng_value, ng_output_shape, ng_axis_set); + SaveNgOp(first_ng_op_map, op->name(), ng_broadcast); + SaveNgOp(ng_op_map, op->name(), ng_broadcast); + return Status::OK(); } static Status TranslateFloorDivOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { - auto ng_floordiv = [](std::shared_ptr ng_input1, - std::shared_ptr ng_input2) { - return std::make_shared( - std::make_shared(ng_input1, ng_input2)); + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + auto ng_floordiv = [&](std::shared_ptr ng_input1, + std::shared_ptr ng_input2) { + auto ng_div = std::make_shared(ng_input1, ng_input2); + SaveNgOp(first_ng_op_map, op->name(), ng_div); + return std::make_shared(ng_div); }; - return TranslateBinaryOp(op, static_input_map, ng_op_map, ng_floordiv); + return TranslateBinaryOp(op, static_input_map, ng_op_map, first_ng_op_map, + ng_floordiv); } static Status TranslateFloorModOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { - auto ng_floormod = [](std::shared_ptr ng_input1, - std::shared_ptr ng_input2) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + auto ng_floormod = [first_ng_op_map, op]( + std::shared_ptr ng_input1, + std::shared_ptr ng_input2) { auto floordiv = std::make_shared( std::make_shared(ng_input1, ng_input2)); + SaveNgOp(first_ng_op_map, op->name(), floordiv); return std::make_shared( ng_input1, std::make_shared(floordiv, ng_input2)); }; - return TranslateBinaryOp(op, static_input_map, ng_op_map, ng_floormod); + return TranslateBinaryOp(op, static_input_map, ng_op_map, first_ng_op_map, + ng_floormod); } static Status TranslateFusedBatchNormOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { bool tf_is_training; if (GetNodeAttr(op->attrs(), "is_training", &tf_is_training) != Status::OK()) { @@ -1738,13 +1797,18 @@ static Status TranslateFusedBatchNormOp( NGRAPH_VLOG(3) << "epsilon: " << tf_epsilon; BatchToNGraph(is_nhwc, ng_input); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } std::shared_ptr ng_batch_norm; if (tf_is_training) { ng_batch_norm = make_shared(tf_epsilon, ng_scale, ng_offset, ng_input); - + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_batch_norm); + } shared_ptr ng_y, ng_mean, ng_variance; ng_y = make_shared(ng_batch_norm, 0); ng_mean = make_shared(ng_batch_norm, 1); @@ -1776,6 +1840,9 @@ static Status TranslateFusedBatchNormOp( } else { ng_batch_norm = make_shared( tf_epsilon, ng_scale, ng_offset, ng_input, ng_mean, ng_variance); + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_batch_norm); + } BatchToTensorflow(is_nhwc, ng_batch_norm); SaveNgOp(ng_op_map, op->name(), ng_batch_norm); } @@ -1785,7 +1852,7 @@ static Status TranslateFusedBatchNormOp( static Status TranslateFusedBatchNormGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCount(op, 5)); bool tf_is_training; @@ -1835,6 +1902,7 @@ static Status TranslateFusedBatchNormGradOp( shared_ptr ng_beta = std::make_shared( ng_scale->get_element_type(), ng_scale->get_shape(), std::vector{ng::shape_size(ng_scale->get_shape()), "0"}); + SaveNgOp(first_ng_op_map, op->name(), ng_beta); BatchToNGraph(is_nhwc, ng_input); BatchToNGraph(is_nhwc, ng_delta); @@ -1873,21 +1941,23 @@ static Status TranslateFusedBatchNormGradOp( static Status TranslateIdentityOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_arg; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_arg)); + // TODO : Create an identity op? SaveNgOp(ng_op_map, op->name(), ng_arg); return Status::OK(); } static Status TranslateL2LossOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); auto const_2 = make_shared( ng_input->get_element_type(), ng::Shape{}, std::vector{"2"}); + SaveNgOp(first_ng_op_map, op->name(), const_2); std::shared_ptr ng_pow = make_shared(ng_input, ng_input); @@ -1907,7 +1977,7 @@ static Status TranslateL2LossOp( static Status TranslateMatMulOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_lhs, ng_rhs; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); @@ -1926,13 +1996,16 @@ static Status TranslateMatMulOp( // The default axis count for nGraph's Dot op is 1, which is just what // we need here. - SaveNgOp(ng_op_map, op->name(), make_shared(ng_lhs, ng_rhs)); + auto result = make_shared(ng_lhs, ng_rhs); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateMaxOp(const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, + Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_max_op; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_max_op)); @@ -1957,7 +2030,7 @@ static Status TranslateMaxOp(const Node* op, std::shared_ptr ng_max = make_shared(ng_input, ng_reduction_axes); - + SaveNgOp(first_ng_op_map, op->name(), ng_max); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -1981,7 +2054,7 @@ static Status TranslateMaxOp(const Node* op, static Status TranslateMaxPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -2014,6 +2087,9 @@ static Status TranslateMaxPoolOp( BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); BatchToNGraph(is_nhwc, ng_input); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), 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); @@ -2031,7 +2107,9 @@ static Status TranslateMaxPoolOp( std::shared_ptr ng_maxpool = make_shared(ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); - + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_maxpool); + } BatchToTensorflow(is_nhwc, ng_maxpool); NGRAPH_VLOG(3) << "maxpool outshape: {" << ng::join(ng_maxpool->get_shape()) @@ -2043,7 +2121,7 @@ static Status TranslateMaxPoolOp( static Status TranslateMaxPool3DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -2076,6 +2154,9 @@ static Status TranslateMaxPool3DOp( BatchedOpParam3DToNGraph(is_ndhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParam3DToNGraph(is_ndhwc, tf_ksize, ng_kernel_shape); BatchToNGraph3D(is_ndhwc, ng_input); + if (is_ndhwc) { + SaveNgOp(first_ng_op_map, op->name(), 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); @@ -2093,7 +2174,9 @@ static Status TranslateMaxPool3DOp( std::shared_ptr ng_maxpool = make_shared(ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); - + if (!is_ndhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_maxpool); + } BatchToTensorflow3D(is_ndhwc, ng_maxpool); NGRAPH_VLOG(3) << "maxpool outshape: {" << ng::join(ng_maxpool->get_shape()) @@ -2105,7 +2188,7 @@ static Status TranslateMaxPool3DOp( static Status TranslateMaxPoolGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_grad, ng_fwd; TF_RETURN_IF_ERROR( GetInputNodes(ng_op_map, op, &ng_input, &ng_fwd, &ng_grad)); @@ -2137,6 +2220,9 @@ static Status TranslateMaxPoolGradOp( BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); BatchToNGraph(is_nhwc, ng_input); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } BatchToNGraph(is_nhwc, ng_grad); BatchToNGraph(is_nhwc, ng_fwd); @@ -2154,6 +2240,9 @@ static Status TranslateMaxPoolGradOp( make_shared(ng_input, ng_grad, ng_fwd, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_maxpool_backprop); + } BatchToTensorflow(is_nhwc, ng_maxpool_backprop); NGRAPH_VLOG(3) << "maxpoolbackprop outshape: {" << ng::join(ng_maxpool_backprop->get_shape()) << "}"; @@ -2163,7 +2252,7 @@ static Status TranslateMaxPoolGradOp( static Status TranslateMeanOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_axes_op; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_axes_op)); @@ -2188,7 +2277,7 @@ static Status TranslateMeanOp( std::shared_ptr ng_mean = ng::builder::mean(ng_input, ng_reduction_axes); - + SaveNgOp(first_ng_op_map, op->name(), ng_mean); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -2212,7 +2301,8 @@ static Status TranslateMeanOp( static Status TranslateMinOp(const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, + Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_min_op; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_min_op)); @@ -2237,6 +2327,7 @@ static Status TranslateMinOp(const Node* op, std::shared_ptr ng_min = make_shared(ng_input, ng_reduction_axes); + SaveNgOp(first_ng_op_map, op->name(), ng_min); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. @@ -2261,7 +2352,7 @@ static Status TranslateMinOp(const Node* op, static Status TranslatePackOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCountMin(op, 1)); ng::NodeVector ng_concat_inputs; @@ -2302,11 +2393,15 @@ static Status TranslatePackOp( for (size_t i = 0; i < ng_concat_inputs.size(); ++i) { ng_concat_inputs[i] = make_shared( ng_concat_inputs[i], ng_axis_order, extended_shape); + SaveNgOp(first_ng_op_map, op->name(), ng_concat_inputs[i]); } ng_axis_order.push_back(input_rank); } auto concat = make_shared(ng_concat_inputs, concat_axis); + if (concat_axis != input_rank) { + SaveNgOp(first_ng_op_map, op->name(), concat); + } SaveNgOp(ng_op_map, op->name(), make_shared(concat, ng_axis_order, output_shape)); return Status::OK(); @@ -2314,7 +2409,8 @@ static Status TranslatePackOp( static Status TranslatePadOp(const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, + Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_paddings_op; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_paddings_op)); @@ -2345,6 +2441,8 @@ static Status TranslatePadOp(const Node* op, // For PadV1 it seems the value is always zero. auto pad_val_op = make_shared( ng_input->get_element_type(), ng::Shape{}, std::vector{"0"}); + SaveNgOp(first_ng_op_map, op->name(), pad_val_op); + auto pad_op = make_shared(ng_input, pad_val_op, padding_below, padding_above, padding_interior); @@ -2354,7 +2452,7 @@ static Status TranslatePadOp(const Node* op, static Status TranslateProdOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_axes_op; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_axes_op)); @@ -2379,7 +2477,7 @@ static Status TranslateProdOp( std::shared_ptr ng_prod = make_shared(ng_input, ng_reduction_axes); - + SaveNgOp(first_ng_op_map, op->name(), ng_prod); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -2403,7 +2501,7 @@ static Status TranslateProdOp( static Status TranslateRankOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -2413,16 +2511,17 @@ static Status TranslateRankOp( auto ng_rank = std::make_shared( ng::element::i32, ng::Shape(), std::vector({input_rank})); - + SaveNgOp(first_ng_op_map, op->name(), ng_rank); SaveNgOp(ng_op_map, op->name(), ng_rank); return Status::OK(); } static Status TranslateReciprocalOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { return TranslateUnaryOp( - op, static_input_map, ng_op_map, [](std::shared_ptr n) { + op, static_input_map, ng_op_map, first_ng_op_map, + [first_ng_op_map, op](std::shared_ptr n) { // Create a constant tensor populated with the value -1. // (1/x = x^(-1)) auto et = n->get_element_type(); @@ -2430,7 +2529,7 @@ static Status TranslateReciprocalOp( std::vector constant_values(ng::shape_size(shape), "-1"); auto ng_exponent = std::make_shared(et, shape, constant_values); - + SaveNgOp(first_ng_op_map, op->name(), ng_exponent); // Raise each element of the input to the power -1. return std::make_shared(n, ng_exponent); }); @@ -2564,7 +2663,7 @@ Status QuantizeAndDequantizeV2Helper( static Status TranslateQuantizeAndDequantizeV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, nullptr, nullptr)); bool range_given; @@ -2611,6 +2710,7 @@ static Status TranslateQuantizeAndDequantizeV2Op( } auto ng_scale = std::make_shared( ng_r_et, ng::Shape(), std::vector({scale})); + SaveNgOp(first_ng_op_map, op->name(), ng_scale); auto ng_offset = std::make_shared(ng_q_et, ng::Shape(), std::vector({0})); ng::op::Quantize::RoundMode ng_round_mode = @@ -2627,7 +2727,7 @@ static Status TranslateQuantizeAndDequantizeV2Op( static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_filter, ng_bias; TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_input)); TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 1, &ng_filter)); @@ -2644,6 +2744,7 @@ static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( } static_inps[i] = std::make_shared( ng::element::f32, ng::Shape({}), tmp_vect); + SaveNgOp(first_ng_op_map, op->name(), static_inps[i]); } std::vector tf_strides; std::vector tf_dilations; @@ -2661,10 +2762,14 @@ static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( BatchedOpParamToNGraph(is_nhwc, ng_input->get_shape(), ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); BatchToNGraph(is_nhwc, ng_input); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } auto& ng_filter_shape = ng_filter->get_shape(); ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); + SaveNgOp(first_ng_op_map, op->name(), ng_filter); 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, @@ -2691,7 +2796,7 @@ static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( static Status TranslateQuantizedMaxPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_min, ng_max; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_min, &ng_max)); std::vector tf_strides; @@ -2709,6 +2814,9 @@ static Status TranslateQuantizedMaxPoolOp( ng_image_shape); BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); BatchToNGraph(is_nhwc, ng_input); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } ng::Shape ng_padding_below{0, 0}; ng::Shape ng_padding_above{0, 0}; Builder::MakePadding(tf_padding_type, ng_image_shape, ng_kernel_shape, @@ -2722,6 +2830,9 @@ static Status TranslateQuantizedMaxPoolOp( ng::builder::ScaledQuantizedMaxPool(ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above, dummy_min, dummy_max); + if (!is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_quant_maxpool); + } BatchToTensorflow(is_nhwc, ng_quant_maxpool); SaveNgOp(ng_op_map, op->name(), ng_quant_maxpool); // For maxpool input min-max remains unchanged and is just propagated along @@ -2733,7 +2844,7 @@ static Status TranslateQuantizedMaxPoolOp( static Status TranslateQuantizeV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, nullptr, nullptr)); @@ -2797,14 +2908,15 @@ static Status TranslateQuantizeV2Op( (mode.compare("SCALED") == 0), ng_min[0], ng_max[0], &ng_scale_val, &ng_offset_val); } catch (const std::exception& e) { - return errors::Internal("Unhandled exception in ComputeScaleOffset: ", - op->name(), " (", op->type_string(), ")\n", - op->def().DebugString(), "\n", "what(): ", - e.what()); + return errors::Internal( + "Unhandled exception in ComputeScaleOffset: ", op->name(), " (", + op->type_string(), ")\n", op->def().DebugString(), "\n", + "what(): ", e.what()); } auto ng_scale = std::make_shared( ng::element::f32, ng::Shape(), std::vector({ng_scale_val})); + SaveNgOp(first_ng_op_map, op->name(), ng_scale); ng::element::Type ng_et; TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); @@ -2834,7 +2946,7 @@ static Status TranslateQuantizeV2Op( static Status TranslateDequantizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, nullptr, nullptr)); @@ -2889,14 +3001,15 @@ static Status TranslateDequantizeOp( (mode.compare("SCALED") == 0), ng_min[0], ng_max[0], &ng_scale_val, &ng_offset_val); } catch (const std::exception& e) { - return errors::Internal("Unhandled exception in ComputeScaleOffset: ", - op->name(), " (", op->type_string(), ")\n", - op->def().DebugString(), "\n", "what(): ", - e.what()); + return errors::Internal( + "Unhandled exception in ComputeScaleOffset: ", op->name(), " (", + op->type_string(), ")\n", op->def().DebugString(), "\n", + "what(): ", e.what()); } auto ng_scale = std::make_shared( ng::element::f32, ng::Shape(), std::vector({ng_scale_val})); + SaveNgOp(first_ng_op_map, op->name(), ng_scale); ng::element::Type ng_et; TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); @@ -2913,44 +3026,48 @@ static Status TranslateDequantizeOp( static Status TranslateReluOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); - - SaveNgOp(ng_op_map, op->name(), make_shared(ng_input)); + auto result = make_shared(ng_input); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateRelu6Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); auto constant_6 = make_shared( ng_input->get_element_type(), ng_input->get_shape(), std::vector(ng::shape_size(ng_input->get_shape()), "6")); + SaveNgOp(first_ng_op_map, op->name(), constant_6); + auto relu6_op = make_shared( make_shared(ng_input), constant_6); SaveNgOp(ng_op_map, op->name(), relu6_op); + return Status::OK(); } static Status TranslateReluGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_arg, ng_delta; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_delta, &ng_arg)); auto ng_relu_grad = std::make_shared(ng_arg, ng_delta); + SaveNgOp(first_ng_op_map, op->name(), ng_relu_grad); SaveNgOp(ng_op_map, op->name(), ng_relu_grad); return Status::OK(); } static Status TranslateReshapeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_shape_op; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_shape_op)); @@ -3008,17 +3125,18 @@ static Status TranslateReshapeOp( ng::AxisVector ng_axis_order(ng_input->get_shape().size()); std::iota(ng_axis_order.begin(), ng_axis_order.end(), 0); - - SaveNgOp(ng_op_map, op->name(), - make_shared(ng_input, ng_axis_order, ng_shape)); + auto result = make_shared(ng_input, ng_axis_order, ng_shape); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateRsqrtOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { return TranslateUnaryOp( - op, static_input_map, ng_op_map, [](std::shared_ptr n) { + op, static_input_map, ng_op_map, first_ng_op_map, + [first_ng_op_map, op](std::shared_ptr n) { // Create a constant tensor populated with the value -1/2. // (1/sqrt(x) = x^(-1/2)) auto et = n->get_element_type(); @@ -3026,7 +3144,7 @@ static Status TranslateRsqrtOp( std::vector constant_values(ng::shape_size(shape), "-0.5"); auto ng_exponent = std::make_shared(et, shape, constant_values); - + SaveNgOp(first_ng_op_map, op->name(), ng_exponent); // Raise each element of the input to the power -0.5. return std::make_shared(n, ng_exponent); }); @@ -3034,7 +3152,7 @@ static Status TranslateRsqrtOp( static Status TranslateShapeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -3057,19 +3175,21 @@ static Status TranslateShapeOp( for (int i = 0; i < rank; i++) { values[i] = input_shape[i]; } - SaveNgOp(ng_op_map, op->name(), - make_shared(type, shape, values)); + auto result = make_shared(type, shape, values); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateSigmoidGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; shared_ptr ng_delta; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_delta)); auto ng_mul = ng_input * ng_delta; + SaveNgOp(first_ng_op_map, op->name(), ng_mul); auto ng_subtract = make_shared(ng_input->get_element_type(), ng_input->get_shape(), std::vector({1})) - @@ -3082,12 +3202,13 @@ static Status TranslateSigmoidGradOp( static Status TranslateSigmoidOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); - auto exp_op = - make_shared(make_shared(ng_input)); + auto neg = make_shared(ng_input); + SaveNgOp(first_ng_op_map, op->name(), neg); + auto exp_op = make_shared(neg); auto constant_1 = make_shared( ng_input->get_element_type(), ng_input->get_shape(), std::vector(ng::shape_size(ng_input->get_shape()), "1")); @@ -3101,7 +3222,7 @@ static Status TranslateSigmoidOp( static Status TranslateSizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -3122,14 +3243,14 @@ static Status TranslateSizeOp( // make a scalar with value equals to result auto ng_result = make_shared(type, ng::Shape(0), std::vector({result})); - + SaveNgOp(first_ng_op_map, op->name(), ng_result); SaveNgOp(ng_op_map, op->name(), ng_result); return Status::OK(); } static Status TranslateSliceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_begin, ng_size; TF_RETURN_IF_ERROR( GetInputNodes(ng_op_map, op, &ng_input, &ng_begin, &ng_size)); @@ -3180,23 +3301,25 @@ static Status TranslateSliceOp( std::vector l(lower_vec.begin(), lower_vec.end()); std::vector u(upper_vec.begin(), upper_vec.end()); auto ng_slice = make_shared(ng_input, l, u); + SaveNgOp(first_ng_op_map, op->name(), ng_slice); SaveNgOp(ng_op_map, op->name(), ng_slice); return Status::OK(); } static Status TranslateSnapshotOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_arg; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_arg)); - + // TODO : Identity may be required here + // TODO : Check the right way to do TODO SaveNgOp(ng_op_map, op->name(), ng_arg); return Status::OK(); } static Status TranslateSoftmaxOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -3208,15 +3331,18 @@ static Status TranslateSoftmaxOp( } auto rank = ng_input->get_shape().size(); ng_axes_softmax.insert(rank - 1); - SaveNgOp(ng_op_map, op->name(), - make_shared(ng_input, ng_axes_softmax)); + auto result = make_shared(ng_input, ng_axes_softmax); + + SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); + return Status::OK(); } // Translate SpaceToDepthOp static Status TranslateSpaceToDepthOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -3281,19 +3407,21 @@ static Status TranslateSpaceToDepthOp( std::vector strides = {1, 1, 1, 1}; strides[width_index] = size_t(block_size); strides[height_index] = size_t(block_size); - strided_slice_result.push_back( - make_shared(ng_input, begin, upper, strides)); + auto temp = make_shared(ng_input, begin, upper, strides); + strided_slice_result.push_back(temp); + SaveNgOp(first_ng_op_map, op->name(), temp); } } - SaveNgOp(ng_op_map, op->name(), make_shared( - strided_slice_result, channel_index)); + SaveNgOp( + ng_op_map, op->name(), + make_shared(strided_slice_result, channel_index)); return Status::OK(); } static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { // TF op Inputs: // 1. Logits/Features: // Shape : [BatchSize, NumOfClasses] @@ -3356,9 +3484,10 @@ static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( ng_axes_class.insert(1); // compute max(logits) and broadcast to shape [B, NC] - auto max_logits = make_shared( - make_shared(ng_features, ng_axes_class), ng_features_shape, - ng_axes_class); + auto op_max = make_shared(ng_features, ng_axes_class); + SaveNgOp(first_ng_op_map, op->name(), op_max); + auto max_logits = + make_shared(op_max, ng_features_shape, ng_axes_class); // logits_normalized : (logits - max_logits) auto logits_normalized = @@ -3400,7 +3529,7 @@ static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( static Status TranslateSplitOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, nullptr, &ng_input)); // num_split : The number of ways to split. Must evenly divide @@ -3430,15 +3559,16 @@ static Status TranslateSplitOp( upper[split_dim] = cursor; std::string output_name = op->name(); - SaveNgOp(ng_op_map, op->name(), - make_shared(ng_input, lower, upper)); + auto result = make_shared(ng_input, lower, upper); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); } return Status::OK(); } static Status TranslateSplitVOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_length, ng_split_dim; TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_input)); @@ -3507,10 +3637,15 @@ static Status TranslateSplitVOp( lower[split_dim] = cursor; cursor += lengths[i]; upper[split_dim] = cursor; - SaveNgOp(ng_op_map, op->name(), - make_shared(ng_input, lower, upper)); + auto result = make_shared(ng_input, lower, upper); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); } } else { + // TODO : As the slice output is same as input node, we are not + // creating another node. + // So if there is a control dependency for this node, should the dependency + // be passed to the input node May be use identity node here SaveNgOp(ng_op_map, op->name(), ng_input); } @@ -3519,8 +3654,8 @@ static Status TranslateSplitVOp( static Status TranslateSquareOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { - return TranslateUnaryOp(op, static_input_map, ng_op_map, + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + return TranslateUnaryOp(op, static_input_map, ng_op_map, first_ng_op_map, [](std::shared_ptr n) { return std::make_shared(n, n); }); @@ -3528,18 +3663,21 @@ static Status TranslateSquareOp( static Status TranslateSquaredDifferenceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { return TranslateBinaryOp( - op, static_input_map, ng_op_map, - [](std::shared_ptr input1, std::shared_ptr input2) { + op, static_input_map, ng_op_map, first_ng_op_map, + [first_ng_op_map, op](std::shared_ptr input1, + std::shared_ptr input2) { auto ng_diff = std::make_shared(input1, input2); + // An extra control dependecy will get added to the subsequent node also + SaveNgOp(first_ng_op_map, op->name(), ng_diff); return std::make_shared(ng_diff, ng_diff); }); } static Status TranslateSqueezeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); size_t input_dims = ng_input->get_shape().size(); @@ -3589,14 +3727,16 @@ static Status TranslateSqueezeOp( ng::AxisVector ng_axis_order(ng_input->get_shape().size()); std::iota(ng_axis_order.begin(), ng_axis_order.end(), 0); - SaveNgOp(ng_op_map, op->name(), - make_shared(ng_input, ng_axis_order, output_shape)); + auto result = + make_shared(ng_input, ng_axis_order, output_shape); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateStridedSliceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { // TODO: implement new_axis_mask, ellipsis_mask shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_input)); @@ -3663,10 +3803,9 @@ static Status TranslateStridedSliceOp( // The first 2 cases breaks down this range if (idx >= 0 && idx <= (static_cast(dim) - 1)) { return idx; - } else if (idx < 0 && - idx + static_cast(dim) >= - 0) { // careful not to do idx >= -dim - // (since dim is unsigned) + } else if (idx < 0 && idx + static_cast(dim) >= + 0) { // careful not to do idx >= -dim + // (since dim is unsigned) return idx + static_cast( dim); // Type casting to int to enable unambiguous auto // type inference of return type @@ -3837,8 +3976,11 @@ static Status TranslateStridedSliceOp( } // atleast one stride was negative, in which case reverse the input - if (neg_strides.size() > 0) + if (neg_strides.size() > 0) { ng_input = make_shared(ng_input, neg_strides); + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } + NGRAPH_VLOG(3) << "NG Lower Vector " << ng::join(ng_begin_vec); NGRAPH_VLOG(3) << "NG End Vector " << ng::join(ng_end_vec); NGRAPH_VLOG(3) << "NG Stride Vector " << ng::join(ng_stride_vec); @@ -3847,6 +3989,10 @@ static Status TranslateStridedSliceOp( std::shared_ptr ng_strided_slice = make_shared( ng_input, ng_begin_vec, ng_end_vec, ng_stride_vec); + if (neg_strides.size() <= 0) { + SaveNgOp(first_ng_op_map, op->name(), ng_strided_slice); + } + if (tf_shrink_axis_mask) { int64 shrink_axis_mask = tf_shrink_axis_mask; vector output_shape; @@ -3894,7 +4040,8 @@ static Status TranslateStridedSliceOp( static Status TranslateSumOp(const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, + Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_axes_op; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_axes_op)); @@ -3920,6 +4067,7 @@ static Status TranslateSumOp(const Node* op, std::shared_ptr ng_sum = make_shared(ng_input, ng_reduction_axes); + SaveNgOp(first_ng_op_map, op->name(), ng_sum); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -3946,11 +4094,12 @@ static Status TranslateSumOp(const Node* op, // where y = tanh(x) and dy is the corresponding input gradient static Status TranslateTanhGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_delta; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_delta)); auto ng_sq = std::make_shared(ng_input, ng_input); + SaveNgOp(first_ng_op_map, op->name(), ng_sq); ng::Shape input_shape = ng_input->get_shape(); std::vector const_values(ng::shape_size(input_shape), "1"); auto ng_const = make_shared(ng_input->get_element_type(), @@ -3964,7 +4113,7 @@ static Status TranslateTanhGradOp( static Status TranslateTileOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_multiples; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_multiples)); @@ -3986,10 +4135,11 @@ static Status TranslateTileOp( output_shape[i] = ng_input_shape[i] * multiples[i]; } if (is_empty) { - SaveNgOp(ng_op_map, op->name(), - make_shared( - ng_input->get_element_type(), output_shape, - std::vector(ng::shape_size(output_shape), "0"))); + auto result = make_shared( + ng_input->get_element_type(), output_shape, + std::vector(ng::shape_size(output_shape), "0")); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); } else { for (int i = 0; i < ng_input_shape.size(); i++) { if (multiples[i] < 0) { @@ -4001,8 +4151,12 @@ static Status TranslateTileOp( tmp_tensors.push_back(ng_output); } auto ng_concat = make_shared(tmp_tensors, i); + if (i == 0) { + SaveNgOp(first_ng_op_map, op->name(), ng_concat); + } ng_output = ng_concat; } + SaveNgOp(ng_op_map, op->name(), ng_output); } return Status::OK(); @@ -4010,7 +4164,7 @@ static Status TranslateTileOp( static Status TranslateTransposeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input, ng_permutation_op; TF_RETURN_IF_ERROR( GetInputNodes(ng_op_map, op, &ng_input, &ng_permutation_op)); @@ -4049,14 +4203,15 @@ static Status TranslateTransposeOp( NGRAPH_VLOG(3) << ng::join(ng_axis_order); - SaveNgOp(ng_op_map, op->name(), - ng::builder::numpy_transpose(ng_input, ng_axis_order)); + auto result = ng::builder::numpy_transpose(ng_input, ng_axis_order); + SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateUnpackOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCount(op, 1)); shared_ptr ng_input; @@ -4100,6 +4255,7 @@ static Status TranslateUnpackOp( upper_bound[unpack_axis] = i + 1; auto slice = make_shared(ng_input, lower_bound, upper_bound); + SaveNgOp(first_ng_op_map, op->name(), slice); auto reshaped = make_shared(slice, ng_axis_order, output_shape); SaveNgOp(ng_op_map, op->name(), reshaped); @@ -4109,7 +4265,7 @@ static Status TranslateUnpackOp( static Status TranslateZerosLikeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map) { + Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); @@ -4117,7 +4273,7 @@ static Status TranslateZerosLikeOp( std::vector const_values(ng::shape_size(input_shape), "0"); auto ng_result = make_shared(ng_input->get_element_type(), input_shape, const_values); - + SaveNgOp(first_ng_op_map, op->name(), ng_result); SaveNgOp(ng_op_map, op->name(), ng_result); return Status::OK(); } @@ -4125,7 +4281,7 @@ static Status TranslateZerosLikeOp( const static std::map< const string, const function&, - Builder::OpMap&)>> + Builder::OpMap&, Builder::OpMap&)>> TRANSLATE_OP_MAP{ {"Abs", TranslateUnaryOp}, {"Add", TranslateBinaryOp}, @@ -4184,7 +4340,7 @@ const static std::map< // Do nothing! NoOps sometimes get placed on nGraph for bureaucratic // reasons, but they have no data flow inputs or outputs. {"NoOp", [](const Node*, const std::vector&, - Builder::OpMap&) { return Status::OK(); }}, + Builder::OpMap&, Builder::OpMap&) { return Status::OK(); }}, {"Pack", TranslatePackOp}, {"Pad", TranslatePadOp}, {"Pow", TranslateBinaryOp}, @@ -4275,7 +4431,7 @@ Status Builder::TranslateGraph( // vector of generated nGraph nodes. // Builder::OpMap ng_op_map; - + Builder::OpMap first_ng_op_map; // // Populate the parameter list, and also put parameters into the op map. // @@ -4310,7 +4466,7 @@ Status Builder::TranslateGraph( << op->type_string(); const function&, - Builder::OpMap&)>* op_fun; + Builder::OpMap&, Builder::OpMap&)>* op_fun; try { op_fun = &(TRANSLATE_OP_MAP.at(op->type_string())); @@ -4327,12 +4483,13 @@ Status Builder::TranslateGraph( } try { - TF_RETURN_IF_ERROR((*op_fun)(op, static_input_map, ng_op_map)); + TF_RETURN_IF_ERROR( + (*op_fun)(op, static_input_map, ng_op_map, first_ng_op_map)); } catch (const std::exception& e) { return errors::Internal("Unhandled exception in op handler: ", op->name(), " (", op->type_string(), ")\n", - op->def().DebugString(), "\n", "what(): ", - e.what()); + op->def().DebugString(), "\n", + "what(): ", e.what()); } } From 55c5a3260481b8a62777ab54908075791ecebabb Mon Sep 17 00:00:00 2001 From: shresthamalik Date: Fri, 25 Jan 2019 22:13:42 -0800 Subject: [PATCH 2/9] Translate Function changes compile. C++ unit tests pass --- src/ngraph_builder.cc | 146 +++++++++++++++++++++++++----------------- 1 file changed, 87 insertions(+), 59 deletions(-) diff --git a/src/ngraph_builder.cc b/src/ngraph_builder.cc index 1a783c5c..2e3f1ea5 100644 --- a/src/ngraph_builder.cc +++ b/src/ngraph_builder.cc @@ -371,7 +371,7 @@ static Status TranslateUnaryOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { return TranslateUnaryOp( - op, static_input_map, ng_op_map, + op, static_input_map, ng_op_map,first_ng_op_map, [](std::shared_ptr n) { return make_shared(n); }); } @@ -809,7 +809,7 @@ static Status TranslateBatchMatMulOp( auto output_shape = ng_lhs_shape; output_shape[n_dims - 1] = ng_rhs_shape[1]; auto dot_output = make_shared(ng_lhs, ng_rhs); - SaveNgOp(ng_op_map, op->name(), dot_output); + SaveNgOp(first_ng_op_map, op->name(), dot_output); size_t compound_size = 1; for (int i = 0; i < out_axes.size(); i++) { @@ -1733,32 +1733,62 @@ static Status TranslateFillOp( static Status TranslateFloorDivOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { - auto ng_floordiv = [&](std::shared_ptr ng_input1, - std::shared_ptr ng_input2) { - auto ng_div = std::make_shared(ng_input1, ng_input2); - SaveNgOp(first_ng_op_map, op->name(), ng_div); - return std::make_shared(ng_div); - }; - return TranslateBinaryOp(op, static_input_map, ng_op_map, first_ng_op_map, - ng_floordiv); + 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) = + ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); + auto ng_div = std::make_shared(ng_lhs, ng_rhs); + SaveNgOp(first_ng_op_map, op->name(), ng_div); + auto ng_floordiv = std::make_shared(ng_div); + SaveNgOp(ng_op_map, op->name(), ng_floordiv); + return Status::OK(); } +// static Status TranslateFloorDivOp( +// const Node* op, const std::vector& static_input_map, +// Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { +// auto ng_floordiv = [&](std::shared_ptr ng_input1, +// std::shared_ptr ng_input2) { +// auto ng_div = std::make_shared(ng_input1, ng_input2); +// SaveNgOp(first_ng_op_map, op->name(), ng_div); +// return std::make_shared(ng_div); +// }; +// return TranslateBinaryOp(op, static_input_map, ng_op_map, first_ng_op_map, +// ng_floordiv); +// } + static Status TranslateFloorModOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { - auto ng_floormod = [first_ng_op_map, op]( - std::shared_ptr ng_input1, - std::shared_ptr ng_input2) { - auto floordiv = std::make_shared( - std::make_shared(ng_input1, ng_input2)); - SaveNgOp(first_ng_op_map, op->name(), floordiv); - return std::make_shared( - ng_input1, std::make_shared(floordiv, ng_input2)); - }; - return TranslateBinaryOp(op, static_input_map, ng_op_map, first_ng_op_map, - ng_floormod); + 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) = + ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); + + auto ng_div = std::make_shared(ng_lhs, ng_rhs); + SaveNgOp(first_ng_op_map, op->name(), ng_div); + auto floordiv = std::make_shared(ng_div); + + auto ng_floormod =std::make_shared( + ng_lhs, std::make_shared(floordiv, ng_rhs)); + SaveNgOp(ng_op_map, op->name(), ng_floormod); + return Status::OK(); } - +// static Status TranslateFloorModOp( +// const Node* op, const std::vector& static_input_map, +// Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { +// auto ng_floormod = [first_ng_op_map, op]( +// std::shared_ptr ng_input1, +// std::shared_ptr ng_input2) { +// auto floordiv = std::make_shared( +// std::make_shared(ng_input1, ng_input2)); +// SaveNgOp(first_ng_op_map, op->name(), floordiv); +// return std::make_shared( +// ng_input1, std::make_shared(floordiv, ng_input2)); +// }; +// return TranslateBinaryOp(op, static_input_map, ng_op_map, first_ng_op_map, +// ng_floormod); +// } static Status TranslateFusedBatchNormOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { @@ -2519,20 +2549,17 @@ static Status TranslateRankOp( static Status TranslateReciprocalOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { - return TranslateUnaryOp( - op, static_input_map, ng_op_map, first_ng_op_map, - [first_ng_op_map, op](std::shared_ptr n) { - // Create a constant tensor populated with the value -1. - // (1/x = x^(-1)) - auto et = n->get_element_type(); - auto shape = n->get_shape(); - std::vector constant_values(ng::shape_size(shape), "-1"); - auto ng_exponent = - std::make_shared(et, shape, constant_values); - SaveNgOp(first_ng_op_map, op->name(), ng_exponent); - // Raise each element of the input to the power -1. - return std::make_shared(n, ng_exponent); - }); + shared_ptr ng_input; + TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + auto et = ng_input->get_element_type(); + auto shape = ng_input->get_shape(); + std::vector constant_values(ng::shape_size(shape), "-1"); + auto ng_exponent = + std::make_shared(et, shape, constant_values); + SaveNgOp(first_ng_op_map, op->name(), ng_exponent); + auto ng_reciprocal = std::make_shared(ng_input, ng_exponent); + SaveNgOp(ng_op_map, op->name(), ng_reciprocal); + return Status::OK(); } static void ComputeScaleOffsetFolded(const uint& num_bits, @@ -3134,20 +3161,21 @@ static Status TranslateReshapeOp( static Status TranslateRsqrtOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { - return TranslateUnaryOp( - op, static_input_map, ng_op_map, first_ng_op_map, - [first_ng_op_map, op](std::shared_ptr n) { - // Create a constant tensor populated with the value -1/2. - // (1/sqrt(x) = x^(-1/2)) - auto et = n->get_element_type(); - auto shape = n->get_shape(); - std::vector constant_values(ng::shape_size(shape), "-0.5"); - auto ng_exponent = - std::make_shared(et, shape, constant_values); - SaveNgOp(first_ng_op_map, op->name(), ng_exponent); - // Raise each element of the input to the power -0.5. - return std::make_shared(n, ng_exponent); - }); + shared_ptr ng_input; + TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + auto et = ng_input->get_element_type(); + auto shape = ng_input->get_shape(); + std::vector constant_values(ng::shape_size(shape), "-0.5"); + auto ng_exponent = + std::make_shared(et, shape, constant_values); + SaveNgOp(first_ng_op_map, op->name(), ng_exponent); + // Create a constant tensor populated with the value -1/2. + // (1/sqrt(x) = x^(-1/2)) + + // Raise each element of the input to the power -0.5. + auto result =std::make_shared(ng_input, ng_exponent); + SaveNgOp(ng_op_map, op->name(), result); + return Status::OK(); } static Status TranslateShapeOp( @@ -3664,15 +3692,15 @@ static Status TranslateSquareOp( static Status TranslateSquaredDifferenceOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { - return TranslateBinaryOp( - op, static_input_map, ng_op_map, first_ng_op_map, - [first_ng_op_map, op](std::shared_ptr input1, - std::shared_ptr input2) { - auto ng_diff = std::make_shared(input1, input2); - // An extra control dependecy will get added to the subsequent node also - SaveNgOp(first_ng_op_map, op->name(), ng_diff); - return std::make_shared(ng_diff, ng_diff); - }); + 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) = + ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); + auto ng_diff = std::make_shared(ng_lhs, ng_rhs); + SaveNgOp(first_ng_op_map, op->name(), ng_diff); + auto ng_sq_diff = std::make_shared(ng_diff, ng_diff); + SaveNgOp(ng_op_map, op->name(), ng_sq_diff); + return Status::OK(); } static Status TranslateSqueezeOp( From 9056da1a3ef692fec901684a6fc8fc2af357f894 Mon Sep 17 00:00:00 2001 From: shresthamalik Date: Fri, 25 Jan 2019 22:33:46 -0800 Subject: [PATCH 3/9] Add Control dependency to ngraph cluster --- src/ngraph_builder.cc | 61 +++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/src/ngraph_builder.cc b/src/ngraph_builder.cc index 2e3f1ea5..58e935d9 100644 --- a/src/ngraph_builder.cc +++ b/src/ngraph_builder.cc @@ -371,7 +371,7 @@ static Status TranslateUnaryOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { return TranslateUnaryOp( - op, static_input_map, ng_op_map,first_ng_op_map, + op, static_input_map, ng_op_map, first_ng_op_map, [](std::shared_ptr n) { return make_shared(n); }); } @@ -1764,13 +1764,13 @@ static Status TranslateFloorModOp( TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); - + auto ng_div = std::make_shared(ng_lhs, ng_rhs); SaveNgOp(first_ng_op_map, op->name(), ng_div); auto floordiv = std::make_shared(ng_div); - auto ng_floormod =std::make_shared( - ng_lhs, std::make_shared(floordiv, ng_rhs)); + auto ng_floormod = std::make_shared( + ng_lhs, std::make_shared(floordiv, ng_rhs)); SaveNgOp(ng_op_map, op->name(), ng_floormod); return Status::OK(); } @@ -2935,10 +2935,10 @@ static Status TranslateQuantizeV2Op( (mode.compare("SCALED") == 0), ng_min[0], ng_max[0], &ng_scale_val, &ng_offset_val); } catch (const std::exception& e) { - return errors::Internal( - "Unhandled exception in ComputeScaleOffset: ", op->name(), " (", - op->type_string(), ")\n", op->def().DebugString(), "\n", - "what(): ", e.what()); + return errors::Internal("Unhandled exception in ComputeScaleOffset: ", + op->name(), " (", op->type_string(), ")\n", + op->def().DebugString(), "\n", "what(): ", + e.what()); } auto ng_scale = std::make_shared( @@ -3028,10 +3028,10 @@ static Status TranslateDequantizeOp( (mode.compare("SCALED") == 0), ng_min[0], ng_max[0], &ng_scale_val, &ng_offset_val); } catch (const std::exception& e) { - return errors::Internal( - "Unhandled exception in ComputeScaleOffset: ", op->name(), " (", - op->type_string(), ")\n", op->def().DebugString(), "\n", - "what(): ", e.what()); + return errors::Internal("Unhandled exception in ComputeScaleOffset: ", + op->name(), " (", op->type_string(), ")\n", + op->def().DebugString(), "\n", "what(): ", + e.what()); } auto ng_scale = std::make_shared( @@ -3171,9 +3171,9 @@ static Status TranslateRsqrtOp( SaveNgOp(first_ng_op_map, op->name(), ng_exponent); // Create a constant tensor populated with the value -1/2. // (1/sqrt(x) = x^(-1/2)) - + // Raise each element of the input to the power -0.5. - auto result =std::make_shared(ng_input, ng_exponent); + auto result = std::make_shared(ng_input, ng_exponent); SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } @@ -3441,9 +3441,8 @@ static Status TranslateSpaceToDepthOp( } } - SaveNgOp( - ng_op_map, op->name(), - make_shared(strided_slice_result, channel_index)); + SaveNgOp(ng_op_map, op->name(), make_shared( + strided_slice_result, channel_index)); return Status::OK(); } @@ -3692,7 +3691,7 @@ static Status TranslateSquareOp( static Status TranslateSquaredDifferenceOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { - shared_ptr ng_lhs, ng_rhs; + 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) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); @@ -3831,9 +3830,10 @@ static Status TranslateStridedSliceOp( // The first 2 cases breaks down this range if (idx >= 0 && idx <= (static_cast(dim) - 1)) { return idx; - } else if (idx < 0 && idx + static_cast(dim) >= - 0) { // careful not to do idx >= -dim - // (since dim is unsigned) + } else if (idx < 0 && + idx + static_cast(dim) >= + 0) { // careful not to do idx >= -dim + // (since dim is unsigned) return idx + static_cast( dim); // Type casting to int to enable unambiguous auto // type inference of return type @@ -4516,8 +4516,8 @@ Status Builder::TranslateGraph( } catch (const std::exception& e) { return errors::Internal("Unhandled exception in op handler: ", op->name(), " (", op->type_string(), ")\n", - op->def().DebugString(), "\n", - "what(): ", e.what()); + op->def().DebugString(), "\n", "what(): ", + e.what()); } } @@ -4544,6 +4544,21 @@ Status Builder::TranslateGraph( ng_result_list[index] = result; } + // Add control dependencies + for (const Edge* edge : input_graph->edges()) { + if (edge->IsControlEdge()) { + Node* src = edge->src(); + Node* dst = edge->dst(); + + for (auto ng_src : ng_op_map[src->name()]) { + for (auto ng_dst : first_ng_op_map[dst->name()]) { + NGRAPH_VLOG(1) << "Adding control edge in nGraph cluster"; + ng_dst->add_control_dependency(ng_src); + } + } + } + } + // // Create the nGraph function. // From 6ae77c0113a30ceeb99ae09779b8a57e2fe4581f Mon Sep 17 00:00:00 2001 From: shresthamalik Date: Sat, 26 Jan 2019 16:39:04 -0800 Subject: [PATCH 4/9] Some corrections. Revisited ops --- src/ngraph_builder.cc | 146 +++++++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 60 deletions(-) diff --git a/src/ngraph_builder.cc b/src/ngraph_builder.cc index 58e935d9..3e2cf05b 100644 --- a/src/ngraph_builder.cc +++ b/src/ngraph_builder.cc @@ -459,12 +459,20 @@ static Status TranslateAddNOp( TF_RETURN_IF_ERROR( GetInputNode(ng_op_map, op, inp_idx, &ng_arg_vec[inp_idx])); - // TODO (malikshr): find out the first node here + // TODO (malikshr): Is empty list a valid input? + // TODO (malikshr) : No op is created + if (ng_arg_vec.size() == 1) { + SaveNgOp(ng_op_map, op->name(), ng_arg_vec[0]); + return Status::OK(); + } + + auto ng_first_add = ng_arg_vec[0] + ng_arg_vec[1]; + SaveNgOp(first_ng_op_map, op->name(), ng_first_add); SaveNgOp(ng_op_map, op->name(), - std::accumulate(std::next(ng_arg_vec.begin()), ng_arg_vec.end(), - ng_arg_vec.at(0))); // accumulation: start with - // first element. default op is - // addition + std::accumulate(ng_arg_vec.begin() + 2, ng_arg_vec.end(), + ng_first_add)); // accumulation: start + // with first element. + // default op is addition return Status::OK(); } @@ -777,19 +785,27 @@ static Status TranslateBatchMatMulOp( auto ng_lhs_axes = out_axes; auto ng_rhs_axes = out_axes; + bool got_first_node = false; + if (tf_adj_x) { 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); + got_first_node = true; + SaveNgOp(first_ng_op_map, 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); + got_first_node = true; + SaveNgOp(first_ng_op_map, 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); + got_first_node = true; + SaveNgOp(first_ng_op_map, op->name(), ng_rhs); } ng_lhs_shape = ng_lhs->get_shape(); @@ -802,15 +818,18 @@ static Status TranslateBatchMatMulOp( } if (n_dims == 2) { auto dot_output = make_shared(ng_lhs, ng_rhs); - SaveNgOp(first_ng_op_map, op->name(), dot_output); + if (!got_first_node) { + SaveNgOp(first_ng_op_map, op->name(), dot_output); + } SaveNgOp(ng_op_map, op->name(), dot_output); } else { auto output_shape = ng_lhs_shape; output_shape[n_dims - 1] = ng_rhs_shape[1]; auto dot_output = make_shared(ng_lhs, ng_rhs); - SaveNgOp(first_ng_op_map, op->name(), dot_output); - + if (!got_first_node) { + SaveNgOp(first_ng_op_map, op->name(), dot_output); + } size_t compound_size = 1; for (int i = 0; i < out_axes.size(); i++) { compound_size *= output_shape[i]; @@ -1106,9 +1125,8 @@ static Status TranslateConv2DOp( std::shared_ptr ng_conv = make_shared( ng_input, ng_filter, ng_strides, ng_dilations, ng_padding_below, ng_padding_above); - if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_conv); - } + + SaveNgOp(first_ng_op_map, op->name(), ng_conv); BatchToTensorflow(is_nhwc, ng_conv); SaveNgOp(ng_op_map, op->name(), ng_conv); @@ -1193,6 +1211,9 @@ static Status TranslateConv2DBackpropFilterOp( static_cast(tf_filter_sizes[0]), static_cast(tf_filter_sizes[1])}; BatchToNGraph(is_nhwc, ng_output_delta); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_output_delta); + } BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_window_movement_strides_forward); BatchedOpParamToNGraph(is_nhwc, tf_dilations, @@ -1328,9 +1349,8 @@ static Status TranslateConv2DBackpropInputOp( ng_batch_shape, ng_filter, ng_out_backprop, ng_strides, ng_dilations, ng_padding_below, ng_padding_above, ng::Strides(ng_batch_shape.size() - 2, 1)); - if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_data); - } + + SaveNgOp(first_ng_op_map, op->name(), ng_data); BatchToTensorflow(is_nhwc, ng_data); @@ -1701,7 +1721,7 @@ static Status TranslateExpandDimsOp( std::iota(shape_dimensions.begin(), shape_dimensions.end(), 0); std::shared_ptr ng_expand_dim = make_shared(ng_input, shape_dimensions, out_shape); - SaveNgOp(ng_op_map, op->name(), ng_expand_dim); + SaveNgOp(first_ng_op_map, op->name(), ng_expand_dim); SaveNgOp(ng_op_map, op->name(), ng_expand_dim); return Status::OK(); } @@ -1744,19 +1764,6 @@ static Status TranslateFloorDivOp( return Status::OK(); } -// static Status TranslateFloorDivOp( -// const Node* op, const std::vector& static_input_map, -// Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { -// auto ng_floordiv = [&](std::shared_ptr ng_input1, -// std::shared_ptr ng_input2) { -// auto ng_div = std::make_shared(ng_input1, ng_input2); -// SaveNgOp(first_ng_op_map, op->name(), ng_div); -// return std::make_shared(ng_div); -// }; -// return TranslateBinaryOp(op, static_input_map, ng_op_map, first_ng_op_map, -// ng_floordiv); -// } - static Status TranslateFloorModOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { @@ -1774,21 +1781,7 @@ static Status TranslateFloorModOp( SaveNgOp(ng_op_map, op->name(), ng_floormod); return Status::OK(); } -// static Status TranslateFloorModOp( -// const Node* op, const std::vector& static_input_map, -// Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { -// auto ng_floormod = [first_ng_op_map, op]( -// std::shared_ptr ng_input1, -// std::shared_ptr ng_input2) { -// auto floordiv = std::make_shared( -// std::make_shared(ng_input1, ng_input2)); -// SaveNgOp(first_ng_op_map, op->name(), floordiv); -// return std::make_shared( -// ng_input1, std::make_shared(floordiv, ng_input2)); -// }; -// return TranslateBinaryOp(op, static_input_map, ng_op_map, first_ng_op_map, -// ng_floormod); -// } + static Status TranslateFusedBatchNormOp( const Node* op, const std::vector& static_input_map, Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { @@ -1935,7 +1928,13 @@ static Status TranslateFusedBatchNormGradOp( SaveNgOp(first_ng_op_map, op->name(), ng_beta); BatchToNGraph(is_nhwc, ng_input); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_input); + } BatchToNGraph(is_nhwc, ng_delta); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_delta); + } std::shared_ptr ng_batch_norm_backprop; @@ -1974,7 +1973,7 @@ static Status TranslateIdentityOp( Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_arg; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_arg)); - // TODO : Create an identity op? + // TODO (malikshr): No op created : control dependency? SaveNgOp(ng_op_map, op->name(), ng_arg); return Status::OK(); } @@ -1991,6 +1990,7 @@ static Status TranslateL2LossOp( std::shared_ptr ng_pow = make_shared(ng_input, ng_input); + SaveNgOp(first_ng_op_map, op->name(), ng_pow); size_t input_rank = ng_input->get_shape().size(); ng::AxisSet axes; @@ -2015,19 +2015,27 @@ static Status TranslateMatMulOp( bool transpose_a = false; bool transpose_b = false; + bool got_first_node = false; if (GetNodeAttr(op->attrs(), "transpose_a", &transpose_a) == Status::OK() && transpose_a) { ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng::AxisVector{1, 0}); + got_first_node = true; + SaveNgOp(first_ng_op_map, 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}); + got_first_node = true; + SaveNgOp(first_ng_op_map, op->name(), ng_rhs); } // The default axis count for nGraph's Dot op is 1, which is just what // we need here. auto result = make_shared(ng_lhs, ng_rhs); - SaveNgOp(first_ng_op_map, op->name(), result); + if (!got_first_node) { + SaveNgOp(first_ng_op_map, op->name(), result); + } + SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } @@ -2254,8 +2262,13 @@ static Status TranslateMaxPoolGradOp( SaveNgOp(first_ng_op_map, op->name(), ng_input); } BatchToNGraph(is_nhwc, ng_grad); + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_grad); + } BatchToNGraph(is_nhwc, ng_fwd); - + if (is_nhwc) { + SaveNgOp(first_ng_op_map, op->name(), ng_fwd); + } 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); @@ -2740,6 +2753,7 @@ static Status TranslateQuantizeAndDequantizeV2Op( SaveNgOp(first_ng_op_map, op->name(), ng_scale); auto ng_offset = std::make_shared(ng_q_et, ng::Shape(), std::vector({0})); + SaveNgOp(first_ng_op_map, op->name(), ng_offset); ng::op::Quantize::RoundMode ng_round_mode = ng::op::Quantize::RoundMode::ROUND_NEAREST_TOWARD_INFINITY; auto ng_quant = make_shared( @@ -2951,6 +2965,7 @@ static Status TranslateQuantizeV2Op( // Cast offset appropriately? Currently using int auto ng_offset = std::make_shared( ng_et, ng::Shape(), std::vector({ng_offset_val})); + SaveNgOp(first_ng_op_map, op->name(), ng_offset); // TODO: Only RoundMode = ROUND_NEAREST_TOWARD_INFINITY is supported, for now. // Support HALF_TO_EVEN later @@ -2960,14 +2975,17 @@ static Status TranslateQuantizeV2Op( SaveNgOp(ng_op_map, op->name(), make_shared(ng_input, ng_scale, ng_offset, ng_et, ng::AxisSet(), ng_round_mode)); - SaveNgOp(ng_op_map, op->name(), - make_shared(ng::element::f32, ng::Shape(), - std::vector({ng_min[0]}))); + auto ng_min_res = make_shared( + ng::element::f32, ng::Shape(), std::vector({ng_min[0]})); + SaveNgOp(first_ng_op_map, op->name(), ng_min_res); + SaveNgOp(ng_op_map, op->name(), ng_min_res); + // TODO: For quantizev2 revisit output min-max (which would change in case // input min-max are too close. For now just propagating inputs - SaveNgOp(ng_op_map, op->name(), - make_shared(ng::element::f32, ng::Shape(), - std::vector({ng_max[0]}))); + auto ng_max_res = make_shared( + ng::element::f32, ng::Shape(), std::vector({ng_max[0]})); + SaveNgOp(first_ng_op_map, op->name(), ng_max_res); + SaveNgOp(ng_op_map, op->name(), ng_max_res); return Status::OK(); } @@ -3044,6 +3062,7 @@ static Status TranslateDequantizeOp( // Cast offset appropriately? Currently using int auto ng_offset = std::make_shared( ng_et, ng::Shape(), std::vector({ng_offset_val})); + SaveNgOp(first_ng_op_map, op->name(), ng_offset); SaveNgOp(ng_op_map, op->name(), make_shared(ng_input, ng_scale, ng_offset, @@ -3057,6 +3076,7 @@ static Status TranslateReluOp( shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); auto result = make_shared(ng_input); + SaveNgOp(first_ng_op_map, op->name(), result); SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); } @@ -3339,8 +3359,7 @@ static Status TranslateSnapshotOp( Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { shared_ptr ng_arg; TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_arg)); - // TODO : Identity may be required here - // TODO : Check the right way to do TODO + // TODO (malikshr) : No op is created SaveNgOp(ng_op_map, op->name(), ng_arg); return Status::OK(); } @@ -3361,7 +3380,7 @@ static Status TranslateSoftmaxOp( ng_axes_softmax.insert(rank - 1); auto result = make_shared(ng_input, ng_axes_softmax); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(first_ng_op_map, op->name(), result); SaveNgOp(ng_op_map, op->name(), result); return Status::OK(); @@ -3669,10 +3688,9 @@ static Status TranslateSplitVOp( SaveNgOp(ng_op_map, op->name(), result); } } else { - // TODO : As the slice output is same as input node, we are not - // creating another node. - // So if there is a control dependency for this node, should the dependency - // be passed to the input node May be use identity node here + // TODO (malikshr): As the slice output is same as input node, no node is + // created. So if there is a control dependency for this node, should the + // dependency be passed to the input node May be use identity node here SaveNgOp(ng_op_map, op->name(), ng_input); } @@ -4543,13 +4561,21 @@ Status Builder::TranslateGraph( ng_result_list[index] = result; } - + NGRAPH_VLOG(1) << "Add control dependencies"; // Add control dependencies for (const Edge* edge : input_graph->edges()) { if (edge->IsControlEdge()) { Node* src = edge->src(); Node* dst = edge->dst(); + NGRAPH_VLOG(1) << "Found Control Edge " << src->type_string() << " -> " + << dst->type_string(); + if (!src->IsOp() || !dst->IsOp()) { + continue; + } + // TODO(malikshr) : add error checks once we are sure that all the + // nodes have an entry + exit point + // Right now some ops like Snapshot dont have for (auto ng_src : ng_op_map[src->name()]) { for (auto ng_dst : first_ng_op_map[dst->name()]) { NGRAPH_VLOG(1) << "Adding control edge in nGraph cluster"; From 15b7e2a0b4633f2e9b58c129761f9ed18ef1a1f7 Mon Sep 17 00:00:00 2001 From: Avijit <30507445+avijit-nervana@users.noreply.github.com> Date: Sat, 26 Jan 2019 21:51:07 -0800 Subject: [PATCH 5/9] Updated the ngraph version --- build_ngtf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_ngtf.py b/build_ngtf.py index edfb49f4..6d1bb05a 100755 --- a/build_ngtf.py +++ b/build_ngtf.py @@ -432,7 +432,7 @@ def main(): #------------------------------- # Component versions - ngraph_version = "v0.12.0-rc.1" + ngraph_version = "sandeep/git-d" tf_version = "v1.12.0" # Default directories From f81f4199d5da171745cf2b90a414c9da9c5dd792 Mon Sep 17 00:00:00 2001 From: Avijit <30507445+avijit-nervana@users.noreply.github.com> Date: Sat, 26 Jan 2019 21:53:32 -0800 Subject: [PATCH 6/9] Update CMakeLists.txt --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37d47fa7..e71725b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -239,7 +239,7 @@ if (NOT USE_PRE_BUILT_NGRAPH) ExternalProject_Add( ext_ngraph GIT_REPOSITORY https://github.com/NervanaSystems/ngraph - GIT_TAG v0.12.0-rc.1 + GIT_TAG sandeep/git-d CMAKE_ARGS -DNGRAPH_DISTRIBUTED_ENABLE=${NGRAPH_DISTRIBUTED_ENABLE} -DNGRAPH_INSTALL_PREFIX=${NGRAPH_ARTIFACTS_DIR} From 00ebc59296c511530895159090ad339b2ac9e173 Mon Sep 17 00:00:00 2001 From: shresthamalik Date: Thu, 31 Jan 2019 11:07:49 -0800 Subject: [PATCH 7/9] Incorporate Review Comments --- build_ngtf.py | 6 +- src/ngraph_builder.cc | 812 +++++++++++++++++++++--------------------- 2 files changed, 409 insertions(+), 409 deletions(-) diff --git a/build_ngtf.py b/build_ngtf.py index edfb49f4..d1e83ed8 100755 --- a/build_ngtf.py +++ b/build_ngtf.py @@ -145,9 +145,9 @@ def setup_venv(venv_dir): "termcolor>=1.1.0", "protobuf>=3.6.1", "keras_applications>=1.0.6", - "--no-deps", + "--no-deps --no-cache-dir", "keras_preprocessing==1.0.5", - "--no-deps", + "--no-deps --no-cache-dir", ] command_executor(package_list) @@ -432,7 +432,7 @@ def main(): #------------------------------- # Component versions - ngraph_version = "v0.12.0-rc.1" + ngraph_version = "sandeep/git-d" tf_version = "v1.12.0" # Default directories diff --git a/src/ngraph_builder.cc b/src/ngraph_builder.cc index 3e2cf05b..bfc98a1b 100644 --- a/src/ngraph_builder.cc +++ b/src/ngraph_builder.cc @@ -59,7 +59,7 @@ static Status ValidateInputCountMin(const Node* op, size_t count) { } // // Helper for storing ops in ng_op_map. -// For most of the cases, op would have one output so +// For most of the cases, op would have one entry/output node so // vector ng_op_map[op_name] would contain one element. // // If storing more than one output_nodes, make sure it's in @@ -79,7 +79,7 @@ static void SaveNgOp(Builder::OpMap& ng_op_map, const std::string& op_name, ng_op_map[op_name].push_back(output_node); } -// Helper for fetching correct input node from ng_op_map. +// Helper for fetching correct input node from output_nodes_ng_op_map. // Handles edge checking to make sure correct input node is // fetched. // @@ -90,30 +90,30 @@ static void SaveNgOp(Builder::OpMap& ng_op_map, const std::string& op_name, // // shared_ptr ng_input; // try { -// ng_input = ng_op_map.at(tf_input->name()); +// ng_input = output_nodes_ng_op_map.at(tf_input->name()); // } catch (const std::out_of_range&) { // return errors::NotFound(tf_input->name(), -// " is not found in the ng_op_map"); +// " is not found in the output_nodes_ng_op_map"); // } // // Into 2 lines: // // shared_ptr ng_input; -// TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_input)) +// TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)) // // // // Parameters: -// Builder::OpMap& ng_op_map - The TF-to-nGraph op map. -// Node* op - TF op being translated. -// input_idx - index of input +// Builder::OpMap& output_nodes_ng_op_map - The TF-to-nGraph op map. +// Node* op - TF op being translated. +// input_idx - index of input // -// shared_ptr *result - ng::Node pointer where result -// will be written +// shared_ptr *result - ng::Node pointer where result +// will be written // // -static Status GetInputNode(const Builder::OpMap& ng_op_map, const Node* op, +static Status GetInputNode(const Builder::OpMap& output_nodes_ng_op_map, const Node* op, size_t input_idx, shared_ptr* result) { // input op may have resulted in more than one ng::Node (eg. Split) // we need to look at Edge to check index of the input op @@ -130,7 +130,7 @@ static Status GetInputNode(const Builder::OpMap& ng_op_map, const Node* op, TF_RETURN_IF_ERROR(op->input_node(input_idx, &tf_input)); const std::vector>* ng_op = nullptr; try { - ng_op = &ng_op_map.at(tf_input->name()); + ng_op = &output_nodes_ng_op_map.at(tf_input->name()); } catch (const out_of_range&) { return Status(error::NOT_FOUND, string("Ngraph op not found for ") + tf_input->name()); @@ -145,28 +145,28 @@ static Status GetInputNode(const Builder::OpMap& ng_op_map, const Node* op, } namespace detail { -static Status GetInputNodes(const Builder::OpMap& ng_op_map, const Node* op, +static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, const Node* op, size_t index) { return Status::OK(); } template -static Status GetInputNodes(const Builder::OpMap& ng_op_map, const Node* op, +static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, const Node* op, size_t index, shared_ptr* result, Arguments&&... remaining) { if (result != nullptr) { - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, index, result)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, index, result)); } - return GetInputNodes(ng_op_map, op, index + 1, remaining...); + return GetInputNodes(output_nodes_ng_op_map, op, index + 1, remaining...); } } // namespace detail template -static Status GetInputNodes(const Builder::OpMap& ng_op_map, const Node* op, +static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, const Node* op, Arguments&&... remaining) { constexpr size_t args_len = sizeof...(Arguments); TF_RETURN_IF_ERROR(ValidateInputCount(op, args_len)); - return detail::GetInputNodes(ng_op_map, op, 0, remaining...); + return detail::GetInputNodes(output_nodes_ng_op_map, op, 0, remaining...); } static Status GetStaticNodeTensor( @@ -328,7 +328,9 @@ Builder::TF_NGRAPH_CONST_MAP() { // Node* op - TF op being translated. Must have one input. // const std::vector& static_input_map // - the static input map -// Builder::OpMap& ng_op_map - The TF-to-nGraph op map. +// Builder::OpMap& output_nodes_ng_op_map - The TF-to-nGraph outputs nodes op map. +// +// Builder::OpMap& entry_nodes_ng_op_map - The TF-to-nGraph entry nodes op map. // // std::function(std::shared_ptr> // create_unary_op - Function to construct the graph implementing @@ -338,21 +340,22 @@ Builder::TF_NGRAPH_CONST_MAP() { // Example usage: // // if (n->type_string == "Square") { -// TF_RETURN_IF_ERROR(TranslateUnaryOp(n, static_input_map, ng_op_map, +// TF_RETURN_IF_ERROR(TranslateUnaryOp(n, static_input_map, output_nodes_ng_op_map, +// entry_nodes_ng_op_map) // [] (std::shared_ptr n) { // return (std::make_shared(n,n)); // }); // } static Status TranslateUnaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map, + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map, std::function(std::shared_ptr)> create_unary_op) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto result = create_unary_op(ng_input); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } @@ -363,15 +366,15 @@ static Status TranslateUnaryOp( // // if (n->type_string == "Abs") { // TF_RETURN_IF_ERROR(TranslateUnaryOp(n, static_input_map, -// ng_op_map)); +// output_nodes_ng_op_map, entry_nodes_ng_op_map)); // } // template static Status TranslateUnaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { return TranslateUnaryOp( - op, static_input_map, ng_op_map, first_ng_op_map, + op, static_input_map, output_nodes_ng_op_map, entry_nodes_ng_op_map, [](std::shared_ptr n) { return make_shared(n); }); } @@ -381,7 +384,9 @@ static Status TranslateUnaryOp( // Node* op - TF op being translated. Must have only two // inputs. // const std::vector& static_input_map - the static input map -// Builder::OpMap& ng_op_map - The TF-to-nGraph op map. +// Builder::OpMap& output_nodes_ng_op_map - The TF-to-nGraph output nodes op map. +// Builder::OpMap& entry_nodes_ng_op_map - The TF-to-nGraph entry nodes op map. +// // std::function(std::shared_ptr, // std::shared_ptr)> // create_binary_op - Function to construct the graph implementing @@ -389,11 +394,11 @@ static Status TranslateUnaryOp( // binaryop // Example Usage: // -// if (op->type_string() == "SquaredDifference") { -// TF_RETURN_IF_ERROR(TranslateBinaryOp(op, ng_op_map, +// if (op->type_string() == "Subtract") { +// TF_RETURN_IF_ERROR(TranslateBinaryOp(op, static_input_map, output_nodes_ng_op_map, +// entry_nodes_ng_op_map, // [](std::shared_ptr ng_input1, std::shared_ptr // ng_input2) { -// auto ng_diff = std::make_shared(input1, input2); // return std::make_shared(ng_diff,ng_diff); // })); // } @@ -401,18 +406,18 @@ static Status TranslateUnaryOp( static Status TranslateBinaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map, + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map, std::function(std::shared_ptr, std::shared_ptr)> create_binary_op) { std::shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); auto result = create_binary_op(ng_lhs, ng_rhs); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } @@ -424,15 +429,15 @@ static Status TranslateBinaryOp( // // if (n->type_string == "Add") { // TF_RETURN_IF_ERROR(TranslateBinaryOp(op, static_input_map, -// ng_op_map)); +// output_nodes_ng_op_map,entry_nodes_ng_op_map)); // } // template static Status TranslateBinaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { return TranslateBinaryOp( - op, static_input_map, ng_op_map, first_ng_op_map, + op, static_input_map, output_nodes_ng_op_map, entry_nodes_ng_op_map, [](std::shared_ptr ng_lhs, std::shared_ptr ng_rhs) { return make_shared(ng_lhs, ng_rhs); }); @@ -440,35 +445,35 @@ static Status TranslateBinaryOp( static Status TranslateAllreduceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto result = make_shared(ng_input); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateAddNOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { std::vector> ng_arg_vec(op->num_inputs()); for (int inp_idx = 0; inp_idx < op->num_inputs(); inp_idx++) TF_RETURN_IF_ERROR( - GetInputNode(ng_op_map, op, inp_idx, &ng_arg_vec[inp_idx])); + GetInputNode(output_nodes_ng_op_map, op, inp_idx, &ng_arg_vec[inp_idx])); // TODO (malikshr): Is empty list a valid input? // TODO (malikshr) : No op is created if (ng_arg_vec.size() == 1) { - SaveNgOp(ng_op_map, op->name(), ng_arg_vec[0]); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_arg_vec[0]); return Status::OK(); } auto ng_first_add = ng_arg_vec[0] + ng_arg_vec[1]; - SaveNgOp(first_ng_op_map, op->name(), ng_first_add); - SaveNgOp(ng_op_map, op->name(), + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_first_add); + SaveNgOp(output_nodes_ng_op_map, op->name(), std::accumulate(ng_arg_vec.begin() + 2, ng_arg_vec.end(), ng_first_add)); // accumulation: start // with first element. @@ -479,9 +484,9 @@ static Status TranslateAddNOp( template static Status TranslateLogicalReduction( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_axes_op; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_axes_op)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); static_assert(std::is_base_of::value, "Expected LogicalReduction type op (Any or All)"); @@ -507,7 +512,7 @@ static Status TranslateLogicalReduction( shared_ptr ng_all_or_any = make_shared(ng_input, ng_reduction_axes); - SaveNgOp(first_ng_op_map, op->name(), ng_all_or_any); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_all_or_any); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -524,16 +529,16 @@ static Status TranslateLogicalReduction( ng_result_shape_with_keep); } - SaveNgOp(ng_op_map, op->name(), ng_all_or_any); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_all_or_any); return Status::OK(); } static Status TranslateArgMaxOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_dim; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_dim)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); std::vector tf_dim; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &tf_dim)); @@ -559,17 +564,17 @@ static Status TranslateArgMaxOp( TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); auto ng_argmax = make_shared(ng_input, input_dims, ng_et); - SaveNgOp(first_ng_op_map, op->name(), ng_argmax); - SaveNgOp(ng_op_map, op->name(), ng_argmax); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_argmax); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_argmax); return Status::OK(); } static Status TranslateArgMinOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_dim; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_dim)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); std::vector tf_dim; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &tf_dim)); @@ -595,16 +600,16 @@ static Status TranslateArgMinOp( TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); auto ng_argmin = make_shared(ng_input, input_dims, ng_et); - SaveNgOp(first_ng_op_map, op->name(), ng_argmin); - SaveNgOp(ng_op_map, op->name(), ng_argmin); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_argmin); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_argmin); return Status::OK(); } static Status TranslateAvgPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); std::vector tf_strides; std::vector tf_ksize; @@ -635,7 +640,7 @@ static Status TranslateAvgPoolOp( BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); @@ -655,21 +660,21 @@ static Status TranslateAvgPoolOp( make_shared(ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above, false); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_avgpool); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_avgpool); } BatchToTensorflow(is_nhwc, ng_avgpool); NGRAPH_VLOG(3) << "avgpool outshape: {" << ng::join(ng_avgpool->get_shape()) << "}"; - SaveNgOp(ng_op_map, op->name(), ng_avgpool); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_avgpool); return Status::OK(); } static Status TranslateAvgPoolGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_grad; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, nullptr, &ng_grad)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_grad)); std::vector tf_orig_input_shape_vec; TF_RETURN_IF_ERROR( @@ -709,7 +714,7 @@ static Status TranslateAvgPoolGradOp( BatchedOpParamReshape(is_nhwc, ng_orig_input_shape, ng_forward_arg_shape); BatchToNGraph(is_nhwc, ng_grad); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_grad); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_grad); } BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_strides); @@ -736,23 +741,23 @@ static Status TranslateAvgPoolGradOp( ng_forward_arg_shape, ng_grad, ng_window_shape, ng_strides, ng_padding_below, ng_padding_above, false); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_avgpool_backprop); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_avgpool_backprop); } BatchToTensorflow(is_nhwc, ng_avgpool_backprop); NGRAPH_VLOG(3) << "avgpoolbackprop outshape: {" << ng::join(ng_avgpool_backprop->get_shape()) << "}"; - SaveNgOp(ng_op_map, op->name(), ng_avgpool_backprop); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_avgpool_backprop); return Status::OK(); } static Status TranslateBatchMatMulOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); auto ng_lhs_shape = ng_lhs->get_shape(); auto ng_rhs_shape = ng_rhs->get_shape(); @@ -792,20 +797,20 @@ static Status TranslateBatchMatMulOp( ng_lhs_axes.push_back(n_dims - 2); ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng_lhs_axes); got_first_node = true; - SaveNgOp(first_ng_op_map, op->name(), ng_lhs); + SaveNgOp(entry_nodes_ng_op_map, 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); got_first_node = true; - SaveNgOp(first_ng_op_map, op->name(), ng_rhs); + SaveNgOp(entry_nodes_ng_op_map, 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); got_first_node = true; - SaveNgOp(first_ng_op_map, op->name(), ng_rhs); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_rhs); } ng_lhs_shape = ng_lhs->get_shape(); @@ -819,16 +824,16 @@ static Status TranslateBatchMatMulOp( if (n_dims == 2) { auto dot_output = make_shared(ng_lhs, ng_rhs); if (!got_first_node) { - SaveNgOp(first_ng_op_map, op->name(), dot_output); + SaveNgOp(entry_nodes_ng_op_map, op->name(), dot_output); } - SaveNgOp(ng_op_map, op->name(), dot_output); + SaveNgOp(output_nodes_ng_op_map, op->name(), dot_output); } else { auto output_shape = ng_lhs_shape; output_shape[n_dims - 1] = ng_rhs_shape[1]; auto dot_output = make_shared(ng_lhs, ng_rhs); if (!got_first_node) { - SaveNgOp(first_ng_op_map, op->name(), dot_output); + SaveNgOp(entry_nodes_ng_op_map, op->name(), dot_output); } size_t compound_size = 1; for (int i = 0; i < out_axes.size(); i++) { @@ -863,9 +868,9 @@ static Status TranslateBatchMatMulOp( } auto concat_op = make_shared(tmp_tensors, 0); if (n_dims == 3) { - SaveNgOp(ng_op_map, op->name(), concat_op); + SaveNgOp(output_nodes_ng_op_map, op->name(), concat_op); } else { - SaveNgOp(ng_op_map, op->name(), + SaveNgOp(output_nodes_ng_op_map, op->name(), make_shared( concat_op, ng::AxisVector{0, 1, 2}, output_shape)); } @@ -875,9 +880,9 @@ static Status TranslateBatchMatMulOp( static Status TranslateBiasAddOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_bias; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_bias)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_bias)); std::string tf_data_format; if (GetNodeAttr(op->attrs(), "data_format", &tf_data_format) != @@ -915,18 +920,18 @@ static Status TranslateBiasAddOp( auto ng_bias_broadcasted = make_shared( ng_bias, ng_input_shape, ng_broadcast_axes); - SaveNgOp(first_ng_op_map, op->name(), ng_bias_broadcasted); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_bias_broadcasted); auto ng_add = ng_input + ng_bias_broadcasted; - SaveNgOp(ng_op_map, op->name(), ng_add); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_add); return Status::OK(); } static Status TranslateBiasAddGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); std::string tf_data_format; if (GetNodeAttr(op->attrs(), "data_format", &tf_data_format) != @@ -966,16 +971,16 @@ static Status TranslateBiasAddGradOp( } ng_biasadd_backprop = make_shared(ng_input, reduction_axes); - SaveNgOp(first_ng_op_map, op->name(), ng_biasadd_backprop); - SaveNgOp(ng_op_map, op->name(), ng_biasadd_backprop); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_biasadd_backprop); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_biasadd_backprop); return Status::OK(); } static Status TranslateCastOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); DataType dtype; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "DstT", &dtype)); @@ -985,8 +990,8 @@ static Status TranslateCastOp( try { auto result = make_shared(ng_input, ng_et); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); } catch (const std::out_of_range&) { return errors::Unimplemented("Unsupported TensorFlow data type: ", DataType_Name(dtype)); @@ -996,7 +1001,7 @@ static Status TranslateCastOp( static Status TranslateConcatV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCountMin(op, 2)); std::vector tf_concat_axis_vec; @@ -1007,7 +1012,7 @@ static Status TranslateConcatV2Op( if (concat_axis < 0) { shared_ptr ng_first_arg; - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_first_arg)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_first_arg)); concat_axis += int64(ng_first_arg->get_shape().size()); } @@ -1016,19 +1021,19 @@ static Status TranslateConcatV2Op( for (int i = 0; i < op->num_inputs() - 1; i++) { shared_ptr ng_arg; - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, i, &ng_arg)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, i, &ng_arg)); ng_args.push_back(ng_arg); } auto result = make_shared(ng_args, size_t(concat_axis)); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateConstOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { DataType dtype; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "dtype", &dtype)); @@ -1051,16 +1056,16 @@ static Status TranslateConstOp( return errors::Unimplemented("Unsupported TensorFlow data type: ", DataType_Name(dtype)); } - SaveNgOp(first_ng_op_map, op->name(), ng_node); - SaveNgOp(ng_op_map, op->name(), ng_node); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_node); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_node); return Status::OK(); } static Status TranslateConv2DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_filter; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_filter)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); std::vector tf_strides; std::vector tf_dilations; @@ -1101,7 +1106,7 @@ static Status TranslateConv2DOp( BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); @@ -1111,7 +1116,7 @@ static Status TranslateConv2DOp( ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - SaveNgOp(first_ng_op_map, op->name(), ng_filter); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1126,19 +1131,17 @@ static Status TranslateConv2DOp( ng_input, ng_filter, ng_strides, ng_dilations, ng_padding_below, ng_padding_above); - SaveNgOp(first_ng_op_map, op->name(), ng_conv); - BatchToTensorflow(is_nhwc, ng_conv); - SaveNgOp(ng_op_map, op->name(), ng_conv); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_conv); return Status::OK(); } static Status TranslateConv2DBackpropFilterOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_data_batch, ng_output_delta; TF_RETURN_IF_ERROR( - GetInputNodes(ng_op_map, op, &ng_data_batch, nullptr, &ng_output_delta)); + GetInputNodes(output_nodes_ng_op_map, op, &ng_data_batch, nullptr, &ng_output_delta)); std::vector tf_strides; std::string tf_padding_type; @@ -1201,7 +1204,7 @@ static Status TranslateConv2DBackpropFilterOp( // nGraph Dilation Stride [f] BatchToNGraph(is_nhwc, ng_data_batch); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_data_batch); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_data_batch); } // tf_filter shape : // [filter_height, filter_width, in_channels, out_channels] @@ -1212,7 +1215,7 @@ static Status TranslateConv2DBackpropFilterOp( static_cast(tf_filter_sizes[1])}; BatchToNGraph(is_nhwc, ng_output_delta); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_output_delta); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_output_delta); } BatchedOpParamToNGraph(is_nhwc, tf_strides, ng_window_movement_strides_forward); @@ -1252,22 +1255,22 @@ static Status TranslateConv2DBackpropFilterOp( ng_window_dilation_strides_forward, ng_padding_below_forward, ng_padding_above_forward, ng_data_dilation_strides_forward); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_back_prop_filter); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_back_prop_filter); } // Reshape the output to tf format : [filter_height, filter_width, // in_channels, out_channels] Reshape<2, 3, 1, 0>(ng_back_prop_filter); - SaveNgOp(ng_op_map, op->name(), ng_back_prop_filter); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_back_prop_filter); return Status::OK(); } static Status TranslateConv2DBackpropInputOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_filter, ng_out_backprop; TF_RETURN_IF_ERROR( - GetInputNodes(ng_op_map, op, nullptr, &ng_filter, &ng_out_backprop)); + GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_filter, &ng_out_backprop)); // TODO: refactor me to be less redundant with other convolution ops std::vector tf_strides; @@ -1313,7 +1316,7 @@ static Status TranslateConv2DBackpropInputOp( BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); BatchToNGraph(is_nhwc, ng_out_backprop); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_out_backprop); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_out_backprop); ng_batch_shape = {static_cast(tf_input_sizes[0]), static_cast(tf_input_sizes[3]), static_cast(tf_input_sizes[1]), @@ -1333,7 +1336,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); - SaveNgOp(first_ng_op_map, op->name(), ng_filter); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1350,20 +1353,18 @@ static Status TranslateConv2DBackpropInputOp( ng_padding_below, ng_padding_above, ng::Strides(ng_batch_shape.size() - 2, 1)); - SaveNgOp(first_ng_op_map, op->name(), ng_data); - BatchToTensorflow(is_nhwc, ng_data); - SaveNgOp(ng_op_map, op->name(), ng_data); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_data); return Status::OK(); } // Translate Conv3D Op static Status TranslateConv3DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_filter; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_filter)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); std::vector tf_strides; std::vector tf_dilations; @@ -1405,7 +1406,7 @@ static Status TranslateConv3DOp( BatchedOpParam3DToNGraph(is_ndhwc, tf_dilations, ng_dilations); BatchToNGraph3D(is_ndhwc, ng_input); if (is_ndhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_dilations: " << ng::join(ng_dilations); @@ -1416,7 +1417,7 @@ static Status TranslateConv3DOp( ng_kernel_shape[1] = ng_filter_shape[1]; ng_kernel_shape[2] = ng_filter_shape[2]; Reshape3D<4, 3, 0, 1, 2>(ng_filter); - SaveNgOp(first_ng_op_map, op->name(), ng_filter); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); @@ -1430,20 +1431,18 @@ static Status TranslateConv3DOp( std::shared_ptr ng_conv = make_shared( ng_input, ng_filter, ng_strides, ng_dilations, ng_padding_below, ng_padding_above); - if (!is_ndhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_conv); - } + BatchToTensorflow3D(is_ndhwc, ng_conv); - SaveNgOp(ng_op_map, op->name(), ng_conv); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_conv); return Status::OK(); } // Translate DepthToSpace op static Status TranslateDepthToSpaceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); // Get the attributes int64 block_size; @@ -1580,7 +1579,7 @@ static Status TranslateDepthToSpaceOp( std::iota(ng_axis_order.begin(), ng_axis_order.end(), 0); auto reshaped = make_shared(ng_input, ng_axis_order, ng_reshape_shape); - SaveNgOp(first_ng_op_map, op->name(), reshaped); + SaveNgOp(entry_nodes_ng_op_map, op->name(), reshaped); auto transposed = ng::builder::numpy_transpose(reshaped, ng_transpose_permutation); @@ -1590,16 +1589,16 @@ static Status TranslateDepthToSpaceOp( ng_axis_order_second_reshape.end(), 0); auto final_reshape = make_shared( transposed, ng_axis_order_second_reshape, ng_output_shape); - SaveNgOp(ng_op_map, op->name(), final_reshape); + SaveNgOp(output_nodes_ng_op_map, op->name(), final_reshape); return Status::OK(); } static Status TranslateDepthwiseConv2dNativeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_filter; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_filter)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); std::vector tf_strides; std::vector tf_dilations; @@ -1632,7 +1631,7 @@ static Status TranslateDepthwiseConv2dNativeOp( BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); @@ -1643,7 +1642,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); - SaveNgOp(first_ng_op_map, op->name(), ng_filter); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_filter); NGRAPH_VLOG(3) << "ng_kernel_shape: " << ng::join(ng_kernel_shape); ng::CoordinateDiff ng_padding_below{0, 0}; @@ -1666,7 +1665,7 @@ static Status TranslateDepthwiseConv2dNativeOp( auto ng_sliced_input = make_shared(ng_input, lower_bound, upper_bound); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_sliced_input); } const std::vector f_lower_bound{0, i, 0, 0}; const std::vector f_upper_bound{filter_shape[0], i + 1, @@ -1690,15 +1689,15 @@ static Status TranslateDepthwiseConv2dNativeOp( make_shared(ng_args, ng_concatenation_axis); BatchToTensorflow(is_nhwc, ng_concat); - SaveNgOp(ng_op_map, op->name(), ng_concat); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_concat); return Status::OK(); } static Status TranslateExpandDimsOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_dim; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_dim)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); std::vector dim_vec; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &dim_vec)); @@ -1721,16 +1720,16 @@ static Status TranslateExpandDimsOp( std::iota(shape_dimensions.begin(), shape_dimensions.end(), 0); std::shared_ptr ng_expand_dim = make_shared(ng_input, shape_dimensions, out_shape); - SaveNgOp(first_ng_op_map, op->name(), ng_expand_dim); - SaveNgOp(ng_op_map, op->name(), ng_expand_dim); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_expand_dim); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_expand_dim); return Status::OK(); } static Status TranslateFillOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_value; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, nullptr, &ng_value)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_value)); std::vector dims_vec; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 0, static_input_map, &dims_vec)); @@ -1744,47 +1743,47 @@ static Status TranslateFillOp( auto ng_broadcast = make_shared(ng_value, ng_output_shape, ng_axis_set); - SaveNgOp(first_ng_op_map, op->name(), ng_broadcast); - SaveNgOp(ng_op_map, op->name(), ng_broadcast); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_broadcast); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_broadcast); return Status::OK(); } static Status TranslateFloorDivOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); auto ng_div = std::make_shared(ng_lhs, ng_rhs); - SaveNgOp(first_ng_op_map, op->name(), ng_div); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_div); auto ng_floordiv = std::make_shared(ng_div); - SaveNgOp(ng_op_map, op->name(), ng_floordiv); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_floordiv); return Status::OK(); } static Status TranslateFloorModOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); auto ng_div = std::make_shared(ng_lhs, ng_rhs); - SaveNgOp(first_ng_op_map, op->name(), ng_div); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_div); auto floordiv = std::make_shared(ng_div); auto ng_floormod = std::make_shared( ng_lhs, std::make_shared(floordiv, ng_rhs)); - SaveNgOp(ng_op_map, op->name(), ng_floormod); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_floormod); return Status::OK(); } static Status TranslateFusedBatchNormOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { bool tf_is_training; if (GetNodeAttr(op->attrs(), "is_training", &tf_is_training) != Status::OK()) { @@ -1795,7 +1794,7 @@ static Status TranslateFusedBatchNormOp( NGRAPH_VLOG(3) << "is_training: " << tf_is_training; shared_ptr ng_input, ng_scale, ng_offset, ng_mean, ng_variance; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_scale, + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_scale, &ng_offset, &ng_mean, &ng_variance)); std::string tf_data_format; @@ -1821,7 +1820,7 @@ static Status TranslateFusedBatchNormOp( BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } std::shared_ptr ng_batch_norm; @@ -1830,7 +1829,7 @@ static Status TranslateFusedBatchNormOp( ng_batch_norm = make_shared(tf_epsilon, ng_scale, ng_offset, ng_input); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_batch_norm); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_batch_norm); } shared_ptr ng_y, ng_mean, ng_variance; ng_y = make_shared(ng_batch_norm, 0); @@ -1850,24 +1849,24 @@ static Status TranslateFusedBatchNormOp( BatchToTensorflow(is_nhwc, ng_y); - SaveNgOp(ng_op_map, op->name(), ng_y); - SaveNgOp(ng_op_map, op->name(), ng_mean); - SaveNgOp(ng_op_map, op->name(), variance); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_y); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_mean); + SaveNgOp(output_nodes_ng_op_map, op->name(), variance); // Output reserve_space_1: A 1D Tensor for the computed batch mean, to be // reused in the gradient computation. - SaveNgOp(ng_op_map, op->name(), ng_mean); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_mean); // Output reserve_space_2: A 1D Tensor for the computed batch variance //(inverted variance in the cuDNN case), to be reused in the gradient // computation. - SaveNgOp(ng_op_map, op->name(), ng_variance); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_variance); } else { ng_batch_norm = make_shared( tf_epsilon, ng_scale, ng_offset, ng_input, ng_mean, ng_variance); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_batch_norm); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_batch_norm); } BatchToTensorflow(is_nhwc, ng_batch_norm); - SaveNgOp(ng_op_map, op->name(), ng_batch_norm); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_batch_norm); } return Status::OK(); @@ -1875,7 +1874,7 @@ static Status TranslateFusedBatchNormOp( static Status TranslateFusedBatchNormGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCount(op, 5)); bool tf_is_training; @@ -1894,7 +1893,7 @@ static Status TranslateFusedBatchNormGradOp( shared_ptr ng_scale; shared_ptr ng_mean; shared_ptr ng_variance; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_delta, &ng_input, + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_delta, &ng_input, &ng_scale, &ng_mean, &ng_variance)); std::string tf_data_format; @@ -1925,15 +1924,15 @@ static Status TranslateFusedBatchNormGradOp( shared_ptr ng_beta = std::make_shared( ng_scale->get_element_type(), ng_scale->get_shape(), std::vector{ng::shape_size(ng_scale->get_shape()), "0"}); - SaveNgOp(first_ng_op_map, op->name(), ng_beta); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_beta); BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } BatchToNGraph(is_nhwc, ng_delta); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_delta); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_delta); } std::shared_ptr ng_batch_norm_backprop; @@ -1950,47 +1949,47 @@ static Status TranslateFusedBatchNormGradOp( BatchToTensorflow(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); - SaveNgOp(ng_op_map, op->name(), ng_beta_delta_op); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_input_delta_op); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_scale_delta_op); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_beta_delta_op); // Output reserve_space_3: Unused placeholder to match the mean input // in FusedBatchNorm. std::shared_ptr output_mean = make_shared( ng_mean->get_element_type(), ng::Shape{}, std::vector{""}); - SaveNgOp(ng_op_map, op->name(), output_mean); + SaveNgOp(output_nodes_ng_op_map, op->name(), output_mean); // Output reserve_space_4: Unused placeholder to match the variance input // in FusedBatchNorm. std::shared_ptr output_variance = make_shared( ng_variance->get_element_type(), ng::Shape{}, std::vector{""}); - SaveNgOp(ng_op_map, op->name(), output_variance); + SaveNgOp(output_nodes_ng_op_map, op->name(), output_variance); return Status::OK(); } static Status TranslateIdentityOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_arg; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_arg)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_arg)); // TODO (malikshr): No op created : control dependency? - SaveNgOp(ng_op_map, op->name(), ng_arg); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_arg); return Status::OK(); } static Status TranslateL2LossOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto const_2 = make_shared( ng_input->get_element_type(), ng::Shape{}, std::vector{"2"}); - SaveNgOp(first_ng_op_map, op->name(), const_2); + SaveNgOp(entry_nodes_ng_op_map, op->name(), const_2); std::shared_ptr ng_pow = make_shared(ng_input, ng_input); - SaveNgOp(first_ng_op_map, op->name(), ng_pow); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_pow); size_t input_rank = ng_input->get_shape().size(); ng::AxisSet axes; @@ -2001,15 +2000,15 @@ static Status TranslateL2LossOp( std::shared_ptr ng_sum = make_shared(ng_pow, axes); std::shared_ptr ng_l2loss = make_shared(ng_sum, const_2); - SaveNgOp(ng_op_map, op->name(), ng_l2loss); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_l2loss); return Status::OK(); } static Status TranslateMatMulOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); // Transpose arguments if requested. bool transpose_a = false; @@ -2020,32 +2019,32 @@ static Status TranslateMatMulOp( transpose_a) { ng_lhs = ng::builder::numpy_transpose(ng_lhs, ng::AxisVector{1, 0}); got_first_node = true; - SaveNgOp(first_ng_op_map, op->name(), ng_lhs); + SaveNgOp(entry_nodes_ng_op_map, 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}); got_first_node = true; - SaveNgOp(first_ng_op_map, op->name(), ng_rhs); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_rhs); } // The default axis count for nGraph's Dot op is 1, which is just what // we need here. auto result = make_shared(ng_lhs, ng_rhs); if (!got_first_node) { - SaveNgOp(first_ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); } - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateMaxOp(const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, - Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_max_op; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_max_op)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_max_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -2068,7 +2067,7 @@ static Status TranslateMaxOp(const Node* op, std::shared_ptr ng_max = make_shared(ng_input, ng_reduction_axes); - SaveNgOp(first_ng_op_map, op->name(), ng_max); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_max); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -2086,15 +2085,15 @@ static Status TranslateMaxOp(const Node* op, ng_result_shape_with_keep); } - SaveNgOp(ng_op_map, op->name(), ng_max); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_max); return Status::OK(); } static Status TranslateMaxPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); std::vector tf_strides; std::vector tf_ksize; @@ -2126,7 +2125,7 @@ static Status TranslateMaxPoolOp( BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); @@ -2146,22 +2145,22 @@ static Status TranslateMaxPoolOp( make_shared(ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_maxpool); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_maxpool); } BatchToTensorflow(is_nhwc, ng_maxpool); NGRAPH_VLOG(3) << "maxpool outshape: {" << ng::join(ng_maxpool->get_shape()) << "}"; - SaveNgOp(ng_op_map, op->name(), ng_maxpool); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_maxpool); return Status::OK(); } static Status TranslateMaxPool3DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); std::vector tf_strides; std::vector tf_ksize; @@ -2193,7 +2192,7 @@ static Status TranslateMaxPool3DOp( BatchedOpParam3DToNGraph(is_ndhwc, tf_ksize, ng_kernel_shape); BatchToNGraph3D(is_ndhwc, ng_input); if (is_ndhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); @@ -2213,23 +2212,23 @@ static Status TranslateMaxPool3DOp( make_shared(ng_input, ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); if (!is_ndhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_maxpool); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_maxpool); } BatchToTensorflow3D(is_ndhwc, ng_maxpool); NGRAPH_VLOG(3) << "maxpool outshape: {" << ng::join(ng_maxpool->get_shape()) << "}"; - SaveNgOp(ng_op_map, op->name(), ng_maxpool); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_maxpool); return Status::OK(); } static Status TranslateMaxPoolGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_grad, ng_fwd; TF_RETURN_IF_ERROR( - GetInputNodes(ng_op_map, op, &ng_input, &ng_fwd, &ng_grad)); + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_fwd, &ng_grad)); std::vector tf_strides; std::vector tf_ksize; @@ -2259,15 +2258,15 @@ static Status TranslateMaxPoolGradOp( BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } BatchToNGraph(is_nhwc, ng_grad); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_grad); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_grad); } BatchToNGraph(is_nhwc, ng_fwd); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_fwd); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_fwd); } NGRAPH_VLOG(3) << "ng_strides: " << ng::join(ng_strides); NGRAPH_VLOG(3) << "ng_image_shape: " << ng::join(ng_image_shape); @@ -2284,20 +2283,20 @@ static Status TranslateMaxPoolGradOp( ng_kernel_shape, ng_strides, ng_padding_below, ng_padding_above); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_maxpool_backprop); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_maxpool_backprop); } BatchToTensorflow(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); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_maxpool_backprop); return Status::OK(); } static Status TranslateMeanOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_axes_op; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_axes_op)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -2320,7 +2319,7 @@ static Status TranslateMeanOp( std::shared_ptr ng_mean = ng::builder::mean(ng_input, ng_reduction_axes); - SaveNgOp(first_ng_op_map, op->name(), ng_mean); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_mean); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -2338,16 +2337,16 @@ static Status TranslateMeanOp( ng_result_shape_with_keep); } - SaveNgOp(ng_op_map, op->name(), ng_mean); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_mean); return Status::OK(); } static Status TranslateMinOp(const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, - Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_min_op; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_min_op)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_min_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -2370,7 +2369,7 @@ static Status TranslateMinOp(const Node* op, std::shared_ptr ng_min = make_shared(ng_input, ng_reduction_axes); - SaveNgOp(first_ng_op_map, op->name(), ng_min); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_min); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. @@ -2389,20 +2388,20 @@ static Status TranslateMinOp(const Node* op, ng_result_shape_with_keep); } - SaveNgOp(ng_op_map, op->name(), ng_min); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_min); return Status::OK(); } static Status TranslatePackOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCountMin(op, 1)); ng::NodeVector ng_concat_inputs; for (size_t i = 0; i < op->num_inputs(); ++i) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, i, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, i, &ng_input)); ng_concat_inputs.push_back(ng_input); } @@ -2436,26 +2435,26 @@ static Status TranslatePackOp( for (size_t i = 0; i < ng_concat_inputs.size(); ++i) { ng_concat_inputs[i] = make_shared( ng_concat_inputs[i], ng_axis_order, extended_shape); - SaveNgOp(first_ng_op_map, op->name(), ng_concat_inputs[i]); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_concat_inputs[i]); } ng_axis_order.push_back(input_rank); } auto concat = make_shared(ng_concat_inputs, concat_axis); if (concat_axis != input_rank) { - SaveNgOp(first_ng_op_map, op->name(), concat); + SaveNgOp(entry_nodes_ng_op_map, op->name(), concat); } - SaveNgOp(ng_op_map, op->name(), + SaveNgOp(output_nodes_ng_op_map, op->name(), make_shared(concat, ng_axis_order, output_shape)); return Status::OK(); } static Status TranslatePadOp(const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, - Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_paddings_op; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_paddings_op)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_paddings_op)); std::vector paddings; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &paddings)); @@ -2484,20 +2483,20 @@ static Status TranslatePadOp(const Node* op, // For PadV1 it seems the value is always zero. auto pad_val_op = make_shared( ng_input->get_element_type(), ng::Shape{}, std::vector{"0"}); - SaveNgOp(first_ng_op_map, op->name(), pad_val_op); + SaveNgOp(entry_nodes_ng_op_map, op->name(), pad_val_op); auto pad_op = make_shared(ng_input, pad_val_op, padding_below, padding_above, padding_interior); - SaveNgOp(ng_op_map, op->name(), pad_op); + SaveNgOp(output_nodes_ng_op_map, op->name(), pad_op); return Status::OK(); } static Status TranslateProdOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_axes_op; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_axes_op)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -2520,7 +2519,7 @@ static Status TranslateProdOp( std::shared_ptr ng_prod = make_shared(ng_input, ng_reduction_axes); - SaveNgOp(first_ng_op_map, op->name(), ng_prod); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_prod); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -2538,40 +2537,40 @@ static Status TranslateProdOp( ng_result_shape_with_keep); } - SaveNgOp(ng_op_map, op->name(), ng_prod); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_prod); return Status::OK(); } static Status TranslateRankOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); ng::Shape input_shape = ng_input->get_shape(); auto input_rank = static_cast(input_shape.size()); auto ng_rank = std::make_shared( ng::element::i32, ng::Shape(), std::vector({input_rank})); - SaveNgOp(first_ng_op_map, op->name(), ng_rank); - SaveNgOp(ng_op_map, op->name(), ng_rank); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_rank); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_rank); return Status::OK(); } static Status TranslateReciprocalOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto et = ng_input->get_element_type(); auto shape = ng_input->get_shape(); std::vector constant_values(ng::shape_size(shape), "-1"); auto ng_exponent = std::make_shared(et, shape, constant_values); - SaveNgOp(first_ng_op_map, op->name(), ng_exponent); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_exponent); auto ng_reciprocal = std::make_shared(ng_input, ng_exponent); - SaveNgOp(ng_op_map, op->name(), ng_reciprocal); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_reciprocal); return Status::OK(); } @@ -2703,9 +2702,9 @@ Status QuantizeAndDequantizeV2Helper( static Status TranslateQuantizeAndDequantizeV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, nullptr, nullptr)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); bool range_given; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "range_given", &range_given)); @@ -2750,15 +2749,15 @@ static Status TranslateQuantizeAndDequantizeV2Op( } auto ng_scale = std::make_shared( ng_r_et, ng::Shape(), std::vector({scale})); - SaveNgOp(first_ng_op_map, op->name(), ng_scale); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_scale); auto ng_offset = std::make_shared(ng_q_et, ng::Shape(), std::vector({0})); - SaveNgOp(first_ng_op_map, op->name(), ng_offset); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_offset); ng::op::Quantize::RoundMode ng_round_mode = ng::op::Quantize::RoundMode::ROUND_NEAREST_TOWARD_INFINITY; auto ng_quant = make_shared( ng_input, ng_scale, ng_offset, ng_q_et, ng::AxisSet(), ng_round_mode); - SaveNgOp(ng_op_map, op->name(), + SaveNgOp(output_nodes_ng_op_map, op->name(), make_shared(ng_quant, ng_scale, ng_offset, ng_r_et, ng::AxisSet())); @@ -2768,11 +2767,11 @@ static Status TranslateQuantizeAndDequantizeV2Op( static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_filter, ng_bias; - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_input)); - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 1, &ng_filter)); - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 2, &ng_bias)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 1, &ng_filter)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 2, &ng_bias)); std::vector> static_inps(6); for (int i = 0; i < static_inps.size(); i++) { std::vector tmp_vect; @@ -2785,7 +2784,7 @@ static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( } static_inps[i] = std::make_shared( ng::element::f32, ng::Shape({}), tmp_vect); - SaveNgOp(first_ng_op_map, op->name(), static_inps[i]); + SaveNgOp(entry_nodes_ng_op_map, op->name(), static_inps[i]); } std::vector tf_strides; std::vector tf_dilations; @@ -2804,13 +2803,13 @@ static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( BatchedOpParamToNGraph(is_nhwc, tf_dilations, ng_dilations); BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } auto& ng_filter_shape = ng_filter->get_shape(); ng_kernel_shape[0] = ng_filter_shape[0]; ng_kernel_shape[1] = ng_filter_shape[1]; Reshape<3, 2, 0, 1>(ng_filter); - SaveNgOp(first_ng_op_map, op->name(), ng_filter); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_filter); 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, @@ -2827,19 +2826,19 @@ static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( static_inps[1], static_inps[2], static_inps[3], static_inps[4], static_inps[5], true); BatchToTensorflow(is_nhwc, ng_quant_conv_bias); - SaveNgOp(ng_op_map, op->name(), ng_quant_conv_bias); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_quant_conv_bias); // Forward the min_freezed_output input to output min - SaveNgOp(ng_op_map, op->name(), static_inps[4]); + SaveNgOp(output_nodes_ng_op_map, op->name(), static_inps[4]); // Forward the max_freezed_output input to output max - SaveNgOp(ng_op_map, op->name(), static_inps[5]); + SaveNgOp(output_nodes_ng_op_map, op->name(), static_inps[5]); return Status::OK(); } static Status TranslateQuantizedMaxPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_min, ng_max; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_min, &ng_max)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_min, &ng_max)); std::vector tf_strides; std::vector tf_ksize; std::string tf_padding_type; @@ -2856,7 +2855,7 @@ static Status TranslateQuantizedMaxPoolOp( BatchedOpParamToNGraph(is_nhwc, tf_ksize, ng_kernel_shape); BatchToNGraph(is_nhwc, ng_input); if (is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } ng::Shape ng_padding_below{0, 0}; ng::Shape ng_padding_above{0, 0}; @@ -2872,22 +2871,22 @@ static Status TranslateQuantizedMaxPoolOp( ng_padding_below, ng_padding_above, dummy_min, dummy_max); if (!is_nhwc) { - SaveNgOp(first_ng_op_map, op->name(), ng_quant_maxpool); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_quant_maxpool); } BatchToTensorflow(is_nhwc, ng_quant_maxpool); - SaveNgOp(ng_op_map, op->name(), ng_quant_maxpool); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_quant_maxpool); // For maxpool input min-max remains unchanged and is just propagated along // https://github.com/tensorflow/tensorflow/blob/9590c4c32dd4346ea5c35673336f5912c6072bf2/tensorflow/core/kernels/quantized_pooling_ops.cc#L99 - SaveNgOp(ng_op_map, op->name(), ng_min); - SaveNgOp(ng_op_map, op->name(), ng_max); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_min); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_max); return Status::OK(); } static Status TranslateQuantizeV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, nullptr, nullptr)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); std::vector ng_min, ng_max; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &ng_min)); @@ -2949,15 +2948,15 @@ static Status TranslateQuantizeV2Op( (mode.compare("SCALED") == 0), ng_min[0], ng_max[0], &ng_scale_val, &ng_offset_val); } catch (const std::exception& e) { - return errors::Internal("Unhandled exception in ComputeScaleOffset: ", - op->name(), " (", op->type_string(), ")\n", - op->def().DebugString(), "\n", "what(): ", - e.what()); + return errors::Internal( + "Unhandled exception in ComputeScaleOffset: ", op->name(), " (", + op->type_string(), ")\n", op->def().DebugString(), "\n", + "what(): ", e.what()); } auto ng_scale = std::make_shared( ng::element::f32, ng::Shape(), std::vector({ng_scale_val})); - SaveNgOp(first_ng_op_map, op->name(), ng_scale); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_scale); ng::element::Type ng_et; TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); @@ -2965,35 +2964,35 @@ static Status TranslateQuantizeV2Op( // Cast offset appropriately? Currently using int auto ng_offset = std::make_shared( ng_et, ng::Shape(), std::vector({ng_offset_val})); - SaveNgOp(first_ng_op_map, op->name(), ng_offset); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_offset); // TODO: Only RoundMode = ROUND_NEAREST_TOWARD_INFINITY is supported, for now. // Support HALF_TO_EVEN later ng::op::Quantize::RoundMode ng_round_mode = ng::op::Quantize::RoundMode::ROUND_NEAREST_TOWARD_INFINITY; - SaveNgOp(ng_op_map, op->name(), + SaveNgOp(output_nodes_ng_op_map, op->name(), make_shared(ng_input, ng_scale, ng_offset, ng_et, ng::AxisSet(), ng_round_mode)); auto ng_min_res = make_shared( ng::element::f32, ng::Shape(), std::vector({ng_min[0]})); - SaveNgOp(first_ng_op_map, op->name(), ng_min_res); - SaveNgOp(ng_op_map, op->name(), ng_min_res); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_min_res); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_min_res); // TODO: For quantizev2 revisit output min-max (which would change in case // input min-max are too close. For now just propagating inputs auto ng_max_res = make_shared( ng::element::f32, ng::Shape(), std::vector({ng_max[0]})); - SaveNgOp(first_ng_op_map, op->name(), ng_max_res); - SaveNgOp(ng_op_map, op->name(), ng_max_res); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_max_res); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_max_res); return Status::OK(); } static Status TranslateDequantizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, nullptr, nullptr)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); std::vector ng_min, ng_max; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &ng_min)); @@ -3046,15 +3045,15 @@ static Status TranslateDequantizeOp( (mode.compare("SCALED") == 0), ng_min[0], ng_max[0], &ng_scale_val, &ng_offset_val); } catch (const std::exception& e) { - return errors::Internal("Unhandled exception in ComputeScaleOffset: ", - op->name(), " (", op->type_string(), ")\n", - op->def().DebugString(), "\n", "what(): ", - e.what()); + return errors::Internal( + "Unhandled exception in ComputeScaleOffset: ", op->name(), " (", + op->type_string(), ")\n", op->def().DebugString(), "\n", + "what(): ", e.what()); } auto ng_scale = std::make_shared( ng::element::f32, ng::Shape(), std::vector({ng_scale_val})); - SaveNgOp(first_ng_op_map, op->name(), ng_scale); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_scale); ng::element::Type ng_et; TF_RETURN_IF_ERROR(TFDataTypeToNGraphElementType(dtype, &ng_et)); @@ -3062,9 +3061,9 @@ static Status TranslateDequantizeOp( // Cast offset appropriately? Currently using int auto ng_offset = std::make_shared( ng_et, ng::Shape(), std::vector({ng_offset_val})); - SaveNgOp(first_ng_op_map, op->name(), ng_offset); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_offset); - SaveNgOp(ng_op_map, op->name(), + SaveNgOp(output_nodes_ng_op_map, op->name(), make_shared(ng_input, ng_scale, ng_offset, ng::element::f32, ng::AxisSet())); return Status::OK(); @@ -3072,51 +3071,51 @@ static Status TranslateDequantizeOp( static Status TranslateReluOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto result = make_shared(ng_input); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateRelu6Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto constant_6 = make_shared( ng_input->get_element_type(), ng_input->get_shape(), std::vector(ng::shape_size(ng_input->get_shape()), "6")); - SaveNgOp(first_ng_op_map, op->name(), constant_6); + SaveNgOp(entry_nodes_ng_op_map, op->name(), constant_6); auto relu6_op = make_shared( make_shared(ng_input), constant_6); - SaveNgOp(ng_op_map, op->name(), relu6_op); + SaveNgOp(output_nodes_ng_op_map, op->name(), relu6_op); return Status::OK(); } static Status TranslateReluGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_arg, ng_delta; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_delta, &ng_arg)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_delta, &ng_arg)); auto ng_relu_grad = std::make_shared(ng_arg, ng_delta); - SaveNgOp(first_ng_op_map, op->name(), ng_relu_grad); - SaveNgOp(ng_op_map, op->name(), ng_relu_grad); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_relu_grad); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_relu_grad); return Status::OK(); } static Status TranslateReshapeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_shape_op; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_shape_op)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_shape_op)); NGRAPH_VLOG(3) << "Input shape: " << ng::join(ng_input->get_shape()); @@ -3173,36 +3172,36 @@ static Status TranslateReshapeOp( ng::AxisVector ng_axis_order(ng_input->get_shape().size()); std::iota(ng_axis_order.begin(), ng_axis_order.end(), 0); auto result = make_shared(ng_input, ng_axis_order, ng_shape); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateRsqrtOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto et = ng_input->get_element_type(); auto shape = ng_input->get_shape(); std::vector constant_values(ng::shape_size(shape), "-0.5"); auto ng_exponent = std::make_shared(et, shape, constant_values); - SaveNgOp(first_ng_op_map, op->name(), ng_exponent); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_exponent); // Create a constant tensor populated with the value -1/2. // (1/sqrt(x) = x^(-1/2)) // Raise each element of the input to the power -0.5. auto result = std::make_shared(ng_input, ng_exponent); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateShapeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); // the shape of the input tensor which will be the value to the Constant Op auto input_shape = ng_input->get_shape(); @@ -3224,38 +3223,38 @@ static Status TranslateShapeOp( values[i] = input_shape[i]; } auto result = make_shared(type, shape, values); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateSigmoidGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; shared_ptr ng_delta; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_delta)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_delta)); auto ng_mul = ng_input * ng_delta; - SaveNgOp(first_ng_op_map, op->name(), ng_mul); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_mul); auto ng_subtract = make_shared(ng_input->get_element_type(), ng_input->get_shape(), std::vector({1})) - ng_input; auto ng_result = ng_mul * ng_subtract; - SaveNgOp(ng_op_map, op->name(), ng_result); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_result); return Status::OK(); } static Status TranslateSigmoidOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto neg = make_shared(ng_input); - SaveNgOp(first_ng_op_map, op->name(), neg); + SaveNgOp(entry_nodes_ng_op_map, op->name(), neg); auto exp_op = make_shared(neg); auto constant_1 = make_shared( ng_input->get_element_type(), ng_input->get_shape(), @@ -3263,16 +3262,16 @@ static Status TranslateSigmoidOp( auto denominator_op = make_shared(constant_1, exp_op); - SaveNgOp(ng_op_map, op->name(), + SaveNgOp(output_nodes_ng_op_map, op->name(), make_shared(constant_1, denominator_op)); return Status::OK(); } static Status TranslateSizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); DataType dtype; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "out_type", &dtype)); @@ -3291,17 +3290,17 @@ static Status TranslateSizeOp( // make a scalar with value equals to result auto ng_result = make_shared(type, ng::Shape(0), std::vector({result})); - SaveNgOp(first_ng_op_map, op->name(), ng_result); - SaveNgOp(ng_op_map, op->name(), ng_result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_result); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_result); return Status::OK(); } static Status TranslateSliceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_begin, ng_size; TF_RETURN_IF_ERROR( - GetInputNodes(ng_op_map, op, &ng_input, &ng_begin, &ng_size)); + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_begin, &ng_size)); std::vector lower_vec; std::vector size_vec; @@ -3349,26 +3348,26 @@ static Status TranslateSliceOp( std::vector l(lower_vec.begin(), lower_vec.end()); std::vector u(upper_vec.begin(), upper_vec.end()); auto ng_slice = make_shared(ng_input, l, u); - SaveNgOp(first_ng_op_map, op->name(), ng_slice); - SaveNgOp(ng_op_map, op->name(), ng_slice); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_slice); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_slice); return Status::OK(); } static Status TranslateSnapshotOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_arg; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_arg)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_arg)); // TODO (malikshr) : No op is created - SaveNgOp(ng_op_map, op->name(), ng_arg); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_arg); return Status::OK(); } static Status TranslateSoftmaxOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto ng_input_shape = ng_input->get_shape(); ng::AxisSet ng_axes_softmax; @@ -3380,8 +3379,8 @@ static Status TranslateSoftmaxOp( ng_axes_softmax.insert(rank - 1); auto result = make_shared(ng_input, ng_axes_softmax); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } @@ -3389,9 +3388,9 @@ static Status TranslateSoftmaxOp( // Translate SpaceToDepthOp static Status TranslateSpaceToDepthOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); // Get the attributes int64 block_size; @@ -3456,18 +3455,19 @@ static Status TranslateSpaceToDepthOp( strides[height_index] = size_t(block_size); auto temp = make_shared(ng_input, begin, upper, strides); strided_slice_result.push_back(temp); - SaveNgOp(first_ng_op_map, op->name(), temp); + SaveNgOp(entry_nodes_ng_op_map, op->name(), temp); } } - SaveNgOp(ng_op_map, op->name(), make_shared( - strided_slice_result, channel_index)); + SaveNgOp( + output_nodes_ng_op_map, op->name(), + make_shared(strided_slice_result, channel_index)); return Status::OK(); } static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { // TF op Inputs: // 1. Logits/Features: // Shape : [BatchSize, NumOfClasses] @@ -3477,7 +3477,7 @@ static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( // Range : [0, NumOfClasses) // Type : int shared_ptr ng_features, ng_labels; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_features, &ng_labels)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_features, &ng_labels)); ng::Shape ng_features_shape = ng_features->get_shape(); ng::Shape ng_labels_shape = ng_labels->get_shape(); @@ -3531,7 +3531,7 @@ static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( // compute max(logits) and broadcast to shape [B, NC] auto op_max = make_shared(ng_features, ng_axes_class); - SaveNgOp(first_ng_op_map, op->name(), op_max); + SaveNgOp(entry_nodes_ng_op_map, op->name(), op_max); auto max_logits = make_shared(op_max, ng_features_shape, ng_axes_class); @@ -3568,16 +3568,16 @@ static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( auto ng_backprop = make_shared(predicted_prob, ng_onehot_labels_float); - SaveNgOp(ng_op_map, op->name(), ng_loss); - SaveNgOp(ng_op_map, op->name(), ng_backprop); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_loss); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_backprop); return Status::OK(); } static Status TranslateSplitOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, nullptr, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_input)); // num_split : The number of ways to split. Must evenly divide // value.shape[split_dim] int32 num_split; @@ -3606,18 +3606,18 @@ static Status TranslateSplitOp( std::string output_name = op->name(); auto result = make_shared(ng_input, lower, upper); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); } return Status::OK(); } static Status TranslateSplitVOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_length, ng_split_dim; - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)); std::vector lengths; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &lengths)); @@ -3684,14 +3684,14 @@ static Status TranslateSplitVOp( cursor += lengths[i]; upper[split_dim] = cursor; auto result = make_shared(ng_input, lower, upper); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); } } else { // TODO (malikshr): As the slice output is same as input node, no node is // created. So if there is a control dependency for this node, should the // dependency be passed to the input node May be use identity node here - SaveNgOp(ng_op_map, op->name(), ng_input); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_input); } return Status::OK(); @@ -3699,8 +3699,9 @@ static Status TranslateSplitVOp( static Status TranslateSquareOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { - return TranslateUnaryOp(op, static_input_map, ng_op_map, first_ng_op_map, + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + return TranslateUnaryOp(op, static_input_map, output_nodes_ng_op_map, + entry_nodes_ng_op_map, [](std::shared_ptr n) { return std::make_shared(n, n); }); @@ -3708,23 +3709,23 @@ static Status TranslateSquareOp( static Status TranslateSquaredDifferenceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); auto ng_diff = std::make_shared(ng_lhs, ng_rhs); - SaveNgOp(first_ng_op_map, op->name(), ng_diff); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_diff); auto ng_sq_diff = std::make_shared(ng_diff, ng_diff); - SaveNgOp(ng_op_map, op->name(), ng_sq_diff); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_sq_diff); return Status::OK(); } static Status TranslateSqueezeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); size_t input_dims = ng_input->get_shape().size(); std::vector tf_axis; @@ -3774,17 +3775,17 @@ static Status TranslateSqueezeOp( auto result = make_shared(ng_input, ng_axis_order, output_shape); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateStridedSliceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { // TODO: implement new_axis_mask, ellipsis_mask shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)); int tf_shrink_axis_mask; TF_RETURN_IF_ERROR( @@ -3848,10 +3849,9 @@ static Status TranslateStridedSliceOp( // The first 2 cases breaks down this range if (idx >= 0 && idx <= (static_cast(dim) - 1)) { return idx; - } else if (idx < 0 && - idx + static_cast(dim) >= - 0) { // careful not to do idx >= -dim - // (since dim is unsigned) + } else if (idx < 0 && idx + static_cast(dim) >= + 0) { // careful not to do idx >= -dim + // (since dim is unsigned) return idx + static_cast( dim); // Type casting to int to enable unambiguous auto // type inference of return type @@ -4024,7 +4024,7 @@ static Status TranslateStridedSliceOp( // atleast one stride was negative, in which case reverse the input if (neg_strides.size() > 0) { ng_input = make_shared(ng_input, neg_strides); - SaveNgOp(first_ng_op_map, op->name(), ng_input); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_input); } NGRAPH_VLOG(3) << "NG Lower Vector " << ng::join(ng_begin_vec); @@ -4036,7 +4036,7 @@ static Status TranslateStridedSliceOp( ng_input, ng_begin_vec, ng_end_vec, ng_stride_vec); if (neg_strides.size() <= 0) { - SaveNgOp(first_ng_op_map, op->name(), ng_strided_slice); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_strided_slice); } if (tf_shrink_axis_mask) { @@ -4080,16 +4080,16 @@ static Status TranslateStridedSliceOp( // time? // TODO: tf_new_axis_mask can exceed rank - SaveNgOp(ng_op_map, op->name(), ng_strided_slice); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_strided_slice); return Status::OK(); } static Status TranslateSumOp(const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, - Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_axes_op; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_axes_op)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -4113,7 +4113,7 @@ static Status TranslateSumOp(const Node* op, std::shared_ptr ng_sum = make_shared(ng_input, ng_reduction_axes); - SaveNgOp(first_ng_op_map, op->name(), ng_sum); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_sum); // If keep_dims is specified we need to reshape to put back the reduced // axes, with length 1. if (tf_keep_dims) { @@ -4131,7 +4131,7 @@ static Status TranslateSumOp(const Node* op, ng_result_shape_with_keep); } - SaveNgOp(ng_op_map, op->name(), ng_sum); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_sum); return Status::OK(); } @@ -4140,12 +4140,12 @@ static Status TranslateSumOp(const Node* op, // where y = tanh(x) and dy is the corresponding input gradient static Status TranslateTanhGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_delta; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_delta)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_delta)); auto ng_sq = std::make_shared(ng_input, ng_input); - SaveNgOp(first_ng_op_map, op->name(), ng_sq); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_sq); ng::Shape input_shape = ng_input->get_shape(); std::vector const_values(ng::shape_size(input_shape), "1"); auto ng_const = make_shared(ng_input->get_element_type(), @@ -4153,15 +4153,15 @@ static Status TranslateTanhGradOp( auto ng_sub = make_shared(ng_const, ng_sq); auto ng_result = make_shared(ng_delta, ng_sub); - SaveNgOp(ng_op_map, op->name(), ng_result); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_result); return Status::OK(); } static Status TranslateTileOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_multiples; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input, &ng_multiples)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_multiples)); std::vector multiples; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &multiples)); @@ -4184,8 +4184,8 @@ static Status TranslateTileOp( auto result = make_shared( ng_input->get_element_type(), output_shape, std::vector(ng::shape_size(output_shape), "0")); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); } else { for (int i = 0; i < ng_input_shape.size(); i++) { if (multiples[i] < 0) { @@ -4198,22 +4198,22 @@ static Status TranslateTileOp( } auto ng_concat = make_shared(tmp_tensors, i); if (i == 0) { - SaveNgOp(first_ng_op_map, op->name(), ng_concat); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_concat); } ng_output = ng_concat; } - SaveNgOp(ng_op_map, op->name(), ng_output); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_output); } return Status::OK(); } static Status TranslateTransposeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_permutation_op; TF_RETURN_IF_ERROR( - GetInputNodes(ng_op_map, op, &ng_input, &ng_permutation_op)); + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_permutation_op)); std::vector permutation; TF_RETURN_IF_ERROR( @@ -4250,18 +4250,18 @@ static Status TranslateTransposeOp( NGRAPH_VLOG(3) << ng::join(ng_axis_order); auto result = ng::builder::numpy_transpose(ng_input, ng_axis_order); - SaveNgOp(first_ng_op_map, op->name(), result); - SaveNgOp(ng_op_map, op->name(), result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), result); + SaveNgOp(output_nodes_ng_op_map, op->name(), result); return Status::OK(); } static Status TranslateUnpackOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCount(op, 1)); shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, op, 0, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)); ng::Shape input_shape = ng_input->get_shape(); size_t input_rank = input_shape.size(); @@ -4301,26 +4301,26 @@ static Status TranslateUnpackOp( upper_bound[unpack_axis] = i + 1; auto slice = make_shared(ng_input, lower_bound, upper_bound); - SaveNgOp(first_ng_op_map, op->name(), slice); + SaveNgOp(entry_nodes_ng_op_map, op->name(), slice); auto reshaped = make_shared(slice, ng_axis_order, output_shape); - SaveNgOp(ng_op_map, op->name(), reshaped); + SaveNgOp(output_nodes_ng_op_map, op->name(), reshaped); } return Status::OK(); } static Status TranslateZerosLikeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& ng_op_map, Builder::OpMap& first_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(ng_op_map, op, &ng_input)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); ng::Shape input_shape = ng_input->get_shape(); std::vector const_values(ng::shape_size(input_shape), "0"); auto ng_result = make_shared(ng_input->get_element_type(), input_shape, const_values); - SaveNgOp(first_ng_op_map, op->name(), ng_result); - SaveNgOp(ng_op_map, op->name(), ng_result); + SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_result); + SaveNgOp(output_nodes_ng_op_map, op->name(), ng_result); return Status::OK(); } @@ -4476,8 +4476,8 @@ Status Builder::TranslateGraph( // The op map holds a mapping from TensorFlow op names (strings) to // vector of generated nGraph nodes. // - Builder::OpMap ng_op_map; - Builder::OpMap first_ng_op_map; + Builder::OpMap output_nodes_ng_op_map; + Builder::OpMap entry_nodes_ng_op_map; // // Populate the parameter list, and also put parameters into the op map. // @@ -4500,7 +4500,7 @@ Status Builder::TranslateGraph( TF_RETURN_IF_ERROR(TFTensorShapeToNGraphShape(inputs[index], &ng_shape)); auto ng_param = make_shared(ng_et, ng_shape); - SaveNgOp(ng_op_map, parm->name(), ng_param); + SaveNgOp(output_nodes_ng_op_map, parm->name(), ng_param); ng_parameter_list[index] = ng_param; } @@ -4530,12 +4530,12 @@ Status Builder::TranslateGraph( try { TF_RETURN_IF_ERROR( - (*op_fun)(op, static_input_map, ng_op_map, first_ng_op_map)); + (*op_fun)(op, static_input_map, output_nodes_ng_op_map, entry_nodes_ng_op_map)); } catch (const std::exception& e) { return errors::Internal("Unhandled exception in op handler: ", op->name(), " (", op->type_string(), ")\n", - op->def().DebugString(), "\n", "what(): ", - e.what()); + op->def().DebugString(), "\n", + "what(): ", e.what()); } } @@ -4557,7 +4557,7 @@ Status Builder::TranslateGraph( } shared_ptr result; - TF_RETURN_IF_ERROR(GetInputNode(ng_op_map, n, 0, &result)); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, n, 0, &result)); ng_result_list[index] = result; } @@ -4576,8 +4576,8 @@ Status Builder::TranslateGraph( // TODO(malikshr) : add error checks once we are sure that all the // nodes have an entry + exit point // Right now some ops like Snapshot dont have - for (auto ng_src : ng_op_map[src->name()]) { - for (auto ng_dst : first_ng_op_map[dst->name()]) { + for (auto ng_src : output_nodes_ng_op_map[src->name()]) { + for (auto ng_dst : entry_nodes_ng_op_map[dst->name()]) { NGRAPH_VLOG(1) << "Adding control edge in nGraph cluster"; ng_dst->add_control_dependency(ng_src); } From 5d4ce59054d56e33fedc22671829f2a3c498276f Mon Sep 17 00:00:00 2001 From: shresthamalik Date: Fri, 1 Feb 2019 15:01:51 -0800 Subject: [PATCH 8/9] Small changes --- src/ngraph_builder.cc | 411 +++++++++++++++++++++++++++--------------- 1 file changed, 264 insertions(+), 147 deletions(-) diff --git a/src/ngraph_builder.cc b/src/ngraph_builder.cc index bfc98a1b..31239d12 100644 --- a/src/ngraph_builder.cc +++ b/src/ngraph_builder.cc @@ -93,13 +93,15 @@ static void SaveNgOp(Builder::OpMap& ng_op_map, const std::string& op_name, // ng_input = output_nodes_ng_op_map.at(tf_input->name()); // } catch (const std::out_of_range&) { // return errors::NotFound(tf_input->name(), -// " is not found in the output_nodes_ng_op_map"); +// " is not found in the +// output_nodes_ng_op_map"); // } // // Into 2 lines: // // shared_ptr ng_input; -// TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)) +// TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, +// &ng_input)) // // // @@ -113,8 +115,9 @@ static void SaveNgOp(Builder::OpMap& ng_op_map, const std::string& op_name, // // -static Status GetInputNode(const Builder::OpMap& output_nodes_ng_op_map, const Node* op, - size_t input_idx, shared_ptr* result) { +static Status GetInputNode(const Builder::OpMap& output_nodes_ng_op_map, + const Node* op, size_t input_idx, + shared_ptr* result) { // input op may have resulted in more than one ng::Node (eg. Split) // we need to look at Edge to check index of the input op std::vector edges; @@ -145,14 +148,15 @@ static Status GetInputNode(const Builder::OpMap& output_nodes_ng_op_map, const N } namespace detail { -static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, const Node* op, - size_t index) { +static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, + const Node* op, size_t index) { return Status::OK(); } template -static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, const Node* op, - size_t index, shared_ptr* result, +static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, + const Node* op, size_t index, + shared_ptr* result, Arguments&&... remaining) { if (result != nullptr) { TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, index, result)); @@ -162,8 +166,8 @@ static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, const } // namespace detail template -static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, const Node* op, - Arguments&&... remaining) { +static Status GetInputNodes(const Builder::OpMap& output_nodes_ng_op_map, + const Node* op, Arguments&&... remaining) { constexpr size_t args_len = sizeof...(Arguments); TF_RETURN_IF_ERROR(ValidateInputCount(op, args_len)); return detail::GetInputNodes(output_nodes_ng_op_map, op, 0, remaining...); @@ -328,9 +332,11 @@ Builder::TF_NGRAPH_CONST_MAP() { // Node* op - TF op being translated. Must have one input. // const std::vector& static_input_map // - the static input map -// Builder::OpMap& output_nodes_ng_op_map - The TF-to-nGraph outputs nodes op map. +// Builder::OpMap& output_nodes_ng_op_map - The TF-to-nGraph outputs nodes +// op map. // -// Builder::OpMap& entry_nodes_ng_op_map - The TF-to-nGraph entry nodes op map. +// Builder::OpMap& entry_nodes_ng_op_map - The TF-to-nGraph entry nodes op +// map. // // std::function(std::shared_ptr> // create_unary_op - Function to construct the graph implementing @@ -340,7 +346,8 @@ Builder::TF_NGRAPH_CONST_MAP() { // Example usage: // // if (n->type_string == "Square") { -// TF_RETURN_IF_ERROR(TranslateUnaryOp(n, static_input_map, output_nodes_ng_op_map, +// TF_RETURN_IF_ERROR(TranslateUnaryOp(n, static_input_map, +// output_nodes_ng_op_map, // entry_nodes_ng_op_map) // [] (std::shared_ptr n) { // return (std::make_shared(n,n)); @@ -348,7 +355,8 @@ Builder::TF_NGRAPH_CONST_MAP() { // } static Status TranslateUnaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map, + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map, std::function(std::shared_ptr)> create_unary_op) { shared_ptr ng_input; @@ -372,7 +380,8 @@ static Status TranslateUnaryOp( template static Status TranslateUnaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { return TranslateUnaryOp( op, static_input_map, output_nodes_ng_op_map, entry_nodes_ng_op_map, [](std::shared_ptr n) { return make_shared(n); }); @@ -384,8 +393,10 @@ static Status TranslateUnaryOp( // Node* op - TF op being translated. Must have only two // inputs. // const std::vector& static_input_map - the static input map -// Builder::OpMap& output_nodes_ng_op_map - The TF-to-nGraph output nodes op map. -// Builder::OpMap& entry_nodes_ng_op_map - The TF-to-nGraph entry nodes op map. +// Builder::OpMap& output_nodes_ng_op_map - The TF-to-nGraph output nodes op +// map. +// Builder::OpMap& entry_nodes_ng_op_map - The TF-to-nGraph entry nodes op +// map. // // std::function(std::shared_ptr, // std::shared_ptr)> @@ -395,7 +406,8 @@ static Status TranslateUnaryOp( // Example Usage: // // if (op->type_string() == "Subtract") { -// TF_RETURN_IF_ERROR(TranslateBinaryOp(op, static_input_map, output_nodes_ng_op_map, +// TF_RETURN_IF_ERROR(TranslateBinaryOp(op, static_input_map, +// output_nodes_ng_op_map, // entry_nodes_ng_op_map, // [](std::shared_ptr ng_input1, std::shared_ptr // ng_input2) { @@ -406,12 +418,14 @@ static Status TranslateUnaryOp( static Status TranslateBinaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map, + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map, std::function(std::shared_ptr, std::shared_ptr)> create_binary_op) { std::shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); @@ -435,7 +449,8 @@ static Status TranslateBinaryOp( template static Status TranslateBinaryOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { return TranslateBinaryOp( op, static_input_map, output_nodes_ng_op_map, entry_nodes_ng_op_map, [](std::shared_ptr ng_lhs, std::shared_ptr ng_rhs) { @@ -445,7 +460,8 @@ static Status TranslateBinaryOp( static Status TranslateAllreduceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -457,12 +473,13 @@ static Status TranslateAllreduceOp( static Status TranslateAddNOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { std::vector> ng_arg_vec(op->num_inputs()); for (int inp_idx = 0; inp_idx < op->num_inputs(); inp_idx++) - TF_RETURN_IF_ERROR( - GetInputNode(output_nodes_ng_op_map, op, inp_idx, &ng_arg_vec[inp_idx])); + TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, inp_idx, + &ng_arg_vec[inp_idx])); // TODO (malikshr): Is empty list a valid input? // TODO (malikshr) : No op is created @@ -484,9 +501,11 @@ static Status TranslateAddNOp( template static Status TranslateLogicalReduction( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_axes_op; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); static_assert(std::is_base_of::value, "Expected LogicalReduction type op (Any or All)"); @@ -535,10 +554,12 @@ static Status TranslateLogicalReduction( static Status TranslateArgMaxOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_dim; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); std::vector tf_dim; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &tf_dim)); @@ -571,10 +592,12 @@ static Status TranslateArgMaxOp( static Status TranslateArgMinOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_dim; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); std::vector tf_dim; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &tf_dim)); @@ -607,7 +630,8 @@ static Status TranslateArgMinOp( static Status TranslateAvgPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -672,9 +696,11 @@ static Status TranslateAvgPoolOp( static Status TranslateAvgPoolGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_grad; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_grad)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_grad)); std::vector tf_orig_input_shape_vec; TF_RETURN_IF_ERROR( @@ -755,9 +781,11 @@ static Status TranslateAvgPoolGradOp( static Status TranslateBatchMatMulOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); auto ng_lhs_shape = ng_lhs->get_shape(); auto ng_rhs_shape = ng_rhs->get_shape(); @@ -880,9 +908,11 @@ static Status TranslateBatchMatMulOp( static Status TranslateBiasAddOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_bias; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_bias)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_bias)); std::string tf_data_format; if (GetNodeAttr(op->attrs(), "data_format", &tf_data_format) != @@ -929,7 +959,8 @@ static Status TranslateBiasAddOp( static Status TranslateBiasAddGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -978,7 +1009,8 @@ static Status TranslateBiasAddGradOp( static Status TranslateCastOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -1001,7 +1033,8 @@ static Status TranslateCastOp( static Status TranslateConcatV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCountMin(op, 2)); std::vector tf_concat_axis_vec; @@ -1012,7 +1045,8 @@ static Status TranslateConcatV2Op( if (concat_axis < 0) { shared_ptr ng_first_arg; - TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_first_arg)); + TF_RETURN_IF_ERROR( + GetInputNode(output_nodes_ng_op_map, op, 0, &ng_first_arg)); concat_axis += int64(ng_first_arg->get_shape().size()); } @@ -1033,7 +1067,8 @@ static Status TranslateConcatV2Op( static Status TranslateConstOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { DataType dtype; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "dtype", &dtype)); @@ -1063,9 +1098,11 @@ static Status TranslateConstOp( static Status TranslateConv2DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_filter; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); std::vector tf_strides; std::vector tf_dilations; @@ -1138,10 +1175,11 @@ static Status TranslateConv2DOp( static Status TranslateConv2DBackpropFilterOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_data_batch, ng_output_delta; - TF_RETURN_IF_ERROR( - GetInputNodes(output_nodes_ng_op_map, op, &ng_data_batch, nullptr, &ng_output_delta)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_data_batch, + nullptr, &ng_output_delta)); std::vector tf_strides; std::string tf_padding_type; @@ -1267,10 +1305,11 @@ static Status TranslateConv2DBackpropFilterOp( static Status TranslateConv2DBackpropInputOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_filter, ng_out_backprop; - TF_RETURN_IF_ERROR( - GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_filter, &ng_out_backprop)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, nullptr, + &ng_filter, &ng_out_backprop)); // TODO: refactor me to be less redundant with other convolution ops std::vector tf_strides; @@ -1362,9 +1401,11 @@ static Status TranslateConv2DBackpropInputOp( // Translate Conv3D Op static Status TranslateConv3DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_filter; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); std::vector tf_strides; std::vector tf_dilations; @@ -1440,7 +1481,8 @@ static Status TranslateConv3DOp( // Translate DepthToSpace op static Status TranslateDepthToSpaceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -1596,9 +1638,11 @@ static Status TranslateDepthToSpaceOp( static Status TranslateDepthwiseConv2dNativeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_filter; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_filter)); std::vector tf_strides; std::vector tf_dilations; @@ -1695,9 +1739,11 @@ static Status TranslateDepthwiseConv2dNativeOp( static Status TranslateExpandDimsOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_dim; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_dim)); std::vector dim_vec; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &dim_vec)); @@ -1727,9 +1773,11 @@ static Status TranslateExpandDimsOp( static Status TranslateFillOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_value; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_value)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_value)); std::vector dims_vec; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 0, static_input_map, &dims_vec)); @@ -1751,9 +1799,11 @@ static Status TranslateFillOp( static Status TranslateFloorDivOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); auto ng_div = std::make_shared(ng_lhs, ng_rhs); @@ -1765,9 +1815,11 @@ static Status TranslateFloorDivOp( static Status TranslateFloorModOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); @@ -1783,7 +1835,8 @@ static Status TranslateFloorModOp( static Status TranslateFusedBatchNormOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { bool tf_is_training; if (GetNodeAttr(op->attrs(), "is_training", &tf_is_training) != Status::OK()) { @@ -1794,8 +1847,9 @@ static Status TranslateFusedBatchNormOp( NGRAPH_VLOG(3) << "is_training: " << tf_is_training; shared_ptr ng_input, ng_scale, ng_offset, ng_mean, ng_variance; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_scale, - &ng_offset, &ng_mean, &ng_variance)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, + &ng_scale, &ng_offset, &ng_mean, + &ng_variance)); std::string tf_data_format; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "data_format", &tf_data_format)); @@ -1874,7 +1928,8 @@ static Status TranslateFusedBatchNormOp( static Status TranslateFusedBatchNormGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCount(op, 5)); bool tf_is_training; @@ -1893,8 +1948,9 @@ static Status TranslateFusedBatchNormGradOp( shared_ptr ng_scale; shared_ptr ng_mean; shared_ptr ng_variance; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_delta, &ng_input, - &ng_scale, &ng_mean, &ng_variance)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_delta, + &ng_input, &ng_scale, &ng_mean, + &ng_variance)); std::string tf_data_format; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "data_format", &tf_data_format)); @@ -1969,7 +2025,8 @@ static Status TranslateFusedBatchNormGradOp( static Status TranslateIdentityOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_arg; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_arg)); // TODO (malikshr): No op created : control dependency? @@ -1979,7 +2036,8 @@ static Status TranslateIdentityOp( static Status TranslateL2LossOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -2006,9 +2064,11 @@ static Status TranslateL2LossOp( static Status TranslateMatMulOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); // Transpose arguments if requested. bool transpose_a = false; @@ -2044,7 +2104,8 @@ static Status TranslateMaxOp(const Node* op, Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_max_op; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_max_op)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_max_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -2091,7 +2152,8 @@ static Status TranslateMaxOp(const Node* op, static Status TranslateMaxPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -2158,7 +2220,8 @@ static Status TranslateMaxPoolOp( static Status TranslateMaxPool3DOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -2225,7 +2288,8 @@ static Status TranslateMaxPool3DOp( static Status TranslateMaxPoolGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_grad, ng_fwd; TF_RETURN_IF_ERROR( GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_fwd, &ng_grad)); @@ -2294,9 +2358,11 @@ static Status TranslateMaxPoolGradOp( static Status TranslateMeanOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_axes_op; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -2346,7 +2412,8 @@ static Status TranslateMinOp(const Node* op, Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_min_op; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_min_op)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_min_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -2394,7 +2461,8 @@ static Status TranslateMinOp(const Node* op, static Status TranslatePackOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCountMin(op, 1)); ng::NodeVector ng_concat_inputs; @@ -2454,7 +2522,8 @@ static Status TranslatePadOp(const Node* op, Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_paddings_op; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_paddings_op)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_paddings_op)); std::vector paddings; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &paddings)); @@ -2494,9 +2563,11 @@ static Status TranslatePadOp(const Node* op, static Status TranslateProdOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_axes_op; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -2543,7 +2614,8 @@ static Status TranslateProdOp( static Status TranslateRankOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -2560,7 +2632,8 @@ static Status TranslateRankOp( static Status TranslateReciprocalOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto et = ng_input->get_element_type(); @@ -2702,9 +2775,11 @@ Status QuantizeAndDequantizeV2Helper( static Status TranslateQuantizeAndDequantizeV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); bool range_given; TF_RETURN_IF_ERROR(GetNodeAttr(op->attrs(), "range_given", &range_given)); @@ -2767,7 +2842,8 @@ static Status TranslateQuantizeAndDequantizeV2Op( static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_filter, ng_bias; TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)); TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 1, &ng_filter)); @@ -2836,9 +2912,11 @@ static Status TranslateQuantizedConv2DWithBiasAndReluAndRequantizeOp( static Status TranslateQuantizedMaxPoolOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_min, ng_max; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_min, &ng_max)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_min, &ng_max)); std::vector tf_strides; std::vector tf_ksize; std::string tf_padding_type; @@ -2884,9 +2962,11 @@ static Status TranslateQuantizedMaxPoolOp( static Status TranslateQuantizeV2Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); std::vector ng_min, ng_max; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &ng_min)); @@ -2948,10 +3028,10 @@ static Status TranslateQuantizeV2Op( (mode.compare("SCALED") == 0), ng_min[0], ng_max[0], &ng_scale_val, &ng_offset_val); } catch (const std::exception& e) { - return errors::Internal( - "Unhandled exception in ComputeScaleOffset: ", op->name(), " (", - op->type_string(), ")\n", op->def().DebugString(), "\n", - "what(): ", e.what()); + return errors::Internal("Unhandled exception in ComputeScaleOffset: ", + op->name(), " (", op->type_string(), ")\n", + op->def().DebugString(), "\n", "what(): ", + e.what()); } auto ng_scale = std::make_shared( @@ -2990,9 +3070,11 @@ static Status TranslateQuantizeV2Op( static Status TranslateDequantizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, nullptr, nullptr)); std::vector ng_min, ng_max; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &ng_min)); @@ -3045,10 +3127,10 @@ static Status TranslateDequantizeOp( (mode.compare("SCALED") == 0), ng_min[0], ng_max[0], &ng_scale_val, &ng_offset_val); } catch (const std::exception& e) { - return errors::Internal( - "Unhandled exception in ComputeScaleOffset: ", op->name(), " (", - op->type_string(), ")\n", op->def().DebugString(), "\n", - "what(): ", e.what()); + return errors::Internal("Unhandled exception in ComputeScaleOffset: ", + op->name(), " (", op->type_string(), ")\n", + op->def().DebugString(), "\n", "what(): ", + e.what()); } auto ng_scale = std::make_shared( @@ -3071,7 +3153,8 @@ static Status TranslateDequantizeOp( static Status TranslateReluOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto result = make_shared(ng_input); @@ -3082,7 +3165,8 @@ static Status TranslateReluOp( static Status TranslateRelu6Op( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -3101,9 +3185,11 @@ static Status TranslateRelu6Op( static Status TranslateReluGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_arg, ng_delta; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_delta, &ng_arg)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_delta, &ng_arg)); auto ng_relu_grad = std::make_shared(ng_arg, ng_delta); SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_relu_grad); @@ -3113,9 +3199,11 @@ static Status TranslateReluGradOp( static Status TranslateReshapeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_shape_op; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_shape_op)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_shape_op)); NGRAPH_VLOG(3) << "Input shape: " << ng::join(ng_input->get_shape()); @@ -3179,7 +3267,8 @@ static Status TranslateReshapeOp( static Status TranslateRsqrtOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); auto et = ng_input->get_element_type(); @@ -3199,7 +3288,8 @@ static Status TranslateRsqrtOp( static Status TranslateShapeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -3230,10 +3320,12 @@ static Status TranslateShapeOp( static Status TranslateSigmoidGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; shared_ptr ng_delta; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_delta)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_delta)); auto ng_mul = ng_input * ng_delta; SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_mul); @@ -3249,7 +3341,8 @@ static Status TranslateSigmoidGradOp( static Status TranslateSigmoidOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -3269,7 +3362,8 @@ static Status TranslateSigmoidOp( static Status TranslateSizeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -3297,10 +3391,11 @@ static Status TranslateSizeOp( static Status TranslateSliceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_begin, ng_size; - TF_RETURN_IF_ERROR( - GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_begin, &ng_size)); + TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, + &ng_begin, &ng_size)); std::vector lower_vec; std::vector size_vec; @@ -3355,7 +3450,8 @@ static Status TranslateSliceOp( static Status TranslateSnapshotOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_arg; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_arg)); // TODO (malikshr) : No op is created @@ -3365,7 +3461,8 @@ static Status TranslateSnapshotOp( static Status TranslateSoftmaxOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -3388,7 +3485,8 @@ static Status TranslateSoftmaxOp( // Translate SpaceToDepthOp static Status TranslateSpaceToDepthOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -3467,7 +3565,8 @@ static Status TranslateSpaceToDepthOp( static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { // TF op Inputs: // 1. Logits/Features: // Shape : [BatchSize, NumOfClasses] @@ -3477,7 +3576,8 @@ static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( // Range : [0, NumOfClasses) // Type : int shared_ptr ng_features, ng_labels; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_features, &ng_labels)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_features, &ng_labels)); ng::Shape ng_features_shape = ng_features->get_shape(); ng::Shape ng_labels_shape = ng_labels->get_shape(); @@ -3575,9 +3675,11 @@ static Status TranslateSparseSoftmaxCrossEntropyWithLogitsOp( static Status TranslateSplitOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_input)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, nullptr, &ng_input)); // num_split : The number of ways to split. Must evenly divide // value.shape[split_dim] int32 num_split; @@ -3614,7 +3716,8 @@ static Status TranslateSplitOp( static Status TranslateSplitVOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_length, ng_split_dim; TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)); @@ -3699,7 +3802,8 @@ static Status TranslateSplitVOp( static Status TranslateSquareOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { return TranslateUnaryOp(op, static_input_map, output_nodes_ng_op_map, entry_nodes_ng_op_map, [](std::shared_ptr n) { @@ -3709,9 +3813,11 @@ static Status TranslateSquareOp( static Status TranslateSquaredDifferenceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_lhs, ng_rhs; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_lhs, &ng_rhs)); std::tie(ng_lhs, ng_rhs) = ng::builder::numpy_broadcast(std::make_pair(ng_lhs, ng_rhs)); auto ng_diff = std::make_shared(ng_lhs, ng_rhs); @@ -3723,7 +3829,8 @@ static Status TranslateSquaredDifferenceOp( static Status TranslateSqueezeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); size_t input_dims = ng_input->get_shape().size(); @@ -3782,7 +3889,8 @@ static Status TranslateSqueezeOp( static Status TranslateStridedSliceOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { // TODO: implement new_axis_mask, ellipsis_mask shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNode(output_nodes_ng_op_map, op, 0, &ng_input)); @@ -3849,9 +3957,10 @@ static Status TranslateStridedSliceOp( // The first 2 cases breaks down this range if (idx >= 0 && idx <= (static_cast(dim) - 1)) { return idx; - } else if (idx < 0 && idx + static_cast(dim) >= - 0) { // careful not to do idx >= -dim - // (since dim is unsigned) + } else if (idx < 0 && + idx + static_cast(dim) >= + 0) { // careful not to do idx >= -dim + // (since dim is unsigned) return idx + static_cast( dim); // Type casting to int to enable unambiguous auto // type inference of return type @@ -4089,7 +4198,8 @@ static Status TranslateSumOp(const Node* op, Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_axes_op; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_axes_op)); bool tf_keep_dims; if (GetNodeAttr(op->attrs(), "keep_dims", &tf_keep_dims) != Status::OK()) { @@ -4140,9 +4250,11 @@ static Status TranslateSumOp(const Node* op, // where y = tanh(x) and dy is the corresponding input gradient static Status TranslateTanhGradOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_delta; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_delta)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_delta)); auto ng_sq = std::make_shared(ng_input, ng_input); SaveNgOp(entry_nodes_ng_op_map, op->name(), ng_sq); @@ -4159,9 +4271,11 @@ static Status TranslateTanhGradOp( static Status TranslateTileOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_multiples; - TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_multiples)); + TF_RETURN_IF_ERROR( + GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_multiples)); std::vector multiples; TF_RETURN_IF_ERROR(GetStaticInputVector(op, 1, static_input_map, &multiples)); @@ -4210,7 +4324,8 @@ static Status TranslateTileOp( static Status TranslateTransposeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input, ng_permutation_op; TF_RETURN_IF_ERROR( GetInputNodes(output_nodes_ng_op_map, op, &ng_input, &ng_permutation_op)); @@ -4257,7 +4372,8 @@ static Status TranslateTransposeOp( static Status TranslateUnpackOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { TF_RETURN_IF_ERROR(ValidateInputCount(op, 1)); shared_ptr ng_input; @@ -4311,7 +4427,8 @@ static Status TranslateUnpackOp( static Status TranslateZerosLikeOp( const Node* op, const std::vector& static_input_map, - Builder::OpMap& output_nodes_ng_op_map, Builder::OpMap& entry_nodes_ng_op_map) { + Builder::OpMap& output_nodes_ng_op_map, + Builder::OpMap& entry_nodes_ng_op_map) { shared_ptr ng_input; TF_RETURN_IF_ERROR(GetInputNodes(output_nodes_ng_op_map, op, &ng_input)); @@ -4529,13 +4646,13 @@ Status Builder::TranslateGraph( } try { - TF_RETURN_IF_ERROR( - (*op_fun)(op, static_input_map, output_nodes_ng_op_map, entry_nodes_ng_op_map)); + TF_RETURN_IF_ERROR((*op_fun)(op, static_input_map, output_nodes_ng_op_map, + entry_nodes_ng_op_map)); } catch (const std::exception& e) { return errors::Internal("Unhandled exception in op handler: ", op->name(), " (", op->type_string(), ")\n", - op->def().DebugString(), "\n", - "what(): ", e.what()); + op->def().DebugString(), "\n", "what(): ", + e.what()); } } From 3ea38679d0b104c8c10f0bac862c5bde4872b7c3 Mon Sep 17 00:00:00 2001 From: Sayantan Sarkar <41931958+sayantan-nervana@users.noreply.github.com> Date: Mon, 4 Feb 2019 10:59:41 -0800 Subject: [PATCH 9/9] Apply suggestions from code review Co-Authored-By: shresthamalik <20289882+shresthamalik@users.noreply.github.com> --- src/ngraph_builder.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngraph_builder.cc b/src/ngraph_builder.cc index 31239d12..d996bff0 100644 --- a/src/ngraph_builder.cc +++ b/src/ngraph_builder.cc @@ -443,7 +443,7 @@ static Status TranslateBinaryOp( // // if (n->type_string == "Add") { // TF_RETURN_IF_ERROR(TranslateBinaryOp(op, static_input_map, -// output_nodes_ng_op_map,entry_nodes_ng_op_map)); +// output_nodes_ng_op_map, entry_nodes_ng_op_map)); // } // template