[nnc backend] Add nnc interpreter backend plugin (#577)
authorVladimir Plazun/AI Tools Lab /SRR/Engineer/삼성전자 <v.plazun@partner.samsung.com>
Wed, 11 Jul 2018 15:24:57 +0000 (18:24 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Wed, 11 Jul 2018 15:24:57 +0000 (00:24 +0900)
Add nnc interpreter backend plugin

Adds nnc_interpreter_plugin target
Implements backend plugin interface for nnc
Can be used to interpret previously loaded computation graph

Signed-off-by: Vladimir Plazun <v.plazun@partner.samsung.com>
contrib/nnc/libs/backend/interpreter/CMakeLists.txt
contrib/nnc/libs/backend/interpreter/plugin/CMakeLists.txt [new file with mode: 0644]
contrib/nnc/libs/backend/interpreter/plugin/include/interpreter_plugin.h [new file with mode: 0644]
contrib/nnc/libs/backend/interpreter/plugin/src/interpreter_plugin.cpp [new file with mode: 0644]

diff --git a/contrib/nnc/libs/backend/interpreter/plugin/CMakeLists.txt b/contrib/nnc/libs/backend/interpreter/plugin/CMakeLists.txt
new file mode 100644 (file)
index 0000000..9d3a49b
--- /dev/null
@@ -0,0 +1,4 @@
+file(GLOB_RECURSE interp_plugin_src ./*.cpp ./*.h)
+add_library(nnc_interpreter_plugin SHARED ${interp_plugin_src})
+target_link_libraries(nnc_interpreter_plugin PRIVATE nnc_interpreter_core nnc_plugin_core)
+target_include_directories(nnc_interpreter_plugin PUBLIC include)
diff --git a/contrib/nnc/libs/backend/interpreter/plugin/include/interpreter_plugin.h b/contrib/nnc/libs/backend/interpreter/plugin/include/interpreter_plugin.h
new file mode 100644 (file)
index 0000000..4f9c67c
--- /dev/null
@@ -0,0 +1,54 @@
+#ifndef _NNC_BACKEND_INTERPRETER_PLUGIN_
+#define _NNC_BACKEND_INTERPRETER_PLUGIN_
+
+#include <unordered_map>
+
+#include "PluginInstance.h"
+#include "PluginException.h"
+#include "ConfigException.h"
+#include "PluginType.h"
+
+#include "nnc/core/linalg/TensorVariant.h"
+#include "nncc/core/ADT/tensor/Shape.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace backend
+{
+namespace interpreter
+{
+namespace plugin
+{
+
+using namespace nncc::contrib;
+using namespace config;
+using nncc::contrib::plugin::AbstractPluginInstance;
+
+class InterpreterPlugin : public AbstractPluginInstance {
+ public:
+  static AbstractPluginInstance &getInstance();
+  void fillSession() override;
+  void checkConfig() override;
+  void *execute(void *data) override;
+
+  void setParam(const std::string &name) override;
+  void setParam(const std::string &name, const std::string &value) override;
+
+  virtual ~InterpreterPlugin();
+
+private:
+  std::unordered_map<std::string, std::string> params;
+
+  nncc::contrib::core::ADT::TensorVariant loadInput(const nncc::core::ADT::tensor::Shape &);
+  nncc::contrib::core::ADT::TensorVariant *_out;
+};
+
+} // namespace plugin
+} // namespace interpreter
+} // namespace backend
+} // namespace contrib
+} // namespace nncc
+
+#endif //_NNC_BACKEND_INTERPRETER_PLUGIN_
diff --git a/contrib/nnc/libs/backend/interpreter/plugin/src/interpreter_plugin.cpp b/contrib/nnc/libs/backend/interpreter/plugin/src/interpreter_plugin.cpp
new file mode 100644 (file)
index 0000000..be3606b
--- /dev/null
@@ -0,0 +1,119 @@
+#include <map>
+#include <vector>
+#include <fstream>
+
+#include "PluginParam.h"
+#include "PluginInstance.h"
+
+#include "nncc/core/ADT/tensor/Shape.h"
+
+#include "interpreter/core/Interpreter.h"
+
+#include "nnc/core/IR/model/actions/ShapeInference.h"
+#include "nnc/core/IR/model/graph/graph.h"
+
+#include "nnc/core/linalg/ShapeRange.h"
+#include "nnc/core/linalg/Tensor.h"
+
+#include "interpreter_plugin.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace backend
+{
+namespace interpreter
+{
+namespace plugin
+{
+
+using nncc::core::ADT::tensor::Shape;
+using namespace nncc::contrib::core::IR::model;
+using nncc::contrib::backend::interpreter::core::NNInterpreter;
+using nncc::contrib::plugin::AbstractPluginInstance;
+using nncc::contrib::config::PluginParam;
+
+static const std::string pluginName = "NNInterpreterPlugin";
+static const std::string pluginVersion = "0.1";
+static const std::string pluginDesc = "NN Interpreter Plugin";
+static const auto pluginType = nncc::contrib::plugin::typeBackEnd;
+
+AbstractPluginInstance &InterpreterPlugin::getInstance() {
+  static InterpreterPlugin instance;
+  return instance;
+}
+
+void InterpreterPlugin::fillSession() {
+  static std::map<std::string, std::string> info = {{"module description", pluginDesc}};
+  static std::vector<PluginParam> moduleParams = {
+      {"filename", "Input file path", false},
+      {"input", "Input node name", false},
+      {"output", "Output node name", false}
+  };
+  AbstractPluginInstance::fillSessionBase(pluginType, pluginVersion, pluginName);
+
+  for (auto &i : info)
+    getSession()->addInfo(i.first, i.second);
+
+  for (auto &p : moduleParams)
+    getSession()->registerParam(p);
+}
+
+void InterpreterPlugin::checkConfig() {
+  //Nothing to configure
+}
+
+void *InterpreterPlugin::execute(void *data) {
+  auto g = static_cast<Graph *>(data);
+  ShapeInference shapeInference;
+
+  NNInterpreter interpreter;
+
+  g->accept(&shapeInference);
+
+  auto input = loadInput(g->getInput(params["input"])->getOperation()->getOutputShape(0));
+  interpreter.setInput(params["input"], input);
+  g->accept(&interpreter);
+
+  _out = new TensorVariant(interpreter.getResult(g->getOutput(params["output"]))[0]);
+  return _out;
+}
+
+void InterpreterPlugin::setParam(const std::string &name) {
+  //No known options
+  (void) name;
+}
+
+void InterpreterPlugin::setParam(const std::string &name, const std::string &value) {
+  params[name] = value;
+}
+
+TensorVariant InterpreterPlugin::loadInput(const Shape &shape)
+{
+  auto f = fopen(params["filename"].c_str(), "rb");
+  fseek(f, 0L, SEEK_END);
+  auto len = ftell(f);
+  rewind(f);
+  auto data = new char[len];
+  auto rlen = fread(data, len, 1, f);
+  assert(rlen == len);
+
+  return TensorVariant(shape, std::shared_ptr<char>(data, [](const char* d) { delete[] d; }), TensorVariant::DTYPE::FLOAT, sizeof(float));
+}
+
+InterpreterPlugin::~InterpreterPlugin()
+{
+  delete _out;
+}
+
+} // namespace plugin
+} // namespace interpreter
+} // namespace backend
+} // namespace contrib
+} // namespace nncc
+
+extern "C" nncc::contrib::backend::interpreter::plugin::AbstractPluginInstance *get_instance() {
+  return &nncc::contrib::backend::interpreter::plugin::InterpreterPlugin::getInstance();
+}
+