[Compiler] Alternate GraphRepresentation
authorJihoon Lee <jhoon.it.lee@samsung.com>
Fri, 2 Apr 2021 08:17:56 +0000 (17:17 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Wed, 7 Apr 2021 01:34:17 +0000 (10:34 +0900)
This patch alternates graph representation and move interpreter to a new
file

v2.
Also change documentation setup

**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>
jni/Android.mk
nntrainer/compiler/compiler.h
nntrainer/compiler/interpreter.h [new file with mode: 0644]
nntrainer/compiler/meson.build
nntrainer/graph/network_graph.h
test/meson.build
test/unittest/compiler/meson.build
test/unittest/compiler/unittest_interpreter.cpp [moved from nntrainer/compiler/compiler.cpp with 60% similarity]

index 7a71e59..aca893e 100644 (file)
@@ -126,7 +126,6 @@ NNTRAINER_SRCS := $(NNTRAINER_ROOT)/nntrainer/models/neuralnet.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/utils/util_func.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/utils/parse_util.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/utils/profiler.cpp \
-                  $(NNTRAINER_ROOT)/nntrainer/compiler/compiler.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/app_context.cpp
 
 # Add tflite backbone building
index 458a293..ac72acd 100644 (file)
@@ -4,34 +4,21 @@
  *
  * @file compiler.h
  * @date 01 April 2021
- * @brief NNTrainer compiler that reads and generates executable graph
+ * @brief NNTrainer compiler that reads to generate optimized graph
+ * @see        https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug No known bugs except for NYI items
  * @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;
+ * GraphCompiler * compiler = new NNTrainerCPUCompiler;
  *
- * ExecutableGraph eg = g.compile();
+ * ExecutableGraph eg = compiler->compile(g);
  *
- * g.setInterpreter(iniInterpreter);
- * auto ini_file = std::open('a.ini');
- * ini_file << g;
  *
- *         +--------+
- *         |iostream|
- *         +--+-----+
- *            |  ^
- * operator<< |  |
- *            |  |
- *        (Interpreter)
- *            |  |
- *            |  | operator>>
- *            |  |
  *    +-------+--+--------+
  *    |GraphRepresentation|
  *    +-------+-----------+
  *      |ExecutableGraph|
  *      +---------------+
  *
- * @see        https://github.com/nnstreamer/nntrainer
- * @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>
 
-namespace nntrainer {
+#include <network_graph.h>
 
-class ExecutableGraph;
+namespace nntrainer {
 
-class GraphRepresentation;
+using GraphRepresentation = NetworkGraph;
+using ExecutableGraph = NetworkGraph;
 
 /**
  * @brief Pure virtual class for the Graph Compiler
@@ -71,12 +55,14 @@ public:
   virtual ~GraphCompiler() {}
   /**
    * @brief serialize graph to a file stream
+   * @todo consider adding delegates argument here when implementing it for
+   * real.
    *
    * @param representation graph representation
    * @param file ifstream to serialize graph
    */
   virtual std::shared_ptr<ExecutableGraph>
-  compile(const GraphRepresentation &representation) = 0;
+  compile(std::shared_ptr<const GraphRepresentation> representation) = 0;
 
   /**
    * @brief deserialize graph from a file stream
@@ -84,153 +70,10 @@ public:
    * @param executable executable graph
    * @return GraphRepresentation graph representation
    */
-  virtual GraphRepresentation
+  virtual std::shared_ptr<GraphRepresentation>
   decompile(std::shared_ptr<ExecutableGraph> executable) = 0;
 };
 
-/**
- * @brief Pure virtual class for the Graph Interpreter
- *
- */
-class GraphInterpreter {
-public:
-  virtual ~GraphInterpreter() {}
-  /**
-   * @brief serialize graph to a stream
-   *
-   * @param representation graph representation
-   * @param out outstream to serialize graph
-   */
-  virtual void serialize(const GraphRepresentation &representation,
-                         std::ostream &out) = 0;
-
-  /**
-   * @brief deserialize graph from a stream
-   *
-   * @param in in stream to deserialize
-   * @return GraphRepresentation graph representation
-   */
-  virtual GraphRepresentation deserialize(std::istream &in) = 0;
-};
-
-/**
- * @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 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...)
- *
- */
-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__
diff --git a/nntrainer/compiler/interpreter.h b/nntrainer/compiler/interpreter.h
new file mode 100644 (file)
index 0000000..3a7cc87
--- /dev/null
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file interpreter.h
+ * @date 01 April 2021
+ * @brief NNTrainer interpreter that reads and generates a graphRepresentation
+ from a file
+ * @see        https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug No known bugs except for NYI items
+ * @details
+ * Graph is convertible either from a file, representation by a appropriate
+ interpreter
+ * For example, if istream would be from a a.tflite file,
+ *
+ * GraphRepresentaion g;
+ * GraphInterpreter * interpreter = new TfliteInterpreter;
+ *
+ * std::ifstream f = std::open("a.tflite");
+ * g = interpreter->serialize(f);
+ *
+ *         +--------+
+ *         |iostream|
+ *         +--+--+--+
+ *            ^  |
+ * serialize()|  |
+ *            |  |
+ *        (Interpreter)
+ *            |  |
+ *            |  | deserialize()
+ *            |  v
+ *    +-------+--+--------+
+ *    |GraphRepresentation|
+ *    +-------+-----------+
+ *
+ */
+#ifndef __INTERPRETER_H__
+#define __INTERPRETER_H__
+
+#include <iostream>
+#include <memory>
+
+#include <network_graph.h>
+
+namespace nntrainer {
+
+using GraphRepresentation = NetworkGraph;
+
+/**
+ * @brief Pure virtual class for the Graph Interpreter
+ *
+ */
+class GraphInterpreter {
+public:
+  virtual ~GraphInterpreter() {}
+  /**
+   * @brief serialize graph to a stream
+   *
+   * @param representation graph representation
+   * @param out outstream to serialize graph
+   */
+  virtual void
+  serialize(std::shared_ptr<const GraphRepresentation> representation,
+            std::ostream &out) = 0;
+
+  /**
+   * @brief deserialize graph from a stream
+   *
+   * @param in in stream to deserialize
+   * @return GraphRepresentation graph representation
+   */
+  virtual std::shared_ptr<GraphRepresentation>
+  deserialize(std::istream &in) = 0;
+};
+
+} // namespace nntrainer
+
+#endif /** __INTERPRETER_H__ */
index cd2ae62..f95d055 100644 (file)
@@ -1,6 +1,4 @@
-compiler_sources = [
-  'compiler.cpp'
-]
+compiler_sources = []
 
 compiler_headers = []
 
index 3f5e3d0..984de1d 100644 (file)
@@ -23,6 +23,7 @@
 #include <vector>
 
 #include <layer_internal.h>
+#include <loss_layer.h>
 
 namespace nntrainer {
 
index 225365a..4b81d88 100644 (file)
@@ -22,6 +22,7 @@ nntrainer_test_deps = [
   gtest_dep
 ]
 
+# this is depedency is for the gtest with main included
 nntrainer_test_main_deps = [
   nntrainer_dep,
   nntrainer_testutil_dep,
index b49596a..e18196c 100644 (file)
@@ -1,5 +1,6 @@
 test_target = [
-  'unittest_compiler'
+  'unittest_compiler',
+  'unittest_interpreter'
 ]
 
 foreach target: test_target
similarity index 60%
rename from nntrainer/compiler/compiler.cpp
rename to test/unittest/compiler/unittest_interpreter.cpp
index e0b3b2d..d10e9b3 100644 (file)
@@ -2,12 +2,14 @@
 /**
  * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
  *
- * @file compiler.cpp
- * @date 01 April 2021
- * @brief NNTrainer compiler that reads and generates excutable graph
+ * @file unittest_interpreter.cpp
+ * @date 02 April 2021
+ * @brief interpreter test
  * @see        https://github.com/nnstreamer/nntrainer
  * @author Jihoon Lee <jhoon.it.lee@samsung.com>
  * @bug No known bugs except for NYI items
  */
 
-namespace nntrainer {} // namespace nntrainer
+#include <gtest/gtest.h>
+
+#include <interpreter.h>