ggml-alloc.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #include "ggml-alloc.h"
  27. #include "ggml-backend-impl.h"
  28. #include "ggml.h"
  29. #include "ggml-impl.h"
  30. #include <assert.h>
  31. #include <limits.h>
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  37. #define MAX_FREE_BLOCKS 256
  38. //#define GGML_ALLOCATOR_DEBUG
  39. //#define AT_PRINTF(...) fprintf(stderr, __VA_ARGS__)
  40. #define AT_PRINTF(...)
  41. static bool ggml_is_view(const struct ggml_tensor * t) {
  42. return t->view_src != NULL;
  43. }
  44. static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {
  45. if (a->type != b->type) {
  46. return false;
  47. }
  48. for (int i = 0; i < GGML_MAX_DIMS; i++) {
  49. if (a->ne[i] != b->ne[i]) {
  50. return false;
  51. }
  52. if (a->nb[i] != b->nb[i]) {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. static bool ggml_op_can_inplace(enum ggml_op op) {
  59. switch (op) {
  60. case GGML_OP_SCALE:
  61. case GGML_OP_DIAG_MASK_ZERO:
  62. case GGML_OP_DIAG_MASK_INF:
  63. case GGML_OP_ADD:
  64. case GGML_OP_ADD1:
  65. case GGML_OP_SUB:
  66. case GGML_OP_MUL:
  67. case GGML_OP_DIV:
  68. case GGML_OP_SQR:
  69. case GGML_OP_SQRT:
  70. case GGML_OP_LOG:
  71. case GGML_OP_UNARY:
  72. case GGML_OP_ROPE:
  73. case GGML_OP_RMS_NORM:
  74. case GGML_OP_SOFT_MAX:
  75. return true;
  76. default:
  77. return false;
  78. }
  79. }
  80. static size_t aligned_offset(const void * buffer, size_t offset, size_t alignment) {
  81. assert(alignment && !(alignment & (alignment - 1))); // power of 2
  82. size_t align = (alignment - (((uintptr_t)buffer + offset) % alignment)) % alignment;
  83. return offset + align;
  84. }
  85. // tallocr
  86. struct ggml_tallocr ggml_tallocr_new(ggml_backend_buffer_t buffer) {
  87. void * base = ggml_backend_buffer_get_base(buffer);
  88. size_t align = ggml_backend_buffer_get_alignment(buffer);
  89. assert(align && !(align & (align - 1))); // power of 2
  90. struct ggml_tallocr talloc = (struct ggml_tallocr) {
  91. /*.buffer = */ buffer,
  92. /*.base = */ base,
  93. /*.alignment = */ align,
  94. /*.offset = */ aligned_offset(base, 0, align),
  95. };
  96. return talloc;
  97. }
  98. void ggml_tallocr_alloc(struct ggml_tallocr * talloc, struct ggml_tensor * tensor) {
  99. size_t size = ggml_backend_buffer_get_alloc_size(talloc->buffer, tensor);
  100. size = GGML_PAD(size, talloc->alignment);
  101. if (talloc->offset + size > ggml_backend_buffer_get_size(talloc->buffer)) {
  102. fprintf(stderr, "%s: not enough space in the buffer to allocate %s (needed %zu, available %zu)\n",
  103. __func__, tensor->name, size, ggml_backend_buffer_get_size(talloc->buffer) - talloc->offset);
  104. GGML_ABORT("not enough space in the buffer");
  105. }
  106. void * addr = (char *)ggml_backend_buffer_get_base(talloc->buffer) + talloc->offset;
  107. talloc->offset += size;
  108. assert(((uintptr_t)addr % talloc->alignment) == 0);
  109. ggml_backend_tensor_alloc(talloc->buffer, tensor, addr);
  110. }
  111. // dynamic tensor allocator
  112. struct free_block {
  113. size_t offset;
  114. size_t size;
  115. };
  116. struct ggml_dyn_tallocr {
  117. size_t alignment;
  118. int n_free_blocks;
  119. struct free_block free_blocks[MAX_FREE_BLOCKS];
  120. size_t max_size;
  121. #ifdef GGML_ALLOCATOR_DEBUG
  122. struct {
  123. const struct ggml_tensor * tensor;
  124. size_t offset;
  125. } allocated_tensors[1024];
  126. #endif
  127. };
  128. #ifdef GGML_ALLOCATOR_DEBUG
  129. static void add_allocated_tensor(struct ggml_dyn_tallocr * alloc, size_t offset, const struct ggml_tensor * tensor) {
  130. for (int i = 0; i < 1024; i++) {
  131. if (alloc->allocated_tensors[i].tensor == NULL) {
  132. alloc->allocated_tensors[i].tensor = tensor;
  133. alloc->allocated_tensors[i].offset = offset;
  134. return;
  135. }
  136. }
  137. GGML_ABORT("out of allocated_tensors");
  138. }
  139. static void remove_allocated_tensor(struct ggml_dyn_tallocr * alloc, size_t offset, const struct ggml_tensor * tensor) {
  140. for (int i = 0; i < 1024; i++) {
  141. if (alloc->allocated_tensors[i].offset == offset) {
  142. alloc->allocated_tensors[i].tensor = NULL;
  143. return;
  144. }
  145. }
  146. GGML_ABORT("tried to free tensor %s not found\n", tensor->name);
  147. }
  148. #endif
  149. static size_t ggml_dyn_tallocr_alloc(struct ggml_dyn_tallocr * alloc, size_t size, const struct ggml_tensor * tensor) {
  150. size = aligned_offset(NULL, size, alloc->alignment);
  151. AT_PRINTF("%s: allocating %s (%zu bytes) - ", __func__, tensor->name, size);
  152. size_t max_avail = 0;
  153. // find the best fitting free block besides the last block
  154. int best_fit_block = -1;
  155. size_t best_fit_size = SIZE_MAX;
  156. for (int i = 0; i < alloc->n_free_blocks - 1; i++) {
  157. struct free_block * block = &alloc->free_blocks[i];
  158. max_avail = MAX(max_avail, block->size);
  159. if (block->size >= size && block->size <= best_fit_size) {
  160. best_fit_block = i;
  161. best_fit_size = block->size;
  162. }
  163. }
  164. if (best_fit_block == -1) {
  165. // the last block is our last resort
  166. struct free_block * block = &alloc->free_blocks[alloc->n_free_blocks - 1];
  167. max_avail = MAX(max_avail, block->size);
  168. if (block->size >= size) {
  169. best_fit_block = alloc->n_free_blocks - 1;
  170. } else {
  171. // this should never happen
  172. fprintf(stderr, "%s: not enough space in the buffer to allocate %zu bytes, largest block available %zu bytes\n",
  173. __func__, size, max_avail);
  174. GGML_ABORT("not enough space in the buffer");
  175. }
  176. }
  177. struct free_block * block = &alloc->free_blocks[best_fit_block];
  178. size_t offset = block->offset;
  179. block->offset = offset + size;
  180. block->size -= size;
  181. if (block->size == 0) {
  182. // remove block if empty
  183. alloc->n_free_blocks--;
  184. for (int j = best_fit_block; j < alloc->n_free_blocks; j++) {
  185. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  186. }
  187. }
  188. AT_PRINTF("block %d, offset %zu\n", best_fit_block, offset);
  189. #ifdef GGML_ALLOCATOR_DEBUG
  190. add_allocated_tensor(alloc, offset, tensor);
  191. size_t cur_max = offset + size;
  192. if (cur_max > alloc->max_size) {
  193. // sort allocated_tensors by offset
  194. for (int i = 0; i < 1024; i++) {
  195. for (int j = i + 1; j < 1024; j++) {
  196. if (alloc->allocated_tensors[i].offset > alloc->allocated_tensors[j].offset) {
  197. const struct ggml_tensor * tmp_tensor = alloc->allocated_tensors[i].tensor;
  198. size_t tmp_offset = alloc->allocated_tensors[i].offset;
  199. alloc->allocated_tensors[i].tensor = alloc->allocated_tensors[j].tensor;
  200. alloc->allocated_tensors[i].offset = alloc->allocated_tensors[j].offset;
  201. alloc->allocated_tensors[j].tensor = tmp_tensor;
  202. alloc->allocated_tensors[j].offset = tmp_offset;
  203. }
  204. }
  205. }
  206. fprintf(stderr, "max_size = %.2f MB: tensors: ", cur_max / 1024.0 / 1024.0);
  207. for (int i = 0; i < 1024; i++) {
  208. if (alloc->allocated_tensors[i].tensor) {
  209. fprintf(stderr, "%s [%zx-%zx] (%.2f MB) ", alloc->allocated_tensors[i].tensor->name,
  210. alloc->allocated_tensors[i].offset,
  211. alloc->allocated_tensors[i].offset + ggml_nbytes(alloc->allocated_tensors[i].tensor),
  212. ggml_nbytes(alloc->allocated_tensors[i].tensor) / 1024.0 / 1024.0);
  213. }
  214. }
  215. fprintf(stderr, "\n");
  216. }
  217. #endif
  218. alloc->max_size = MAX(alloc->max_size, offset + size);
  219. return offset;
  220. GGML_UNUSED(tensor);
  221. }
  222. // this is a very naive implementation, but for our case the number of free blocks should be very small
  223. static void ggml_dyn_tallocr_free_tensor(struct ggml_dyn_tallocr * alloc, size_t offset, size_t size, const struct ggml_tensor * tensor) {
  224. size = aligned_offset(NULL, size, alloc->alignment);
  225. AT_PRINTF("%s: freeing %s at %zu (%zu bytes) - n_free_blocks = %d\n", __func__, tensor->name, offset, size, alloc->n_free_blocks);
  226. #ifdef GGML_ALLOCATOR_DEBUG
  227. remove_allocated_tensor(alloc, offset, tensor);
  228. #endif
  229. // see if we can merge with an existing block
  230. for (int i = 0; i < alloc->n_free_blocks; i++) {
  231. struct free_block * block = &alloc->free_blocks[i];
  232. // check if ptr is at the end of the block
  233. if (block->offset + block->size == offset) {
  234. block->size += size;
  235. // check if we can merge with the next block
  236. if (i < alloc->n_free_blocks - 1 && block->offset + block->size == alloc->free_blocks[i+1].offset) {
  237. block->size += alloc->free_blocks[i+1].size;
  238. alloc->n_free_blocks--;
  239. for (int j = i+1; j < alloc->n_free_blocks; j++) {
  240. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  241. }
  242. }
  243. return;
  244. }
  245. // check if ptr is at the beginning of the block
  246. if (offset + size == block->offset) {
  247. block->offset = offset;
  248. block->size += size;
  249. // check if we can merge with the previous block
  250. if (i > 0 && alloc->free_blocks[i-1].offset + alloc->free_blocks[i-1].size == block->offset) {
  251. alloc->free_blocks[i-1].size += block->size;
  252. alloc->n_free_blocks--;
  253. for (int j = i; j < alloc->n_free_blocks; j++) {
  254. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  255. }
  256. }
  257. return;
  258. }
  259. }
  260. // otherwise, add a new block
  261. GGML_ASSERT(alloc->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks");
  262. // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster)
  263. int insert_pos = 0;
  264. while (insert_pos < alloc->n_free_blocks && alloc->free_blocks[insert_pos].offset < offset) {
  265. insert_pos++;
  266. }
  267. // shift all blocks from insert_pos onward to make room for the new block
  268. for (int i = alloc->n_free_blocks; i > insert_pos; i--) {
  269. alloc->free_blocks[i] = alloc->free_blocks[i-1];
  270. }
  271. // insert the new block
  272. alloc->free_blocks[insert_pos].offset = offset;
  273. alloc->free_blocks[insert_pos].size = size;
  274. alloc->n_free_blocks++;
  275. GGML_UNUSED(tensor);
  276. }
  277. static void ggml_dyn_tallocr_reset(struct ggml_dyn_tallocr * alloc) {
  278. alloc->n_free_blocks = 1;
  279. alloc->free_blocks[0].offset = 0;
  280. alloc->free_blocks[0].size = SIZE_MAX/2; // restrict maximum size of a measure allocator to half size_t max to avoid overflows
  281. alloc->max_size = 0;
  282. #ifdef GGML_ALLOCATOR_DEBUG
  283. for (int i = 0; i < 1024; i++) {
  284. alloc->allocated_tensors[i].tensor = NULL;
  285. }
  286. #endif
  287. }
  288. static struct ggml_dyn_tallocr * ggml_dyn_tallocr_new(size_t alignment) {
  289. struct ggml_dyn_tallocr * alloc = (struct ggml_dyn_tallocr *)malloc(sizeof(struct ggml_dyn_tallocr));
  290. *alloc = (struct ggml_dyn_tallocr) {
  291. /*.alignment = */ alignment,
  292. /*.n_free_blocks = */ 0,
  293. /*.free_blocks = */ {{0}},
  294. /*.max_size = */ 0,
  295. #ifdef GGML_ALLOCATOR_DEBUG
  296. /*.allocated_tensors = */ {{0}},
  297. #endif
  298. };
  299. ggml_dyn_tallocr_reset(alloc);
  300. return alloc;
  301. }
  302. static void ggml_dyn_tallocr_free(struct ggml_dyn_tallocr * alloc) {
  303. free(alloc);
  304. }
  305. static size_t ggml_dyn_tallocr_max_size(struct ggml_dyn_tallocr * alloc) {
  306. return alloc->max_size;
  307. }
  308. /////////////////////////////////////
  309. // graph allocator
  310. struct hash_node {
  311. int n_children;
  312. int n_views;
  313. int buffer_id;
  314. size_t offset; // offset within the buffer
  315. bool allocated;
  316. };
  317. struct tensor_alloc {
  318. int buffer_id;
  319. size_t offset;
  320. size_t size_max; // 0 = pre-allocated, unused, or view
  321. };
  322. struct leaf_alloc {
  323. int buffer_id;
  324. struct tensor_alloc leaf;
  325. };
  326. struct node_alloc {
  327. struct tensor_alloc dst;
  328. struct tensor_alloc src[GGML_MAX_SRC];
  329. };
  330. struct ggml_gallocr {
  331. ggml_backend_buffer_type_t * bufts; // [n_buffers]
  332. ggml_backend_buffer_t * buffers; // [n_buffers]
  333. struct ggml_dyn_tallocr ** buf_tallocs; // [n_buffers]
  334. int n_buffers;
  335. struct ggml_hash_set hash_set;
  336. struct hash_node * hash_values; // [hash_set.size]
  337. struct node_alloc * node_allocs; // [n_nodes]
  338. int n_nodes;
  339. struct leaf_alloc * leaf_allocs; // [n_leafs]
  340. int n_leafs;
  341. };
  342. ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs) {
  343. ggml_gallocr_t galloc = (ggml_gallocr_t)calloc(1, sizeof(struct ggml_gallocr));
  344. GGML_ASSERT(galloc != NULL);
  345. galloc->bufts = calloc(n_bufs, sizeof(ggml_backend_buffer_type_t));
  346. GGML_ASSERT(galloc->bufts != NULL);
  347. galloc->buffers = calloc(n_bufs, sizeof(ggml_backend_buffer_t));
  348. GGML_ASSERT(galloc->buffers != NULL);
  349. galloc->buf_tallocs = calloc(n_bufs, sizeof(struct ggml_dyn_tallocr *));
  350. GGML_ASSERT(galloc->buf_tallocs != NULL);
  351. for (int i = 0; i < n_bufs; i++) {
  352. galloc->bufts[i] = bufts[i];
  353. galloc->buffers[i] = NULL;
  354. // check if the same buffer type is used multiple times and reuse the same allocator
  355. for (int j = 0; j < i; j++) {
  356. if (bufts[i] == bufts[j]) {
  357. galloc->buf_tallocs[i] = galloc->buf_tallocs[j];
  358. break;
  359. }
  360. }
  361. if (galloc->buf_tallocs[i] == NULL) {
  362. size_t alignment = ggml_backend_buft_get_alignment(bufts[i]);
  363. galloc->buf_tallocs[i] = ggml_dyn_tallocr_new(alignment);
  364. }
  365. }
  366. galloc->n_buffers = n_bufs;
  367. return galloc;
  368. }
  369. ggml_gallocr_t ggml_gallocr_new(ggml_backend_buffer_type_t buft) {
  370. return ggml_gallocr_new_n(&buft, 1);
  371. }
  372. void ggml_gallocr_free(ggml_gallocr_t galloc) {
  373. if (galloc == NULL) {
  374. return;
  375. }
  376. for (int i = 0; i < galloc->n_buffers; i++) {
  377. if (galloc->buffers != NULL) {
  378. // skip if already freed
  379. bool freed = false;
  380. for (int j = 0; j < i; j++) {
  381. if (galloc->buffers[j] == galloc->buffers[i]) {
  382. freed = true;
  383. break;
  384. }
  385. }
  386. if (!freed) {
  387. ggml_backend_buffer_free(galloc->buffers[i]);
  388. }
  389. }
  390. if (galloc->buf_tallocs != NULL) {
  391. // skip if already freed
  392. bool freed = false;
  393. for (int j = 0; j < i; j++) {
  394. if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) {
  395. freed = true;
  396. break;
  397. }
  398. }
  399. if (!freed) {
  400. ggml_dyn_tallocr_free(galloc->buf_tallocs[i]);
  401. }
  402. }
  403. }
  404. ggml_hash_set_free(&galloc->hash_set);
  405. free(galloc->hash_values);
  406. free(galloc->bufts);
  407. free(galloc->buffers);
  408. free(galloc->buf_tallocs);
  409. free(galloc->node_allocs);
  410. free(galloc->leaf_allocs);
  411. free(galloc);
  412. }
  413. typedef struct ggml_gallocr * ggml_gallocr_t;
  414. static struct hash_node * ggml_gallocr_hash_get(ggml_gallocr_t galloc, struct ggml_tensor * t) {
  415. size_t i = ggml_hash_find_or_insert(&galloc->hash_set, t);
  416. return &galloc->hash_values[i];
  417. }
  418. static bool ggml_gallocr_is_own(ggml_gallocr_t galloc, struct ggml_tensor * t) {
  419. return ggml_gallocr_hash_get(galloc, t)->allocated;
  420. }
  421. static void ggml_gallocr_set_node_offset(ggml_gallocr_t galloc, struct ggml_tensor * node, int buffer_id, size_t offset) {
  422. struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
  423. hn->buffer_id = buffer_id;
  424. hn->offset = offset;
  425. hn->allocated = true;
  426. }
  427. static bool ggml_gallocr_is_allocated(ggml_gallocr_t galloc, struct ggml_tensor * t) {
  428. return t->data != NULL || ggml_gallocr_hash_get(galloc, t)->allocated;
  429. }
  430. static void ggml_gallocr_allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node, int buffer_id) {
  431. struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
  432. if (!ggml_gallocr_is_allocated(galloc, node) && !ggml_is_view(node)) {
  433. hn->allocated = true;
  434. assert(hn->offset == 0);
  435. // try to reuse a parent's buffer (inplace)
  436. if (ggml_op_can_inplace(node->op)) {
  437. for (int i = 0; i < GGML_MAX_SRC; i++) {
  438. struct ggml_tensor * parent = node->src[i];
  439. if (parent == NULL) {
  440. continue;
  441. }
  442. // if the node's data is external, then we cannot re-use it
  443. if (!ggml_gallocr_is_own(galloc, parent)) {
  444. AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data);
  445. continue;
  446. }
  447. // outputs cannot be reused
  448. if (parent->flags & GGML_TENSOR_FLAG_OUTPUT || (parent->view_src != NULL && parent->view_src->flags & GGML_TENSOR_FLAG_OUTPUT)) {
  449. AT_PRINTF("not reusing parent %s for %s as it is an output\n", parent->name, node->name);
  450. continue;
  451. }
  452. if (!ggml_are_same_layout(node, parent)) {
  453. AT_PRINTF("not reusing parent %s for %s as layouts are different\n", parent->name, node->name);
  454. continue;
  455. }
  456. struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent);
  457. if (p_hn->n_children == 1 && p_hn->n_views == 0) {
  458. if (ggml_is_view(parent)) {
  459. struct ggml_tensor * view_src = parent->view_src;
  460. struct hash_node * view_src_hn = ggml_gallocr_hash_get(galloc, view_src);
  461. if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) {
  462. AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name);
  463. assert(view_src_hn->offset == p_hn->offset);
  464. hn->buffer_id = p_hn->buffer_id;
  465. hn->offset = p_hn->offset;
  466. p_hn->allocated = false; // avoid freeing the parent
  467. view_src_hn->allocated = false;
  468. return;
  469. }
  470. } else {
  471. AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name);
  472. hn->buffer_id = p_hn->buffer_id;
  473. hn->offset = p_hn->offset;
  474. p_hn->allocated = false; // avoid freeing the parent
  475. return;
  476. }
  477. }
  478. }
  479. }
  480. // allocate tensor from the buffer
  481. struct ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id];
  482. ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id];
  483. size_t size = ggml_backend_buft_get_alloc_size(buft, node);
  484. size_t offset = ggml_dyn_tallocr_alloc(alloc, size, node);
  485. hn->buffer_id = buffer_id;
  486. hn->offset = offset;
  487. return;
  488. }
  489. }
  490. static void ggml_gallocr_free_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  491. // graph outputs are never freed
  492. if (node->flags & GGML_TENSOR_FLAG_OUTPUT) {
  493. AT_PRINTF("not freeing output %s\n", node->name);
  494. return;
  495. }
  496. struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
  497. size_t offset = hn->offset;
  498. int buffer_id = hn->buffer_id;
  499. struct ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id];
  500. ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id];
  501. size_t size = ggml_backend_buft_get_alloc_size(buft, node);
  502. ggml_dyn_tallocr_free_tensor(alloc, offset, size, node);
  503. hn->allocated = false;
  504. }
  505. static int get_node_buffer_id(const int * node_buffer_ids, int i) {
  506. return node_buffer_ids ? node_buffer_ids[i] : 0;
  507. }
  508. 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) {
  509. // clear hash tables
  510. ggml_hash_set_reset(&galloc->hash_set);
  511. memset(galloc->hash_values, 0, sizeof(struct hash_node) * galloc->hash_set.size);
  512. // allocate leafs
  513. // these may be tensors that the application is not using in the graph, but may still want to allocate for other purposes
  514. for (int i = 0; i < graph->n_leafs; i++) {
  515. struct ggml_tensor * leaf = graph->leafs[i];
  516. ggml_gallocr_allocate_node(galloc, leaf, get_node_buffer_id(leaf_buffer_ids, i));
  517. }
  518. // count number of children and views
  519. // allocate other graph inputs and leafs first to avoid overwriting them
  520. for (int i = 0; i < graph->n_nodes; i++) {
  521. struct ggml_tensor * node = graph->nodes[i];
  522. // TODO: better way to add external dependencies
  523. // GGML_OP_NONE does not appear normally in the graph nodes, but is used by ggml-backend to add dependencies to
  524. // control when some tensors are allocated and freed. in this case, the dependencies are in `src`, but the node
  525. // itself is never used and should not be considered a dependency
  526. if (ggml_is_view(node) && node->op != GGML_OP_NONE) {
  527. struct ggml_tensor * view_src = node->view_src;
  528. ggml_gallocr_hash_get(galloc, view_src)->n_views += 1;
  529. }
  530. if (node->flags & GGML_TENSOR_FLAG_INPUT) {
  531. ggml_gallocr_allocate_node(galloc, graph->nodes[i], get_node_buffer_id(node_buffer_ids, i));
  532. }
  533. for (int j = 0; j < GGML_MAX_SRC; j++) {
  534. struct ggml_tensor * src = node->src[j];
  535. if (src == NULL) {
  536. continue;
  537. }
  538. ggml_gallocr_hash_get(galloc, src)->n_children += 1;
  539. // allocate explicit inputs
  540. if (src->flags & GGML_TENSOR_FLAG_INPUT) {
  541. ggml_gallocr_allocate_node(galloc, src, get_node_buffer_id(node_buffer_ids, i));
  542. }
  543. }
  544. }
  545. // allocate tensors
  546. for (int i = 0; i < graph->n_nodes; i++) {
  547. struct ggml_tensor * node = graph->nodes[i];
  548. int buffer_id = get_node_buffer_id(node_buffer_ids, i);
  549. // allocate parents (only leafs need to be allocated at this point)
  550. for (int j = 0; j < GGML_MAX_SRC; j++) {
  551. struct ggml_tensor * parent = node->src[j];
  552. if (parent == NULL) {
  553. continue;
  554. }
  555. ggml_gallocr_allocate_node(galloc, parent, buffer_id);
  556. }
  557. // allocate node
  558. ggml_gallocr_allocate_node(galloc, node, buffer_id);
  559. AT_PRINTF("exec: %s (%s) <= ", ggml_op_desc(node), node->name);
  560. for (int j = 0; j < GGML_MAX_SRC; j++) {
  561. struct ggml_tensor * parent = node->src[j];
  562. if (parent == NULL) {
  563. continue;
  564. }
  565. AT_PRINTF("%s", parent->name);
  566. if (j < GGML_MAX_SRC - 1 && node->src[j + 1] != NULL) {
  567. AT_PRINTF(", ");
  568. }
  569. }
  570. AT_PRINTF("\n");
  571. // update parents
  572. for (int j = 0; j < GGML_MAX_SRC; j++) {
  573. struct ggml_tensor * parent = node->src[j];
  574. if (parent == NULL) {
  575. continue;
  576. }
  577. struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent);
  578. p_hn->n_children -= 1;
  579. AT_PRINTF("parent %s: %d children, %d views, allocated: %d\n",
  580. parent->name, p_hn->n_children, p_hn->n_views, p_hn->allocated);
  581. if (p_hn->n_children == 0 && p_hn->n_views == 0) {
  582. if (ggml_is_view(parent)) {
  583. struct ggml_tensor * view_src = parent->view_src;
  584. struct hash_node * view_src_hn = ggml_gallocr_hash_get(galloc, view_src);
  585. view_src_hn->n_views -= 1;
  586. AT_PRINTF("view_src %s: %d children, %d views\n",
  587. view_src->name, view_src_hn->n_children, view_src_hn->n_views);
  588. if (view_src_hn->n_views == 0 && view_src_hn->n_children == 0 && view_src_hn->allocated) {
  589. ggml_gallocr_free_node(galloc, view_src);
  590. }
  591. }
  592. else if (p_hn->allocated) {
  593. ggml_gallocr_free_node(galloc, parent);
  594. }
  595. }
  596. AT_PRINTF("\n");
  597. }
  598. }
  599. }
  600. bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
  601. size_t min_hash_size = graph->n_nodes + graph->n_leafs;
  602. // add 25% margin to avoid hash collisions
  603. min_hash_size += min_hash_size / 4;
  604. // initialize hash table
  605. if (galloc->hash_set.size < min_hash_size) {
  606. ggml_hash_set_free(&galloc->hash_set);
  607. galloc->hash_set = ggml_hash_set_new(min_hash_size);
  608. GGML_ASSERT(galloc->hash_set.keys != NULL);
  609. free(galloc->hash_values);
  610. galloc->hash_values = malloc(sizeof(struct hash_node) * galloc->hash_set.size);
  611. GGML_ASSERT(galloc->hash_values != NULL);
  612. }
  613. // reset allocators
  614. for (int i = 0; i < galloc->n_buffers; i++) {
  615. ggml_dyn_tallocr_reset(galloc->buf_tallocs[i]);
  616. }
  617. // allocate in hash table
  618. ggml_gallocr_alloc_graph_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids);
  619. // set the node_allocs from the hash table
  620. if (galloc->n_nodes < graph->n_nodes) {
  621. free(galloc->node_allocs);
  622. galloc->node_allocs = calloc(graph->n_nodes, sizeof(struct node_alloc));
  623. GGML_ASSERT(galloc->node_allocs != NULL);
  624. }
  625. galloc->n_nodes = graph->n_nodes;
  626. for (int i = 0; i < graph->n_nodes; i++) {
  627. struct ggml_tensor * node = graph->nodes[i];
  628. struct node_alloc * node_alloc = &galloc->node_allocs[i];
  629. if (node->view_src || node->data) {
  630. node_alloc->dst.buffer_id = -1;
  631. node_alloc->dst.offset = SIZE_MAX;
  632. node_alloc->dst.size_max = 0;
  633. } else {
  634. struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
  635. node_alloc->dst.buffer_id = hn->buffer_id;
  636. node_alloc->dst.offset = hn->offset;
  637. node_alloc->dst.size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node);
  638. }
  639. for (int j = 0; j < GGML_MAX_SRC; j++) {
  640. struct ggml_tensor * src = node->src[j];
  641. if (!src || src->view_src || src->data) {
  642. node_alloc->src[j].buffer_id = -1;
  643. node_alloc->src[j].offset = SIZE_MAX;
  644. node_alloc->src[j].size_max = 0;
  645. } else {
  646. struct hash_node * hn = ggml_gallocr_hash_get(galloc, src);
  647. node_alloc->src[j].buffer_id = hn->buffer_id;
  648. node_alloc->src[j].offset = hn->offset;
  649. node_alloc->src[j].size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], src);
  650. }
  651. }
  652. }
  653. if (galloc->n_leafs < graph->n_leafs) {
  654. free(galloc->leaf_allocs);
  655. galloc->leaf_allocs = calloc(graph->n_leafs, sizeof(galloc->leaf_allocs[0]));
  656. GGML_ASSERT(galloc->leaf_allocs != NULL);
  657. }
  658. galloc->n_leafs = graph->n_leafs;
  659. for (int i = 0; i < graph->n_leafs; i++) {
  660. struct ggml_tensor * leaf = graph->leafs[i];
  661. struct hash_node * hn = ggml_gallocr_hash_get(galloc, leaf);
  662. galloc->leaf_allocs[i].buffer_id = hn->buffer_id;
  663. if (leaf->view_src || leaf->data) {
  664. galloc->leaf_allocs[i].leaf.buffer_id = -1;
  665. galloc->leaf_allocs[i].leaf.offset = SIZE_MAX;
  666. galloc->leaf_allocs[i].leaf.size_max = 0;
  667. } else {
  668. galloc->leaf_allocs[i].leaf.buffer_id = hn->buffer_id;
  669. galloc->leaf_allocs[i].leaf.offset = hn->offset;
  670. galloc->leaf_allocs[i].leaf.size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], leaf);
  671. }
  672. }
  673. // reallocate buffers if needed
  674. for (int i = 0; i < galloc->n_buffers; i++) {
  675. // if the buffer type is used multiple times, we reuse the same buffer
  676. for (int j = 0; j < i; j++) {
  677. if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) {
  678. galloc->buffers[i] = galloc->buffers[j];
  679. break;
  680. }
  681. }
  682. size_t cur_size = galloc->buffers[i] ? ggml_backend_buffer_get_size(galloc->buffers[i]) : 0;
  683. size_t new_size = ggml_dyn_tallocr_max_size(galloc->buf_tallocs[i]);
  684. // even if there are no tensors allocated in this buffer, we still need to allocate it to initialize views
  685. if (new_size > cur_size || galloc->buffers[i] == NULL) {
  686. #ifndef NDEBUG
  687. fprintf(stderr, "%s: reallocating %s buffer from size %.02f MiB to %.02f MiB\n", __func__, ggml_backend_buft_name(galloc->bufts[i]), cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
  688. #endif
  689. ggml_backend_buffer_free(galloc->buffers[i]);
  690. galloc->buffers[i] = ggml_backend_buft_alloc_buffer(galloc->bufts[i], new_size);
  691. if (galloc->buffers[i] == NULL) {
  692. fprintf(stderr, "%s: failed to allocate %s buffer of size %zu\n", __func__, ggml_backend_buft_name(galloc->bufts[i]), new_size);
  693. return false;
  694. }
  695. ggml_backend_buffer_set_usage(galloc->buffers[i], GGML_BACKEND_BUFFER_USAGE_COMPUTE);
  696. }
  697. }
  698. return true;
  699. }
  700. bool ggml_gallocr_reserve(ggml_gallocr_t galloc, struct ggml_cgraph *graph) {
  701. return ggml_gallocr_reserve_n(galloc, graph, NULL, NULL);
  702. }
  703. static void ggml_gallocr_init_tensor(ggml_gallocr_t galloc, struct ggml_tensor * tensor, struct tensor_alloc * tensor_alloc) {
  704. int buffer_id = tensor_alloc->buffer_id;
  705. assert(tensor->data || tensor->view_src || ggml_backend_buffer_get_alloc_size(galloc->buffers[buffer_id], tensor) <= tensor_alloc->size_max);
  706. if (tensor->view_src != NULL) {
  707. if (tensor->buffer == NULL) {
  708. assert(tensor_alloc->offset == SIZE_MAX);
  709. if (tensor->view_src->buffer == NULL) {
  710. // this tensor was allocated without ggml-backend
  711. return;
  712. }
  713. ggml_backend_view_init(tensor);
  714. }
  715. } else {
  716. if (tensor->data == NULL) {
  717. assert(tensor_alloc->offset != SIZE_MAX);
  718. assert(ggml_backend_buffer_get_alloc_size(galloc->buffers[buffer_id], tensor) <= tensor_alloc->size_max);
  719. void * base = ggml_backend_buffer_get_base(galloc->buffers[buffer_id]);
  720. void * addr = (char *)base + tensor_alloc->offset;
  721. ggml_backend_tensor_alloc(galloc->buffers[buffer_id], tensor, addr);
  722. } else {
  723. if (tensor->buffer == NULL) {
  724. // this tensor was allocated without ggml-backend
  725. return;
  726. }
  727. }
  728. }
  729. }
  730. static bool ggml_gallocr_node_needs_realloc(ggml_gallocr_t galloc, struct ggml_tensor * node, struct tensor_alloc * talloc) {
  731. size_t node_size = (node->data || node->view_src) ? 0 : ggml_backend_buft_get_alloc_size(galloc->bufts[talloc->buffer_id], node);
  732. return talloc->size_max >= node_size;
  733. }
  734. static bool ggml_gallocr_needs_realloc(ggml_gallocr_t galloc, struct ggml_cgraph * graph) {
  735. if (galloc->n_nodes != graph->n_nodes) {
  736. #ifndef NDEBUG
  737. fprintf(stderr, "%s: graph has different number of nodes\n", __func__);
  738. #endif
  739. return true;
  740. }
  741. if (galloc->n_leafs != graph->n_leafs) {
  742. #ifndef NDEBUG
  743. fprintf(stderr, "%s: graph has different number of leafs\n", __func__);
  744. #endif
  745. return true;
  746. }
  747. for (int i = 0; i < graph->n_nodes; i++) {
  748. struct ggml_tensor * node = graph->nodes[i];
  749. struct node_alloc * node_alloc = &galloc->node_allocs[i];
  750. if (!ggml_gallocr_node_needs_realloc(galloc, node, &node_alloc->dst)) {
  751. #ifndef NDEBUG
  752. fprintf(stderr, "%s: node %s is not valid\n", __func__, node->name);
  753. #endif
  754. return true;
  755. }
  756. for (int j = 0; j < GGML_MAX_SRC; j++) {
  757. struct ggml_tensor * src = node->src[j];
  758. if (src == NULL) {
  759. continue;
  760. }
  761. if (!ggml_gallocr_node_needs_realloc(galloc, src, &node_alloc->src[j])) {
  762. #ifndef NDEBUG
  763. fprintf(stderr, "%s: src %d (%s) of node %s is not valid\n", __func__, j, src->name, node->name);
  764. #endif
  765. return true;
  766. }
  767. }
  768. }
  769. return false;
  770. }
  771. bool ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, struct ggml_cgraph * graph) {
  772. if (ggml_gallocr_needs_realloc(galloc, graph)) {
  773. if (galloc->n_buffers == 1) {
  774. #ifndef NDEBUG
  775. fprintf(stderr, "%s: reallocating buffers automatically\n", __func__);
  776. #endif
  777. if (!ggml_gallocr_reserve(galloc, graph)) {
  778. return false;
  779. }
  780. } else {
  781. #ifndef NDEBUG
  782. fprintf(stderr, "%s: cannot reallocate multi buffer graph automatically, call reserve\n", __func__);
  783. #endif
  784. return false;
  785. }
  786. }
  787. // reset buffers
  788. for (int i = 0; i < galloc->n_buffers; i++) {
  789. if (galloc->buffers[i] != NULL) {
  790. ggml_backend_buffer_reset(galloc->buffers[i]);
  791. }
  792. }
  793. // allocate the graph tensors from the previous assignments
  794. // leafs
  795. for (int i = 0; i < graph->n_leafs; i++) {
  796. struct ggml_tensor * leaf = graph->leafs[i];
  797. struct leaf_alloc * leaf_alloc = &galloc->leaf_allocs[i];
  798. ggml_gallocr_init_tensor(galloc, leaf, &leaf_alloc->leaf);
  799. }
  800. // nodes
  801. for (int i = 0; i < graph->n_nodes; i++) {
  802. struct ggml_tensor * node = graph->nodes[i];
  803. struct node_alloc * node_alloc = &galloc->node_allocs[i];
  804. for (int j = 0; j < GGML_MAX_SRC; j++) {
  805. struct ggml_tensor * src = node->src[j];
  806. if (src == NULL) {
  807. continue;
  808. }
  809. ggml_gallocr_init_tensor(galloc, src, &node_alloc->src[j]);
  810. }
  811. ggml_gallocr_init_tensor(galloc, node, &node_alloc->dst);
  812. }
  813. return true;
  814. }
  815. size_t ggml_gallocr_get_buffer_size(ggml_gallocr_t galloc, int buffer_id) {
  816. GGML_ASSERT(buffer_id >= 0 && buffer_id < galloc->n_buffers);
  817. if (galloc->buffers[buffer_id] == NULL) {
  818. return 0;
  819. }
  820. for (int i = 0; i < buffer_id; i++) {
  821. if (galloc->buffers[i] == galloc->buffers[buffer_id]) {
  822. // this buffer is the same as a previous one due to the same buffer type being used multiple times
  823. // only return the buffer size the first time it appears to avoid double counting
  824. return 0;
  825. }
  826. }
  827. return ggml_backend_buffer_get_size(galloc->buffers[buffer_id]);
  828. }
  829. // utils
  830. static bool alloc_tensor_range(struct ggml_context * ctx,
  831. struct ggml_tensor * first, struct ggml_tensor * last,
  832. ggml_backend_buffer_type_t buft, size_t size,
  833. ggml_backend_buffer_t ** buffers, size_t * n_buffers) {
  834. ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buft, size);
  835. if (buffer == NULL) {
  836. #ifndef NDEBUG
  837. fprintf(stderr, "%s: failed to allocate %s buffer of size %zu\n", __func__, ggml_backend_buft_name(buft), size);
  838. #endif
  839. for (size_t i = 0; i < *n_buffers; i++) {
  840. ggml_backend_buffer_free((*buffers)[i]);
  841. }
  842. free(*buffers);
  843. return false;
  844. }
  845. struct ggml_tallocr tallocr = ggml_tallocr_new(buffer);
  846. for (struct ggml_tensor * t = first; t != last; t = ggml_get_next_tensor(ctx, t)) {
  847. if (t->data == NULL) {
  848. if (t->view_src == NULL) {
  849. ggml_tallocr_alloc(&tallocr, t);
  850. } else if (t->buffer == NULL) {
  851. ggml_backend_view_init(t);
  852. }
  853. } else {
  854. if (t->view_src != NULL && t->buffer == NULL) {
  855. // view of a pre-allocated tensor
  856. ggml_backend_view_init(t);
  857. }
  858. }
  859. }
  860. *buffers = realloc(*buffers, sizeof(ggml_backend_buffer_t) * (*n_buffers + 1));
  861. (*buffers)[(*n_buffers)++] = buffer;
  862. return true;
  863. }
  864. ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) {
  865. GGML_ASSERT(ggml_get_no_alloc(ctx) == true);
  866. size_t alignment = ggml_backend_buft_get_alignment(buft);
  867. size_t max_size = ggml_backend_buft_get_max_size(buft);
  868. ggml_backend_buffer_t * buffers = NULL;
  869. size_t n_buffers = 0;
  870. size_t cur_buf_size = 0;
  871. struct ggml_tensor * first = ggml_get_first_tensor(ctx);
  872. for (struct ggml_tensor * t = first; t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  873. size_t this_size = 0;
  874. if (t->data == NULL && t->view_src == NULL) {
  875. this_size = GGML_PAD(ggml_backend_buft_get_alloc_size(buft, t), alignment);
  876. }
  877. if (this_size > max_size) {
  878. fprintf(stderr, "%s: tensor %s is too large to fit in a %s buffer (tensor size: %zu, max buffer size: %zu)\n",
  879. __func__, t->name,
  880. ggml_backend_buft_name(buft),
  881. this_size, max_size);
  882. for (size_t i = 0; i < n_buffers; i++) {
  883. ggml_backend_buffer_free(buffers[i]);
  884. }
  885. free(buffers);
  886. return NULL;
  887. }
  888. if ((cur_buf_size + this_size) > max_size) {
  889. // allocate tensors in the current buffer
  890. if (!alloc_tensor_range(ctx, first, t, buft, cur_buf_size, &buffers, &n_buffers)) {
  891. return NULL;
  892. }
  893. first = t;
  894. cur_buf_size = this_size;
  895. } else {
  896. cur_buf_size += this_size;
  897. }
  898. }
  899. // allocate remaining tensors
  900. if (cur_buf_size > 0) {
  901. if (!alloc_tensor_range(ctx, first, NULL, buft, cur_buf_size, &buffers, &n_buffers)) {
  902. return NULL;
  903. }
  904. }
  905. if (n_buffers == 0) {
  906. #ifndef NDEBUG
  907. fprintf(stderr, "%s: all tensors in the context are already allocated\n", __func__);
  908. #endif
  909. return NULL;
  910. }
  911. ggml_backend_buffer_t buffer;
  912. if (n_buffers == 1) {
  913. buffer = buffers[0];
  914. } else {
  915. buffer = ggml_backend_multi_buffer_alloc_buffer(buffers, n_buffers);
  916. }
  917. free(buffers);
  918. return buffer;
  919. }
  920. ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, ggml_backend_t backend) {
  921. return ggml_backend_alloc_ctx_tensors_from_buft(ctx, ggml_backend_get_default_buffer_type(backend));
  922. }