Markus Tavenrath commited on
Commit
2c3741a
·
1 Parent(s): 2bd85b6

Add support for VK_EXT_debug_utils to add labels to Vulkan objects. (llama/13792)

Browse files

* 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.

ggml/src/ggml-vulkan/ggml-vulkan.cpp CHANGED
@@ -1041,6 +1041,14 @@ void vk_memory_logger::log_deallocation(vk_buffer_ref buf_ref) {
1041
  struct vk_instance_t {
1042
  vk::Instance instance;
1043
 
 
 
 
 
 
 
 
 
1044
  std::vector<size_t> device_indices;
1045
  vk_device devices[GGML_VK_MAX_DEVICES];
1046
  };
@@ -1180,6 +1188,14 @@ static void ggml_vk_create_pipeline_func(vk_device& device, vk_pipeline& pipelin
1180
  }
1181
  pipeline->compiled = true;
1182
 
 
 
 
 
 
 
 
 
1183
  {
1184
  std::lock_guard<std::mutex> guard(device->mutex);
1185
  device->pipelines.insert({ pipeline->name, pipeline });
@@ -3561,6 +3577,8 @@ static void ggml_vk_print_gpu_info(size_t idx) {
3561
  static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
3562
  static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
3563
 
 
 
3564
  static void ggml_vk_instance_init() {
3565
  if (vk_instance_initialized) {
3566
  return;
@@ -3581,7 +3599,7 @@ static void ggml_vk_instance_init() {
3581
  #ifdef __APPLE__
3582
  const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
3583
  #endif
3584
-
3585
  std::vector<const char*> layers;
3586
 
3587
  if (validation_ext) {
@@ -3596,6 +3614,9 @@ static void ggml_vk_instance_init() {
3596
  extensions.push_back("VK_KHR_portability_enumeration");
3597
  }
3598
  #endif
 
 
 
3599
  vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
3600
  #ifdef __APPLE__
3601
  if (portability_enumeration_ext) {
@@ -3619,6 +3640,18 @@ static void ggml_vk_instance_init() {
3619
  vk_instance.instance = vk::createInstance(instance_create_info);
3620
  vk_instance_initialized = true;
3621
 
 
 
 
 
 
 
 
 
 
 
 
 
3622
  vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
3623
 
3624
  // 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
9656
  VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
9657
  ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
9658
 
 
 
 
 
 
 
 
9659
  uint64_t total_mat_mul_bytes = 0;
9660
  for (int i = 0; i < cgraph->n_nodes; i++) {
9661
  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
10345
  UNUSED(instance_extensions);
10346
  }
10347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10348
  static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
10349
  switch (props.vendorID) {
10350
  case VK_VENDOR_ID_INTEL:
 
1041
  struct vk_instance_t {
1042
  vk::Instance instance;
1043
 
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
+
1052
  std::vector<size_t> device_indices;
1053
  vk_device devices[GGML_VK_MAX_DEVICES];
1054
  };
 
1188
  }
1189
  pipeline->compiled = true;
1190
 
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
+
1199
  {
1200
  std::lock_guard<std::mutex> guard(device->mutex);
1201
  device->pipelines.insert({ pipeline->name, pipeline });
 
3577
  static bool ggml_vk_instance_validation_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
3578
  static bool ggml_vk_instance_portability_enumeration_ext_available(const std::vector<vk::ExtensionProperties>& instance_extensions);
3579
 
3580
+ static bool ggml_vk_instance_debug_utils_ext_available(const std::vector<vk::ExtensionProperties> & instance_extensions);
3581
+
3582
  static void ggml_vk_instance_init() {
3583
  if (vk_instance_initialized) {
3584
  return;
 
3599
  #ifdef __APPLE__
3600
  const bool portability_enumeration_ext = ggml_vk_instance_portability_enumeration_ext_available(instance_extensions);
3601
  #endif
3602
+ const bool debug_utils_ext = ggml_vk_instance_debug_utils_ext_available(instance_extensions) && getenv("GGML_VK_DEBUG_MARKERS") != nullptr;
3603
  std::vector<const char*> layers;
3604
 
3605
  if (validation_ext) {
 
3614
  extensions.push_back("VK_KHR_portability_enumeration");
3615
  }
3616
  #endif
3617
+ if (debug_utils_ext) {
3618
+ extensions.push_back("VK_EXT_debug_utils");
3619
+ }
3620
  vk::InstanceCreateInfo instance_create_info(vk::InstanceCreateFlags{}, &app_info, layers, extensions);
3621
  #ifdef __APPLE__
3622
  if (portability_enumeration_ext) {
 
3640
  vk_instance.instance = vk::createInstance(instance_create_info);
3641
  vk_instance_initialized = true;
3642
 
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();
3655
  vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
3656
 
3657
  // Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan
 
9689
  VK_LOG_DEBUG("ggml_backend_vk_graph_compute(" << cgraph->n_nodes << " nodes)");
9690
  ggml_backend_vk_context * ctx = (ggml_backend_vk_context *)backend->context;
9691
 
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
+
9699
  uint64_t total_mat_mul_bytes = 0;
9700
  for (int i = 0; i < cgraph->n_nodes; i++) {
9701
  ggml_vk_build_graph(ctx, cgraph->nodes[i], i, nullptr, 0, true, false, false, false);
 
10385
  UNUSED(instance_extensions);
10386
  }
10387
 
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
+
10404
  static bool ggml_vk_khr_cooperative_matrix_support(const vk::PhysicalDeviceProperties& props, const vk::PhysicalDeviceDriverProperties& driver_props, vk_device_architecture arch) {
10405
  switch (props.vendorID) {
10406
  case VK_VENDOR_ID_INTEL: