Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-alloc.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-alloc.c` 1173 lines ยท 45068 bytes Raw Blame History 1 #include "ggml-alloc.h" 2 #include "ggml-backend-impl.h" 3 #include "ggml.h" 4 #include "ggml-impl.h" 5 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 13 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 14 #define MAX_FREE_BLOCKS 256 15 16 //#define GGML_ALLOCATOR_DEBUG 17 18 //#define AT_PRINTF(...) GGML_LOG_DEBUG(__VA_ARGS__) 19 #define AT_PRINTF(...) 20 21 // ops that return true for this function must not use restrict pointers for their backend implementations 22 bool ggml_op_can_inplace(enum ggml_op op) { 23 switch (op) { 24 case GGML_OP_FILL: 25 case GGML_OP_SCALE: 26 case GGML_OP_DIAG_MASK_ZERO: 27 case GGML_OP_DIAG_MASK_INF: 28 case GGML_OP_ADD: 29 case GGML_OP_ADD_ID: 30 case GGML_OP_ADD1: 31 case GGML_OP_SUB: 32 case GGML_OP_MUL: 33 case GGML_OP_DIV: 34 case GGML_OP_SQR: 35 case GGML_OP_SQRT: 36 case GGML_OP_LOG: 37 case GGML_OP_UNARY: 38 case GGML_OP_ROPE: 39 case GGML_OP_ROPE_BACK: 40 case GGML_OP_SILU_BACK: 41 case GGML_OP_RMS_NORM: 42 case GGML_OP_RMS_NORM_BACK: 43 case GGML_OP_CLAMP: 44 case GGML_OP_SOFT_MAX: 45 case GGML_OP_SOFT_MAX_BACK: 46 return true; 47 48 default: 49 return false; 50 } 51 } 52 53 static size_t aligned_offset(const void * buffer, size_t offset, size_t alignment) { 54 assert(alignment && !(alignment & (alignment - 1))); // power of 2 55 size_t align = (alignment - (((uintptr_t)buffer + offset) % alignment)) % alignment; 56 return offset + align; 57 } 58 59 // tallocr 60 61 struct ggml_tallocr ggml_tallocr_new(ggml_backend_buffer_t buffer) { 62 void * base = ggml_backend_buffer_get_base(buffer); 63 size_t align = ggml_backend_buffer_get_alignment(buffer); 64 65 assert(align && !(align & (align - 1))); // power of 2 66 67 struct ggml_tallocr talloc = (struct ggml_tallocr) { 68 /*.buffer = */ buffer, 69 /*.base = */ base, 70 /*.alignment = */ align, 71 /*.offset = */ aligned_offset(base, 0, align), 72 }; 73 return talloc; 74 } 75 76 enum ggml_status ggml_tallocr_alloc(struct ggml_tallocr * talloc, struct ggml_tensor * tensor) { 77 size_t size = ggml_backend_buffer_get_alloc_size(talloc->buffer, tensor); 78 size = GGML_PAD(size, talloc->alignment); 79 80 if (talloc->offset + size > ggml_backend_buffer_get_size(talloc->buffer)) { 81 GGML_LOG_ERROR("%s: not enough space in the buffer to allocate %s (needed %zu, available %zu)\n", 82 __func__, tensor->name, size, ggml_backend_buffer_get_size(talloc->buffer) - talloc->offset); 83 GGML_ABORT("not enough space in the buffer"); 84 } 85 86 void * addr = (char *)ggml_backend_buffer_get_base(talloc->buffer) + talloc->offset; 87 talloc->offset += size; 88 89 assert(((uintptr_t)addr % talloc->alignment) == 0); 90 91 return ggml_backend_tensor_alloc(talloc->buffer, tensor, addr); 92 } 93 94 // dynamic tensor allocator 95 96 #define GGML_VBUFFER_MAX_CHUNKS 16 97 98 // relative memory address within an allocation that can be split into multiple buffers (chunks) 99 struct buffer_address { 100 int chunk; // index of a backend buffer 101 size_t offset; // local memory offset within the buffer 102 }; 103 104 static const struct buffer_address GGML_BUFFER_ADDRESS_INVALID = { -1, SIZE_MAX }; 105 106 static bool ggml_buffer_address_less(struct buffer_address a, struct buffer_address b) { 107 return a.chunk != b.chunk ? a.chunk < b.chunk : a.offset < b.offset; 108 } 109 110 struct free_block { 111 size_t offset; 112 size_t size; 113 }; 114 115 struct tallocr_chunk { 116 struct free_block free_blocks[MAX_FREE_BLOCKS]; 117 int n_free_blocks; 118 size_t max_size; 119 }; 120 121 struct ggml_dyn_tallocr { 122 size_t alignment; 123 size_t max_chunk_size; 124 struct tallocr_chunk * chunks[GGML_VBUFFER_MAX_CHUNKS]; 125 int n_chunks; 126 127 #ifdef GGML_ALLOCATOR_DEBUG 128 struct { 129 const struct ggml_tensor * tensor; 130 struct buffer_address addr; 131 } allocated_tensors[1024]; 132 #endif 133 }; 134 135 static void ggml_dyn_tallocr_insert_block(struct tallocr_chunk * chunk, size_t offset, size_t size) { 136 GGML_ASSERT(chunk->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks"); 137 // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster) 138 int insert_pos = 0; 139 while (insert_pos < chunk->n_free_blocks && chunk->free_blocks[insert_pos].offset < offset) { 140 insert_pos++; 141 } 142 // shift all blocks from insert_pos onward to make room for the new block 143 for (int i = chunk->n_free_blocks; i > insert_pos; i--) { 144 chunk->free_blocks[i] = chunk->free_blocks[i-1]; 145 } 146 // insert the new block 147 chunk->free_blocks[insert_pos].offset = offset; 148 chunk->free_blocks[insert_pos].size = size; 149 chunk->n_free_blocks++; 150 } 151 152 static void ggml_dyn_tallocr_remove_block(struct tallocr_chunk * chunk, int idx) { 153 // shift all elements after idx by 1 to the left, overwriting the element at idx 154 for (int i = idx; i < chunk->n_free_blocks - 1; i++) { 155 chunk->free_blocks[i] = chunk->free_blocks[i+1]; 156 } 157 chunk->n_free_blocks--; 158 } 159 160 static int ggml_dyn_tallocr_new_chunk(struct ggml_dyn_tallocr * alloc, size_t min_size) { 161 if (alloc->n_chunks >= GGML_VBUFFER_MAX_CHUNKS) { 162 return -1; 163 } 164 struct tallocr_chunk * chunk = calloc(1, sizeof(struct tallocr_chunk)); 165 chunk->n_free_blocks = 1; 166 chunk->free_blocks[0].offset = 0; 167 // available space in a chunk is limited to max_chunk_size, but can be higher if: 168 // 1. a single tensor exceeds the maximum, and cannot fit any other way 169 // 2. we are running out of chunks 170 // backends will either manage to allocate the larger size, or report an error. 171 chunk->free_blocks[0].size = MAX(min_size, alloc->max_chunk_size); 172 if (alloc->n_chunks == GGML_VBUFFER_MAX_CHUNKS - 1) { 173 chunk->free_blocks[0].size = SIZE_MAX/2; 174 } 175 alloc->chunks[alloc->n_chunks] = chunk; 176 alloc->n_chunks++; 177 return alloc->n_chunks - 1; 178 } 179 180 #ifdef GGML_ALLOCATOR_DEBUG 181 static void add_allocated_tensor(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, const struct ggml_tensor * tensor) { 182 for (int i = 0; i < 1024; i++) { 183 if (alloc->allocated_tensors[i].tensor == NULL) { 184 alloc->allocated_tensors[i].tensor = tensor; 185 alloc->allocated_tensors[i].addr = addr; 186 return; 187 } 188 } 189 GGML_ABORT("out of allocated_tensors"); 190 } 191 static void remove_allocated_tensor(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, const struct ggml_tensor * tensor) { 192 for (int i = 0; i < 1024; i++) { 193 if (alloc->allocated_tensors[i].addr.chunk == addr.chunk && alloc->allocated_tensors[i].addr.offset == addr.offset) { 194 alloc->allocated_tensors[i].tensor = NULL; 195 return; 196 } 197 } 198 GGML_ABORT("tried to free tensor %s not found\n", tensor->name); 199 } 200 #endif 201 202 static struct buffer_address ggml_dyn_tallocr_alloc(struct ggml_dyn_tallocr * alloc, size_t size, const struct ggml_tensor * tensor) { 203 size = aligned_offset(NULL, size, alloc->alignment); 204 205 AT_PRINTF("%s: allocating %s (%zu bytes) - ", __func__, tensor->name, size); 206 207 int best_fit_chunk = -1; 208 int best_fit_block = -1; 209 size_t max_avail = 0; 210 211 // find the best fitting free block besides the last block, within any chunk 212 for (int c = 0; c < alloc->n_chunks; ++c) { 213 struct tallocr_chunk * chunk = alloc->chunks[c]; 214 size_t best_fit_size = SIZE_MAX; 215 for (int i = 0; i < chunk->n_free_blocks - 1; i++) { 216 struct free_block * block = &chunk->free_blocks[i]; 217 max_avail = MAX(max_avail, block->size); 218 if (block->size >= size && block->size <= best_fit_size) { 219 best_fit_chunk = c; 220 best_fit_block = i; 221 best_fit_size = block->size; 222 } 223 } 224 } 225 226 if (best_fit_block == -1) { 227 // no suitable block found, try the last block (this may grow a chunks size) 228 int64_t best_reuse = INT64_MIN; 229 for (int c = 0; c < alloc->n_chunks; ++c) { 230 struct tallocr_chunk * chunk = alloc->chunks[c]; 231 if (chunk->n_free_blocks > 0) { 232 struct free_block * block = &chunk->free_blocks[chunk->n_free_blocks - 1]; 233 max_avail = MAX(max_avail, block->size); 234 int64_t reuse_factor = chunk->max_size - block->offset - size; 235 // reuse_factor < 0 : amount of extra memory that needs to be allocated 236 // reuse_factor = 0 : allocated free space exactly matches tensor size 237 // reuse_factor > 0 : superfluous memory that will remain unused 238 bool better_reuse = best_reuse < 0 && reuse_factor > best_reuse; 239 bool better_fit = reuse_factor >= 0 && reuse_factor < best_reuse; 240 if (block->size >= size && (better_reuse || better_fit)) { 241 best_fit_chunk = c; 242 best_fit_block = chunk->n_free_blocks - 1; 243 best_reuse = reuse_factor; 244 } 245 } 246 } 247 } 248 249 if (best_fit_block == -1) { 250 // none of the existing chunks have enough space left 251 best_fit_chunk = ggml_dyn_tallocr_new_chunk(alloc, size); 252 best_fit_block = 0; 253 } 254 if (best_fit_chunk == -1) { 255 // since the last chunk always has virtually endless memory, this should never happen 256 GGML_LOG_ERROR("%s: not enough space in the buffer to allocate %zu bytes, largest block available %zu bytes\n", 257 __func__, size, max_avail); 258 GGML_ABORT("graph allocation: failed to reserve memory"); 259 } 260 261 struct tallocr_chunk * chunk = alloc->chunks[best_fit_chunk]; 262 struct free_block * block = &chunk->free_blocks[best_fit_block]; 263 struct buffer_address addr = {.chunk = best_fit_chunk, .offset = block->offset }; 264 block->offset += size; 265 block->size -= size; 266 if (block->size == 0) { 267 // remove block if empty 268 ggml_dyn_tallocr_remove_block(chunk, best_fit_block); 269 } 270 271 AT_PRINTF("block %d, offset %zu, chunk %d\n", best_fit_block, addr.offset, addr.chunk); 272 273 #ifdef GGML_ALLOCATOR_DEBUG 274 add_allocated_tensor(alloc, addr, tensor); 275 size_t cur_max = addr.offset + size; 276 if (cur_max > chunk->max_size) { 277 // sort allocated_tensors by chunk/offset 278 for (int i = 0; i < 1024; i++) { 279 for (int j = i + 1; j < 1024; j++) { 280 if (ggml_buffer_address_less(alloc->allocated_tensors[j].addr, alloc->allocated_tensors[i].addr)) { 281 const struct ggml_tensor * tmp_tensor = alloc->allocated_tensors[i].tensor; 282 struct buffer_address tmp_addr = alloc->allocated_tensors[i].addr; 283 alloc->allocated_tensors[i].tensor = alloc->allocated_tensors[j].tensor; 284 alloc->allocated_tensors[i].addr = alloc->allocated_tensors[j].addr; 285 alloc->allocated_tensors[j].tensor = tmp_tensor; 286 alloc->allocated_tensors[j].addr = tmp_addr; 287 } 288 } 289 } 290 GGML_LOG_DEBUG("max_size[%d] = %.2f MB: tensors: ", addr.chunk, cur_max / 1024.0 / 1024.0); 291 for (int i = 0; i < 1024; i++) { 292 if (alloc->allocated_tensors[i].tensor) { 293 GGML_LOG_DEBUG("%s [%d: %zx-%zx] (%.2f MB) ", alloc->allocated_tensors[i].tensor->name, 294 alloc->allocated_tensors[i].addr.chunk, 295 alloc->allocated_tensors[i].addr.offset, 296 alloc->allocated_tensors[i].addr.offset + ggml_nbytes(alloc->allocated_tensors[i].tensor), 297 ggml_nbytes(alloc->allocated_tensors[i].tensor) / 1024.0 / 1024.0); 298 } 299 } 300 GGML_LOG_DEBUG("\n"); 301 } 302 #endif 303 304 chunk->max_size = MAX(chunk->max_size, addr.offset + size); 305 306 return addr; 307 308 GGML_UNUSED(tensor); 309 } 310 311 // this is a very naive implementation, but for our case the number of free blocks should be very small 312 static void ggml_dyn_tallocr_free_bytes(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, size_t size) { 313 size = aligned_offset(NULL, size, alloc->alignment); 314 315 struct tallocr_chunk * chunk = alloc->chunks[addr.chunk]; 316 317 // see if we can merge with an existing block 318 for (int i = 0; i < chunk->n_free_blocks; i++) { 319 struct free_block * block = &chunk->free_blocks[i]; 320 // check if ptr is at the end of the block 321 if (block->offset + block->size == addr.offset) { 322 block->size += size; 323 // check if we can merge with the next block 324 if (i < chunk->n_free_blocks - 1) { 325 struct free_block * next = &chunk->free_blocks[i+1]; 326 if (block->offset + block->size == next->offset) { 327 block->size += next->size; 328 ggml_dyn_tallocr_remove_block(chunk, i+1); 329 } 330 } 331 return; 332 } 333 // check if ptr is at the beginning of the block 334 if (addr.offset + size == block->offset) { 335 block->offset = addr.offset; 336 block->size += size; 337 // check if we can merge with the previous block 338 if (i > 0) { 339 struct free_block * prev = &chunk->free_blocks[i-1]; 340 if (prev->offset + prev->size == block->offset) { 341 prev->size += block->size; 342 ggml_dyn_tallocr_remove_block(chunk, i); 343 } 344 } 345 return; 346 } 347 } 348 // otherwise, add a new block 349 ggml_dyn_tallocr_insert_block(chunk, addr.offset, size); 350 } 351 352 static void ggml_dyn_tallocr_reset(struct ggml_dyn_tallocr * alloc) { 353 for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS; i++) { 354 free(alloc->chunks[i]); 355 alloc->chunks[i] = NULL; 356 } 357 alloc->n_chunks = 0; 358 359 #ifdef GGML_ALLOCATOR_DEBUG 360 for (int i = 0; i < 1024; i++) { 361 alloc->allocated_tensors[i].tensor = NULL; 362 } 363 #endif 364 } 365 366 static struct ggml_dyn_tallocr * ggml_dyn_tallocr_new(size_t alignment, size_t max_buffer_size) { 367 struct ggml_dyn_tallocr * alloc = (struct ggml_dyn_tallocr *)malloc(sizeof(struct ggml_dyn_tallocr)); 368 369 *alloc = (struct ggml_dyn_tallocr) { 370 /*.alignment = */ alignment, 371 /*.max_chunk_size = */ MIN(max_buffer_size, SIZE_MAX/2), // clamp to avoid overflows 372 /*.chunks = */ {NULL}, 373 /*.n_chunks = */ 0, 374 #ifdef GGML_ALLOCATOR_DEBUG 375 /*.allocated_tensors = */ {{0}}, 376 #endif 377 }; 378 379 ggml_dyn_tallocr_reset(alloc); 380 381 return alloc; 382 } 383 384 static void ggml_dyn_tallocr_free(struct ggml_dyn_tallocr * alloc) { 385 for (int i = 0; i < alloc->n_chunks; ++i) { 386 free(alloc->chunks[i]); 387 } 388 free(alloc); 389 } 390 391 static size_t ggml_dyn_tallocr_max_size(struct ggml_dyn_tallocr * alloc, int chunk) { 392 return chunk < alloc->n_chunks ? alloc->chunks[chunk]->max_size : 0; 393 } 394 395 396 // virtual buffer with contiguous memory range, split into multiple backend buffers (chunks) 397 398 struct vbuffer { 399 ggml_backend_buffer_t chunks[GGML_VBUFFER_MAX_CHUNKS]; 400 }; 401 402 static void ggml_vbuffer_free(struct vbuffer * buf) { 403 if (buf == NULL) { 404 return; 405 } 406 for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS; ++i) { 407 ggml_backend_buffer_free(buf->chunks[i]); 408 } 409 free(buf); 410 } 411 412 static size_t ggml_vbuffer_chunk_size(struct vbuffer * buf, int chunk) { 413 return buf->chunks[chunk] ? ggml_backend_buffer_get_size(buf->chunks[chunk]) : 0; 414 } 415 416 static size_t ggml_vbuffer_size(struct vbuffer * buf) { 417 size_t size = 0; 418 for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS && buf->chunks[i]; ++i) { 419 size += ggml_backend_buffer_get_size(buf->chunks[i]); 420 } 421 return size; 422 } 423 424 static struct vbuffer * ggml_vbuffer_alloc(ggml_backend_buffer_type_t buft, const struct ggml_dyn_tallocr * talloc, enum ggml_backend_buffer_usage usage) { 425 struct vbuffer * buf = (struct vbuffer *)calloc(1, sizeof(struct vbuffer)); 426 if (buf == NULL) { 427 return NULL; 428 } 429 430 for (int n = 0; n < talloc->n_chunks; n++) { 431 size_t chunk_size = talloc->chunks[n]->max_size; 432 buf->chunks[n] = ggml_backend_buft_alloc_buffer(buft, chunk_size); 433 if (buf->chunks[n] == NULL) { 434 ggml_vbuffer_free(buf); 435 return NULL; 436 } 437 ggml_backend_buffer_set_usage(buf->chunks[n], usage); 438 } 439 return buf; 440 } 441 442 static void ggml_vbuffer_tensor_alloc(struct vbuffer * buf, struct ggml_tensor * tensor, struct buffer_address buf_addr) { 443 void * base = ggml_backend_buffer_get_base(buf->chunks[buf_addr.chunk]); 444 void * addr = (char *)base + buf_addr.offset; 445 ggml_backend_tensor_alloc(buf->chunks[buf_addr.chunk], tensor, addr); 446 } 447 448 static void ggml_vbuffer_reset(struct vbuffer * buf) { 449 for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS && buf->chunks[i]; ++i) { 450 ggml_backend_buffer_reset(buf->chunks[i]); 451 } 452 } 453 454 455 ///////////////////////////////////// 456 457 // graph allocator 458 459 struct hash_node { 460 int n_children; 461 int n_views; 462 int buffer_id; 463 struct buffer_address addr; 464 bool allocated; 465 }; 466 467 struct tensor_alloc { 468 int buffer_id; 469 struct buffer_address addr; 470 size_t size_max; // 0 = pre-allocated, unused, or view 471 }; 472 473 struct leaf_alloc { 474 struct tensor_alloc leaf; 475 }; 476 477 struct node_alloc { 478 struct tensor_alloc dst; 479 struct tensor_alloc src[GGML_MAX_SRC]; 480 }; 481 482 struct ggml_gallocr { 483 ggml_backend_buffer_type_t * bufts; // [n_buffers] 484 struct vbuffer ** buffers; // [n_buffers] 485 struct ggml_dyn_tallocr ** buf_tallocs; // [n_buffers] 486 int n_buffers; 487 488 struct ggml_hash_set hash_set; 489 struct hash_node * hash_values; // [hash_set.size] 490 491 struct node_alloc * node_allocs; // [n_nodes] 492 int n_nodes; 493 494 struct leaf_alloc * leaf_allocs; // [n_leafs] 495 int n_leafs; 496 }; 497 498 ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs) { 499 ggml_gallocr_t galloc = (ggml_gallocr_t)calloc(1, sizeof(struct ggml_gallocr)); 500 GGML_ASSERT(galloc != NULL); 501 502 galloc->bufts = calloc(n_bufs, sizeof(ggml_backend_buffer_type_t)); 503 GGML_ASSERT(galloc->bufts != NULL); 504 505 galloc->buffers = calloc(n_bufs, sizeof(struct vbuffer *)); 506 GGML_ASSERT(galloc->buffers != NULL); 507 508 galloc->buf_tallocs = calloc(n_bufs, sizeof(struct ggml_dyn_tallocr *)); 509 GGML_ASSERT(galloc->buf_tallocs != NULL); 510 511 for (int i = 0; i < n_bufs; i++) { 512 galloc->bufts[i] = bufts[i]; 513 galloc->buffers[i] = NULL; 514 515 // check if the same buffer type is used multiple times and reuse the same allocator 516 for (int j = 0; j < i; j++) { 517 if (bufts[i] == bufts[j]) { 518 galloc->buf_tallocs[i] = galloc->buf_tallocs[j]; 519 break; 520 } 521 } 522 523 if (galloc->buf_tallocs[i] == NULL) { 524 size_t alignment = ggml_backend_buft_get_alignment(bufts[i]); 525 size_t max_size = ggml_backend_buft_get_max_size(bufts[i]); 526 galloc->buf_tallocs[i] = ggml_dyn_tallocr_new(alignment, max_size); 527 } 528 } 529 galloc->n_buffers = n_bufs; 530 531 return galloc; 532 } 533 534 ggml_gallocr_t ggml_gallocr_new(ggml_backend_buffer_type_t buft) { 535 return ggml_gallocr_new_n(&buft, 1); 536 } 537 538 void ggml_gallocr_free(ggml_gallocr_t galloc) { 539 if (galloc == NULL) { 540 return; 541 } 542 543 for (int i = 0; i < galloc->n_buffers; i++) { 544 if (galloc->buffers != NULL) { 545 // skip if already freed 546 bool freed = false; 547 for (int j = 0; j < i; j++) { 548 if (galloc->buffers[j] == galloc->buffers[i]) { 549 freed = true; 550 break; 551 } 552 } 553 if (!freed) { 554 ggml_vbuffer_free(galloc->buffers[i]); 555 } 556 } 557 if (galloc->buf_tallocs != NULL) { 558 // skip if already freed 559 bool freed = false; 560 for (int j = 0; j < i; j++) { 561 if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) { 562 freed = true; 563 break; 564 } 565 } 566 if (!freed) { 567 ggml_dyn_tallocr_free(galloc->buf_tallocs[i]); 568 } 569 } 570 } 571 572 ggml_hash_set_free(&galloc->hash_set); 573 free(galloc->hash_values); 574 free(galloc->bufts); 575 free(galloc->buffers); 576 free(galloc->buf_tallocs); 577 free(galloc->node_allocs); 578 free(galloc->leaf_allocs); 579 free(galloc); 580 } 581 582 typedef struct ggml_gallocr * ggml_gallocr_t; 583 584 static struct hash_node * ggml_gallocr_hash_get(ggml_gallocr_t galloc, struct ggml_tensor * t) { 585 size_t i = ggml_hash_find_or_insert(&galloc->hash_set, t); 586 return &galloc->hash_values[i]; 587 } 588 589 static bool ggml_gallocr_is_own(ggml_gallocr_t galloc, struct ggml_tensor * t) { 590 return ggml_gallocr_hash_get(galloc, t)->allocated; 591 } 592 593 static bool ggml_gallocr_is_allocated(ggml_gallocr_t galloc, struct ggml_tensor * t) { 594 return t->data != NULL // tensor data already set externally 595 || t->buffer // tensor on external buffer (but not yet allocated) 596 || ggml_gallocr_is_own(galloc, t); // tensor will be allocated by galloc 597 } 598 599 // free the extra space at the end if the new tensor is smaller 600 static void ggml_gallocr_free_extra_space(ggml_gallocr_t galloc, struct ggml_tensor * node, struct ggml_tensor * parent) { 601 struct hash_node * hn = ggml_gallocr_hash_get(galloc, node); 602 struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent); 603 604 size_t parent_size = ggml_backend_buft_get_alloc_size(galloc->bufts[p_hn->buffer_id], parent); 605 size_t node_size = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node); 606 607 GGML_ASSERT(parent_size >= node_size); 608 609 // note: we want after the freeing the chunks to continue to be aligned 610 struct ggml_dyn_tallocr * p_alloc = galloc->buf_tallocs[p_hn->buffer_id]; 611 parent_size = aligned_offset(NULL, parent_size, p_alloc->alignment); 612 node_size = aligned_offset(NULL, node_size, p_alloc->alignment); 613 614 if (parent_size > node_size) { 615 struct buffer_address p_addr = p_hn->addr; 616 p_addr.offset += node_size; 617 size_t extra_size = parent_size - node_size; 618 AT_PRINTF("freeing extra %zu bytes from parent %s for %s\n", extra_size, parent->name, node->name); 619 ggml_dyn_tallocr_free_bytes(p_alloc, p_addr, extra_size); 620 } 621 } 622 623 static void ggml_gallocr_allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node, int buffer_id) { 624 GGML_ASSERT(buffer_id >= 0); 625 struct hash_node * hn = ggml_gallocr_hash_get(galloc, node); 626 627 if (!ggml_gallocr_is_allocated(galloc, node) && !ggml_impl_is_view(node)) { 628 hn->allocated = true; 629 assert(hn->addr.offset == 0); 630 631 // try to reuse a parent's buffer (inplace) 632 if (ggml_op_can_inplace(node->op)) { 633 for (int i = 0; i < GGML_MAX_SRC; i++) { 634 struct ggml_tensor * parent = node->src[i]; 635 if (parent == NULL) { 636 continue; 637 } 638 639 // if the node's data is external, then we cannot re-use it 640 if (!ggml_gallocr_is_own(galloc, parent)) { 641 AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data); 642 continue; 643 } 644 645 // outputs cannot be reused 646 if (parent->flags & GGML_TENSOR_FLAG_OUTPUT || (parent->view_src != NULL && parent->view_src->flags & GGML_TENSOR_FLAG_OUTPUT)) { 647 AT_PRINTF("not reusing parent %s for %s as it is an output\n", parent->name, node->name); 648 continue; 649 } 650 651 if (!ggml_are_same_layout(node, parent)) { 652 AT_PRINTF("not reusing parent %s for %s as layouts are different\n", parent->name, node->name); 653 continue; 654 } 655 656 struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent); 657 if (p_hn->n_children == 1 && p_hn->n_views == 0) { 658 if (ggml_impl_is_view(parent)) { 659 struct ggml_tensor * view_src = parent->view_src; 660 struct hash_node * view_src_hn = ggml_gallocr_hash_get(galloc, view_src); 661 if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) { 662 AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name); 663 assert(view_src_hn->addr.chunk == p_hn->addr.chunk && view_src_hn->addr.offset == p_hn->addr.offset); 664 hn->buffer_id = p_hn->buffer_id; 665 hn->addr = p_hn->addr; 666 p_hn->allocated = false; // avoid freeing the parent 667 view_src_hn->allocated = false; 668 ggml_gallocr_free_extra_space(galloc, node, view_src); 669 return; 670 } 671 } else { 672 AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name); 673 hn->buffer_id = p_hn->buffer_id; 674 hn->addr = p_hn->addr; 675 p_hn->allocated = false; // avoid freeing the parent 676 ggml_gallocr_free_extra_space(galloc, node, parent); 677 return; 678 } 679 } 680 } 681 } 682 // allocate tensor from the buffer 683 struct ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id]; 684 ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id]; 685 size_t size = ggml_backend_buft_get_alloc_size(buft, node); 686 hn->buffer_id = buffer_id; 687 hn->addr = ggml_dyn_tallocr_alloc(alloc, size, node); 688 } 689 } 690 691 static void ggml_gallocr_free_node(ggml_gallocr_t galloc, struct ggml_tensor * node) { 692 // graph outputs are never freed 693 if (node->flags & GGML_TENSOR_FLAG_OUTPUT) { 694 AT_PRINTF("not freeing output %s\n", node->name); 695 return; 696 } 697 698 struct hash_node * hn = ggml_gallocr_hash_get(galloc, node); 699 int buffer_id = hn->buffer_id; 700 struct ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id]; 701 ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id]; 702 size_t size = ggml_backend_buft_get_alloc_size(buft, node); 703 704 AT_PRINTF("%s: freeing %s at {chunk=%d, offset=%zu} (%zu bytes) - n_free_blocks = %d\n", 705 __func__, node->name, hn->addr.chunk, hn->addr.offset, size, alloc->chunks[hn->addr.chunk]->n_free_blocks); 706 #ifdef GGML_ALLOCATOR_DEBUG 707 remove_allocated_tensor(alloc, hn->addr, node); 708 #endif 709 710 ggml_dyn_tallocr_free_bytes(alloc, hn->addr, size); 711 hn->allocated = false; 712 } 713 714 static int get_node_buffer_id(const int * node_buffer_ids, int i) { 715 return node_buffer_ids ? node_buffer_ids[i] : 0; 716 } 717 718 static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) { 719 // clear hash tables 720 ggml_hash_set_reset(&galloc->hash_set); 721 memset(galloc->hash_values, 0, sizeof(struct hash_node) * galloc->hash_set.size); 722 723 // allocate leafs 724 // these may be tensors that the application is not using in the graph, but may still want to allocate for other purposes 725 for (int i = 0; i < graph->n_leafs; i++) { 726 struct ggml_tensor * leaf = graph->leafs[i]; 727 ggml_gallocr_allocate_node(galloc, leaf, get_node_buffer_id(leaf_buffer_ids, i)); 728 } 729 730 // count number of children and views 731 // allocate other graph inputs and leafs first to avoid overwriting them 732 for (int i = 0; i < graph->n_nodes; i++) { 733 struct ggml_tensor * node = graph->nodes[i]; 734 735 // TODO: better way to add external dependencies 736 // GGML_OP_NONE does not appear normally in the graph nodes, but is used by ggml-backend to add dependencies to 737 // control when some tensors are allocated and freed. in this case, the dependencies are in `src`, but the node 738 // itself is never used and should not be considered a dependency 739 if (ggml_impl_is_view(node) && node->op != GGML_OP_NONE) { 740 struct ggml_tensor * view_src = node->view_src; 741 ggml_gallocr_hash_get(galloc, view_src)->n_views += 1; 742 } 743 744 if (node->flags & GGML_TENSOR_FLAG_INPUT) { 745 ggml_gallocr_allocate_node(galloc, graph->nodes[i], get_node_buffer_id(node_buffer_ids, i)); 746 } 747 748 for (int j = 0; j < GGML_MAX_SRC; j++) { 749 struct ggml_tensor * src = node->src[j]; 750 if (src == NULL) { 751 continue; 752 } 753 754 ggml_gallocr_hash_get(galloc, src)->n_children += 1; 755 756 // allocate explicit inputs 757 if (src->flags & GGML_TENSOR_FLAG_INPUT) { 758 ggml_gallocr_allocate_node(galloc, src, get_node_buffer_id(node_buffer_ids, i)); 759 } 760 } 761 } 762 763 // allocate tensors 764 for (int i = 0; i < graph->n_nodes; i++) { 765 struct ggml_tensor * node = graph->nodes[i]; 766 int buffer_id = get_node_buffer_id(node_buffer_ids, i); 767 768 // allocate parents (only leafs need to be allocated at this point) 769 for (int j = 0; j < GGML_MAX_SRC; j++) { 770 struct ggml_tensor * parent = node->src[j]; 771 if (parent == NULL) { 772 continue; 773 } 774 ggml_gallocr_allocate_node(galloc, parent, buffer_id); 775 } 776 777 // allocate node 778 ggml_gallocr_allocate_node(galloc, node, buffer_id); 779 780 AT_PRINTF("exec: %s (%s) <= ", ggml_op_desc(node), node->name); 781 for (int j = 0; j < GGML_MAX_SRC; j++) { 782 struct ggml_tensor * parent = node->src[j]; 783 if (parent == NULL) { 784 continue; 785 } 786 AT_PRINTF("%s", parent->name); 787 if (j < GGML_MAX_SRC - 1 && node->src[j + 1] != NULL) { 788 AT_PRINTF(", "); 789 } 790 } 791 AT_PRINTF("\n"); 792 793 // update parents 794 for (int j = 0; j < GGML_MAX_SRC; j++) { 795 struct ggml_tensor * parent = node->src[j]; 796 if (parent == NULL) { 797 continue; 798 } 799 struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent); 800 p_hn->n_children -= 1; 801 802 AT_PRINTF("parent %s: %d children, %d views, allocated: %d\n", 803 parent->name, p_hn->n_children, p_hn->n_views, p_hn->allocated); 804 805 if (p_hn->n_children == 0 && p_hn->n_views == 0) { 806 if (ggml_impl_is_view(parent)) { 807 struct ggml_tensor * view_src = parent->view_src; 808 struct hash_node * view_src_hn = ggml_gallocr_hash_get(galloc, view_src); 809 view_src_hn->n_views -= 1; 810 AT_PRINTF("view_src %s: %d children, %d views\n", 811 view_src->name, view_src_hn->n_children, view_src_hn->n_views); 812 if (view_src_hn->n_views == 0 && view_src_hn->n_children == 0 && view_src_hn->allocated) { 813 ggml_gallocr_free_node(galloc, view_src); 814 } 815 } 816 else if (p_hn->allocated) { 817 ggml_gallocr_free_node(galloc, parent); 818 } 819 } 820 AT_PRINTF("\n"); 821 } 822 } 823 } 824 825 static bool ggml_gallocr_reserve_n_impl( 826 ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids, bool no_alloc) { 827 size_t min_hash_size = graph->n_nodes + graph->n_leafs; 828 // add 25% margin to avoid hash collisions 829 min_hash_size += min_hash_size / 4; 830 831 // initialize hash table 832 if (galloc->hash_set.size < min_hash_size) { 833 ggml_hash_set_free(&galloc->hash_set); 834 galloc->hash_set = ggml_hash_set_new(min_hash_size); 835 GGML_ASSERT(galloc->hash_set.keys != NULL); 836 837 free(galloc->hash_values); 838 galloc->hash_values = malloc(sizeof(struct hash_node) * galloc->hash_set.size); 839 GGML_ASSERT(galloc->hash_values != NULL); 840 } 841 842 // reset allocators 843 for (int i = 0; i < galloc->n_buffers; i++) { 844 ggml_dyn_tallocr_reset(galloc->buf_tallocs[i]); 845 } 846 847 // allocate in hash table 848 ggml_gallocr_alloc_graph_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids); 849 850 // set the node_allocs from the hash table 851 if (galloc->n_nodes < graph->n_nodes) { 852 free(galloc->node_allocs); 853 galloc->node_allocs = calloc(graph->n_nodes, sizeof(struct node_alloc)); 854 GGML_ASSERT(galloc->node_allocs != NULL); 855 } 856 galloc->n_nodes = graph->n_nodes; 857 for (int i = 0; i < graph->n_nodes; i++) { 858 struct ggml_tensor * node = graph->nodes[i]; 859 struct node_alloc * node_alloc = &galloc->node_allocs[i]; 860 if (node->view_src || node->data) { 861 node_alloc->dst.buffer_id = -1; 862 node_alloc->dst.addr = GGML_BUFFER_ADDRESS_INVALID; 863 node_alloc->dst.size_max = 0; 864 } else { 865 struct hash_node * hn = ggml_gallocr_hash_get(galloc, node); 866 node_alloc->dst.buffer_id = hn->buffer_id; 867 node_alloc->dst.addr = hn->addr; 868 node_alloc->dst.size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node); 869 } 870 for (int j = 0; j < GGML_MAX_SRC; j++) { 871 struct ggml_tensor * src = node->src[j]; 872 if (!src || src->view_src || src->data) { 873 node_alloc->src[j].buffer_id = -1; 874 node_alloc->src[j].addr = GGML_BUFFER_ADDRESS_INVALID; 875 node_alloc->src[j].size_max = 0; 876 } else { 877 struct hash_node * hn = ggml_gallocr_hash_get(galloc, src); 878 node_alloc->src[j].buffer_id = hn->buffer_id; 879 node_alloc->src[j].addr = hn->addr; 880 node_alloc->src[j].size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], src); 881 } 882 } 883 } 884 if (galloc->n_leafs < graph->n_leafs) { 885 free(galloc->leaf_allocs); 886 galloc->leaf_allocs = calloc(graph->n_leafs, sizeof(galloc->leaf_allocs[0])); 887 GGML_ASSERT(galloc->leaf_allocs != NULL); 888 } 889 galloc->n_leafs = graph->n_leafs; 890 for (int i = 0; i < graph->n_leafs; i++) { 891 struct ggml_tensor * leaf = graph->leafs[i]; 892 struct hash_node * hn = ggml_gallocr_hash_get(galloc, leaf); 893 if (leaf->view_src || leaf->data) { 894 galloc->leaf_allocs[i].leaf.buffer_id = -1; 895 galloc->leaf_allocs[i].leaf.addr = GGML_BUFFER_ADDRESS_INVALID; 896 galloc->leaf_allocs[i].leaf.size_max = 0; 897 } else { 898 galloc->leaf_allocs[i].leaf.buffer_id = hn->buffer_id; 899 galloc->leaf_allocs[i].leaf.addr = hn->addr; 900 galloc->leaf_allocs[i].leaf.size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], leaf); 901 } 902 } 903 904 // reallocate buffers if needed 905 for (int i = 0; i < galloc->n_buffers; i++) { 906 // if the buffer type is used multiple times, we reuse the same buffer 907 for (int j = 0; j < i; j++) { 908 if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) { 909 galloc->buffers[i] = galloc->buffers[j]; 910 break; 911 } 912 } 913 914 // even if there are no tensors allocated in this buffer, we still need to allocate it to initialize views 915 bool realloc = galloc->buffers[i] == NULL; 916 size_t new_size = 0; 917 for (int c = 0; c < galloc->buf_tallocs[i]->n_chunks; c++) { 918 size_t cur_chunk_size = galloc->buffers[i] ? ggml_vbuffer_chunk_size(galloc->buffers[i], c) : 0; 919 size_t new_chunk_size = ggml_dyn_tallocr_max_size(galloc->buf_tallocs[i], c); 920 new_size += new_chunk_size; 921 if (new_chunk_size > cur_chunk_size) { 922 realloc = true; 923 } 924 } 925 if (realloc) { 926 #ifndef NDEBUG 927 { 928 size_t cur_size = galloc->buffers[i] ? ggml_vbuffer_size(galloc->buffers[i]) : 0; 929 if (cur_size > 0) { 930 GGML_LOG_DEBUG("%s: reallocating %s buffer from size %.02f MiB to %.02f MiB\n", 931 __func__, ggml_backend_buft_name(galloc->bufts[i]), cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0); 932 } 933 } 934 #endif 935 ggml_vbuffer_free(galloc->buffers[i]); 936 if (no_alloc) { 937 galloc->buffers[i] = NULL; 938 } else { 939 galloc->buffers[i] = ggml_vbuffer_alloc(galloc->bufts[i], galloc->buf_tallocs[i], GGML_BACKEND_BUFFER_USAGE_COMPUTE); 940 if (galloc->buffers[i] == NULL) { 941 GGML_LOG_ERROR("%s: failed to allocate %s buffer of size %zu\n", __func__, ggml_backend_buft_name(galloc->bufts[i]), new_size); 942 return false; 943 } 944 } 945 } 946 } 947 948 return true; 949 } 950 951 void ggml_gallocr_reserve_n_size( 952 ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids, size_t * sizes) { 953 GGML_ASSERT(ggml_gallocr_reserve_n_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids, /*no_alloc =*/ true)); 954 for (int i = 0; i < galloc->n_buffers; i++) { 955 sizes[i] = 0; 956 for (int c = 0; c < galloc->buf_tallocs[i]->n_chunks; c++) { 957 sizes[i] += galloc->buf_tallocs[i]->chunks[c]->max_size; 958 } 959 } 960 } 961 962 bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) { 963 return ggml_gallocr_reserve_n_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids, /*no_alloc =*/ false); 964 } 965 966 bool ggml_gallocr_reserve(ggml_gallocr_t galloc, struct ggml_cgraph *graph) { 967 return ggml_gallocr_reserve_n(galloc, graph, NULL, NULL); 968 } 969 970 static void ggml_gallocr_init_tensor(ggml_gallocr_t galloc, struct ggml_tensor * tensor, struct tensor_alloc * tensor_alloc) { 971 int buffer_id = tensor_alloc->buffer_id; 972 assert(tensor->data || tensor->view_src || ggml_backend_buft_get_alloc_size(galloc->bufts[buffer_id], tensor) <= tensor_alloc->size_max); 973 974 if (tensor->view_src != NULL) { 975 if (tensor->buffer == NULL) { 976 assert(tensor_alloc->addr.offset == SIZE_MAX); 977 if (tensor->view_src->buffer == NULL) { 978 // this tensor was allocated without ggml-backend 979 return; 980 } 981 ggml_backend_view_init(tensor); 982 } 983 } else { 984 if (tensor->data == NULL) { 985 assert(tensor_alloc->addr.offset != SIZE_MAX); 986 assert(ggml_backend_buft_get_alloc_size(galloc->bufts[buffer_id], tensor) <= tensor_alloc->size_max); 987 ggml_vbuffer_tensor_alloc(galloc->buffers[buffer_id], tensor, tensor_alloc->addr); 988 } else { 989 if (tensor->buffer == NULL) { 990 // this tensor was allocated without ggml-backend 991 return; 992 } 993 } 994 } 995 } 996 997 static bool ggml_gallocr_node_needs_realloc(ggml_gallocr_t galloc, struct ggml_tensor * node, struct tensor_alloc * talloc) { 998 size_t node_size = 0; 999 if (!node->data && !node->view_src) { 1000 // If we previously had data but don't now then reallocate 1001 if (talloc->buffer_id < 0) { 1002 return false; 1003 } 1004 node_size = ggml_backend_buft_get_alloc_size(galloc->bufts[talloc->buffer_id], node); 1005 } 1006 return talloc->size_max >= node_size; 1007 } 1008 1009 static bool ggml_gallocr_needs_realloc(ggml_gallocr_t galloc, struct ggml_cgraph * graph) { 1010 if (galloc->n_nodes != graph->n_nodes) { 1011 #ifndef NDEBUG 1012 GGML_LOG_DEBUG("%s: graph has different number of nodes\n", __func__); 1013 #endif 1014 return true; 1015 } 1016 1017 if (galloc->n_leafs != graph->n_leafs) { 1018 #ifndef NDEBUG 1019 GGML_LOG_DEBUG("%s: graph has different number of leafs\n", __func__); 1020 #endif 1021 return true; 1022 } 1023 1024 for (int i = 0; i < graph->n_nodes; i++) { 1025 struct ggml_tensor * node = graph->nodes[i]; 1026 struct node_alloc * node_alloc = &galloc->node_allocs[i]; 1027 1028 if (!ggml_gallocr_node_needs_realloc(galloc, node, &node_alloc->dst)) { 1029 #ifndef NDEBUG 1030 GGML_LOG_DEBUG("%s: node %s is not valid\n", __func__, node->name); 1031 #endif 1032 return true; 1033 } 1034 1035 for (int j = 0; j < GGML_MAX_SRC; j++) { 1036 struct ggml_tensor * src = node->src[j]; 1037 if (src == NULL) { 1038 continue; 1039 } 1040 if (!ggml_gallocr_node_needs_realloc(galloc, src, &node_alloc->src[j])) { 1041 #ifndef NDEBUG 1042 GGML_LOG_DEBUG("%s: src %d (%s) of node %s is not valid\n", __func__, j, src->name, node->name); 1043 #endif 1044 return true; 1045 } 1046 } 1047 } 1048 1049 return false; 1050 } 1051 1052 bool ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, struct ggml_cgraph * graph) { 1053 if (ggml_gallocr_needs_realloc(galloc, graph)) { 1054 if (galloc->n_buffers == 1) { 1055 #ifndef NDEBUG 1056 GGML_LOG_DEBUG("%s: reallocating buffers automatically\n", __func__); 1057 #endif 1058 if (!ggml_gallocr_reserve(galloc, graph)) { 1059 return false; 1060 } 1061 } else { 1062 #ifndef NDEBUG 1063 GGML_LOG_DEBUG("%s: cannot reallocate multi buffer graph automatically, call reserve\n", __func__); 1064 #endif 1065 return false; 1066 } 1067 } 1068 1069 // reset buffers 1070 for (int i = 0; i < galloc->n_buffers; i++) { 1071 if (galloc->buffers[i] != NULL) { 1072 ggml_vbuffer_reset(galloc->buffers[i]); 1073 } 1074 } 1075 1076 // allocate the graph tensors from the previous assignments 1077 // leafs 1078 for (int i = 0; i < graph->n_leafs; i++) { 1079 struct ggml_tensor * leaf = graph->leafs[i]; 1080 struct leaf_alloc * leaf_alloc = &galloc->leaf_allocs[i]; 1081 ggml_gallocr_init_tensor(galloc, leaf, &leaf_alloc->leaf); 1082 } 1083 // nodes 1084 for (int i = 0; i < graph->n_nodes; i++) { 1085 struct ggml_tensor * node = graph->nodes[i]; 1086 struct node_alloc * node_alloc = &galloc->node_allocs[i]; 1087 for (int j = 0; j < GGML_MAX_SRC; j++) { 1088 struct ggml_tensor * src = node->src[j]; 1089 if (src == NULL) { 1090 continue; 1091 } 1092 ggml_gallocr_init_tensor(galloc, src, &node_alloc->src[j]); 1093 } 1094 ggml_gallocr_init_tensor(galloc, node, &node_alloc->dst); 1095 } 1096 1097 return true; 1098 } 1099 1100 size_t ggml_gallocr_get_buffer_size(ggml_gallocr_t galloc, int buffer_id) { 1101 GGML_ASSERT(buffer_id >= 0 && buffer_id < galloc->n_buffers); 1102 1103 if (galloc->buffers[buffer_id] == NULL) { 1104 return 0; 1105 } 1106 1107 for (int i = 0; i < buffer_id; i++) { 1108 if (galloc->buffers[i] == galloc->buffers[buffer_id]) { 1109 // this buffer is the same as a previous one due to the same buffer type being used multiple times 1110 // only return the buffer size the first time it appears to avoid double counting 1111 return 0; 1112 } 1113 } 1114 1115 return ggml_vbuffer_size(galloc->buffers[buffer_id]); 1116 } 1117 1118 // utils 1119 1120 static struct ggml_tensor ** ggml_backend_alloc_ctx_tensors_from_buft_collect( 1121 struct ggml_context * ctx, int * n_tensors) { 1122 int n = 0; 1123 for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { 1124 n++; 1125 } 1126 *n_tensors = n; 1127 if (n == 0) { 1128 return NULL; 1129 } 1130 1131 struct ggml_tensor ** tensors = (struct ggml_tensor **) malloc(n * sizeof(struct ggml_tensor *)); 1132 if (tensors == NULL) { 1133 GGML_LOG_ERROR("%s: failed to allocate %zu bytes\n", __func__, n * sizeof(struct ggml_tensor *)); 1134 return NULL; 1135 } 1136 int i = 0; 1137 for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) { 1138 tensors[i++] = t; 1139 } 1140 return tensors; 1141 } 1142 1143 ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) { 1144 GGML_ASSERT(ggml_get_no_alloc(ctx) == true); 1145 1146 int n_tensors = 0; 1147 struct ggml_tensor ** tensors = ggml_backend_alloc_ctx_tensors_from_buft_collect(ctx, &n_tensors); 1148 if (tensors == NULL) { 1149 return NULL; 1150 } 1151 1152 ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer_n(buft, tensors, n_tensors); 1153 free(tensors); 1154 return buffer; 1155 } 1156 1157 size_t ggml_backend_alloc_ctx_tensors_from_buft_size(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) { 1158 GGML_ASSERT(ggml_get_no_alloc(ctx) == true); 1159 1160 int n_tensors = 0; 1161 struct ggml_tensor ** tensors = ggml_backend_alloc_ctx_tensors_from_buft_collect(ctx, &n_tensors); 1162 if (tensors == NULL) { 1163 return 0; 1164 } 1165 1166 size_t nbytes_total = ggml_backend_buft_get_alloc_size_n(buft, tensors, n_tensors); 1167 free(tensors); 1168 return nbytes_total; 1169 } 1170 1171 ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, ggml_backend_t backend) { 1172 return ggml_backend_alloc_ctx_tensors_from_buft(ctx, ggml_backend_get_default_buffer_type(backend)); 1173 }