Skip to content

Commit 2c293ed

Browse files
committed
Disable GL_KHR_cooperative_matrix Vulkan extension if not available.
1 parent e6e7c75 commit 2c293ed

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

ggml/src/ggml-vulkan/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ if (Vulkan_FOUND)
2222
add_compile_definitions(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
2323
endif()
2424

25+
# Compile a test shader to determine whether GL_KHR_cooperative_matrix is supported.
26+
# If it's not, there will be an error to stderr.
27+
# If it's supported, set a define to indicate that we should compile those shaders
28+
execute_process(COMMAND ${Vulkan_GLSLC_EXECUTABLE} -o - -fshader-stage=compute --target-env=vulkan1.3 "${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_coopmat_support.comp"
29+
OUTPUT_VARIABLE glslc_output
30+
ERROR_VARIABLE glslc_error)
31+
32+
if (${glslc_error} MATCHES ".*extension not supported: GL_KHR_cooperative_matrix.*")
33+
message(STATUS "GL_KHR_cooperative_matrix not supported by glslc")
34+
else()
35+
message(STATUS "GL_KHR_cooperative_matrix supported by glslc")
36+
add_compile_definitions(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
37+
endif()
38+
2539
target_link_libraries(ggml-vulkan PRIVATE Vulkan::Vulkan)
2640
target_include_directories(ggml-vulkan PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
2741

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

Lines changed: 17 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")) {
@@ -2564,9 +2574,11 @@ static void ggml_vk_print_gpu_info(size_t idx) {
25642574
}
25652575
}
25662576

2577+
#if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
25672578
if (!ggml_vk_khr_cooperative_matrix_support(props2.properties, driver_props)) {
25682579
coopmat_support = false;
25692580
}
2581+
#endif
25702582

25712583
const char* GGML_VK_DISABLE_F16 = getenv("GGML_VK_DISABLE_F16");
25722584
bool force_disable_f16 = GGML_VK_DISABLE_F16 != nullptr;
@@ -2593,6 +2605,7 @@ static void ggml_vk_print_gpu_info(size_t idx) {
25932605
// Pointer to the last chain element
25942606
VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_features;
25952607

2608+
#if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
25962609
VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features;
25972610
coopmat_features.pNext = nullptr;
25982611
coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR;
@@ -2608,6 +2621,7 @@ static void ggml_vk_print_gpu_info(size_t idx) {
26082621
fp16 = fp16 && vk12_features.shaderFloat16;
26092622

26102623
coopmat_support = coopmat_support && coopmat_features.cooperativeMatrix;
2624+
#endif
26112625

26122626
std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none";
26132627

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)