Skip to content

Commit bb16041

Browse files
authored
Add support for VK_EXT_debug_utils to add labels to Vulkan objects. (ggml-org#13792)
* Add support for VK_EXT_debug_utils to add labels to Vulkan objects. In step 1 compute pipelines are getting labeled. * remove #ifdef for debug utils and add queue marker.
1 parent 58cba76 commit bb16041

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

ggml/src/ggml-vulkan/ggml-vulkan.cpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,14 @@ void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
10411041
struct vk_instance_t {
10421042
vk::Instance instance;
10431043

1044+
bool debug_utils_support = false; // VK_EXT_debug_utils enabled
1045+
PFN_vkSetDebugUtilsObjectNameEXT pfn_vkSetDebugUtilsObjectNameEXT = {};
1046+
PFN_vkQueueBeginDebugUtilsLabelEXT pfn_vkQueueBeginDebugUtilsLabelEXT = {};
1047+
PFN_vkQueueEndDebugUtilsLabelEXT pfn_vkQueueEndDebugUtilsLabelEXT = {};
1048+
PFN_vkCmdBeginDebugUtilsLabelEXT pfn_vkCmdBeginDebugUtilsLabelEXT = {};
1049+
PFN_vkCmdEndDebugUtilsLabelEXT pfn_vkCmdEndDebugUtilsLabelEXT = {};
1050+
PFN_vkCmdInsertDebugUtilsLabelEXT pfn_vkCmdInsertDebugUtilsLabelEXT = {};
1051+
10441052
std::vector<size_t> device_indices;
10451053
vk_device devices[GGML_VK_MAX_DEVICES];
10461054
};
@@ -1180,6 +1188,14 @@ static void ggml_vk_create_pipeline_func(vk_device& device, vk_pipeline& pipelin
11801188
}
11811189
pipeline->compiled = true;
11821190

1191+
if (vk_instance.debug_utils_support) {
1192+
vk::DebugUtilsObjectNameInfoEXT duoni;
1193+
duoni.objectType = vk::ObjectType::ePipeline;
1194+
duoni.pObjectName = pipeline->name.c_str();
1195+
duoni.objectHandle = reinterpret_cast<uint64_t>(static_cast<VkPipeline_T*>(pipeline->pipeline));
1196+
vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
1197+
}
1198+
11831199
{
11841200
std::lock_guard<std::mutex> guard(device->mutex);
11851201
device->pipelines.insert({ pipeline->name, pipeline });
@@ -3561,6 +3577,8 @@ static void ggml_vk_print_gpu_info(size_t idx) {
35613577
static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
35623578
static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
35633579

3580+
static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
3581+
35643582
static void ggml_vk_instance_init() {
35653583
if (vk_instance_initialized) {
35663584
return;
@@ -3581,7 +3599,7 @@ static void ggml_vk_instance_init() {
35813599
#ifdef __APPLE__
35823600
const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
35833601
#endif
3584-
3602+
const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
35853603
std::vector<const char*> layers;
35863604

35873605
if (validation_ext) {
@@ -3596,6 +3614,9 @@ static void ggml_vk_instance_init() {
35963614
extensions.push_back("VK_KHR_portability_enumeration");
35973615
}
35983616
#endif
3617+
if (debug_utils_ext) {
3618+
extensions.push_back("VK_EXT_debug_utils");
3619+
}
35993620
vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
36003621
#ifdef __APPLE__
36013622
if (portability_enumeration_ext) {
@@ -3619,6 +3640,18 @@ static void ggml_vk_instance_init() {
36193640
vk_instance.instance = vk::createInstance(instance_create_info);
36203641
vk_instance_initialized = true;
36213642

3643+
if (debug_utils_ext) {
3644+
vk_instance.debug_utils_support = true;
3645+
vk_instance.pfn_vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkSetDebugUtilsObjectNameEXT");
3646+
vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT = (PFN_vkQueueBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueBeginDebugUtilsLabelEXT");
3647+
vk_instance.pfn_vkQueueEndDebugUtilsLabelEXT = (PFN_vkQueueEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkQueueEndDebugUtilsLabelEXT");
3648+
vk_instance.pfn_vkCmdBeginDebugUtilsLabelEXT = (PFN_vkCmdBeginDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdBeginDebugUtilsLabelEXT");
3649+
vk_instance.pfn_vkCmdEndDebugUtilsLabelEXT = (PFN_vkCmdEndDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdEndDebugUtilsLabelEXT");
3650+
vk_instance.pfn_vkCmdInsertDebugUtilsLabelEXT = (PFN_vkCmdInsertDebugUtilsLabelEXT) vkGetInstanceProcAddr(vk_instance.instance, "vkCmdInsertDebugUtilsLabelEXT");
3651+
3652+
}
3653+
3654+
size_t num_available_devices = vk_instance.instance.enumeratePhysicalDevices().size();
36223655
vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
36233656

36243657
// Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
@@ -9656,6 +9689,13 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
96569689
VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
96579690
ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
96589691

9692+
if (vk_instance.debug_utils_support) {
9693+
vk::DebugUtilsLabelEXT dul = {};
9694+
dul.pLabelName = "ggml_backend_vk_graph_compute";
9695+
dul.color = std::array<float,4>{1.0f, 1.0f, 1.0f, 1.0f};
9696+
vk_instance.pfn_vkQueueBeginDebugUtilsLabelEXT(ctx->device->compute_queue.queue, reinterpret_cast<VkDebugUtilsLabelEXT*>(&dul));
9697+
}
9698+
96599699
uint64_t total_mat_mul_bytes = 0;
96609700
for (int i = 0; i < cgraph->n_nodes; i++) {
96619701
ggml_vk_build_graph(ctx, cgraph->nodes[i], i, nullptr, 0, true, false, false, false);
@@ -10345,6 +10385,22 @@ static bool ggml_vk_instance_portability_enumeration_ext_available(const std::ve
1034510385
UNUSED(instance_extensions);
1034610386
}
1034710387

10388+
// Extension availability
10389+
static bool ggml_vk_instance_debug_utils_ext_available(
10390+
const std::vector<vk::ExtensionProperties> & instance_extensions) {
10391+
// Check for portability enumeration extension for MoltenVK support
10392+
for (const auto & properties : instance_extensions) {
10393+
if (strcmp("VK_EXT_debug_utils", properties.extensionName) == 0) {
10394+
return true;
10395+
}
10396+
}
10397+
10398+
std::cerr << "ggml_vulkan: WARNING: Instance extension VK_EXT_debug_utils not found." << std::endl;
10399+
return false;
10400+
10401+
UNUSED(instance_extensions);
10402+
}
10403+
1034810404
static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
1034910405
switch (props.vendorID) {
1035010406
case VK_VENDOR_ID_INTEL:

0 commit comments

Comments
 (0)