From 5e9f9b20ab8c121b6b237d8c3ef6aef693d2fc0f 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, 30 Jul 2019 22:10:10 +0300 Subject: [PATCH] [mir/Caffe2 importer] Cleanup after changing interface (#6033) * Remove `cleanup` method. * Move `import` and `createIR` methods into private section. * Use `make_unique` to deal with `unique_ptr`s. Signed-off-by: Sergei Barannikov --- compiler/mir-caffe2-importer/CMakeLists.txt | 2 +- compiler/mir-caffe2-importer/caffe2_importer.cpp | 27 ++++++++++++------------ compiler/mir-caffe2-importer/caffe2_importer.h | 21 +++++------------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/compiler/mir-caffe2-importer/CMakeLists.txt b/compiler/mir-caffe2-importer/CMakeLists.txt index 5b1c3d7..20dd4a7 100644 --- a/compiler/mir-caffe2-importer/CMakeLists.txt +++ b/compiler/mir-caffe2-importer/CMakeLists.txt @@ -26,4 +26,4 @@ set(MIR_CAFFE2_IMPORTER_SOURCES add_library(mir_caffe2_importer STATIC ${MIR_CAFFE2_IMPORTER_SOURCES}) set_target_properties(mir_caffe2_importer PROPERTIES POSITION_INDEPENDENT_CODE ON) target_include_directories(mir_caffe2_importer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(mir_caffe2_importer PUBLIC mir caffe2proto) +target_link_libraries(mir_caffe2_importer PUBLIC mir caffe2proto PRIVATE stdex) diff --git a/compiler/mir-caffe2-importer/caffe2_importer.cpp b/compiler/mir-caffe2-importer/caffe2_importer.cpp index 27c15d9..e8ea29a 100644 --- a/compiler/mir-caffe2-importer/caffe2_importer.cpp +++ b/compiler/mir-caffe2-importer/caffe2_importer.cpp @@ -29,7 +29,9 @@ #include #include #include +#include #include +#include #include namespace nnc @@ -39,20 +41,22 @@ using namespace ::caffe2; using mir::Shape; Caffe2Importer::Caffe2Importer(std::string predict_net, std::string init_net, - std::vector> input_shapes) - : _predictNet(std::move(predict_net)), _initNet(std::move(init_net)), _graph(new mir::Graph()), - _opCreator(new Caffe2OpCreator(_graph)) + const std::vector> &input_shapes) + : _predictNet(std::move(predict_net)), _initNet(std::move(init_net)) { for (auto &shape : input_shapes) _inputShapes.emplace_back(shape); + + _graph = stdex::make_unique(); + _opCreator = stdex::make_unique(_graph.get()); } Caffe2Importer::~Caffe2Importer() = default; -void Caffe2Importer::cleanup() { delete _graph; } - static void loadModelFile(const std::string &filename, caffe2::NetDef *net) { + GOOGLE_PROTOBUF_VERIFY_VERSION; + int file_handle = open(filename.c_str(), O_RDONLY); if (file_handle == -1) @@ -75,13 +79,10 @@ static void loadModelFile(const std::string &filename, caffe2::NetDef *net) void Caffe2Importer::import() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - _net.reset(new NetDef()); + _net = stdex::make_unique(); loadModelFile(_predictNet, _net.get()); - std::unique_ptr net2; - net2.reset(new NetDef()); + auto net2 = stdex::make_unique(); loadModelFile(_initNet, net2.get()); _net->MergeFrom(*net2); @@ -90,20 +91,20 @@ void Caffe2Importer::import() preloadAllTensors(); } -mir::Graph *Caffe2Importer::createIR() +std::unique_ptr Caffe2Importer::createIR() { for (auto &op : _net->op()) createMIRNodesFromOp(op); setGraphOutputs(); - return _graph; + return std::move(_graph); } std::unique_ptr Caffe2Importer::importModel() { import(); - return std::unique_ptr(createIR()); + return createIR(); } void Caffe2Importer::collectUnsupportedOps() diff --git a/compiler/mir-caffe2-importer/caffe2_importer.h b/compiler/mir-caffe2-importer/caffe2_importer.h index 3e04b52..1b39341 100644 --- a/compiler/mir-caffe2-importer/caffe2_importer.h +++ b/compiler/mir-caffe2-importer/caffe2_importer.h @@ -32,31 +32,17 @@ class Caffe2Importer { public: explicit Caffe2Importer(std::string predict_net, std::string init_net, - std::vector> input_shapes); - - /** - * @brief Import model from file, must be called before 'createIR' method - * @throw PassException in case, if model couldn't be parsed or NNC doesn't support it - */ - void import(); - - /** - * @brief Create MIR graph from caffe model, must be called after 'import' method - * @return MIR graph, corresponding to processed caffe model - */ - mir::Graph *createIR(); + const std::vector> &input_shapes); /// @brief Load the model and convert it into a MIR Graph. std::unique_ptr importModel(); - void cleanup(); - ~Caffe2Importer(); private: std::string _predictNet; std::string _initNet; - mir::Graph *_graph; + std::unique_ptr _graph; std::unique_ptr<::caffe2::NetDef> _net; std::unique_ptr _opCreator; std::vector _inputShapes; @@ -71,6 +57,9 @@ private: std::map _MIRTensors; + void import(); + std::unique_ptr createIR(); + /** * @brief Pass through caffe2 graph and collect ops unsupported by NNC * @throw PassException with message, containing detected problems -- 2.7.4