[mir/Caffe2 importer] Cleanup after changing interface (#6033)
authorСергей Баранников/AI Tools Lab /SRR/Engineer/삼성전자 <s.barannikov@samsung.com>
Tue, 30 Jul 2019 19:10:10 +0000 (22:10 +0300)
committerEfimov Alexander/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Tue, 30 Jul 2019 19:10:10 +0000 (22:10 +0300)
* 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 <s.barannikov@samsung.com>
compiler/mir-caffe2-importer/CMakeLists.txt
compiler/mir-caffe2-importer/caffe2_importer.cpp
compiler/mir-caffe2-importer/caffe2_importer.h

index 5b1c3d7..20dd4a7 100644 (file)
@@ -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)
index 27c15d9..e8ea29a 100644 (file)
@@ -29,7 +29,9 @@
 #include <cassert>
 #include <cerrno>
 #include <cstring>
+#include <stdex/Memory.h>
 #include <stdexcept>
+#include <utility>
 #include <vector>
 
 namespace nnc
@@ -39,20 +41,22 @@ using namespace ::caffe2;
 using mir::Shape;
 
 Caffe2Importer::Caffe2Importer(std::string predict_net, std::string init_net,
-                               std::vector<std::vector<int>> input_shapes)
-    : _predictNet(std::move(predict_net)), _initNet(std::move(init_net)), _graph(new mir::Graph()),
-      _opCreator(new Caffe2OpCreator(_graph))
+                               const std::vector<std::vector<int>> &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<mir::Graph>();
+  _opCreator = stdex::make_unique<Caffe2OpCreator>(_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<NetDef>();
   loadModelFile(_predictNet, _net.get());
 
-  std::unique_ptr<NetDef> net2;
-  net2.reset(new NetDef());
+  auto net2 = stdex::make_unique<NetDef>();
   loadModelFile(_initNet, net2.get());
   _net->MergeFrom(*net2);
 
@@ -90,20 +91,20 @@ void Caffe2Importer::import()
   preloadAllTensors();
 }
 
-mir::Graph *Caffe2Importer::createIR()
+std::unique_ptr<mir::Graph> Caffe2Importer::createIR()
 {
   for (auto &op : _net->op())
     createMIRNodesFromOp(op);
 
   setGraphOutputs();
 
-  return _graph;
+  return std::move(_graph);
 }
 
 std::unique_ptr<mir::Graph> Caffe2Importer::importModel()
 {
   import();
-  return std::unique_ptr<mir::Graph>(createIR());
+  return createIR();
 }
 
 void Caffe2Importer::collectUnsupportedOps()
index 3e04b52..1b39341 100644 (file)
@@ -32,31 +32,17 @@ class Caffe2Importer
 {
 public:
   explicit Caffe2Importer(std::string predict_net, std::string init_net,
-                          std::vector<std::vector<int>> 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<std::vector<int>> &input_shapes);
 
   /// @brief Load the model and convert it into a MIR Graph.
   std::unique_ptr<mir::Graph> importModel();
 
-  void cleanup();
-
   ~Caffe2Importer();
 
 private:
   std::string _predictNet;
   std::string _initNet;
-  mir::Graph *_graph;
+  std::unique_ptr<mir::Graph> _graph;
   std::unique_ptr<::caffe2::NetDef> _net;
   std::unique_ptr<Caffe2OpCreator> _opCreator;
   std::vector<mir::Shape> _inputShapes;
@@ -71,6 +57,9 @@ private:
 
   std::map<std::string, mir::TensorVariant> _MIRTensors;
 
+  void import();
+  std::unique_ptr<mir::Graph> createIR();
+
   /**
    * @brief Pass through caffe2 graph and collect ops unsupported by NNC
    * @throw PassException with message, containing detected problems