Skip to content

Commit 60651ca

Browse files
sohzmCloudwalk9SkutteOlegleejet
authored andcommitted
feat: add vulkan backend support (leejet#291)
* Fix includes and init vulkan the same as llama.cpp * Add Windows Vulkan CI * Updated ggml submodule * support epsilon as a parameter for ggml_group_norm --------- Co-authored-by: Cloudwalk <cloudwalk@icculus.org> Co-authored-by: Oleg Skutte <00.00.oleg.00.00@gmail.com> Co-authored-by: leejet <leejet714@gmail.com>
1 parent b3f86c7 commit 60651ca

File tree

7 files changed

+44
-3
lines changed

7 files changed

+44
-3
lines changed

.github/workflows/build.yml

+13
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ jobs:
148148
windows-latest-cmake:
149149
runs-on: windows-2019
150150

151+
env:
152+
VULKAN_VERSION: 1.3.261.1
153+
151154
strategy:
152155
matrix:
153156
include:
@@ -163,6 +166,8 @@ jobs:
163166
defines: "-DSD_CUBLAS=ON -DSD_BUILD_SHARED_LIBS=ON"
164167
- build: "rocm5.5"
165168
defines: '-G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DSD_HIPBLAS=ON -DCMAKE_BUILD_TYPE=Release -DAMDGPU_TARGETS="gfx1100;gfx1102;gfx1030" -DSD_BUILD_SHARED_LIBS=ON'
169+
- build: 'vulkan'
170+
defines: "-DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON"
166171
steps:
167172
- name: Clone
168173
id: checkout
@@ -192,6 +197,14 @@ jobs:
192197
uses: urkle/action-get-ninja@v1
193198
with:
194199
version: 1.11.1
200+
- name: Install Vulkan SDK
201+
id: get_vulkan
202+
if: ${{ matrix.build == 'vulkan' }}
203+
run: |
204+
curl.exe -o $env:RUNNER_TEMP/VulkanSDK-Installer.exe -L "https://sdk.lunarg.com/sdk/download/${env:VULKAN_VERSION}/windows/VulkanSDK-${env:VULKAN_VERSION}-Installer.exe"
205+
& "$env:RUNNER_TEMP\VulkanSDK-Installer.exe" --accept-licenses --default-answer --confirm-command install
206+
Add-Content $env:GITHUB_ENV "VULKAN_SDK=C:\VulkanSDK\${env:VULKAN_VERSION}"
207+
Add-Content $env:GITHUB_PATH "C:\VulkanSDK\${env:VULKAN_VERSION}\bin"
195208
196209
- name: Build
197210
id: cmake_build

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ option(SD_BUILD_EXAMPLES "sd: build examples" ${SD_STANDALONE})
2727
option(SD_CUBLAS "sd: cuda backend" OFF)
2828
option(SD_HIPBLAS "sd: rocm backend" OFF)
2929
option(SD_METAL "sd: metal backend" OFF)
30+
option(SD_VULKAN "sd: vulkan backend" OFF)
3031
option(SD_SYCL "sd: sycl backend" OFF)
3132
option(SD_FLASH_ATTN "sd: use flash attention for x4 less memory usage" OFF)
3233
option(SD_FAST_SOFTMAX "sd: x1.5 faster softmax, indeterministic (sometimes, same seed don't generate same image), cuda only" OFF)
@@ -45,6 +46,12 @@ if(SD_METAL)
4546
add_definitions(-DSD_USE_METAL)
4647
endif()
4748

49+
if (SD_VULKAN)
50+
message("Use Vulkan as backend stable-diffusion")
51+
set(GGML_VULKAN ON)
52+
add_definitions(-DSD_USE_VULKAN)
53+
endif ()
54+
4855
if (SD_HIPBLAS)
4956
message("Use HIPBLAS as backend stable-diffusion")
5057
set(GGML_HIPBLAS ON)

ggml

Submodule ggml updated from a06c683 to 21f9e5c

ggml_extend.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "ggml-metal.h"
3333
#endif
3434

35+
#ifdef SD_USE_VULKAN
36+
#include "ggml-vulkan.h"
37+
#endif
38+
3539
#ifdef SD_USE_SYCL
3640
#include "ggml-sycl.h"
3741
#endif
@@ -655,7 +659,7 @@ __STATIC_INLINE__ struct ggml_tensor* ggml_nn_attention(struct ggml_context* ctx
655659
struct ggml_tensor* k,
656660
struct ggml_tensor* v,
657661
bool mask = false) {
658-
#if defined(SD_USE_FLASH_ATTENTION) && !defined(SD_USE_CUBLAS) && !defined(SD_USE_METAL) && !defined(SD_USE_SYCL)
662+
#if defined(SD_USE_FLASH_ATTENTION) && !defined(SD_USE_CUBLAS) && !defined(SD_USE_METAL) && !defined(SD_USE_VULKAN) && !defined(SD_USE_SYCL)
659663
struct ggml_tensor* kqv = ggml_flash_attn(ctx, q, k, v, false); // [N * n_head, n_token, d_head]
660664
#else
661665
float d_head = (float)q->ne[0];

model.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include "ggml-metal.h"
2222
#endif
2323

24+
#ifdef SD_USE_VULKAN
25+
#include "ggml-vulkan.h"
26+
#endif
27+
2428
#define ST_HEADER_SIZE_LEN 8
2529

2630
uint64_t read_u64(uint8_t* buffer) {

stable-diffusion.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ class StableDiffusionGGML {
160160
ggml_backend_metal_log_set_callback(ggml_log_callback_default, nullptr);
161161
backend = ggml_backend_metal_init();
162162
#endif
163+
#ifdef SD_USE_VULKAN
164+
LOG_DEBUG("Using Vulkan backend");
165+
for (int device = 0; device < ggml_backend_vk_get_device_count(); ++device) {
166+
backend = ggml_backend_vk_init(device);
167+
}
168+
if(!backend) {
169+
LOG_WARN("Failed to initialize Vulkan backend");
170+
}
171+
#endif
163172
#ifdef SD_USE_SYCL
164173
LOG_DEBUG("Using SYCL backend");
165174
backend = ggml_backend_sycl_init(0);
@@ -170,7 +179,7 @@ class StableDiffusionGGML {
170179
backend = ggml_backend_cpu_init();
171180
}
172181
#ifdef SD_USE_FLASH_ATTENTION
173-
#if defined(SD_USE_CUBLAS) || defined(SD_USE_METAL) || defined(SD_USE_SYCL)
182+
#if defined(SD_USE_CUBLAS) || defined(SD_USE_METAL) || defined (SD_USE_SYCL) || defined(SD_USE_VULKAN)
174183
LOG_WARN("Flash Attention not supported with GPU Backend");
175184
#else
176185
LOG_INFO("Flash Attention enabled");

upscaler.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ struct UpscalerGGML {
2424
ggml_backend_metal_log_set_callback(ggml_log_callback_default, nullptr);
2525
backend = ggml_backend_metal_init();
2626
#endif
27+
#ifdef SD_USE_VULKAN
28+
LOG_DEBUG("Using Vulkan backend");
29+
backend = ggml_backend_vk_init(0);
30+
#endif
2731
#ifdef SD_USE_SYCL
2832
LOG_DEBUG("Using SYCL backend");
2933
backend = ggml_backend_sycl_init(0);

0 commit comments

Comments
 (0)