From: Jihoon Lee Date: Fri, 2 Apr 2021 08:17:56 +0000 (+0900) Subject: [Compiler] Alternate GraphRepresentation X-Git-Tag: submit/tizen/20210407.103859~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1b29cb8d2241961d67c77c0070185ace1af9b1e6;p=platform%2Fcore%2Fml%2Fnntrainer.git [Compiler] Alternate GraphRepresentation 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 --- diff --git a/jni/Android.mk b/jni/Android.mk index 7a71e59..aca893e 100644 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -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 diff --git a/nntrainer/compiler/compiler.h b/nntrainer/compiler/compiler.h index 458a293..ac72acd 100644 --- a/nntrainer/compiler/compiler.h +++ b/nntrainer/compiler/compiler.h @@ -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 + * @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| * +-------+-----------+ @@ -46,21 +33,18 @@ * |ExecutableGraph| * +---------------+ * - * @see https://github.com/nnstreamer/nntrainer - * @author Jihoon Lee - * @bug No known bugs except for NYI items */ #ifndef __COMPILER_H__ #define __COMPILER_H__ -#include #include -namespace nntrainer { +#include -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 - compile(const GraphRepresentation &representation) = 0; + compile(std::shared_ptr 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 decompile(std::shared_ptr 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 interpreter_ = nullptr, - std::shared_ptr 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 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 compiler_, - std::shared_ptr graph) { - /** NYI! */ - } - - /** - * @brief compile a graph to a executable graph - * - * @return std::shared_ptr - */ - std::shared_ptr compile() { - /** NYI !*/ - return nullptr; - }; - - /** - * @brief decompile the graph to a representation - * - * @param graph graph to decompile - */ - void decompile(std::shared_ptr 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 interpreter_) { - interpreter = interpreter_; - } - - /** - * @brief Set the Graph Compiler object - * - * @param compiler_ compiler - */ - void setGraphCompiler(std::shared_ptr compiler_) { - compiler = compiler_; - } - -private: - std::shared_ptr interpreter; - std::shared_ptr compiler; -}; - } // namespace nntrainer #endif // __COMPILER_H__ diff --git a/nntrainer/compiler/interpreter.h b/nntrainer/compiler/interpreter.h new file mode 100644 index 0000000..3a7cc87 --- /dev/null +++ b/nntrainer/compiler/interpreter.h @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: Apache-2.0 +/** + * Copyright (C) 2021 Jihoon Lee + * + * @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 + * @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 +#include + +#include + +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 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 + deserialize(std::istream &in) = 0; +}; + +} // namespace nntrainer + +#endif /** __INTERPRETER_H__ */ diff --git a/nntrainer/compiler/meson.build b/nntrainer/compiler/meson.build index cd2ae62..f95d055 100644 --- a/nntrainer/compiler/meson.build +++ b/nntrainer/compiler/meson.build @@ -1,6 +1,4 @@ -compiler_sources = [ - 'compiler.cpp' -] +compiler_sources = [] compiler_headers = [] diff --git a/nntrainer/graph/network_graph.h b/nntrainer/graph/network_graph.h index 3f5e3d0..984de1d 100644 --- a/nntrainer/graph/network_graph.h +++ b/nntrainer/graph/network_graph.h @@ -23,6 +23,7 @@ #include #include +#include namespace nntrainer { diff --git a/test/meson.build b/test/meson.build index 225365a..4b81d88 100644 --- a/test/meson.build +++ b/test/meson.build @@ -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, diff --git a/test/unittest/compiler/meson.build b/test/unittest/compiler/meson.build index b49596a..e18196c 100644 --- a/test/unittest/compiler/meson.build +++ b/test/unittest/compiler/meson.build @@ -1,5 +1,6 @@ test_target = [ - 'unittest_compiler' + 'unittest_compiler', + 'unittest_interpreter' ] foreach target: test_target diff --git a/nntrainer/compiler/compiler.cpp b/test/unittest/compiler/unittest_interpreter.cpp similarity index 60% rename from nntrainer/compiler/compiler.cpp rename to test/unittest/compiler/unittest_interpreter.cpp index e0b3b2d..d10e9b3 100644 --- a/nntrainer/compiler/compiler.cpp +++ b/test/unittest/compiler/unittest_interpreter.cpp @@ -2,12 +2,14 @@ /** * Copyright (C) 2021 Jihoon Lee * - * @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 * @bug No known bugs except for NYI items */ -namespace nntrainer {} // namespace nntrainer +#include + +#include