Eliminate VLAs

VLAs complicate static analysis and bloat stack size. Replace VLA
allocation with STL containers.

PiperOrigin-RevId: 286101915
Change-Id: I2f6a934824fdbd7c54f4c2618de3f24ce9094205
This commit is contained in:
Benjamin Barenblat 2019-12-17 18:13:37 -08:00 committed by TensorFlower Gardener
parent c69bd15c41
commit d24004cf48
3 changed files with 10 additions and 7 deletions
tensorflow
core/platform
lite
examples/ios/camera
experimental/objc/sources

View File

@ -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;

View File

@ -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);

View File

@ -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];