From: Сергей Баранников/AI Tools Lab /SRR/Engineer/삼성전자 Date: Wed, 28 Aug 2019 07:53:09 +0000 (+0900) Subject: [mir_caffe2] Do not set names of operations (#6812) X-Git-Tag: accepted/tizen/unified/20190903.052428~84 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=db5049decdd73606cc05a3d8b76d87081342caad;p=platform%2Fcore%2Fml%2Fnnfw.git [mir_caffe2] Do not set names of operations (#6812) Remove useless setting of operation names. Signed-off-by: Sergei Barannikov --- diff --git a/compiler/mir-caffe2-importer/caffe2_importer.cpp b/compiler/mir-caffe2-importer/caffe2_importer.cpp index 7432589..b8b6f41 100644 --- a/compiler/mir-caffe2-importer/caffe2_importer.cpp +++ b/compiler/mir-caffe2-importer/caffe2_importer.cpp @@ -19,6 +19,7 @@ #include "caffe2_op_creator.h" #include "caffe2_proto_helper.h" +#include "mir/ops/InputOp.h" #include "mir/ops/OutputOp.h" #include @@ -96,10 +97,11 @@ std::unique_ptr Caffe2Importer::createIR() // Create inputs. This has to be done after processing initializers, because they may contain // fake inputs. - // TODO Caffe2 does not provide a way to detect model inputs. For now assume that the first input - // of the first operation is the only input to the model. + // TODO Caffe2 does not provide a way to detect model inputs and outputs. For now assume that: + // - there is exactly one input; + // - the input is for the first layer. const auto &input_name = _predict_net->op(0).input(0); - auto input = _opCreator->createInput(input_name, _inputShapes[0]); + auto input = _graph->create(_inputShapes[0])->getOutput(0); setOutputForTensor(input_name, input); for (const auto &op : _predict_net->op()) @@ -204,12 +206,6 @@ void Caffe2Importer::createMIRNodesFromOp(const OperatorDef &op) { setOutputForTensor(op.output(i), outputs[i]); } - - // `outputs` can be empty if constant input was not processed. - if (!outputs.empty()) - { - _lastMIROp = outputs.at(0)->getNode(); - } } std::vector Caffe2Importer::getInputMIROps(const OperatorDef &op) @@ -244,10 +240,13 @@ mir::Operation::Output *Caffe2Importer::getOutputForTensor(const std::string &na void Caffe2Importer::setGraphOutputs() { - // For now, we assume that: - // - there is exactly one output; - // - the output is from the last layer. - _graph->create("out", _lastMIROp->getOutput(0)); + // Create outputs. + // TODO Caffe2 does not provide a way to detect model inputs and outputs. For now assume that: + // - there is exactly one output; + // - the output is from the last layer. + const auto &output_name = _predict_net->op().rbegin()->output(0); + auto output = getOutputForTensor(output_name); + _graph->create(output); } const std::map Caffe2Importer::_operatorTypes = { diff --git a/compiler/mir-caffe2-importer/caffe2_importer.h b/compiler/mir-caffe2-importer/caffe2_importer.h index ccff1c9..9af8387 100644 --- a/compiler/mir-caffe2-importer/caffe2_importer.h +++ b/compiler/mir-caffe2-importer/caffe2_importer.h @@ -52,7 +52,6 @@ private: // Maps Caffe2 operator input names to corresponding MIR operation outputs. std::unordered_map _blobNameToOutput; - mir::Operation *_lastMIROp = nullptr; void import(); std::unique_ptr createIR(); diff --git a/compiler/mir-caffe2-importer/caffe2_op_creator.cpp b/compiler/mir-caffe2-importer/caffe2_op_creator.cpp index 4242ab7..430fbad 100644 --- a/compiler/mir-caffe2-importer/caffe2_op_creator.cpp +++ b/compiler/mir-caffe2-importer/caffe2_op_creator.cpp @@ -24,7 +24,6 @@ #include "mir/ops/Conv2DOp.h" #include "mir/ops/DepthwiseConv2DOp.h" #include "mir/ops/FullyConnectedOp.h" -#include "mir/ops/InputOp.h" #include "mir/ops/MulOp.h" #include "mir/ops/PoolOp.h" #include "mir/ops/ReluOp.h" @@ -217,16 +216,14 @@ static Shape getWindowShape(const ::caffe2::OperatorDef &op, mir::Operation::Output *Caffe2OpCreator::convertCaffeToMIR(mir::Operation::Output *arg) { // NCHW -> NHWC - auto transpose = - createOp("CaffeToMIR", arg, std::vector{0, 2, 3, 1}); + auto transpose = createOp(arg, std::vector{0, 2, 3, 1}); return transpose->getOutput(0); } mir::Operation::Output *Caffe2OpCreator::convertMIRToCaffe(mir::Operation::Output *arg) { // NHWC -> NCHW - auto transpose = - createOp("MIRToCaffe", arg, std::vector{0, 3, 1, 2}); + auto transpose = createOp(arg, std::vector{0, 3, 1, 2}); return transpose->getOutput(0); } @@ -320,7 +317,7 @@ Caffe2OpCreator::convertConstant(const std::vector &in if (!hasArgument(op.arg(), "values")) return {}; - return {createOp("", createTensor(op))->getOutput(0)}; + return {createOp(createTensor(op))->getOutput(0)}; } std::vector @@ -330,12 +327,12 @@ Caffe2OpCreator::convertAdd(const std::vector &inputs, if (getSingleArgument(op, "broadcast", 0) != 0) { // FIXME This only works when 'axis' == 1 and the second input is 1-D. - auto result = createOp("", convertCaffeToMIR(inputs[0]), inputs[1])->getOutput(0); + auto result = createOp(convertCaffeToMIR(inputs[0]), inputs[1])->getOutput(0); return {convertMIRToCaffe(result)}; } - auto result = createOp("", inputs[0], inputs[1])->getOutput(0); + auto result = createOp(inputs[0], inputs[1])->getOutput(0); return {result}; } std::vector @@ -354,8 +351,8 @@ Caffe2OpCreator::convertAveragePool(const std::vector std::vector pad_before, pad_after; std::tie(pad_before, pad_after) = getPadding(op); - auto pooling = createOp("Average_Pool", convertCaffeToMIR(inputs[0]), pool_type, - window_shape, strides, pad_before, pad_after, border_type); + auto pooling = createOp(convertCaffeToMIR(inputs[0]), pool_type, window_shape, + strides, pad_before, pad_after, border_type); return {convertMIRToCaffe(pooling->getOutput(0))}; } @@ -386,9 +383,9 @@ Caffe2OpCreator::convertConv(const std::vector &inputs { // TODO handle properly kernel with layer multiplier auto transposed_tensor = mir::transposeTensor<0, 1, 3, 2>(kernel_tensor); - auto kernel = createOp("Constant", transposed_tensor)->getOutput(0); - result = createOp("Depthwise_Conv2D", convertCaffeToMIR(inputs[0]), - kernel, stride_shape, pad_before, pad_after) + auto kernel = createOp(transposed_tensor)->getOutput(0); + result = createOp(convertCaffeToMIR(inputs[0]), kernel, stride_shape, + pad_before, pad_after) ->getOutput(0); } else @@ -397,15 +394,15 @@ Caffe2OpCreator::convertConv(const std::vector &inputs if (num_groups != 1) kernel_tensor = fixGroupedKernel(num_groups, kernel_tensor); kernel_tensor = transposeTensor<3, 0, 1, 2>(kernel_tensor); - auto kernel = createOp("Constant", kernel_tensor)->getOutput(0); - result = createOp("Conv2D", convertCaffeToMIR(inputs[0]), kernel, stride_shape, - pad_before, pad_after) + auto kernel = createOp(kernel_tensor)->getOutput(0); + result = createOp(convertCaffeToMIR(inputs[0]), kernel, stride_shape, pad_before, + pad_after) ->getOutput(0); } if (op.input_size() > 2) { - result = createOp("", result, inputs[2])->getOutput(0); + result = createOp(result, inputs[2])->getOutput(0); } return {convertMIRToCaffe(result)}; @@ -419,7 +416,7 @@ Caffe2OpCreator::convertConcat(const std::vector &inpu // `1` corresponds to the default (channels) axis. int axis = getSingleArgument(op, "axis", 1); - auto result = createOp("Concat", inputs, axis); + auto result = createOp(inputs, axis); return {result->getOutput(0)}; } @@ -449,10 +446,10 @@ Caffe2OpCreator::convertFC(const std::vector &inputs, // Transform input into 2-D tensor by flattening axes Shape shape{input_shape.dim(0), input_shape.numElements() / input_shape.dim(0)}; - auto reshape = createOp("Reshape", inputs[0], shape)->getOutput(0); - auto weights = createOp("Constant", weights_tensor)->getOutput(0); - auto result = createOp("Fully_Connected", reshape, weights)->getOutput(0); - result = createOp("", result, inputs[2])->getOutput(0); + auto reshape = createOp(inputs[0], shape)->getOutput(0); + auto weights = createOp(weights_tensor)->getOutput(0); + auto result = createOp(reshape, weights)->getOutput(0); + result = createOp(result, inputs[2])->getOutput(0); return {result}; } @@ -472,8 +469,8 @@ Caffe2OpCreator::convertMaxPool(const std::vector &inp std::vector pad_before, pad_after; std::tie(pad_before, pad_after) = getPadding(op); - auto pooling = createOp("Pool", convertCaffeToMIR(inputs[0]), pool_type, - window_shape, strides, pad_before, pad_after, border_type); + auto pooling = createOp(convertCaffeToMIR(inputs[0]), pool_type, window_shape, + strides, pad_before, pad_after, border_type); return {convertMIRToCaffe(pooling->getOutput(0))}; } @@ -485,19 +482,19 @@ Caffe2OpCreator::convertMul(const std::vector &inputs, if (getSingleArgument(op, "broadcast", 0) != 0) { // FIXME This only works when `axis` == 1 and the second input is 1-D. - auto result = createOp("", convertCaffeToMIR(inputs[0]), inputs[1])->getOutput(0); + auto result = createOp(convertCaffeToMIR(inputs[0]), inputs[1])->getOutput(0); return {convertMIRToCaffe(result)}; } - auto result = createOp("", inputs[0], inputs[1])->getOutput(0); + auto result = createOp(inputs[0], inputs[1])->getOutput(0); return {result}; } std::vector Caffe2OpCreator::convertRelu(const std::vector &inputs) { - auto relu = createOp("Relu", inputs[0]); + auto relu = createOp(inputs[0]); return {relu->getOutput(0)}; } @@ -513,7 +510,7 @@ Caffe2OpCreator::convertResizeNearest(const std::vector("ResizeNearest", convertCaffeToMIR(inputs[0]), + auto resize = createOp(convertCaffeToMIR(inputs[0]), ops::ResizeOp::ResizeMethod::nearestNeighbor, scales); return {convertMIRToCaffe(resize->getOutput(0))}; } @@ -521,7 +518,7 @@ Caffe2OpCreator::convertResizeNearest(const std::vector Caffe2OpCreator::convertSigmoid(const std::vector &inputs) { - auto result = createOp("Sigmoid", inputs[0]); + auto result = createOp(inputs[0]); return {result->getOutput(0)}; } @@ -530,7 +527,7 @@ Caffe2OpCreator::convertSoftmax(const std::vector &inp const ::caffe2::OperatorDef &op) { int axis = getSingleArgument(op, "axis", 1); - auto softmax = createOp("Softmax", inputs[0], axis); + auto softmax = createOp(inputs[0], axis); return {softmax->getOutput(0)}; } @@ -568,19 +565,19 @@ Caffe2OpCreator::convertSpatialBN(const std::vector &i for (auto &idx : ShapeRange(bias_data.getShape())) bias_data.at(idx) *= -1; - auto mean = createOp("Constant", mean_tensor)->getOutput(0); - auto result = createOp("", convertCaffeToMIR(inputs[0]), mean)->getOutput(0); + auto mean = createOp(mean_tensor)->getOutput(0); + auto result = createOp(convertCaffeToMIR(inputs[0]), mean)->getOutput(0); // res2 = res1 * scale / (var + epsilon) Tensor multiplier(scale_tensor); for (auto &idx : ShapeRange(scale_tensor.getShape())) multiplier.at(idx) /= std::sqrt(*reinterpret_cast(var_tensor.at(idx)) + eps); - auto scale = createOp("Constant", scale_tensor)->getOutput(0); - result = createOp("", result, scale)->getOutput(0); + auto scale = createOp(scale_tensor)->getOutput(0); + result = createOp(result, scale)->getOutput(0); // overall_res = res2 + bias - auto bias = createOp("Constant", bias_tensor)->getOutput(0); - result = createOp("", result, bias)->getOutput(0); + auto bias = createOp(bias_tensor)->getOutput(0); + result = createOp(result, bias)->getOutput(0); return {convertMIRToCaffe(result)}; } @@ -588,10 +585,10 @@ Caffe2OpCreator::convertSpatialBN(const std::vector &i std::vector Caffe2OpCreator::convertSum(const std::vector &inputs) { - auto result = createOp("", inputs[0], inputs[1])->getOutput(0); + auto result = createOp(inputs[0], inputs[1])->getOutput(0); for (int i = 2; i < static_cast(inputs.size()); ++i) { - result = createOp("", result, inputs[i])->getOutput(0); + result = createOp(result, inputs[i])->getOutput(0); } return {result}; } @@ -605,7 +602,7 @@ Caffe2OpCreator::convertClip(const std::vector &inputs float min = getSingleArgument(op, "min", float(0)); assert(max > 0.0 && min == 0.0 && "Support only if clip is CappedRelu"); - auto cap_relu = createOp("Capped_Relu", inputs[0], max); + auto cap_relu = createOp(inputs[0], max); return {cap_relu->getOutput(0)}; } @@ -630,14 +627,9 @@ Caffe2OpCreator::convertReshape(const std::vector &inp } Shape out_shape(shape_vec); - auto reshape = createOp("Reshape", inputs[0], out_shape); + auto reshape = createOp(inputs[0], out_shape); return {reshape->getOutput(0)}; } -Operation::Output *Caffe2OpCreator::createInput(const std::string &name, const mir::Shape &shape) -{ - return _graph->create(name, shape)->getOutput(0); -} - } // namespace mir_caffe2 diff --git a/compiler/mir-caffe2-importer/caffe2_op_creator.h b/compiler/mir-caffe2-importer/caffe2_op_creator.h index 41cda23..57424a2 100644 --- a/compiler/mir-caffe2-importer/caffe2_op_creator.h +++ b/compiler/mir-caffe2-importer/caffe2_op_creator.h @@ -41,8 +41,6 @@ class Caffe2OpCreator public: explicit Caffe2OpCreator(mir::Graph *g) : _graph(g) {} - Operation::Output *createInput(const std::string &name, const mir::Shape &shape); - std::vector convertConstant(const std::vector &inputs, const ::caffe2::OperatorDef &op); @@ -110,17 +108,13 @@ private: mir::Operation::Output *convertMIRToCaffe(mir::Operation::Output *arg); - template - mir::Operation *createOp(const std::string &name, Types &&... args); + template mir::Operation *createOp(Types &&... args); }; template -mir::Operation *Caffe2OpCreator::createOp(const std::string &name, Types &&... args) +mir::Operation *Caffe2OpCreator::createOp(Types &&... args) { - mir::Operation *new_op = _graph->create("", std::forward(args)...); - std::string op_name = name + "_" + std::to_string(new_op->getId()); - new_op->setName(op_name); - return new_op; + return _graph->create(std::forward(args)...); } } // namespace mir_caffe2