[Filter/TF] refcount of tf-buffer
authorJaeyun <jy1210.jung@samsung.com>
Mon, 8 Apr 2019 04:56:49 +0000 (13:56 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Tue, 9 Apr 2019 09:46:36 +0000 (18:46 +0900)
With mem-optimized flag, always unref tf-buffer after making tensor with passed input data.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
ext/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc

index feae0fa..f85438f 100644 (file)
@@ -428,7 +428,7 @@ class TFBuffer : public TensorBuffer {
     proto->set_allocator_name (cpu_allocator ()->Name ());
   }
 
-  // Prevents input forwarding from mutating this buffer.
+  /* Prevents input forwarding from mutating this buffer. */
   bool OwnsMemory () const override { return false; }
 };
 
@@ -449,34 +449,39 @@ TFCore::run (const GstTensorMemory * input, GstTensorMemory * output)
 
   std::vector <std::pair <string, Tensor>> input_feeds;
   std::vector <Tensor> outputs;
-  Tensor in;
 
   for (int i = 0; i < inputTensorMeta.num_tensors; ++i) {
+    Tensor in;
+
     /* If the datatype is STRING, it should be handled in specific process */
     if (input_tensor_info[i].type == DT_STRING) {
       in = Tensor (input_tensor_info[i].type, input_tensor_info[i].shape);
       in.scalar<string>()() = string ((char *) input[i].data, input[i].size);
     } else {
       if (mem_optmz) {
-        TFBuffer *buf = new TFBuffer;
-        buf->len_ = input[i].size;
-        buf->data_ = input[i].data;
-
+        TFBuffer *buf;
         TF_DataType dataType;
+
         if (!getTensorTypeToTF_Capi (input[i].type, &dataType)){
           g_critical ("This data type is not valid: %d", input[i].type);
-          buf->Unref();
           return -1;
         }
+
         /* this input tensor should be UNREF */
+        buf = new TFBuffer;
+        buf->len_ = input[i].size;
+        buf->data_ = input[i].data;
+
         in = TensorCApi::MakeTensor (
           dataType,
           input_tensor_info[i].shape,
           buf
         );
+
+        buf->Unref();
+
         if (!in.IsAligned ()) {
           g_critical ("the input tensor %s is not aligned", inputTensorMeta.info[i].name);
-          buf->Unref();
           return -2;
         }
       } else {
@@ -492,15 +497,6 @@ TFCore::run (const GstTensorMemory * input, GstTensorMemory * output)
   Status run_status =
       session->Run (input_feeds, output_tensor_names, {}, &outputs);
 
-  if (mem_optmz) {
-    for (int i = 0; i < inputTensorMeta.num_tensors; ++i) {
-      if (input_feeds[i].second.dtype () != DT_STRING){
-        TensorBuffer *buf = TensorCApi::Buffer (input_feeds[i].second);
-        buf->Unref();
-      }
-    }
-  }
-
   if (!run_status.ok()) {
     g_critical ("Failed to run model: %s\n", run_status.ToString().c_str());
     return -1;