• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/micro/recording_simple_memory_allocator.h"
17 
18 #include <cstdint>
19 
20 #include "tensorflow/lite/micro/micro_error_reporter.h"
21 #include "tensorflow/lite/micro/test_helpers.h"
22 #include "tensorflow/lite/micro/testing/micro_test.h"
23 
24 TF_LITE_MICRO_TESTS_BEGIN
25 
TF_LITE_MICRO_TEST(TestRecordsTailAllocations)26 TF_LITE_MICRO_TEST(TestRecordsTailAllocations) {
27   constexpr size_t arena_size = 1024;
28   uint8_t arena[arena_size];
29   tflite::RecordingSimpleMemoryAllocator allocator(
30       tflite::GetMicroErrorReporter(), arena, arena_size);
31 
32   uint8_t* result = allocator.AllocateFromTail(/*size=*/10, /*alignment=*/1);
33   TF_LITE_MICRO_EXPECT_NE(result, nullptr);
34   TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), static_cast<size_t>(10));
35   TF_LITE_MICRO_EXPECT_EQ(allocator.GetRequestedBytes(),
36                           static_cast<size_t>(10));
37   TF_LITE_MICRO_EXPECT_EQ(allocator.GetAllocatedCount(),
38                           static_cast<size_t>(1));
39 
40   result = allocator.AllocateFromTail(/*size=*/20, /*alignment=*/1);
41   TF_LITE_MICRO_EXPECT_NE(result, nullptr);
42   TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), static_cast<size_t>(30));
43   TF_LITE_MICRO_EXPECT_EQ(allocator.GetRequestedBytes(),
44                           static_cast<size_t>(30));
45   TF_LITE_MICRO_EXPECT_EQ(allocator.GetAllocatedCount(),
46                           static_cast<size_t>(2));
47 }
48 
TF_LITE_MICRO_TEST(TestRecordsMisalignedTailAllocations)49 TF_LITE_MICRO_TEST(TestRecordsMisalignedTailAllocations) {
50   constexpr size_t arena_size = 1024;
51   uint8_t arena[arena_size];
52   tflite::RecordingSimpleMemoryAllocator allocator(
53       tflite::GetMicroErrorReporter(), arena, arena_size);
54 
55   uint8_t* result = allocator.AllocateFromTail(/*size=*/10, /*alignment=*/12);
56   TF_LITE_MICRO_EXPECT_NE(result, nullptr);
57   // Validate used bytes in 8 byte range that can included alignment of 12:
58   TF_LITE_MICRO_EXPECT_GE(allocator.GetUsedBytes(), static_cast<size_t>(10));
59   TF_LITE_MICRO_EXPECT_LE(allocator.GetUsedBytes(), static_cast<size_t>(20));
60   TF_LITE_MICRO_EXPECT_EQ(allocator.GetRequestedBytes(),
61                           static_cast<size_t>(10));
62   TF_LITE_MICRO_EXPECT_EQ(allocator.GetAllocatedCount(),
63                           static_cast<size_t>(1));
64 }
65 
TF_LITE_MICRO_TEST(TestDoesNotRecordFailedTailAllocations)66 TF_LITE_MICRO_TEST(TestDoesNotRecordFailedTailAllocations) {
67   constexpr size_t arena_size = 1024;
68   uint8_t arena[arena_size];
69   tflite::RecordingSimpleMemoryAllocator allocator(
70       tflite::GetMicroErrorReporter(), arena, arena_size);
71 
72   uint8_t* result = allocator.AllocateFromTail(/*size=*/2048, /*alignment=*/1);
73   TF_LITE_MICRO_EXPECT(result == nullptr);
74   TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), static_cast<size_t>(0));
75   TF_LITE_MICRO_EXPECT_EQ(allocator.GetRequestedBytes(),
76                           static_cast<size_t>(0));
77   TF_LITE_MICRO_EXPECT_EQ(allocator.GetAllocatedCount(),
78                           static_cast<size_t>(0));
79 }
80 
TF_LITE_MICRO_TEST(TestRecordsHeadSizeAdjustment)81 TF_LITE_MICRO_TEST(TestRecordsHeadSizeAdjustment) {
82   constexpr size_t arena_size = 1024;
83   uint8_t arena[arena_size];
84   tflite::RecordingSimpleMemoryAllocator allocator(
85       tflite::GetMicroErrorReporter(), arena, arena_size);
86 
87   TF_LITE_MICRO_EXPECT_EQ(
88       kTfLiteOk, allocator.SetHeadBufferSize(/*size=*/5, /*alignment=*/1));
89   TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), static_cast<size_t>(5));
90   TF_LITE_MICRO_EXPECT_EQ(allocator.GetRequestedBytes(),
91                           static_cast<size_t>(5));
92   // Head adjustments do not count as an allocation:
93   TF_LITE_MICRO_EXPECT_EQ(allocator.GetAllocatedCount(),
94                           static_cast<size_t>(0));
95 
96   uint8_t* result = allocator.AllocateFromTail(/*size=*/15, /*alignment=*/1);
97   TF_LITE_MICRO_EXPECT_NE(result, nullptr);
98   TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), static_cast<size_t>(20));
99   TF_LITE_MICRO_EXPECT_EQ(allocator.GetRequestedBytes(),
100                           static_cast<size_t>(20));
101   TF_LITE_MICRO_EXPECT_EQ(allocator.GetAllocatedCount(),
102                           static_cast<size_t>(1));
103 }
104 
TF_LITE_MICRO_TEST(TestRecordsMisalignedHeadSizeAdjustments)105 TF_LITE_MICRO_TEST(TestRecordsMisalignedHeadSizeAdjustments) {
106   constexpr size_t arena_size = 1024;
107   uint8_t arena[arena_size];
108   tflite::RecordingSimpleMemoryAllocator allocator(
109       tflite::GetMicroErrorReporter(), arena, arena_size);
110 
111   TF_LITE_MICRO_EXPECT_EQ(
112       kTfLiteOk, allocator.SetHeadBufferSize(/*size=*/10, /*alignment=*/12));
113   // Validate used bytes in 8 byte range that can included alignment of 12:
114   TF_LITE_MICRO_EXPECT_GE(allocator.GetUsedBytes(), static_cast<size_t>(10));
115   TF_LITE_MICRO_EXPECT_LE(allocator.GetUsedBytes(), static_cast<size_t>(20));
116   TF_LITE_MICRO_EXPECT_EQ(allocator.GetRequestedBytes(),
117                           static_cast<size_t>(10));
118   // Head adjustments do not count as an allocation:
119   TF_LITE_MICRO_EXPECT_EQ(allocator.GetAllocatedCount(),
120                           static_cast<size_t>(0));
121 }
122 
TF_LITE_MICRO_TEST(TestDoesNotRecordFailedTailAllocations)123 TF_LITE_MICRO_TEST(TestDoesNotRecordFailedTailAllocations) {
124   constexpr size_t arena_size = 1024;
125   uint8_t arena[arena_size];
126   tflite::RecordingSimpleMemoryAllocator allocator(
127       tflite::GetMicroErrorReporter(), arena, arena_size);
128 
129   TF_LITE_MICRO_EXPECT_EQ(kTfLiteError, allocator.SetHeadBufferSize(
130                                             /*size=*/2048, /*alignment=*/1));
131   TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), static_cast<size_t>(0));
132   TF_LITE_MICRO_EXPECT_EQ(allocator.GetRequestedBytes(),
133                           static_cast<size_t>(0));
134   TF_LITE_MICRO_EXPECT_EQ(allocator.GetAllocatedCount(),
135                           static_cast<size_t>(0));
136 }
137 
138 TF_LITE_MICRO_TESTS_END
139