[Compiler] Add Graph Representation
authorJihoon Lee <jhoon.it.lee@samsung.com>
Thu, 1 Apr 2021 10:36:08 +0000 (19:36 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Wed, 7 Apr 2021 01:34:17 +0000 (10:34 +0900)
This patch adds graph representation and it's interface

**Self evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test: [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Jihoon Lee <jhoon.it.lee@samsung.com>
nntrainer/compiler/compiler.cpp
nntrainer/compiler/compiler.h
test/unittest/compiler/unittest_compiler.cpp

index ac6c90b..e0b3b2d 100644 (file)
  * @bug No known bugs except for NYI items
  */
 
-#include <iostream>
-
-namespace nntrainer {
-
-void hello_world() { std::cout << "hello_world\n"; }
-
-} // namespace nntrainer
+namespace nntrainer {} // namespace nntrainer
index 6a16849..458a293 100644 (file)
@@ -7,7 +7,21 @@
  * @brief NNTrainer compiler that reads and generates executable graph
  * @details
  * Graph is convertible either to iostream, representation, executable by
- * appropriate compiler and interpreter
+ * appropriate compiler and interpreter.
+ * For example, if istream would be from a a.tflite file,
+ *
+ * GraphRepresentaion g;
+ * g.setInterpreter(tfliteInterpreter);
+ * g.setCompiler(nntrainerCPUCompiler);
+ * auto tflite_file = std::open('a.tflite');
+ * tflite_file >> g;
+ *
+ * ExecutableGraph eg = g.compile();
+ *
+ * g.setInterpreter(iniInterpreter);
+ * auto ini_file = std::open('a.ini');
+ * ini_file << g;
+ *
  *         +--------+
  *         |iostream|
  *         +--+-----+
@@ -36,6 +50,8 @@
  * @author Jihoon Lee <jhoon.it.lee@samsung.com>
  * @bug No known bugs except for NYI items
  */
+#ifndef __COMPILER_H__
+#define __COMPILER_H__
 
 #include <iostream>
 #include <memory>
@@ -97,12 +113,124 @@ public:
   virtual GraphRepresentation deserialize(std::istream &in) = 0;
 };
 
-class GraphInterpreter;
+/**
+ * @brief Pure Virtual class for ExecutableGraph
+ *
+ * @note this class is an interface to executable graph as actual graph would be
+ * different framework by framework
+ *
+ */
+class ExecutableGraph {
+  /// NYI!
+};
 
 /**
- * @brief a simple scaffoliding function
+ * @brief Graph Representation
+ *
+ * @note this class must have the graph representation of the most expanded
+ * version as far as nntrainer supports.
+ *
+ * GraphRepresentation contains the operation configurations, connections and
+ * buffer(this might not...)
  *
  */
-void hello_world();
+class GraphRepresentation {
+public:
+  /**
+   * @brief Construct a new Graph Representation object
+   *
+   * @param interpreter_ interpreter to use
+   * @param compiler_ compiler to use
+   */
+  GraphRepresentation(std::shared_ptr<GraphInterpreter> interpreter_ = nullptr,
+                      std::shared_ptr<GraphCompiler> compiler_ = nullptr) :
+    interpreter(interpreter_),
+    compiler(compiler_) {}
+
+  /**
+   * @brief Construct a new Graph Representation object from ifstream
+   *
+   * @param interpreter_ interpreter to use
+   * @param istream istream to convert to the representation
+   */
+  GraphRepresentation(std::shared_ptr<GraphInterpreter> interpreter_,
+                      std::istream &in) {
+    /** NYI! */
+  }
+
+  /**
+   * @brief Construct a new Graph Representation object from a executable graph
+   *
+   * @param compiler_ compiler to use
+   * @param graph graph that will be decompiled (not owing)
+   */
+  GraphRepresentation(std::shared_ptr<GraphCompiler> compiler_,
+                      std::shared_ptr<ExecutableGraph> graph) {
+    /** NYI! */
+  }
+
+  /**
+   * @brief compile a graph to a executable graph
+   *
+   * @return std::shared_ptr<ExecutableGraph>
+   */
+  std::shared_ptr<ExecutableGraph> compile() {
+    /** NYI !*/
+    return nullptr;
+  };
+
+  /**
+   * @brief decompile the graph to a representation
+   *
+   * @param graph graph to decompile
+   */
+  void decompile(std::shared_ptr<ExecutableGraph> graph){/** NYI! */};
+
+  /**
+   * @brief out operator to a file
+   *
+   * @param out outstream
+   * @return std::ostream& outstream
+   */
+  std::ostream &operator<<(std::ostream &out) {
+    interpreter->serialize(*this, out);
+    return out;
+  }
+
+  /**
+   * @brief in stream
+   *
+   * @param input
+   * @return std::istream&
+   */
+  std::istream &operator>>(std::istream &input) {
+    interpreter->deserialize(input);
+    return input;
+  }
+
+  /**
+   * @brief Set the Graph Interpreter object
+   *
+   * @param interpreter_ interpreter
+   */
+  void setGraphInterpreter(std::shared_ptr<GraphInterpreter> interpreter_) {
+    interpreter = interpreter_;
+  }
+
+  /**
+   * @brief Set the Graph Compiler object
+   *
+   * @param compiler_ compiler
+   */
+  void setGraphCompiler(std::shared_ptr<GraphCompiler> compiler_) {
+    compiler = compiler_;
+  }
+
+private:
+  std::shared_ptr<GraphInterpreter> interpreter;
+  std::shared_ptr<GraphCompiler> compiler;
+};
 
 } // namespace nntrainer
+
+#endif // __COMPILER_H__
index ff65454..cddaa03 100644 (file)
@@ -13,5 +13,3 @@
 #include <gtest/gtest.h>
 
 #include <compiler.h>
-
-TEST(scaffoldingTest, sampleTest) { EXPECT_NO_THROW(nntrainer::hello_world()); }