[IniWrapper] Add factory method
authorJihoon Lee <jhoon.it.lee@samsung.com>
Wed, 1 Sep 2021 04:27:39 +0000 (13:27 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 7 Sep 2021 11:27:12 +0000 (20:27 +0900)
This patch adds a function to create ini section directly from
exportable which implements .exportTo to reduce overlapping codes

**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/ini_interpreter.cpp
nntrainer/models/neuralnet.cpp
nntrainer/models/neuralnet.h
nntrainer/utils/ini_wrapper.h

index 1d46d69..5377a1b 100644 (file)
@@ -21,7 +21,6 @@
 #include <layer_node.h>
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
-#include <node_exporter.h>
 #include <parse_util.h>
 #include <time_dist.h>
 #include <util_func.h>
@@ -260,19 +259,9 @@ void IniGraphInterpreter::serialize(const GraphRepresentation &representation,
        iter++) {
     const auto &ln = *iter;
 
-    IniSection s(ln->getName());
+    IniSection s = IniSection::FromExportable(ln->getName(), *ln);
     s.setEntry("type", ln->getType());
 
-    Exporter e;
-    ln->exportTo(e, ExportMethods::METHOD_STRINGVECTOR);
-
-    const auto key_val_pairs =
-      e.getResult<ExportMethods::METHOD_STRINGVECTOR>();
-
-    for (const auto &pair : *key_val_pairs) {
-      s.setEntry(pair.first, pair.second);
-    }
-
     sections.push_back(s);
   }
 
index f7706b5..d4e4b72 100644 (file)
@@ -420,18 +420,9 @@ void NeuralNetwork::saveModelIni(const std::string &file_path) {
        "permitted, path: "
     << file_path;
 
-  IniSection model_section("model");
+  IniSection model_section = IniSection::FromExportable("model", *this);
   model_section.setEntry("type", "NeuralNetwork");
 
-  Exporter e;
-  e.saveResult(model_props, ExportMethods::METHOD_STRINGVECTOR, this);
-  e.saveResult(model_flex_props, ExportMethods::METHOD_STRINGVECTOR, this);
-
-  const auto key_val_pairs = e.getResult<ExportMethods::METHOD_STRINGVECTOR>();
-  for (const auto &pair : *key_val_pairs) {
-    model_section.setEntry(pair.first, pair.second);
-  }
-
   IniWrapper wrapper("model_saver", {model_section});
   wrapper.save_ini(file_path);
 
@@ -836,6 +827,12 @@ void NeuralNetwork::printPreset(std::ostream &out, unsigned int preset) {
   print(out, flags, layer_preset);
 }
 
+void NeuralNetwork::exportTo(Exporter &exporter,
+                             const ExportMethods &method) const {
+  exporter.saveResult(model_props, method, this);
+  exporter.saveResult(model_flex_props, method, this);
+}
+
 void NeuralNetwork::print(std::ostream &out, unsigned int flags,
                           LayerNode::PrintPreset layerPrintPreset) {
   if (flags & PRINT_INST_INFO) {
index 0d41ba4..47946e7 100644 (file)
@@ -53,6 +53,9 @@ enum class DatasetModeType;
 
 namespace nntrainer {
 
+class Exporter;
+enum class ExportMethods;
+
 /**
  * @brief     Enumeration of Network Type
  */
@@ -354,6 +357,15 @@ public:
   int getLayer(const char *name, NodeType *layer);
 
   /**
+   * @brief this function helps exporting the layer in a predefined format,
+   * while workarounding issue caused by templated function type eraser
+   *
+   * @param     exporter exporter that conatins exporting logic
+   * @param     method enum value to identify how it should be exported to
+   */
+  void exportTo(Exporter &exporter, const ExportMethods &method) const;
+
+  /**
    * @brief     get input dimension of neural network
    * @retval std::vector<TensorDim> input dimension
    */
index d12fa3c..e4557f6 100644 (file)
 #include <iostream>
 #include <map>
 #include <string>
-
 #include <vector>
 
+#include <node_exporter.h>
+
 #ifndef __INI_WRAPPER_H__
 #define __INI_WRAPPER_H__
 
@@ -76,6 +77,32 @@ public:
   IniSection() : section_name(""), entry{} {};
 
   /**
+   * @brief Construct a new Ini Section object which implements object::exportTo
+   *
+   * @tparam Exportable object with member object::exportTo
+   * @param section_name section name
+   * @param exportable exportable object
+   */
+  template <typename Exportable>
+  static IniSection FromExportable(const std::string &section_name,
+                                   const Exportable &exportable) {
+    IniSection s(section_name);
+    Exporter e;
+    exportable.exportTo(e, ExportMethods::METHOD_STRINGVECTOR);
+    const auto key_val_pairs =
+      e.getResult<ExportMethods::METHOD_STRINGVECTOR>();
+
+    if (!key_val_pairs) {
+      throw std::invalid_argument("returend pairs are nullptr!");
+    }
+
+    for (const auto &pair : *key_val_pairs) {
+      s.setEntry(pair.first, pair.second);
+    }
+    return s;
+  }
+
+  /**
    * @brief Default destructor for the Ini Section object
    *
    */