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_MEMORY_PLANNER_H_ 16 #define TENSORFLOW_LITE_MEMORY_PLANNER_H_ 17 18 #include <vector> 19 20 #include "tensorflow/lite/c/common.h" 21 22 namespace tflite { 23 24 // A MemoryPlanner is responsible for planning and executing a number of 25 // memory-related operations that are necessary in TF Lite. 26 class MemoryPlanner { 27 public: ~MemoryPlanner()28 virtual ~MemoryPlanner() {} 29 30 // Plans the necessary memory allocations. This is the MemoryPlanner's 31 // pre-processing step and is called when the graph structure is known but 32 // actual size of the tensors is not. 33 virtual TfLiteStatus PlanAllocations() = 0; 34 35 // Allocates the necessary memory to execute all nodes in the interval 36 // [first_node, last_node]. 37 virtual TfLiteStatus ExecuteAllocations(int first_node, int last_node) = 0; 38 39 // Invalidates allocations made earlier. This is called when tensors sizes 40 // have changed. All planned allocations remain, but can't be used until 41 // ExecuteAllocations() is called. 42 virtual TfLiteStatus ResetAllocations() = 0; 43 44 // Invalidates allocations after the given node execution. 45 virtual TfLiteStatus ResetAllocationsAfter(int node) = 0; 46 47 // NOTE: The following two methods modify the data pointers for all tensors on 48 // the non-persistent arena (inputs, outputs, intermediates). If the user has 49 // manually set the pointers for any of these, they would need to be set 50 // again. 51 52 // This releases memory allocated for non-persistent tensors. 53 // It does NOT clear the allocation plan, but the memory can't be used 54 // until AcquireNonPersistentMemory() is called. 55 // It is safe to call Reset/PlanAllocations after this method, without calling 56 // ReleaseTemporaryAllocations in case tensor sizes change. 57 virtual TfLiteStatus ReleaseNonPersistentMemory() = 0; 58 59 // Allocates the necessary memory to contain non-persistent tensors. 60 virtual TfLiteStatus AcquireNonPersistentMemory() = 0; 61 62 // Returns true if the non-persistent memory is available. 63 virtual bool HasNonPersistentMemory() = 0; 64 65 // Dumps the memory planning information against the specified op node 66 // execution plan (i.e. `execution_plan`) for the purpose of debugging. 67 virtual void DumpDebugInfo(const std::vector<int>& execution_plan) const = 0; 68 69 // Returns a map of allocation information. It's only used for debugging. 70 virtual void GetAllocInfo(size_t *arena_size, 71 size_t *arena_persist_size) const = 0; 72 }; 73 74 } // namespace tflite 75 76 #endif // TENSORFLOW_LITE_MEMORY_PLANNER_H_ 77