[Realizer] Implement previous input realizer
authorJihoon Lee <jhoon.it.lee@samsung.com>
Thu, 18 Nov 2021 07:02:30 +0000 (16:02 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 29 Nov 2021 04:05:59 +0000 (13:05 +0900)
This patch implement previous input realizer.

**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/previous_input_realizer.cpp
nntrainer/compiler/previous_input_realizer.h
test/unittest/compiler/unittest_realizer.cpp

index 03ba1b0..14cc205 100644 (file)
@@ -9,22 +9,64 @@
  * @author Jihoon Lee <jhoon.it.lee@samsung.com>
  * @bug No known bugs except for NYI items
  */
+#include <algorithm>
 #include <compiler_fwd.h>
 #include <memory>
+#include <stdexcept>
 #include <vector>
 
+#include <layer_node.h>
+#include <nntrainer_log.h>
 #include <previous_input_realizer.h>
 
 namespace nntrainer {
 
 PreviousInputRealizer::PreviousInputRealizer(
-  const std::vector<std::string> &identified_input) {}
+  const std::vector<std::string> &identified_inputs_) :
+  identified_inputs(identified_inputs_) {}
 
 PreviousInputRealizer::~PreviousInputRealizer() {}
 
 GraphRepresentation
 PreviousInputRealizer::realize(const GraphRepresentation &reference) {
-  return GraphRepresentation();
+  GraphRepresentation processed(reference.begin(), reference.end());
+
+  /**
+   * @brief for node has input connection, below function determines if the node
+   * should be input node or add input_layers from previous layer
+   *
+   */
+  auto is_actually_an_input_node = [this](const LayerNode &node) {
+    return node.hasInputShapeProperty() or
+           std::any_of(identified_inputs.begin(), identified_inputs.end(),
+                       [&node](auto &name) { return node.getName() == name; });
+  };
+
+  for (auto iter = processed.begin(); iter != processed.end(); ++iter) {
+    auto &node = *iter;
+    if (node->getNumInputConnections() != 0) {
+      continue;
+    }
+
+    if (is_actually_an_input_node(*node)) {
+      continue;
+    }
+
+    NNTR_THROW_IF(iter == processed.begin(), std::invalid_argument)
+      << "First node must be identified as an input if it is qualified to be "
+         "input, name: "
+      << node->getName();
+
+    auto &prev_node = *(iter - 1);
+    ml_logi(
+      "%s is identified as a non-input node and default input layer(%s) is "
+      "being set ",
+      node->getName().c_str(), prev_node->getName().c_str());
+
+    node->setInputLayers({prev_node->getName()});
+  }
+
+  return processed;
 }
 
 } // namespace nntrainer
index 1fdd528..15abc11 100644 (file)
@@ -30,10 +30,10 @@ public:
   /**
    * @brief Construct a new Previous Input Realizer object
    *
-   * @param identified_input node that is identified as an input, this must not
+   * @param identified_inputs node that is identified as an input, this must not
    * connect to other nodes automatically
    */
-  PreviousInputRealizer(const std::vector<std::string> &identified_input);
+  PreviousInputRealizer(const std::vector<std::string> &identified_inputs);
 
   /**
    * @brief Destroy the Graph Realizer object
@@ -46,6 +46,9 @@ public:
    *
    */
   GraphRepresentation realize(const GraphRepresentation &reference) override;
+
+private:
+  std::vector<std::string> identified_inputs; /**< inputs are identified */
 };
 
 } // namespace nntrainer
index a1093c1..96bca86 100644 (file)
@@ -231,7 +231,7 @@ TEST(InputRealizer, remap_p) {
   realizeAndEqual(r, before, after);
 }
 
-TEST(PreviousInputRealizer, DISABLED_previous_p) {
+TEST(PreviousInputRealizer, previous_p) {
   { /// realization without identifying custom input
     std::vector<LayerRepresentation> before = {
       {"fully_connected", {"name=fc1", "input_shape=1"}}, // model input
@@ -267,7 +267,7 @@ TEST(PreviousInputRealizer, DISABLED_previous_p) {
   }
 }
 
-TEST(PreviousInputRealizer, DISABLED_user_not_identifying_first_input_n) {
+TEST(PreviousInputRealizer, user_not_identifying_first_input_n) {
   /// realization without identifying custom input
   std::vector<LayerRepresentation> before = {
     {"fully_connected", {"name=fc1"}}, // this should be model input but