ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-blas / ggml-blas.cpp
| 1 | #include "ggml.h" |
| 2 | #include "ggml-impl.h" |
| 3 | #include "ggml-blas.h" |
| 4 | #include "ggml-backend-impl.h" |
| 5 | |
| 6 | #include <future> |
| 7 | #include <vector> |
| 8 | #include <cstring> |
| 9 | |
| 10 | #if defined(GGML_BLAS_USE_ACCELERATE) |
| 11 | # include <Accelerate/Accelerate.h> |
| 12 | #elif defined(GGML_BLAS_USE_MKL) |
| 13 | # include <mkl.h> |
| 14 | #elif defined(GGML_BLAS_USE_BLIS) |
| 15 | # include <blis.h> |
| 16 | #elif defined(GGML_BLAS_USE_NVPL) |
| 17 | # include <nvpl_blas.h> |
| 18 | #else |
| 19 | # include <cblas.h> |
| 20 | #endif |
| 21 | |
| 22 | struct ggml_backend_blas_context { |
| 23 | int n_threads = GGML_DEFAULT_N_THREADS; |
| 24 | std::unique_ptr<char[]> work_data; |
| 25 | size_t work_size = 0; |
| 26 | #ifndef GGML_USE_OPENMP |
| 27 | std::vector<std::future<void>> tasks; |
| 28 | #endif |
| 29 | }; |
| 30 | |
| 31 | static void ggml_backend_blas_mul_mat(ggml_backend_blas_context * ctx, struct ggml_tensor * dst) { |
| 32 | const struct ggml_tensor * src0 = dst->src[0]; |
| 33 | const struct ggml_tensor * src1 = dst->src[1]; |
| 34 | |
| 35 | GGML_TENSOR_BINARY_OP_LOCALS |
| 36 | |
| 37 | const enum ggml_type type = src0->type; |
| 38 | |
| 39 | GGML_ASSERT(ne0 == ne01); |
| 40 | GGML_ASSERT(ne1 == ne11); |
| 41 | GGML_ASSERT(ne2 == ne12); |
| 42 | GGML_ASSERT(ne3 == ne13); |
| 43 | |
| 44 | // we don't support permuted src0 or src1 |
| 45 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
| 46 | GGML_ASSERT(nb10 == ggml_type_size(src1->type)); |
| 47 | |
| 48 | // dst cannot be transposed or permuted |
| 49 | GGML_ASSERT(nb0 == sizeof(float)); |
| 50 | GGML_ASSERT(nb0 <= nb1); |
| 51 | GGML_ASSERT(nb1 <= nb2); |
| 52 | GGML_ASSERT(nb2 <= nb3); |
| 53 | |
| 54 | // broadcast factors |
| 55 | const int64_t r2 = ne12/ne02; |
| 56 | const int64_t r3 = ne13/ne03; |
| 57 | |
| 58 | const int64_t ne_plane = ne01*ne00; |
| 59 | const size_t desired_wsize = type == GGML_TYPE_F32 ? 0 : ne03*ne02*ne_plane*sizeof(float); |
| 60 | |
| 61 | if (ctx->work_size < desired_wsize) { |
| 62 | ctx->work_data.reset(new char[desired_wsize]); |
| 63 | ctx->work_size = desired_wsize; |
| 64 | } |
| 65 | void * wdata = ctx->work_data.get(); |
| 66 | |
| 67 | // convert src0 to float |
| 68 | if (type != GGML_TYPE_F32) { |
| 69 | const auto * type_traits = ggml_get_type_traits(type); |
| 70 | ggml_to_float_t const to_float = type_traits->to_float; |
| 71 | |
| 72 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 73 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 74 | const void * x = (char *) src0->data + i02*nb02 + i03*nb03; |
| 75 | float * const wplane = (float *) wdata + i02*ne_plane + i03*ne02*ne_plane; |
| 76 | |
| 77 | const int min_cols_per_thread = 4096; |
| 78 | const int min_rows_per_thread = std::max((int)(min_cols_per_thread/ne00), 1); |
| 79 | const int n_threads = std::max(std::min(ctx->n_threads, (int)(ne01/min_rows_per_thread)), 1); |
| 80 | |
| 81 | #ifdef GGML_USE_OPENMP |
| 82 | #pragma omp parallel for num_threads(n_threads) |
| 83 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 84 | to_float((const char *) x + i01*nb01, wplane + i01*ne00, ne00); |
| 85 | } |
| 86 | #else |
| 87 | for (int i = 1; i < n_threads; i++) { |
| 88 | const int64_t start = i*ne01/n_threads; |
| 89 | const int64_t end = (i + 1)*ne01/n_threads; |
| 90 | if (start < end) { |
| 91 | ctx->tasks.push_back(std::async(std::launch::async, [=]() { |
| 92 | for (int64_t i01 = start; i01 < end; i01++) { |
| 93 | to_float((const char *) x + i01*nb01, wplane + i01*ne00, ne00); |
| 94 | } |
| 95 | })); |
| 96 | } |
| 97 | } |
| 98 | { |
| 99 | // reuse the current thread for the first task |
| 100 | const int64_t start = 0; |
| 101 | const int64_t end = ne01/n_threads; |
| 102 | for (int64_t i01 = start; i01 < end; i01++) { |
| 103 | to_float((const char *) x + i01*nb01, wplane + i01*ne00, ne00); |
| 104 | } |
| 105 | } |
| 106 | #endif |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #ifndef GGML_USE_OPENMP |
| 111 | // wait for all tasks to finish |
| 112 | for (auto & task : ctx->tasks) { |
| 113 | task.get(); |
| 114 | } |
| 115 | ctx->tasks.clear(); |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | #if defined(GGML_BLAS_USE_OPENBLAS) |
| 120 | openblas_set_num_threads(ctx->n_threads); |
| 121 | #elif defined(GGML_BLAS_USE_BLIS) |
| 122 | bli_thread_set_num_threads(ctx->n_threads); |
| 123 | #elif defined(GGML_BLAS_USE_NVPL) |
| 124 | nvpl_blas_set_num_threads(ctx->n_threads); |
| 125 | #elif defined(GGML_BLAS_USE_MKL) |
| 126 | mkl_set_num_threads(ctx->n_threads); |
| 127 | #endif |
| 128 | |
| 129 | for (int64_t i13 = 0; i13 < ne13; i13++) { |
| 130 | for (int64_t i12 = 0; i12 < ne12; i12++) { |
| 131 | const int64_t i03 = i13/r3; |
| 132 | const int64_t i02 = i12/r2; |
| 133 | |
| 134 | const float * x = (float *) ((char *) src0->data + i02*nb02 + i03*nb03); |
| 135 | const float * y = (float *) ((char *) src1->data + i12*nb12 + i13*nb13); |
| 136 | float * d = (float *) ((char *) dst->data + i12*nb2 + i13*nb3); |
| 137 | |
| 138 | if (type != GGML_TYPE_F32) { |
| 139 | x = (float *) wdata + i02*ne_plane + i03*ne02*ne_plane; |
| 140 | } |
| 141 | |
| 142 | cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, |
| 143 | ne1, ne01, ne10, |
| 144 | 1.0f, y, ne10, |
| 145 | x, ne00, |
| 146 | 0.0f, d, ne01); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static void ggml_backend_blas_out_prod(ggml_backend_blas_context * ctx, struct ggml_tensor * dst) { |
| 152 | const struct ggml_tensor * src0 = dst->src[0]; |
| 153 | const struct ggml_tensor * src1 = dst->src[1]; |
| 154 | |
| 155 | GGML_TENSOR_BINARY_OP_LOCALS |
| 156 | |
| 157 | GGML_ASSERT(ne0 == ne00); |
| 158 | GGML_ASSERT(ne1 == ne10); |
| 159 | GGML_ASSERT(ne2 == ne02); |
| 160 | GGML_ASSERT(ne02 == ne12); |
| 161 | GGML_ASSERT(ne3 == ne13); |
| 162 | GGML_ASSERT(ne03 == ne13); |
| 163 | |
| 164 | // we don't support permuted src0 or src1 |
| 165 | GGML_ASSERT(nb00 == sizeof(float)); |
| 166 | |
| 167 | // dst cannot be transposed or permuted |
| 168 | GGML_ASSERT(nb0 == sizeof(float)); |
| 169 | // GGML_ASSERT(nb0 <= nb1); |
| 170 | // GGML_ASSERT(nb1 <= nb2); |
| 171 | // GGML_ASSERT(nb2 <= nb3); |
| 172 | |
| 173 | // Arguments to ggml_compute_forward_out_prod (expressed as major,minor) |
| 174 | // src0: (k,n) |
| 175 | // src1: (k,m) |
| 176 | // dst: (m,n) |
| 177 | // |
| 178 | // Arguments to sgemm (see https://github.com/Reference-LAPACK/lapack/blob/master/BLAS/SRC/sgemm.f) |
| 179 | // Also expressed as (major,minor) |
| 180 | // a: (m,k): so src1 transposed |
| 181 | // b: (k,n): so src0 |
| 182 | // c: (m,n) |
| 183 | // |
| 184 | // However, if ggml_is_transposed(src1) is true, then |
| 185 | // src1->data already contains a transposed version, so sgemm mustn't |
| 186 | // transpose it further. |
| 187 | |
| 188 | int n = src0->ne[0]; |
| 189 | int k = src0->ne[1]; |
| 190 | int m = src1->ne[0]; |
| 191 | |
| 192 | CBLAS_TRANSPOSE transposeA; |
| 193 | int lda; |
| 194 | |
| 195 | if (!ggml_is_transposed(src1)) { |
| 196 | transposeA = CblasTrans; |
| 197 | lda = m; |
| 198 | } else { |
| 199 | transposeA = CblasNoTrans; |
| 200 | lda = k; |
| 201 | } |
| 202 | |
| 203 | float * a = (float *) ((char *) src1->data); |
| 204 | float * b = (float *) ((char *) src0->data); |
| 205 | float * c = (float *) ((char *) dst->data); |
| 206 | |
| 207 | cblas_sgemm(CblasRowMajor, transposeA, CblasNoTrans, m, n, k, 1.0, a, lda, b, n, 0.0, c, n); |
| 208 | |
| 209 | GGML_UNUSED(ctx); |
| 210 | } |
| 211 | |
| 212 | // backend interface |
| 213 | |
| 214 | static const char * ggml_backend_blas_get_name(ggml_backend_t backend) { |
| 215 | return "BLAS"; |
| 216 | |
| 217 | GGML_UNUSED(backend); |
| 218 | } |
| 219 | |
| 220 | static void ggml_backend_blas_free(ggml_backend_t backend) { |
| 221 | ggml_backend_blas_context * ctx = (ggml_backend_blas_context *)backend->context; |
| 222 | delete ctx; |
| 223 | delete backend; |
| 224 | } |
| 225 | |
| 226 | static enum ggml_status ggml_backend_blas_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) { |
| 227 | ggml_backend_blas_context * ctx = (ggml_backend_blas_context *)backend->context; |
| 228 | |
| 229 | for (int i = 0; i < cgraph->n_nodes; i++) { |
| 230 | struct ggml_tensor * node = cgraph->nodes[i]; |
| 231 | |
| 232 | if ((node->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) { |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | switch (node->op) { |
| 237 | case GGML_OP_MUL_MAT: |
| 238 | ggml_backend_blas_mul_mat(ctx, node); |
| 239 | break; |
| 240 | |
| 241 | case GGML_OP_OUT_PROD: |
| 242 | ggml_backend_blas_out_prod(ctx, node); |
| 243 | break; |
| 244 | |
| 245 | case GGML_OP_NONE: |
| 246 | case GGML_OP_RESHAPE: |
| 247 | case GGML_OP_VIEW: |
| 248 | case GGML_OP_PERMUTE: |
| 249 | case GGML_OP_TRANSPOSE: |
| 250 | break; |
| 251 | |
| 252 | default: |
| 253 | GGML_ABORT("%s: unsupported op %s\n", __func__, ggml_op_desc(node)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return GGML_STATUS_SUCCESS; |
| 258 | |
| 259 | GGML_UNUSED(backend); |
| 260 | } |
| 261 | |
| 262 | static struct ggml_backend_i blas_backend_i = { |
| 263 | /* .get_name = */ ggml_backend_blas_get_name, |
| 264 | /* .free = */ ggml_backend_blas_free, |
| 265 | /* .set_tensor_async = */ NULL, |
| 266 | /* .get_tensor_async = */ NULL, |
| 267 | /* .set_tensor_2d_async = */ NULL, |
| 268 | /* .get_tensor_2d_async = */ NULL, |
| 269 | /* .cpy_tensor_async = */ NULL, |
| 270 | /* .synchronize = */ NULL, |
| 271 | /* .graph_plan_create = */ NULL, |
| 272 | /* .graph_plan_free = */ NULL, |
| 273 | /* .graph_plan_update = */ NULL, |
| 274 | /* .graph_plan_compute = */ NULL, |
| 275 | /* .graph_compute = */ ggml_backend_blas_graph_compute, |
| 276 | /* .event_record = */ NULL, |
| 277 | /* .event_wait = */ NULL, |
| 278 | /* .graph_optimize = */ NULL, |
| 279 | }; |
| 280 | |
| 281 | static ggml_guid_t ggml_backend_blas_guid(void) { |
| 282 | static ggml_guid guid = { 0x12, 0xa8, 0xae, 0xf4, 0xc0, 0x1e, 0x61, 0x97, 0x8f, 0xeb, 0x33, 0x04, 0xa1, 0x33, 0x51, 0x2d }; |
| 283 | return &guid; |
| 284 | } |
| 285 | |
| 286 | ggml_backend_t ggml_backend_blas_init(void) { |
| 287 | ggml_backend_blas_context * ctx = new ggml_backend_blas_context; |
| 288 | |
| 289 | ggml_backend_t backend = new ggml_backend { |
| 290 | /* .guid = */ ggml_backend_blas_guid(), |
| 291 | /* .iface = */ blas_backend_i, |
| 292 | /* .device = */ ggml_backend_reg_dev_get(ggml_backend_blas_reg(), 0), |
| 293 | /* .context = */ ctx, |
| 294 | }; |
| 295 | |
| 296 | #if defined(GGML_BLAS_USE_OPENBLAS) && defined(GGML_USE_OPENMP) |
| 297 | if (openblas_get_parallel() != OPENBLAS_OPENMP) { |
| 298 | GGML_LOG_DEBUG("%s: warning: ggml is using OpenMP, but OpenBLAS was compiled without OpenMP support\n", __func__); |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | #if defined(BLIS_ENABLE_CBLAS) && defined(GGML_USE_OPENMP) && !defined(BLIS_ENABLE_OPENMP) |
| 303 | GGML_LOG_DEBUG("%s: warning: ggml is using OpenMP, but BLIS was compiled without OpenMP support\n", __func__); |
| 304 | #endif |
| 305 | |
| 306 | return backend; |
| 307 | } |
| 308 | |
| 309 | bool ggml_backend_is_blas(ggml_backend_t backend) { |
| 310 | return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_blas_guid()); |
| 311 | } |
| 312 | |
| 313 | void ggml_backend_blas_set_n_threads(ggml_backend_t backend_blas, int n_threads) { |
| 314 | GGML_ASSERT(ggml_backend_is_blas(backend_blas)); |
| 315 | |
| 316 | ggml_backend_blas_context * ctx = (ggml_backend_blas_context *)backend_blas->context; |
| 317 | ctx->n_threads = n_threads; |
| 318 | } |
| 319 | |
| 320 | // device interface |
| 321 | |
| 322 | static const char * ggml_backend_blas_device_get_name(ggml_backend_dev_t dev) { |
| 323 | return "BLAS"; |
| 324 | |
| 325 | GGML_UNUSED(dev); |
| 326 | } |
| 327 | |
| 328 | static const char * ggml_backend_blas_device_get_description(ggml_backend_dev_t dev) { |
| 329 | #if defined(GGML_BLAS_USE_ACCELERATE) |
| 330 | return "Accelerate"; |
| 331 | #elif defined(GGML_BLAS_USE_MKL) |
| 332 | return "MKL"; |
| 333 | #elif defined(GGML_BLAS_USE_AOCL) |
| 334 | return "AOCL-BLAS"; |
| 335 | #elif defined(GGML_BLAS_USE_BLIS) |
| 336 | return "BLIS"; |
| 337 | #elif defined(GGML_BLAS_USE_NVPL) |
| 338 | return "NVPL"; |
| 339 | #elif defined(GGML_BLAS_USE_OPENBLAS) |
| 340 | return "OpenBLAS"; |
| 341 | #else |
| 342 | return "BLAS"; |
| 343 | #endif |
| 344 | |
| 345 | GGML_UNUSED(dev); |
| 346 | } |
| 347 | |
| 348 | static void ggml_backend_blas_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) { |
| 349 | // no memory to report |
| 350 | *free = 0; |
| 351 | *total = 0; |
| 352 | |
| 353 | GGML_UNUSED(dev); |
| 354 | } |
| 355 | |
| 356 | static enum ggml_backend_dev_type ggml_backend_blas_device_get_type(ggml_backend_dev_t dev) { |
| 357 | return GGML_BACKEND_DEVICE_TYPE_ACCEL; |
| 358 | |
| 359 | GGML_UNUSED(dev); |
| 360 | } |
| 361 | |
| 362 | static void ggml_backend_blas_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) { |
| 363 | props->name = ggml_backend_blas_device_get_name(dev); |
| 364 | props->description = ggml_backend_blas_device_get_description(dev); |
| 365 | props->type = ggml_backend_blas_device_get_type(dev); |
| 366 | ggml_backend_blas_device_get_memory(dev, &props->memory_free, &props->memory_total); |
| 367 | props->caps = { |
| 368 | /* .async = */ false, |
| 369 | /* .host_buffer = */ false, |
| 370 | /* .buffer_from_host_ptr = */ true, |
| 371 | /* .events = */ false, |
| 372 | /* .mmap_support = */ true, |
| 373 | }; |
| 374 | } |
| 375 | |
| 376 | static ggml_backend_t ggml_backend_blas_device_init_backend(ggml_backend_dev_t dev, const char * params) { |
| 377 | return ggml_backend_blas_init(); |
| 378 | |
| 379 | GGML_UNUSED(dev); |
| 380 | GGML_UNUSED(params); |
| 381 | } |
| 382 | |
| 383 | static ggml_backend_buffer_type_t ggml_backend_blas_device_get_buffer_type(ggml_backend_dev_t dev) { |
| 384 | return ggml_backend_cpu_buffer_type(); |
| 385 | |
| 386 | GGML_UNUSED(dev); |
| 387 | } |
| 388 | |
| 389 | static ggml_backend_buffer_t ggml_backend_blas_device_buffer_from_host_ptr(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) { |
| 390 | return ggml_backend_cpu_buffer_from_ptr(ptr, size); |
| 391 | |
| 392 | GGML_UNUSED(dev); |
| 393 | GGML_UNUSED(max_tensor_size); |
| 394 | } |
| 395 | |
| 396 | static bool ggml_backend_blas_device_supports_op(ggml_backend_dev_t dev, const struct ggml_tensor * op) { |
| 397 | const struct ggml_tensor * src0 = op->src[0]; |
| 398 | const struct ggml_tensor * src1 = op->src[1]; |
| 399 | |
| 400 | switch (op->op) { |
| 401 | case GGML_OP_NONE: |
| 402 | case GGML_OP_RESHAPE: |
| 403 | case GGML_OP_VIEW: |
| 404 | case GGML_OP_PERMUTE: |
| 405 | case GGML_OP_TRANSPOSE: |
| 406 | return true; |
| 407 | |
| 408 | case GGML_OP_MUL_MAT: |
| 409 | { |
| 410 | // BLAS usually is only faster for large matrices |
| 411 | const struct ggml_tensor * src0 = op->src[0]; |
| 412 | const struct ggml_tensor * src1 = op->src[1]; |
| 413 | |
| 414 | const int64_t ne10 = src1->ne[0]; |
| 415 | |
| 416 | const int64_t ne0 = op->ne[0]; |
| 417 | const int64_t ne1 = op->ne[1]; |
| 418 | |
| 419 | // TODO: find the optimal value |
| 420 | const int64_t min_batch = 32; |
| 421 | |
| 422 | // default back to CPU fast path |
| 423 | // see: https://github.com/ggml-org/llama.cpp/issues/25565 |
| 424 | if (ggml_get_op_params_i32(op, 1) == GGML_HINT_SRC0_IS_HADAMARD) { |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | return ggml_is_contiguous(src0) && |
| 429 | ggml_is_contiguous(src1) && |
| 430 | src1->type == GGML_TYPE_F32 && |
| 431 | (ne0 >= min_batch && ne1 >= min_batch && ne10 >= min_batch) && |
| 432 | (src0->type == GGML_TYPE_F32 || ggml_get_type_traits(src0->type)->to_float != NULL); |
| 433 | } |
| 434 | |
| 435 | case GGML_OP_OUT_PROD: |
| 436 | return op->src[0]->type == GGML_TYPE_F32 && |
| 437 | op->src[1]->type == GGML_TYPE_F32 && |
| 438 | ggml_is_matrix(src0) && |
| 439 | ggml_is_matrix(src1) && |
| 440 | ggml_is_contiguous(src0) && |
| 441 | (ggml_is_contiguous(src1) || ggml_is_transposed(src1)) && |
| 442 | (src0->type == GGML_TYPE_F32 || ggml_get_type_traits(src0->type)->to_float != NULL); |
| 443 | |
| 444 | default: |
| 445 | return false; |
| 446 | |
| 447 | } |
| 448 | |
| 449 | GGML_UNUSED(dev); |
| 450 | } |
| 451 | |
| 452 | static bool ggml_backend_blas_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) { |
| 453 | return ggml_backend_buft_is_host(buft); |
| 454 | |
| 455 | GGML_UNUSED(dev); |
| 456 | } |
| 457 | |
| 458 | static const struct ggml_backend_device_i ggml_backend_blas_device_i = { |
| 459 | /* .get_name = */ ggml_backend_blas_device_get_name, |
| 460 | /* .get_description = */ ggml_backend_blas_device_get_description, |
| 461 | /* .get_memory = */ ggml_backend_blas_device_get_memory, |
| 462 | /* .get_type = */ ggml_backend_blas_device_get_type, |
| 463 | /* .get_props = */ ggml_backend_blas_device_get_props, |
| 464 | /* .init_backend = */ ggml_backend_blas_device_init_backend, |
| 465 | /* .get_buffer_type = */ ggml_backend_blas_device_get_buffer_type, |
| 466 | /* .get_host_buffer_type = */ NULL, |
| 467 | /* .buffer_from_host_ptr = */ ggml_backend_blas_device_buffer_from_host_ptr, |
| 468 | /* .supports_op = */ ggml_backend_blas_device_supports_op, |
| 469 | /* .supports_buft = */ ggml_backend_blas_device_supports_buft, |
| 470 | /* .offload_op = */ NULL, |
| 471 | /* .event_new = */ NULL, |
| 472 | /* .event_free = */ NULL, |
| 473 | /* .event_synchronize = */ NULL, |
| 474 | }; |
| 475 | |
| 476 | // backend reg interface |
| 477 | |
| 478 | static const char * ggml_backend_blas_reg_get_name(ggml_backend_reg_t reg) { |
| 479 | return "BLAS"; |
| 480 | |
| 481 | GGML_UNUSED(reg); |
| 482 | } |
| 483 | |
| 484 | static size_t ggml_backend_blas_reg_get_device_count(ggml_backend_reg_t reg) { |
| 485 | return 1; |
| 486 | |
| 487 | GGML_UNUSED(reg); |
| 488 | } |
| 489 | |
| 490 | static ggml_backend_dev_t ggml_backend_blas_reg_get_device(ggml_backend_reg_t reg, size_t index) { |
| 491 | GGML_ASSERT(index == 0); |
| 492 | |
| 493 | static ggml_backend_device ggml_backend_blas_device = { |
| 494 | /* .iface = */ ggml_backend_blas_device_i, |
| 495 | /* .reg = */ reg, |
| 496 | /* .context = */ nullptr, |
| 497 | }; |
| 498 | |
| 499 | return &ggml_backend_blas_device; |
| 500 | |
| 501 | GGML_UNUSED(reg); |
| 502 | GGML_UNUSED(index); |
| 503 | } |
| 504 | |
| 505 | static void * ggml_backend_blas_get_proc_address(ggml_backend_reg_t reg, const char * name) { |
| 506 | if (std::strcmp(name, "ggml_backend_set_n_threads") == 0) { |
| 507 | return (void *)ggml_backend_blas_set_n_threads; |
| 508 | } |
| 509 | return NULL; |
| 510 | |
| 511 | GGML_UNUSED(reg); |
| 512 | GGML_UNUSED(name); |
| 513 | } |
| 514 | |
| 515 | static const struct ggml_backend_reg_i ggml_backend_blas_reg_i = { |
| 516 | /* .get_name = */ ggml_backend_blas_reg_get_name, |
| 517 | /* .get_device_count = */ ggml_backend_blas_reg_get_device_count, |
| 518 | /* .get_device = */ ggml_backend_blas_reg_get_device, |
| 519 | /* .get_proc_address = */ ggml_backend_blas_get_proc_address, |
| 520 | }; |
| 521 | |
| 522 | ggml_backend_reg_t ggml_backend_blas_reg(void) { |
| 523 | static struct ggml_backend_reg ggml_backend_blas_reg = { |
| 524 | /* .api_version = */ GGML_BACKEND_API_VERSION, |
| 525 | /* .iface = */ ggml_backend_blas_reg_i, |
| 526 | /* .context = */ NULL, |
| 527 | }; |
| 528 | |
| 529 | return &ggml_backend_blas_reg; |
| 530 | } |
| 531 | |
| 532 | GGML_BACKEND_DL_IMPL(ggml_backend_blas_reg) |