1 /* Copyright 2017 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 #ifndef TENSORFLOW_LITE_SIMPLE_MEMORY_ARENA_H_ 16 #define TENSORFLOW_LITE_SIMPLE_MEMORY_ARENA_H_ 17 18 #include <stddef.h> 19 20 #include <cstdint> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include "tensorflow/lite/c/common.h" 26 27 namespace tflite { 28 29 // This little structure holds the offset and the size for a dynamic memory 30 // allocation in the memory arena as well as first_node and last_node that use 31 // corresponding tensor. It means that continuous part of memory with this size 32 // needs to be allocated before execution of operation in the first node and can 33 // be deallocated after execution of the operation in the last_node. When the 34 // arena is committed and the underlying buffer is set, the alloc can be 35 // resolved into an actual memory pointer. 36 struct ArenaAllocWithUsageInterval { ArenaAllocWithUsageIntervalArenaAllocWithUsageInterval37 ArenaAllocWithUsageInterval() { reset(); } 38 39 size_t offset; 40 size_t size; 41 int32_t tensor; 42 int32_t first_node; 43 int32_t last_node; 44 resetArenaAllocWithUsageInterval45 inline void reset() { 46 offset = 0; 47 size = 0; 48 tensor = -1; 49 first_node = -1; 50 last_node = -1; 51 } 52 53 inline bool operator<(const ArenaAllocWithUsageInterval& other) const { 54 return offset < other.offset; 55 } 56 }; 57 58 // This small class is responsible for allocating, deallocating and reusing 59 // dynamic memory from a common underlying buffer. The arena can be used in 60 // scenarios when the pattern of memory allocations and deallocations is 61 // repetitive, e.g. running NN inference in multiple iterations. Note that 62 // zero-sized allocations are explicitly allowed, and will resolve to null. 63 class SimpleMemoryArena { 64 public: SimpleMemoryArena(size_t arena_alignment)65 explicit SimpleMemoryArena(size_t arena_alignment) 66 : committed_(false), 67 arena_alignment_(arena_alignment), 68 high_water_mark_(0), 69 underlying_buffer_size_(0), 70 ordered_allocs_() {} 71 72 // Schedule memory allocation for a tensor with a given size, assuming that it 73 // needs to be allocated before the execution of first_node, and deallocated 74 // after the execution of last_node. 75 TfLiteStatus Allocate(TfLiteContext* context, size_t alignment, size_t size, 76 int32_t tensor, int32_t first_node, int32_t last_node, 77 ArenaAllocWithUsageInterval* new_alloc); 78 79 TfLiteStatus Deallocate(TfLiteContext* context, 80 const ArenaAllocWithUsageInterval& alloc); 81 RequiredBufferSize()82 inline size_t RequiredBufferSize() { 83 // Add in a small amount of padding to reduce the chance of resize events 84 // for small allocations. 85 size_t padding = arena_alignment_; 86 return arena_alignment_ + high_water_mark_ + padding; 87 } 88 89 TfLiteStatus Commit(TfLiteContext* context); 90 91 TfLiteStatus ResolveAlloc(TfLiteContext* context, 92 const ArenaAllocWithUsageInterval& alloc, 93 char** output_ptr); 94 95 // This clears allocation details but does not release the underlying buffer. 96 // New allocations should be committed & resolved before using this arena 97 // again. 98 TfLiteStatus ClearPlan(); 99 100 // This releases the underlying buffer but does not clear the allocation plan. 101 // Since all associated pointers are invalidated, the arena cannot be used 102 // again until Commit() is called & tensor allocations are resolved. 103 TfLiteStatus ReleaseBuffer(); 104 GetBufferSize()105 size_t GetBufferSize() { return underlying_buffer_size_; } 106 BasePointer()107 std::intptr_t BasePointer() const { 108 return reinterpret_cast<std::intptr_t>(underlying_buffer_aligned_ptr_); 109 } 110 111 // Dumps the memory allocation information of this memory arena (which could 112 // be differentiated from others by the `name`) against the specified op node 113 // execution plan (i.e. `execution_plan`) for the purpose of debugging. 114 // Note: in order to have minimal binary increase caused by this debug info 115 // dump implementation for the TfLite library, and allow users to plug-in 116 // their own memory planner debugger, we have utilized weak symbols to meet 117 // these two requirementsements. By default, there is no debugging info 118 // dumped. To override this, provide a strong defintion of 119 // tflite::DumpArenaInfo(...) whose weak defintion is in 120 // simple_memory_arena.cc. TfLite provides a sample one as 121 // "lite:simple_memory_arena_debug_dump". When this dep is added to the 122 // program, calling this function will output information of this memory arena 123 // about tenosrs and ops, such as memory arena utilization rate, live tensors 124 // at each op etc. 125 void DumpDebugInfo(const std::string& name, 126 const std::vector<int>& execution_plan) const; 127 128 private: 129 bool committed_; 130 size_t arena_alignment_; 131 size_t high_water_mark_; 132 std::unique_ptr<char[]> underlying_buffer_; 133 size_t underlying_buffer_size_; 134 char* underlying_buffer_aligned_ptr_; 135 std::vector<ArenaAllocWithUsageInterval> ordered_allocs_; 136 }; 137 138 } // namespace tflite 139 140 #endif // TENSORFLOW_LITE_SIMPLE_MEMORY_ARENA_H_ 141