Skip to content

Commit 7774c38

Browse files
jorgep31415facebook-github-bot
authored andcommitted
Separate PhysicalDevice/DeviceHandle from Adapter.* (#4187)
Summary: Pull Request resolved: #4187 `Adapter.*` has too much going on. ghstack-source-id: 233090245 exported-using-ghexport bypass-github-export-checks bypass-github-pytorch-ci-checks bypass-github-executorch-ci-checks Reviewed By: SS-JIA Differential Revision: D59540131 fbshipit-source-id: 3acdda2ccc56f7b0ceeb48d072cfb38c1a800bf8
1 parent 81cf3ab commit 7774c38

File tree

4 files changed

+246
-199
lines changed

4 files changed

+246
-199
lines changed

backends/vulkan/runtime/vk_api/Adapter.cpp

Lines changed: 0 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -10,103 +10,13 @@
1010

1111
#include <executorch/backends/vulkan/runtime/vk_api/Adapter.h>
1212

13-
#include <bitset>
14-
#include <cstring>
1513
#include <iomanip>
16-
#include <sstream>
17-
#include <utility>
1814

1915
namespace vkcompute {
2016
namespace vkapi {
2117

22-
PhysicalDevice::PhysicalDevice(VkPhysicalDevice physical_device_handle)
23-
: handle(physical_device_handle),
24-
properties{},
25-
memory_properties{},
26-
shader_16bit_storage{
27-
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES},
28-
shader_8bit_storage{
29-
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES},
30-
shader_float16_int8_types{
31-
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR},
32-
queue_families{},
33-
num_compute_queues(0),
34-
has_unified_memory(false),
35-
has_timestamps(properties.limits.timestampComputeAndGraphics),
36-
timestamp_period(properties.limits.timestampPeriod),
37-
extension_features(&shader_16bit_storage) {
38-
// Extract physical device properties
39-
vkGetPhysicalDeviceProperties(handle, &properties);
40-
vkGetPhysicalDeviceMemoryProperties(handle, &memory_properties);
41-
42-
VkPhysicalDeviceFeatures2 features2{
43-
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
44-
45-
// Create linked list to query availability of extensions
46-
features2.pNext = &shader_16bit_storage;
47-
shader_16bit_storage.pNext = &shader_8bit_storage;
48-
shader_8bit_storage.pNext = &shader_float16_int8_types;
49-
shader_float16_int8_types.pNext = nullptr;
50-
51-
vkGetPhysicalDeviceFeatures2(handle, &features2);
52-
53-
// Check if there are any memory types have both the HOST_VISIBLE and the
54-
// DEVICE_LOCAL property flags
55-
const VkMemoryPropertyFlags unified_memory_flags =
56-
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
57-
for (size_t i = 0; i < memory_properties.memoryTypeCount; ++i) {
58-
if (memory_properties.memoryTypes[i].propertyFlags | unified_memory_flags) {
59-
has_unified_memory = true;
60-
break;
61-
}
62-
}
63-
64-
uint32_t queue_family_count = 0;
65-
vkGetPhysicalDeviceQueueFamilyProperties(
66-
handle, &queue_family_count, nullptr);
67-
68-
queue_families.resize(queue_family_count);
69-
vkGetPhysicalDeviceQueueFamilyProperties(
70-
handle, &queue_family_count, queue_families.data());
71-
72-
// Find the total number of compute queues
73-
for (const VkQueueFamilyProperties& p : queue_families) {
74-
// Check if this family has compute capability
75-
if (p.queueFlags & VK_QUEUE_COMPUTE_BIT) {
76-
num_compute_queues += p.queueCount;
77-
}
78-
}
79-
}
80-
8118
namespace {
8219

83-
void find_requested_device_extensions(
84-
VkPhysicalDevice physical_device,
85-
std::vector<const char*>& enabled_extensions,
86-
const std::vector<const char*>& requested_extensions) {
87-
uint32_t device_extension_properties_count = 0;
88-
VK_CHECK(vkEnumerateDeviceExtensionProperties(
89-
physical_device, nullptr, &device_extension_properties_count, nullptr));
90-
std::vector<VkExtensionProperties> device_extension_properties(
91-
device_extension_properties_count);
92-
VK_CHECK(vkEnumerateDeviceExtensionProperties(
93-
physical_device,
94-
nullptr,
95-
&device_extension_properties_count,
96-
device_extension_properties.data()));
97-
98-
std::vector<const char*> enabled_device_extensions;
99-
100-
for (const auto& requested_extension : requested_extensions) {
101-
for (const auto& extension : device_extension_properties) {
102-
if (strcmp(requested_extension, extension.extensionName) == 0) {
103-
enabled_extensions.push_back(requested_extension);
104-
break;
105-
}
106-
}
107-
}
108-
}
109-
11020
VkDevice create_logical_device(
11121
const PhysicalDevice& physical_device,
11222
const uint32_t num_queues_to_create,
@@ -210,76 +120,8 @@ VkDevice create_logical_device(
210120
return handle;
211121
}
212122

213-
// Print utils
214-
215-
std::string get_device_type_str(const VkPhysicalDeviceType type) {
216-
switch (type) {
217-
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
218-
return "INTEGRATED_GPU";
219-
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
220-
return "DISCRETE_GPU";
221-
case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
222-
return "VIRTUAL_GPU";
223-
case VK_PHYSICAL_DEVICE_TYPE_CPU:
224-
return "CPU";
225-
default:
226-
return "UNKNOWN";
227-
}
228-
}
229-
230-
std::string get_memory_properties_str(const VkMemoryPropertyFlags flags) {
231-
std::bitset<10> values(flags);
232-
std::stringstream ss("|");
233-
if (values[0]) {
234-
ss << " DEVICE_LOCAL |";
235-
}
236-
if (values[1]) {
237-
ss << " HOST_VISIBLE |";
238-
}
239-
if (values[2]) {
240-
ss << " HOST_COHERENT |";
241-
}
242-
if (values[3]) {
243-
ss << " HOST_CACHED |";
244-
}
245-
if (values[4]) {
246-
ss << " LAZILY_ALLOCATED |";
247-
}
248-
249-
return ss.str();
250-
}
251-
252-
std::string get_queue_family_properties_str(const VkQueueFlags flags) {
253-
std::bitset<10> values(flags);
254-
std::stringstream ss("|");
255-
if (values[0]) {
256-
ss << " GRAPHICS |";
257-
}
258-
if (values[1]) {
259-
ss << " COMPUTE |";
260-
}
261-
if (values[2]) {
262-
ss << " TRANSFER |";
263-
}
264-
265-
return ss.str();
266-
}
267-
268123
} // namespace
269124

270-
//
271-
// DeviceHandle
272-
//
273-
274-
DeviceHandle::DeviceHandle(VkDevice device) : handle(device) {}
275-
276-
DeviceHandle::~DeviceHandle() {
277-
if (VK_NULL_HANDLE == handle) {
278-
return;
279-
}
280-
vkDestroyDevice(handle, nullptr);
281-
}
282-
283125
//
284126
// Adapter
285127
//

backends/vulkan/runtime/vk_api/Adapter.h

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,14 @@
1212

1313
#include <executorch/backends/vulkan/runtime/vk_api/vk_api.h>
1414

15-
#include <executorch/backends/vulkan/runtime/utils/VecUtils.h>
16-
15+
#include <executorch/backends/vulkan/runtime/vk_api/Device.h>
1716
#include <executorch/backends/vulkan/runtime/vk_api/Pipeline.h>
18-
#include <executorch/backends/vulkan/runtime/vk_api/Shader.h>
1917

2018
#include <executorch/backends/vulkan/runtime/vk_api/memory/Allocator.h>
2119

22-
#include <array>
23-
#include <mutex>
24-
#include <ostream>
25-
2620
namespace vkcompute {
2721
namespace vkapi {
2822

29-
struct PhysicalDevice final {
30-
// Handle
31-
VkPhysicalDevice handle;
32-
33-
// Properties obtained from Vulkan
34-
VkPhysicalDeviceProperties properties;
35-
VkPhysicalDeviceMemoryProperties memory_properties;
36-
// Additional features available from extensions
37-
VkPhysicalDevice16BitStorageFeatures shader_16bit_storage;
38-
VkPhysicalDevice8BitStorageFeatures shader_8bit_storage;
39-
VkPhysicalDeviceShaderFloat16Int8Features shader_float16_int8_types;
40-
41-
// Available GPU queues
42-
std::vector<VkQueueFamilyProperties> queue_families;
43-
44-
// Metadata
45-
uint32_t num_compute_queues;
46-
bool has_unified_memory;
47-
bool has_timestamps;
48-
float timestamp_period;
49-
50-
// Head of the linked list of extensions to be requested
51-
void* extension_features{nullptr};
52-
53-
explicit PhysicalDevice(VkPhysicalDevice);
54-
};
55-
56-
struct DeviceHandle final {
57-
VkDevice handle;
58-
59-
explicit DeviceHandle(VkDevice);
60-
~DeviceHandle();
61-
};
62-
6323
//
6424
// A Vulkan Adapter represents a logical device and all its properties. It
6525
// manages all relevant properties of the underlying physical device, a

0 commit comments

Comments
 (0)