[graph] bug fix on layer names
authorParichay Kapoor <pk.kapoor@samsung.com>
Mon, 15 Mar 2021 06:59:18 +0000 (15:59 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 16 Mar 2021 07:06:21 +0000 (16:06 +0900)
Major bug fix on layer names. Layer names were being checked for duplicacy
but not added in the names list. This patch solves that bug.

Also added bug fixes to backbone being added to the graph properly
with appropriate name checkings now.

**Self evaluation:**
1. Build test: [x]Passed [ ]Failed [ ]Skipped
2. Run test: [x]Passed [ ]Failed [ ]Skipped

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
nntrainer/graph/network_graph.cpp
nntrainer/graph/network_graph.h

index c9a0b7111fa6b3cc45fc0d0cc7a7fc79b98bcf04..dbaeae9aab0bb515ffdf601d688d971de9e2135a 100644 (file)
@@ -60,8 +60,6 @@ void NetworkGraph::addLayerNode(std::shared_ptr<Layer> layer) {
   std::list<LayerNode> l;
   std::unique_ptr<LayerNode> node = std::make_unique<LayerNode>();
 
-  ensureName(layer);
-
   node->layer = layer;
   node->index = num_node;
 
@@ -148,14 +146,17 @@ void NetworkGraph::ensureName(std::shared_ptr<Layer> layer,
   std::string orig_name = layer->getName();
   bool orig_name_empty = orig_name.empty();
   if (!orig_name_empty && !force_rename &&
-      layer_names.end() == layer_names.find(orig_name))
+      layer_names.end() == layer_names.find(orig_name)) {
+    layer_names.insert(orig_name);
     return;
+  }
 
   /** If just prefix with layer name makes it unique - directly set the name */
   if (!orig_name_empty) {
     std::string direct_name = prefix + orig_name;
     if (layer_names.find(direct_name) == layer_names.end()) {
       layer->setName(direct_name);
+      layer_names.insert(direct_name);
       return;
     }
   }
@@ -172,6 +173,7 @@ void NetworkGraph::ensureName(std::shared_ptr<Layer> layer,
   } while (iter != layer_names.end());
 
   layer->setName(name);
+  layer_names.insert(name);
 }
 
 int NetworkGraph::realizeMultiInputType(Layer &current) {
@@ -584,9 +586,19 @@ NetworkGraph::getGraph(const std::string &input_layer,
 void NetworkGraph::extendGraph(std::vector<std::shared_ptr<Layer>> graph,
                                std::string prefix) {
 
+  /**
+   * The input_layers for graph[0] here is provided to the backbone by the ini
+   * file and is overwritten here by the model loader for connection making.
+   *
+   * This loop intends to connect a new backbone to be added with an old
+   * backbone.
+   */
   for (unsigned int i = 0; i < graph[0]->input_layers.size(); ++i) {
     if (sub_in_out.find(graph[0]->input_layers[i]) != sub_in_out.end()) {
       graph[0]->input_layers[i] = sub_in_out[graph[0]->input_layers[i]];
+    } else if (layer_names.find(graph[0]->input_layers[i]) ==
+               layer_names.end()) {
+      throw std::runtime_error("Input layer name for backbone not found.");
     }
   }
 
@@ -596,20 +608,24 @@ void NetworkGraph::extendGraph(std::vector<std::shared_ptr<Layer>> graph,
      * Add prefix to the existing layer name,
      * and ensure it is unique in this new graph
      */
-    std::string org_name = prefix + layer->getName();
+    std::string orig_name = prefix + layer->getName();
     ensureName(layer, prefix, true);
-    sub_in_out.insert(std::make_pair(org_name, layer->getName()));
+    sub_in_out.insert(std::make_pair(orig_name, layer->getName()));
 
     for (unsigned int i = 0; i < layer->input_layers.size(); ++i) {
       if (sub_in_out.find(prefix + layer->input_layers[i]) !=
           sub_in_out.end()) {
         layer->input_layers[i] = sub_in_out[prefix + layer->input_layers[i]];
+      } else if (layer_names.find(layer->input_layers[i]) ==
+                 layer_names.end()) {
+        throw std::runtime_error("Input layer name for backbone not found.");
       }
     }
 
     layers.push_back(layer);
   }
 
+  /** This allows connecting a layer to the backbone */
   sub_in_out.insert(std::make_pair(prefix, layers.back()->getName()));
 }
 
index 93dff7b8168339e78fb18605a4259a9eed716c93..141d2df5fda8b698b830802baf5a11d6b3888fdf 100644 (file)
@@ -103,6 +103,7 @@ public:
   friend void swap(NetworkGraph &lhs, NetworkGraph &rhs) {
     using std::swap;
 
+    swap(lhs.num_node, rhs.num_node);
     swap(lhs.layers, rhs.layers);
     swap(lhs.adj, rhs.adj);
     swap(lhs.Sorted, rhs.Sorted);
@@ -330,7 +331,7 @@ private:
 
   void updateNameInLayers(const std::string &cname, const std::string &name);
 
-  std::map<std::string, std::string> sub_in_out; /** This is map to identyfy
+  std::map<std::string, std::string> sub_in_out; /** This is map to identify
                    input and output layer name of subgraph */
   std::vector<std::shared_ptr<Layer>>
     layers;                              /**< vector for store layer pointers */