ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-metal / ggml-metal.cpp
| 1 | #include "ggml-metal.h" |
| 2 | |
| 3 | #include "ggml-impl.h" |
| 4 | #include "ggml-backend-impl.h" |
| 5 | |
| 6 | #include "ggml-metal-device.h" |
| 7 | #include "ggml-metal-fusion.h" |
| 8 | #include "ggml-metal-context.h" |
| 9 | #include "ggml-metal-ops.h" |
| 10 | #include "ggml-metal-tuning.h" |
| 11 | |
| 12 | #include <mutex> |
| 13 | #include <string> |
| 14 | |
| 15 | #define GGML_METAL_NAME "MTL" |
| 16 | #define GGML_METAL_MAX_DEVICES 16 |
| 17 | |
| 18 | // number of Metal devices |
| 19 | // note: can be overridden with GGML_METAL_DEVICES env to simulate virtual devices |
| 20 | static int g_devices = 1; |
| 21 | |
| 22 | // forward declaration |
| 23 | static bool ggml_backend_buffer_is_metal(ggml_backend_buffer_t buffer); |
| 24 | |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
| 26 | // backend interface |
| 27 | //////////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | // shared buffer |
| 30 | |
| 31 | static void ggml_backend_metal_buffer_shared_free_buffer(ggml_backend_buffer_t buffer) { |
| 32 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 33 | |
| 34 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 35 | |
| 36 | ggml_metal_buffer_free(ctx); |
| 37 | } |
| 38 | |
| 39 | static void * ggml_backend_metal_buffer_shared_get_base(ggml_backend_buffer_t buffer) { |
| 40 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 41 | |
| 42 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 43 | |
| 44 | return ggml_metal_buffer_get_base(ctx); |
| 45 | } |
| 46 | |
| 47 | static void ggml_backend_metal_buffer_shared_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) { |
| 48 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 49 | |
| 50 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 51 | |
| 52 | ggml_metal_buffer_memset_tensor(ctx, tensor, value, offset, size); |
| 53 | } |
| 54 | |
| 55 | static void ggml_backend_metal_buffer_shared_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) { |
| 56 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 57 | |
| 58 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 59 | |
| 60 | ggml_metal_buffer_set_tensor(ctx, tensor, data, offset, size); |
| 61 | } |
| 62 | |
| 63 | static void ggml_backend_metal_buffer_shared_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) { |
| 64 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 65 | |
| 66 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 67 | |
| 68 | ggml_metal_buffer_get_tensor(ctx, tensor, data, offset, size); |
| 69 | } |
| 70 | |
| 71 | static void ggml_backend_metal_buffer_shared_set_tensor_n(ggml_backend_buffer_t buffer, struct ggml_tensor * const * tensors, const void * const * data, const size_t * offsets, const size_t * sizes, size_t n_tensors) { |
| 72 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 73 | |
| 74 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 75 | |
| 76 | ggml_metal_buffer_set_tensor_n(ctx, tensors, data, offsets, sizes, n_tensors); |
| 77 | } |
| 78 | |
| 79 | static void ggml_backend_metal_buffer_shared_get_tensor_n(ggml_backend_buffer_t buffer, const struct ggml_tensor * const * tensors, void * const * data, const size_t * offsets, const size_t * sizes, size_t n_tensors) { |
| 80 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 81 | |
| 82 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 83 | |
| 84 | ggml_metal_buffer_get_tensor_n(ctx, tensors, data, offsets, sizes, n_tensors); |
| 85 | } |
| 86 | |
| 87 | static bool ggml_backend_metal_buffer_shared_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) { |
| 88 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 89 | |
| 90 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 91 | |
| 92 | if (!ggml_backend_buffer_is_metal(src->buffer)) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | return ggml_metal_buffer_cpy_tensor(ctx, src, dst); |
| 97 | } |
| 98 | |
| 99 | static bool ggml_backend_metal_buffer_shared_cpy_tensor_n(ggml_backend_buffer_t buffer, const struct ggml_tensor * const * srcs, struct ggml_tensor * const * dsts, size_t n_tensors) { |
| 100 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 101 | |
| 102 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 103 | |
| 104 | for (size_t i = 0; i < n_tensors; ++i) { |
| 105 | if (!ggml_backend_buffer_is_metal(srcs[i]->buffer)) { |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return ggml_metal_buffer_cpy_tensor_n(ctx, srcs, dsts, n_tensors); |
| 111 | } |
| 112 | |
| 113 | static void ggml_backend_metal_buffer_shared_clear(ggml_backend_buffer_t buffer, uint8_t value) { |
| 114 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 115 | |
| 116 | GGML_ASSERT(ggml_metal_buffer_is_shared(ctx)); |
| 117 | |
| 118 | ggml_metal_buffer_clear(ctx, value); |
| 119 | } |
| 120 | |
| 121 | static ggml_backend_buffer_i ggml_backend_metal_buffer_shared_i = { |
| 122 | /* .free_buffer = */ ggml_backend_metal_buffer_shared_free_buffer, |
| 123 | /* .get_base = */ ggml_backend_metal_buffer_shared_get_base, |
| 124 | /* .init_tensor = */ NULL, |
| 125 | /* .memset_tensor = */ ggml_backend_metal_buffer_shared_memset_tensor, |
| 126 | /* .set_tensor = */ ggml_backend_metal_buffer_shared_set_tensor, |
| 127 | /* .get_tensor = */ ggml_backend_metal_buffer_shared_get_tensor, |
| 128 | /* .set_tensor_2d = */ NULL, |
| 129 | /* .get_tensor_2d = */ NULL, |
| 130 | /* .cpy_tensor = */ ggml_backend_metal_buffer_shared_cpy_tensor, |
| 131 | /* .clear = */ ggml_backend_metal_buffer_shared_clear, |
| 132 | /* .reset = */ NULL, |
| 133 | /* .set_tensor_n = */ ggml_backend_metal_buffer_shared_set_tensor_n, |
| 134 | /* .get_tensor_n = */ ggml_backend_metal_buffer_shared_get_tensor_n, |
| 135 | /* .cpy_tensor_n = */ ggml_backend_metal_buffer_shared_cpy_tensor_n, |
| 136 | }; |
| 137 | |
| 138 | // private buffer |
| 139 | |
| 140 | static void ggml_backend_metal_buffer_private_free_buffer(ggml_backend_buffer_t buffer) { |
| 141 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 142 | |
| 143 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 144 | |
| 145 | ggml_metal_buffer_free(ctx); |
| 146 | } |
| 147 | |
| 148 | static void * ggml_backend_metal_buffer_private_get_base(ggml_backend_buffer_t buffer) { |
| 149 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 150 | |
| 151 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 152 | |
| 153 | return ggml_metal_buffer_get_base(ctx); |
| 154 | } |
| 155 | |
| 156 | static void ggml_backend_metal_buffer_private_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) { |
| 157 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 158 | |
| 159 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 160 | |
| 161 | ggml_metal_buffer_memset_tensor(ctx, tensor, value, offset, size); |
| 162 | } |
| 163 | |
| 164 | static void ggml_backend_metal_buffer_private_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data, size_t offset, size_t size) { |
| 165 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 166 | |
| 167 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 168 | |
| 169 | ggml_metal_buffer_set_tensor(ctx, tensor, data, offset, size); |
| 170 | } |
| 171 | |
| 172 | static void ggml_backend_metal_buffer_private_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data, size_t offset, size_t size) { |
| 173 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 174 | |
| 175 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 176 | |
| 177 | ggml_metal_buffer_get_tensor(ctx, tensor, data, offset, size); |
| 178 | } |
| 179 | |
| 180 | static void ggml_backend_metal_buffer_private_set_tensor_n(ggml_backend_buffer_t buffer, struct ggml_tensor * const * tensors, const void * const * data, const size_t * offsets, const size_t * sizes, size_t n_tensors) { |
| 181 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 182 | |
| 183 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 184 | |
| 185 | ggml_metal_buffer_set_tensor_n(ctx, tensors, data, offsets, sizes, n_tensors); |
| 186 | } |
| 187 | |
| 188 | static void ggml_backend_metal_buffer_private_get_tensor_n(ggml_backend_buffer_t buffer, const struct ggml_tensor * const * tensors, void * const * data, const size_t * offsets, const size_t * sizes, size_t n_tensors) { |
| 189 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 190 | |
| 191 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 192 | |
| 193 | ggml_metal_buffer_get_tensor_n(ctx, tensors, data, offsets, sizes, n_tensors); |
| 194 | } |
| 195 | |
| 196 | static bool ggml_backend_metal_buffer_private_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) { |
| 197 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 198 | |
| 199 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 200 | |
| 201 | if (!ggml_backend_buffer_is_metal(src->buffer)) { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | return ggml_metal_buffer_cpy_tensor(ctx, src, dst); |
| 206 | } |
| 207 | |
| 208 | static bool ggml_backend_metal_buffer_private_cpy_tensor_n(ggml_backend_buffer_t buffer, const struct ggml_tensor * const * srcs, struct ggml_tensor * const * dsts, size_t n_tensors) { |
| 209 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 210 | |
| 211 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 212 | |
| 213 | for (size_t i = 0; i < n_tensors; ++i) { |
| 214 | if (!ggml_backend_buffer_is_metal(srcs[i]->buffer)) { |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return ggml_metal_buffer_cpy_tensor_n(ctx, srcs, dsts, n_tensors); |
| 220 | } |
| 221 | |
| 222 | static void ggml_backend_metal_buffer_private_clear(ggml_backend_buffer_t buffer, uint8_t value) { |
| 223 | ggml_metal_buffer_t ctx = (ggml_metal_buffer_t)buffer->context; |
| 224 | |
| 225 | GGML_ASSERT(!ggml_metal_buffer_is_shared(ctx)); |
| 226 | |
| 227 | ggml_metal_buffer_clear(ctx, value); |
| 228 | } |
| 229 | |
| 230 | static ggml_backend_buffer_i ggml_backend_metal_buffer_private_i = { |
| 231 | /* .free_buffer = */ ggml_backend_metal_buffer_private_free_buffer, |
| 232 | /* .get_base = */ ggml_backend_metal_buffer_private_get_base, |
| 233 | /* .init_tensor = */ NULL, |
| 234 | /* .memset_tensor = */ ggml_backend_metal_buffer_private_memset_tensor, |
| 235 | /* .set_tensor = */ ggml_backend_metal_buffer_private_set_tensor, |
| 236 | /* .get_tensor = */ ggml_backend_metal_buffer_private_get_tensor, |
| 237 | /* .set_tensor_2d = */ NULL, |
| 238 | /* .get_tensor_2d = */ NULL, |
| 239 | /* .cpy_tensor = */ ggml_backend_metal_buffer_private_cpy_tensor, |
| 240 | /* .clear = */ ggml_backend_metal_buffer_private_clear, |
| 241 | /* .reset = */ NULL, |
| 242 | /* .set_tensor_n = */ ggml_backend_metal_buffer_private_set_tensor_n, |
| 243 | /* .get_tensor_n = */ ggml_backend_metal_buffer_private_get_tensor_n, |
| 244 | /* .cpy_tensor_n = */ ggml_backend_metal_buffer_private_cpy_tensor_n, |
| 245 | }; |
| 246 | |
| 247 | static bool ggml_backend_buffer_is_metal(ggml_backend_buffer_t buffer) { |
| 248 | return buffer->iface.free_buffer == ggml_backend_metal_buffer_shared_free_buffer || |
| 249 | buffer->iface.free_buffer == ggml_backend_metal_buffer_private_free_buffer; |
| 250 | } |
| 251 | |
| 252 | // |
| 253 | // buffer types |
| 254 | // |
| 255 | |
| 256 | struct ggml_backend_metal_buffer_type { |
| 257 | int device; |
| 258 | std::string name; |
| 259 | }; |
| 260 | |
| 261 | struct ggml_backend_metal_buffer_type_deleter { |
| 262 | void operator()(ggml_backend_metal_buffer_type * ctx) const { |
| 263 | delete ctx; |
| 264 | } |
| 265 | }; |
| 266 | |
| 267 | typedef std::unique_ptr<ggml_backend_metal_buffer_type, ggml_backend_metal_buffer_type_deleter> ggml_backend_metal_buffer_type_ptr; |
| 268 | |
| 269 | // common method for allocating shread or private Metal buffers |
| 270 | static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size, bool shared) { |
| 271 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)buft->device->context; |
| 272 | ggml_metal_buffer_t res = ggml_metal_buffer_init(ctx_dev, size, shared); |
| 273 | |
| 274 | if (res == NULL) { |
| 275 | GGML_LOG_ERROR("%s: failed to allocate Metal buffer of %zu bytes (out of memory)\n", __func__, size); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | ggml_backend_buffer_i buf_i = ggml_metal_buffer_is_shared(res) |
| 280 | ? ggml_backend_metal_buffer_shared_i |
| 281 | : ggml_backend_metal_buffer_private_i; |
| 282 | |
| 283 | return ggml_backend_buffer_init(buft, buf_i, res, size); |
| 284 | } |
| 285 | |
| 286 | static size_t ggml_backend_metal_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) { |
| 287 | size_t res = ggml_nbytes(tensor); |
| 288 | |
| 289 | // some operations require additional memory for fleeting data: |
| 290 | switch (tensor->op) { |
| 291 | case GGML_OP_MUL_MAT_ID: |
| 292 | { |
| 293 | res += ggml_metal_op_mul_mat_id_extra_tpe(tensor); |
| 294 | res += ggml_metal_op_mul_mat_id_extra_ids(tensor); |
| 295 | } break; |
| 296 | case GGML_OP_FLASH_ATTN_EXT: |
| 297 | { |
| 298 | res += ggml_metal_op_flash_attn_ext_extra_pad(tensor); |
| 299 | res += ggml_metal_op_flash_attn_ext_extra_blk(tensor); |
| 300 | res += ggml_metal_op_flash_attn_ext_extra_tmp(tensor); |
| 301 | res += ggml_metal_op_flash_attn_ext_extra_kv_f16(tensor); |
| 302 | res += ggml_metal_op_flash_attn_ext_extra_idx(tensor); |
| 303 | res += ggml_metal_op_flash_attn_ext_extra_rows(tensor); |
| 304 | } break; |
| 305 | case GGML_OP_CUMSUM: |
| 306 | case GGML_OP_ARGSORT: |
| 307 | { |
| 308 | res *= 2; |
| 309 | } break; |
| 310 | case GGML_OP_TOP_K: |
| 311 | { |
| 312 | res = 2*sizeof(int32_t)*ggml_nelements(tensor->src[0]); |
| 313 | } break; |
| 314 | case GGML_OP_ARGMAX: |
| 315 | { |
| 316 | res += ggml_metal_op_argmax_extra(tensor); |
| 317 | } break; |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | return res; |
| 323 | |
| 324 | GGML_UNUSED(buft); |
| 325 | } |
| 326 | |
| 327 | // default (shared) buffer type |
| 328 | |
| 329 | static const char * ggml_backend_metal_buffer_type_shared_get_name(ggml_backend_buffer_type_t buft) { |
| 330 | ggml_backend_metal_buffer_type * ctx = (ggml_backend_metal_buffer_type *)buft->context; |
| 331 | |
| 332 | return ctx->name.c_str(); |
| 333 | } |
| 334 | |
| 335 | static ggml_backend_buffer_t ggml_backend_metal_buffer_type_shared_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) { |
| 336 | return ggml_backend_metal_buffer_type_alloc_buffer(buft, size, true); |
| 337 | } |
| 338 | |
| 339 | static size_t ggml_backend_metal_buffer_type_shared_get_alignment(ggml_backend_buffer_type_t buft) { |
| 340 | return 32; |
| 341 | |
| 342 | GGML_UNUSED(buft); |
| 343 | } |
| 344 | |
| 345 | static size_t ggml_backend_metal_buffer_type_shared_get_max_size(ggml_backend_buffer_type_t buft) { |
| 346 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)buft->device->context; |
| 347 | |
| 348 | return ggml_metal_device_get_props(ctx_dev)->max_buffer_size; |
| 349 | } |
| 350 | |
| 351 | static size_t ggml_backend_metal_buffer_type_shared_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) { |
| 352 | return ggml_backend_metal_buffer_type_get_alloc_size(buft, tensor); |
| 353 | } |
| 354 | |
| 355 | static bool ggml_backend_metal_buffer_type_shared_is_host(ggml_backend_buffer_type_t buft) { |
| 356 | return false; |
| 357 | |
| 358 | GGML_UNUSED(buft); |
| 359 | } |
| 360 | |
| 361 | static ggml_backend_buffer_type_t ggml_backend_metal_buffer_type_shared(int device) { |
| 362 | static std::mutex mutex; |
| 363 | std::lock_guard<std::mutex> lock(mutex); |
| 364 | |
| 365 | static std::vector<ggml_backend_buffer_type> bufts; |
| 366 | static std::vector<ggml_backend_metal_buffer_type_ptr> ctxs; |
| 367 | |
| 368 | static bool initialized = false; |
| 369 | if (!initialized) { |
| 370 | bufts.reserve(g_devices); |
| 371 | ctxs.reserve(g_devices); |
| 372 | |
| 373 | for (int i = 0; i < g_devices; ++i) { |
| 374 | ggml_backend_metal_buffer_type * raw_ctx = |
| 375 | new ggml_backend_metal_buffer_type { |
| 376 | /* .device = */ i, |
| 377 | /* .name = */ GGML_METAL_NAME + std::to_string(i), |
| 378 | }; |
| 379 | ctxs.emplace_back(raw_ctx); |
| 380 | |
| 381 | ggml_backend_buffer_type buft = { |
| 382 | /* .iface = */ { |
| 383 | /* .get_name = */ ggml_backend_metal_buffer_type_shared_get_name, |
| 384 | /* .alloc_buffer = */ ggml_backend_metal_buffer_type_shared_alloc_buffer, |
| 385 | /* .alloc_buffer_n = */ NULL, |
| 386 | /* .get_alignment = */ ggml_backend_metal_buffer_type_shared_get_alignment, |
| 387 | /* .get_max_size = */ ggml_backend_metal_buffer_type_shared_get_max_size, |
| 388 | /* .get_alloc_size = */ ggml_backend_metal_buffer_type_shared_get_alloc_size, |
| 389 | /* .get_alloc_size_n = */ NULL, |
| 390 | /* .is_host = */ ggml_backend_metal_buffer_type_shared_is_host, |
| 391 | }, |
| 392 | /* .device = */ ggml_backend_reg_dev_get(ggml_backend_metal_reg(), i), |
| 393 | /* .context = */ raw_ctx, |
| 394 | }; |
| 395 | |
| 396 | bufts.emplace_back(buft); |
| 397 | } |
| 398 | |
| 399 | initialized = true; |
| 400 | } |
| 401 | |
| 402 | return &bufts[device]; |
| 403 | } |
| 404 | |
| 405 | // default (private) buffer type |
| 406 | |
| 407 | static const char * ggml_backend_metal_buffer_type_private_get_name(ggml_backend_buffer_type_t buft) { |
| 408 | ggml_backend_metal_buffer_type * ctx = (ggml_backend_metal_buffer_type *)buft->context; |
| 409 | |
| 410 | return ctx->name.c_str(); |
| 411 | } |
| 412 | |
| 413 | static ggml_backend_buffer_t ggml_backend_metal_buffer_type_private_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) { |
| 414 | return ggml_backend_metal_buffer_type_alloc_buffer(buft, size, false); |
| 415 | } |
| 416 | |
| 417 | static size_t ggml_backend_metal_buffer_type_private_get_alignment(ggml_backend_buffer_type_t buft) { |
| 418 | return 32; |
| 419 | |
| 420 | GGML_UNUSED(buft); |
| 421 | } |
| 422 | |
| 423 | static size_t ggml_backend_metal_buffer_type_private_get_max_size(ggml_backend_buffer_type_t buft) { |
| 424 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)buft->device->context; |
| 425 | |
| 426 | return ggml_metal_device_get_props(ctx_dev)->max_buffer_size; |
| 427 | } |
| 428 | |
| 429 | static size_t ggml_backend_metal_buffer_type_private_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) { |
| 430 | return ggml_backend_metal_buffer_type_get_alloc_size(buft, tensor); |
| 431 | } |
| 432 | |
| 433 | static bool ggml_backend_metal_buffer_type_private_is_host(ggml_backend_buffer_type_t buft) { |
| 434 | return false; |
| 435 | |
| 436 | GGML_UNUSED(buft); |
| 437 | } |
| 438 | |
| 439 | static ggml_backend_buffer_type_t ggml_backend_metal_buffer_type_private(int device) { |
| 440 | static std::mutex mutex; |
| 441 | std::lock_guard<std::mutex> lock(mutex); |
| 442 | |
| 443 | static std::vector<ggml_backend_buffer_type> bufts; |
| 444 | static std::vector<ggml_backend_metal_buffer_type_ptr> ctxs; |
| 445 | |
| 446 | static bool initialized = false; |
| 447 | if (!initialized) { |
| 448 | bufts.reserve(g_devices); |
| 449 | ctxs.reserve(g_devices); |
| 450 | |
| 451 | for (int i = 0; i < g_devices; ++i) { |
| 452 | ggml_backend_metal_buffer_type * raw_ctx = new ggml_backend_metal_buffer_type{ |
| 453 | /* .device = */ i, |
| 454 | /* .name = */ GGML_METAL_NAME + std::to_string(i) + "_Private" |
| 455 | }; |
| 456 | ctxs.emplace_back(raw_ctx); |
| 457 | |
| 458 | ggml_backend_buffer_type buft = { |
| 459 | /* .iface = */ { |
| 460 | /* .get_name = */ ggml_backend_metal_buffer_type_private_get_name, |
| 461 | /* .alloc_buffer = */ ggml_backend_metal_buffer_type_private_alloc_buffer, |
| 462 | /* .alloc_buffer_n = */ NULL, |
| 463 | /* .get_alignment = */ ggml_backend_metal_buffer_type_private_get_alignment, |
| 464 | /* .get_max_size = */ ggml_backend_metal_buffer_type_private_get_max_size, |
| 465 | /* .get_alloc_size = */ ggml_backend_metal_buffer_type_private_get_alloc_size, |
| 466 | /* .get_alloc_size_n = */ NULL, |
| 467 | /* .is_host = */ ggml_backend_metal_buffer_type_private_is_host, |
| 468 | }, |
| 469 | /* .device = */ ggml_backend_reg_dev_get(ggml_backend_metal_reg(), i), |
| 470 | /* .context = */ raw_ctx, |
| 471 | }; |
| 472 | |
| 473 | bufts.emplace_back(buft); |
| 474 | } |
| 475 | |
| 476 | initialized = true; |
| 477 | } |
| 478 | |
| 479 | return &bufts[device]; |
| 480 | } |
| 481 | |
| 482 | // mapped buffer type |
| 483 | |
| 484 | static const char * ggml_backend_metal_buffer_type_mapped_get_name(ggml_backend_buffer_type_t buft) { |
| 485 | ggml_backend_metal_buffer_type * ctx = (ggml_backend_metal_buffer_type *)buft->context; |
| 486 | |
| 487 | return ctx->name.c_str(); |
| 488 | } |
| 489 | |
| 490 | static ggml_backend_buffer_t ggml_backend_metal_buffer_type_mapped_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) { |
| 491 | // for mapped buffers, prefer shared memory |
| 492 | return ggml_backend_metal_buffer_type_alloc_buffer(buft, size, true); |
| 493 | } |
| 494 | |
| 495 | static size_t ggml_backend_metal_buffer_type_mapped_get_alignment(ggml_backend_buffer_type_t buft) { |
| 496 | return 32; |
| 497 | |
| 498 | GGML_UNUSED(buft); |
| 499 | } |
| 500 | |
| 501 | static size_t ggml_backend_metal_buffer_type_mapped_get_max_size(ggml_backend_buffer_type_t buft) { |
| 502 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)buft->device->context; |
| 503 | |
| 504 | return ggml_metal_device_get_props(ctx_dev)->max_buffer_size; |
| 505 | } |
| 506 | |
| 507 | static size_t ggml_backend_metal_buffer_type_mapped_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) { |
| 508 | return ggml_backend_metal_buffer_type_get_alloc_size(buft, tensor); |
| 509 | } |
| 510 | |
| 511 | static bool ggml_backend_metal_buffer_type_mapped_is_host(ggml_backend_buffer_type_t buft) { |
| 512 | return false; |
| 513 | |
| 514 | GGML_UNUSED(buft); |
| 515 | } |
| 516 | |
| 517 | static ggml_backend_buffer_type_t ggml_backend_metal_buffer_type_mapped(int device) { |
| 518 | static std::mutex mutex; |
| 519 | std::lock_guard<std::mutex> lock(mutex); |
| 520 | |
| 521 | static std::vector<ggml_backend_buffer_type> bufts; |
| 522 | static std::vector<ggml_backend_metal_buffer_type_ptr> ctxs; |
| 523 | |
| 524 | static bool initialized = false; |
| 525 | if (!initialized) { |
| 526 | bufts.reserve(g_devices); |
| 527 | ctxs.reserve(g_devices); |
| 528 | |
| 529 | for (int i = 0; i < g_devices; ++i) { |
| 530 | ggml_backend_metal_buffer_type * raw_ctx = new ggml_backend_metal_buffer_type{ |
| 531 | /* .device = */ i, |
| 532 | /* .name = */ GGML_METAL_NAME + std::to_string(i) + "_Mapped" |
| 533 | }; |
| 534 | ctxs.emplace_back(raw_ctx); |
| 535 | |
| 536 | // note: not obvious, but this buffer type still needs to implement .alloc_buffer: |
| 537 | // https://github.com/ggml-org/llama.cpp/pull/15832#discussion_r2333177099 |
| 538 | ggml_backend_buffer_type buft = { |
| 539 | /* .iface = */ { |
| 540 | /* .get_name = */ ggml_backend_metal_buffer_type_mapped_get_name, |
| 541 | /* .alloc_buffer = */ ggml_backend_metal_buffer_type_mapped_alloc_buffer, |
| 542 | /* .alloc_buffer_n = */ NULL, |
| 543 | /* .get_alignment = */ ggml_backend_metal_buffer_type_mapped_get_alignment, |
| 544 | /* .get_max_size = */ ggml_backend_metal_buffer_type_mapped_get_max_size, |
| 545 | /* .get_alloc_size = */ ggml_backend_metal_buffer_type_mapped_get_alloc_size, |
| 546 | /* .get_alloc_size_n = */ NULL, |
| 547 | /* .is_host = */ ggml_backend_metal_buffer_type_mapped_is_host, |
| 548 | }, |
| 549 | /* .device = */ ggml_backend_reg_dev_get(ggml_backend_metal_reg(), i), |
| 550 | /* .context = */ raw_ctx, |
| 551 | }; |
| 552 | |
| 553 | bufts.emplace_back(buft); |
| 554 | } |
| 555 | |
| 556 | initialized = true; |
| 557 | } |
| 558 | |
| 559 | return &bufts[device]; |
| 560 | } |
| 561 | |
| 562 | // backend |
| 563 | |
| 564 | static const char * ggml_backend_metal_name(ggml_backend_t backend) { |
| 565 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 566 | |
| 567 | return ggml_metal_get_name(ctx); |
| 568 | } |
| 569 | |
| 570 | static void ggml_backend_metal_free(ggml_backend_t backend) { |
| 571 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 572 | |
| 573 | // wait for any ongoing async operations to finish |
| 574 | ggml_metal_synchronize(ctx); |
| 575 | |
| 576 | ggml_metal_free(ctx); |
| 577 | |
| 578 | free(backend); |
| 579 | } |
| 580 | |
| 581 | static void ggml_backend_metal_synchronize(ggml_backend_t backend) { |
| 582 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 583 | |
| 584 | ggml_metal_synchronize(ctx); |
| 585 | } |
| 586 | |
| 587 | static void ggml_backend_metal_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data, size_t offset, size_t size) { |
| 588 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 589 | |
| 590 | ggml_metal_set_tensor_async(ctx, tensor, data, offset, size); |
| 591 | } |
| 592 | |
| 593 | static void ggml_backend_metal_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data, size_t offset, size_t size) { |
| 594 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 595 | |
| 596 | ggml_metal_get_tensor_async(ctx, tensor, data, offset, size); |
| 597 | } |
| 598 | |
| 599 | static bool ggml_backend_metal_cpy_tensor_async(ggml_backend_t backend_src, ggml_backend_t backend_dst, const ggml_tensor * src, ggml_tensor * dst) { |
| 600 | if (!ggml_backend_is_metal(backend_src) || !ggml_backend_is_metal(backend_dst)) { |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | if (!ggml_backend_buffer_is_metal(src->buffer) || !ggml_backend_buffer_is_metal(dst->buffer)) { |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | ggml_metal_t ctx_src = (ggml_metal_t)backend_src->context; |
| 609 | ggml_metal_t ctx_dst = (ggml_metal_t)backend_dst->context; |
| 610 | |
| 611 | //ggml_backend_buffer_t buf_src = src->view_src ? src->view_src->buffer : src->buffer; |
| 612 | //ggml_backend_buffer_t buf_dst = dst->view_src ? dst->view_src->buffer : dst->buffer; |
| 613 | |
| 614 | //ggml_metal_buffer_t buf_ctx_src = (ggml_metal_buffer_t)buf_src->context; |
| 615 | //ggml_metal_buffer_t buf_ctx_dst = (ggml_metal_buffer_t)buf_dst->context; |
| 616 | |
| 617 | return ggml_metal_cpy_tensor_async(ctx_src, ctx_dst, src, dst); |
| 618 | } |
| 619 | |
| 620 | static enum ggml_status ggml_backend_metal_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) { |
| 621 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 622 | |
| 623 | return ggml_metal_graph_compute(ctx, cgraph); |
| 624 | } |
| 625 | |
| 626 | static void ggml_backend_metal_event_record(ggml_backend_t backend, ggml_backend_event_t event) { |
| 627 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 628 | ggml_metal_event_t ev = (ggml_metal_event_t)event->context; |
| 629 | |
| 630 | ggml_metal_event_record(ctx, ev); |
| 631 | } |
| 632 | |
| 633 | static void ggml_backend_metal_event_wait(ggml_backend_t backend, ggml_backend_event_t event) { |
| 634 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 635 | ggml_metal_event_t ev = (ggml_metal_event_t)event->context; |
| 636 | |
| 637 | ggml_metal_event_wait(ctx, ev); |
| 638 | } |
| 639 | |
| 640 | static void ggml_backend_metal_graph_optimize(ggml_backend_t backend, ggml_cgraph * cgraph, ggml_backend_graph_optimize_params * params) { |
| 641 | GGML_ASSERT(params && params->add_alloc_dep); |
| 642 | |
| 643 | // keep the MoE weighted-reduction inputs alive until the fused output so the |
| 644 | // allocator cannot reuse them while the fused kernel is still reading them |
| 645 | ggml_metal_fusion_add_alloc_deps(params->user_data, params->add_alloc_dep, cgraph); |
| 646 | |
| 647 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 648 | |
| 649 | ggml_metal_graph_optimize(ctx, cgraph); |
| 650 | } |
| 651 | |
| 652 | static void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) { |
| 653 | GGML_ASSERT(ggml_backend_is_metal(backend)); |
| 654 | |
| 655 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 656 | |
| 657 | ggml_metal_set_n_cb(ctx, n_cb); |
| 658 | } |
| 659 | |
| 660 | static ggml_backend_i ggml_backend_metal_i = { |
| 661 | /* .get_name = */ ggml_backend_metal_name, |
| 662 | /* .free = */ ggml_backend_metal_free, |
| 663 | /* .set_tensor_async = */ ggml_backend_metal_set_tensor_async, |
| 664 | /* .get_tensor_async = */ ggml_backend_metal_get_tensor_async, |
| 665 | /* .set_tensor_2d_async = */ NULL, |
| 666 | /* .get_tensor_2d_async = */ NULL, |
| 667 | /* .cpy_tensor_async = */ ggml_backend_metal_cpy_tensor_async, // only needed for multi-GPU setups |
| 668 | /* .synchronize = */ ggml_backend_metal_synchronize, |
| 669 | /* .graph_plan_create = */ NULL, |
| 670 | /* .graph_plan_free = */ NULL, |
| 671 | /* .graph_plan_update = */ NULL, |
| 672 | /* .graph_plan_compute = */ NULL, |
| 673 | /* .graph_compute = */ ggml_backend_metal_graph_compute, |
| 674 | /* .event_record = */ ggml_backend_metal_event_record, |
| 675 | /* .event_wait = */ ggml_backend_metal_event_wait, |
| 676 | /* .graph_optimize = */ ggml_backend_metal_graph_optimize, |
| 677 | }; |
| 678 | |
| 679 | static ggml_guid_t ggml_backend_metal_guid(void) { |
| 680 | static ggml_guid guid = { 0x81, 0xa1, 0x8b, 0x1e, 0x71, 0xec, 0x79, 0xed, 0x2b, 0x85, 0xdc, 0x8a, 0x61, 0x98, 0x30, 0xe6 }; |
| 681 | return &guid; |
| 682 | } |
| 683 | |
| 684 | ggml_backend_t ggml_backend_metal_init(void) { |
| 685 | ggml_backend_dev_t dev = ggml_backend_reg_dev_get(ggml_backend_metal_reg(), 0); |
| 686 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 687 | |
| 688 | ggml_metal_t ctx = ggml_metal_init(ctx_dev); |
| 689 | if (ctx == NULL) { |
| 690 | GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__); |
| 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | ggml_backend_t backend = (ggml_backend_t) malloc(sizeof(ggml_backend)); |
| 695 | |
| 696 | *backend = { |
| 697 | /* .guid = */ ggml_backend_metal_guid(), |
| 698 | /* .interface = */ ggml_backend_metal_i, |
| 699 | /* .device = */ dev, |
| 700 | /* .context = */ ctx, |
| 701 | }; |
| 702 | |
| 703 | ggml_backend_metal_set_n_cb(backend, 1); |
| 704 | |
| 705 | return backend; |
| 706 | } |
| 707 | |
| 708 | bool ggml_backend_is_metal(ggml_backend_t backend) { |
| 709 | return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_metal_guid()); |
| 710 | } |
| 711 | |
| 712 | void ggml_backend_metal_set_abort_callback(ggml_backend_t backend, ggml_abort_callback abort_callback, void * user_data) { |
| 713 | GGML_ASSERT(ggml_backend_is_metal(backend)); |
| 714 | |
| 715 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 716 | |
| 717 | ggml_metal_set_abort_callback(ctx, abort_callback, user_data); |
| 718 | } |
| 719 | |
| 720 | bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family) { |
| 721 | GGML_ASSERT(ggml_backend_is_metal(backend)); |
| 722 | |
| 723 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 724 | |
| 725 | return ggml_metal_supports_family(ctx, family); |
| 726 | } |
| 727 | |
| 728 | void ggml_backend_metal_capture_next_compute(ggml_backend_t backend) { |
| 729 | GGML_ASSERT(ggml_backend_is_metal(backend)); |
| 730 | |
| 731 | ggml_metal_t ctx = (ggml_metal_t)backend->context; |
| 732 | |
| 733 | ggml_metal_capture_next_compute(ctx); |
| 734 | } |
| 735 | |
| 736 | // backend device |
| 737 | |
| 738 | static const char * ggml_backend_metal_device_get_name(ggml_backend_dev_t dev) { |
| 739 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 740 | |
| 741 | const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx_dev); |
| 742 | |
| 743 | return props_dev->name; |
| 744 | } |
| 745 | |
| 746 | static const char * ggml_backend_metal_device_get_description(ggml_backend_dev_t dev) { |
| 747 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 748 | |
| 749 | return ggml_metal_device_get_props(ctx_dev)->desc; |
| 750 | } |
| 751 | |
| 752 | static void ggml_backend_metal_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) { |
| 753 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 754 | |
| 755 | ggml_metal_device_get_memory(ctx_dev, free, total); |
| 756 | } |
| 757 | |
| 758 | static enum ggml_backend_dev_type ggml_backend_metal_device_get_type(ggml_backend_dev_t dev) { |
| 759 | return GGML_BACKEND_DEVICE_TYPE_GPU; |
| 760 | |
| 761 | GGML_UNUSED(dev); |
| 762 | } |
| 763 | |
| 764 | static void ggml_backend_metal_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) { |
| 765 | props->name = ggml_backend_metal_device_get_name(dev); |
| 766 | props->description = ggml_backend_metal_device_get_description(dev); |
| 767 | props->type = ggml_backend_metal_device_get_type(dev); |
| 768 | |
| 769 | ggml_backend_metal_device_get_memory(dev, &props->memory_free, &props->memory_total); |
| 770 | |
| 771 | props->caps = { |
| 772 | /* .async = */ true, |
| 773 | /* .host_buffer = */ false, |
| 774 | /* .buffer_from_host_ptr = */ true, |
| 775 | /* .events = */ true, |
| 776 | /* .mmap_support = */ true, |
| 777 | }; |
| 778 | } |
| 779 | |
| 780 | static ggml_backend_t ggml_backend_metal_device_init_backend(ggml_backend_dev_t dev, const char * params) { |
| 781 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 782 | |
| 783 | ggml_metal_t ctx = ggml_metal_init(ctx_dev); |
| 784 | if (ctx == NULL) { |
| 785 | GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__); |
| 786 | return NULL; |
| 787 | } |
| 788 | |
| 789 | ggml_backend_t backend = (ggml_backend_t) malloc(sizeof(ggml_backend)); |
| 790 | |
| 791 | *backend = { |
| 792 | /* .guid = */ ggml_backend_metal_guid(), |
| 793 | /* .interface = */ ggml_backend_metal_i, |
| 794 | /* .device = */ dev, |
| 795 | /* .context = */ ctx, |
| 796 | }; |
| 797 | |
| 798 | ggml_backend_metal_set_n_cb(backend, 1); |
| 799 | |
| 800 | return backend; |
| 801 | |
| 802 | GGML_UNUSED(params); |
| 803 | } |
| 804 | |
| 805 | static ggml_backend_buffer_type_t ggml_backend_metal_device_get_buffer_type(ggml_backend_dev_t dev) { |
| 806 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 807 | |
| 808 | const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx_dev); |
| 809 | |
| 810 | return props_dev->use_shared_buffers ? ggml_backend_metal_buffer_type_shared(props_dev->device) : ggml_backend_metal_buffer_type_private(props_dev->device); |
| 811 | } |
| 812 | |
| 813 | static ggml_backend_buffer_t ggml_backend_metal_device_buffer_mapped(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) { |
| 814 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 815 | |
| 816 | ggml_metal_buffer_t res = ggml_metal_buffer_map(ctx_dev, ptr, size, max_tensor_size); |
| 817 | |
| 818 | const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx_dev); |
| 819 | |
| 820 | return ggml_backend_buffer_init(ggml_backend_metal_buffer_type_mapped(props_dev->device), ggml_backend_metal_buffer_shared_i, res, size); |
| 821 | } |
| 822 | |
| 823 | static bool ggml_backend_metal_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) { |
| 824 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 825 | |
| 826 | return ggml_metal_device_supports_op(ctx_dev, op); |
| 827 | } |
| 828 | |
| 829 | static bool ggml_backend_metal_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) { |
| 830 | return |
| 831 | buft->device == dev && ( |
| 832 | buft->iface.get_name == ggml_backend_metal_buffer_type_shared_get_name || |
| 833 | buft->iface.get_name == ggml_backend_metal_buffer_type_private_get_name || |
| 834 | buft->iface.get_name == ggml_backend_metal_buffer_type_mapped_get_name); |
| 835 | |
| 836 | GGML_UNUSED(dev); |
| 837 | } |
| 838 | |
| 839 | static int64_t get_op_batch_size(const ggml_tensor * op) { |
| 840 | switch (op->op) { |
| 841 | case GGML_OP_MUL_MAT: |
| 842 | return op->ne[1]; |
| 843 | case GGML_OP_MUL_MAT_ID: |
| 844 | return op->ne[2]; |
| 845 | default: |
| 846 | return ggml_nrows(op); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | static bool ggml_backend_metal_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) { |
| 851 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 852 | |
| 853 | return (op->op == GGML_OP_MUL_MAT || |
| 854 | op->op == GGML_OP_MUL_MAT_ID) && |
| 855 | get_op_batch_size(op) >= ggml_metal_device_get_props(ctx_dev)->op_offload_min_batch_size; |
| 856 | } |
| 857 | |
| 858 | static ggml_backend_event_t ggml_backend_metal_device_event_new(ggml_backend_dev_t dev) { |
| 859 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 860 | |
| 861 | ggml_metal_event_t event = ggml_metal_device_event_init(ctx_dev); |
| 862 | GGML_ASSERT(event); |
| 863 | |
| 864 | ggml_backend_event_t ev = new ggml_backend_event { |
| 865 | /* .device = */ dev, |
| 866 | /* .context = */ event, |
| 867 | }; |
| 868 | |
| 869 | return ev; |
| 870 | } |
| 871 | |
| 872 | static void ggml_backend_metal_device_event_free(ggml_backend_dev_t dev, ggml_backend_event_t event) { |
| 873 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 874 | |
| 875 | ggml_metal_event_t ev = (ggml_metal_event_t)event->context; |
| 876 | |
| 877 | ggml_metal_device_event_free(ctx_dev, ev); |
| 878 | |
| 879 | delete event; |
| 880 | } |
| 881 | |
| 882 | static void ggml_backend_metal_device_event_synchronize(ggml_backend_dev_t dev, ggml_backend_event_t event) { |
| 883 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 884 | |
| 885 | ggml_metal_event_t evt = (ggml_metal_event_t)event->context; |
| 886 | |
| 887 | ggml_metal_device_event_synchronize(ctx_dev, evt); |
| 888 | } |
| 889 | |
| 890 | static ggml_backend_device_i ggml_backend_metal_device_i = { |
| 891 | /* .get_name = */ ggml_backend_metal_device_get_name, |
| 892 | /* .get_description = */ ggml_backend_metal_device_get_description, |
| 893 | /* .get_memory = */ ggml_backend_metal_device_get_memory, |
| 894 | /* .get_type = */ ggml_backend_metal_device_get_type, |
| 895 | /* .get_props = */ ggml_backend_metal_device_get_props, |
| 896 | /* .init_backend = */ ggml_backend_metal_device_init_backend, |
| 897 | /* .get_buffer_type = */ ggml_backend_metal_device_get_buffer_type, |
| 898 | /* .get_host_buffer_type = */ NULL, |
| 899 | /* .buffer_from_host_ptr = */ ggml_backend_metal_device_buffer_mapped, |
| 900 | /* .supports_op = */ ggml_backend_metal_device_supports_op, |
| 901 | /* .supports_buft = */ ggml_backend_metal_device_supports_buft, |
| 902 | /* .offload_op = */ ggml_backend_metal_device_offload_op, |
| 903 | /* .event_new = */ ggml_backend_metal_device_event_new, |
| 904 | /* .event_free = */ ggml_backend_metal_device_event_free, |
| 905 | /* .event_synchronize = */ ggml_backend_metal_device_event_synchronize, |
| 906 | }; |
| 907 | |
| 908 | // backend registry |
| 909 | |
| 910 | struct ggml_backend_metal_reg { |
| 911 | std::vector<ggml_backend_dev_t> devices; |
| 912 | }; |
| 913 | |
| 914 | typedef struct ggml_backend_metal_reg * ggml_backend_metal_reg_t; |
| 915 | |
| 916 | static ggml_backend_metal_reg_t ggml_backend_metal_reg_init(void) { |
| 917 | ggml_backend_metal_reg_t ctx = new struct ggml_backend_metal_reg; |
| 918 | |
| 919 | return ctx; |
| 920 | } |
| 921 | |
| 922 | static void ggml_backend_metal_reg_free(ggml_backend_metal_reg_t ctx) { |
| 923 | delete ctx; |
| 924 | } |
| 925 | |
| 926 | struct ggml_backend_metal_reg_deleter { |
| 927 | void operator()(ggml_backend_metal_reg_t ctx) { |
| 928 | ggml_backend_metal_reg_free(ctx); |
| 929 | } |
| 930 | }; |
| 931 | |
| 932 | typedef std::unique_ptr<struct ggml_backend_metal_reg, ggml_backend_metal_reg_deleter> ggml_backend_metal_reg_ptr; |
| 933 | |
| 934 | static const char * ggml_backend_metal_reg_get_name(ggml_backend_reg_t reg) { |
| 935 | return GGML_METAL_NAME; |
| 936 | |
| 937 | GGML_UNUSED(reg); |
| 938 | } |
| 939 | |
| 940 | static size_t ggml_backend_metal_reg_device_count(ggml_backend_reg_t reg) { |
| 941 | ggml_backend_metal_reg_t ctx = (ggml_backend_metal_reg_t)reg->context; |
| 942 | return ctx->devices.size(); |
| 943 | } |
| 944 | |
| 945 | static ggml_backend_dev_t ggml_backend_metal_reg_device_get(ggml_backend_reg_t reg, size_t index) { |
| 946 | ggml_backend_metal_reg_t ctx = (ggml_backend_metal_reg_t)reg->context; |
| 947 | GGML_ASSERT(index < ctx->devices.size()); |
| 948 | return ctx->devices[index]; |
| 949 | } |
| 950 | |
| 951 | static ggml_backend_feature g_ggml_backend_metal_features[] = { |
| 952 | #if defined(GGML_METAL_EMBED_LIBRARY) |
| 953 | { "EMBED_LIBRARY", "1" }, |
| 954 | #endif |
| 955 | { NULL, NULL }, |
| 956 | }; |
| 957 | |
| 958 | static ggml_backend_feature * ggml_backend_metal_get_features(ggml_backend_reg_t reg) { |
| 959 | return g_ggml_backend_metal_features; |
| 960 | |
| 961 | GGML_UNUSED(reg); |
| 962 | } |
| 963 | |
| 964 | // test/tune-only override for the FA vec (Q, NE) selection, reached via proc_address. |
| 965 | static void ggml_backend_metal_tuning_set_fa_vec_override(int Q, int NE) { |
| 966 | ggml_metal_tuning::fa_vec_set_override({ (int8_t) Q, (int8_t) NE }); |
| 967 | } |
| 968 | |
| 969 | static void ggml_backend_metal_tuning_clear_fa_vec_override(void) { |
| 970 | ggml_metal_tuning::fa_vec_clear_override(); |
| 971 | } |
| 972 | |
| 973 | static int ggml_backend_metal_tuning_fa_vec_ne11_bucket(int64_t ne11) { |
| 974 | return ggml_metal_tuning::fa_vec_ne11_bucket(ne11); |
| 975 | } |
| 976 | |
| 977 | static int ggml_backend_metal_tuning_fa_vec_ne01_bucket(int64_t ne01) { |
| 978 | return ggml_metal_tuning::fa_vec_ne01_bucket(ne01); |
| 979 | } |
| 980 | |
| 981 | static int ggml_backend_metal_tuning_fa_vec_baseline_ne(int dk, int dv) { |
| 982 | return ggml_metal_tuning::fa_vec_baseline_ne(dk, dv); |
| 983 | } |
| 984 | |
| 985 | static const char * ggml_backend_metal_tuning_device_token(ggml_backend_dev_t dev) { |
| 986 | ggml_metal_device_t ctx_dev = (ggml_metal_device_t)dev->context; |
| 987 | |
| 988 | return ggml_metal_device_id_token(ggml_metal_device_get_props(ctx_dev)->device_id); |
| 989 | } |
| 990 | |
| 991 | // generic fusion debugging API (ad-hoc proc-address mechanism): the test resolves the device |
| 992 | // fusion context once and passes that opaque handle to the rest of the functions |
| 993 | typedef void * ggml_backend_fusion_t; |
| 994 | |
| 995 | static ggml_backend_fusion_t ggml_backend_metal_fusion_get(ggml_backend_dev_t dev) { |
| 996 | return ggml_metal_device_get_fusion_info((ggml_metal_device_t)dev->context); |
| 997 | } |
| 998 | |
| 999 | static void ggml_backend_metal_fusion_stats_init(ggml_backend_fusion_t finfo) { |
| 1000 | ggml_metal_fusion_info_stats_init((struct ggml_metal_fusion_info *) finfo); |
| 1001 | } |
| 1002 | |
| 1003 | static void ggml_backend_metal_fusion_stats_reset(ggml_backend_fusion_t finfo) { |
| 1004 | ggml_metal_fusion_info_stats_reset((struct ggml_metal_fusion_info *) finfo); |
| 1005 | } |
| 1006 | |
| 1007 | static int ggml_backend_metal_fusion_stats_get(ggml_backend_fusion_t finfo, const char ** labels, uint64_t * counts, int n) { |
| 1008 | return ggml_metal_fusion_info_stats_get((struct ggml_metal_fusion_info *) finfo, labels, counts, n); |
| 1009 | } |
| 1010 | |
| 1011 | static void ggml_backend_metal_fusion_set_enabled(ggml_backend_fusion_t finfo, bool enabled) { |
| 1012 | ggml_metal_fusion_info_set_enabled((struct ggml_metal_fusion_info *) finfo, enabled); |
| 1013 | } |
| 1014 | |
| 1015 | static void * ggml_backend_metal_get_proc_address(ggml_backend_reg_t reg, const char * name) { |
| 1016 | if (strcmp(name, "ggml_backend_get_features") == 0) { |
| 1017 | return (void *)ggml_backend_metal_get_features; |
| 1018 | } |
| 1019 | if (strcmp(name, "ggml_backend_metal_tuning_set_fa_vec_override") == 0) { |
| 1020 | return (void *)ggml_backend_metal_tuning_set_fa_vec_override; |
| 1021 | } |
| 1022 | if (strcmp(name, "ggml_backend_metal_tuning_clear_fa_vec_override") == 0) { |
| 1023 | return (void *)ggml_backend_metal_tuning_clear_fa_vec_override; |
| 1024 | } |
| 1025 | if (strcmp(name, "ggml_backend_metal_tuning_fa_vec_ne11_bucket") == 0) { |
| 1026 | return (void *)ggml_backend_metal_tuning_fa_vec_ne11_bucket; |
| 1027 | } |
| 1028 | if (strcmp(name, "ggml_backend_metal_tuning_fa_vec_ne01_bucket") == 0) { |
| 1029 | return (void *)ggml_backend_metal_tuning_fa_vec_ne01_bucket; |
| 1030 | } |
| 1031 | if (strcmp(name, "ggml_backend_metal_tuning_fa_vec_baseline_ne") == 0) { |
| 1032 | return (void *)ggml_backend_metal_tuning_fa_vec_baseline_ne; |
| 1033 | } |
| 1034 | if (strcmp(name, "ggml_backend_metal_tuning_device_token") == 0) { |
| 1035 | return (void *)ggml_backend_metal_tuning_device_token; |
| 1036 | } |
| 1037 | // generic fusion debugging API (ad-hoc proc-address mechanism, not part of the official |
| 1038 | // ggml backend interface yet; a backend that adopts it exports these exact names) |
| 1039 | if (strcmp(name, "ggml_backend_fusion_get") == 0) { |
| 1040 | return (void *)ggml_backend_metal_fusion_get; |
| 1041 | } |
| 1042 | if (strcmp(name, "ggml_backend_fusion_stats_init") == 0) { |
| 1043 | return (void *)ggml_backend_metal_fusion_stats_init; |
| 1044 | } |
| 1045 | if (strcmp(name, "ggml_backend_fusion_stats_reset") == 0) { |
| 1046 | return (void *)ggml_backend_metal_fusion_stats_reset; |
| 1047 | } |
| 1048 | if (strcmp(name, "ggml_backend_fusion_stats_get") == 0) { |
| 1049 | return (void *)ggml_backend_metal_fusion_stats_get; |
| 1050 | } |
| 1051 | if (strcmp(name, "ggml_backend_fusion_set_enabled") == 0) { |
| 1052 | return (void *)ggml_backend_metal_fusion_set_enabled; |
| 1053 | } |
| 1054 | |
| 1055 | return NULL; |
| 1056 | |
| 1057 | GGML_UNUSED(reg); |
| 1058 | } |
| 1059 | |
| 1060 | static ggml_backend_reg_i ggml_backend_metal_reg_i = { |
| 1061 | /* .get_name = */ ggml_backend_metal_reg_get_name, |
| 1062 | /* .get_device_count = */ ggml_backend_metal_reg_device_count, |
| 1063 | /* .get_device = */ ggml_backend_metal_reg_device_get, |
| 1064 | /* .get_proc_address = */ ggml_backend_metal_get_proc_address, |
| 1065 | }; |
| 1066 | |
| 1067 | static ggml_backend_dev_t ggml_backend_metal_device_init(ggml_backend_reg_t reg, int device) { |
| 1068 | return new ggml_backend_device { |
| 1069 | /* .iface = */ ggml_backend_metal_device_i, |
| 1070 | /* .reg = */ reg, |
| 1071 | /* .context = */ ggml_metal_device_get(device, g_devices), |
| 1072 | }; |
| 1073 | } |
| 1074 | |
| 1075 | static void ggml_backend_metal_device_free(ggml_backend_dev_t dev) { |
| 1076 | delete dev; |
| 1077 | } |
| 1078 | |
| 1079 | struct ggml_backend_device_deleter { |
| 1080 | void operator()(ggml_backend_dev_t ctx) { |
| 1081 | ggml_backend_metal_device_free(ctx); |
| 1082 | } |
| 1083 | }; |
| 1084 | |
| 1085 | typedef std::unique_ptr<ggml_backend_device, ggml_backend_device_deleter> ggml_backend_device_ptr; |
| 1086 | |
| 1087 | ggml_backend_reg_t ggml_backend_metal_reg(void) { |
| 1088 | static ggml_backend_reg reg; |
| 1089 | static bool initialized = false; |
| 1090 | |
| 1091 | { |
| 1092 | static std::mutex mutex; |
| 1093 | std::lock_guard<std::mutex> lock(mutex); |
| 1094 | |
| 1095 | const char * env = getenv("GGML_METAL_DEVICES"); |
| 1096 | if (env) { |
| 1097 | g_devices = atoi(env); |
| 1098 | } |
| 1099 | |
| 1100 | static std::vector<ggml_backend_device_ptr> devs; |
| 1101 | |
| 1102 | if (!initialized) { |
| 1103 | // workaround macOS limitation (kIOGPUCommandBufferCallbackErrorImpactingInteractivity) until proper fix becomes possible |
| 1104 | // ref: https://github.com/ggml-org/llama.cpp/issues/20141#issuecomment-4272947703 |
| 1105 | setenv("AGX_RELAX_CDM_CTXSTORE_TIMEOUT", "1", true); |
| 1106 | |
| 1107 | static ggml_backend_metal_reg_ptr reg_ctx(ggml_backend_metal_reg_init()); |
| 1108 | |
| 1109 | for (int i = 0; i < g_devices; ++i) { |
| 1110 | auto * dev = ggml_backend_metal_device_init(®, i); |
| 1111 | devs.emplace_back(dev); |
| 1112 | |
| 1113 | reg_ctx->devices.push_back(dev); |
| 1114 | } |
| 1115 | |
| 1116 | reg = { |
| 1117 | /* .api_version = */ GGML_BACKEND_API_VERSION, |
| 1118 | /* .iface = */ ggml_backend_metal_reg_i, |
| 1119 | /* .context = */ reg_ctx.get(), |
| 1120 | }; |
| 1121 | } |
| 1122 | |
| 1123 | initialized = true; |
| 1124 | } |
| 1125 | |
| 1126 | return ® |
| 1127 | } |
| 1128 | |
| 1129 | GGML_BACKEND_DL_IMPL(ggml_backend_metal_reg) |