1 /* Copyright 2019 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 #ifndef TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_MEMORY_PLANNER_H_ 17 #define TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_MEMORY_PLANNER_H_ 18 19 #include "tensorflow/lite/c/common.h" 20 #include "tensorflow/lite/core/api/error_reporter.h" 21 22 namespace tflite { 23 24 // Interface class for planning the layout of memory buffers during the 25 // execution of a graph. 26 // It's designed to be used by a client that iterates in any order through the 27 // buffers it wants to lay out, and then calls the getter functions for 28 // information about the calculated layout. For example: 29 // 30 // SomeMemoryPlanner planner; 31 // planner.AddBuffer(reporter, 100, 0, 1); // Buffer 0 32 // planner.AddBuffer(reporter, 50, 2, 3); // Buffer 1 33 // planner.AddBuffer(reporter, 50, 2, 3); // Buffer 2 34 // 35 // int offset0; 36 // TF_EXPECT_OK(planner.GetOffsetForBuffer(reporter, 0, &offset0)); 37 // int offset1; 38 // TF_EXPECT_OK(planner.GetOffsetForBuffer(reporter, 1, &offset1)); 39 // int offset2; 40 // TF_EXPECT_OK(planner.GetOffsetForBuffer(reporter, 2, &offset2)); 41 // const int arena_size_needed = planner.GetMaximumMemorySize(); 42 // 43 // The goal is for applications to be able to experiment with different layout 44 // strategies without changing their client code, by swapping out classes that 45 // implement this interface.= 46 class MemoryPlanner { 47 public: MemoryPlanner()48 MemoryPlanner() {} ~MemoryPlanner()49 virtual ~MemoryPlanner() {} 50 51 // Pass information about a buffer's size and lifetime to the layout 52 // algorithm. The order this is called implicitly assigns an index to the 53 // result, so the buffer information that's passed into the N-th call of 54 // this method will be used as the buffer_index argument to 55 // GetOffsetForBuffer(). 56 virtual TfLiteStatus AddBuffer(tflite::ErrorReporter* error_reporter, 57 int size, int first_time_used, 58 int last_time_used) = 0; 59 60 // The largest contiguous block of memory that's needed to hold the layout. 61 virtual size_t GetMaximumMemorySize() = 0; 62 // How many buffers have been added to the planner. 63 virtual int GetBufferCount() = 0; 64 // Calculated layout offset for the N-th buffer added to the planner. 65 virtual TfLiteStatus GetOffsetForBuffer(tflite::ErrorReporter* error_reporter, 66 int buffer_index, int* offset) = 0; 67 }; 68 69 } // namespace tflite 70 71 #endif // TENSORFLOW_LITE_MICRO_MEMORY_PLANNER_MEMORY_PLANNER_H_ 72