mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-31 08:51:55 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -8,6 +8,20 @@ if (Vulkan_FOUND) | |||||||
|                              ../../include/ggml-vulkan.h |                              ../../include/ggml-vulkan.h | ||||||
|                             ) |                             ) | ||||||
|  |  | ||||||
|  |     # Compile a test shader to determine whether GL_KHR_cooperative_matrix is supported. | ||||||
|  |     # If it's not, there will be an error to stderr. | ||||||
|  |     # If it's supported, set a define to indicate that we should compile those shaders | ||||||
|  |     execute_process(COMMAND ${Vulkan_GLSLC_EXECUTABLE} -o - -fshader-stage=compute --target-env=vulkan1.3 "${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_coopmat_support.comp" | ||||||
|  |                     OUTPUT_VARIABLE glslc_output | ||||||
|  |                     ERROR_VARIABLE glslc_error) | ||||||
|  |  | ||||||
|  |     if (${glslc_error} MATCHES ".*extension not supported: GL_KHR_cooperative_matrix.*") | ||||||
|  |         message(STATUS "GL_KHR_cooperative_matrix not supported by glslc") | ||||||
|  |     else() | ||||||
|  |         message(STATUS "GL_KHR_cooperative_matrix supported by glslc") | ||||||
|  |         add_compile_definitions(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) | ||||||
|  |     endif() | ||||||
|  |  | ||||||
|     # Compile a test shader to determine whether GL_NV_cooperative_matrix2 is supported. |     # Compile a test shader to determine whether GL_NV_cooperative_matrix2 is supported. | ||||||
|     # If it's not, there will be an error to stderr. |     # If it's not, there will be an error to stderr. | ||||||
|     # If it's supported, set a define to indicate that we should compile those shaders |     # If it's supported, set a define to indicate that we should compile those shaders | ||||||
|   | |||||||
| @@ -1645,6 +1645,7 @@ static void ggml_vk_load_shaders(vk_device& device) { | |||||||
| #undef CREATE_MM2 | #undef CREATE_MM2 | ||||||
|     } else |     } else | ||||||
| #endif  // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT) | #endif  // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT) | ||||||
|  | #if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) | ||||||
|     if (device->coopmat_support) { |     if (device->coopmat_support) { | ||||||
|         // Create 6 variants, {s,m,l}x{unaligned,aligned} |         // Create 6 variants, {s,m,l}x{unaligned,aligned} | ||||||
| #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \ | #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) { | |||||||
|         } |         } | ||||||
| #undef CREATE_MM2 | #undef CREATE_MM2 | ||||||
| #undef CREATE_MM | #undef CREATE_MM | ||||||
|     } else if (device->fp16) { |     } else | ||||||
|  | #endif  // defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) | ||||||
|  |     if (device->fp16) { | ||||||
|         // Create 6 variants, {s,m,l}x{unaligned,aligned} |         // Create 6 variants, {s,m,l}x{unaligned,aligned} | ||||||
| #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \ | #define CREATE_MM(PIPELINE_NAME, NAMELC, F16ACC, WG_DENOMS, WARPTILE, PUSHCONST, PARAMCOUNT, ID) \ | ||||||
|         if (device->mul_mat ## ID ## _l) \ |         if (device->mul_mat ## ID ## _l) \ | ||||||
| @@ -2242,6 +2245,7 @@ static vk_device ggml_vk_get_device(size_t idx) { | |||||||
|             last_struct = (VkBaseOutStructure *)&subgroup_size_control_features; |             last_struct = (VkBaseOutStructure *)&subgroup_size_control_features; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  | #if defined(VK_KHR_cooperative_matrix) | ||||||
|         VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features; |         VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features; | ||||||
|         coopmat_features.pNext = nullptr; |         coopmat_features.pNext = nullptr; | ||||||
|         coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR; |         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) { | |||||||
|             last_struct->pNext = (VkBaseOutStructure *)&coopmat_features; |             last_struct->pNext = (VkBaseOutStructure *)&coopmat_features; | ||||||
|             last_struct = (VkBaseOutStructure *)&coopmat_features; |             last_struct = (VkBaseOutStructure *)&coopmat_features; | ||||||
|         } |         } | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #if defined(VK_NV_cooperative_matrix2) | #if defined(VK_NV_cooperative_matrix2) | ||||||
|         VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {}; |         VkPhysicalDeviceCooperativeMatrix2FeaturesNV coopmat2_features {}; | ||||||
| @@ -2283,7 +2288,9 @@ static vk_device ggml_vk_get_device(size_t idx) { | |||||||
|             device_extensions.push_back("VK_EXT_subgroup_size_control"); |             device_extensions.push_back("VK_EXT_subgroup_size_control"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  | #if defined(VK_KHR_cooperative_matrix) | ||||||
|         device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix; |         device->coopmat_support = device->coopmat_support && coopmat_features.cooperativeMatrix; | ||||||
|  | #endif | ||||||
|  |  | ||||||
|         if (coopmat2_support) { |         if (coopmat2_support) { | ||||||
| #if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT) | #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) { | |||||||
|             device_extensions.push_back("VK_KHR_shader_float16_int8"); |             device_extensions.push_back("VK_KHR_shader_float16_int8"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  | #if defined(VK_KHR_cooperative_matrix) | ||||||
|         if (device->coopmat_support) { |         if (device->coopmat_support) { | ||||||
|             // Query supported shapes |             // Query supported shapes | ||||||
|             std::vector<VkCooperativeMatrixPropertiesKHR> cm_props; |             std::vector<VkCooperativeMatrixPropertiesKHR> cm_props; | ||||||
| @@ -2442,7 +2450,7 @@ static vk_device ggml_vk_get_device(size_t idx) { | |||||||
|         if (device->coopmat_support) { |         if (device->coopmat_support) { | ||||||
|             device_extensions.push_back("VK_KHR_cooperative_matrix"); |             device_extensions.push_back("VK_KHR_cooperative_matrix"); | ||||||
|         } |         } | ||||||
|  | #endif | ||||||
|         device->name = GGML_VK_NAME + std::to_string(idx); |         device->name = GGML_VK_NAME + std::to_string(idx); | ||||||
|  |  | ||||||
|         device_create_info = { |         device_create_info = { | ||||||
| @@ -2553,9 +2561,11 @@ static void ggml_vk_print_gpu_info(size_t idx) { | |||||||
|             fp16_storage = true; |             fp16_storage = true; | ||||||
|         } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) { |         } else if (strcmp("VK_KHR_shader_float16_int8", properties.extensionName) == 0) { | ||||||
|             fp16_compute = true; |             fp16_compute = true; | ||||||
|         } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 && | #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) | ||||||
|  |        } else if (strcmp("VK_KHR_cooperative_matrix", properties.extensionName) == 0 && | ||||||
|                    !getenv("GGML_VK_DISABLE_COOPMAT")) { |                    !getenv("GGML_VK_DISABLE_COOPMAT")) { | ||||||
|             coopmat_support = true; |             coopmat_support = true; | ||||||
|  | #endif | ||||||
| #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT) | #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT) | ||||||
|         } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 && |         } else if (strcmp("VK_NV_cooperative_matrix2", properties.extensionName) == 0 && | ||||||
|                    !getenv("GGML_VK_DISABLE_COOPMAT2")) { |                    !getenv("GGML_VK_DISABLE_COOPMAT2")) { | ||||||
| @@ -2593,6 +2603,7 @@ static void ggml_vk_print_gpu_info(size_t idx) { | |||||||
|     // Pointer to the last chain element |     // Pointer to the last chain element | ||||||
|     VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_features; |     VkBaseOutStructure * last_struct = (VkBaseOutStructure *)&vk12_features; | ||||||
|  |  | ||||||
|  | #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) | ||||||
|     VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features; |     VkPhysicalDeviceCooperativeMatrixFeaturesKHR coopmat_features; | ||||||
|     coopmat_features.pNext = nullptr; |     coopmat_features.pNext = nullptr; | ||||||
|     coopmat_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR; |     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) { | |||||||
|     fp16 = fp16 && vk12_features.shaderFloat16; |     fp16 = fp16 && vk12_features.shaderFloat16; | ||||||
|  |  | ||||||
|     coopmat_support = coopmat_support && coopmat_features.cooperativeMatrix; |     coopmat_support = coopmat_support && coopmat_features.cooperativeMatrix; | ||||||
|  | #endif | ||||||
|  |  | ||||||
|     std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none"; |     std::string matrix_cores = coopmat2_support ? "NV_coopmat2" : coopmat_support ? "KHR_coopmat" : "none"; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -0,0 +1,7 @@ | |||||||
|  | #version 460 | ||||||
|  |  | ||||||
|  | #extension GL_KHR_cooperative_matrix : require | ||||||
|  |  | ||||||
|  | void main() | ||||||
|  | { | ||||||
|  | } | ||||||
| @@ -342,9 +342,11 @@ void process_shaders() { | |||||||
|         matmul_shaders(true, matmul_id, false, false, false); |         matmul_shaders(true, matmul_id, false, false, false); | ||||||
|         matmul_shaders(true, matmul_id, false, false, true); |         matmul_shaders(true, matmul_id, false, false, true); | ||||||
|  |  | ||||||
|  | #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) | ||||||
|         // Coopmat, fp32acc and fp16acc |         // Coopmat, fp32acc and fp16acc | ||||||
|         matmul_shaders(true, matmul_id, true, false, false); |         matmul_shaders(true, matmul_id, true, false, false); | ||||||
|         matmul_shaders(true, matmul_id, true, false, true); |         matmul_shaders(true, matmul_id, true, false, true); | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT) | #if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT) | ||||||
|         // Coopmat2, fp32acc and fp16acc |         // Coopmat2, fp32acc and fp16acc | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Mathieu Baudier
					Mathieu Baudier