ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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
34namespace onert
35{
36namespace backend
37{
38namespace train
39{
40
44template <typename Index> class BumpPlanner : public basic::IMemoryPlanner<Index>
45{
46private:
47 using MemoryPlans = typename basic::IMemoryPlanner<Index>::MemoryPlans;
48
49public:
55 void claim(const Index &, size_t) override;
60 void release(const Index &) override;
65 uint32_t capacity() override { return _capacity; }
70 MemoryPlans &memory_plans() override { return _mem_plans; }
71
72private:
73 uint32_t _capacity = 0;
74 MemoryPlans _mem_plans;
75};
76
80template <typename Index> class FirstFitPlanner : public basic::IMemoryPlanner<Index>
81{
82private:
83 using MemoryPlans = typename basic::IMemoryPlanner<Index>::MemoryPlans;
84
85public:
91 void claim(const Index &, size_t) override;
96 void release(const Index &) override;
101 uint32_t capacity() override { return _capacity; }
106 MemoryPlans &memory_plans() override { return _mem_plans; }
107
108private:
109 uint32_t _capacity = 0;
110 MemoryPlans _mem_plans;
111 // Use std::map because claim() assumes that _claim_table is sorted by uint32_t(base_offset)
112 std::map<uint32_t, Index> _claim_table;
113};
114
118template <typename Index> class WICPlanner : public basic::IMemoryPlanner<Index>
119{
120private:
121 using MemoryPlans = typename basic::IMemoryPlanner<Index>::MemoryPlans;
122
123public:
124 WICPlanner();
125
131 void claim(const Index &, size_t) override;
136 void release(const Index &) override;
141 uint32_t capacity() override
142 {
143 if (!_initialized)
144 buildMemoryPlans();
145 return _capacity;
146 }
151 MemoryPlans &memory_plans() override;
152
153private:
154 void buildMemoryPlans();
155
156 bool _initialized;
157 uint32_t _capacity;
158 MemoryPlans _mem_plans;
159 std::unordered_set<Index> _live_indices;
160 std::unordered_map<Index, std::vector<Index>> _interference_graph;
161 // Sort tensors by descending order of size
162 std::multimap<uint32_t, Index, std::greater<uint32_t>> _indices;
163};
164
165} // namespace train
166} // namespace backend
167} // namespace onert
168
169#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