From d0c7f0537426d93ca26b1e43174d32575db47e64 Mon Sep 17 00:00:00 2001 From: Jaeyun Date: Mon, 8 Apr 2019 13:56:49 +0900 Subject: [PATCH] [Filter/TF] refcount of tf-buffer With mem-optimized flag, always unref tf-buffer after making tensor with passed input data. Signed-off-by: Jaeyun Jung --- .../tensor_filter/tensor_filter_tensorflow_core.cc | 30 ++++++++++------------ 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/ext/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc b/ext/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc index feae0fa..f85438f 100644 --- a/ext/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc +++ b/ext/nnstreamer/tensor_filter/tensor_filter_tensorflow_core.cc @@ -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 > input_feeds; std::vector 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 ((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; -- 2.7.4