[API] Bug fix in C-API get_layer
authorParichay Kapoor <pk.kapoor@samsung.com>
Mon, 13 Jul 2020 04:09:22 +0000 (13:09 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 13 Jul 2020 12:53:29 +0000 (21:53 +0900)
ml_nnmodel_get_layer function was adding another layer in struct NeuralNetwork if layer wasnt found in layers_map
This function was supposed to only add layer in layers_map and not modify struct NeuralNetwork

This PR applies the above bug fix.

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
api/capi/src/nntrainer.cpp

index ce4518087e0278f793e2c81c92895bda8fab6d2b..3540451fc01ade73c125ee0c297280589002a29a 100644 (file)
@@ -314,11 +314,16 @@ int ml_nnmodel_get_layer(ml_nnmodel_h model, const char *layer_name,
 
   std::unordered_map<std::string, ml_nnlayer *>::iterator layer_iter =
     nnmodel->layers_map.find(std::string(layer_name));
+  /** if layer found in layers_map, return layer */
   if (layer_iter != nnmodel->layers_map.end()) {
     *layer = layer_iter->second;
     return status;
   }
 
+  /**
+   * if layer not found in layers_map, get layer from NeuralNetwork,
+   * wrap it in struct nnlayer, add new entry in layer_map and then return
+   */
   NN = nnmodel->network;
   returnable f = [&]() { return NN->getLayer(layer_name, &NL); };
   status = nntrainer_exception_boundary(f);
@@ -329,14 +334,10 @@ int ml_nnmodel_get_layer(ml_nnmodel_h model, const char *layer_name,
   ml_nnlayer *nnlayer = new ml_nnlayer;
   nnlayer->magic = ML_NNTRAINER_MAGIC;
   nnlayer->layer = NL;
-  *layer = nnlayer;
-
-  status = ml_nnmodel_add_layer(model, *layer);
-  if (status != ML_ERROR_NONE) {
-    delete nnlayer;
-    *layer = nullptr;
-  }
+  nnlayer->in_use = true;
+  nnmodel->layers_map.insert({NL->getName(), nnlayer});
 
+  *layer = nnlayer;
   return status;
 }