[Realizer] Implement activation realizer
authorJihoon Lee <jhoon.it.lee@samsung.com>
Tue, 23 Nov 2021 03:58:50 +0000 (12:58 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Wed, 1 Dec 2021 02:39:57 +0000 (11:39 +0900)
This patch implement activation 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>
jni/Android.mk
nntrainer/compiler/activation_realizer.cpp [new file with mode: 0644]
nntrainer/compiler/activation_realizer.h [new file with mode: 0644]
nntrainer/compiler/flatten_realizer.cpp
nntrainer/compiler/meson.build

index e1c4c0e..43a5328 100644 (file)
@@ -196,6 +196,7 @@ NNTRAINER_SRCS := $(NNTRAINER_ROOT)/nntrainer/models/neuralnet.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/utils/base_properties.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/compiler/ini_interpreter.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/compiler/flatten_realizer.cpp \
+                  $(NNTRAINER_ROOT)/nntrainer/compiler/activation_realizer.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/compiler/recurrent_realizer.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/compiler/previous_input_realizer.cpp \
                   $(NNTRAINER_ROOT)/nntrainer/compiler/multiout_realizer.cpp \
diff --git a/nntrainer/compiler/activation_realizer.cpp b/nntrainer/compiler/activation_realizer.cpp
new file mode 100644 (file)
index 0000000..d7304b8
--- /dev/null
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file activation_realizer.cpp
+ * @date 23 November 2021
+ * @brief NNTrainer graph realizer which realizes activation!=none to actual
+ * activation node
+ * @see        https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+#include "nntrainer_error.h"
+#include <activation_realizer.h>
+
+#include <activation_layer.h>
+#include <layer_node.h>
+#include <stdexcept>
+
+namespace nntrainer {
+
+ActivationRealizer::~ActivationRealizer() {}
+
+GraphRepresentation
+ActivationRealizer::realize(const GraphRepresentation &reference) {
+  GraphRepresentation processed;
+  processed.reserve(reference.size());
+
+  for (auto &node : reference) {
+    processed.push_back(node);
+    if (node->getType() == ActivationLayer::type) {
+      /// realizing activation type not allowed because there is no good way to
+      /// tell between 1. it is activation layer, 2. activation layer wants
+      /// realization for now. later, we should have relu, sigmoid kind as layer
+      /// type to resolve this matter, this is checked
+      /// node->getActivationToBeRealized() but explicitly stated in order to
+      /// make it robust, plus we might want to change the behavior
+      continue;
+    }
+
+    if (auto act = node->getActivationToBeRealized();
+        act != ActivationType::ACT_NONE) {
+      NNTR_THROW_IF(act == ActivationType::ACT_UNKNOWN, std::invalid_argument)
+        << "unknown activation type for layer: " << node->getName();
+
+      auto layer_name = node->getName();
+      props::Activation act_prop;
+      act_prop.set(act);
+      node->setProperty({"activation=none"});
+      node->setProperty({"name=" + layer_name + "/activation_realized"});
+
+      auto act_node =
+        createLayerNode("activation", {"name=" + layer_name,
+                                       "activation=" + to_string(act_prop)});
+      act_node->setProperty({"input_layers=" + node->getName()});
+      processed.push_back(std::move(act_node));
+    }
+  }
+
+  return processed;
+}
+} // namespace nntrainer
diff --git a/nntrainer/compiler/activation_realizer.h b/nntrainer/compiler/activation_realizer.h
new file mode 100644 (file)
index 0000000..b49b53d
--- /dev/null
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
+ *
+ * @file activation_realizer.h
+ * @date 23 November 2021
+ * @brief NNTrainer graph realizer which realizes activation!=none to actual
+ * activation node
+ * @see        https://github.com/nnstreamer/nntrainer
+ * @author Jihoon Lee <jhoon.it.lee@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+#ifndef __ACTIVATION_REALIZER_H__
+#define __ACTIVATION_REALIZER_H__
+
+#include <memory>
+#include <vector>
+
+#include <realizer.h>
+
+namespace nntrainer {
+
+/**
+ * @brief Graph realizer which realizes activation
+ *
+ */
+class ActivationRealizer final : public GraphRealizer {
+public:
+  /**
+   * @brief Destroy the Graph Realizer object
+   *
+   */
+  ~ActivationRealizer();
+
+  /**
+   * @brief graph realizer creates a new graph based on the reference
+   *
+   */
+  GraphRepresentation realize(const GraphRepresentation &reference) override;
+};
+
+} // namespace nntrainer
+
+#endif // __ACTIVATION_REALIZER_H__
index 04e96bd..ce2ebb4 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Copyright (C) 2021 Jihoon Lee <jhoon.it.lee@samsung.com>
  *
- * @file flatten_realizer.h
+ * @file flatten_realizer.cpp
  * @date 09 October 2021
  * @brief NNTrainer graph realizer which realizes flatten=true to actual node
  * @see        https://github.com/nnstreamer/nntrainer
index 3912833..073ec59 100644 (file)
@@ -1,5 +1,6 @@
 compiler_sources = [
   'ini_interpreter.cpp',
+  'activation_realizer.cpp',
   'flatten_realizer.cpp',
   'recurrent_realizer.cpp',
   'remap_realizer.cpp',