Refactor soft backend (#1305)
authorEfimov Alexander/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Wed, 5 Sep 2018 12:58:26 +0000 (15:58 +0300)
committerРоман Михайлович Русяев/AI Tools Lab /SRR/Staff Engineer/삼성전자 <r.rusyaev@samsung.com>
Wed, 5 Sep 2018 12:58:26 +0000 (15:58 +0300)
Remove redundant soft backend classes
Move all C++ code snippets to code_snippet directory
Generate headers from snippets to generated directory

Signed-off-by: Efimov Alexander <a.efimov@samsung.com>
29 files changed:
contrib/nnc/cmake/soft_backend.cmake
contrib/nnc/plugin/soft_backend/CMakeLists.txt
contrib/nnc/plugin/soft_backend/base_generator.cpp
contrib/nnc/plugin/soft_backend/base_generator.h
contrib/nnc/plugin/soft_backend/c_backend.cpp [deleted file]
contrib/nnc/plugin/soft_backend/c_generator.cpp
contrib/nnc/plugin/soft_backend/code_snippets/cpp_add_bias.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_add_bias.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_capped_relu.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_capped_relu.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_common_funcs.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_common_funcs.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_concat.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_concat.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_conv.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_conv.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_depthwise_conv.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_depthwise_conv.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_fully_connected.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_fully_connected.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_header_types.def [moved from contrib/nnc/plugin/soft_backend/cpp_header_types.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_operations.def [moved from contrib/nnc/plugin/soft_backend/cpp_operations.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_pool.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_pool.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_relu.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_relu.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/cpp_softmax.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/cpp_softmax.def with 100% similarity]
contrib/nnc/plugin/soft_backend/code_snippets/eigen.def [moved from contrib/nnc/plugin/soft_backend/cpp_ops/eigen.def with 100% similarity]
contrib/nnc/plugin/soft_backend/cpp_backend.cpp [deleted file]
contrib/nnc/plugin/soft_backend/cpp_generator.cpp
contrib/nnc/plugin/soft_backend/soft_backend.cpp [deleted file]
contrib/nnc/plugin/soft_backend/soft_backend.h [deleted file]
contrib/nnc/tests/soft_backend/compile_cpp.cpp
contrib/nnc/unittests/soft_backend/CMakeLists.txt
contrib/nnc/unittests/soft_backend/cpp_header_types.cpp
contrib/nnc/unittests/soft_backend/cpp_operations.cpp
contrib/nnc/unittests/soft_backend/generator.cpp
contrib/nnc/utils/def2src.cpp

index 24dcda6..ccabc07 100644 (file)
@@ -2,7 +2,7 @@ function(make_generated_sources DEF_SOURCES OUT_DIR GEN_SOURCES)
     set(GEN_OUT "")
     foreach(file IN LISTS DEF_SOURCES)
         get_filename_component(file_name ${file} NAME_WE)
-        set(out_file "${OUT_DIR}/${file_name}.h")
+        set(out_file "${OUT_DIR}/${file_name}.generated.h")
         list(APPEND GEN_OUT "${out_file}")
         add_custom_command(
                 OUTPUT  ${out_file}
index b21f65b..22b5998 100644 (file)
@@ -1,6 +1,6 @@
-set(SOFT_BACKEND_COMMON_SOURCES soft_backend.cpp base_generator.cpp model_analyzer.cpp serializer.cpp)
-set(SOFT_BACKEND_CPP_SOURCES cpp_backend.cpp cpp_generator.cpp)
-set(SOFT_BACKEND_C_SOURCES c_backend.cpp c_generator.cpp)
+set(SOFT_BACKEND_COMMON_SOURCES base_generator.cpp model_analyzer.cpp serializer.cpp)
+set(SOFT_BACKEND_CPP_SOURCES cpp_generator.cpp)
+set(SOFT_BACKEND_C_SOURCES c_generator.cpp)
 set(DEF_CONV ${NNC_ROOT_SRC_DIR}/utils/def2src.cpp)
 
 file(GLOB_RECURSE SOFT_DEF_SOURCES "*.def")
index 7fac842..dd08bde 100644 (file)
@@ -89,8 +89,9 @@ void BaseCodeGenerator::materializeModelParams(ostream &out, const Serializer &s
   }
 }
 
-void BaseCodeGenerator::generate(Graph *g)
+void *BaseCodeGenerator::execute(void *data)
 {
+  Graph *g = reinterpret_cast<Graph *>(data);
   // inference shapes
   core::IR::model::ShapeInference si;
   g->accept(&si);
@@ -119,6 +120,8 @@ void BaseCodeGenerator::generate(Graph *g)
   auto modelStream = getStream(_paramsPath);
   materializeModelParams(*modelStream, serializer);
   modelStream.reset();
+
+  return nullptr;
 }
 
 } // namespace soft
index dc2266e..369f1fb 100644 (file)
@@ -2,6 +2,7 @@
 #define _NNC_SOFT_BACKEND_BASE_GENERATOR_H_
 
 #include "core/modelIR/graph.h"
+#include "support/PluginInstance.h"
 
 #include <string>
 #include <ostream>
@@ -19,10 +20,10 @@ class ModelAnalyzer;
 
 class Serializer;
 
-class BaseCodeGenerator
+class BaseCodeGenerator: public nncc::contrib::plugin::BackendPlugin
 {
 public:
-  void generate(nncc::contrib::core::IR::model::Graph *g);
+  void *execute(void *data) override;
 
 protected:
   virtual void formatTensorNames(const ModelAnalyzer &ma) = 0;
diff --git a/contrib/nnc/plugin/soft_backend/c_backend.cpp b/contrib/nnc/plugin/soft_backend/c_backend.cpp
deleted file mode 100644 (file)
index ef31a6e..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-#include "soft_backend.h"
-#include "c_generator.h"
-
-using namespace nncc::contrib::plugin;
-using namespace nncc::contrib::backend::soft;
-
-namespace nncc
-{
-namespace contrib
-{
-namespace backend
-{
-namespace soft
-{
-
-class CSoftBackend final: public BaseSoftBackend
-{
-public:
-  CSoftBackend &operator=(const CSoftBackend &) = delete;
-
-  CSoftBackend(const CSoftBackend &) = delete;
-
-  void generate(nncc::contrib::core::IR::model::Graph *g) override
-  {
-    CCodeGenerator gen;
-    gen.generate(g);
-  }
-
-  static CSoftBackend &getInstance()
-  {
-    static CSoftBackend instance;
-    return instance;
-  }
-
-private:
-  CSoftBackend() = default;
-};
-
-} // namespace soft
-} // namespace backend
-} // namespace contrib
-} // namespace nncc
-
-
-extern "C" Plugin *get_instance()
-{
-  return &CSoftBackend::getInstance();
-}
index 89e394e..55d5e91 100644 (file)
@@ -34,3 +34,8 @@ void CCodeGenerator::materializeCode(ostream &out, const ModelAnalyzer &ma, cons
 } // namespace contrib
 } // namespace nncc
 
+extern "C" nncc::contrib::plugin::Plugin *get_instance()
+{
+  static nncc::contrib::backend::soft::CCodeGenerator cCodeGenerator;
+  return &cCodeGenerator;
+}
diff --git a/contrib/nnc/plugin/soft_backend/cpp_backend.cpp b/contrib/nnc/plugin/soft_backend/cpp_backend.cpp
deleted file mode 100644 (file)
index 3b5240f..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-#include "soft_backend.h"
-#include "cpp_generator.h"
-
-using namespace nncc::contrib::plugin;
-using namespace nncc::contrib::backend::soft;
-
-namespace nncc
-{
-namespace contrib
-{
-namespace backend
-{
-namespace soft
-{
-
-class CPPSoftBackend final: public BaseSoftBackend
-{
-public:
-  CPPSoftBackend &operator=(const CPPSoftBackend &) = delete;
-
-  CPPSoftBackend(const CPPSoftBackend &) = delete;
-
-  void generate(nncc::contrib::core::IR::model::Graph *g) override
-  {
-    CPPCodeGenerator gen;
-    gen.generate(g);
-  }
-
-  static CPPSoftBackend &getInstance()
-  {
-    static CPPSoftBackend instance;
-    return instance;
-  }
-
-private:
-  CPPSoftBackend() = default;
-};
-
-} // namespace soft
-} // namespace backend
-} // namespace contrib
-} // namespace nncc
-
-
-extern "C" Plugin *get_instance()
-{
-  return &CPPSoftBackend::getInstance();
-}
index 1dbff0a..2ad887f 100644 (file)
@@ -8,23 +8,22 @@ using namespace std;
 using namespace nncc::contrib;
 using namespace nncc::contrib::core::IR::model;
 
-#include "cpp_header_types.h"
-#include "cpp_operations.h"
-
 #include "param_constants.def"
 
-#include "param_constants.h"
-#include "eigen.h"
-#include "cpp_common_funcs.h"
-#include "cpp_add_bias.h"
-#include "cpp_capped_relu.h"
-#include "cpp_concat.h"
-#include "cpp_conv.h"
-#include "cpp_depthwise_conv.h"
-#include "cpp_fully_connected.h"
-#include "cpp_pool.h"
-#include "cpp_relu.h"
-#include "cpp_softmax.h"
+#include "cpp_header_types.generated.h"
+#include "cpp_operations.generated.h"
+#include "param_constants.generated.h"
+#include "eigen.generated.h"
+#include "cpp_common_funcs.generated.h"
+#include "cpp_add_bias.generated.h"
+#include "cpp_capped_relu.generated.h"
+#include "cpp_concat.generated.h"
+#include "cpp_conv.generated.h"
+#include "cpp_depthwise_conv.generated.h"
+#include "cpp_fully_connected.generated.h"
+#include "cpp_pool.generated.h"
+#include "cpp_relu.generated.h"
+#include "cpp_softmax.generated.h"
 
 namespace nncc
 {
@@ -289,3 +288,8 @@ void CPPCodeGenerator::materializeCode(ostream &out, const ModelAnalyzer &ma, co
 } // namespace contrib
 } // namespace nncc
 
+extern "C" nncc::contrib::plugin::Plugin *get_instance()
+{
+  static nncc::contrib::backend::soft::CPPCodeGenerator cppCodeGenerator;
+  return &cppCodeGenerator;
+}
diff --git a/contrib/nnc/plugin/soft_backend/soft_backend.cpp b/contrib/nnc/plugin/soft_backend/soft_backend.cpp
deleted file mode 100644 (file)
index 61e9288..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <map>
-#include <vector>
-#include <iostream>
-#include <cassert>
-
-#include "soft_backend.h"
-#include "support/PluginException.h"
-#include "core/modelIR/graph.h"
-
-#include "support/Debug.h"
-#define DEBUG_AREA "soft_backend"
-
-using namespace std;
-using namespace nncc::contrib;
-using namespace nncc::contrib::plugin;
-using namespace nncc::contrib::core::IR::model;
-using namespace nncc::contrib::backend::soft;
-
-namespace nncc
-{
-namespace contrib
-{
-namespace backend
-{
-namespace soft
-{
-
-void *BaseSoftBackend::execute(void *data)
-{
-  assert(data);
-  Graph *graph = static_cast<Graph*>(data);
-  generate(graph);
-  return data;
-}
-
-} // namespace soft
-} // namespace backend
-} // namespace contrib
-} // namespace nncc
diff --git a/contrib/nnc/plugin/soft_backend/soft_backend.h b/contrib/nnc/plugin/soft_backend/soft_backend.h
deleted file mode 100644 (file)
index 08a31ea..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-#ifndef _NNC_SOFT_BACKEND_PLUGIN_H_
-#define _NNC_SOFT_BACKEND_PLUGIN_H_
-
-#include "support/PluginInstance.h"
-#include "core/modelIR/graph.h"
-#include "base_generator.h"
-
-#include <string>
-#include <map>
-
-namespace nncc
-{
-namespace contrib
-{
-namespace backend
-{
-namespace soft
-{
-
-enum class OPT_ID;
-
-class BaseSoftBackend : public nncc::contrib::plugin::BackendPlugin
-{
-public:
-  BaseSoftBackend &operator=(const BaseSoftBackend &) = delete;
-
-  BaseSoftBackend(const BaseSoftBackend &) = delete;
-
-  void *execute(void *data) override;
-
-protected:
-  BaseSoftBackend() = default;
-
-  ~BaseSoftBackend() override = default;
-
-  virtual void generate(nncc::contrib::core::IR::model::Graph *g) = 0;
-};
-
-} // namespace soft
-} // namespace backend
-} // namespace contrib
-} // namespace nncc
-
-#endif //_NNC_SOFT_BACKEND_PLUGIN_H_
index ddf4c72..a38f396 100644 (file)
@@ -21,7 +21,7 @@
 #include "cpp_generator.h"
 
 // This header generated and contains array with test_main.def contents
-#include "test_main.h"
+#include "test_main.generated.h"
 
 using namespace std;
 
@@ -78,7 +78,7 @@ int main(int argc, const char *argv[])
   Graph g;
   fillGraph(g);
 
-  nncc::contrib::backend::soft::CPPCodeGenerator().generate(&g);
+  nncc::contrib::backend::soft::CPPCodeGenerator().execute(&g);
 
   string basePath = outputDir + "/" + artifactName;
 
index a67999c..e5415b7 100644 (file)
@@ -1,5 +1,4 @@
-set(SOFT_BACKEND_CPP_SOURCES ${NNC_SOFT_BACKEND_DIR}/cpp_backend.cpp
-                             ${NNC_SOFT_BACKEND_DIR}/cpp_generator.cpp)
+set(SOFT_BACKEND_CPP_SOURCES ${NNC_SOFT_BACKEND_DIR}/cpp_generator.cpp)
 
 file(GLOB_RECURSE SOFT_DEF_SOURCES "${NNC_SOFT_BACKEND_DIR}/*.def")
 file(GLOB_RECURSE TESTS "*.cpp")
index f06b968..443e720 100644 (file)
@@ -4,7 +4,7 @@
 #include <functional>
 #include <numeric>
 
-#include "cpp_header_types.def"
+#include "code_snippets/cpp_header_types.def"
 
 #include "gtest/gtest.h"
 
index 50baf69..78a604d 100644 (file)
@@ -4,23 +4,23 @@
 #include <functional>
 
 // artifact part
-#include "cpp_ops/eigen.def"
+#include "code_snippets/eigen.def"
 
-#include "cpp_ops/cpp_common_funcs.def"
+#include "code_snippets/cpp_common_funcs.def"
 
-#include "cpp_ops/cpp_add_bias.def"
-#include "cpp_ops/cpp_capped_relu.def"
-#include "cpp_ops/cpp_concat.def"
-#include "cpp_ops/cpp_conv.def"
-#include "cpp_ops/cpp_depthwise_conv.def"
-#include "cpp_ops/cpp_fully_connected.def"
-#include "cpp_ops/cpp_pool.def"
-#include "cpp_ops/cpp_relu.def"
-#include "cpp_ops/cpp_softmax.def"
+#include "code_snippets/cpp_add_bias.def"
+#include "code_snippets/cpp_capped_relu.def"
+#include "code_snippets/cpp_concat.def"
+#include "code_snippets/cpp_conv.def"
+#include "code_snippets/cpp_depthwise_conv.def"
+#include "code_snippets/cpp_fully_connected.def"
+#include "code_snippets/cpp_pool.def"
+#include "code_snippets/cpp_relu.def"
+#include "code_snippets/cpp_softmax.def"
 
 #include "param_constants.def"
-#include "cpp_header_types.def"
-#include "cpp_operations.def"
+#include "code_snippets/cpp_header_types.def"
+#include "code_snippets/cpp_operations.def"
 
 // soft backend part
 
index 005c756..f983e32 100644 (file)
@@ -85,7 +85,7 @@ TEST(Generator, check_generator_call)
   assert(!isFileExists(TEST_DIR) && "remove output dir");
   {
       CPPCodeGenerator gen;
-      gen.generate(&g);
+      gen.execute(&g);
   }
   checkOutputExists(BASE_NAME);
 
@@ -95,7 +95,7 @@ TEST(Generator, check_generator_call)
   deleteFile(BASE_NAME ".params");
   {
       CPPCodeGenerator gen;
-      gen.generate(&g);
+      gen.execute(&g);
   }
   checkOutputExists(BASE_NAME);
 
@@ -107,7 +107,7 @@ TEST(Generator, check_generator_call)
   assert(sBefore.st_size == 0);
   {
       CPPCodeGenerator gen;
-      gen.generate(&g);
+      gen.execute(&g);
   }
   res = stat(BASE_NAME ".h", &sAfter);
   assert(res == 0);
index 57b64a9..a993e0d 100644 (file)
@@ -67,7 +67,7 @@ int main(int argc, char *argv[]) {
   for (int i = 2; i < argc; i++) {
     std::string sourceFullFileName = argv[i];
     std::string filename = extractFileName(sourceFullFileName);
-    std::string outputFileName = OutPutDir + "/" + filename + ".h";
+    std::string outputFileName = OutPutDir + "/" + filename + ".generated.h";
 
     if (fileToArray(sourceFullFileName, outputFileName, filename) != 0)
       return -1;