-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.cpp
464 lines (396 loc) · 10.6 KB
/
Resource.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <ATen/native/vulkan/api/Resource.h>
#include <ATen/native/vulkan/api/Adapter.h>
namespace at {
namespace native {
namespace vulkan {
namespace api {
namespace {
VmaAllocator create_allocator(
const VkInstance instance,
const VkPhysicalDevice physical_device,
const VkDevice device) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
instance,
"Invalid Vulkan instance!");
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
physical_device,
"Invalid Vulkan physical device!");
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
device,
"Invalid Vulkan device!");
const VmaAllocatorCreateInfo allocator_create_info{
0u,
physical_device,
device,
0u,
nullptr,
nullptr,
1u,
nullptr,
nullptr, // TODO (Ashkan): VULKAN_WRAPPER
nullptr,
instance,
VK_API_VERSION_1_0,
};
VmaAllocator allocator{};
VK_CHECK(vmaCreateAllocator(&allocator_create_info, &allocator));
TORCH_CHECK(allocator, "Invalid VMA allocator!");
return allocator;
}
VmaAllocationCreateInfo create_allocation_create_info(
const VmaMemoryUsage usage) {
return VmaAllocationCreateInfo{
0u, /* VMA_ALLOCATION_CREATE_MAPPED_BIT - MoltenVK Issue #175 */
/* VMA_ALLOCATION_CREATE_STRATEGY_MIN_FRAGMENTATION_BIT */
usage,
0u,
0u,
0u,
VK_NULL_HANDLE,
nullptr,
};
}
void release_buffer(const Resource::Buffer& buffer) {
// Safe to pass null as buffer or allocation.
vmaDestroyBuffer(
buffer.memory.allocator,
buffer.object.handle,
buffer.memory.allocation);
}
void release_image(const Resource::Image& image) {
// Sampler is an immutable object. Its lifetime is managed through the cache.
if (VK_NULL_HANDLE != image.object.view) {
VmaAllocatorInfo allocator_info{};
vmaGetAllocatorInfo(image.memory.allocator, &allocator_info);
vkDestroyImageView(allocator_info.device, image.object.view, nullptr);
}
// Safe to pass null as image or allocation.
vmaDestroyImage(
image.memory.allocator,
image.object.handle,
image.memory.allocation);
}
} // namespace
void* map(const Resource::Memory& memory) {
// Call will be ignored by implementation if the memory type this allocation
// belongs to is not HOST_VISIBLE or is HOST_COHERENT, which is the behavior
// we want.
VK_CHECK(vmaInvalidateAllocation(
memory.allocator, memory.allocation, 0u, VK_WHOLE_SIZE));
void* data = nullptr;
VK_CHECK(vmaMapMemory(memory.allocator, memory.allocation, &data));
return data;
}
Resource::Memory::Scope::Scope(
const VmaAllocator allocator,
const VmaAllocation allocation,
const Access access)
: allocator_(allocator),
allocation_(allocation),
access_(access) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
allocator,
"Invalid VMA allocator!");
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
allocation,
"Invalid VMA allocation!");
}
void Resource::Memory::Scope::operator()(const void* const data) const {
if (C10_UNLIKELY(!data)) {
return;
}
vmaUnmapMemory(allocator_, allocation_);
if (Access::Write == access_) {
// Call will be ignored by implementation if the memory type this allocation
// belongs to is not HOST_VISIBLE or is HOST_COHERENT, which is the behavior
// we want.
VK_CHECK(vmaFlushAllocation(allocator_, allocation_, 0u, VK_WHOLE_SIZE));
}
}
Resource::Image::Sampler::Factory::Factory(const GPU& gpu)
: device_(gpu.device) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
device_,
"Invalid Vulkan device!");
}
typename Resource::Image::Sampler::Factory::Handle
Resource::Image::Sampler::Factory::operator()(
const Descriptor& descriptor) const {
const VkSamplerCreateInfo sampler_create_info{
VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
nullptr,
0u,
descriptor.filter,
descriptor.filter,
descriptor.mipmap_mode,
descriptor.address_mode,
descriptor.address_mode,
descriptor.address_mode,
0.0f,
VK_FALSE,
0.0f,
VK_FALSE,
VK_COMPARE_OP_NEVER,
0.0f,
0.0f,
descriptor.border,
VK_FALSE,
};
VkSampler sampler{};
VK_CHECK(vkCreateSampler(
device_,
&sampler_create_info,
nullptr,
&sampler));
TORCH_CHECK(
sampler,
"Invalid Vulkan image sampler!");
return Handle{
sampler,
Deleter(device_),
};
}
void Resource::Fence::wait(const uint64_t timeout_nanoseconds) {
const VkFence fence = handle(/* add_to_waitlist = */ false);
const auto waitlist_itr = std::find(
pool->fence_.waitlist.cbegin(),
pool->fence_.waitlist.cend(),
fence);
if (pool->fence_.waitlist.cend() != waitlist_itr) {
VK_CHECK(vkWaitForFences(
pool->device_,
1u,
&fence,
VK_TRUE,
timeout_nanoseconds));
VK_CHECK(vkResetFences(
pool->device_,
1u,
&fence));
pool->fence_.waitlist.erase(waitlist_itr);
}
}
Resource::Pool::Pool(const GPU& gpu)
: device_(gpu.device),
allocator_(
create_allocator(
gpu.adapter->runtime->instance(),
gpu.adapter->handle,
device_),
vmaDestroyAllocator),
buffer_{},
image_{
.sampler = Image::Sampler{gpu},
},
fence_{} {
buffer_.pool.reserve(Configuration::kReserve);
image_.pool.reserve(Configuration::kReserve);
fence_.pool.reserve(Configuration::kReserve);
}
Resource::Pool::~Pool() {
try {
purge();
}
catch (...) {
}
}
Resource::Pool::Pool(Pool&& pool)
: device_(std::move(pool.device_)),
allocator_(std::move(pool.allocator_)),
buffer_(std::move(pool.buffer_)),
image_(std::move(pool.image_)),
fence_(std::move(pool.fence_)) {
pool.device_ = VK_NULL_HANDLE;
}
Resource::Pool& Resource::Pool::operator=(Pool&& pool) {
if (&pool != this) {
device_ = std::move(pool.device_);
allocator_ = std::move(pool.allocator_);
buffer_ = std::move(pool.buffer_);
image_ = std::move(pool.image_);
fence_ = std::move(pool.fence_);
pool.device_ = VK_NULL_HANDLE;
};
return *this;
}
Resource::Buffer Resource::Pool::buffer(
const Buffer::Descriptor& descriptor) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
device_ && allocator_,
"This resource pool is in an invalid state! ",
"Potential reason: This resource pool is moved from.");
const VkBufferCreateInfo buffer_create_info{
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
nullptr,
0u,
descriptor.size,
descriptor.usage.buffer,
VK_SHARING_MODE_EXCLUSIVE,
0u,
nullptr,
};
const VmaAllocationCreateInfo allocation_create_info =
create_allocation_create_info(descriptor.usage.memory);
VkBuffer buffer{};
VmaAllocation allocation{};
VmaAllocationInfo allocation_info{};
VK_CHECK(vmaCreateBuffer(
allocator_.get(),
&buffer_create_info,
&allocation_create_info,
&buffer,
&allocation,
&allocation_info));
TORCH_CHECK(buffer, "Invalid Vulkan buffer!");
TORCH_CHECK(allocation, "Invalid VMA allocation!");
buffer_.pool.emplace_back(
Buffer{
Buffer::Object{
buffer,
0u,
descriptor.size,
},
Memory{
allocator_.get(),
allocation,
},
},
&release_buffer);
return buffer_.pool.back().get();
}
Resource::Image Resource::Pool::image(
const Image::Descriptor& descriptor) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
device_ && allocator_,
"This resource pool is in an invalid state! ",
"Potential reason: This resource pool is moved from.");
const VkImageCreateInfo image_create_info{
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
nullptr,
0u,
descriptor.type,
descriptor.format,
descriptor.extent,
1u,
1u,
VK_SAMPLE_COUNT_1_BIT,
VK_IMAGE_TILING_OPTIMAL,
descriptor.usage.image,
VK_SHARING_MODE_EXCLUSIVE,
0u,
nullptr,
VK_IMAGE_LAYOUT_UNDEFINED,
};
const VmaAllocationCreateInfo allocation_create_info =
create_allocation_create_info(descriptor.usage.memory);
VkImage image{};
VmaAllocation allocation{};
VmaAllocationInfo allocation_info{};
VK_CHECK(vmaCreateImage(
allocator_.get(),
&image_create_info,
&allocation_create_info,
&image,
&allocation,
&allocation_info));
TORCH_CHECK(image, "Invalid Vulkan image!");
TORCH_CHECK(allocation, "Invalid VMA allocation!");
const VkImageViewCreateInfo image_view_create_info{
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
nullptr,
0u,
image,
descriptor.view.type,
descriptor.view.format,
{
VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY,
},
{
VK_IMAGE_ASPECT_COLOR_BIT,
0u,
1u,
0u,
1u,
},
};
VkImageView view{};
VK_CHECK(vkCreateImageView(
device_,
&image_view_create_info,
nullptr,
&view));
TORCH_CHECK(
view,
"Invalid Vulkan image view!");
image_.pool.emplace_back(
Image{
Image::Object{
image,
VK_IMAGE_LAYOUT_UNDEFINED,
view,
image_.sampler.cache.retrieve(descriptor.sampler),
},
Memory{
allocator_.get(),
allocation,
},
},
&release_image);
return image_.pool.back().get();
}
Resource::Fence Resource::Pool::fence() {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
device_ && allocator_,
"This resource pool is in an invalid state! ",
"Potential reason: This resource pool is moved from.");
if (fence_.pool.size() == fence_.in_use) {
const VkFenceCreateInfo fence_create_info{
VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
nullptr,
0u,
};
VkFence fence{};
VK_CHECK(vkCreateFence(
device_,
&fence_create_info,
nullptr,
&fence));
TORCH_CHECK(
fence,
"Invalid Vulkan fence!");
fence_.pool.emplace_back(fence, VK_DELETER(Fence)(device_));
}
return Fence{
this,
fence_.in_use++,
};
}
void Resource::Pool::purge() {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
device_ && allocator_,
"This resource pool is in an invalid state! ",
"Potential reason: This resource pool is moved from.");
if (!fence_.waitlist.empty()) {
VK_CHECK(vkWaitForFences(
device_,
fence_.waitlist.size(),
fence_.waitlist.data(),
VK_TRUE,
UINT64_MAX));
VK_CHECK(vkResetFences(
device_,
fence_.waitlist.size(),
fence_.waitlist.data()));
fence_.waitlist.clear();
}
fence_.in_use = 0u;
image_.pool.clear();
buffer_.pool.clear();
}
} // namespace api
} // namespace vulkan
} // namespace native
} // namespace at