Manage bias_data lifetime correclty (#385)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 3 Apr 2018 13:49:11 +0000 (22:49 +0900)
committer이성재/동작제어Lab(SR)/Principal Engineer/삼성전자 <sj925.lee@samsung.com>
Tue, 3 Apr 2018 13:49:11 +0000 (22:49 +0900)
T/F Lite Interpreter assumes that buffer passed via
SetTensorParametersReadOnly method lives longer than the interpreter
itself.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
tools/nnapi_unittests/tests/conv_1.cpp

index c7e598a..d936ee9 100644 (file)
@@ -59,6 +59,16 @@ int main(int argc, char **argv)
     assert(kernel_size == off);
   }
 
+  // Configure Bias Data
+  const uint32_t bias_size = bias.size();
+  float bias_data[bias_size] = { 0.0f, };
+
+  // Fill bias data
+  for (uint32_t off = 0; off < bias.size(); ++off)
+  {
+    bias_data[off] = bias.at(off);
+  }
+
   // Assumption on this example
   assert(IFM_C == KER_C);
   assert(KER_N == bias.size());
@@ -95,7 +105,7 @@ int main(int argc, char **argv)
                                         {1 /*N*/, IFM_H, IFM_W, IFM_C} /* dims */,
                                         quantization);
 
-    // NOTE kernel_data should live longer than interpreter!
+    // NOTE kernel_data & bias_data should live longer than interpreter!
     interp.SetTensorParametersReadOnly(2,
                                        kTfLiteFloat32 /* type */,
                                        "filter" /* name */,
@@ -103,22 +113,12 @@ int main(int argc, char **argv)
                                        quantization,
                                        reinterpret_cast<const char *>(kernel_data), kernel_size * sizeof(float));
 
-    // Configure Bias
-    const uint32_t bias_size = bias.size();
-    float bias_data[bias_size] = { 0.0f, };
-
-    // Fill bias data
-    for (uint32_t off = 0; off < bias.size(); ++off)
-    {
-      bias_data[off] = bias.at(off);
-    }
-
     interp.SetTensorParametersReadOnly(3,
                                        kTfLiteFloat32 /* type */,
                                        "bias" /* name */,
                                        { bias.size() } /* dims */,
                                        quantization,
-                                       reinterpret_cast<const char *>(bias_data), sizeof(bias_data));
+                                       reinterpret_cast<const char *>(bias_data), bias_size * sizeof(float));
 
     // Add Convolution Node
     //