Change AllocatePersistentBuffer API to just return a pointer if succeed, or nullptr upon failure
Changes the canonical usage pattern from:
void* data = nullptr;
if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
kTfLiteError) {
return nullptr;
}
return data;
to:
return context->AllocatePersistentBuffer(context, sizeof(OpData);
PiperOrigin-RevId: 322452971
Change-Id: If5de6a44978ce464b33605b8ed186d9767e0716d
diff --git a/tensorflow/lite/c/common.h b/tensorflow/lite/c/common.h
index 692a8ea..0979c4d 100644
--- a/tensorflow/lite/c/common.h
+++ b/tensorflow/lite/c/common.h
@@ -703,12 +703,11 @@
void* profiler;
// Allocate persistent buffer which has the same life time as the interpreter.
+ // Returns nullptr on failure.
// The memory is allocated from heap for TFL, and from tail in TFLM.
- // If *ptr is not nullptr, the pointer will be reallocated.
- // This method is only available in Prepare stage.
+ // This method is only available in Init or Prepare stage.
// WARNING: This is an experimental interface that is subject to change.
- TfLiteStatus (*AllocatePersistentBuffer)(struct TfLiteContext* ctx,
- size_t bytes, void** ptr);
+ void* (*AllocatePersistentBuffer)(struct TfLiteContext* ctx, size_t bytes);
// Allocate a buffer which will be deallocated right after invoke phase.
// The memory is allocated from heap in TFL, and from volatile arena in TFLM.
diff --git a/tensorflow/lite/micro/kernels/add.cc b/tensorflow/lite/micro/kernels/add.cc
index 8d2ea6c..be089da 100644
--- a/tensorflow/lite/micro/kernels/add.cc
+++ b/tensorflow/lite/micro/kernels/add.cc
@@ -163,12 +163,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/cmsis-nn/add.cc b/tensorflow/lite/micro/kernels/cmsis-nn/add.cc
index 0ccdd16..4190e04 100644
--- a/tensorflow/lite/micro/kernels/cmsis-nn/add.cc
+++ b/tensorflow/lite/micro/kernels/cmsis-nn/add.cc
@@ -173,12 +173,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/cmsis-nn/conv.cc b/tensorflow/lite/micro/kernels/cmsis-nn/conv.cc
index 909b37c..da60841 100644
--- a/tensorflow/lite/micro/kernels/cmsis-nn/conv.cc
+++ b/tensorflow/lite/micro/kernels/cmsis-nn/conv.cc
@@ -109,9 +109,8 @@
}
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
- void* raw;
- context->AllocatePersistentBuffer(context, sizeof(int), &raw);
- return raw;
+ TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
+ return context->AllocatePersistentBuffer(context, sizeof(int));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/cmsis-nn/depthwise_conv.cc b/tensorflow/lite/micro/kernels/cmsis-nn/depthwise_conv.cc
index 889193a..53d2d56 100644
--- a/tensorflow/lite/micro/kernels/cmsis-nn/depthwise_conv.cc
+++ b/tensorflow/lite/micro/kernels/cmsis-nn/depthwise_conv.cc
@@ -102,12 +102,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/cmsis-nn/fully_connected.cc b/tensorflow/lite/micro/kernels/cmsis-nn/fully_connected.cc
index c7e64dd..1ea7f98 100644
--- a/tensorflow/lite/micro/kernels/cmsis-nn/fully_connected.cc
+++ b/tensorflow/lite/micro/kernels/cmsis-nn/fully_connected.cc
@@ -77,12 +77,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/cmsis-nn/pooling.cc b/tensorflow/lite/micro/kernels/cmsis-nn/pooling.cc
index 77883f4..d0babb4 100644
--- a/tensorflow/lite/micro/kernels/cmsis-nn/pooling.cc
+++ b/tensorflow/lite/micro/kernels/cmsis-nn/pooling.cc
@@ -249,12 +249,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus MaxPrepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/comparisons.cc b/tensorflow/lite/micro/kernels/comparisons.cc
index e63a1f6..8f6a830 100644
--- a/tensorflow/lite/micro/kernels/comparisons.cc
+++ b/tensorflow/lite/micro/kernels/comparisons.cc
@@ -528,12 +528,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/concatenation.cc b/tensorflow/lite/micro/kernels/concatenation.cc
index 9b5515a..e6e56a5 100644
--- a/tensorflow/lite/micro/kernels/concatenation.cc
+++ b/tensorflow/lite/micro/kernels/concatenation.cc
@@ -119,12 +119,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
@@ -184,15 +179,13 @@
data->params.axis = CalculatePositiveAxis(params->axis, output);
data->params.inputs_count = node->inputs->size;
- float* input_scales = nullptr;
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, node->inputs->size * sizeof(float),
- reinterpret_cast<void**>(&input_scales)));
+ float* input_scales =
+ reinterpret_cast<float*>(context->AllocatePersistentBuffer(
+ context, node->inputs->size * sizeof(float)));
- int32_t* input_zero_points = nullptr;
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, node->inputs->size * sizeof(int32_t),
- reinterpret_cast<void**>(&input_zero_points)));
+ int32_t* input_zero_points =
+ reinterpret_cast<int32_t*>(context->AllocatePersistentBuffer(
+ context, node->inputs->size * sizeof(int32_t)));
// Allocate persistent scale and zeropoint buffers.
// Store input scale and zero point values in OpParams:
diff --git a/tensorflow/lite/micro/kernels/conv.cc b/tensorflow/lite/micro/kernels/conv.cc
index fec6f1e..1f286dd 100644
--- a/tensorflow/lite/micro/kernels/conv.cc
+++ b/tensorflow/lite/micro/kernels/conv.cc
@@ -109,12 +109,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
@@ -137,12 +132,12 @@
// Dynimically allocate per-channel quantization parameters.
const int num_channels = filter->dims->data[kConvQuantizedDimension];
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t),
- reinterpret_cast<void**>(&data->per_channel_output_multiplier)));
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t),
- reinterpret_cast<void**>(&data->per_channel_output_shift)));
+ data->per_channel_output_multiplier =
+ reinterpret_cast<int32_t*>(context->AllocatePersistentBuffer(
+ context, num_channels * sizeof(int32_t)));
+ data->per_channel_output_shift =
+ reinterpret_cast<int32_t*>(context->AllocatePersistentBuffer(
+ context, num_channels * sizeof(int32_t)));
// All per-channel quantized tensors need valid zero point and scale arrays.
if (input->type == kTfLiteInt8) {
diff --git a/tensorflow/lite/micro/kernels/depthwise_conv.cc b/tensorflow/lite/micro/kernels/depthwise_conv.cc
index f85323b..c75e0d9 100644
--- a/tensorflow/lite/micro/kernels/depthwise_conv.cc
+++ b/tensorflow/lite/micro/kernels/depthwise_conv.cc
@@ -95,12 +95,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
@@ -124,12 +119,12 @@
// quantized types, only a single scale and zero point is needed.
const int num_channels = filter->dims->data[kDepthwiseConvQuantizedDimension];
// Dynimically allocate per-channel quantization parameters.
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t),
- reinterpret_cast<void**>(&data->per_channel_output_multiplier)));
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t),
- reinterpret_cast<void**>(&data->per_channel_output_shift)));
+ data->per_channel_output_multiplier =
+ reinterpret_cast<int32_t*>(context->AllocatePersistentBuffer(
+ context, num_channels * sizeof(int32_t)));
+ data->per_channel_output_shift =
+ reinterpret_cast<int32_t*>(context->AllocatePersistentBuffer(
+ context, num_channels * sizeof(int32_t)));
// All per-channel quantized tensors need valid zero point and scale arrays.
if (input->type == kTfLiteInt8) {
diff --git a/tensorflow/lite/micro/kernels/dequantize.cc b/tensorflow/lite/micro/kernels/dequantize.cc
index 55a41bf..607ada6 100644
--- a/tensorflow/lite/micro/kernels/dequantize.cc
+++ b/tensorflow/lite/micro/kernels/dequantize.cc
@@ -38,12 +38,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/fully_connected.cc b/tensorflow/lite/micro/kernels/fully_connected.cc
index 88c150d..3766267 100644
--- a/tensorflow/lite/micro/kernels/fully_connected.cc
+++ b/tensorflow/lite/micro/kernels/fully_connected.cc
@@ -72,12 +72,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/hard_swish.cc b/tensorflow/lite/micro/kernels/hard_swish.cc
index d4b46c7..fecb8bd 100644
--- a/tensorflow/lite/micro/kernels/hard_swish.cc
+++ b/tensorflow/lite/micro/kernels/hard_swish.cc
@@ -34,12 +34,8 @@
constexpr int kOutputTensor = 0;
void* HardSwishInit(TfLiteContext* context, const char* buffer, size_t length) {
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(HardSwishParams),
- &data) == kTfLiteError) {
- return nullptr;
- }
- return data;
+ TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
+ return context->AllocatePersistentBuffer(context, sizeof(HardSwishParams));
}
TfLiteStatus HardSwishPrepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/logistic.cc b/tensorflow/lite/micro/kernels/logistic.cc
index 3125945..6fcf60f 100644
--- a/tensorflow/lite/micro/kernels/logistic.cc
+++ b/tensorflow/lite/micro/kernels/logistic.cc
@@ -68,12 +68,7 @@
void* LogisticInit(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus LogisticPrepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/quantize.cc b/tensorflow/lite/micro/kernels/quantize.cc
index 243ed96..2817697 100644
--- a/tensorflow/lite/micro/kernels/quantize.cc
+++ b/tensorflow/lite/micro/kernels/quantize.cc
@@ -36,12 +36,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/softmax.cc b/tensorflow/lite/micro/kernels/softmax.cc
index 6dbae3b..881efda 100644
--- a/tensorflow/lite/micro/kernels/softmax.cc
+++ b/tensorflow/lite/micro/kernels/softmax.cc
@@ -101,12 +101,7 @@
void* SoftmaxInit(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(SoftmaxParams),
- &data) == kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(SoftmaxParams));
}
TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/svdf.cc b/tensorflow/lite/micro/kernels/svdf.cc
index fde5269..c0bae4a 100644
--- a/tensorflow/lite/micro/kernels/svdf.cc
+++ b/tensorflow/lite/micro/kernels/svdf.cc
@@ -338,12 +338,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini/conv.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini/conv.cc
index 26cc0f0..dc39cc4 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini/conv.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini/conv.cc
@@ -303,12 +303,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
@@ -333,12 +328,12 @@
// quantized types, only a single scale and zero point is needed.
const int num_channels = filter->dims->data[kConvQuantizedDimension];
// Dynimically allocate per-channel quantization parameters.
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t),
- reinterpret_cast<void**>(&op_data->per_channel_output_multiplier)));
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t),
- reinterpret_cast<void**>(&op_data->per_channel_output_shift)));
+ op_data->per_channel_output_multiplier =
+ reinterpret_cast<int32_t>(context->AllocatePersistentBuffer(
+ context, num_channels * sizeof(int32_t)));
+ op_data->per_channel_output_shift =
+ reinterpret_cast<int32_t>(context->AllocatePersistentBuffer(
+ context, num_channels * sizeof(int32_t)));
// All per-channel quantized tensors need valid zero point and scale arrays.
if (input->type == kTfLiteInt8) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini/depthwise_conv.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini/depthwise_conv.cc
index a3aff59..e7a37b6 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini/depthwise_conv.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini/depthwise_conv.cc
@@ -352,12 +352,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
@@ -381,12 +376,12 @@
// quantized types, only a single scale and zero point is needed.
const int num_channels = filter->dims->data[kDepthwiseConvQuantizedDimension];
// Dynimically allocate per-channel quantization parameters.
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t),
- reinterpret_cast<void**>(&op_data->per_channel_output_multiplier)));
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t),
- reinterpret_cast<void**>(&op_data->per_channel_output_shift)));
+ op_data->per_channel_output_multiplier =
+ reinterpret_cast<int32_t>(context->AllocatePersistentBuffer(
+ context, num_channels * sizeof(int32_t)));
+ op_data->per_channel_output_shift =
+ reinterpret_cast<int32_t>(context->AllocatePersistentBuffer(
+ context, num_channels * sizeof(int32_t)));
// All per-channel quantized tensors need valid zero point and scale arrays.
if (input->type == kTfLiteInt8) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini/fully_connected.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini/fully_connected.cc
index a89c53f..6ebfbe7 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini/fully_connected.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini/fully_connected.cc
@@ -163,12 +163,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini/quantize.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini/quantize.cc
index 9e612cf..e735214 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini/quantize.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini/quantize.cc
@@ -109,12 +109,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini/softmax.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini/softmax.cc
index 3a4b157..0fb3646 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini/softmax.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini/softmax.cc
@@ -148,12 +148,7 @@
void* SoftmaxInit(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini/svdf.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini/svdf.cc
index dc071b2..8520dc2 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini/svdf.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini/svdf.cc
@@ -259,13 +259,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context != nullptr);
- TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/fully_connected.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/fully_connected.cc
index 6264373..f9b49a2 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/fully_connected.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/fully_connected.cc
@@ -98,12 +98,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/quantize.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/quantize.cc
index 7c521e7..513f926 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/quantize.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/quantize.cc
@@ -110,12 +110,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/softmax.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/softmax.cc
index 6588dff..90fc2cd 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/softmax.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/softmax.cc
@@ -98,12 +98,7 @@
void* SoftmaxInit(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/svdf.cc b/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/svdf.cc
index 6da8768..537b48d 100644
--- a/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/svdf.cc
+++ b/tensorflow/lite/micro/kernels/xtensa_hifimini_staging/svdf.cc
@@ -190,12 +190,7 @@
void* Init(TfLiteContext* context, const char* buffer, size_t length) {
TFLITE_DCHECK(context != nullptr);
TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- void* data = nullptr;
- if (context->AllocatePersistentBuffer(context, sizeof(OpData), &data) ==
- kTfLiteError) {
- return nullptr;
- }
- return data;
+ return context->AllocatePersistentBuffer(context, sizeof(OpData));
}
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
diff --git a/tensorflow/lite/micro/memory_helpers.cc b/tensorflow/lite/micro/memory_helpers.cc
index 20e6e10..d1e0392 100644
--- a/tensorflow/lite/micro/memory_helpers.cc
+++ b/tensorflow/lite/micro/memory_helpers.cc
@@ -140,9 +140,9 @@
output->bytes = size;
- TF_LITE_ENSURE_STATUS(context->AllocatePersistentBuffer(
- context, TfLiteIntArrayGetSizeInBytes(size),
- reinterpret_cast<void**>(&output->dims)));
+ output->dims =
+ reinterpret_cast<TfLiteIntArray*>(context->AllocatePersistentBuffer(
+ context, TfLiteIntArrayGetSizeInBytes(size)));
output->dims->size = input->dims->size;
for (int i = 0; i < dimensions_count; i++) {
diff --git a/tensorflow/lite/micro/memory_helpers_test.cc b/tensorflow/lite/micro/memory_helpers_test.cc
index 25ade76..5000a88 100644
--- a/tensorflow/lite/micro/memory_helpers_test.cc
+++ b/tensorflow/lite/micro/memory_helpers_test.cc
@@ -20,9 +20,15 @@
namespace {
-TfLiteStatus FakeAllocatePersistentBuffer(TfLiteContext* context, size_t bytes,
- void** ptr) {
- return kTfLiteOk;
+// This just needs to be big enough to handle the array of 5 ints allocated
+// in TestAllocateOutputDimensionsFromInput below.
+const int kGlobalPersistentBufferLength = 100;
+char global_persistent_buffer[kGlobalPersistentBufferLength];
+
+// Only need to handle a single allocation at a time for output dimensions
+// in TestAllocateOutputDimensionsFromInput.
+void* FakeAllocatePersistentBuffer(TfLiteContext* context, size_t bytes) {
+ return reinterpret_cast<void*>(global_persistent_buffer);
}
} // namespace
@@ -181,8 +187,8 @@
TfLiteTensor output_tensor = tflite::testing::CreateInt32Tensor(
nullptr, tflite::testing::IntArrayFromInts(output_dims));
TfLiteContext context;
- // Set allocator to no-op to avoid segfault. Memory is already allocated for
- // output dims.
+ // Only need to allocate space for output_tensor.dims. Use a simple
+ // fake allocator.
context.AllocatePersistentBuffer = FakeAllocatePersistentBuffer;
TF_LITE_MICRO_EXPECT_EQ(
diff --git a/tensorflow/lite/micro/micro_allocator.cc b/tensorflow/lite/micro/micro_allocator.cc
index 8ad6db3..73e7cd8 100644
--- a/tensorflow/lite/micro/micro_allocator.cc
+++ b/tensorflow/lite/micro/micro_allocator.cc
@@ -681,17 +681,8 @@
return kTfLiteOk;
}
-TfLiteStatus MicroAllocator::AllocatePersistentBuffer(size_t bytes,
- void** ptr) {
- uint8_t* data = memory_allocator_->AllocateFromTail(bytes, kBufferAlignment);
- if (data == nullptr) {
- TF_LITE_REPORT_ERROR(error_reporter_,
- "Failed to allocate persistent buffer of size %d",
- bytes);
- return kTfLiteError;
- }
- (*ptr) = data;
- return kTfLiteOk;
+void* MicroAllocator::AllocatePersistentBuffer(size_t bytes) {
+ return memory_allocator_->AllocateFromTail(bytes, kBufferAlignment);
}
TfLiteStatus MicroAllocator::RequestScratchBufferInArena(int node_id,
diff --git a/tensorflow/lite/micro/micro_allocator.h b/tensorflow/lite/micro/micro_allocator.h
index 47dad62..efd11b8 100644
--- a/tensorflow/lite/micro/micro_allocator.h
+++ b/tensorflow/lite/micro/micro_allocator.h
@@ -154,7 +154,7 @@
// Allocates persistent buffer which has the same life time as the allocator.
// The memory is immediately available and is allocated from the tail of the
// arena.
- TfLiteStatus AllocatePersistentBuffer(size_t bytes, void** ptr);
+ void* AllocatePersistentBuffer(size_t bytes);
// Register a scratch buffer of size `bytes` for Node with `node_id`.
// This method only allocates a BufferHandle holding information for memory
diff --git a/tensorflow/lite/micro/micro_interpreter.cc b/tensorflow/lite/micro/micro_interpreter.cc
index 87c8da7..41efe9e 100644
--- a/tensorflow/lite/micro/micro_interpreter.cc
+++ b/tensorflow/lite/micro/micro_interpreter.cc
@@ -47,10 +47,10 @@
MicroAllocator* allocator, const Model* model)
: allocator_(allocator), error_reporter_(error_reporter), model_(model) {}
-TfLiteStatus ContextHelper::AllocatePersistentBuffer(TfLiteContext* ctx,
- size_t bytes, void** ptr) {
+void* ContextHelper::AllocatePersistentBuffer(TfLiteContext* ctx,
+ size_t bytes) {
return reinterpret_cast<ContextHelper*>(ctx->impl_)
- ->allocator_->AllocatePersistentBuffer(bytes, ptr);
+ ->allocator_->AllocatePersistentBuffer(bytes);
}
TfLiteStatus ContextHelper::RequestScratchBufferInArena(TfLiteContext* ctx,
diff --git a/tensorflow/lite/micro/micro_interpreter.h b/tensorflow/lite/micro/micro_interpreter.h
index df70514..67d7457 100644
--- a/tensorflow/lite/micro/micro_interpreter.h
+++ b/tensorflow/lite/micro/micro_interpreter.h
@@ -42,8 +42,7 @@
MicroAllocator* allocator, const Model* model);
// Functions that will be assigned to function pointers on TfLiteContext:
- static TfLiteStatus AllocatePersistentBuffer(TfLiteContext* ctx, size_t bytes,
- void** ptr);
+ static void* AllocatePersistentBuffer(TfLiteContext* ctx, size_t bytes);
static TfLiteStatus RequestScratchBufferInArena(TfLiteContext* ctx,
size_t bytes,
int* buffer_idx);
diff --git a/tensorflow/lite/micro/test_helpers.cc b/tensorflow/lite/micro/test_helpers.cc
index 5df5b34..7278fea 100644
--- a/tensorflow/lite/micro/test_helpers.cc
+++ b/tensorflow/lite/micro/test_helpers.cc
@@ -574,9 +574,7 @@
TFLITE_DCHECK(context->GetScratchBuffer == nullptr);
TFLITE_DCHECK(context->RequestScratchBufferInArena == nullptr);
- void* raw;
- TFLITE_DCHECK(context->AllocatePersistentBuffer(context, sizeof(OpData),
- &raw) == kTfLiteOk);
+ void* raw = context->AllocatePersistentBuffer(context, sizeof(OpData));
OpData* data = reinterpret_cast<OpData*>(raw);
*data = {};
return raw;
diff --git a/tensorflow/lite/micro/testing/test_utils.cc b/tensorflow/lite/micro/testing/test_utils.cc
index 5aa73df..ec5396e 100644
--- a/tensorflow/lite/micro/testing/test_utils.cc
+++ b/tensorflow/lite/micro/testing/test_utils.cc
@@ -44,15 +44,9 @@
// signature of TfLiteContext::AllocatePersistentBuffer and isn't needed in the
// implementation because we are assuming a single global
// simple_memory_allocator_
-TfLiteStatus AllocatePersistentBuffer(TfLiteContext* context, size_t bytes,
- void** ptr) {
+void* AllocatePersistentBuffer(TfLiteContext* context, size_t bytes) {
TFLITE_DCHECK(simple_memory_allocator_ != nullptr);
- TFLITE_DCHECK(ptr != nullptr);
- *ptr = simple_memory_allocator_->AllocateFromTail(bytes, kBufferAlignment);
- if (*ptr == nullptr) {
- return kTfLiteError;
- }
- return kTfLiteOk;
+ return simple_memory_allocator_->AllocateFromTail(bytes, kBufferAlignment);
}
TfLiteStatus RequestScratchBufferInArena(TfLiteContext* context, size_t bytes,
diff --git a/tensorflow/lite/tools/benchmark/experimental/c/c_api_types.h b/tensorflow/lite/tools/benchmark/experimental/c/c_api_types.h
index 692a8ea..0979c4d 100644
--- a/tensorflow/lite/tools/benchmark/experimental/c/c_api_types.h
+++ b/tensorflow/lite/tools/benchmark/experimental/c/c_api_types.h
@@ -703,12 +703,11 @@
void* profiler;
// Allocate persistent buffer which has the same life time as the interpreter.
+ // Returns nullptr on failure.
// The memory is allocated from heap for TFL, and from tail in TFLM.
- // If *ptr is not nullptr, the pointer will be reallocated.
- // This method is only available in Prepare stage.
+ // This method is only available in Init or Prepare stage.
// WARNING: This is an experimental interface that is subject to change.
- TfLiteStatus (*AllocatePersistentBuffer)(struct TfLiteContext* ctx,
- size_t bytes, void** ptr);
+ void* (*AllocatePersistentBuffer)(struct TfLiteContext* ctx, size_t bytes);
// Allocate a buffer which will be deallocated right after invoke phase.
// The memory is allocated from heap in TFL, and from volatile arena in TFLM.