Skip to content

Commit 7f63c13

Browse files
taronaeoggerganov
authored andcommitted
ggml : initial zDNN backend (llama/14975)
1 parent d2a0023 commit 7f63c13

File tree

3 files changed

+979
-0
lines changed

3 files changed

+979
-0
lines changed

src/ggml-zdnn/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
if (DEFINED ZDNN_ROOT)
2+
message(STATUS "zdnn: using ZDNN_ROOT override: ${ZDNN_ROOT}")
3+
set(ZDNN_HINT "${ZDNN_ROOT}")
4+
else()
5+
set(ZDNN_HINT "")
6+
endif()
7+
8+
find_path(ZDNN_INCLUDE
9+
NAMES zdnn.h
10+
HINTS ${ZDNN_HINT} /usr /usr/local
11+
PATH_SUFFIXES include)
12+
if (ZDNN_INCLUDE)
13+
message(STATUS "zdnn: found include: ${ZDNN_INCLUDE}")
14+
else()
15+
message(FATAL_ERROR "zdnn: include directory not found, please set ZDNN_ROOT to the proper path if necessary")
16+
endif()
17+
18+
find_library(ZDNN_LIB
19+
NAMES zdnn
20+
HINTS ${ZDNN_HINT} /usr /usr/local
21+
PATH_SUFFIXES lib lib64)
22+
if (ZDNN_LIB)
23+
message(STATUS "zdnn: found library: ${ZDNN_LIB}")
24+
else()
25+
message(FATAL_ERROR "zdnn: library not found, please set ZDNN_ROOT to the proper path if necessary")
26+
endif()
27+
28+
file(GLOB GGML_SOURCES_ZDNN "*.c" "*.cpp")
29+
file(GLOB GGML_HEADERS_ZDNN "*.h" "*.hpp")
30+
31+
ggml_add_backend_library(ggml-zdnn ${GGML_HEADERS_ZDNN} ${GGML_SOURCES_ZDNN})
32+
target_link_libraries(ggml-zdnn PRIVATE ${ZDNN_LIB})
33+
target_include_directories(ggml-zdnn PRIVATE ${ZDNN_INCLUDE})
34+
target_link_directories(ggml-zdnn PRIVATE ${ZDNN_LIB})
35+
36+
target_compile_definitions(ggml-zdnn PRIVATE GGML_USE_ZDNN)

src/ggml-zdnn/ggml-zdnn-impl.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#ifndef GGML_ZDNN_IMPL
2+
#define GGML_ZDNN_IMPL
3+
4+
#include "zdnn.h"
5+
#include "ggml.h"
6+
#include "ggml-zdnn.h"
7+
8+
#include <vector>
9+
#include <memory>
10+
#include <vecintrin.h>
11+
12+
#define GGML_ZDNN_NAME "zDNN"
13+
#define GGML_ZDNN_VERSION ZDNN_VERNUM
14+
15+
#define vec_neg(a) (-(a)) // Vector Negate
16+
#define vec_add(a, b) ((a) + (b)) // Vector Add
17+
#define vec_sub(a, b) ((a) - (b)) // Vector Subtract
18+
#define vec_mul(a, b) ((a) * (b)) // Vector Multiply
19+
#define vec_div(a, b) ((a) / (b)) // Vector Divide
20+
#define vec_sl(a, b) ((a) << (b)) // Vector Shift Left
21+
#define vec_sra(a, b) ((a) >> (b)) // Vector Shift Right
22+
#define vec_sr(a, b) ((a) >> (b)) // Vector Shift Right Algebraic
23+
#define vec_slo(a, b) vec_slb(a, (b) << 64) // Vector Shift Left by Octet
24+
#define vec_sro(a, b) vec_srb(a, (b) << 64) // Vector Shift Right by Octet
25+
26+
#ifndef vec_and
27+
#define vec_and(a, b) ((a) & (b)) // Vector AND
28+
#endif
29+
30+
#ifndef vec_or
31+
#define vec_or(a, b) ((a) | (b)) // Vector OR
32+
#endif
33+
34+
#ifndef vec_xor
35+
#define vec_xor(a, b) ((a) ^ (b)) // Vector XOR
36+
#endif
37+
38+
typedef signed char char8x16_t __attribute__((vector_size(16)));
39+
typedef unsigned char uchar8x16_t __attribute__((vector_size(16)));
40+
41+
typedef int8_t int8x16_t __attribute__((vector_size(16)));
42+
typedef int16_t int16x8_t __attribute__((vector_size(16)));
43+
typedef int32_t int32x4_t __attribute__((vector_size(16)));
44+
typedef uint8_t uint8x16_t __attribute__((vector_size(16)));
45+
typedef uint16_t uint16x8_t __attribute__((vector_size(16)));
46+
typedef uint32_t uint32x4_t __attribute__((vector_size(16)));
47+
48+
typedef float float32x4_t __attribute__((vector_size(16)));
49+
typedef double double64x2_t __attribute__((vector_size(16)));
50+
51+
typedef signed long long long64x2_t __attribute__((vector_size(16)));
52+
typedef unsigned long long ulong64x2_t __attribute__((vector_size(16)));
53+
54+
#define ZDNN_CHECK(stmt) \
55+
do { \
56+
zdnn_status status = (stmt); \
57+
GGML_ASSERT(status == ZDNN_OK); \
58+
} while (0);
59+
60+
struct ggml_backend_zdnn_device_context {
61+
int zdnn_device;
62+
int zdnn_device_ref_count;
63+
64+
bool has_parmblkformat_0;
65+
bool has_parmblkformat_1;
66+
67+
size_t max_size;
68+
69+
char name[128];
70+
};
71+
72+
struct ggml_backend_zdnn_context {
73+
int device;
74+
ggml_cgraph * gf;
75+
};
76+
77+
struct ggml_backend_zdnn_buffer {
78+
void * data;
79+
size_t size;
80+
81+
zdnn_tensor_desc pre_tfm_desc;
82+
zdnn_tensor_desc tfm_desc;
83+
zdnn_ztensor ztensor;
84+
85+
char name[GGML_MAX_NAME];
86+
};
87+
88+
struct ggml_backend_zdnn_buffer_context {
89+
void * all_data;
90+
size_t all_size;
91+
bool owned;
92+
93+
int n_buffers;
94+
std::vector<std::unique_ptr<ggml_backend_zdnn_buffer>> buffers;
95+
};
96+
97+
#endif // GGML_ZDNN_IMPL

0 commit comments

Comments
 (0)