[Property] Add shared_from key to the layer node
authorJihoon Lee <jhoon.it.lee@samsung.com>
Fri, 1 Oct 2021 08:07:45 +0000 (17:07 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 7 Oct 2021 10:20:47 +0000 (19:20 +0900)
This patch add shared_from key to the layer node

**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/graph/network_graph.cpp
nntrainer/layers/layer_context.h
nntrainer/layers/layer_node.cpp
nntrainer/layers/layer_node.h
nntrainer/utils/node_exporter.cpp
nntrainer/utils/node_exporter.h

index 19d5caf..fa5c38b 100644 (file)
@@ -617,6 +617,9 @@ void NetworkGraph::extendGraph(std::vector<std::shared_ptr<LayerNode>> ex_graph,
   /** This allows connecting a layer to the backbone */
   sub_in_out.insert(
     std::make_pair(prefix, graph.getNode(graph.size() - 1)->getName()));
+
+  /** @todo Update shared_from node name as well */
+  /** @todo Add test for this */
 }
 
 void NetworkGraph::addLayer(std::shared_ptr<LayerNode> layer) {
index e445464..98358eb 100644 (file)
@@ -327,6 +327,7 @@ public:
   /**
    * @brief Get the Weight Gradient tensor object
    *
+   * @note this method returns the fresh gradient to be filled
    * @param idx Identifier of the weight
    * @return Tensor& Reference to the weight grad tensor
    */
index 55b71e1..d862679 100644 (file)
@@ -127,6 +127,12 @@ public:
   }
 };
 
+class SharedFrom : public Name {
+public:
+  static constexpr const char *key = "shared_from"; /**< unique key to access */
+  using prop_tag = str_prop_tag;                    /**< property type */
+};
+
 } // namespace props
 
 /**
@@ -171,7 +177,8 @@ LayerNode::LayerNode(std::unique_ptr<nntrainer::Layer> &&l) :
   layer(std::move(l)),
   run_context(nullptr),
   layer_node_props(new PropsType(props::Name(), props::Distribute(),
-                                 props::Trainable(), {}, {})),
+                                 props::Trainable(), {}, {},
+                                 props::SharedFrom())),
   layer_node_props_realization(
     new RealizationPropsType(props::Flatten(), props::Activation())),
   loss(new props::Loss()),
@@ -255,6 +262,11 @@ bool LayerNode::getFlatten() const {
   return flatten.get();
 }
 
+std::string LayerNode::getSharedFrom() const {
+  auto &shared_from = std::get<props::SharedFrom>(*layer_node_props);
+  return shared_from.empty() ? "" : shared_from.get();
+}
+
 bool LayerNode::getDistribute() const {
   auto &distribute = std::get<props::Distribute>(*layer_node_props);
   if (distribute.empty()) {
index eabb461..5db3e32 100644 (file)
@@ -49,6 +49,7 @@ class Loss;
 class InputLayer;
 class InputShape;
 class Activation;
+class SharedFrom;
 } // namespace props
 
 /**
@@ -253,6 +254,13 @@ public:
   bool getFlatten() const;
 
   /**
+   * @brief Get the Shared From property of the layer node
+   *
+   * @return std::string node name where the weights are borrowed
+   */
+  std::string getSharedFrom() const;
+
+  /**
    * @brief     get distribute for this layer
    * @retval dist to enable/disable distribute
    */
@@ -630,7 +638,8 @@ properties in the context/graph unless intended. */
 
   using PropsType =
     std::tuple<props::Name, props::Distribute, props::Trainable,
-               std::vector<props::InputLayer>, std::vector<props::InputShape>>;
+               std::vector<props::InputLayer>, std::vector<props::InputShape>,
+               props::SharedFrom>;
 
   using RealizationPropsType = std::tuple<props::Flatten, props::Activation>;
   /** these realization properties results in addition of new layers, hence
index 66e2fe6..aab8145 100644 (file)
@@ -56,7 +56,7 @@ template <>
 void Exporter::saveTflResult(
   const std::tuple<props::Name, props::Distribute, props::Trainable,
                    std::vector<props::InputLayer>,
-                   std::vector<props::InputShape>> &props,
+                   std::vector<props::InputShape>, props::SharedFrom> &props,
   const LayerNode *self) {
   createIfNull(tf_node);
   tf_node->setLayerNode(*self);
index 6e171c0..b68e3ea 100644 (file)
@@ -214,6 +214,7 @@ class WeightRegularizer;
 class WeightRegularizerConstant;
 class WeightInitializer;
 class BiasInitializer;
+class SharedFrom;
 } // namespace props
 
 class LayerNode;
@@ -225,7 +226,7 @@ template <>
 void Exporter::saveTflResult(
   const std::tuple<props::Name, props::Distribute, props::Trainable,
                    std::vector<props::InputLayer>,
-                   std::vector<props::InputShape>> &props,
+                   std::vector<props::InputShape>, props::SharedFrom> &props,
   const LayerNode *self);
 
 class LayerImpl;