From 501ef7718dfd1953da1bcd116eda28dcdb8bccf4 Mon Sep 17 00:00:00 2001 From: Jihoon Lee Date: Thu, 8 Apr 2021 20:08:31 +0900 Subject: [PATCH] [Ini/Serialize] Implement logic to serialize This patch implements general concept of serializing a node. Below is the plan to implement this logic 0. Implement logic to serialize [*] 1. Add export api 2. Add exporter 3. Add export test 4. Implement properties 5. combine with export 6. Add extra test **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: Jihoon Lee --- nntrainer/compiler/ini_interpreter.cpp | 23 +++++++++++++++++++---- nntrainer/utils/ini_wrapper.cpp | 6 +++++- nntrainer/utils/ini_wrapper.h | 8 ++++++++ test/unittest/compiler/unittest_interpreter.cpp | 22 ++++++++++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/nntrainer/compiler/ini_interpreter.cpp b/nntrainer/compiler/ini_interpreter.cpp index a883320..17d9187 100644 --- a/nntrainer/compiler/ini_interpreter.cpp +++ b/nntrainer/compiler/ini_interpreter.cpp @@ -13,10 +13,12 @@ */ #include +#include #include #include #include +#include #include #include #include @@ -46,9 +48,7 @@ namespace { /** @todo: * 1. deprecate tag dispatching along with #1072 * 2. deprecate getMergeableGraph (extendGraph should accept graph itself) - */ - -/** + * * @brief Plain Layer tag */ class PlainLayer {}; @@ -266,7 +266,22 @@ getMergeableGraph(std::shared_ptr graph, void IniGraphInterpreter::serialize( std::shared_ptr representation, - const std::string &out) {} + const std::string &out) { + + std::vector sections; + for (const auto &ln : representation->getSorted()) { + const auto &layer = ln.layer; + + IniSection s(layer->getName()); + s.setEntry("type", layer->getType()); + + /// @todo: implement export a property + std::cout << ln.layer->getName() << std::endl; + } + + auto ini = IniWrapper(out, sections); + ini.save_ini(); +} std::shared_ptr IniGraphInterpreter::deserialize(const std::string &in) { diff --git a/nntrainer/utils/ini_wrapper.cpp b/nntrainer/utils/ini_wrapper.cpp index 58d90be..519502b 100644 --- a/nntrainer/utils/ini_wrapper.cpp +++ b/nntrainer/utils/ini_wrapper.cpp @@ -49,6 +49,10 @@ void IniSection::setEntry(const std::map &entry) { } } +void IniSection::setEntry(const std::string &key, const std::string &value) { + entry[key] = value; +} + void IniSection::setEntry(const std::string &entry_str) { // setting property separated by "|" std::regex words_regex("[^|]+"); @@ -70,7 +74,7 @@ void IniSection::setEntry(const std::string &entry_str) { NNTR_THROW_IF(status != ML_ERROR_NONE, std::invalid_argument) << "getKeyValue Failed"; - entry[key] = value; + setEntry(key, value); } } diff --git a/nntrainer/utils/ini_wrapper.h b/nntrainer/utils/ini_wrapper.h index e772956..2dda96d 100644 --- a/nntrainer/utils/ini_wrapper.h +++ b/nntrainer/utils/ini_wrapper.h @@ -155,6 +155,14 @@ public: */ std::string getName() const { return section_name; } + /** + * @brief Set the Entry object by key and value + * + * @param key key to update + * @param value value to be added + */ + void setEntry(const std::string &key, const std::string &value); + private: /** * @brief Set the Entry diff --git a/test/unittest/compiler/unittest_interpreter.cpp b/test/unittest/compiler/unittest_interpreter.cpp index 196f7e4..790434d 100644 --- a/test/unittest/compiler/unittest_interpreter.cpp +++ b/test/unittest/compiler/unittest_interpreter.cpp @@ -110,6 +110,28 @@ TEST_P(nntrainerInterpreterTest, graphEqual) { } } +/** + * @brief graph serialize after deserialize, compare if they are the same + * + */ +TEST_P(nntrainerInterpreterTest, graphSerializeAfterDeserialize) { + auto g = interpreter->deserialize(file_path); + + auto out_file_path = file_path + ".out"; + + /// @todo: change this to something like graph::finalize + int status = g->compile(nntrainer::LossType::LOSS_NONE); + EXPECT_EQ(status, ML_ERROR_NONE); + interpreter->serialize(g, out_file_path); + + // auto new_g = interpreter->deserialize(out_file_path); + + /// @todo: enable this + /// check if graph is the same + // EXPECT_EQ(*g, *new_g); + // EXPECT_EQ(remove(out_file_path.c_str()), 0); +} + auto fc0 = LayerReprentation("fully_connected", {"name=fc0", "unit=1", "input_shape=1:1:100"}); -- 2.7.4