From d24004cf48b41fce96ad1f11d83bec7584b777a1 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat <bbaren@google.com> Date: Tue, 17 Dec 2019 18:13:37 -0800 Subject: [PATCH] Eliminate VLAs VLAs complicate static analysis and bloat stack size. Replace VLA allocation with STL containers. PiperOrigin-RevId: 286101915 Change-Id: I2f6a934824fdbd7c54f4c2618de3f24ce9094205 --- tensorflow/core/platform/env.cc | 6 +++--- .../lite/examples/ios/camera/CameraExampleViewController.mm | 5 +++-- tensorflow/lite/experimental/objc/sources/TFLInterpreter.mm | 6 ++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tensorflow/core/platform/env.cc b/tensorflow/core/platform/env.cc index ee4ae92f905..eedfa2ee48f 100644 --- a/tensorflow/core/platform/env.cc +++ b/tensorflow/core/platform/env.cc @@ -322,9 +322,9 @@ string Env::GetExecutablePath() { #ifdef __APPLE__ uint32_t buffer_size(0U); _NSGetExecutablePath(nullptr, &buffer_size); - char unresolved_path[buffer_size]; - _NSGetExecutablePath(unresolved_path, &buffer_size); - CHECK(realpath(unresolved_path, exe_path)); + std::vector<char> unresolved_path(buffer_size); + _NSGetExecutablePath(unresolved_path.data(), &buffer_size); + CHECK(realpath(unresolved_path.data(), exe_path)); #elif defined(__FreeBSD__) int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; size_t exe_path_size = PATH_MAX; diff --git a/tensorflow/lite/examples/ios/camera/CameraExampleViewController.mm b/tensorflow/lite/examples/ios/camera/CameraExampleViewController.mm index e92a4d9ab1c..665131195e4 100644 --- a/tensorflow/lite/examples/ios/camera/CameraExampleViewController.mm +++ b/tensorflow/lite/examples/ios/camera/CameraExampleViewController.mm @@ -22,6 +22,7 @@ #include <fstream> #include <iostream> #include <queue> +#include <vector> #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" @@ -357,11 +358,11 @@ void ProcessInputWithQuantizedModel( uint8_t* quantized_output = interpreter->typed_output_tensor<uint8_t>(0); int32_t zero_point = input_tensor->params.zero_point; float scale = input_tensor->params.scale; - float output[output_size]; + std::vector<float> output(output_size); for (int i = 0; i < output_size; ++i) { output[i] = (quantized_output[i] - zero_point) * scale; } - GetTopN(output, output_size, kNumResults, kThreshold, &top_results); + GetTopN(output.data(), output_size, kNumResults, kThreshold, &top_results); } else { float* output = interpreter->typed_output_tensor<float>(0); GetTopN(output, output_size, kNumResults, kThreshold, &top_results); diff --git a/tensorflow/lite/experimental/objc/sources/TFLInterpreter.mm b/tensorflow/lite/experimental/objc/sources/TFLInterpreter.mm index 8ef4c571558..e0cca1076f6 100644 --- a/tensorflow/lite/experimental/objc/sources/TFLInterpreter.mm +++ b/tensorflow/lite/experimental/objc/sources/TFLInterpreter.mm @@ -14,6 +14,8 @@ #import "tensorflow/lite/experimental/objc/apis/TFLInterpreter.h" +#include <vector> + #import "TFLErrorUtil.h" #import "TFLQuantizationParameters+Internal.h" #import "TFLTensor+Internal.h" @@ -168,7 +170,7 @@ static void TFLInterpreterErrorReporter(void *user_data, const char *format, va_ return NO; } - int cDimensions[self.inputTensorCount]; + std::vector<int> cDimensions(self.inputTensorCount); for (int dimIndex = 0; dimIndex < shape.count; ++dimIndex) { int dimension = shape[dimIndex].intValue; if (dimension <= 0) { @@ -181,7 +183,7 @@ static void TFLInterpreterErrorReporter(void *user_data, const char *format, va_ cDimensions[dimIndex] = dimension; } - if (TfLiteInterpreterResizeInputTensor(self.interpreter, (int32_t)index, cDimensions, + if (TfLiteInterpreterResizeInputTensor(self.interpreter, (int32_t)index, cDimensions.data(), (int32_t)shape.count) != kTfLiteOk) { NSString *errorDescription = [NSString stringWithFormat:@"Failed to resize input tensor at index (%lu).", (unsigned long)index];