-
Notifications
You must be signed in to change notification settings - Fork 966
Expand file tree
/
Copy pathmemory_manager.h
More file actions
169 lines (155 loc) · 6.1 KB
/
memory_manager.h
File metadata and controls
169 lines (155 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <executorch/runtime/core/hierarchical_allocator.h>
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/core/portable_type/device.h>
#include <executorch/runtime/core/span.h>
namespace executorch {
namespace runtime {
/**
* A container class for allocators used during Method load and execution.
*
* This class consolidates all dynamic memory needs for Method load and
* execution. This can allow for heap-based as well as heap-less execution
* (relevant to some embedded scenarios), and overall provides more control over
* memory use.
*
* This class, however, cannot ensure all allocation is accounted for since
* kernel and backend implementations are free to use a separate way to allocate
* memory (e.g., for things like scratch space). But we do suggest that backends
* and kernels use these provided allocators whenever possible.
*/
class MemoryManager final {
public:
/**
* Constructs a new MemoryManager.
*
* @param[in] method_allocator The allocator to use when loading a Method and
* allocating its internal structures. Must outlive the Method that uses
* it.
* @param[in] planned_memory The memory-planned buffers to use for mutable
* tensor data when executing a Method. Must outlive the Method that uses
* it. May be `nullptr` if the Method does not use any memory-planned
* tensor data. The sizes of the buffers in this HierarchicalAllocator
* must agree with the corresponding
* `MethodMeta::num_memory_planned_buffers()` and
* `MethodMeta::memory_planned_buffer_size(N)` values, which are embedded
* in the Program.
* @param[in] temp_allocator The allocator to use when allocating temporary
* data during kernel or delegate execution. Must outlive the Method that
* uses it. May be `nullptr` if the Method does not use kernels or
* delegates that allocate temporary data. This allocator will be reset
* after every kernel or delegate call during execution.
*/
explicit MemoryManager(
MemoryAllocator* method_allocator,
HierarchicalAllocator* planned_memory = nullptr,
MemoryAllocator* temp_allocator = nullptr)
: method_allocator_(method_allocator),
planned_memory_(planned_memory),
temp_allocator_(temp_allocator) {
ET_CHECK_MSG(
method_allocator != temp_allocator,
"method allocator cannot be the same as temp allocator");
}
/**
* Constructs a new MemoryManager with per-buffer device metadata.
*
* @param[in] method_allocator Same as above.
* @param[in] planned_memory Same as above. May contain a mix of CPU and
* device pointers — HierarchicalAllocator only does pointer arithmetic,
* so device pointers are valid.
* @param[in] temp_allocator Same as above.
* @param[in] planned_buffer_devices One entry per planned memory buffer
* (same count as planned_memory buffers), indicating the device type for
* each buffer. For CPU-only programs, use the 3-arg constructor instead.
*/
MemoryManager(
MemoryAllocator* method_allocator,
HierarchicalAllocator* planned_memory,
MemoryAllocator* temp_allocator,
Span<const etensor::DeviceType> planned_buffer_devices)
: method_allocator_(method_allocator),
planned_memory_(planned_memory),
temp_allocator_(temp_allocator),
planned_buffer_devices_(planned_buffer_devices) {
ET_CHECK_MSG(
method_allocator != temp_allocator,
"method allocator cannot be the same as temp allocator");
}
/**
* DEPRECATED: Use the constructor without `constant_allocator` instead.
*
* TODO(T162089316): Remove this once all users migrate to the new ctor.
*/
ET_DEPRECATED MemoryManager(
MemoryAllocator* constant_allocator,
HierarchicalAllocator* non_constant_allocator,
MemoryAllocator* runtime_allocator,
MemoryAllocator* temporary_allocator)
: MemoryManager(
/*method_allocator=*/runtime_allocator,
/*planned_memory=*/non_constant_allocator,
/*temp_allocator=*/temporary_allocator) {
(void)constant_allocator; // Suppress unused variable warning
}
/**
* Returns the allocator that the runtime will use to allocate internal
* structures while loading a Method. Must not be used after its associated
* Method has been loaded.
*/
MemoryAllocator* method_allocator() const {
return method_allocator_;
}
/**
* Returns the memory-planned buffers to use for mutable tensor data.
*/
HierarchicalAllocator* planned_memory() const {
return planned_memory_;
}
/**
* Returns the allocator to use for allocating temporary data during kernel or
* delegate execution.
*
* This allocator will be reset after every kernel or delegate call during
* execution.
*/
MemoryAllocator* temp_allocator() const {
return temp_allocator_;
}
/**
* Returns per-buffer device metadata. One entry per planned memory buffer,
* same count as planned_memory buffers. Empty if no device metadata was
* provided (CPU-only program).
*/
Span<const etensor::DeviceType> planned_buffer_devices() const {
return planned_buffer_devices_;
}
/**
* Returns true if any planned buffer is on a non-CPU device.
* When false, the memory setup is CPU-only and follows the legacy path.
*/
bool has_device_memory() const {
return planned_buffer_devices_.size() > 0;
}
private:
MemoryAllocator* method_allocator_;
HierarchicalAllocator* planned_memory_;
MemoryAllocator* temp_allocator_;
Span<const etensor::DeviceType> planned_buffer_devices_;
};
} // namespace runtime
} // namespace executorch
namespace torch {
namespace executor {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::runtime::MemoryManager;
} // namespace executor
} // namespace torch