• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CONTRIB_LITE_ARENA_PLANNER_H_
16 #define TENSORFLOW_CONTRIB_LITE_ARENA_PLANNER_H_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "tensorflow/contrib/lite/context.h"
22 #include "tensorflow/contrib/lite/graph_info.h"
23 #include "tensorflow/contrib/lite/memory_planner.h"
24 #include "tensorflow/contrib/lite/simple_memory_arena.h"
25 
26 namespace tflite {
27 
28 class AllocationInfo;
29 
30 // A memory planner that makes all the allocations using arenas.
31 //
32 // Before a model is executed by the interpreter, this class determines when
33 // each tensor needs to be allocated and deallocated, and preallocates all the
34 // necessary memory (the PlanAllocations phase). It then assigns portions of
35 // this memory buffer to each tensor (the ExecuteAllocations phase). Tensors may
36 // share some of the bufer if a tensor B is to be allocated after another tensor
37 // A has been deallocated.
38 //
39 // If dynamic tensors are used the planning steps can be repeated during model
40 // execution. Since dynamic tensors don't have sizes until after the
41 // corresponding operation is executed, this class supports incremental
42 // planning.
43 class ArenaPlanner : public MemoryPlanner {
44  public:
45   // Ownership of 'context' is not taken and it must remain util the
46   // ArenaPlanner is destroyed.
47   ArenaPlanner(TfLiteContext* context, std::unique_ptr<GraphInfo> graph_info);
48   ~ArenaPlanner() override;
49   ArenaPlanner(const ArenaPlanner&) = delete;
50   ArenaPlanner& operator=(const ArenaPlanner&) = delete;
51 
52   TfLiteStatus ResetAllocations() override;
53   TfLiteStatus PlanAllocations() override;
54   TfLiteStatus ExecuteAllocations(int first_node, int last_node) override;
55 
56   // Returns the base arena location for a given allocation type.
57   int64_t BasePointer(TfLiteAllocationType type);
58 
59  private:
60   // Make sure all the arenas have reserved enough memory to store all their
61   // tensors.
62   TfLiteStatus Commit();
63 
64   // Traverse the allocation queue and reserve space in the appropriate arena
65   // for all tensors affected by ops in the interval [first_node, last_node].
66   TfLiteStatus CalculateAllocations(int first_node, int last_node);
67 
68   // Assign absolute memory location to a tensor, based on its relative
69   // position inside the corresponding arena buffer.
70   TfLiteStatus ResolveTensorAllocation(int tensor_index);
71 
72   // Register an allocation for the given tensor.
73   TfLiteStatus CalculateTensorAllocation(int tensor_index);
74 
75   // Register a deallocation for the given tensor.
76   TfLiteStatus CalculateTensorDeallocation(int tensor_index);
77 
78   // Register an allocation for all internal (temporary) tensors of
79   // 'node_index'.
80   TfLiteStatus CalculateAllocationOfInternalTensors(int node_index);
81 
82   // Register a deallocation for all internal (temporary) tensors of
83   // 'node_index'.
84   TfLiteStatus CalculateDeallocationOfInternalTensors(int node_index);
85 
86   TfLiteContext* context_;
87   std::unique_ptr<GraphInfo> graph_info_;
88 
89   // Stores allocation data for all tensors.
90   std::vector<ArenaAlloc> allocs_;
91 
92   // A chronological list of instructions to allocated and deallocate tensors,
93   // reflecting the way they are used in the graph.
94   std::vector<AllocationInfo> alloc_queue_;
95 
96   // Raw memory buffer that is allocated for all temporary and graph outputs.
97   // that are declared kTfLiteArenaRw.
98   SimpleMemoryArena arena_;
99 
100   // Raw memory buffer that is allocated for persistent tensors that are
101   // declared as kTfLiteArenaRwPersistent.
102   SimpleMemoryArena persistent_arena_;
103 };
104 
105 }  // namespace tflite
106 
107 #endif  // TENSORFLOW_CONTRIB_LITE_ARENA_PLANNER_H_
108