Skip to content

Commit 9a2d135

Browse files
authored
Speed up device-to-host staging reads (pytorch#22023)
Differential Revision: D116964600 Pull Request resolved: pytorch#22023
1 parent b29a04a commit 9a2d135

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎backends/vulkan/runtime/api/containers/StagingBuffer.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ void StagingBuffer::cast_float_to_half_and_copy_to(
178178
vulkan_buffer_.vma_allocator(),
179179
vulkan_buffer_.allocation(),
180180
0u,
181-
VK_WHOLE_SIZE);
181+
numel * sizeof(float));
182182
const float* src = reinterpret_cast<const float*>(data());
183183
for (size_t i = 0; i < numel; ++i) {
184184
dst[i] = float_to_half(src[i]);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class StagingBuffer final {
107107
vulkan_buffer_.vma_allocator(),
108108
vulkan_buffer_.allocation(),
109109
0u,
110-
VK_WHOLE_SIZE);
110+
nbytes);
111111
memcpy(dst, data(), nbytes);
112112
}
113113

@@ -118,7 +118,7 @@ class StagingBuffer final {
118118
vulkan_buffer_.vma_allocator(),
119119
vulkan_buffer_.allocation(),
120120
0u,
121-
VK_WHOLE_SIZE);
121+
numel * sizeof(SRC_T));
122122
const SRC_T* src = reinterpret_cast<const SRC_T*>(data());
123123
for (size_t i = 0; i < numel; ++i) {
124124
dst[i] = static_cast<DST_T>(src[i]);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ VulkanBuffer Allocator::create_staging_buffer(
177177
if (direction == CopyDirection::HOST_TO_DEVICE) {
178178
alloc_create_info.flags |=
179179
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
180+
alloc_create_info.preferredFlags = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
180181
} else {
181182
alloc_create_info.flags |= allocation_strategy_device_to_host_;
183+
alloc_create_info.preferredFlags = VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
182184
}
183185
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
184186
alloc_create_info.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
185-
alloc_create_info.preferredFlags =
186-
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
187187

188188
return VulkanBuffer(allocator_, size, alloc_create_info, buffer_usage);
189189
}

‎backends/vulkan/test/vulkan_compute_api_test.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,39 @@ TEST_F(VulkanComputeAPITest, print_adapter) {
108108
std::cout << *(context()->adapter_ptr()) << std::endl;
109109
}
110110

111+
TEST_F(VulkanComputeAPITest, device_to_host_staging_prefers_cached_memory) {
112+
vkapi::VulkanBuffer staging_buffer =
113+
context()->adapter_ptr()->vma().create_staging_buffer(
114+
4096, vkapi::CopyDirection::DEVICE_TO_HOST);
115+
const VmaAllocator allocator = staging_buffer.vma_allocator();
116+
ASSERT_NE(allocator, VK_NULL_HANDLE);
117+
118+
const VkPhysicalDeviceMemoryProperties* memory_properties = nullptr;
119+
vmaGetMemoryProperties(allocator, &memory_properties);
120+
ASSERT_NE(memory_properties, nullptr);
121+
122+
bool has_host_cached_memory = false;
123+
for (uint32_t i = 0; i < memory_properties->memoryTypeCount; ++i) {
124+
const VkMemoryPropertyFlags flags =
125+
memory_properties->memoryTypes[i].propertyFlags;
126+
has_host_cached_memory = has_host_cached_memory ||
127+
((flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) &&
128+
(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT));
129+
}
130+
if (!has_host_cached_memory) {
131+
GTEST_SKIP() << "Device does not expose host-cached visible memory";
132+
}
133+
134+
const VmaAllocation allocation = staging_buffer.allocation();
135+
ASSERT_NE(allocation, VK_NULL_HANDLE);
136+
137+
VkMemoryPropertyFlags selected_flags = 0;
138+
vmaGetAllocationMemoryProperties(allocator, allocation, &selected_flags);
139+
140+
EXPECT_TRUE(selected_flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT)
141+
<< "Selected memory with flags " << selected_flags;
142+
}
143+
111144
#if defined(VK_KHR_pipeline_executable_properties) && \
112145
defined(ETVK_INSPECT_PIPELINES)
113146

0 commit comments

Comments
 (0)