From dd15ab46995e84d69c9197157a7a20ee5c402af0 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: Thu, 12 Sep 2019 18:19:24 +0300 Subject: [PATCH] [mir_tflite] Set `data_format` parameter explicitly (#7415) * Pass `data_format` to constructors of conv-like ops explicitly. * Replace deprecated `PaddingType::Same` with equivalent `PaddingType::SameUpper`. * Remove unused includes. Signed-off-by: Sergei Barannikov --- compiler/mir-tflite-importer/tflite_importer.cpp | 4 +- compiler/mir-tflite-importer/tflite_op_creator.cpp | 59 ++++++++++++++-------- compiler/mir-tflite-importer/tflite_op_creator.h | 20 ++------ 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/compiler/mir-tflite-importer/tflite_importer.cpp b/compiler/mir-tflite-importer/tflite_importer.cpp index aa8afe4..4a43a0d 100644 --- a/compiler/mir-tflite-importer/tflite_importer.cpp +++ b/compiler/mir-tflite-importer/tflite_importer.cpp @@ -124,7 +124,7 @@ void TfliteImporter::collectUnsupportedOps() } } -std::unique_ptr TfliteImporter::createIR() +std::unique_ptr TfliteImporter::createIR() { walkGraphAndCreateMIR(); return std::move(_graph); @@ -306,7 +306,7 @@ std::vector TfliteImporter::getMIRInputsForOperator(co { assert(_tensorMap.find(i) == _tensorMap.end()); mir::TensorVariant mir_tensor = createTensor(tensor, buffer); - inputs.emplace_back(_graph->create(mir_tensor)->getOutput(0)); + inputs.emplace_back(_graph->create(mir_tensor)->getOutput(0)); } else { diff --git a/compiler/mir-tflite-importer/tflite_op_creator.cpp b/compiler/mir-tflite-importer/tflite_op_creator.cpp index ae5bd94..207f137 100644 --- a/compiler/mir-tflite-importer/tflite_op_creator.cpp +++ b/compiler/mir-tflite-importer/tflite_op_creator.cpp @@ -54,7 +54,24 @@ using namespace ::tflite; namespace mir_tflite { -static void calculatePadding(tflite::Padding padding, const Shape &input_shape, +namespace ops = mir::ops; +using mir::Shape; + +static mir::ops::PaddingType convertPadding(tflite::Padding padding) +{ + switch (padding) + { + case tflite::Padding_VALID: + return mir::ops::PaddingType::Valid; + case tflite::Padding_SAME: + return mir::ops::PaddingType::SameUpper; + default: + assert(false); + } +} + +// TODO Move this to MIR? +static void calculatePadding(mir::ops::PaddingType padding_type, const mir::Shape &input_shape, const std::vector &window_size, const std::vector &strides, std::vector &padding_before, @@ -66,9 +83,9 @@ static void calculatePadding(tflite::Padding padding, const Shape &input_shape, assert(padding_before.size() == num_spatial_dims); assert(padding_after.size() == num_spatial_dims); - switch (padding) + switch (padding_type) { - case tflite::Padding_SAME: + case mir::ops::PaddingType::SameUpper: for (int i = 0; i < num_spatial_dims; ++i) { // Assuming NHWC format. @@ -80,7 +97,7 @@ static void calculatePadding(tflite::Padding padding, const Shape &input_shape, padding_after[i] = total_padding - padding_before[i]; } break; - case tflite::Padding_VALID: + case mir::ops::PaddingType::Valid: for (int i = 0; i < num_spatial_dims; ++i) { padding_before[i] = 0; @@ -123,14 +140,15 @@ TFLiteOpCreator::convertConv2D(const Conv2DOptions *opts, std::vector padding_before(2); std::vector padding_after(2); + const auto padding_type = convertPadding(opts->padding()); const auto &input_shape = input->getShape(); const auto &kernel_shape = kernel->getShape(); std::vector kernel_size{kernel_shape.dim(1), kernel_shape.dim(2)}; - calculatePadding(opts->padding(), input_shape, kernel_size, strides, padding_before, - padding_after); + calculatePadding(padding_type, input_shape, kernel_size, strides, padding_before, padding_after); - auto result = - createOp(input, kernel, strides, padding_before, padding_after)->getOutput(0); + auto result = createOp(input, kernel, strides, padding_before, padding_after, + mir::DataFormat::NHWC) + ->getOutput(0); result = createOp(result, bias)->getOutput(0); return {addFusedActivation(result, opts->fused_activation_function())}; } @@ -151,15 +169,15 @@ TFLiteOpCreator::convertDepthwiseConv2D(const DepthwiseConv2DOptions *opts, std::vector padding_before(2); std::vector padding_after(2); + const auto padding_type = convertPadding(opts->padding()); const auto &input_shape = input->getShape(); const auto &kernel_shape = kernel->getShape(); std::vector kernel_size{kernel_shape.dim(0), kernel_shape.dim(1)}; - calculatePadding(opts->padding(), input_shape, kernel_size, strides, padding_before, - padding_after); + calculatePadding(padding_type, input_shape, kernel_size, strides, padding_before, padding_after); - auto result = - createOp(input, kernel, strides, padding_before, padding_after) - ->getOutput(0); + auto result = createOp(input, kernel, strides, padding_before, + padding_after, mir::DataFormat::NHWC) + ->getOutput(0); result = createOp(result, bias)->getOutput(0); return {addFusedActivation(result, opts->fused_activation_function())}; } @@ -184,8 +202,8 @@ TFLiteOpCreator::convertMaxPool2D(const ::tflite::Pool2DOptions *opts, std::vector padding_before(2); std::vector padding_after(2); - calculatePadding(opts->padding(), input_shape, window_size, strides, padding_before, - padding_after); + const auto padding_type = convertPadding(opts->padding()); + calculatePadding(padding_type, input_shape, window_size, strides, padding_before, padding_after); auto result = createOp(input, window_size, strides, padding_before, padding_after, mir::DataFormat::NHWC); @@ -204,8 +222,8 @@ TFLiteOpCreator::convertAveragePool2D(const ::tflite::Pool2DOptions *opts, std::vector padding_before(2); std::vector padding_after(2); - calculatePadding(opts->padding(), input_shape, window_size, strides, padding_before, - padding_after); + const auto padding_type = convertPadding(opts->padding()); + calculatePadding(padding_type, input_shape, window_size, strides, padding_before, padding_after); auto result = createOp(input, window_size, strides, padding_before, padding_after, false, mir::DataFormat::NHWC); @@ -271,9 +289,10 @@ TFLiteOpCreator::convertTransposeConv(const ::tflite::TransposeConvOptions *opts const std::vector axis_order{1, 2, 0, 3}; kernel = createOp(kernel, axis_order)->getOutput(0); - auto result = - createOp(input, kernel, strides, paddingMap[opts->padding()], output_shape) - ->getOutput(0); + const auto padding_type = convertPadding(opts->padding()); + auto result = createOp(input, kernel, strides, padding_type, output_shape, + mir::DataFormat::NHWC) + ->getOutput(0); return {result}; } diff --git a/compiler/mir-tflite-importer/tflite_op_creator.h b/compiler/mir-tflite-importer/tflite_op_creator.h index 058ebd7..5f0b48b 100644 --- a/compiler/mir-tflite-importer/tflite_op_creator.h +++ b/compiler/mir-tflite-importer/tflite_op_creator.h @@ -19,28 +19,18 @@ #include "schema_generated.h" -#include "mir/ops/CommonProps.h" #include "mir/Graph.h" -#include "mir/TensorVariant.h" -#include "mir/Shape.h" -#include -#include -#include -#include +#include #include namespace mir_tflite { -namespace ops = mir::ops; -using mir::Graph; -using mir::Shape; - class TFLiteOpCreator { public: - explicit TFLiteOpCreator(Graph *g) : _graph(g) {} + explicit TFLiteOpCreator(mir::Graph *g) : _graph(g) {} std::vector convertConv2D(const ::tflite::Conv2DOptions *opts, @@ -147,11 +137,7 @@ public: const std::vector &inputs); private: - Graph *_graph; - - std::map<::tflite::Padding, ops::PaddingType> paddingMap = { - {::tflite::Padding_SAME, ops::PaddingType::Same}, - {::tflite::Padding_VALID, ops::PaddingType::Valid}}; + mir::Graph *_graph; mir::Operation::Output *addFusedActivation(mir::Operation::Output *input, ::tflite::ActivationFunctionType activation_type); -- 2.7.4