Test compilation of artifact (#1119)
authorEfimov Alexander/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Tue, 4 Sep 2018 15:06:23 +0000 (18:06 +0300)
committerРоман Михайлович Русяев/AI Tools Lab /SRR/Staff Engineer/삼성전자 <r.rusyaev@samsung.com>
Tue, 4 Sep 2018 15:06:23 +0000 (18:06 +0300)
Adds test to check that backend is able to produce compilable files

Signed-off-by: Efimov Alexander <a.efimov@samsung.com>
contrib/nnc/tests/CMakeLists.txt
contrib/nnc/tests/soft_backend/CMakeLists.txt [new file with mode: 0644]
contrib/nnc/tests/soft_backend/compile_cpp.cpp [new file with mode: 0644]
contrib/nnc/tests/soft_backend/test_main.def [new file with mode: 0644]

index d39d0f0..60bc260 100644 (file)
@@ -1,2 +1,3 @@
 add_subdirectory(import)
+add_subdirectory(soft_backend)
 add_subdirectory(interpreter)
diff --git a/contrib/nnc/tests/soft_backend/CMakeLists.txt b/contrib/nnc/tests/soft_backend/CMakeLists.txt
new file mode 100644 (file)
index 0000000..00c7e10
--- /dev/null
@@ -0,0 +1,9 @@
+file(GLOB_RECURSE SOFT_TEST_DEF_SOURCES *.def)
+
+make_generated_sources("${SOFT_TEST_DEF_SOURCES}" ${CMAKE_CURRENT_BINARY_DIR} SOFT_TEST_GENERATED_SOURCES)
+
+add_executable(system_soft_backend_cpp_compile compile_cpp.cpp ${OPTIONS_SRC} ${SOFT_TEST_GENERATED_SOURCES})
+target_link_libraries(system_soft_backend_cpp_compile PRIVATE nnc_support soft_backend_cpp nnc_core)
+target_include_directories(system_soft_backend_cpp_compile PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${NNC_SOFT_BACKEND_DIR})
+
+add_test(NAME system_test_soft_backend_cpp_compile COMMAND system_soft_backend_cpp_compile -d test_output --target=x86-c++ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
diff --git a/contrib/nnc/tests/soft_backend/compile_cpp.cpp b/contrib/nnc/tests/soft_backend/compile_cpp.cpp
new file mode 100644 (file)
index 0000000..ddf4c72
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * This is simple tests to check that generator is running properly and creates compilable artifact
+ * This test is not intended to check correctness of generated artifact
+ */
+
+#include <iostream>
+#include <fstream>
+#include <string>
+
+#include <stdlib.h>
+
+#include "support/CommandLine.h"
+#include "option/Options.h"
+
+#include "core/modelIR/graph.h"
+#include "core/modelIR/Shape.h"
+#include "core/modelIR/operations/relu_op.h"
+#include "core/modelIR/operations/variable_op.h"
+
+#include "core/modelIR/ShapeInference.h"
+#include "cpp_generator.h"
+
+// This header generated and contains array with test_main.def contents
+#include "test_main.h"
+
+using namespace std;
+
+using namespace nncc::contrib;
+using namespace nncc::contrib::core::data;
+using namespace nncc::contrib::core::IR::model;
+
+// Creates simple graph with input and output
+void fillGraph(Graph &g)
+{
+  INode *opNode = g.create<ops::ReluOp>("out");
+  Shape inputShape{1, 2, 3};
+
+  INode *inputNode = g.create<ops::VariableOp>("in");
+
+  opNode->connectInputTo(0, inputNode->getOutput(0));
+
+  inputNode->getOperation()->setOutputShape(0, inputShape);
+
+  g.markOutput(opNode);
+
+  ShapeInference shapeInferencer;
+  g.accept(&shapeInferencer);
+}
+
+static void checkFileExists(const string &path)
+{
+  ifstream f(path);
+  if (!f.good())
+  {
+    cerr << "file " << path << " not created\n";
+    exit(1);
+  }
+}
+
+static void createMain(const string &path, const string &headerPath)
+{
+  ofstream out(path);
+  if (!out.good())
+  {
+    cerr << "Main file " << path << " not created\n";
+    exit(1);
+  }
+  out << "#include \"" << headerPath << "\"\n";
+  out.write(test_main, sizeof(test_main));
+}
+
+int main(int argc, const char *argv[])
+{
+  clopt::CommandLine::getParser()->parseCommandLine(argc, argv, false);
+  std::string outputDir = clopt::artifactDir;
+  std::string artifactName = clopt::artifactName;
+
+  Graph g;
+  fillGraph(g);
+
+  nncc::contrib::backend::soft::CPPCodeGenerator().generate(&g);
+
+  string basePath = outputDir + "/" + artifactName;
+
+  string codePath = basePath + ".cpp";
+  string headerPath = basePath + ".h";
+  string mainPath = basePath + "_main.cpp";
+
+  checkFileExists(codePath);
+  checkFileExists(headerPath);
+  checkFileExists(basePath + ".params");
+
+  createMain(mainPath, artifactName + ".h");
+
+  string targetCompiler = "g++ -Wall --std=c++11";
+
+  string compilerCommand = targetCompiler + " -I" + outputDir + " " + mainPath + " " + codePath;
+
+  // call compiler
+  int res = system(compilerCommand.c_str());
+
+  if (res == -1)
+  {
+    cerr << "failed to call compiler\n";
+    return 2;
+  }
+  if (res != 0)
+  {
+    cerr << "compiler did not succeed with error code " << res << ": " << compilerCommand << "\n";
+    return 3;
+  }
+  return 0;
+}
diff --git a/contrib/nnc/tests/soft_backend/test_main.def b/contrib/nnc/tests/soft_backend/test_main.def
new file mode 100644 (file)
index 0000000..75d219f
--- /dev/null
@@ -0,0 +1,10 @@
+int main()
+{
+  Shape s{1, 2, 3};
+  Tensor in_t(s);
+  NNModel model("nnmodel.params");
+  model.set_in(in_t);
+  model.doInference();
+  std::shared_ptr<Tensor> out_t = model.get_out();
+  return 0;
+}
\ No newline at end of file