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_ARENA_PLANNER_H_
16 #define TENSORFLOW_LITE_ARENA_PLANNER_H_
17 
18 #include <cstdint>
19 #include <memory>
20 #include <vector>
21 
22 #include "tensorflow/lite/c/common.h"
23 #include "tensorflow/lite/graph_info.h"
24 #include "tensorflow/lite/memory_planner.h"
25 #include "tensorflow/lite/simple_memory_arena.h"
26 #include "tensorflow/lite/util.h"
27 
28 namespace tflite {
29 
30 constexpr const int kDefaultArenaAlignment = 64;
31 struct AllocationInfo;
32 
33 // A memory planner that makes all the allocations using arenas.
34 //
35 // Before a model is executed by the interpreter, this class determines when
36 // each tensor needs to be allocated and deallocated, and preallocates all the
37 // necessary memory (the PlanAllocations phase). It then assigns portions of
38 // this memory buffer to each tensor (the ExecuteAllocations phase). Tensors may
39 // share some of the buffer if a tensor B is to be allocated after another
40 // tensor A has been deallocated.
41 //
42 // If dynamic tensors are used the planning steps can be repeated during model
43 // execution. Since dynamic tensors don't have sizes until after the
44 // corresponding operation is executed, this class supports incremental
45 // planning.
46 class ArenaPlanner : public MemoryPlanner {
47  public:
48   // Ownership of 'context' is not taken and it must remain util the
49   // ArenaPlanner is destroyed. The inputs to the graph will not share
50   // memory with any other tensor, effectively preserving them until the end
51   // of inference.
52   ArenaPlanner(TfLiteContext* context, std::unique_ptr<GraphInfo> graph_info,
53                bool preserve_all_tensors, int tensor_alignment);
54   ~ArenaPlanner() override;
55   ArenaPlanner(const ArenaPlanner&) = delete;
56   ArenaPlanner& operator=(const ArenaPlanner&) = delete;
57 
58   TfLiteStatus ResetAllocations() override;
59   TfLiteStatus ResetAllocationsAfter(int node) override;
60   TfLiteStatus PlanAllocations() override;
61   TfLiteStatus ExecuteAllocations(int first_node, int last_node) override;
62   TfLiteStatus ReleaseNonPersistentMemory() override;
63   TfLiteStatus AcquireNonPersistentMemory() override;
64   bool HasNonPersistentMemory() override;
65 
66   // Returns the base arena location for a given allocation type.
67   std::intptr_t BasePointer(TfLiteAllocationType type);
68 
69  private:
70   // Make sure all the arenas have reserved enough memory to store all their
71   // tensors.
72   TfLiteStatus Commit();
73 
74   // Returns vector of tensor number ordered by the following algorithm.
75   // Comparator to sort tensors for the allocation algorithm:
76   // - Tensors that have lifespan through the whole model inference time go
77   // first;
78   // - Other tensors (e.g. intermediate and temporary ones) are sorted in
79   // non-increasing order of their size. If sizes of two tensors are equal, the
80   // one that needs to be allocated earlier goes first.
81   std::vector<int32_t> CreateTensorAllocationVector(int first_node,
82                                                     int last_node);
83 
84   // Traverse the allocation queue and reserve space in the appropriate arena
85   // for all tensors affected by ops in the interval [first_node, last_node].
86   TfLiteStatus CalculateAllocations(int first_node, int last_node);
87 
88   // Assign absolute memory location to a tensor, based on its relative
89   // position inside the corresponding arena buffer.
90   TfLiteStatus ResolveTensorAllocation(int tensor_index);
91 
92   // Register an allocation for all internal (temporary) tensors of
93   // 'node_index'.
94   TfLiteStatus CalculateAllocationOfInternalTensors(int node_index);
95 
96   // Register a deallocation for all internal (temporary) tensors of
97   // 'node_index'.
98   TfLiteStatus CalculateDeallocationOfInternalTensors(int node_index);
99 
100   TfLiteContext* context_;
101   std::unique_ptr<GraphInfo> graph_info_;
102 
103   // Stores allocation data for all tensors.
104   std::vector<ArenaAllocWithUsageInterval> allocs_;
105 
106   // First node, that uses the tensor. It needs to be allocated before
107   // execution of the node's operation.
108   std::vector<int32_t> alloc_node_;
109 
110   // Last node, that uses the tensor. It can be deallocated after execution of
111   // the node's operation.
112   std::vector<int32_t> dealloc_node_;
113 
114   // Raw memory buffer that is allocated for all temporary and graph outputs
115   // that are declared kTfLiteArenaRw.
116   SimpleMemoryArena arena_;
117 
118   // Raw memory buffer that is allocated for persistent tensors that are
119   // declared as kTfLiteArenaRwPersistent.
120   SimpleMemoryArena persistent_arena_;
121 
122   // If true, then no overlapping of memory areas is done, meaning intermediate
123   // tensors and temporary tensors can be queried after running.
124   // (modulo running delegates)
125   bool preserve_all_tensors_;
126 
127   // Number of bytes that tensor buffers should be aligned to.
128   int tensor_alignment_;
129 };
130 
131 }  // namespace tflite
132 
133 #endif  // TENSORFLOW_LITE_ARENA_PLANNER_H_
134