Skip to content

Commit b95296c

Browse files
committed
vulkan: add 1.1 shader compatibility mode
if GGML_VULKAN_MIN_1_1 is defined: - rte shaders will be built with 1.1 API and "SPV_KHR_float_controls" spirv extension. - all no _cm2 shaders will be built with 1.1 API No changes if GGML_VULKAN_MIN_1_1 is not defined (default).
1 parent 95fa092 commit b95296c

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

ggml/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ option(GGML_VULKAN_MEMORY_DEBUG "ggml: enable Vulkan memory debug ou
222222
option(GGML_VULKAN_SHADER_DEBUG_INFO "ggml: enable Vulkan shader debug info" OFF)
223223
option(GGML_VULKAN_VALIDATE "ggml: enable Vulkan validation" OFF)
224224
option(GGML_VULKAN_RUN_TESTS "ggml: run Vulkan tests" OFF)
225+
option(GGML_VULKAN_MIN_1_1 "ggml: target Vulkan 1.1 minimum (SPIR-V 1.3)" OFF)
225226
option(GGML_WEBGPU "ggml: use WebGPU" OFF)
226227
option(GGML_WEBGPU_DEBUG "ggml: enable WebGPU debug output" OFF)
227228
option(GGML_WEBGPU_CPU_PROFILE "ggml: enable WebGPU profiling (CPU)" OFF)

ggml/src/ggml-vulkan/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ if (Vulkan_FOUND)
112112
list(APPEND VULKAN_SHADER_GEN_CMAKE_ARGS -DGGML_VULKAN_SHADER_DEBUG_INFO=ON)
113113
endif()
114114

115+
if (GGML_VULKAN_MIN_1_1)
116+
add_compile_definitions(GGML_VULKAN_MIN_1_1)
117+
list(APPEND VULKAN_SHADER_GEN_CMAKE_ARGS -DGGML_VULKAN_MIN_1_1=ON)
118+
endif()
119+
115120
if (GGML_VULKAN_VALIDATE)
116121
add_compile_definitions(GGML_VULKAN_VALIDATE)
117122
endif()

ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ if (GGML_VULKAN_SHADER_DEBUG_INFO)
2323
add_compile_definitions(GGML_VULKAN_SHADER_DEBUG_INFO)
2424
message(STATUS "Enabling shader debug info")
2525
endif()
26+
if (GGML_VULKAN_MIN_1_1)
27+
add_compile_definitions(GGML_VULKAN_MIN_1_1)
28+
endif()
2629

2730
set(TARGET vulkan-shaders-gen)
2831
add_executable(${TARGET} vulkan-shaders-gen.cpp)
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11

22
#if RTE16
33
#extension GL_EXT_spirv_intrinsics : enable
4-
spirv_execution_mode(capabilities = [4467], 4462, 16); // RoundingModeRTE, 16 bits
4+
spirv_execution_mode(
5+
#ifdef VULKAN11_RTE
6+
extensions = ["SPV_KHR_float_controls"],
7+
#endif
8+
capabilities = [4467], 4462, 16); // RoundingModeRTE, 16 bits
59
#endif // RTE16

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,21 @@ compile_count_guard acquire_compile_slot() {
322322
}
323323

324324
void string_to_spv_func(std::string name, std::string in_path, std::string out_path, std::map<std::string, std::string> defines, bool coopmat, bool dep_file, compile_count_guard slot) {
325-
std::string target_env = (name.find("_cm2") != std::string::npos) ? "--target-env=vulkan1.3" : "--target-env=vulkan1.2";
325+
std::string target_env;
326+
bool is_vulkan11;
327+
#ifdef GGML_VULKAN_MIN_1_1
328+
// Vulkan 1.1 compatibility mode
329+
if (name.find("_cm2") != std::string::npos) {
330+
target_env = "--target-env=vulkan1.3";
331+
is_vulkan11 = false;
332+
} else {
333+
target_env = "--target-env=vulkan1.1";
334+
is_vulkan11 = true;
335+
}
336+
#else
337+
target_env = (name.find("_cm2") != std::string::npos) ? "--target-env=vulkan1.3" : "--target-env=vulkan1.2";
338+
is_vulkan11 = false;
339+
#endif
326340

327341
#ifdef _WIN32
328342
std::vector<std::string> cmd = {GLSLC, "-fshader-stage=compute", target_env, "\"" + in_path + "\"", "-o", "\"" + out_path + "\""};
@@ -333,7 +347,13 @@ void string_to_spv_func(std::string name, std::string in_path, std::string out_p
333347
// disable spirv-opt for coopmat shaders for https://github.com/ggerganov/llama.cpp/issues/10734
334348
// disable spirv-opt for bf16 shaders for https://github.com/ggml-org/llama.cpp/issues/15344
335349
// disable spirv-opt for rope shaders for https://github.com/ggml-org/llama.cpp/issues/16860
336-
if (!coopmat && name.find("bf16") == std::string::npos && name.find("rope") == std::string::npos) {
350+
// disable spirv-opt for RTE shaders with vulkan1.1: spirv-opt rejects RoundingModeRTE with vulkan1.1 target
351+
bool has_rte = (name.find("_rte") != std::string::npos) ||
352+
(defines.find("RTE16") != defines.end() && defines.at("RTE16") == "1");
353+
if (!coopmat &&
354+
name.find("bf16") == std::string::npos &&
355+
name.find("rope") == std::string::npos &&
356+
!(has_rte && is_vulkan11)) {
337357
cmd.push_back("-O");
338358
}
339359

@@ -351,6 +371,11 @@ void string_to_spv_func(std::string name, std::string in_path, std::string out_p
351371
cmd.push_back("-g");
352372
#endif
353373

374+
// Need SPV_KHR_float_controls extension for Vulkan 1.1 RTE shaders
375+
if (is_vulkan11 && has_rte) {
376+
cmd.push_back("-DVULKAN11_RTE=1");
377+
}
378+
354379
for (const auto& define : defines) {
355380
cmd.push_back("-D" + define.first + "=" + define.second);
356381
}

0 commit comments

Comments
 (0)