From 797a85537cb26a845115db59eb88f7e54ef09c1d Mon Sep 17 00:00:00 2001 From: =?utf8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=91=D0=B0=D1=80?= =?utf8?q?=D0=B0=D0=BD=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2/AI=20Tools=20Lab=20/S?= =?utf8?q?RR/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 27 Aug 2019 03:53:27 +0900 Subject: [PATCH] [mir_caffe] Do not set names of operations (#6814) Remove useless setting of operation names. Signed-off-by: Sergei Barannikov --- compiler/mir-caffe-importer/caffe_op_creator.cpp | 156 +++++++++++------------ compiler/mir-caffe-importer/caffe_op_creator.h | 7 +- 2 files changed, 79 insertions(+), 84 deletions(-) diff --git a/compiler/mir-caffe-importer/caffe_op_creator.cpp b/compiler/mir-caffe-importer/caffe_op_creator.cpp index 02db7ec..60e7384 100644 --- a/compiler/mir-caffe-importer/caffe_op_creator.cpp +++ b/compiler/mir-caffe-importer/caffe_op_creator.cpp @@ -116,14 +116,14 @@ using namespace mir; mir::Operation::Output *CaffeOpCreator::convertCaffeToMIR(mir::Operation::Output *arg) { // NCHW -> NHWC - auto transpose = createOp("", arg, std::vector{0, 2, 3, 1}); + auto transpose = createOp(arg, std::vector{0, 2, 3, 1}); return transpose->getOutput(0); } mir::Operation::Output *CaffeOpCreator::convertMIRToCaffe(mir::Operation::Output *arg) { // NHWC -> NCHW - auto transpose = createOp("", arg, std::vector{0, 3, 1, 2}); + auto transpose = createOp(arg, std::vector{0, 3, 1, 2}); return transpose->getOutput(0); } @@ -144,7 +144,7 @@ std::vector CaffeOpCreator::createSplit(mir::Operation std::vector outputs(num_parts); for (int32_t i = 0; i < num_parts; ++i) { - outputs[i] = createOp("", arg, starts, sizes)->getOutput(0); + outputs[i] = createOp(arg, starts, sizes)->getOutput(0); starts.dim(axis) += part_size; } @@ -175,9 +175,9 @@ mir::Operation::Output *CaffeOpCreator::createFullyConnected(mir::Operation::Out for (int32_t i = axis; i < input_shape.rank(); ++i) inner_size *= input_shape.dim(i); - auto flatten = createOp("", input, Shape{outer_size, inner_size})->getOutput(0); - auto fc = createOp("", flatten, weights)->getOutput(0); - return createOp("", fc, result_shape)->getOutput(0); + auto flatten = createOp(input, Shape{outer_size, inner_size})->getOutput(0); + auto fc = createOp(flatten, weights)->getOutput(0); + return createOp(fc, result_shape)->getOutput(0); } TensorVariant CaffeOpCreator::convertBlob(const BlobProto &blob) @@ -219,7 +219,8 @@ std::vector CaffeOpCreator::convertInput(const LayerPa const auto &blob_name = layer.top(i); const auto &blob_shape = params.shape(num_shapes == 1 ? 0 : i); const mir::Shape shape = convertBlobShape(blob_shape); - auto variable = createOp(blob_name, shape); + auto variable = createOp(shape); + variable->setName(blob_name); outputs.push_back(variable->getOutput(0)); } @@ -321,9 +322,9 @@ CaffeOpCreator::convertConvolution(const caffe::LayerParameter &layer, // This is depthwise convolution // TODO handle properly kernel with layer multiplier auto transposed_tensor = transposeTensor<0, 1, 3, 2>(kernel_weights); - auto kernel = createOp("", transposed_tensor)->getOutput(0); - result = createOp(layer.name(), convertCaffeToMIR(inputs[0]), kernel, - strides, padding, padding) + auto kernel = createOp(transposed_tensor)->getOutput(0); + result = createOp(convertCaffeToMIR(inputs[0]), kernel, strides, + padding, padding) ->getOutput(0); } else @@ -334,17 +335,17 @@ CaffeOpCreator::convertConvolution(const caffe::LayerParameter &layer, kernel_weights = fixGroupedKernel(params.group(), kernel_weights); } kernel_weights = transposeTensor<3, 0, 1, 2>(kernel_weights); - auto kernel = createOp("", kernel_weights)->getOutput(0); - result = createOp(layer.name(), convertCaffeToMIR(inputs[0]), kernel, strides, - padding, padding) - ->getOutput(0); + auto kernel = createOp(kernel_weights)->getOutput(0); + result = + createOp(convertCaffeToMIR(inputs[0]), kernel, strides, padding, padding) + ->getOutput(0); } // Add the bias, if any. if (params.bias_term()) { - auto bias = createOp("", convertBlob(layer.blobs(1)))->getOutput(0); - result = createOp(layer.name() + ".bias", result, bias)->getOutput(0); + auto bias = createOp(convertBlob(layer.blobs(1)))->getOutput(0); + result = createOp(result, bias)->getOutput(0); } return {convertMIRToCaffe(result)}; @@ -368,16 +369,15 @@ CaffeOpCreator::convertDeconvolution(const caffe::LayerParameter &layer, // first we need to convert kernel of grouped convolution to appropriate ordinary kernel kernel_weights = fixGroupedKernel(opts.group(), kernel_weights); } - auto kernel = createOp("", kernel_weights)->getOutput(0); - auto result = createOp(layer.name(), convertCaffeToMIR(inputs[0]), kernel, - strides, padding) + auto kernel = createOp(kernel_weights)->getOutput(0); + auto result = createOp(convertCaffeToMIR(inputs[0]), kernel, strides, padding) ->getOutput(0); // bias_term is optional (so might not be present) and defaults to true if (opts.bias_term()) { - auto bias = createOp("", convertBlob(layer.blobs(1)))->getOutput(0); - result = createOp(layer.name() + ".bias", result, bias)->getOutput(0); + auto bias = createOp(convertBlob(layer.blobs(1)))->getOutput(0); + result = createOp(result, bias)->getOutput(0); } return {convertMIRToCaffe(result)}; @@ -393,14 +393,14 @@ CaffeOpCreator::convertInnerProduct(const LayerParameter &layer, if (!params.transpose()) weights_tensor = transposeTensor<1, 0>(weights_tensor); - auto weights = createOp("", weights_tensor)->getOutput(0); + auto weights = createOp(weights_tensor)->getOutput(0); auto result = createFullyConnected(inputs[0], weights, params.axis()); // Add the bias, if any. if (params.bias_term()) { - auto bias = createOp("", convertBlob(layer.blobs(1)))->getOutput(0); - result = createOp(layer.name() + ".bias", result, bias)->getOutput(0); + auto bias = createOp(convertBlob(layer.blobs(1)))->getOutput(0); + result = createOp(result, bias)->getOutput(0); } return {result}; @@ -411,7 +411,7 @@ CaffeOpCreator::convertConcat(const caffe::LayerParameter &layer, const std::vector &inputs) { const auto ¶ms = layer.concat_param(); - auto concat = createOp(layer.name(), inputs, params.axis()); + auto concat = createOp(inputs, params.axis()); return {concat->getOutput(0)}; } @@ -518,9 +518,8 @@ CaffeOpCreator::convertPooling(const caffe::LayerParameter &layer, assert(false); } - auto pooling = - createOp(layer.name(), convertCaffeToMIR(inputs[0]), pool_type, window_shape, - strides, padding_before, padding_after, border_type); + auto pooling = createOp(convertCaffeToMIR(inputs[0]), pool_type, window_shape, + strides, padding_before, padding_after, border_type); return {convertMIRToCaffe(pooling->getOutput(0))}; } @@ -538,15 +537,14 @@ CaffeOpCreator::convertSoftmax(const caffe::LayerParameter &layer, if (params.axis() != 1) throw std::runtime_error("Softmax: unsupported axis"); int32_t axis = 3; - auto input = createOp(layer.name() + ".trans1", inputs[0], - std::vector{0, 2, 3, 1}); - auto softmax = createOp(layer.name(), input->getOutput(0), axis); - auto result = createOp(layer.name() + ".trans2", softmax->getOutput(0), - std::vector{0, 3, 1, 2}); + auto input = createOp(inputs[0], std::vector{0, 2, 3, 1}); + auto softmax = createOp(input->getOutput(0), axis); + auto result = + createOp(softmax->getOutput(0), std::vector{0, 3, 1, 2}); return {result->getOutput(0)}; } - auto softmax = createOp(layer.name(), inputs[0], params.axis()); + auto softmax = createOp(inputs[0], params.axis()); return {softmax->getOutput(0)}; } @@ -578,7 +576,7 @@ CaffeOpCreator::convertReshape(const caffe::LayerParameter &layer, { auto &opts = layer.reshape_param(); const mir::Shape new_shape = convertBlobShape(opts.shape()); - auto reshape = createOp(layer.name(), inputs[0], new_shape); + auto reshape = createOp(inputs[0], new_shape); return {reshape->getOutput(0)}; } @@ -590,11 +588,11 @@ CaffeOpCreator::convertReLU(const caffe::LayerParameter &layer, if (layer.relu_param().has_negative_slope()) { float alpha = layer.relu_param().negative_slope(); - relu = createOp(layer.name(), inputs[0], alpha); + relu = createOp(inputs[0], alpha); } else { - relu = createOp(layer.name(), inputs[0]); + relu = createOp(inputs[0]); } return {relu->getOutput(0)}; @@ -605,15 +603,14 @@ CaffeOpCreator::convertScale(const caffe::LayerParameter &layer, const std::vector &inputs) { const auto ¶ms = layer.scale_param(); - auto scale = createOp("", convertBlob(layer.blobs(0)))->getOutput(0); - auto result = - createOp(layer.name(), convertCaffeToMIR(inputs[0]), scale)->getOutput(0); + auto scale = createOp(convertBlob(layer.blobs(0)))->getOutput(0); + auto result = createOp(convertCaffeToMIR(inputs[0]), scale)->getOutput(0); // Add the bias, if any. if (params.bias_term()) { - auto bias = createOp("", convertBlob(layer.blobs(1)))->getOutput(0); - result = createOp(layer.name() + ".bias", result, bias)->getOutput(0); + auto bias = createOp(convertBlob(layer.blobs(1)))->getOutput(0); + result = createOp(result, bias)->getOutput(0); } return {convertMIRToCaffe(result)}; @@ -652,18 +649,18 @@ CaffeOpCreator::convertBatchNorm(const caffe::LayerParameter &layer, Tensor mean_accessor(mean_tensor); for (const auto &idx : ShapeRange(mean_accessor.getShape())) mean_accessor.at(idx) *= -scale_factor; - auto c1 = createOp("", mean_tensor)->getOutput(0); + auto c1 = createOp(mean_tensor)->getOutput(0); // C2 = 1 / sqrt(var / scale_factor + epsilon) Tensor var_accessor(var_tensor); for (const auto &idx : ShapeRange(var_accessor.getShape())) var_accessor.at(idx) = 1.0f / std::sqrt(var_accessor.at(idx) * scale_factor + eps); - auto c2 = createOp("", var_tensor)->getOutput(0); + auto c2 = createOp(var_tensor)->getOutput(0); // Y = (X + C1) * C2 input = convertCaffeToMIR(input); - auto result = createOp("", input, c1)->getOutput(0); - result = createOp("", result, c2)->getOutput(0); + auto result = createOp(input, c1)->getOutput(0); + result = createOp(result, c2)->getOutput(0); return {convertMIRToCaffe(result)}; } @@ -680,7 +677,7 @@ CaffeOpCreator::convertELU(const caffe::LayerParameter &layer, const std::vector &inputs) { auto &opts = layer.elu_param(); - auto elu = createOp(layer.name(), inputs[0], opts.alpha()); + auto elu = createOp(inputs[0], opts.alpha()); return {elu->getOutput(0)}; } @@ -689,15 +686,14 @@ CaffeOpCreator::convertEmbed(const caffe::LayerParameter &layer, const std::vector &inputs) { const auto ¶ms = layer.embed_param(); - auto data = createOp(layer.name() + ".weights", convertBlob(layer.blobs(0))); - auto result = - createOp(layer.name(), data->getOutput(0), inputs[0], 0)->getOutput(0); + auto data = createOp(convertBlob(layer.blobs(0))); + auto result = createOp(data->getOutput(0), inputs[0], 0)->getOutput(0); // Add the bias, if any. if (params.bias_term()) { - auto bias = createOp("", convertBlob(layer.blobs(1)))->getOutput(0); - result = createOp(layer.name() + ".bias", result, bias)->getOutput(0); + auto bias = createOp(convertBlob(layer.blobs(1)))->getOutput(0); + result = createOp(result, bias)->getOutput(0); } return {result}; @@ -707,7 +703,7 @@ std::vector CaffeOpCreator::convertSigmoid(const caffe::LayerParameter &layer, const std::vector &inputs) { - auto result = createOp(layer.name(), inputs[0]); + auto result = createOp(inputs[0]); return {result->getOutput(0)}; } @@ -715,7 +711,7 @@ std::vector CaffeOpCreator::convertTanH(const caffe::LayerParameter &layer, const std::vector &inputs) { - auto tanh = createOp(layer.name(), inputs[0]); + auto tanh = createOp(inputs[0]); return {tanh->getOutput(0)}; } @@ -730,10 +726,10 @@ CaffeOpCreator::convertEltwise(const caffe::LayerParameter &layer, { case EltwiseParameter_EltwiseOp_PROD: { - result = createOp("", inputs[0], inputs[1])->getOutput(0); + result = createOp(inputs[0], inputs[1])->getOutput(0); for (int i = 2; i < layer.bottom_size(); ++i) { - result = createOp("", result, inputs[i])->getOutput(0); + result = createOp(result, inputs[i])->getOutput(0); } break; } @@ -749,24 +745,24 @@ CaffeOpCreator::convertEltwise(const caffe::LayerParameter &layer, { const float coeff_val = params.coeff(i); TensorVariant coeff_tensor(mir::DataType::FLOAT32, Shape{}, &coeff_val); - auto coeff_const = createOp("", coeff_tensor)->getOutput(0); - scaled_inputs[i] = createOp("", coeff_const, inputs[i])->getOutput(0); + auto coeff_const = createOp(coeff_tensor)->getOutput(0); + scaled_inputs[i] = createOp(coeff_const, inputs[i])->getOutput(0); } } } - result = createOp("", scaled_inputs[0], scaled_inputs[1])->getOutput(0); + result = createOp(scaled_inputs[0], scaled_inputs[1])->getOutput(0); for (int i = 2; i < layer.bottom_size(); ++i) { - result = createOp("", result, scaled_inputs[i])->getOutput(0); + result = createOp(result, scaled_inputs[i])->getOutput(0); } break; } case EltwiseParameter_EltwiseOp_MAX: { - result = createOp("", inputs[0], inputs[1])->getOutput(0); + result = createOp(inputs[0], inputs[1])->getOutput(0); for (int i = 2; i < layer.bottom_size(); ++i) { - result = createOp("", result, inputs[i])->getOutput(0); + result = createOp(result, inputs[i])->getOutput(0); } break; } @@ -872,20 +868,20 @@ CaffeOpCreator::convertLSTM(const caffe::LayerParameter &layer, auto xw_tensor = transposeTensor<1, 0>(convertBlob(layer.blobs(0))); auto xb_tensor = convertBlob(layer.blobs(1)); auto hw_tensor = transposeTensor<1, 0>(convertBlob(layer.blobs(2))); - auto xw = createOp("", xw_tensor)->getOutput(0); - auto xb = createOp("", xb_tensor)->getOutput(0); - auto hw = createOp("", hw_tensor)->getOutput(0); + auto xw = createOp(xw_tensor)->getOutput(0); + auto xb = createOp(xb_tensor)->getOutput(0); + auto hw = createOp(hw_tensor)->getOutput(0); // Add a dummy dimension so that element-wise operations perform properly. - cont = createOp("", cont, Shape{seq_length, batch_size, 1})->getOutput(0); + cont = createOp(cont, Shape{seq_length, batch_size, 1})->getOutput(0); // Initialize cell and hidden states with zeros. auto zero_tensor = createZeroedTensor(Shape{1, batch_size, hidden_size}); - auto c_t = createOp("", zero_tensor)->getOutput(0); - auto h_t = createOp("", zero_tensor)->getOutput(0); + auto c_t = createOp(zero_tensor)->getOutput(0); + auto h_t = createOp(zero_tensor)->getOutput(0); auto x_xw = createFullyConnected(x, xw, 2); - auto x_xw_b = createOp("", x_xw, xb)->getOutput(0); + auto x_xw_b = createOp(x_xw, xb)->getOutput(0); // Split input and continuation tensors into seq_length slices. std::vector x_xw_b_slices = createSplit(x_xw_b, seq_length, 0); @@ -894,28 +890,28 @@ CaffeOpCreator::convertLSTM(const caffe::LayerParameter &layer, for (int32_t t = 0; t < seq_length; t++) { - auto c_cont_t = createOp("", c_t, cont_slices[t])->getOutput(0); - auto h_cont_t = createOp("", h_t, cont_slices[t])->getOutput(0); + auto c_cont_t = createOp(c_t, cont_slices[t])->getOutput(0); + auto h_cont_t = createOp(h_t, cont_slices[t])->getOutput(0); auto x_xw_b_t = x_xw_b_slices[t]; auto h_hw_t = createFullyConnected(h_cont_t, hw, 2); - auto activation_inputs_concat = createOp("", x_xw_b_t, h_hw_t)->getOutput(0); + auto activation_inputs_concat = createOp(x_xw_b_t, h_hw_t)->getOutput(0); auto activation_inputs = createSplit(activation_inputs_concat, 4, 2); - auto i_t = createOp("", activation_inputs[0])->getOutput(0); - auto f_t = createOp("", activation_inputs[1])->getOutput(0); - auto o_t = createOp("", activation_inputs[2])->getOutput(0); - auto g_t = createOp("", activation_inputs[3])->getOutput(0); + auto i_t = createOp(activation_inputs[0])->getOutput(0); + auto f_t = createOp(activation_inputs[1])->getOutput(0); + auto o_t = createOp(activation_inputs[2])->getOutput(0); + auto g_t = createOp(activation_inputs[3])->getOutput(0); - c_t = createOp("", createOp("", c_cont_t, f_t)->getOutput(0), - createOp("", i_t, g_t)->getOutput(0)) + c_t = createOp(createOp(c_cont_t, f_t)->getOutput(0), + createOp(i_t, g_t)->getOutput(0)) ->getOutput(0); - h_t = createOp("", createOp("", c_t)->getOutput(0), o_t)->getOutput(0); + h_t = createOp(createOp(c_t)->getOutput(0), o_t)->getOutput(0); h_slices[t] = h_t; } - return {createOp("", h_slices, 0)->getOutput(0)}; + return {createOp(h_slices, 0)->getOutput(0)}; } } // namespace mir_caffe diff --git a/compiler/mir-caffe-importer/caffe_op_creator.h b/compiler/mir-caffe-importer/caffe_op_creator.h index 7a3abce..f83c372 100644 --- a/compiler/mir-caffe-importer/caffe_op_creator.h +++ b/compiler/mir-caffe-importer/caffe_op_creator.h @@ -139,14 +139,13 @@ private: TensorVariant convertBlob(const caffe::BlobProto &blob); - template - mir::Operation *createOp(const std::string &name, Types &&... args); + template mir::Operation *createOp(Types &&... args); }; template -mir::Operation *CaffeOpCreator::createOp(const std::string &name, Types &&... args) +mir::Operation *CaffeOpCreator::createOp(Types &&... args) { - return _graph->create(name, std::forward(args)...); + return _graph->create("", std::forward(args)...); } } // namespace mir_caffe -- 2.7.4