Skip to content

Commit debc40d

Browse files
committed
ggml: register zdnn as a backend
Signed-off-by: Aaron Teo <[email protected]>
1 parent d709084 commit debc40d

File tree

6 files changed

+387
-11
lines changed

6 files changed

+387
-11
lines changed

ggml/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ option(GGML_OPENCL_PROFILING "ggml: use OpenCL profiling (increas
189189
option(GGML_OPENCL_EMBED_KERNELS "ggml: embed kernels" ON)
190190
option(GGML_OPENCL_USE_ADRENO_KERNELS "ggml: use optimized kernels for Adreno" ON)
191191

192+
option(GGML_ZDNN "ggml: use zDNN accelerator" ON)
193+
192194
# toolchain for vulkan-shaders-gen
193195
set (GGML_VULKAN_SHADERS_GEN_TOOLCHAIN "" CACHE FILEPATH "ggml: toolchain file for vulkan-shaders-gen")
194196

@@ -251,6 +253,7 @@ set(GGML_PUBLIC_HEADERS
251253
include/ggml-rpc.h
252254
include/ggml-sycl.h
253255
include/ggml-vulkan.h
256+
include/ggml-zdnn.h
254257
include/gguf.h)
255258

256259
set_target_properties(ggml PROPERTIES PUBLIC_HEADER "${GGML_PUBLIC_HEADERS}")

ggml/include/ggml-zdnn.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include "ggml.h"
4+
#include "ggml-backend.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#define GGML_ZDNN_BACKEND_NAME "ZDNN"
11+
#define GGML_ZDNN_BACKEND_VERSION ZDNN_VERNUM
12+
13+
GGML_BACKEND_API ggml_backend_t ggml_backend_zdnn_init();
14+
GGML_BACKEND_API bool ggml_backend_is_zdnn();
15+
16+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_zdnn_reg();
17+
18+
#ifdef __cplusplus
19+
}
20+
#endif

ggml/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ ggml_add_backend(RPC)
329329
ggml_add_backend(SYCL)
330330
ggml_add_backend(Vulkan)
331331
ggml_add_backend(OpenCL)
332+
ggml_add_backend(ZDNN)
332333

333334
foreach (target ggml-base ggml)
334335
target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include> $<INSTALL_INTERFACE:include>)

ggml/src/ggml-backend-reg.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
#include "ggml-kompute.h"
6767
#endif
6868

69+
#ifdef GGML_USE_ZDNN
70+
#include "ggml-zdnn.h"
71+
#endif
72+
6973
// disable C++17 deprecation warning for std::codecvt_utf8
7074
#if defined(__clang__)
7175
# pragma clang diagnostic push
@@ -180,6 +184,9 @@ struct ggml_backend_registry {
180184
#ifdef GGML_USE_KOMPUTE
181185
register_backend(ggml_backend_kompute_reg());
182186
#endif
187+
#ifdef GGML_USE_ZDNN
188+
register_backend(ggml_backend_zdnn_reg());
189+
#endif
183190
#ifdef GGML_USE_CPU
184191
register_backend(ggml_backend_cpu_reg());
185192
#endif
@@ -562,18 +569,19 @@ void ggml_backend_load_all_from_path(const char * dir_path) {
562569
bool silent = false;
563570
#endif
564571

565-
ggml_backend_load_best("blas", silent, dir_path);
566-
ggml_backend_load_best("cann", silent, dir_path);
567-
ggml_backend_load_best("cuda", silent, dir_path);
568-
ggml_backend_load_best("hip", silent, dir_path);
572+
ggml_backend_load_best("blas" , silent, dir_path);
573+
ggml_backend_load_best("cann" , silent, dir_path);
574+
ggml_backend_load_best("cuda" , silent, dir_path);
575+
ggml_backend_load_best("hip" , silent, dir_path);
569576
ggml_backend_load_best("kompute", silent, dir_path);
570-
ggml_backend_load_best("metal", silent, dir_path);
571-
ggml_backend_load_best("rpc", silent, dir_path);
572-
ggml_backend_load_best("sycl", silent, dir_path);
573-
ggml_backend_load_best("vulkan", silent, dir_path);
574-
ggml_backend_load_best("opencl", silent, dir_path);
575-
ggml_backend_load_best("musa", silent, dir_path);
576-
ggml_backend_load_best("cpu", silent, dir_path);
577+
ggml_backend_load_best("metal" , silent, dir_path);
578+
ggml_backend_load_best("rpc" , silent, dir_path);
579+
ggml_backend_load_best("sycl" , silent, dir_path);
580+
ggml_backend_load_best("vulkan" , silent, dir_path);
581+
ggml_backend_load_best("opencl" , silent, dir_path);
582+
ggml_backend_load_best("musa" , silent, dir_path);
583+
ggml_backend_load_best("zdnn" , silent, dir_path);
584+
ggml_backend_load_best("cpu" , silent, dir_path);
577585
// check the environment variable GGML_BACKEND_PATH to load an out-of-tree backend
578586
const char * backend_path = std::getenv("GGML_BACKEND_PATH");
579587
if (backend_path) {

ggml/src/ggml-zdnn/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
if (GGML_ZDNN)
2+
file(GLOB GGML_SOURCES_ZDNN "*.cpp")
3+
list(APPEND GGML_HEADERS_ZDNN "../../include/ggml-zdnn.h")
4+
5+
ggml_add_backend_library(ggml-zdnn ${GGML_HEADERS_ZDNN} ${GGML_SOURCES_ZDNN})
6+
add_compile_definitions(GGML_USE_ZDNN) # Double check if required, may already be done by ggml-cpu
7+
8+
if (DEFINED ZDNN_ROOT)
9+
message(STATUS "Using ZDNN_ROOT override: ${ZDNN_ROOT}")
10+
set(ZDNN_HINT "${ZDNN_ROOT}/include")
11+
else()
12+
set(ZDNN_HINT "")
13+
endif()
14+
15+
find_path(ZDNN_INCLUDE_DIR
16+
NAMES zdnn.h
17+
HINTS ${ZDNN_HINT} /usr/include /usr/local/include
18+
)
19+
if (ZDNN_INCLUDE_DIR)
20+
message(STATUS "zDNN found, Includes: ${ZDNN_INCLUDE_DIR}")
21+
else()
22+
message(FATAL_ERROR "zDNN not found, please set ZDNN_ROOT to the proper location if necessary.")
23+
endif()
24+
25+
find_library(ZDNN_LIBRARY_DIR
26+
NAMES zdnn
27+
HINTS ${ZDNN_HINT} /usr/lib /usr/lib64 /usr/local/lib
28+
)
29+
if (ZDNN_LIBRARY_DIR)
30+
message(STATUS "zDNN found, Library: ${ZDNN_LIBRARY_DIR}")
31+
else()
32+
message(FATAL_ERROR "zDNN not found, please set ZDNN_ROOT to the proper location if necessary.")
33+
endif()
34+
35+
target_link_libraries(ggml-zdnn PRIVATE ${ZDNN_LIBRARY_DIR})
36+
target_include_directories(ggml-zdnn PRIVATE ${ZDNN_INCLUDE_DIR})
37+
38+
set(ZDNN_FLAGS -lzdnn)
39+
target_compile_options(ggml-zdnn PRIVATE ${ZDNN_FLAGS})
40+
endif()
41+

0 commit comments

Comments
 (0)