Skip to content

Commit 02f0430

Browse files
authored
Disable GL_KHR_cooperative_matrix Vulkan extension if not available. (#11117)
* Disable GL_KHR_cooperative_matrix Vulkan extension if not available. * Perform Vulkan extensions checks in a more sensible order * Remove unnecessary #ifdef directive
1 parent bec2183 commit 02f0430

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

ggml/src/ggml-vulkan/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ if (Vulkan_FOUND)
88
../../include/ggml-vulkan.h
99
)
1010

11+
# Compile a test shader to determine whether GL_KHR_cooperative_matrix is supported.
12+
# If it's not, there will be an error to stderr.
13+
# If it's supported, set a define to indicate that we should compile those shaders
14+
execute_process(COMMAND ${Vulkan_GLSLC_EXECUTABLE} -o - -fshader-stage=compute --target-env=vulkan1.3 "${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_coopmat_support.comp"
15+
OUTPUT_VARIABLE glslc_output
16+
ERROR_VARIABLE glslc_error)
17+
18+
if (${glslc_error} MATCHES ".*extension not supported: GL_KHR_cooperative_matrix.*")
19+
message(STATUS "GL_KHR_cooperative_matrix not supported by glslc")
20+
else()
21+
message(STATUS "GL_KHR_cooperative_matrix supported by glslc")
22+
add_compile_definitions(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
23+
endif()
24+
1125
# Compile a test shader to determine whether GL_NV_cooperative_matrix2 is supported.
1226
# If it's not, there will be an error to stderr.
1327
# If it's supported, set a define to indicate that we should compile those shaders

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ static void ggml_vk_load_shaders(vk_device& device) {
16451645
#undef CREATE_MM2
16461646
} else
16471647
#endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
1648+
#if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
16481649
if (device->coopmat_support) {
16491650
// Create 6 variants, {s,m,l}x{unaligned,aligned}
16501651
#define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
@@ -1739,7 +1740,9 @@ static void ggml_vk_load_shaders(vk_device& device) {
17391740
}
17401741
#undef CREATE_MM2
17411742
#undef CREATE_MM
1742-
} else if (device->fp16) {
1743+
} else
1744+
#endif // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
1745+
if (device->fp16) {
17431746
// Create 6 variants, {s,m,l}x{unaligned,aligned}
17441747
#define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \
17451748
if (device->mul_mat ## ID ## _l) \
@@ -2242,6 +2245,7 @@ static vk_device ggml_vk_get_device(size_t idx) {
22422245
last_struct = (VkBaseOutStructure *)&subgroup_size_control_features;
22432246
}
22442247

2248+
#if defined(VK_KHR_cooperative_matrix)
22452249
VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
22462250
coopmat_features.pNext = nullptr;
22472251
coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
@@ -2251,6 +2255,7 @@ static vk_device ggml_vk_get_device(size_t idx) {
22512255
last_struct->pNext = (VkBaseOutStructure *)&coopmat_features;
22522256
last_struct = (VkBaseOutStructure *)&coopmat_features;
22532257
}
2258+
#endif
22542259

22552260
#if defined(VK_NV_cooperative_matrix2)
22562261
VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {};
@@ -2283,7 +2288,9 @@ static vk_device ggml_vk_get_device(size_t idx) {
22832288
device_extensions.push_back("VK_EXT_subgroup_size_control");
22842289
}
22852290

2291+
#if defined(VK_KHR_cooperative_matrix)
22862292
device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix;
2293+
#endif
22872294

22882295
if (coopmat2_support) {
22892296
#if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
@@ -2376,6 +2383,7 @@ static vk_device ggml_vk_get_device(size_t idx) {
23762383
device_extensions.push_back("VK_KHR_shader_float16_int8");
23772384
}
23782385

2386+
#if defined(VK_KHR_cooperative_matrix)
23792387
if (device->coopmat_support) {
23802388
// Query supported shapes
23812389
std::vector<VkCooperativeMatrixPropertiesKHR> cm_props;
@@ -2442,7 +2450,7 @@ static vk_device ggml_vk_get_device(size_t idx) {
24422450
if (device->coopmat_support) {
24432451
device_extensions.push_back("VK_KHR_cooperative_matrix");
24442452
}
2445-
2453+
#endif
24462454
device->name = GGML_VK_NAME + std::to_string(idx);
24472455

24482456
device_create_info = {
@@ -2553,9 +2561,11 @@ static void ggml_vk_print_gpu_info(size_t idx) {
25532561
fp16_storage = true;
25542562
} else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) {
25552563
fp16_compute = true;
2556-
} else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
2564+
#if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
2565+
} else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 &&
25572566
!getenv("GGML_VK_DISABLE_COOPMAT")) {
25582567
coopmat_support = true;
2568+
#endif
25592569
#if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
25602570
} else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 &&
25612571
!getenv("GGML_VK_DISABLE_COOPMAT2")) {
@@ -2593,6 +2603,7 @@ static void ggml_vk_print_gpu_info(size_t idx) {
25932603
// Pointer to the last chain element
25942604
VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_features;
25952605

2606+
#if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
25962607
VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
25972608
coopmat_features.pNext = nullptr;
25982609
coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
@@ -2608,6 +2619,7 @@ static void ggml_vk_print_gpu_info(size_t idx) {
26082619
fp16 = fp16 && vk12_features.shaderFloat16;
26092620

26102621
coopmat_support = coopmat_support && coopmat_features.cooperativeMatrix;
2622+
#endif
26112623

26122624
std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
26132625

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#version 460
2+
3+
#extension GL_KHR_cooperative_matrix : require
4+
5+
void main()
6+
{
7+
}

ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,11 @@ void process_shaders() {
342342
matmul_shaders(true, matmul_id, false, false, false);
343343
matmul_shaders(true, matmul_id, false, false, true);
344344

345+
#if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
345346
// Coopmat, fp32acc and fp16acc
346347
matmul_shaders(true, matmul_id, true, false, false);
347348
matmul_shaders(true, matmul_id, true, false, true);
349+
#endif
348350

349351
#if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
350352
// Coopmat2, fp32acc and fp16acc

0 commit comments

Comments
 (0)