ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-zdnn / ggml-zdnn.cpp
| 1 | #include "ggml-zdnn.h" |
| 2 | #include "ggml-impl.h" |
| 3 | #include "ggml-backend-impl.h" |
| 4 | |
| 5 | #include "ggml-zdnn/common.hpp" |
| 6 | #include "ggml-zdnn/fattn.hpp" |
| 7 | #include "ggml-zdnn/mmf.hpp" |
| 8 | #include "ggml-zdnn/mmq.hpp" |
| 9 | #include "ggml-zdnn/utils.hpp" |
| 10 | #include "ggml.h" |
| 11 | |
| 12 | #include <vector> |
| 13 | #include <memory> |
| 14 | #include <csignal> // raise(SIGTRAP) |
| 15 | #include <thread> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | static void ggml_zdnn_compute_forward_mul_mat( |
| 19 | ggml_backend_zdnn_context * ctx, |
| 20 | ggml_tensor * dst) { |
| 21 | |
| 22 | const ggml_tensor * src0 = dst->src[0]; // weights |
| 23 | const ggml_tensor * src1 = dst->src[1]; // inputs |
| 24 | |
| 25 | if (ggml_is_quantized(src0->type)) { |
| 26 | ggml_zdnn_mul_mat_q(ctx, src0, src1, dst); |
| 27 | } else { |
| 28 | ggml_zdnn_mul_mat_f(ctx, src0, src1, dst); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static bool ggml_zdnn_compute_forward( |
| 33 | ggml_backend_zdnn_context * ctx, |
| 34 | ggml_tensor * dst) { |
| 35 | |
| 36 | switch (dst->op) { |
| 37 | case GGML_OP_MUL_MAT: |
| 38 | { |
| 39 | ggml_zdnn_compute_forward_mul_mat(ctx, dst); |
| 40 | } break; |
| 41 | |
| 42 | case GGML_OP_FLASH_ATTN_EXT: |
| 43 | { |
| 44 | ggml_zdnn_flash_attn_ext(ctx, dst); |
| 45 | } break; |
| 46 | |
| 47 | default: |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | static enum ggml_status ggml_zdnn_graph_compute(ggml_backend_t backend, ggml_cgraph * gf) { |
| 55 | ggml_backend_zdnn_context * ctx = ( ggml_backend_zdnn_context *)backend->context; |
| 56 | ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *)backend->device->context; |
| 57 | |
| 58 | ctx->gf = gf; |
| 59 | for (int i = 0; i < gf->n_nodes; i++) { |
| 60 | ggml_tensor * node = gf->nodes[i]; |
| 61 | |
| 62 | if (ggml_is_empty(node) |
| 63 | || node->op == GGML_OP_NONE |
| 64 | || node->op == GGML_OP_RESHAPE |
| 65 | || node->op == GGML_OP_VIEW |
| 66 | || node->op == GGML_OP_PERMUTE |
| 67 | || node->op == GGML_OP_TRANSPOSE) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if ((node->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | bool ok = ggml_zdnn_compute_forward(ctx, node); |
| 76 | if (!ok) { |
| 77 | GGML_LOG_ERROR("%s: unsupported op %s (%s)\n", |
| 78 | __func__, node->name, ggml_op_name(node->op)); |
| 79 | } |
| 80 | |
| 81 | GGML_ASSERT(ok); |
| 82 | } |
| 83 | |
| 84 | return GGML_STATUS_SUCCESS; |
| 85 | |
| 86 | GGML_UNUSED(ctx_dev); |
| 87 | } |
| 88 | |
| 89 | static int64_t ggml_zdnn_mul_mat_min_batch(ggml_type type, int32_t n_threads) { |
| 90 | switch (type) { |
| 91 | case GGML_TYPE_Q8_0: |
| 92 | return std::max<int64_t>(1, n_threads - 2); |
| 93 | default: |
| 94 | return 1; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static bool ggml_zdnn_supports_op(const ggml_backend_zdnn_device_context * ctx_dev, const ggml_tensor * op) { |
| 99 | switch (op->op) { |
| 100 | case GGML_OP_NONE: |
| 101 | case GGML_OP_RESHAPE: |
| 102 | case GGML_OP_VIEW: |
| 103 | case GGML_OP_TRANSPOSE: |
| 104 | case GGML_OP_PERMUTE: |
| 105 | return true; |
| 106 | |
| 107 | case GGML_OP_MUL_MAT: |
| 108 | { |
| 109 | const ggml_tensor * weights = op->src[0]; |
| 110 | const ggml_tensor * inputs = op->src[1]; |
| 111 | |
| 112 | const int64_t ne10 = inputs->ne[0]; |
| 113 | const int64_t ne0 = op->ne[0]; |
| 114 | const int64_t ne1 = op->ne[1]; |
| 115 | |
| 116 | const int64_t max_batch = ctx_dev->max_size; |
| 117 | |
| 118 | if (!ggml_is_matrix(weights) || !ggml_is_matrix(inputs) || |
| 119 | !ggml_is_contiguous(weights) || !ggml_is_contiguous(inputs) || |
| 120 | weights->view_src != nullptr || |
| 121 | ne0 > max_batch || ne1 > max_batch || ne10 > max_batch) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | // offload smaller batches to the CPU |
| 126 | if (ne1 < ggml_zdnn_mul_mat_min_batch(weights->type, ctx_dev->n_threads)) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | switch (weights->type) { |
| 131 | case GGML_TYPE_F32: |
| 132 | case GGML_TYPE_F16: |
| 133 | case GGML_TYPE_BF16: |
| 134 | // the inputs ztensor is used, and views have none |
| 135 | return inputs->view_src == nullptr; |
| 136 | case GGML_TYPE_Q8_0: |
| 137 | // the inputs are quantized from their data, so views work as well |
| 138 | return ctx_dev->has_parmblkformat_1 && inputs->type == GGML_TYPE_F32; |
| 139 | default: |
| 140 | return false; |
| 141 | } |
| 142 | } break; |
| 143 | |
| 144 | case GGML_OP_FLASH_ATTN_EXT: |
| 145 | return ggml_zdnn_supports_flash_attn_ext(ctx_dev, op); |
| 146 | |
| 147 | default: |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | //////////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | // |
| 155 | // globals |
| 156 | // |
| 157 | |
| 158 | // initialised in ggml_backend_zdnn_reg |
| 159 | static ggml_backend_reg g_ggml_backend_zdnn_reg; |
| 160 | static ggml_backend_device g_ggml_backend_zdnn_device; |
| 161 | |
| 162 | static ggml_backend_zdnn_device_context g_ggml_ctx_dev_main = { |
| 163 | /* .zdnn_device = */ 0, |
| 164 | /* .zdnn_device_ref_count = */ 0, |
| 165 | /* .has_parmblkformat_0 = */ false, |
| 166 | /* .has_parmblkformat_1 = */ false, |
| 167 | /* .max_size = */ 0, |
| 168 | /* .n_threads = */ 1, |
| 169 | /* .name = */ "", |
| 170 | }; |
| 171 | |
| 172 | static int ggml_backend_zdnn_device_acq(ggml_backend_zdnn_device_context * ctx) { |
| 173 | assert(ctx != NULL); |
| 174 | |
| 175 | if (ctx->zdnn_device == 0) { |
| 176 | ctx->zdnn_device = 1; |
| 177 | } |
| 178 | |
| 179 | if (ctx->zdnn_device >= 1) { |
| 180 | ctx->has_parmblkformat_0 = zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_0); |
| 181 | ctx->has_parmblkformat_1 = zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_1); |
| 182 | ctx->max_size = zdnn_get_nnpa_max_dim_idx_size(); |
| 183 | // llama.cpp uses every core by default, and splits the first graph before it sets the threads |
| 184 | ctx->n_threads = std::max<int32_t>(1, (int32_t)std::thread::hardware_concurrency()); |
| 185 | strncpy(ctx->name, GGML_ZDNN_NAME, sizeof(ctx->name) - 1); |
| 186 | } |
| 187 | |
| 188 | ctx->zdnn_device_ref_count++; |
| 189 | return ctx->zdnn_device; |
| 190 | } |
| 191 | |
| 192 | static void ggml_backend_zdnn_device_rel(ggml_backend_zdnn_device_context * ctx) { |
| 193 | assert(ctx != NULL); |
| 194 | assert(ctx->zdnn_device_ref_count > 0); |
| 195 | |
| 196 | ctx->zdnn_device_ref_count--; |
| 197 | if (ctx->zdnn_device_ref_count == 0) { |
| 198 | if (ctx->zdnn_device >= 0) { |
| 199 | ctx->zdnn_device = 0; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | static ggml_backend_zdnn_context * ggml_zdnn_init(ggml_backend_dev_t dev) { |
| 205 | GGML_LOG_INFO("%s: allocating\n", __func__); |
| 206 | GGML_LOG_INFO("%s: found 1 device\n", __func__); |
| 207 | |
| 208 | #ifdef STATIC_LIB |
| 209 | zdnn_init(); |
| 210 | #endif |
| 211 | |
| 212 | ggml_backend_zdnn_context * ctx = new ggml_backend_zdnn_context(); |
| 213 | ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *)dev->context; |
| 214 | |
| 215 | int device = 1; |
| 216 | GGML_LOG_INFO("%s: picking default device: %s\n", __func__, ctx_dev->name); |
| 217 | |
| 218 | ctx->device = device; |
| 219 | GGML_LOG_INFO("%s: NNPA name: %s\n", __func__, ctx_dev->name); |
| 220 | GGML_LOG_INFO("%s: NNPA_PARMBLKFORMAT_0 = %s\n", __func__, ctx_dev->has_parmblkformat_0 ? "true" : "false"); |
| 221 | GGML_LOG_INFO("%s: NNPA_PARMBLKFORMAT_1 = %s\n", __func__, ctx_dev->has_parmblkformat_1 ? "true" : "false"); |
| 222 | |
| 223 | ctx->n_threads = GGML_DEFAULT_N_THREADS; |
| 224 | ctx->gf = nullptr; |
| 225 | |
| 226 | return ctx; |
| 227 | } |
| 228 | |
| 229 | static void ggml_zdnn_free(ggml_backend_zdnn_context * ctx) { |
| 230 | GGML_LOG_INFO("%s: deallocating\n", __func__); |
| 231 | |
| 232 | for (const auto & it : ctx->zero_bias) { |
| 233 | ZDNN_CHECK(zdnn_free_ztensor_buffer(&it.second->ztensor)); |
| 234 | } |
| 235 | |
| 236 | delete ctx; |
| 237 | } |
| 238 | |
| 239 | // |
| 240 | // backend interface |
| 241 | // |
| 242 | |
| 243 | static void ggml_backend_zdnn_buffer_free_buffer(ggml_backend_buffer_t buffer) { |
| 244 | ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context; |
| 245 | |
| 246 | for (const auto & buf_ptr : ctx->buffers) { |
| 247 | ggml_backend_zdnn_buffer * buf = buf_ptr.get(); |
| 248 | |
| 249 | // Free any extra buffer allocated for the tensor. E.g., bias for GGML_OP_MUL_MAT |
| 250 | if (buf->extra != nullptr) free(buf->extra->data); |
| 251 | if (buf->ztensor.buffer_size > 0) ZDNN_CHECK(zdnn_free_ztensor_buffer(&buf->ztensor)); |
| 252 | } |
| 253 | |
| 254 | if (ctx->owned) { |
| 255 | ggml_aligned_free(ctx->all_data, ctx->all_size); |
| 256 | } |
| 257 | |
| 258 | delete ctx; |
| 259 | } |
| 260 | |
| 261 | static void * ggml_backend_zdnn_buffer_get_base(ggml_backend_buffer_t buffer) { |
| 262 | ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context; |
| 263 | return ctx->all_data; |
| 264 | } |
| 265 | |
| 266 | static enum ggml_status ggml_backend_zdnn_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) { |
| 267 | if (tensor->view_src != NULL) { |
| 268 | assert(tensor->view_src->buffer->buft == buffer->buft); |
| 269 | return GGML_STATUS_SUCCESS; |
| 270 | } |
| 271 | |
| 272 | // reject empty tensors to avoid zDNN crash |
| 273 | if (ggml_is_empty(tensor)) { |
| 274 | return GGML_STATUS_SUCCESS; |
| 275 | } |
| 276 | |
| 277 | ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context; |
| 278 | |
| 279 | const int64_t tsize = ggml_nbytes(tensor); |
| 280 | int buffer_idx = ctx->n_buffers; |
| 281 | |
| 282 | std::unique_ptr<ggml_backend_zdnn_buffer> zdnn_buffer = std::make_unique<ggml_backend_zdnn_buffer>(); |
| 283 | zdnn_buffer->data = tensor->data; |
| 284 | zdnn_buffer->size = tsize; |
| 285 | zdnn_buffer->extra = nullptr; |
| 286 | snprintf(zdnn_buffer->name, GGML_MAX_NAME, "%s", tensor->name); |
| 287 | |
| 288 | ggml_zdnn_init_tensor(zdnn_buffer.get(), tensor); |
| 289 | tensor->extra = zdnn_buffer.get(); |
| 290 | |
| 291 | switch (tensor->op) { |
| 292 | case GGML_OP_MUL_MAT: |
| 293 | { |
| 294 | std::unique_ptr<ggml_backend_zdnn_buffer> zdnn_bias_buffer = std::make_unique<ggml_backend_zdnn_buffer>(); |
| 295 | zdnn_bias_buffer->data = (void *)calloc(tensor->ne[0], ggml_element_size(tensor)); |
| 296 | zdnn_bias_buffer->size = ggml_element_size(tensor) * tensor->ne[0]; |
| 297 | snprintf(zdnn_bias_buffer->name, GGML_MAX_NAME, "%.*s (bias)", |
| 298 | GGML_MAX_NAME - (int)sizeof(" (bias)"), tensor->name); |
| 299 | |
| 300 | const int64_t bias_dim[GGML_MAX_DIMS] = { 1, 1, 1, tensor->ne[0] }; |
| 301 | ggml_zdnn_create_tensor(zdnn_bias_buffer->pre_tfm_desc, |
| 302 | zdnn_bias_buffer->tfm_desc, |
| 303 | zdnn_bias_buffer->ztensor, |
| 304 | tensor, bias_dim, ZDNN_1D); |
| 305 | |
| 306 | ggml_zdnn_load_tensor(zdnn_bias_buffer->ztensor, zdnn_bias_buffer->data); |
| 307 | zdnn_buffer->extra = zdnn_bias_buffer.get(); |
| 308 | |
| 309 | ctx->buffers.push_back(std::move(zdnn_bias_buffer)); |
| 310 | ctx->n_buffers++; |
| 311 | } break; |
| 312 | default: |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | ctx->buffers.push_back(std::move(zdnn_buffer)); |
| 317 | ctx->n_buffers++; |
| 318 | |
| 319 | // GGML_LOG_INFO("%s: initialised tensor '%s' in buffer %d, size = %8.2f MiB\n", |
| 320 | // __func__, tensor->name, buffer_idx, tsize); |
| 321 | |
| 322 | return GGML_STATUS_SUCCESS; |
| 323 | |
| 324 | GGML_UNUSED(buffer_idx); |
| 325 | } |
| 326 | |
| 327 | 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) { |
| 328 | memset((char *)tensor->data + offset, value, size); |
| 329 | |
| 330 | GGML_UNUSED(buffer); |
| 331 | } |
| 332 | |
| 333 | 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) { |
| 334 | memcpy((char *)tensor->data + offset, data, size); |
| 335 | |
| 336 | // views share the ztensor of their source, which is now stale, and empty tensors have none |
| 337 | if (tensor->view_src != nullptr) { |
| 338 | ggml_backend_zdnn_buffer * src_extra = (ggml_backend_zdnn_buffer *)tensor->view_src->extra; |
| 339 | if (src_extra != nullptr && src_extra->ztensor.is_transformed) { |
| 340 | zdnn_reset_ztensor(&src_extra->ztensor); |
| 341 | } |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | ggml_backend_zdnn_buffer * extra = (ggml_backend_zdnn_buffer *)tensor->extra; |
| 346 | if (extra == nullptr) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if (ggml_is_quantized(tensor->type)) { |
| 351 | GGML_ASSERT(offset == 0 && size == ggml_nbytes(tensor)); |
| 352 | ggml_zdnn_load_tensor_q(extra, tensor); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | // Fixes the LLAMA_SET_ROWS bug |
| 357 | // see: https://github.com/ggml-org/llama.cpp/issues/15414 |
| 358 | // only weights are transformed here, others on first use, as not every op reads the ztensor and some data (masks) is not finite |
| 359 | if (tensor->buffer->usage != GGML_BACKEND_BUFFER_USAGE_WEIGHTS) { |
| 360 | zdnn_reset_ztensor(&extra->ztensor); |
| 361 | return; |
| 362 | } |
| 363 | if (extra->ztensor.is_transformed == false) ggml_zdnn_load_tensor(extra->ztensor, tensor->data); |
| 364 | |
| 365 | GGML_UNUSED(buffer); |
| 366 | } |
| 367 | |
| 368 | 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) { |
| 369 | memcpy(data, (const char *)tensor->data + offset, size); |
| 370 | |
| 371 | GGML_UNUSED(buffer); |
| 372 | } |
| 373 | |
| 374 | static void ggml_backend_zdnn_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) { |
| 375 | ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context; |
| 376 | |
| 377 | memset(ctx->all_data, value, ctx->all_size); |
| 378 | } |
| 379 | |
| 380 | static void ggml_backend_zdnn_buffer_reset(ggml_backend_buffer_t buffer) { |
| 381 | ggml_backend_zdnn_buffer_context * ctx = (ggml_backend_zdnn_buffer_context *)buffer->context; |
| 382 | |
| 383 | for (const auto & buf_ptr : ctx->buffers) { |
| 384 | ggml_backend_zdnn_buffer * buf = buf_ptr.get(); |
| 385 | |
| 386 | if (buf->extra != nullptr) { |
| 387 | free(buf->extra->data); |
| 388 | } |
| 389 | |
| 390 | if (buf->ztensor.buffer_size > 0) { |
| 391 | ZDNN_CHECK(zdnn_free_ztensor_buffer(&buf->ztensor)); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | if (ctx->buffers.size() > 1) { |
| 396 | ctx->buffers.resize(1); |
| 397 | } |
| 398 | |
| 399 | ctx->n_buffers = 1; |
| 400 | } |
| 401 | |
| 402 | static ggml_backend_buffer_i ggml_backend_zdnn_buffer_i = { |
| 403 | /* .free_buffer = */ ggml_backend_zdnn_buffer_free_buffer, |
| 404 | /* .get_base = */ ggml_backend_zdnn_buffer_get_base, |
| 405 | /* .init_tensor = */ ggml_backend_zdnn_buffer_init_tensor, |
| 406 | /* .memset_tensor = */ ggml_backend_zdnn_buffer_memset_tensor, |
| 407 | /* .set_tensor = */ ggml_backend_zdnn_buffer_set_tensor, |
| 408 | /* .get_tensor = */ ggml_backend_zdnn_buffer_get_tensor, |
| 409 | /* .set_tensor_2d = */ NULL, |
| 410 | /* .get_tensor_2d = */ NULL, |
| 411 | /* .cpy_tensor = */ NULL, |
| 412 | /* .clear = */ ggml_backend_zdnn_buffer_clear, |
| 413 | /* .reset = */ ggml_backend_zdnn_buffer_reset, |
| 414 | /* .set_tensor_n = */ NULL, |
| 415 | /* .get_tensor_n = */ NULL, |
| 416 | /* .cpy_tensor_n = */ NULL, |
| 417 | }; |
| 418 | |
| 419 | // |
| 420 | // default buffer type |
| 421 | // |
| 422 | |
| 423 | static const char * ggml_backend_zdnn_buffer_type_get_name(ggml_backend_buffer_type_t buft) { |
| 424 | return GGML_ZDNN_NAME; |
| 425 | |
| 426 | GGML_UNUSED(buft); |
| 427 | } |
| 428 | |
| 429 | static ggml_backend_buffer_t ggml_backend_zdnn_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) { |
| 430 | ggml_backend_zdnn_buffer_context * ctx = new ggml_backend_zdnn_buffer_context(); |
| 431 | |
| 432 | const size_t size_page = sysconf(_SC_PAGESIZE); |
| 433 | |
| 434 | size_t size_aligned = size; |
| 435 | if ((size_aligned % size_page) != 0) { |
| 436 | size_aligned += size_page - (size_aligned % size_page); |
| 437 | } |
| 438 | |
| 439 | ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *)buft->device->context; |
| 440 | |
| 441 | GGML_ASSERT(ctx_dev->zdnn_device >= 0); |
| 442 | int device = ctx_dev->zdnn_device; GGML_UNUSED(device); |
| 443 | |
| 444 | ctx->all_data = ggml_aligned_malloc(size_aligned); |
| 445 | ctx->all_size = size_aligned; |
| 446 | ctx->owned = true; |
| 447 | ctx->n_buffers = 1; |
| 448 | |
| 449 | if (ctx->all_data != NULL) { |
| 450 | std::unique_ptr<ggml_backend_zdnn_buffer> zdnn_buffer = std::make_unique<ggml_backend_zdnn_buffer>(); |
| 451 | zdnn_buffer->data = ctx->all_data; |
| 452 | zdnn_buffer->size = size_aligned; |
| 453 | ctx->buffers.push_back(std::move(zdnn_buffer)); |
| 454 | } |
| 455 | |
| 456 | if (size_aligned > 0 && (ctx->all_data == NULL)) { |
| 457 | GGML_LOG_ERROR("%s: error: failed to allocate buffer, size = %8.2f\n", |
| 458 | __func__, size_aligned / 1024.0 / 1024.0); |
| 459 | delete ctx; |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | return ggml_backend_buffer_init(buft, ggml_backend_zdnn_buffer_i, ctx, size); |
| 464 | } |
| 465 | |
| 466 | static size_t ggml_backend_zdnn_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) { |
| 467 | return 256; |
| 468 | |
| 469 | GGML_UNUSED(buft); |
| 470 | } |
| 471 | |
| 472 | static bool ggml_backend_zdnn_buffer_type_is_host(ggml_backend_buffer_type_t buft) { |
| 473 | return true; |
| 474 | |
| 475 | GGML_UNUSED(buft); |
| 476 | } |
| 477 | |
| 478 | ggml_backend_buffer_type_t ggml_backend_zdnn_buffer_type(void) { |
| 479 | static ggml_backend_buffer_type ggml_backend_buffer_type_zdnn = { |
| 480 | /* .iface = */ { |
| 481 | /* .get_name = */ ggml_backend_zdnn_buffer_type_get_name, |
| 482 | /* .alloc_buffer = */ ggml_backend_zdnn_buffer_type_alloc_buffer, |
| 483 | /* .alloc_buffer_n = */ NULL, |
| 484 | /* .get_alignment = */ ggml_backend_zdnn_buffer_type_get_alignment, |
| 485 | /* .get_max_size = */ NULL, |
| 486 | /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes |
| 487 | /* .get_alloc_size_n = */ NULL, |
| 488 | /* .is_host = */ ggml_backend_zdnn_buffer_type_is_host, |
| 489 | }, |
| 490 | /* .device = */ &g_ggml_backend_zdnn_device, |
| 491 | /* .context = */ NULL, |
| 492 | }; |
| 493 | |
| 494 | return &ggml_backend_buffer_type_zdnn; |
| 495 | } |
| 496 | |
| 497 | // |
| 498 | // backend |
| 499 | // |
| 500 | |
| 501 | static const char * ggml_backend_zdnn_name(ggml_backend_t backend) { |
| 502 | return GGML_ZDNN_NAME; |
| 503 | |
| 504 | GGML_UNUSED(backend); |
| 505 | } |
| 506 | |
| 507 | static void ggml_backend_zdnn_free(ggml_backend_t backend) { |
| 508 | ggml_backend_zdnn_context * ctx = (ggml_backend_zdnn_context *)backend->context; |
| 509 | |
| 510 | ggml_zdnn_free(ctx); |
| 511 | free(backend); |
| 512 | } |
| 513 | |
| 514 | static enum ggml_status ggml_backend_zdnn_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) { |
| 515 | return ggml_zdnn_graph_compute(backend, cgraph); |
| 516 | } |
| 517 | |
| 518 | static ggml_backend_i ggml_backend_zdnn_i = { |
| 519 | /* .get_name = */ ggml_backend_zdnn_name, |
| 520 | /* .free = */ ggml_backend_zdnn_free, |
| 521 | /* .set_tensor_async = */ NULL, |
| 522 | /* .get_tensor_async = */ NULL, |
| 523 | /* .set_tensor_2d_async = */ NULL, |
| 524 | /* .get_tensor_2d_async = */ NULL, |
| 525 | /* .cpy_tensor_async = */ NULL, |
| 526 | /* .synchronize = */ NULL, |
| 527 | /* .graph_plan_create = */ NULL, |
| 528 | /* .graph_plan_free = */ NULL, |
| 529 | /* .graph_plan_update = */ NULL, |
| 530 | /* .graph_plan_compute = */ NULL, |
| 531 | /* .graph_compute = */ ggml_backend_zdnn_graph_compute, |
| 532 | /* .event_record = */ NULL, |
| 533 | /* .event_wait = */ NULL, |
| 534 | /* .graph_optimize = */ NULL, |
| 535 | }; |
| 536 | |
| 537 | static ggml_guid_t ggml_backend_zdnn_guid(void) { |
| 538 | static char guid_str[] = "IBM-ZDNN-ACCELER"; |
| 539 | return reinterpret_cast<ggml_guid_t>(guid_str); |
| 540 | } |
| 541 | |
| 542 | bool ggml_backend_is_zdnn(ggml_backend_t backend) { |
| 543 | return backend != NULL && |
| 544 | ggml_guid_matches(backend->guid, ggml_backend_zdnn_guid()); |
| 545 | } |
| 546 | |
| 547 | // |
| 548 | // backend device |
| 549 | // |
| 550 | |
| 551 | static const char * ggml_backend_zdnn_device_get_name(ggml_backend_dev_t dev) { |
| 552 | return GGML_ZDNN_NAME; |
| 553 | |
| 554 | GGML_UNUSED(dev); |
| 555 | } |
| 556 | |
| 557 | static const char * ggml_backend_zdnn_device_get_description(ggml_backend_dev_t dev) { |
| 558 | return "IBM Z Neural Network Processing Assist (NNPA)"; |
| 559 | |
| 560 | GGML_UNUSED(dev); |
| 561 | } |
| 562 | |
| 563 | static void ggml_backend_zdnn_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) { |
| 564 | *free = 0; |
| 565 | *total = 0; |
| 566 | |
| 567 | GGML_UNUSED(dev); |
| 568 | } |
| 569 | |
| 570 | static enum ggml_backend_dev_type ggml_backend_zdnn_device_get_type(ggml_backend_dev_t dev) { |
| 571 | return GGML_BACKEND_DEVICE_TYPE_ACCEL; |
| 572 | |
| 573 | GGML_UNUSED(dev); |
| 574 | } |
| 575 | |
| 576 | static void ggml_backend_zdnn_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) { |
| 577 | props->name = ggml_backend_zdnn_device_get_name(dev); |
| 578 | props->description = ggml_backend_zdnn_device_get_description(dev); |
| 579 | props->type = ggml_backend_zdnn_device_get_type(dev); |
| 580 | ggml_backend_zdnn_device_get_memory(dev, &props->memory_free, &props->memory_total); |
| 581 | props->caps = { |
| 582 | /* .async = */ false, |
| 583 | /* .host_buffer = */ false, |
| 584 | /* .buffer_from_host_ptr = */ false, |
| 585 | /* .events = */ false, |
| 586 | /* .mmap_support = */ true, |
| 587 | }; |
| 588 | } |
| 589 | |
| 590 | static ggml_backend_t ggml_backend_zdnn_device_init(ggml_backend_dev_t dev, const char * params) { |
| 591 | ggml_backend_zdnn_context * ctx = ggml_zdnn_init(dev); |
| 592 | if (ctx == NULL) { |
| 593 | GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__); |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | ggml_backend_t backend = (ggml_backend *)malloc(sizeof(ggml_backend)); |
| 598 | *backend = { |
| 599 | /* .guid = */ ggml_backend_zdnn_guid(), |
| 600 | /* .iface = */ ggml_backend_zdnn_i, |
| 601 | /* .device = */ dev, |
| 602 | /* .context = */ ctx |
| 603 | }; |
| 604 | |
| 605 | return backend; |
| 606 | |
| 607 | GGML_UNUSED(params); |
| 608 | } |
| 609 | |
| 610 | static ggml_backend_buffer_type_t ggml_backend_zdnn_device_get_buffer_type(ggml_backend_dev_t dev) { |
| 611 | return ggml_backend_zdnn_buffer_type(); |
| 612 | |
| 613 | GGML_UNUSED(dev); |
| 614 | } |
| 615 | |
| 616 | static bool ggml_backend_zdnn_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) { |
| 617 | ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *) dev->context; |
| 618 | |
| 619 | return ggml_zdnn_supports_op(ctx_dev, op); |
| 620 | } |
| 621 | |
| 622 | static bool ggml_backend_zdnn_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) { |
| 623 | return |
| 624 | buft->iface.get_name == ggml_backend_zdnn_buffer_type_get_name; |
| 625 | |
| 626 | GGML_UNUSED(dev); |
| 627 | } |
| 628 | |
| 629 | static ggml_backend_device_i ggml_backend_zdnn_device_i = { |
| 630 | /* .get_name = */ ggml_backend_zdnn_device_get_name, |
| 631 | /* .get_description = */ ggml_backend_zdnn_device_get_description, |
| 632 | /* .get_memory = */ ggml_backend_zdnn_device_get_memory, |
| 633 | /* .get_type = */ ggml_backend_zdnn_device_get_type, |
| 634 | /* .get_props = */ ggml_backend_zdnn_device_get_props, |
| 635 | /* .init_backend = */ ggml_backend_zdnn_device_init, |
| 636 | /* .get_buffer_type = */ ggml_backend_zdnn_device_get_buffer_type, |
| 637 | /* .get_host_buffer_type = */ NULL, |
| 638 | /* .buffer_from_host_ptr = */ NULL, |
| 639 | /* .supports_op = */ ggml_backend_zdnn_device_supports_op, |
| 640 | /* .supports_buft = */ ggml_backend_zdnn_device_supports_buft, |
| 641 | /* .offload_op = */ NULL, |
| 642 | /* .event_new = */ NULL, |
| 643 | /* .event_free = */ NULL, |
| 644 | /* .event_synchronize = */ NULL, |
| 645 | }; |
| 646 | |
| 647 | // |
| 648 | // backend registry |
| 649 | // |
| 650 | |
| 651 | static const char * ggml_backend_zdnn_reg_get_name(ggml_backend_reg_t reg) { |
| 652 | return GGML_ZDNN_NAME; |
| 653 | |
| 654 | GGML_UNUSED(reg); |
| 655 | } |
| 656 | |
| 657 | static size_t ggml_backend_zdnn_reg_device_count(ggml_backend_reg_t reg) { |
| 658 | if (!zdnn_is_nnpa_installed()) { |
| 659 | return 0; |
| 660 | } |
| 661 | return 1; |
| 662 | |
| 663 | GGML_UNUSED(reg); |
| 664 | } |
| 665 | |
| 666 | static ggml_backend_dev_t ggml_backend_zdnn_reg_device_get(ggml_backend_reg_t reg, size_t index) { |
| 667 | GGML_ASSERT(index == 0); |
| 668 | |
| 669 | return &g_ggml_backend_zdnn_device; |
| 670 | |
| 671 | GGML_UNUSED(reg); |
| 672 | GGML_UNUSED(index); |
| 673 | } |
| 674 | |
| 675 | static ggml_backend_feature g_ggml_backend_zdnn_features[] = { |
| 676 | { "NNPA", zdnn_is_nnpa_installed() ? "1" : "0" }, |
| 677 | { "NNPA_PARMBLKFORMAT_0", zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_0) ? "1" : "0" }, |
| 678 | { "NNPA_PARMBLKFORMAT_1", zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_1) ? "1" : "0" }, |
| 679 | { NULL, NULL }, |
| 680 | }; |
| 681 | |
| 682 | static ggml_backend_feature * ggml_backend_zdnn_get_features(ggml_backend_reg_t reg) { |
| 683 | return g_ggml_backend_zdnn_features; |
| 684 | |
| 685 | GGML_UNUSED(reg); |
| 686 | } |
| 687 | |
| 688 | static void ggml_backend_zdnn_set_n_threads(ggml_backend_t backend, int n_threads) { |
| 689 | GGML_ASSERT(ggml_backend_is_zdnn(backend)); |
| 690 | |
| 691 | ggml_backend_zdnn_context * ctx = (ggml_backend_zdnn_context *)backend->context; |
| 692 | ctx->n_threads = n_threads; |
| 693 | |
| 694 | ggml_backend_zdnn_device_context * ctx_dev = (ggml_backend_zdnn_device_context *)backend->device->context; |
| 695 | ctx_dev->n_threads = n_threads; |
| 696 | } |
| 697 | |
| 698 | static void * ggml_backend_zdnn_get_proc_address(ggml_backend_reg_t reg, const char * name) { |
| 699 | if (strcmp(name, "ggml_backend_get_features") == 0) { |
| 700 | return (void *) ggml_backend_zdnn_get_features; |
| 701 | } |
| 702 | if (strcmp(name, "ggml_backend_set_n_threads") == 0) { |
| 703 | return (void *) ggml_backend_zdnn_set_n_threads; |
| 704 | } |
| 705 | |
| 706 | return NULL; |
| 707 | |
| 708 | GGML_UNUSED(reg); |
| 709 | } |
| 710 | |
| 711 | static ggml_backend_reg_i ggml_backend_zdnn_reg_i = { |
| 712 | /* .get_name = */ ggml_backend_zdnn_reg_get_name, |
| 713 | /* .get_device_count = */ ggml_backend_zdnn_reg_device_count, |
| 714 | /* .get_device = */ ggml_backend_zdnn_reg_device_get, |
| 715 | /* .get_proc_address = */ ggml_backend_zdnn_get_proc_address |
| 716 | }; |
| 717 | |
| 718 | static void ggml_zdnn_cleanup(void) { |
| 719 | ggml_backend_zdnn_device_rel(&g_ggml_ctx_dev_main); |
| 720 | } |
| 721 | |
| 722 | // TODO: make thread-safe |
| 723 | ggml_backend_reg_t ggml_backend_zdnn_reg(void) { |
| 724 | ggml_backend_zdnn_device_acq(&g_ggml_ctx_dev_main); |
| 725 | |
| 726 | // register cleanup callback |
| 727 | atexit(ggml_zdnn_cleanup); |
| 728 | |
| 729 | { |
| 730 | g_ggml_backend_zdnn_reg = { |
| 731 | /* .api_version = */ GGML_ZDNN_VERSION, |
| 732 | /* .iface = */ ggml_backend_zdnn_reg_i, |
| 733 | /* .context = */ NULL |
| 734 | }; |
| 735 | |
| 736 | g_ggml_backend_zdnn_device = { |
| 737 | /* .iface = */ ggml_backend_zdnn_device_i, |
| 738 | /* .reg = */ &g_ggml_backend_zdnn_reg, |
| 739 | /* .context = */ &g_ggml_ctx_dev_main |
| 740 | }; |
| 741 | |
| 742 | return &g_ggml_backend_zdnn_reg; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | GGML_BACKEND_DL_IMPL(ggml_backend_zdnn_reg) |