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_DELEGATES_GPU_COMMON_MEMORY_MANAGEMENT_INTERNAL_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_MEMORY_MANAGEMENT_INTERNAL_H_ 18 19 #include <stddef.h> 20 21 #include <limits> 22 #include <vector> 23 24 #include "absl/memory/memory.h" 25 #include "tensorflow/lite/delegates/gpu/common/memory_management/types.h" 26 #include "tensorflow/lite/delegates/gpu/common/types.h" 27 28 namespace tflite { 29 namespace gpu { 30 31 const size_t kNotAssigned = std::numeric_limits<size_t>::max(); 32 33 // This structure is used to save the initial indices of usage records after 34 // they are sorted. 35 template <typename TensorSizeT> 36 struct TensorUsageWithIndex { 37 const TensorUsageRecord<TensorSizeT>* usage_record; 38 size_t idx; 39 TensorUsageWithIndexTensorUsageWithIndex40 TensorUsageWithIndex(const TensorUsageRecord<TensorSizeT>* usage_record, 41 size_t idx) 42 : usage_record(usage_record), idx(idx) {} 43 }; 44 45 bool CompareBySize(const TensorUsageWithIndex<size_t>& first, 46 const TensorUsageWithIndex<size_t>& second); 47 48 // TaskProfile is a vector with information about all intermediate tensors, that 49 // should exist in memory during the execution of the task. Elements of the 50 // vector must be sorted in non-increasing order of corresponding tensors sizes. 51 using TaskProfile = std::vector<TensorUsageWithIndex<size_t>>; 52 53 // Size of object, that covers both input objects (2-dimensional case). 54 bool IsCoveringObject(const uint2& first_object, const uint2& second_object); 55 56 // Size of object, that covers both input objects (3-dimensional case). 57 bool IsCoveringObject(const uint3& first_object, const uint3& second_object); 58 59 // Difference between two objects in elements count (1-dimensional case). 60 size_t AbsDiffInElements(const size_t first_size, const size_t second_size); 61 62 // Difference between two objects in elements count (2-dimensional case). 63 size_t AbsDiffInElements(const uint2& first_size, const uint2& second_size); 64 65 // Difference between two objects in elements count (3-dimensional case). 66 size_t AbsDiffInElements(const uint3& first_size, const uint3& second_size); 67 68 template <typename ObjectSizeT> 69 struct PoolRecord { PoolRecordPoolRecord70 PoolRecord(ObjectSizeT size, size_t obj_id) 71 : object_size(size), object_id(obj_id) {} 72 73 // Objects in pool are ordered by size. 74 bool operator<(const PoolRecord& other) const { 75 return (object_size < other.object_size) || 76 (object_size == other.object_size && object_id < other.object_id); 77 } 78 79 ObjectSizeT object_size; 80 size_t object_id; 81 }; 82 83 struct QueueRecord { QueueRecordQueueRecord84 QueueRecord(TaskId task_id, size_t obj_id) 85 : last_task(task_id), object_id(obj_id) {} 86 87 // Objects in queue are ordered by last_task. 88 bool operator<(const QueueRecord& other) const { 89 return (last_task > other.last_task) || 90 (last_task == other.last_task && object_id > other.object_id); 91 } 92 93 // Last task, where shared object is used. 94 TaskId last_task; 95 size_t object_id; 96 }; 97 98 // Returns a vector that contains TaskProfile for each task. 99 std::vector<TaskProfile> CalculateTaskProfiles( 100 const std::vector<TensorUsageRecord<size_t>>& usage_records); 101 102 // Iterates over all task profiles to calculate maximum at each position. 103 std::vector<size_t> CalculatePositionalMaximums( 104 const std::vector<TensorUsageRecord<size_t>>& usage_records); 105 106 } // namespace gpu 107 } // namespace tflite 108 109 #endif // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_MEMORY_MANAGEMENT_INTERNAL_H_ 110