ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MemoryPlanner.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
22#ifndef __ONERT_BACKEND_TRAIN_MEMORY_PLANNER_H__
23#define __ONERT_BACKEND_TRAIN_MEMORY_PLANNER_H__
24
26
28
29#include <map>
30#include <vector>
31#include <unordered_set>
32#include <memory>
33
35{
36
40template <typename Index> class BumpPlanner : public basic::IMemoryPlanner<Index>
41{
42private:
43 using MemoryPlans = typename basic::IMemoryPlanner<Index>::MemoryPlans;
44
45public:
51 void claim(const Index &, size_t) override;
56 void release(const Index &) override;
61 uint32_t capacity() override { return _capacity; }
66 MemoryPlans &memory_plans() override { return _mem_plans; }
67
68private:
69 uint32_t _capacity = 0;
70 MemoryPlans _mem_plans;
71};
72
76template <typename Index> class FirstFitPlanner : public basic::IMemoryPlanner<Index>
77{
78private:
79 using MemoryPlans = typename basic::IMemoryPlanner<Index>::MemoryPlans;
80
81public:
87 void claim(const Index &, size_t) override;
92 void release(const Index &) override;
97 uint32_t capacity() override { return _capacity; }
102 MemoryPlans &memory_plans() override { return _mem_plans; }
103
104private:
105 uint32_t _capacity = 0;
106 MemoryPlans _mem_plans;
107 // Use std::map because claim() assumes that _claim_table is sorted by uint32_t(base_offset)
108 std::map<uint32_t, Index> _claim_table;
109};
110
114template <typename Index> class WICPlanner : public basic::IMemoryPlanner<Index>
115{
116private:
117 using MemoryPlans = typename basic::IMemoryPlanner<Index>::MemoryPlans;
118
119public:
120 WICPlanner();
121
127 void claim(const Index &, size_t) override;
132 void release(const Index &) override;
137 uint32_t capacity() override
138 {
139 if (!_initialized)
140 buildMemoryPlans();
141 return _capacity;
142 }
147 MemoryPlans &memory_plans() override;
148
149private:
150 void buildMemoryPlans();
151
152 bool _initialized;
153 uint32_t _capacity;
154 MemoryPlans _mem_plans;
155 std::unordered_set<Index> _live_indices;
156 std::unordered_map<Index, std::vector<Index>> _interference_graph;
157 // Sort tensors by descending order of size
158 std::multimap<uint32_t, Index, std::greater<uint32_t>> _indices;
159};
160
161} // namespace onert::backend::train
162
163#endif // __ONERT_BACKEND_TRAIN_MEMORY_PLANNER_H__
Class to plan memory by bump way.
void release(const Index &) override
Release memory for tensor by bump way.
void claim(const Index &, size_t) override
Claim memory for tensor by bump way.
MemoryPlans & memory_plans() override
Get MemoryPlans.
uint32_t capacity() override
Get capacity for memory planning.
Class to plan memory by firstfit way.
void claim(const Index &, size_t) override
Claim memory for tensor by firstfit way.
void release(const Index &) override
Release memory for tensor by firstfit way.
MemoryPlans & memory_plans() override
Get MemoryPlans.
uint32_t capacity() override
Get capacity for memory planning.
Class to plan memory by Weighted Interval Color algorithm.
void release(const Index &) override
Release memory for tensor by WIC algorithm.
MemoryPlans & memory_plans() override
Get MemoryPlans.
uint32_t capacity() override
Get capacity for memory planning.
void claim(const Index &, size_t) override
Claim memory for tensor by WIC algorithm.
std::unordered_map< Index, Block > MemoryPlans