Skip to content

Commit 381d92c

Browse files
Unified buffer index tests to regular and eval tensor initiation
1 parent 409e60b commit 381d92c

File tree

2 files changed

+5
-27
lines changed

2 files changed

+5
-27
lines changed

tensorflow/lite/micro/micro_allocator.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,7 @@ TfLiteStatus MicroAllocator::PopulateTfLiteTensorFromFlatbuffer(
11341134
// TODO(b/162311891): This method serves as a stub to ensure quantized
11351135
// allocations in the tail can be recorded. Once the interpreter has APIs for
11361136
// accessing buffers on TfLiteEvalTensor this method can be dropped.
1137+
11371138
// Validate subgraph and tensor indices before dereferencing FlatBuffer
11381139
// vectors to avoid out-of-bounds access.
11391140
if (subgraph_idx < 0 ||

tensorflow/lite/micro/micro_allocator_test.cc

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ TF_LITE_MICRO_TEST(TestMultiSubgraphNumScratchAllocations) {
13881388
// New tests validating invalid buffer index guards in tensor initialization
13891389
// and model allocation paths.
13901390

1391-
TF_LITE_MICRO_TEST(InitializeTfLiteTensorFromFlatbuffer_InvalidBufferIndexReturnsError) {
1391+
TF_LITE_MICRO_TEST(TestInitializeTensorInvalidBufferIndex) {
13921392
// Arena and allocator for temporary/persistent allocations used by init.
13931393
constexpr size_t arena_size = 1024;
13941394
uint8_t arena[arena_size];
@@ -1415,44 +1415,21 @@ TF_LITE_MICRO_TEST(InitializeTfLiteTensorFromFlatbuffer_InvalidBufferIndexReturn
14151415
tflite::testing::CreateFlatbufferBuffers();
14161416

14171417
TfLiteTensor out_tensor;
1418+
TfLiteEvalTensor out_eval_tensor;
1419+
14181420
// Expect kTfLiteError due to invalid buffer index.
14191421
TF_LITE_MICRO_EXPECT_EQ(
14201422
kTfLiteError,
14211423
tflite::internal::InitializeTfLiteTensorFromFlatbuffer(
14221424
simple_allocator, simple_allocator, /*allocate_temp=*/false,
14231425
*bad_tensor, buffers, &out_tensor));
1424-
1425-
simple_allocator->~SingleArenaBufferAllocator();
1426-
}
1427-
1428-
TF_LITE_MICRO_TEST(InitializeTfLiteEvalTensorFromFlatbuffer_InvalidBufferIndexReturnsError) {
1429-
// Build a flatbuffer Tensor that references a non-existent buffer index.
1430-
flatbuffers::FlatBufferBuilder builder;
1431-
const int32_t dims_data[] = {1};
1432-
auto dims_vec = builder.CreateVector(dims_data, 1);
1433-
const uint32_t kInvalidBufferIndex = 7;
1434-
auto name_str = builder.CreateString("invalid_eval_buffer_tensor");
1435-
auto tensor_offset = tflite::CreateTensor(
1436-
builder, /*shape=*/dims_vec, tflite::TensorType_INT32,
1437-
/*buffer=*/kInvalidBufferIndex, /*name=*/name_str,
1438-
/*quantization=*/0, /*is_variable=*/false, /*sparsity=*/0);
1439-
builder.Finish(tensor_offset);
1440-
const tflite::Tensor* bad_tensor =
1441-
flatbuffers::GetRoot<tflite::Tensor>(builder.GetBufferPointer());
1442-
1443-
// Create a buffers vector with a single empty buffer.
1444-
const flatbuffers::Vector<flatbuffers::Offset<tflite::Buffer>>* buffers =
1445-
tflite::testing::CreateFlatbufferBuffers();
1446-
1447-
TfLiteEvalTensor out_eval_tensor;
1448-
// Expect kTfLiteError due to invalid buffer index.
14491426
TF_LITE_MICRO_EXPECT_EQ(
14501427
kTfLiteError,
14511428
tflite::internal::InitializeTfLiteEvalTensorFromFlatbuffer(
14521429
*bad_tensor, buffers, &out_eval_tensor));
14531430
}
14541431

1455-
TF_LITE_MICRO_TEST(StartModelAllocation_FailsWhenSubgraphHasTensorWithInvalidBufferIndex) {
1432+
TF_LITE_MICRO_TEST(TestModelAllocationSubgraphInvalidBufferIndex) {
14561433
// Build a minimal model with a single tensor that references an invalid
14571434
// buffer index. Make it an output tensor to mimic output-table invalid case
14581435
// but the allocator will validate all tensors equally, so this also

0 commit comments

Comments
 (0)