Add Caffe model visitor skeleton (#540)
authorDmitry Mozolev/AI Tools Lab /SRR/Engineer/삼성전자 <d.mozolev@samsung.com>
Tue, 10 Jul 2018 10:54:50 +0000 (13:54 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Tue, 10 Jul 2018 10:54:50 +0000 (19:54 +0900)
Add Caffe model visitor skeleton

This commit adds Caffe model visitor skeleton.

Signed-off-by: Dmitry Mozolev <d.mozolev@samsung.com>
contrib/nnc/libs/frontend/caffe/CMakeLists.txt
contrib/nnc/libs/frontend/caffe/include/caffe_model_visitor.h [new file with mode: 0644]
contrib/nnc/libs/frontend/caffe/src/caffe_model_visitor.cpp [new file with mode: 0644]

index d76bc97..c78457f 100644 (file)
@@ -21,4 +21,6 @@ target_include_directories(caffe_importer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc
 
 target_link_libraries(caffe_importer PUBLIC caffeproto)
 target_link_libraries(caffe_importer PUBLIC ${nn_import_common})
+target_link_libraries(caffe_importer PRIVATE nnc_core)
 target_link_libraries(caffe_importer PRIVATE nncc_core)
+target_link_libraries(caffe_importer PRIVATE nnc_plugin_core)
diff --git a/contrib/nnc/libs/frontend/caffe/include/caffe_model_visitor.h b/contrib/nnc/libs/frontend/caffe/include/caffe_model_visitor.h
new file mode 100644 (file)
index 0000000..87510be
--- /dev/null
@@ -0,0 +1,62 @@
+#ifndef NNCC_CAFFE_IR_VISITOR_H
+#define NNCC_CAFFE_IR_VISITOR_H
+
+#include <map>
+#include <vector>
+
+#include "nnc/core/IR/model/graph/graph.h"
+#include "nnc/core/IR/model/graph/ir_node.h"
+#include "nnc/core/linalg/TensorVariant.h"
+
+#include "caffe_visitor.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace caffe
+{
+
+using namespace ::caffe;
+
+using IrTensor = nncc::contrib::core::ADT::TensorVariant;
+using nncc::contrib::core::IR::model::Graph;
+using nncc::contrib::core::IR::model::ADT::INode;
+using nncc::core::ADT::tensor::Shape;
+
+class ModelVisitor : public Visitor
+{
+public:
+    ModelVisitor() : graph(new Graph()) {};
+
+    void visit(const NetParameter&) override;
+    void visit(const LayerParameter&) override;
+    void visit(const BlobProto&) override;
+    void visit(const BlobShape&) override;
+
+    Graph* getGraph();
+
+private:
+    Graph* graph = nullptr;
+
+    std::vector<Shape> inputShapes;
+    std::map<std::string, INode::Ref> opsForBlobsTheyOutput;
+
+    std::shared_ptr<IrTensor> createTensor(const BlobProto&);
+    std::vector<INode::Ref> createOpInputs(const LayerParameter&);
+    std::vector<std::shared_ptr<IrTensor>> createOpParams(const LayerParameter&);
+
+    void createGraphInputs(const std::vector<std::string> &names,
+                           const std::vector<Shape> &shapes);
+    void processInputLayer(const LayerParameter&);
+    void processDeprecatedInput(const NetParameter&);
+};
+
+} // namespace caffe
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc
+
+#endif //NNCC_CAFFE_IR_VISITOR_H
diff --git a/contrib/nnc/libs/frontend/caffe/src/caffe_model_visitor.cpp b/contrib/nnc/libs/frontend/caffe/src/caffe_model_visitor.cpp
new file mode 100644 (file)
index 0000000..54d8794
--- /dev/null
@@ -0,0 +1,75 @@
+#include <vector>
+#include <cassert>
+#include <stdexcept>
+
+#include "nncc/core/ADT/tensor/Shape.h"
+#include "nnc/core/IR/model/operations/variable_op.h"
+
+#include "shape_helper.h"
+#include "caffe_model_visitor.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace caffe
+{
+
+using VariableOp = nncc::contrib::core::IR::model::ops::VariableOp;
+using nncc::core::ADT::tensor::Shape;
+
+void ModelVisitor::visit(const NetParameter& np)
+{
+    processDeprecatedInput(np);
+}
+
+void ModelVisitor::visit(const LayerParameter& lp)
+{
+  throw std::runtime_error("Not yet implemented");
+}
+
+void ModelVisitor::visit(const BlobProto&) { throw std::runtime_error("Not yet implemented"); }
+void ModelVisitor::visit(const BlobShape&) { throw std::runtime_error("Not yet implemented"); }
+
+Graph *ModelVisitor::getGraph()
+{
+    return graph;
+}
+
+void ModelVisitor::createGraphInputs(const std::vector<std::string> &names,
+                                     const std::vector<Shape> &shapes)
+{
+  throw std::runtime_error("Not yet implemented");
+}
+
+void ModelVisitor::processDeprecatedInput(const NetParameter& np)
+{
+  throw std::runtime_error("Not yet implemented");
+}
+
+void ModelVisitor::processInputLayer(const LayerParameter& lp)
+{
+  throw std::runtime_error("Not yet implemented");
+}
+
+std::shared_ptr<IrTensor> ModelVisitor::createTensor(const BlobProto &bp)
+{
+  throw std::runtime_error("Not yet implemented");
+}
+
+std::vector<INode::Ref> ModelVisitor::createOpInputs(const LayerParameter &lp)
+{
+  throw std::runtime_error("Not yet implemented");
+}
+
+std::vector<std::shared_ptr<IrTensor>> ModelVisitor::createOpParams(const LayerParameter &lp)
+{
+  throw std::runtime_error("Not yet implemented");
+}
+
+} // namespace caffe
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc