Spaces:
Running
Running
ggml : initial zDNN backend (llama/14975)
Browse files
ggml/src/ggml-zdnn/CMakeLists.txt
ADDED
|
@@ -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)
|
ggml/src/ggml-zdnn/ggml-zdnn-impl.h
ADDED
|
@@ -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
|
ggml/src/ggml-zdnn/ggml-zdnn.cpp
ADDED
|
@@ -0,0 +1,846 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "zdnn.h"
|
| 2 |
+
#include "ggml-zdnn.h"
|
| 3 |
+
#include "ggml-zdnn-impl.h"
|
| 4 |
+
|
| 5 |
+
#include "ggml-impl.h"
|
| 6 |
+
#include "ggml-backend-impl.h"
|
| 7 |
+
|
| 8 |
+
#include <vector>
|
| 9 |
+
#include <memory>
|
| 10 |
+
#include <csignal>
|
| 11 |
+
#include <unistd.h>
|
| 12 |
+
|
| 13 |
+
inline zdnn_data_types ggml_zdnn_type_mapping(ggml_type type) {
|
| 14 |
+
switch (type) {
|
| 15 |
+
case GGML_TYPE_F32:
|
| 16 |
+
return FP32;
|
| 17 |
+
case GGML_TYPE_F16:
|
| 18 |
+
return FP16;
|
| 19 |
+
case GGML_TYPE_BF16:
|
| 20 |
+
return BFLOAT;
|
| 21 |
+
case GGML_TYPE_I8:
|
| 22 |
+
return INT8;
|
| 23 |
+
case GGML_TYPE_I32:
|
| 24 |
+
return INT32;
|
| 25 |
+
case GGML_TYPE_Q8_0:
|
| 26 |
+
return INT8;
|
| 27 |
+
default:
|
| 28 |
+
GGML_ABORT("%s: fatal: unable to determine zTensor data type",
|
| 29 |
+
__func__);
|
| 30 |
+
break;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
inline void ggml_zdnn_create_tensor(zdnn_tensor_desc & pre_tfm_desc,
|
| 35 |
+
zdnn_tensor_desc & tfm_desc,
|
| 36 |
+
zdnn_ztensor & ztensor,
|
| 37 |
+
const ggml_tensor * src,
|
| 38 |
+
const int64_t * ne,
|
| 39 |
+
const zdnn_data_layouts layout) {
|
| 40 |
+
zdnn_init_pre_transformed_desc(
|
| 41 |
+
layout,
|
| 42 |
+
ggml_zdnn_type_mapping(src->type),
|
| 43 |
+
&pre_tfm_desc,
|
| 44 |
+
ne[3], ne[2], ne[1], ne[0]
|
| 45 |
+
);
|
| 46 |
+
|
| 47 |
+
ZDNN_CHECK(zdnn_generate_transformed_desc(&pre_tfm_desc, &tfm_desc));
|
| 48 |
+
ZDNN_CHECK(zdnn_init_ztensor_with_malloc(&pre_tfm_desc, &tfm_desc, &ztensor));
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
inline void ggml_zdnn_load_tensor(zdnn_ztensor & ztensor,
|
| 52 |
+
void * buffer) {
|
| 53 |
+
ZDNN_CHECK(zdnn_transform_ztensor(&ztensor, buffer));
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
inline void ggml_zdnn_init_tensor(ggml_backend_zdnn_buffer * buffer, const ggml_tensor * tensor) {
|
| 57 |
+
switch (tensor->op) {
|
| 58 |
+
case GGML_OP_MUL_MAT:
|
| 59 |
+
{
|
| 60 |
+
zdnn_init_pre_transformed_desc(
|
| 61 |
+
ZDNN_2D,
|
| 62 |
+
ggml_zdnn_type_mapping(tensor->type),
|
| 63 |
+
&buffer->pre_tfm_desc,
|
| 64 |
+
tensor->ne[1], tensor->ne[0]
|
| 65 |
+
);
|
| 66 |
+
} break;
|
| 67 |
+
|
| 68 |
+
default:
|
| 69 |
+
{
|
| 70 |
+
// For 4D tensors, GGML uses NCHW layout. However, because zDNN
|
| 71 |
+
// automatically transforms everything to NHWC, we will use it
|
| 72 |
+
// directly to avoid the performance penalty changing the
|
| 73 |
+
// layout and reshaping the tensor.
|
| 74 |
+
zdnn_init_pre_transformed_desc(
|
| 75 |
+
ZDNN_NHWC,
|
| 76 |
+
ggml_zdnn_type_mapping(tensor->type),
|
| 77 |
+
&buffer->pre_tfm_desc,
|
| 78 |
+
tensor->ne[3], tensor->ne[2], tensor->ne[1], tensor->ne[0]
|
| 79 |
+
);
|
| 80 |
+
|
| 81 |
+
// TODO: Consider adding a ggml check.
|
| 82 |
+
// TODO: If tensor = 4D, use ZDNN_NCHW by default.
|
| 83 |
+
// TODO: If tensor = 2D, use ZDNN_NHWC by default.
|
| 84 |
+
} break;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
ZDNN_CHECK(zdnn_generate_transformed_desc(&buffer->pre_tfm_desc, &buffer->tfm_desc));
|
| 88 |
+
ZDNN_CHECK(zdnn_init_ztensor_with_malloc(&buffer->pre_tfm_desc, &buffer->tfm_desc, &buffer->ztensor));
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
static void ggml_zdnn_mul_mat_op(ggml_backend_zdnn_context * ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
|
| 92 |
+
GGML_TENSOR_BINARY_OP_LOCALS;
|
| 93 |
+
|
| 94 |
+
const enum ggml_type type = src0->type;
|
| 95 |
+
|
| 96 |
+
GGML_ASSERT(ne0 == ne01);
|
| 97 |
+
GGML_ASSERT(ne1 == ne11);
|
| 98 |
+
GGML_ASSERT(ne2 == ne12);
|
| 99 |
+
GGML_ASSERT(ne3 == ne13);
|
| 100 |
+
|
| 101 |
+
// we don't support permuted src0 or src1
|
| 102 |
+
GGML_ASSERT(nb00 == ggml_type_size(type));
|
| 103 |
+
GGML_ASSERT(nb10 == ggml_type_size(src1->type));
|
| 104 |
+
|
| 105 |
+
// dst cannot be transposed or permuted
|
| 106 |
+
GGML_ASSERT(nb0 == sizeof(float));
|
| 107 |
+
GGML_ASSERT(nb0 <= nb1);
|
| 108 |
+
GGML_ASSERT(nb1 <= nb2);
|
| 109 |
+
GGML_ASSERT(nb2 <= nb3);
|
| 110 |
+
|
| 111 |
+
const ggml_tensor * weights = src0;
|
| 112 |
+
const ggml_tensor * inputs = src1;
|
| 113 |
+
ggml_tensor * output = dst;
|
| 114 |
+
|
| 115 |
+
ggml_backend_zdnn_buffer * weights_extra = (ggml_backend_zdnn_buffer *)weights->extra;
|
| 116 |
+
ggml_backend_zdnn_buffer * inputs_extra = (ggml_backend_zdnn_buffer *)inputs->extra;
|
| 117 |
+
ggml_backend_zdnn_buffer * output_extra = (ggml_backend_zdnn_buffer *)output->extra;
|
| 118 |
+
|
| 119 |
+
zdnn_tensor_desc ptd_bias, td_bias;
|
| 120 |
+
zdnn_ztensor zt_bias;
|
| 121 |
+
|
| 122 |
+
const int64_t weights_rows = ne01;
|
| 123 |
+
const int64_t weights_cols = ne00;
|
| 124 |
+
const int64_t inputs_rows = ne11;
|
| 125 |
+
const int64_t inputs_cols = ne10;
|
| 126 |
+
|
| 127 |
+
assert(inputs_cols == weights_cols);
|
| 128 |
+
|
| 129 |
+
const int64_t output_rows = ne1;
|
| 130 |
+
const int64_t output_cols = ne0;
|
| 131 |
+
|
| 132 |
+
const int64_t bias_dim [GGML_MAX_DIMS] = { 1, 1, 1, output_cols };
|
| 133 |
+
ggml_zdnn_create_tensor(ptd_bias, td_bias, zt_bias, output, bias_dim, ZDNN_1D);
|
| 134 |
+
|
| 135 |
+
void * bias_data = (void *)calloc(ne0, ggml_element_size(output));
|
| 136 |
+
if (weights_extra->ztensor.is_transformed == false) ggml_zdnn_load_tensor(weights_extra->ztensor, weights->data);
|
| 137 |
+
if (inputs_extra->ztensor.is_transformed == false) ggml_zdnn_load_tensor(inputs_extra->ztensor, inputs->data);
|
| 138 |
+
ggml_zdnn_load_tensor(zt_bias, bias_data);
|
| 139 |
+
|
| 140 |
+
// GGML_LOG_INFO("%s: tensor '%s' tensor dimensions: [%ld, %ld, %ld, %ld] pre_tfm_desc dimensions: [%ld, %ld, %ld, %ld]\n",
|
| 141 |
+
// __func__, weights_extra->name,
|
| 142 |
+
// weights->ne[3], weights->ne[2], weights->ne[1], weights->ne[0],
|
| 143 |
+
// weights_extra->pre_tfm_desc.dim1,
|
| 144 |
+
// weights_extra->pre_tfm_desc.dim2,
|
| 145 |
+
// weights_extra->pre_tfm_desc.dim3,
|
| 146 |
+
// weights_extra->pre_tfm_desc.dim4);
|
| 147 |
+
|
| 148 |
+
// GGML_LOG_INFO("%s: tensor '%s' tensor dimensions: [%ld, %ld, %ld, %ld] pre_tfm_desc dimensions: [%ld, %ld, %ld, %ld]\n",
|
| 149 |
+
// __func__, inputs_extra->name,
|
| 150 |
+
// inputs->ne[3], inputs->ne[2], inputs->ne[1], inputs->ne[0],
|
| 151 |
+
// inputs_extra->pre_tfm_desc.dim1,
|
| 152 |
+
// inputs_extra->pre_tfm_desc.dim2,
|
| 153 |
+
// inputs_extra->pre_tfm_desc.dim3,
|
| 154 |
+
// inputs_extra->pre_tfm_desc.dim4);
|
| 155 |
+
|
| 156 |
+
GGML_ASSERT(weights_extra->pre_tfm_desc.dim1 == weights->ne[0] && "weights_extra->pre_tfm_desc.dim1 must match weights->ne[0]");
|
| 157 |
+
GGML_ASSERT(weights_extra->pre_tfm_desc.dim2 == weights->ne[1] && "weights_extra->pre_tfm_desc.dim2 must match weights->ne[1]");
|
| 158 |
+
GGML_ASSERT(inputs_extra->pre_tfm_desc.dim1 == inputs->ne[0] && "inputs_extra->pre_tfm_desc.dim1 must match inputs->ne[0]");
|
| 159 |
+
GGML_ASSERT(inputs_extra->pre_tfm_desc.dim2 == inputs->ne[1] && "inputs_extra->pre_tfm_desc.dim2 must match inputs->ne[1]");
|
| 160 |
+
|
| 161 |
+
ZDNN_CHECK(zdnn_matmul_transpose_op(&inputs_extra->ztensor, &weights_extra->ztensor, &zt_bias,
|
| 162 |
+
false, true, MATMUL_OP_ADDITION, &output_extra->ztensor));
|
| 163 |
+
// TODO: Remove in the future as we are currently DLF16 -> FP32 then in the next op, FP32 -> DLF16 again. Inefficient.
|
| 164 |
+
ZDNN_CHECK(zdnn_transform_origtensor(&output_extra->ztensor, output->data));
|
| 165 |
+
|
| 166 |
+
ZDNN_CHECK(zdnn_free_ztensor_buffer(&zt_bias));
|
| 167 |
+
free(bias_data);
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
static void ggml_zdnn_mul_mat_dispatch(ggml_backend_zdnn_context * ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
|
| 171 |
+
bool use_mul_mat_vec =
|
| 172 |
+
(src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_F16)
|
| 173 |
+
&& src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
|
| 174 |
+
&& src0->ne[0] % 2 == 0 && src1->ne[1] == 1;
|
| 175 |
+
|
| 176 |
+
bool use_mul_mat_vec_q =
|
| 177 |
+
ggml_is_quantized(src0->type)
|
| 178 |
+
&& src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
|
| 179 |
+
|
| 180 |
+
bool use_mul_mat_q =
|
| 181 |
+
ggml_is_quantized(src0->type)
|
| 182 |
+
&& src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
|
| 183 |
+
|
| 184 |
+
// debug helpers
|
| 185 |
+
// GGML_LOG_INFO("%s: use_mul_mat_vec = %d\n", __func__, use_mul_mat_vec);
|
| 186 |
+
// GGML_LOG_INFO("%s: use_mul_mat_vec_q = %d\n", __func__, use_mul_mat_vec_q);
|
| 187 |
+
// GGML_LOG_INFO("%s: use_mul_mat_q = %d\n", __func__, use_mul_mat_q);
|
| 188 |
+
// GGML_LOG_INFO("%s: src0: %8d %8d %8d %8d\n", __func__, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
|
| 189 |
+
// GGML_LOG_INFO("%s: %8d %8d %8d %8d\n", __func__, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
|
| 190 |
+
// GGML_LOG_INFO("%s: src1: %8d %8d %8d %8d\n", __func__, src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
|
| 191 |
+
// GGML_LOG_INFO("%s: %8d %8d %8d %8d\n", __func__, src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
|
| 192 |
+
// GGML_LOG_INFO("%s: src0 is contiguous %d, transposed %d, type = %s, name = %s\n", __func__, ggml_is_contiguous(src0), ggml_is_transposed(src0), ggml_type_name(src0->type), src0->name);
|
| 193 |
+
// GGML_LOG_INFO("%s: src1 is contiguous %d, transposed %d, type = %s, name = %s\n", __func__, ggml_is_contiguous(src1), ggml_is_transposed(src1), ggml_type_name(src1->type), src1->name);
|
| 194 |
+
|
| 195 |
+
if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16
|
| 196 |
+
&& !ggml_is_transposed(src0) && !ggml_is_transposed(src1)
|
| 197 |
+
&& src1->ne[2] * src1->ne[3] > 1) {
|
| 198 |
+
// general KQ + KQV multi-batch
|
| 199 |
+
GGML_LOG_INFO("%s: using zdnn_mul_mat_batched for KQ + KQV multi-batch\n", __func__);
|
| 200 |
+
// ggml_zdnn_mul_mat_batched(ctx, src0, src1, dst);
|
| 201 |
+
} else if (use_mul_mat_vec) {
|
| 202 |
+
GGML_LOG_INFO("%s: using zdnn_op_mul_mat_vec for vector multiplication\n", __func__);
|
| 203 |
+
// ggml_zdnn_op_mul_mat(ctx, src0, src1, dst, ggml_zdnn_op_mul_mat_vec, nullptr);
|
| 204 |
+
} else if (use_mul_mat_vec_q) {
|
| 205 |
+
GGML_LOG_INFO("%s: using zdnn_op_mul_mat_vec_q for quantized vector multiplication\n", __func__);
|
| 206 |
+
// ggml_zdnn_op_mul_mat(ctx, src0, src1, dst, ggml_zdnn_op_mul_mat_vec_q, ggml_zdnn_quantize_row_q8_1);
|
| 207 |
+
} else if (use_mul_mat_q) {
|
| 208 |
+
GGML_LOG_INFO("%s: using zdnn_op_mul_mat_q for quantized matrix multiplication\n", __func__);
|
| 209 |
+
// ggml_zdnn_op_mul_mat(ctx, src0, src1, dst, ggml_zdnn_op_mul_mat_q, ggml_zdnn_quantize_mmq_q8_1);
|
| 210 |
+
} else {
|
| 211 |
+
// GGML_LOG_INFO("%s: using zdnn_op_mul_mat for general matrix multiplication\n", __func__);
|
| 212 |
+
ggml_zdnn_mul_mat_op(ctx, src0, src1, dst);
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
static bool ggml_zdnn_compute_forward(ggml_backend_zdnn_context * ctx, ggml_tensor * dst) {
|
| 217 |
+
switch (dst->op) {
|
| 218 |
+
case GGML_OP_MUL_MAT:
|
| 219 |
+
ggml_zdnn_mul_mat_dispatch(ctx, dst->src[0], dst->src[1], dst);
|
| 220 |
+
break;
|
| 221 |
+
|
| 222 |
+
default:
|
| 223 |
+
return false;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
return true;
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
static enum ggml_status ggml_zdnn_graph_compute(ggml_backend_t backend, ggml_cgraph * gf) {
|
| 230 |
+
ggml_backend_zdnn_context * ctx = ( ggml_backend_zdnn_context *)backend->context;
|
| 231 |
+
ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *)backend->device->context;
|
| 232 |
+
|
| 233 |
+
ctx->gf = gf;
|
| 234 |
+
for (int i = 0; i < gf->n_nodes; i++) {
|
| 235 |
+
ggml_tensor * node = gf->nodes[i];
|
| 236 |
+
|
| 237 |
+
if (ggml_is_empty(node)
|
| 238 |
+
|| node->op == GGML_OP_NONE
|
| 239 |
+
|| node->op == GGML_OP_RESHAPE
|
| 240 |
+
|| node->op == GGML_OP_VIEW
|
| 241 |
+
|| node->op == GGML_OP_PERMUTE
|
| 242 |
+
|| node->op == GGML_OP_TRANSPOSE) {
|
| 243 |
+
continue;
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
bool ok = ggml_zdnn_compute_forward(ctx, node);
|
| 247 |
+
if (!ok) {
|
| 248 |
+
GGML_LOG_ERROR("%s: unsupported op %s (%s)\n",
|
| 249 |
+
__func__, node->name, ggml_op_name(node->op));
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
GGML_ASSERT(ok);
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
return GGML_STATUS_SUCCESS;
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
static bool ggml_zdnn_supports_op(const ggml_backend_zdnn_device_context * ctx_dev, const ggml_tensor * op) {
|
| 259 |
+
switch (op->op) {
|
| 260 |
+
case GGML_OP_NONE:
|
| 261 |
+
case GGML_OP_RESHAPE:
|
| 262 |
+
case GGML_OP_VIEW:
|
| 263 |
+
case GGML_OP_TRANSPOSE:
|
| 264 |
+
case GGML_OP_PERMUTE:
|
| 265 |
+
return true;
|
| 266 |
+
|
| 267 |
+
case GGML_OP_MUL_MAT:
|
| 268 |
+
{
|
| 269 |
+
const ggml_tensor * src0 = op->src[0];
|
| 270 |
+
const ggml_tensor * src1 = op->src[1];
|
| 271 |
+
|
| 272 |
+
const int64_t ne10 = src1->ne[0];
|
| 273 |
+
const int64_t ne0 = op->ne[0];
|
| 274 |
+
const int64_t ne1 = op->ne[1];
|
| 275 |
+
|
| 276 |
+
const int64_t max_batch = ctx_dev->max_size;
|
| 277 |
+
|
| 278 |
+
return ggml_is_matrix(src0) &&
|
| 279 |
+
ggml_is_matrix(src1) &&
|
| 280 |
+
ggml_is_contiguous(src0) &&
|
| 281 |
+
ggml_is_contiguous(src1) &&
|
| 282 |
+
src0->view_src == nullptr && src1->view_src == nullptr &&
|
| 283 |
+
src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 &&
|
| 284 |
+
(ne0 <= max_batch && ne1 <= max_batch && ne10 <= max_batch);
|
| 285 |
+
} break;
|
| 286 |
+
|
| 287 |
+
default:
|
| 288 |
+
return false;
|
| 289 |
+
}
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
////////////////////////////////////////////////////////////////////////////////
|
| 293 |
+
|
| 294 |
+
//
|
| 295 |
+
// globals
|
| 296 |
+
//
|
| 297 |
+
|
| 298 |
+
// initialised in ggml_backend_zdnn_reg
|
| 299 |
+
static ggml_backend_reg g_ggml_backend_zdnn_reg;
|
| 300 |
+
static ggml_backend_device g_ggml_backend_zdnn_device;
|
| 301 |
+
|
| 302 |
+
static ggml_backend_zdnn_device_context g_ggml_ctx_dev_main = {
|
| 303 |
+
/* .zdnn_device = */ 0,
|
| 304 |
+
/* .zdnn_device_ref_count = */ 0,
|
| 305 |
+
/* .has_parmblkformat_0 = */ false,
|
| 306 |
+
/* .has_parmblkformat_1 = */ false,
|
| 307 |
+
/* .max_size = */ 0,
|
| 308 |
+
/* .name = */ "",
|
| 309 |
+
};
|
| 310 |
+
|
| 311 |
+
static int ggml_backend_zdnn_device_acq(ggml_backend_zdnn_device_context * ctx) {
|
| 312 |
+
assert(ctx != NULL);
|
| 313 |
+
|
| 314 |
+
if (ctx->zdnn_device == 0) {
|
| 315 |
+
ctx->zdnn_device = 1;
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
if (ctx->zdnn_device >= 1) {
|
| 319 |
+
ctx->has_parmblkformat_0 = zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_0);
|
| 320 |
+
ctx->has_parmblkformat_1 = zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_1);
|
| 321 |
+
ctx->max_size = zdnn_get_nnpa_max_dim_idx_size();
|
| 322 |
+
strncpy(ctx->name, GGML_ZDNN_NAME, sizeof(ctx->name) - 1);
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
ctx->zdnn_device_ref_count++;
|
| 326 |
+
return ctx->zdnn_device;
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
static void ggml_backend_zdnn_device_rel(ggml_backend_zdnn_device_context * ctx) {
|
| 330 |
+
assert(ctx != NULL);
|
| 331 |
+
assert(ctx->zdnn_device_ref_count > 0);
|
| 332 |
+
|
| 333 |
+
ctx->zdnn_device_ref_count--;
|
| 334 |
+
if (ctx->zdnn_device_ref_count == 0) {
|
| 335 |
+
if (ctx->zdnn_device >= 0) {
|
| 336 |
+
ctx->zdnn_device = 0;
|
| 337 |
+
}
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
static ggml_backend_zdnn_context * ggml_zdnn_init(ggml_backend_dev_t dev) {
|
| 342 |
+
GGML_LOG_INFO("%s: allocating\n", __func__);
|
| 343 |
+
GGML_LOG_INFO("%s: found 1 device\n", __func__);
|
| 344 |
+
|
| 345 |
+
#ifdef STATIC_LIB
|
| 346 |
+
zdnn_init();
|
| 347 |
+
#endif
|
| 348 |
+
|
| 349 |
+
ggml_backend_zdnn_context * ctx = new ggml_backend_zdnn_context();
|
| 350 |
+
ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *)dev->context;
|
| 351 |
+
|
| 352 |
+
int device = 1;
|
| 353 |
+
GGML_LOG_INFO("%s: picking default device: %s\n", __func__, ctx_dev->name);
|
| 354 |
+
|
| 355 |
+
ctx->device = device;
|
| 356 |
+
GGML_LOG_INFO("%s: NNPA name: %s\n", __func__, ctx_dev->name);
|
| 357 |
+
GGML_LOG_INFO("%s: NNPA_PARMBLKFORMAT_0 = %s\n", __func__, ctx_dev->has_parmblkformat_0 ? "true" : "false");
|
| 358 |
+
GGML_LOG_INFO("%s: NNPA_PARMBLKFORMAT_1 = %s\n", __func__, ctx_dev->has_parmblkformat_1 ? "true" : "false");
|
| 359 |
+
|
| 360 |
+
ctx->gf = nullptr;
|
| 361 |
+
|
| 362 |
+
return ctx;
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
static void ggml_zdnn_free(ggml_backend_zdnn_context * ctx) {
|
| 366 |
+
GGML_LOG_INFO("%s: deallocating\n", __func__);
|
| 367 |
+
delete ctx;
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
//
|
| 371 |
+
// backend interface
|
| 372 |
+
//
|
| 373 |
+
|
| 374 |
+
static void ggml_backend_zdnn_buffer_free_buffer(ggml_backend_buffer_t buffer) {
|
| 375 |
+
ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context;
|
| 376 |
+
|
| 377 |
+
for (int i = 0; i < ctx->n_buffers; i++) {
|
| 378 |
+
if (ctx->buffers[i]->ztensor.buffer != NULL && ctx->buffers[i]->ztensor.is_transformed) {
|
| 379 |
+
ZDNN_CHECK(zdnn_free_ztensor_buffer(&ctx->buffers[i]->ztensor));
|
| 380 |
+
}
|
| 381 |
+
}
|
| 382 |
+
|
| 383 |
+
delete ctx;
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
static void * ggml_backend_zdnn_buffer_get_base(ggml_backend_buffer_t buffer) {
|
| 387 |
+
ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context;
|
| 388 |
+
return ctx->all_data;
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
static enum ggml_status ggml_backend_zdnn_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
|
| 392 |
+
if (tensor->view_src != NULL) {
|
| 393 |
+
assert(tensor->view_src->buffer->buft == buffer->buft);
|
| 394 |
+
return GGML_STATUS_SUCCESS;
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context;
|
| 398 |
+
|
| 399 |
+
const int64_t tsize = ggml_nbytes(tensor);
|
| 400 |
+
int buffer_idx = ctx->n_buffers;
|
| 401 |
+
|
| 402 |
+
std::unique_ptr<ggml_backend_zdnn_buffer> zdnn_buffer = std::make_unique<ggml_backend_zdnn_buffer>();
|
| 403 |
+
zdnn_buffer->data = tensor->data;
|
| 404 |
+
zdnn_buffer->size = tsize;
|
| 405 |
+
strncpy(zdnn_buffer->name, tensor->name, GGML_MAX_NAME - 1);
|
| 406 |
+
|
| 407 |
+
ggml_zdnn_init_tensor(zdnn_buffer.get(), tensor);
|
| 408 |
+
tensor->extra = zdnn_buffer.get();
|
| 409 |
+
|
| 410 |
+
ctx->buffers.push_back(std::move(zdnn_buffer));
|
| 411 |
+
ctx->n_buffers++;
|
| 412 |
+
|
| 413 |
+
// GGML_LOG_INFO("%s: initialised tensor '%s' in buffer %d, size = %8.2f MiB\n",
|
| 414 |
+
// __func__, tensor->name, buffer_idx, tsize);
|
| 415 |
+
|
| 416 |
+
return GGML_STATUS_SUCCESS;
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
static void ggml_backend_zdnn_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
|
| 420 |
+
memset((char *)tensor->data + offset, value, size);
|
| 421 |
+
|
| 422 |
+
GGML_UNUSED(buffer);
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
static void ggml_backend_zdnn_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
|
| 426 |
+
memcpy((char *)tensor->data + offset, data, size);
|
| 427 |
+
|
| 428 |
+
GGML_UNUSED(buffer);
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
static void ggml_backend_zdnn_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) {
|
| 432 |
+
memcpy(data, (const char *)tensor->data + offset, size);
|
| 433 |
+
|
| 434 |
+
GGML_UNUSED(buffer);
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
static void ggml_backend_zdnn_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
|
| 438 |
+
ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context;
|
| 439 |
+
|
| 440 |
+
memset(ctx->all_data, value, ctx->all_size);
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
static ggml_backend_buffer_i ggml_backend_zdnn_buffer_i = {
|
| 444 |
+
/* .free_buffer = */ ggml_backend_zdnn_buffer_free_buffer,
|
| 445 |
+
/* .get_base = */ ggml_backend_zdnn_buffer_get_base,
|
| 446 |
+
/* .init_tensor = */ ggml_backend_zdnn_buffer_init_tensor,
|
| 447 |
+
/* .memset_tensor = */ ggml_backend_zdnn_buffer_memset_tensor,
|
| 448 |
+
/* .set_tensor = */ ggml_backend_zdnn_buffer_set_tensor,
|
| 449 |
+
/* .get_tensor = */ ggml_backend_zdnn_buffer_get_tensor,
|
| 450 |
+
/* .cpy_tensor = */ NULL,
|
| 451 |
+
/* .clear = */ ggml_backend_zdnn_buffer_clear,
|
| 452 |
+
/* .reset = */ NULL,
|
| 453 |
+
};
|
| 454 |
+
|
| 455 |
+
//
|
| 456 |
+
// default buffer type
|
| 457 |
+
//
|
| 458 |
+
|
| 459 |
+
static const char * ggml_backend_zdnn_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
|
| 460 |
+
return GGML_ZDNN_NAME;
|
| 461 |
+
|
| 462 |
+
GGML_UNUSED(buft);
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
static ggml_backend_buffer_t ggml_backend_zdnn_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
|
| 466 |
+
ggml_backend_zdnn_buffer_context * ctx = new ggml_backend_zdnn_buffer_context();
|
| 467 |
+
|
| 468 |
+
const size_t size_page = sysconf(_SC_PAGESIZE);
|
| 469 |
+
|
| 470 |
+
size_t size_aligned = size;
|
| 471 |
+
if ((size_aligned % size_page) != 0) {
|
| 472 |
+
size_aligned += size_page - (size_aligned % size_page);
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *)buft->device->context;
|
| 476 |
+
|
| 477 |
+
GGML_ASSERT(ctx_dev->zdnn_device >= 0);
|
| 478 |
+
int device = ctx_dev->zdnn_device; GGML_UNUSED(device);
|
| 479 |
+
|
| 480 |
+
ctx->all_data = ggml_aligned_malloc(size_aligned);
|
| 481 |
+
ctx->all_size = size_aligned;
|
| 482 |
+
ctx->owned = true;
|
| 483 |
+
ctx->n_buffers = 1;
|
| 484 |
+
|
| 485 |
+
if (ctx->all_data != NULL) {
|
| 486 |
+
std::unique_ptr<ggml_backend_zdnn_buffer> zdnn_buffer = std::make_unique<ggml_backend_zdnn_buffer>();
|
| 487 |
+
zdnn_buffer->data = ctx->all_data;
|
| 488 |
+
zdnn_buffer->size = size_aligned;
|
| 489 |
+
ctx->buffers.push_back(std::move(zdnn_buffer));
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
if (size_aligned > 0 && (ctx->all_data == NULL)) {
|
| 493 |
+
GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f\n",
|
| 494 |
+
__func__, size_aligned / 1024.0 / 1024.0);
|
| 495 |
+
delete ctx;
|
| 496 |
+
return NULL;
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
return ggml_backend_buffer_init(buft, ggml_backend_zdnn_buffer_i, ctx, size);
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
static size_t ggml_backend_zdnn_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
|
| 503 |
+
return 256;
|
| 504 |
+
|
| 505 |
+
GGML_UNUSED(buft);
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
static bool ggml_backend_zdnn_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
|
| 509 |
+
return true;
|
| 510 |
+
|
| 511 |
+
GGML_UNUSED(buft);
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
ggml_backend_buffer_type_t ggml_backend_zdnn_buffer_type(void) {
|
| 515 |
+
static ggml_backend_buffer_type ggml_backend_buffer_type_zdnn = {
|
| 516 |
+
/* .iface = */ {
|
| 517 |
+
/* .get_name = */ ggml_backend_zdnn_buffer_type_get_name,
|
| 518 |
+
/* .alloc_buffer = */ ggml_backend_zdnn_buffer_type_alloc_buffer,
|
| 519 |
+
/* .get_alignment = */ ggml_backend_zdnn_buffer_type_get_alignment,
|
| 520 |
+
/* .get_max_size = */ NULL,
|
| 521 |
+
/* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
|
| 522 |
+
/* .is_host = */ ggml_backend_zdnn_buffer_type_is_host,
|
| 523 |
+
},
|
| 524 |
+
/* .device = */ &g_ggml_backend_zdnn_device,
|
| 525 |
+
/* .context = */ NULL,
|
| 526 |
+
};
|
| 527 |
+
|
| 528 |
+
return &ggml_backend_buffer_type_zdnn;
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
static const char * ggml_backend_zdnn_buffer_from_ptr_type_get_name(ggml_backend_buffer_type_t buft) {
|
| 532 |
+
return GGML_ZDNN_NAME "_Mapped";
|
| 533 |
+
|
| 534 |
+
GGML_UNUSED(buft);
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
static ggml_backend_buffer_type_t ggml_backend_zdnn_buffer_from_ptr_type(void) {
|
| 538 |
+
static ggml_backend_buffer_type ggml_backend_buffer_from_ptr_type_zdnn = {
|
| 539 |
+
/* .iface = */ {
|
| 540 |
+
/* .get_name = */ ggml_backend_zdnn_buffer_from_ptr_type_get_name,
|
| 541 |
+
/* .alloc_buffer = */ ggml_backend_zdnn_buffer_type_alloc_buffer,
|
| 542 |
+
/* .get_alignment = */ ggml_backend_zdnn_buffer_type_get_alignment,
|
| 543 |
+
/* .get_max_size = */ NULL,
|
| 544 |
+
/* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
|
| 545 |
+
/* .is_host = */ ggml_backend_zdnn_buffer_type_is_host,
|
| 546 |
+
},
|
| 547 |
+
/* .device = */ &g_ggml_backend_zdnn_device,
|
| 548 |
+
/* .context = */ NULL,
|
| 549 |
+
};
|
| 550 |
+
|
| 551 |
+
return &ggml_backend_buffer_from_ptr_type_zdnn;
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
//
|
| 555 |
+
// backend
|
| 556 |
+
//
|
| 557 |
+
|
| 558 |
+
static const char * ggml_backend_zdnn_name(ggml_backend_t backend) {
|
| 559 |
+
return GGML_ZDNN_NAME;
|
| 560 |
+
|
| 561 |
+
GGML_UNUSED(backend);
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
static void ggml_backend_zdnn_free(ggml_backend_t backend) {
|
| 565 |
+
ggml_backend_zdnn_context * ctx = (ggml_backend_zdnn_context *)backend->context;
|
| 566 |
+
|
| 567 |
+
ggml_zdnn_free(ctx);
|
| 568 |
+
free(backend);
|
| 569 |
+
}
|
| 570 |
+
|
| 571 |
+
static enum ggml_status ggml_backend_zdnn_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
|
| 572 |
+
return ggml_zdnn_graph_compute(backend, cgraph);
|
| 573 |
+
}
|
| 574 |
+
|
| 575 |
+
static ggml_backend_i ggml_backend_zdnn_i = {
|
| 576 |
+
/* .get_name = */ ggml_backend_zdnn_name,
|
| 577 |
+
/* .free = */ ggml_backend_zdnn_free,
|
| 578 |
+
/* .set_tensor_async = */ NULL,
|
| 579 |
+
/* .get_tensor_async = */ NULL,
|
| 580 |
+
/* .cpy_tensor_async = */ NULL,
|
| 581 |
+
/* .synchronize = */ NULL,
|
| 582 |
+
/* .graph_plan_create = */ NULL,
|
| 583 |
+
/* .graph_plan_free = */ NULL,
|
| 584 |
+
/* .graph_plan_update = */ NULL,
|
| 585 |
+
/* .graph_plan_compute = */ NULL,
|
| 586 |
+
/* .graph_compute = */ ggml_backend_zdnn_graph_compute,
|
| 587 |
+
/* .event_record = */ NULL,
|
| 588 |
+
/* .event_wait = */ NULL,
|
| 589 |
+
};
|
| 590 |
+
|
| 591 |
+
static ggml_guid_t ggml_backend_zdnn_guid(void) {
|
| 592 |
+
static const char * guid_str = "IBM-ZDNN-ACCELER";
|
| 593 |
+
return reinterpret_cast<ggml_guid_t>((void *)guid_str);
|
| 594 |
+
}
|
| 595 |
+
|
| 596 |
+
// TODO: remove in the future
|
| 597 |
+
ggml_backend_t ggml_backend_zdnn_init(void) {
|
| 598 |
+
ggml_backend_dev_t dev = ggml_backend_reg_dev_get(ggml_backend_zdnn_reg(), 0);
|
| 599 |
+
|
| 600 |
+
ggml_backend_zdnn_context * ctx = ggml_zdnn_init(dev);
|
| 601 |
+
if (ctx == NULL) {
|
| 602 |
+
GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__);
|
| 603 |
+
return NULL;
|
| 604 |
+
}
|
| 605 |
+
|
| 606 |
+
ggml_backend_t backend = (ggml_backend_t)malloc(sizeof(ggml_backend));
|
| 607 |
+
*backend = (ggml_backend) {
|
| 608 |
+
/* .guid = */ ggml_backend_zdnn_guid(),
|
| 609 |
+
/* .iface = */ ggml_backend_zdnn_i,
|
| 610 |
+
/* .device = */ dev,
|
| 611 |
+
/* .context = */ ctx,
|
| 612 |
+
};
|
| 613 |
+
|
| 614 |
+
return backend;
|
| 615 |
+
}
|
| 616 |
+
|
| 617 |
+
bool ggml_backend_is_zdnn(ggml_backend_t backend) {
|
| 618 |
+
return backend != NULL &&
|
| 619 |
+
ggml_guid_matches(backend->guid, ggml_backend_zdnn_guid());
|
| 620 |
+
|
| 621 |
+
GGML_UNUSED(backend);
|
| 622 |
+
}
|
| 623 |
+
|
| 624 |
+
//
|
| 625 |
+
// backend device
|
| 626 |
+
//
|
| 627 |
+
|
| 628 |
+
static const char * ggml_backend_zdnn_device_get_name(ggml_backend_dev_t dev) {
|
| 629 |
+
return GGML_ZDNN_NAME;
|
| 630 |
+
|
| 631 |
+
GGML_UNUSED(dev);
|
| 632 |
+
}
|
| 633 |
+
|
| 634 |
+
static const char * ggml_backend_zdnn_device_get_description(ggml_backend_dev_t dev) {
|
| 635 |
+
return "IBM Z Neural Network Processing Assist (NNPA)";
|
| 636 |
+
}
|
| 637 |
+
|
| 638 |
+
static void ggml_backend_zdnn_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
|
| 639 |
+
*free = 0;
|
| 640 |
+
*total = 0;
|
| 641 |
+
}
|
| 642 |
+
|
| 643 |
+
static enum ggml_backend_dev_type ggml_backend_zdnn_device_get_type(ggml_backend_dev_t dev) {
|
| 644 |
+
return GGML_BACKEND_DEVICE_TYPE_ACCEL;
|
| 645 |
+
|
| 646 |
+
GGML_UNUSED(dev);
|
| 647 |
+
}
|
| 648 |
+
|
| 649 |
+
static void ggml_backend_zdnn_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) {
|
| 650 |
+
props->name = ggml_backend_zdnn_device_get_name(dev);
|
| 651 |
+
props->description = ggml_backend_zdnn_device_get_description(dev);
|
| 652 |
+
props->type = ggml_backend_zdnn_device_get_type(dev);
|
| 653 |
+
ggml_backend_zdnn_device_get_memory(dev, &props->memory_free, &props->memory_total);
|
| 654 |
+
props->caps = (ggml_backend_dev_caps) {
|
| 655 |
+
/* .async = */ false,
|
| 656 |
+
/* .host_buffer = */ false,
|
| 657 |
+
/* .buffer_from_host_ptr = */ true,
|
| 658 |
+
/* .events = */ false,
|
| 659 |
+
};
|
| 660 |
+
}
|
| 661 |
+
|
| 662 |
+
static ggml_backend_t ggml_backend_zdnn_device_init(ggml_backend_dev_t dev, const char * params) {
|
| 663 |
+
ggml_backend_zdnn_context * ctx = ggml_zdnn_init(dev);
|
| 664 |
+
if (ctx == NULL) {
|
| 665 |
+
GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__);
|
| 666 |
+
return NULL;
|
| 667 |
+
}
|
| 668 |
+
|
| 669 |
+
ggml_backend_t backend = (ggml_backend *)malloc(sizeof(ggml_backend));
|
| 670 |
+
*backend = (ggml_backend) {
|
| 671 |
+
/* .guid = */ ggml_backend_zdnn_guid(),
|
| 672 |
+
/* .iface = */ ggml_backend_zdnn_i,
|
| 673 |
+
/* .device = */ dev,
|
| 674 |
+
/* .context = */ ctx,
|
| 675 |
+
};
|
| 676 |
+
|
| 677 |
+
return backend;
|
| 678 |
+
|
| 679 |
+
GGML_UNUSED(params);
|
| 680 |
+
}
|
| 681 |
+
|
| 682 |
+
static ggml_backend_buffer_type_t ggml_backend_zdnn_device_get_buffer_type(ggml_backend_dev_t dev) {
|
| 683 |
+
return ggml_backend_zdnn_buffer_type();
|
| 684 |
+
|
| 685 |
+
GGML_UNUSED(dev);
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
static ggml_backend_buffer_t ggml_backend_zdnn_device_buffer_from_ptr(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) {
|
| 689 |
+
ggml_backend_zdnn_buffer_context * ctx = new ggml_backend_zdnn_buffer_context();
|
| 690 |
+
|
| 691 |
+
ctx->all_data = ptr;
|
| 692 |
+
ctx->all_size = size;
|
| 693 |
+
ctx->owned = false;
|
| 694 |
+
ctx->n_buffers = 0;
|
| 695 |
+
|
| 696 |
+
const size_t size_page = sysconf(_SC_PAGESIZE);
|
| 697 |
+
|
| 698 |
+
// page-align the data ptr
|
| 699 |
+
{
|
| 700 |
+
const uintptr_t offs = (uintptr_t) ptr % size_page;
|
| 701 |
+
ptr = (void *)((char *)ptr - offs);
|
| 702 |
+
size += offs;
|
| 703 |
+
}
|
| 704 |
+
|
| 705 |
+
size_t size_aligned = size;
|
| 706 |
+
if ((size_aligned % size_page) != 0) {
|
| 707 |
+
size_aligned += size_page - (size_aligned % size_page);
|
| 708 |
+
}
|
| 709 |
+
|
| 710 |
+
ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *)dev->context;
|
| 711 |
+
|
| 712 |
+
GGML_ASSERT(ctx_dev->zdnn_device >= 0);
|
| 713 |
+
int device = ctx_dev->zdnn_device; GGML_UNUSED(device);
|
| 714 |
+
|
| 715 |
+
std::unique_ptr<ggml_backend_zdnn_buffer> zdnn_buffer = std::make_unique<ggml_backend_zdnn_buffer>();
|
| 716 |
+
zdnn_buffer->data = ptr;
|
| 717 |
+
zdnn_buffer->size = size;
|
| 718 |
+
ctx->buffers.push_back(std::move(zdnn_buffer));
|
| 719 |
+
|
| 720 |
+
GGML_LOG_INFO("%s: allocated buffer, size = %8.2f MiB\n",
|
| 721 |
+
__func__, size_aligned / 1024.0 / 1024.0);
|
| 722 |
+
|
| 723 |
+
++ctx->n_buffers;
|
| 724 |
+
|
| 725 |
+
return ggml_backend_buffer_init(ggml_backend_zdnn_buffer_from_ptr_type(), ggml_backend_zdnn_buffer_i, ctx, size);
|
| 726 |
+
}
|
| 727 |
+
|
| 728 |
+
static bool ggml_backend_zdnn_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
|
| 729 |
+
ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *) dev->context;
|
| 730 |
+
|
| 731 |
+
return ggml_zdnn_supports_op(ctx_dev, op);
|
| 732 |
+
}
|
| 733 |
+
|
| 734 |
+
static bool ggml_backend_zdnn_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
|
| 735 |
+
return
|
| 736 |
+
buft->iface.get_name == ggml_backend_zdnn_buffer_type_get_name ||
|
| 737 |
+
buft->iface.get_name == ggml_backend_zdnn_buffer_from_ptr_type_get_name;
|
| 738 |
+
|
| 739 |
+
GGML_UNUSED(dev);
|
| 740 |
+
}
|
| 741 |
+
|
| 742 |
+
static ggml_backend_device_i ggml_backend_zdnn_device_i = {
|
| 743 |
+
/* .get_name = */ ggml_backend_zdnn_device_get_name,
|
| 744 |
+
/* .get_description = */ ggml_backend_zdnn_device_get_description,
|
| 745 |
+
/* .get_memory = */ ggml_backend_zdnn_device_get_memory,
|
| 746 |
+
/* .get_type = */ ggml_backend_zdnn_device_get_type,
|
| 747 |
+
/* .get_props = */ ggml_backend_zdnn_device_get_props,
|
| 748 |
+
/* .init_backend = */ ggml_backend_zdnn_device_init,
|
| 749 |
+
/* .get_buffer_type = */ ggml_backend_zdnn_device_get_buffer_type,
|
| 750 |
+
/* .get_host_buffer_type = */ NULL,
|
| 751 |
+
/* .buffer_from_host_ptr = */ ggml_backend_zdnn_device_buffer_from_ptr,
|
| 752 |
+
/* .supports_op = */ ggml_backend_zdnn_device_supports_op,
|
| 753 |
+
/* .supports_buft = */ ggml_backend_zdnn_device_supports_buft,
|
| 754 |
+
/* .offload_op = */ NULL,
|
| 755 |
+
/* .event_new = */ NULL,
|
| 756 |
+
/* .event_free = */ NULL,
|
| 757 |
+
/* .event_synchronize = */ NULL,
|
| 758 |
+
};
|
| 759 |
+
|
| 760 |
+
//
|
| 761 |
+
// backend registry
|
| 762 |
+
//
|
| 763 |
+
|
| 764 |
+
static const char * ggml_backend_zdnn_reg_get_name(ggml_backend_reg_t reg) {
|
| 765 |
+
return GGML_ZDNN_NAME;
|
| 766 |
+
|
| 767 |
+
GGML_UNUSED(reg);
|
| 768 |
+
}
|
| 769 |
+
|
| 770 |
+
static size_t ggml_backend_zdnn_reg_device_count(ggml_backend_reg_t reg) {
|
| 771 |
+
if (!zdnn_is_nnpa_installed()) {
|
| 772 |
+
return 0;
|
| 773 |
+
}
|
| 774 |
+
return 1;
|
| 775 |
+
|
| 776 |
+
GGML_UNUSED(reg);
|
| 777 |
+
}
|
| 778 |
+
|
| 779 |
+
static ggml_backend_dev_t ggml_backend_zdnn_reg_device_get(ggml_backend_reg_t reg, size_t index) {
|
| 780 |
+
GGML_ASSERT(index == 0);
|
| 781 |
+
|
| 782 |
+
return &g_ggml_backend_zdnn_device;
|
| 783 |
+
|
| 784 |
+
GGML_UNUSED(reg);
|
| 785 |
+
GGML_UNUSED(index);
|
| 786 |
+
}
|
| 787 |
+
|
| 788 |
+
static ggml_backend_feature g_ggml_backend_zdnn_features[] = {
|
| 789 |
+
{ "NNPA", zdnn_is_nnpa_installed() ? "1" : "0" },
|
| 790 |
+
{ "NNPA_PARMBLKFORMAT_0", zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_0) ? "1" : "0" },
|
| 791 |
+
{ "NNPA_PARMBLKFORMAT_1", zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_1) ? "1" : "0" },
|
| 792 |
+
{ NULL, NULL },
|
| 793 |
+
};
|
| 794 |
+
|
| 795 |
+
static ggml_backend_feature * ggml_backend_zdnn_get_features(ggml_backend_reg_t reg) {
|
| 796 |
+
return g_ggml_backend_zdnn_features;
|
| 797 |
+
|
| 798 |
+
GGML_UNUSED(reg);
|
| 799 |
+
}
|
| 800 |
+
|
| 801 |
+
static void * ggml_backend_zdnn_get_proc_address(ggml_backend_reg_t reg, const char * name) {
|
| 802 |
+
if (strcmp(name, "ggml_backend_get_features") == 0) {
|
| 803 |
+
return (void *) ggml_backend_zdnn_get_features;
|
| 804 |
+
}
|
| 805 |
+
|
| 806 |
+
return NULL;
|
| 807 |
+
|
| 808 |
+
GGML_UNUSED(reg);
|
| 809 |
+
}
|
| 810 |
+
|
| 811 |
+
static ggml_backend_reg_i ggml_backend_zdnn_reg_i = {
|
| 812 |
+
/* .get_name = */ ggml_backend_zdnn_reg_get_name,
|
| 813 |
+
/* .get_device_count = */ ggml_backend_zdnn_reg_device_count,
|
| 814 |
+
/* .get_device = */ ggml_backend_zdnn_reg_device_get,
|
| 815 |
+
/* .get_proc_address = */ ggml_backend_zdnn_get_proc_address,
|
| 816 |
+
};
|
| 817 |
+
|
| 818 |
+
static void ggml_zdnn_cleanup(void) {
|
| 819 |
+
ggml_backend_zdnn_device_rel(&g_ggml_ctx_dev_main);
|
| 820 |
+
}
|
| 821 |
+
|
| 822 |
+
// TODO: make thread-safe
|
| 823 |
+
ggml_backend_reg_t ggml_backend_zdnn_reg(void) {
|
| 824 |
+
ggml_backend_zdnn_device_acq(&g_ggml_ctx_dev_main);
|
| 825 |
+
|
| 826 |
+
// register cleanup callback
|
| 827 |
+
atexit(ggml_zdnn_cleanup);
|
| 828 |
+
|
| 829 |
+
{
|
| 830 |
+
g_ggml_backend_zdnn_reg = (ggml_backend_reg) {
|
| 831 |
+
/* .api_version = */ GGML_ZDNN_VERSION,
|
| 832 |
+
/* .iface = */ ggml_backend_zdnn_reg_i,
|
| 833 |
+
/* .context = */ NULL,
|
| 834 |
+
};
|
| 835 |
+
|
| 836 |
+
g_ggml_backend_zdnn_device = (ggml_backend_device) {
|
| 837 |
+
/* .iface = */ ggml_backend_zdnn_device_i,
|
| 838 |
+
/* .reg = */ &g_ggml_backend_zdnn_reg,
|
| 839 |
+
/* .context = */ &g_ggml_ctx_dev_main,
|
| 840 |
+
};
|
| 841 |
+
|
| 842 |
+
return &g_ggml_backend_zdnn_reg;
|
| 843 |
+
}
|
| 844 |
+
}
|
| 845 |
+
|
| 846 |
+
GGML_BACKEND_DL_IMPL(ggml_backend_zdnn_reg)
|