mirror of
https://github.com/vee1e/tflite-micro.git
synced 2026-09-01 17:57:27 +00:00
* Fixed StackAllocator * Bumped kStackAllocatorSize to 16KB * Simplified * Added micro_test v2 * Sample migration * More APIs * Fixed format * Output is more aligned with gtest * Removed legacy-eq * Migrated memory_planner tests * Code style * Added ~~~ALL TESTS PASSED~~~ * Review * Review 2 * Output messages
192 lines
8.9 KiB
C++
192 lines
8.9 KiB
C++
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
==============================================================================*/
|
|
|
|
#include "tensorflow/lite/micro/micro_allocation_info.h"
|
|
|
|
#include "tensorflow/lite/micro/arena_allocator/single_arena_buffer_allocator.h"
|
|
#include "tensorflow/lite/micro/test_helpers.h"
|
|
#include "tensorflow/lite/micro/testing/micro_test_v2.h"
|
|
|
|
TEST(MicroAllocationInfoTest, TestSingleSubgraph) {
|
|
constexpr int kArenaSize = 1024;
|
|
uint8_t arena[kArenaSize];
|
|
const tflite::Model* model = tflite::testing::GetSimpleMockModel();
|
|
tflite::SingleArenaBufferAllocator allocator(arena, kArenaSize);
|
|
tflite::AllocationInfoBuilder builder(model, &allocator);
|
|
builder.CreateAllocationInfo(0);
|
|
tflite::MicroAllocator* micro_allocator =
|
|
tflite::MicroAllocator::Create(arena, kArenaSize);
|
|
tflite::SubgraphAllocations* subgraph_allocations =
|
|
micro_allocator->StartModelAllocation(model);
|
|
builder.InitializeAllocationInfo(nullptr, subgraph_allocations);
|
|
builder.MarkAllocationLifetimes(0, nullptr, nullptr, subgraph_allocations);
|
|
EXPECT_EQ(builder.AllocationCount(), 4);
|
|
tflite::AllocationInfo* allocation_info = builder.Finish();
|
|
EXPECT_EQ(allocation_info[0].first_created, 0);
|
|
EXPECT_EQ(allocation_info[0].last_used, 2);
|
|
EXPECT_EQ(allocation_info[1].first_created, -1);
|
|
EXPECT_EQ(allocation_info[1].last_used, 2);
|
|
EXPECT_EQ(allocation_info[2].first_created, 1);
|
|
EXPECT_EQ(allocation_info[2].last_used, 2);
|
|
EXPECT_EQ(allocation_info[3].first_created, 2);
|
|
EXPECT_EQ(allocation_info[3].last_used, 2);
|
|
}
|
|
|
|
TEST(MicroAllocationInfoTest, TestSingleSubgraphWithIntermediates) {
|
|
constexpr int kArenaSize = 1024;
|
|
uint8_t arena[kArenaSize];
|
|
const tflite::Model* model = tflite::testing::GetSimpleStatefulModel();
|
|
tflite::SingleArenaBufferAllocator allocator(arena, kArenaSize);
|
|
tflite::AllocationInfoBuilder builder(model, &allocator);
|
|
builder.CreateAllocationInfo(0);
|
|
tflite::MicroAllocator* micro_allocator =
|
|
tflite::MicroAllocator::Create(arena, kArenaSize);
|
|
tflite::SubgraphAllocations* subgraph_allocations =
|
|
micro_allocator->StartModelAllocation(model);
|
|
builder.InitializeAllocationInfo(nullptr, subgraph_allocations);
|
|
builder.MarkAllocationLifetimes(0, nullptr, nullptr, subgraph_allocations);
|
|
EXPECT_EQ(builder.AllocationCount(), 4);
|
|
tflite::AllocationInfo* allocation_info = builder.Finish();
|
|
EXPECT_EQ(allocation_info[0].first_created, 0);
|
|
EXPECT_EQ(allocation_info[0].last_used, 1);
|
|
EXPECT_EQ(allocation_info[0].needs_allocating, true);
|
|
EXPECT_EQ(allocation_info[1].first_created, 1);
|
|
EXPECT_EQ(allocation_info[1].last_used, 1);
|
|
EXPECT_EQ(allocation_info[1].needs_allocating, true);
|
|
EXPECT_EQ(allocation_info[2].first_created, 1);
|
|
EXPECT_EQ(allocation_info[2].last_used, 1);
|
|
EXPECT_EQ(allocation_info[2].needs_allocating, true);
|
|
EXPECT_EQ(allocation_info[3].first_created, -1);
|
|
EXPECT_EQ(allocation_info[3].last_used, -1);
|
|
EXPECT_EQ(allocation_info[3].needs_allocating, false);
|
|
}
|
|
|
|
TEST(MicroAllocationInfoTest, TestMultiSubgraphWithIf) {
|
|
constexpr int kArenaSize = 1024;
|
|
uint8_t arena[kArenaSize];
|
|
const tflite::Model* model =
|
|
tflite::testing::GetSimpleModelWithSubgraphsAndIf();
|
|
tflite::SingleArenaBufferAllocator allocator(arena, kArenaSize);
|
|
tflite::AllocationInfoBuilder builder(model, &allocator);
|
|
builder.CreateAllocationInfo(0);
|
|
tflite::MicroAllocator* micro_allocator =
|
|
tflite::MicroAllocator::Create(arena, kArenaSize);
|
|
tflite::SubgraphAllocations* subgraph_allocations =
|
|
micro_allocator->StartModelAllocation(model);
|
|
builder.InitializeAllocationInfo(nullptr, subgraph_allocations);
|
|
builder.MarkAllocationLifetimes(0, nullptr, nullptr, subgraph_allocations);
|
|
EXPECT_EQ(builder.AllocationCount(), 10);
|
|
tflite::AllocationInfo* allocation_info = builder.Finish();
|
|
EXPECT_EQ(allocation_info[0].first_created, 0);
|
|
EXPECT_EQ(allocation_info[0].last_used, 5);
|
|
EXPECT_EQ(allocation_info[1].first_created, 0);
|
|
EXPECT_EQ(allocation_info[1].last_used, 5);
|
|
EXPECT_EQ(allocation_info[2].first_created, 0);
|
|
EXPECT_EQ(allocation_info[2].last_used, 5);
|
|
EXPECT_EQ(allocation_info[3].first_created, 1);
|
|
EXPECT_EQ(allocation_info[3].last_used, 5);
|
|
EXPECT_EQ(allocation_info[4].first_created, 2);
|
|
EXPECT_EQ(allocation_info[4].last_used, 3);
|
|
EXPECT_EQ(allocation_info[5].first_created, 2);
|
|
EXPECT_EQ(allocation_info[5].last_used, 3);
|
|
EXPECT_EQ(allocation_info[6].first_created, 3);
|
|
EXPECT_EQ(allocation_info[6].last_used, 3);
|
|
EXPECT_EQ(allocation_info[7].first_created, 4);
|
|
EXPECT_EQ(allocation_info[7].last_used, 5);
|
|
EXPECT_EQ(allocation_info[8].first_created, 4);
|
|
EXPECT_EQ(allocation_info[8].last_used, 5);
|
|
EXPECT_EQ(allocation_info[9].first_created, 5);
|
|
EXPECT_EQ(allocation_info[9].last_used, 5);
|
|
}
|
|
|
|
TEST(MicroAllocationInfoTest, TestMultiSubgraphWithIfAndEmptySubgraph) {
|
|
constexpr int kArenaSize = 1024;
|
|
uint8_t arena[kArenaSize];
|
|
const tflite::Model* model =
|
|
tflite::testing::GetSimpleModelWithIfAndEmptySubgraph();
|
|
tflite::SingleArenaBufferAllocator allocator(arena, kArenaSize);
|
|
tflite::AllocationInfoBuilder builder(model, &allocator);
|
|
builder.CreateAllocationInfo(0);
|
|
tflite::MicroAllocator* micro_allocator =
|
|
tflite::MicroAllocator::Create(arena, kArenaSize);
|
|
tflite::SubgraphAllocations* subgraph_allocations =
|
|
micro_allocator->StartModelAllocation(model);
|
|
builder.InitializeAllocationInfo(nullptr, subgraph_allocations);
|
|
builder.MarkAllocationLifetimes(0, nullptr, nullptr, subgraph_allocations);
|
|
EXPECT_EQ(builder.AllocationCount(), 10);
|
|
tflite::AllocationInfo* allocation_info = builder.Finish();
|
|
EXPECT_EQ(allocation_info[0].first_created, 0);
|
|
EXPECT_EQ(allocation_info[0].last_used, 4);
|
|
EXPECT_EQ(allocation_info[1].first_created, 0);
|
|
EXPECT_EQ(allocation_info[1].last_used, 4);
|
|
EXPECT_EQ(allocation_info[2].first_created, 0);
|
|
EXPECT_EQ(allocation_info[2].last_used, 4);
|
|
EXPECT_EQ(allocation_info[3].first_created, 1);
|
|
EXPECT_EQ(allocation_info[3].last_used, 4);
|
|
EXPECT_EQ(allocation_info[4].first_created, 2);
|
|
EXPECT_EQ(allocation_info[4].last_used, 3);
|
|
EXPECT_EQ(allocation_info[5].first_created, 2);
|
|
EXPECT_EQ(allocation_info[5].last_used, 3);
|
|
EXPECT_EQ(allocation_info[6].first_created, 3);
|
|
EXPECT_EQ(allocation_info[6].last_used, 3);
|
|
EXPECT_EQ(allocation_info[7].first_created, 4);
|
|
EXPECT_EQ(allocation_info[7].last_used, 4);
|
|
EXPECT_EQ(allocation_info[8].first_created, 4);
|
|
EXPECT_EQ(allocation_info[8].last_used, 4);
|
|
EXPECT_EQ(allocation_info[9].first_created, 4);
|
|
EXPECT_EQ(allocation_info[9].last_used, 4);
|
|
}
|
|
|
|
TEST(MicroAllocationInfoTest, TestMultiSubgraphWithIfAndInputSubgraphOverlap) {
|
|
constexpr int kArenaSize = 2048;
|
|
uint8_t arena[kArenaSize];
|
|
const tflite::Model* model =
|
|
tflite::testing::GetModelWithIfAndSubgraphInputTensorOverlap();
|
|
tflite::SingleArenaBufferAllocator allocator(arena, kArenaSize);
|
|
tflite::AllocationInfoBuilder builder(model, &allocator);
|
|
builder.CreateAllocationInfo(0);
|
|
tflite::MicroAllocator* micro_allocator =
|
|
tflite::MicroAllocator::Create(arena, kArenaSize);
|
|
tflite::SubgraphAllocations* subgraph_allocations =
|
|
micro_allocator->StartModelAllocation(model);
|
|
builder.InitializeAllocationInfo(nullptr, subgraph_allocations);
|
|
builder.MarkAllocationLifetimes(0, nullptr, nullptr, subgraph_allocations);
|
|
EXPECT_EQ(builder.AllocationCount(), 11);
|
|
tflite::AllocationInfo* allocation_info = builder.Finish();
|
|
EXPECT_EQ(allocation_info[0].first_created, 0);
|
|
EXPECT_EQ(allocation_info[0].last_used, 5);
|
|
EXPECT_EQ(allocation_info[1].first_created, 0);
|
|
EXPECT_EQ(allocation_info[1].last_used, 5);
|
|
EXPECT_EQ(allocation_info[2].first_created, 0);
|
|
EXPECT_EQ(allocation_info[2].last_used, 6);
|
|
EXPECT_EQ(allocation_info[3].first_created, 1);
|
|
EXPECT_EQ(allocation_info[3].last_used, 6);
|
|
EXPECT_EQ(allocation_info[4].first_created, 6);
|
|
EXPECT_EQ(allocation_info[4].last_used, 6);
|
|
EXPECT_EQ(allocation_info[5].first_created, 2);
|
|
EXPECT_EQ(allocation_info[5].last_used, 3);
|
|
EXPECT_EQ(allocation_info[6].first_created, 2);
|
|
EXPECT_EQ(allocation_info[6].last_used, 3);
|
|
EXPECT_EQ(allocation_info[7].first_created, 3);
|
|
EXPECT_EQ(allocation_info[7].last_used, 3);
|
|
EXPECT_EQ(allocation_info[8].first_created, 4);
|
|
EXPECT_EQ(allocation_info[8].last_used, 5);
|
|
EXPECT_EQ(allocation_info[9].first_created, 4);
|
|
EXPECT_EQ(allocation_info[9].last_used, 5);
|
|
EXPECT_EQ(allocation_info[10].first_created, 5);
|
|
EXPECT_EQ(allocation_info[10].last_used, 5);
|
|
}
|
|
|
|
TF_LITE_MICRO_TESTS_MAIN
|