Skip to content

Commit 5af5ed0

Browse files
authored
[ET-VK] Simplify Allocator's buffer creation methods
Differential Revision: D62049778 Pull Request resolved: #5014
1 parent b73312f commit 5af5ed0

File tree

6 files changed

+28
-29
lines changed

6 files changed

+28
-29
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
00e3eea170ce5db8ea9c62ce5e48f13886cd6d20
1+
e4cd76cf8283c8ddbf95674b020fbfcff467cb4b

backends/vulkan/runtime/api/containers/StagingBuffer.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ class StagingBuffer final {
2929
StagingBuffer(
3030
Context* context_p,
3131
const vkapi::ScalarType dtype,
32-
const size_t numel,
33-
const bool gpuonly = false)
32+
const size_t numel)
3433
: context_p_(context_p),
3534
dtype_(dtype),
3635
numel_(numel),
3736
nbytes_(element_size(dtype_) * numel_),
38-
vulkan_buffer_(context_p_->adapter_ptr()->vma().create_storage_buffer(
39-
nbytes_,
40-
gpuonly)) {}
37+
vulkan_buffer_(
38+
context_p_->adapter_ptr()->vma().create_staging_buffer(nbytes_)) {}
4139

4240
StagingBuffer(const StagingBuffer&) = delete;
4341
StagingBuffer& operator=(const StagingBuffer&) = delete;

backends/vulkan/runtime/api/containers/Tensor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ vkapi::VulkanBuffer allocate_buffer(
540540
}
541541

542542
return adapter_ptr->vma().create_storage_buffer(
543-
element_size(dtype) * numel, /*gpu_only = */ true, allocate_memory);
543+
element_size(dtype) * numel, allocate_memory);
544544
}
545545

546546
vTensorStorage::vTensorStorage(

backends/vulkan/runtime/vk_api/memory/Allocator.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,32 +132,34 @@ VulkanImage Allocator::create_image(
132132
allocate_memory);
133133
}
134134

135+
VulkanBuffer Allocator::create_staging_buffer(const VkDeviceSize size) {
136+
const VkBufferUsageFlags buffer_usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
137+
138+
VmaAllocationCreateInfo alloc_create_info = {};
139+
alloc_create_info.flags = DEFAULT_ALLOCATION_STRATEGY;
140+
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
141+
142+
// Staging buffers are accessed by both the CPU and GPU, so set the
143+
// appropriate flags to indicate that the host device will be accessing
144+
// the data from this buffer.
145+
alloc_create_info.flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT;
146+
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
147+
alloc_create_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
148+
alloc_create_info.preferredFlags =
149+
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
150+
151+
return VulkanBuffer(allocator_, size, alloc_create_info, buffer_usage);
152+
}
153+
135154
VulkanBuffer Allocator::create_storage_buffer(
136155
const VkDeviceSize size,
137-
const bool gpu_only,
138156
const bool allocate_memory) {
139157
const VkBufferUsageFlags buffer_usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
140158

141159
VmaAllocationCreateInfo alloc_create_info = {};
142160
alloc_create_info.flags = DEFAULT_ALLOCATION_STRATEGY;
143161
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
144162

145-
// The create storage buffer will be accessed by both the CPU and GPU, so set
146-
// the appropriate flags to indicate that the host device will be accessing
147-
// the data from this buffer.
148-
if (!gpu_only) {
149-
// Deferred memory allocation should only be used for GPU only buffers.
150-
VK_CHECK_COND(
151-
allocate_memory,
152-
"Only GPU-only buffers should use deferred memory allocation");
153-
154-
alloc_create_info.flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT;
155-
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
156-
alloc_create_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
157-
alloc_create_info.preferredFlags = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
158-
VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
159-
}
160-
161163
return VulkanBuffer(
162164
allocator_, size, alloc_create_info, buffer_usage, allocate_memory);
163165
}
@@ -170,9 +172,7 @@ VulkanBuffer Allocator::create_uniform_buffer(const VkDeviceSize size) {
170172

171173
VkBufferUsageFlags buffer_usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
172174

173-
VulkanBuffer uniform_buffer(
174-
allocator_, size, alloc_create_info, buffer_usage);
175-
return uniform_buffer;
175+
return VulkanBuffer(allocator_, size, alloc_create_info, buffer_usage);
176176
}
177177

178178
} // namespace vkapi

backends/vulkan/runtime/vk_api/memory/Allocator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ class Allocator final {
6262
const bool allow_transfer = false,
6363
const bool allocate_memory = true);
6464

65+
VulkanBuffer create_staging_buffer(const VkDeviceSize);
66+
6567
VulkanBuffer create_storage_buffer(
6668
const VkDeviceSize,
67-
const bool gpu_only = true,
6869
const bool allocate_memory = true);
6970

7071
/*

install_requirements.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def python_is_compatible():
9494
# NOTE: If a newly-fetched version of the executorch repo changes the value of
9595
# NIGHTLY_VERSION, you should re-run this script to install the necessary
9696
# package versions.
97-
NIGHTLY_VERSION = "dev20240901"
97+
NIGHTLY_VERSION = "dev20240829"
9898

9999
# The pip repository that hosts nightly torch packages.
100100
TORCH_NIGHTLY_URL = "https://download.pytorch.org/whl/nightly/cpu"

0 commit comments

Comments
 (0)