Skip to content

Commit b9c9c9f

Browse files
authored
vulkan: initialize vulkan-hpp to allow using extension function pointers (#15705)
Use this to query register count for shader compiles on NVIDIA. Currently this is only for performance debug, but it could eventually be used in some heuristics like split_k.
1 parent 50f4281 commit b9c9c9f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
#include "ggml-cpu.h"
66
#endif
77

8+
// See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
9+
#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
10+
811
#include <vulkan/vulkan.hpp>
912

13+
// See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
14+
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
15+
1016
#include <algorithm>
1117
#include <cmath>
1218
#include <iomanip>
@@ -121,6 +127,8 @@ struct vk_pipeline_struct {
121127
bool needed {};
122128
// set to true when the shader has been compiled
123129
bool compiled {};
130+
// number of registers used, extracted from pipeline executable properties
131+
uint32_t register_count {};
124132
};
125133

126134
typedef std::shared_ptr<vk_pipeline_struct> vk_pipeline;
@@ -429,6 +437,8 @@ struct vk_device_struct {
429437

430438
bool coopmat2;
431439

440+
bool pipeline_executable_properties_support {};
441+
432442
size_t idx;
433443

434444
bool mul_mat_l[GGML_TYPE_COUNT];
@@ -1603,6 +1613,20 @@ static void ggml_vk_create_pipeline_func(vk_device& device, vk_pipeline& pipelin
16031613
vk_instance.pfn_vkSetDebugUtilsObjectNameEXT(device->device, &static_cast<VkDebugUtilsObjectNameInfoEXT &>(duoni));
16041614
}
16051615

1616+
if (device->pipeline_executable_properties_support) {
1617+
vk::PipelineExecutableInfoKHR executableInfo;
1618+
executableInfo.pipeline = pipeline->pipeline;
1619+
1620+
auto statistics = device->device.getPipelineExecutableStatisticsKHR(executableInfo);
1621+
for (auto & s : statistics) {
1622+
// "Register Count" is reported by NVIDIA drivers.
1623+
if (strcmp(s.name, "Register Count") == 0) {
1624+
VK_LOG_DEBUG(pipeline->name << " " << s.name << ": " << s.value.u64 << " registers");
1625+
pipeline->register_count = (uint32_t)s.value.u64;
1626+
}
1627+
}
1628+
}
1629+
16061630
{
16071631
std::lock_guard<std::recursive_mutex> guard(device->mutex);
16081632
device->all_pipelines.push_back(pipeline);
@@ -3610,6 +3634,7 @@ static vk_device ggml_vk_get_device(size_t idx) {
36103634
bool amd_shader_core_properties2 = false;
36113635
bool pipeline_robustness = false;
36123636
bool coopmat2_support = false;
3637+
bool pipeline_executable_properties_support = false;
36133638
device->coopmat_support = false;
36143639
device->integer_dot_product = false;
36153640
bool bfloat16_support = false;
@@ -3652,6 +3677,8 @@ static vk_device ggml_vk_get_device(size_t idx) {
36523677
!getenv("GGML_VK_DISABLE_BFLOAT16")) {
36533678
bfloat16_support = true;
36543679
#endif
3680+
} else if (strcmp("VK_KHR_pipeline_executable_properties", properties.extensionName) == 0) {
3681+
pipeline_executable_properties_support = true;
36553682
}
36563683
}
36573684

@@ -3878,8 +3905,18 @@ static vk_device ggml_vk_get_device(size_t idx) {
38783905
device_extensions.push_back("VK_KHR_shader_integer_dot_product");
38793906
}
38803907

3908+
VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR pep_features {};
3909+
pep_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR;
3910+
if (pipeline_executable_properties_support) {
3911+
last_struct->pNext = (VkBaseOutStructure *)&pep_features;
3912+
last_struct = (VkBaseOutStructure *)&pep_features;
3913+
device_extensions.push_back("VK_KHR_pipeline_executable_properties");
3914+
}
3915+
38813916
vkGetPhysicalDeviceFeatures2(device->physical_device, &device_features2);
38823917

3918+
device->pipeline_executable_properties_support = pipeline_executable_properties_support;
3919+
38833920
device->fp16 = device->fp16 && vk12_features.shaderFloat16;
38843921

38853922
#if defined(VK_KHR_shader_bfloat16)
@@ -4395,6 +4432,9 @@ static void ggml_vk_instance_init() {
43954432
}
43964433
VK_LOG_DEBUG("ggml_vk_instance_init()");
43974434

4435+
// See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
4436+
VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr);
4437+
43984438
uint32_t api_version = vk::enumerateInstanceVersion();
43994439

44004440
if (api_version < VK_API_VERSION_1_2) {
@@ -4462,6 +4502,9 @@ static void ggml_vk_instance_init() {
44624502

44634503
vk_perf_logger_enabled = getenv("GGML_VK_PERF_LOGGER") != nullptr;
44644504

4505+
// See https://github.com/KhronosGroup/Vulkan-Hpp?tab=readme-ov-file#extensions--per-device-function-pointers-
4506+
VULKAN_HPP_DEFAULT_DISPATCHER.init(vk_instance.instance);
4507+
44654508
std::vector<vk::PhysicalDevice> devices = vk_instance.instance.enumeratePhysicalDevices();
44664509

44674510
// Emulate behavior of CUDA_VISIBLE_DEVICES for Vulkan

0 commit comments

Comments
 (0)