-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathallocator.cpp
More file actions
244 lines (214 loc) · 9.08 KB
/
allocator.cpp
File metadata and controls
244 lines (214 loc) · 9.08 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright (c) 2025, IST Austria, developed by Erik Schultheis
// SPDX-License-Identifier: Apache-2.0
//
#include "allocator.h"
#include <iostream>
#include <unordered_map>
#include <fmt/core.h>
#include "utilities/gpu_info.h"
TensorAllocator::TensorAllocator(TensorAllocator&&) noexcept = default;
TensorAllocator& TensorAllocator::operator=(TensorAllocator&&) noexcept = default;
struct sTotalAllocations {
long ON_DEVICE = 0;
long MANAGED = 0;
long PINNED = 0;
long ON_HOST = 0;
long WRITE_CMB = 0;
long& operator[](EAllocationType kind)
{
switch (kind) {
case EAllocationType::ON_DEVICE: return ON_DEVICE;
case EAllocationType::MANAGED: return MANAGED;
case EAllocationType::PINNED: return PINNED;
case EAllocationType::WRITE_CMB: return WRITE_CMB;
case EAllocationType::ON_HOST: return ON_HOST;
default: throw std::logic_error("Unknown allocation type");
}
}
};
struct TensorAllocator::sAllocStats
{
std::string Context = "";
std::unordered_map<std::string, sTotalAllocations> TensorStats;
std::unordered_map<std::string, sTotalAllocations> ContextStats;
};
template<typename Container>
Tensor allocate_tensor(ETensorDType dtype, EAllocationType kind, const Container& shape)
{
if(shape.size() > MAX_TENSOR_DIM) {
throw std::runtime_error("Tensor rank too large");
}
int did;
CUDA_CHECK(cudaGetDevice(&did));
int rank = narrow<int>(shape.size());
std::size_t total = std::accumulate(std::begin(shape), std::end(shape), 1l, std::multiplies<>());
std::byte* ptr;
if(kind == EAllocationType::ON_DEVICE) {
CUDA_CHECK(cudaMalloc(reinterpret_cast<void**>(&ptr), total * get_dtype_size(dtype)));
} else if(kind == EAllocationType::MANAGED) {
CUDA_CHECK(cudaMallocManaged(reinterpret_cast<void**>(&ptr), total * get_dtype_size(dtype)));
} else if(kind == EAllocationType::PINNED) {
CUDA_CHECK(cudaMallocHost(reinterpret_cast<void**>(&ptr), total * get_dtype_size(dtype)));
did = -1;
} else if(kind == EAllocationType::WRITE_CMB) {
CUDA_CHECK(cudaHostAlloc(reinterpret_cast<void**>(&ptr), total * get_dtype_size(dtype), cudaHostAllocWriteCombined | cudaHostAllocMapped));
did = -1;
} else {
ptr = new std::byte[total * get_dtype_size(dtype)];
did = -1;
}
std::array<long, MAX_TENSOR_DIM> sizes{};
std::copy(shape.begin(), shape.end(), sizes.begin());
std::fill(sizes.begin() + shape.size(), sizes.end(), 1);
return Tensor{dtype, sizes, ptr, nullptr, rank, did};
}
void record_stats(std::unordered_map<std::string, sTotalAllocations>& target, std::string name, EAllocationType kind, long bytes) {
target[name][kind] += narrow<long>(bytes);
}
Tensor TensorAllocator::allocate(ETensorDType dtype, const char* name, EAllocationType kind, const std::initializer_list<long>& shape) {
return allocate_impl(dtype, name, kind, shape);
}
Tensor TensorAllocator::allocate(ETensorDType dtype, const char* name, EAllocationType kind, const std::vector<long>& shape) {
return allocate_impl(dtype, name, kind, shape);
}
Tensor TensorAllocator::allocate(ETensorDType dtype, const char* name, const std::initializer_list<long>& shape) {
return allocate_impl(dtype, name, EAllocationType::ON_DEVICE, shape);
}
Tensor TensorAllocator::allocate(ETensorDType dtype, const char* name, const std::vector<long>& shape) {
return allocate_impl(dtype, name, EAllocationType::ON_DEVICE, shape);
}
TensorShard TensorAllocator::allocate_shard(ETensorDType dtype, int shard_idx, int num_shards, const char* name, const std::vector<long>& shape, EAllocationType kind) {
std::vector<long> shard_shape(shape);
shard_shape[0] = div_exact(shape[0], (long)num_shards);
return TensorShard(allocate(dtype, name, kind, shard_shape), shard_idx, num_shards, shape);
}
template<typename Container>
Tensor TensorAllocator::allocate_impl(ETensorDType dtype, const char* name, EAllocationType kind, const Container& shape) {
try {
Tensor allocated = allocate_tensor(dtype, kind, shape);
m_Pointers.emplace_back(kind, allocated.Data, allocated.bytes());
record_stats(m_Stats->TensorStats, name, kind, allocated.bytes());
if (!m_Stats->Context.empty()){
record_stats(m_Stats->ContextStats, m_Stats->Context, kind, allocated.bytes());
}
if(mCallback) {
mCallback(m_Stats->Context, name, kind, allocated.bytes());
}
return allocated;
} catch (const cuda_error& error) {
if(error.code == cudaErrorMemoryAllocation) {
print_stats();
std::string shape_str = "[";
for(auto s: shape) {
shape_str += std::to_string(s) + ", ";
}
shape_str.pop_back();
shape_str.pop_back();
shape_str.push_back(']');
std::string message = fmt::format("Cuda OOM when allocating tensor {} of shape {} with dtype {} in context {}",
name, shape_str, dtype_to_str(dtype), m_Stats->Context);
throw std::runtime_error(message);
}
throw;
}
}
TensorAllocator::TensorAllocator() : m_Stats(std::make_unique<sAllocStats>()) {
}
TensorAllocator::~TensorAllocator() noexcept {
CUDA_CHECK(cudaDeviceSynchronize());
int did;
CUDA_CHECK(cudaGetDevice(&did));
for (auto& ptr: m_Pointers) {
try {
switch (ptr.Kind) {
case EAllocationType::ON_DEVICE:
case EAllocationType::MANAGED:
CUDA_CHECK(cudaFree(ptr.Pointer));
break;
case EAllocationType::WRITE_CMB:
case EAllocationType::PINNED:
CUDA_CHECK(cudaFreeHost(ptr.Pointer));
break;
case EAllocationType::ON_HOST:
delete[] ptr.Pointer;
break;
}
} catch (const cuda_error& error) {
const char* kind_str;
switch (ptr.Kind) {
case EAllocationType::ON_DEVICE: kind_str = "device"; break;
case EAllocationType::MANAGED: kind_str = "managed"; break;
case EAllocationType::PINNED: kind_str = "pinned"; break;
case EAllocationType::ON_HOST: kind_str = "host"; break;
default: kind_str = "unknown"; break;
}
fprintf(stderr, "Cuda error on device %d when deleting allocation %p [%s of size %ld]: %s\n", did, ptr.Pointer, kind_str, ptr.Size, error.what());
fflush(stderr);
std::terminate();
}
}
}
void TensorAllocator::print_stats() const {
for (auto& [name, amount]: m_Stats->TensorStats)
{
if (amount.ON_DEVICE < total_allocation() / 1024) continue; // skip tiny tensors
if (amount.ON_DEVICE >= 1024 * 1024 * 20) {
std::cerr << name << ": " << amount.ON_DEVICE / 1024 / 1024 << " MiB\n";
} else {
std::cerr << name << ": " << amount.ON_DEVICE / 1024 << " KiB\n";
}
}
}
std::size_t TensorAllocator::total_allocation() const {
std::size_t total = 0;
for(const auto& ptr: m_Pointers) {
total += ptr.Size;
}
return total;
}
std::size_t TensorAllocator::total_allocation(EAllocationType kind) const {
std::size_t total = 0;
for(const auto& ptr: m_Pointers) {
if(ptr.Kind == kind) {
total += ptr.Size;
}
}
return total;
}
void TensorAllocator::set_context(const std::string& ctx) {
m_Stats->Context = ctx;
}
const std::string& TensorAllocator::get_context() const {
return m_Stats->Context;
}
TensorAllocator::AllocationMonitor::AllocationMonitor(const std::string& name, TensorAllocator* alloc) :
mName(name), mAllocator(alloc), mParent(alloc->get_context()) {
alloc->set_context(mName);
}
TensorAllocator::AllocationMonitor::~AllocationMonitor() {
if (mAllocator->get_context() != mName) {
throw std::runtime_error("AllocationMonitor: Improper nesting");
}
mAllocator->set_context(mParent);
}
std::vector<std::pair<std::string, sSegmentMemory>> TensorAllocator::get_allocation_segments() const {
long sum = 0;
std::vector<std::pair<std::string, sSegmentMemory>> segments;
for (const auto& [name, amount]: m_Stats->ContextStats) {
segments.emplace_back(name, sSegmentMemory{amount.ON_DEVICE, amount.MANAGED, amount.PINNED + amount.WRITE_CMB, amount.ON_HOST});
sum += amount.ON_DEVICE;
}
std::size_t free = 0;
std::size_t total = 0;
long reserved = get_mem_reserved();
CUDA_CHECK(cudaMemGetInfo(&free, &total));
segments.emplace_back("Free", sSegmentMemory{(long)free, 0, 0, 0});
if(reserved > 0) {
segments.emplace_back("Reserved", sSegmentMemory{reserved, 0, 0, 0});
}
segments.emplace_back("Other", sSegmentMemory{(long)total - (long)free - sum - reserved, 0, 0, 0});
return segments;
}
void TensorAllocator::set_callback(std::function<void(const std::string&, const std::string&, EAllocationType, std::size_t)> cb) {
mCallback = std::move(cb);
}