[nnkit] Introduce nnkit_support_caffe (#936)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 8 Aug 2018 10:30:20 +0000 (19:30 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 8 Aug 2018 10:30:20 +0000 (19:30 +0900)
This commit extracts BlobContext and its descendents from nnkit Caffe
backend implementation as nnkit_support_caffe library.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/nnkit/backends/caffe/CMakeLists.txt
contrib/nnkit/backends/caffe/Module.cpp
contrib/nnkit/libs/support/caffe/CMakeLists.txt [new file with mode: 0644]
contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/BlobContext.h [new file with mode: 0644]
contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/InputBlobContext.h [new file with mode: 0644]
contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/OutputBlobContext.h [new file with mode: 0644]

index 3481bfb..3139fea 100644 (file)
@@ -1,9 +1,6 @@
-nncc_find_package(Caffe QUIET)
-
-if(NOT Caffe_FOUND)
+if(NOT TARGET nnkit_support_caffe)
   return()
-endif(NOT Caffe_FOUND)
+endif(NOT TARGET nnkit_support_caffe)
 
 add_library(nnkit_caffe_backend SHARED Module.cpp)
-target_link_libraries(nnkit_caffe_backend nnkit_intf_backend)
-target_link_libraries(nnkit_caffe_backend caffe)
+target_link_libraries(nnkit_caffe_backend nnkit_support_caffe)
index f62cb65..d80e6df 100644 (file)
@@ -1,8 +1,14 @@
+#include "nnkit/support/caffe/BlobContext.h"
+#include "nnkit/support/caffe/InputBlobContext.h"
+#include "nnkit/support/caffe/OutputBlobContext.h"
+
 #include <nncc/core/ADT/tensor/LexicalLayout.h>
 #include <nncc/core/ADT/tensor/Overlay.h>
 
 #include <caffe/caffe.hpp>
 
+using namespace nnkit::support::caffe;
+
 namespace
 {
 
@@ -21,77 +27,6 @@ template <typename DType> nncc::core::ADT::tensor::Shape shape(const caffe::Blob
   return shape;
 }
 
-template <typename DType> struct BlobContext
-{
-  virtual ~BlobContext() = default;
-
-  virtual uint32_t size(void) const = 0;
-
-  virtual std::string name(uint32_t n) const = 0;
-  virtual caffe::Blob<DType> *blob(uint32_t n) = 0;
-
-  DType *region(uint32_t n)
-  {
-    return blob(n)->mutable_cpu_data();
-  }
-};
-
-template <typename DType> class InputBlobContext final : public BlobContext<DType>
-{
-public:
-  InputBlobContext(caffe::Net<DType> &net) : _net(net)
-  {
-    // DO NOTHING
-  }
-
-public:
-  uint32_t size(void) const override
-  {
-    return _net.num_inputs();
-  }
-
-  std::string name(uint32_t n) const override
-  {
-    return _net.blob_names().at(_net.input_blob_indices().at(n));
-  }
-
-  caffe::Blob<DType> *blob(uint32_t n) override
-  {
-    return _net.input_blobs().at(n);
-  }
-
-private:
-  caffe::Net<DType> &_net;
-};
-
-template <typename DType> class OutputBlobContext final : public BlobContext<DType>
-{
-public:
-  OutputBlobContext(caffe::Net<DType> &net) : _net(net)
-  {
-    // DO NOTHING
-  }
-
-public:
-  uint32_t size(void) const override
-  {
-    return _net.num_outputs();
-  }
-
-  std::string name(uint32_t n) const override
-  {
-    return _net.blob_names().at(_net.output_blob_indices().at(n));
-  }
-
-  caffe::Blob<DType> *blob(uint32_t n) override
-  {
-    return _net.output_blobs().at(n);
-  }
-
-private:
-  caffe::Net<DType> &_net;
-};
-
 }
 
 #include <nnkit/TensorContext.h>
diff --git a/contrib/nnkit/libs/support/caffe/CMakeLists.txt b/contrib/nnkit/libs/support/caffe/CMakeLists.txt
new file mode 100644 (file)
index 0000000..91a2a71
--- /dev/null
@@ -0,0 +1,10 @@
+nncc_find_package(Caffe QUIET)
+
+if(NOT Caffe_FOUND)
+  return()
+endif(NOT Caffe_FOUND)
+
+add_library(nnkit_support_caffe INTERFACE)
+target_include_directories(nnkit_support_caffe INTERFACE include)
+target_link_libraries(nnkit_support_caffe INTERFACE nnkit_intf_backend)
+target_link_libraries(nnkit_support_caffe INTERFACE caffe)
diff --git a/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/BlobContext.h b/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/BlobContext.h
new file mode 100644 (file)
index 0000000..02d5ec5
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef __NNKIT_SUPPORT_CAFFE_BLOB_CONTEXT_H__
+#define __NNKIT_SUPPORT_CAFFE_BLOB_CONTEXT_H__
+
+#include <caffe/blob.hpp>
+
+namespace nnkit
+{
+namespace support
+{
+namespace caffe
+{
+
+template <typename DType> struct BlobContext
+{
+  virtual ~BlobContext() = default;
+
+  virtual uint32_t size(void) const = 0;
+
+  virtual std::string name(uint32_t n) const = 0;
+  virtual ::caffe::Blob<DType> *blob(uint32_t n) = 0;
+
+  DType *region(uint32_t n)
+  {
+    return blob(n)->mutable_cpu_data();
+  }
+};
+
+} // namespace caffe
+} // namespace support
+} // namespace nnkit
+
+#endif // __NNKIT_SUPPORT_CAFFE_BLOB_CONTEXT_H__
diff --git a/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/InputBlobContext.h b/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/InputBlobContext.h
new file mode 100644 (file)
index 0000000..c4e50be
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef __NNKIT_SUPPORT_CAFFE_INPUT_BLOB_CONTEXT_H__
+#define __NNKIT_SUPPORT_CAFFE_INPUT_BLOB_CONTEXT_H__
+
+#include "nnkit/support/caffe/BlobContext.h"
+
+#include <caffe/net.hpp>
+
+namespace nnkit
+{
+namespace support
+{
+namespace caffe
+{
+
+template <typename DType> class InputBlobContext final : public BlobContext<DType>
+{
+public:
+  InputBlobContext(::caffe::Net<DType> &net) : _net(net)
+  {
+    // DO NOTHING
+  }
+
+public:
+  uint32_t size(void) const override
+  {
+    return _net.num_inputs();
+  }
+
+  std::string name(uint32_t n) const override
+  {
+    return _net.blob_names().at(_net.input_blob_indices().at(n));
+  }
+
+  ::caffe::Blob<DType> *blob(uint32_t n) override
+  {
+    return _net.input_blobs().at(n);
+  }
+
+private:
+  ::caffe::Net<DType> &_net;
+};
+
+} // namespace caffe
+} // namespace support
+} // namespace nnkit
+
+#endif // __NNKIT_SUPPORT_CAFFE_INPUT_BLOB_CONTEXT_H__
diff --git a/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/OutputBlobContext.h b/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/OutputBlobContext.h
new file mode 100644 (file)
index 0000000..437b0b9
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef __NNKIT_SUPPORT_CAFFE_OUTPUT_BLOB_CONTEXT_H__
+#define __NNKIT_SUPPORT_CAFFE_OUTPUT_BLOB_CONTEXT_H__
+
+#include "nnkit/support/caffe/BlobContext.h"
+
+#include <caffe/net.hpp>
+
+namespace nnkit
+{
+namespace support
+{
+namespace caffe
+{
+
+template <typename DType> class OutputBlobContext final : public BlobContext<DType>
+{
+public:
+  OutputBlobContext(::caffe::Net<DType> &net) : _net(net)
+  {
+    // DO NOTHING
+  }
+
+public:
+  uint32_t size(void) const override
+  {
+    return _net.num_outputs();
+  }
+
+  std::string name(uint32_t n) const override
+  {
+    return _net.blob_names().at(_net.output_blob_indices().at(n));
+  }
+
+  ::caffe::Blob<DType> *blob(uint32_t n) override
+  {
+    return _net.output_blobs().at(n);
+  }
+
+private:
+  ::caffe::Net<DType> &_net;
+};
+
+} // namespace caffe
+} // namespace support
+} // namespace nnkit
+
+#endif // __NNKIT_SUPPORT_CAFFE_OUTPUT_BLOB_CONTEXT_H__