ggml-alloc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /**
  2. * llama.cpp - git 3ebb00935f3f0522b75df49c2769ab1774b91380
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023 Georgi Gerganov
  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.h"
  28. #include <assert.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #define UNUSED(x) (void)(x)
  34. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  35. //#define GGML_ALLOCATOR_DEBUG
  36. //#define AT_PRINTF printf
  37. #define AT_PRINTF(...) ((void)0)
  38. struct hash_node {
  39. struct ggml_tensor * t;
  40. int n_children;
  41. int n_views;
  42. };
  43. static size_t hash(void * p) {
  44. return (size_t)p % GGML_GRAPH_HASHTABLE_SIZE;
  45. }
  46. static struct hash_node * hash_get(struct hash_node hash_table[], struct ggml_tensor * t) {
  47. size_t h = hash(t);
  48. // linear probing
  49. size_t i = h;
  50. while (hash_table[i].t != NULL) {
  51. if (hash_table[i].t == t) {
  52. return &hash_table[i];
  53. }
  54. i = (i + 1) % GGML_GRAPH_HASHTABLE_SIZE;
  55. if (i == h) {
  56. // hash table is full
  57. GGML_ASSERT(false);
  58. }
  59. }
  60. hash_table[i].t = t;
  61. return &hash_table[i];
  62. }
  63. // TODO: GGML_PAD ?
  64. static size_t aligned_offset(const void * buffer, size_t offset, size_t alignment) {
  65. assert(alignment && !(alignment & (alignment - 1))); // power of 2
  66. size_t align = (alignment - (((uintptr_t)buffer + offset) % alignment)) % alignment;
  67. return offset + align;
  68. }
  69. struct free_block {
  70. void * addr;
  71. size_t size;
  72. };
  73. #define MAX_FREE_BLOCKS 128
  74. struct ggml_allocr {
  75. void * data;
  76. size_t size;
  77. size_t alignment;
  78. int n_free_blocks;
  79. struct free_block free_blocks[MAX_FREE_BLOCKS];
  80. struct hash_node hash_table[GGML_GRAPH_HASHTABLE_SIZE];
  81. size_t max_size;
  82. bool measure;
  83. #ifdef GGML_ALLOCATOR_DEBUG
  84. struct ggml_tensor * allocated_tensors[1024];
  85. #endif
  86. };
  87. #ifdef GGML_ALLOCATOR_DEBUG
  88. static void add_allocated_tensor(struct ggml_allocator * alloc, struct ggml_tensor * tensor) {
  89. for (int i = 0; i < 1024; i++) {
  90. if (alloc->allocated_tensors[i] == NULL) {
  91. alloc->allocated_tensors[i] = tensor;
  92. return;
  93. }
  94. }
  95. GGML_ASSERT(!"out of allocated_tensors");
  96. }
  97. static void remove_allocated_tensor(struct ggml_allocator * alloc, struct ggml_tensor * tensor) {
  98. for (int i = 0; i < 1024; i++) {
  99. if (alloc->allocated_tensors[i] == tensor ||
  100. (alloc->allocated_tensors[i] != NULL && alloc->allocated_tensors[i]->data == tensor->data)) {
  101. alloc->allocated_tensors[i] = NULL;
  102. return;
  103. }
  104. }
  105. printf("tried to free tensor %s not found\n", tensor->name);
  106. GGML_ASSERT(!"tensor not found");
  107. }
  108. #endif
  109. static size_t ggml_allocator_get_alloc_size(struct ggml_allocr * alloc, struct ggml_tensor * tensor) {
  110. return ggml_nbytes(tensor);
  111. UNUSED(alloc);
  112. }
  113. void ggml_allocr_alloc(struct ggml_allocr * alloc, struct ggml_tensor * tensor) {
  114. size_t size = ggml_allocator_get_alloc_size(alloc, tensor);
  115. size = aligned_offset(NULL, size, alloc->alignment);
  116. AT_PRINTF("%s: allocating %s (%zu bytes) - ", __func__, tensor->name, size);
  117. size_t max_avail = 0;
  118. // find the best fitting free block
  119. int best_fit_block = -1;
  120. size_t best_fit_size = SIZE_MAX;
  121. for (int i = 0; i < alloc->n_free_blocks; i++) {
  122. struct free_block * block = &alloc->free_blocks[i];
  123. max_avail = MAX(max_avail, block->size);
  124. if (block->size >= size && block->size <= best_fit_size) {
  125. best_fit_block = i;
  126. best_fit_size = block->size;
  127. }
  128. }
  129. AT_PRINTF("block %d\n", best_fit_block);
  130. if (best_fit_block == -1) {
  131. fprintf(stderr, "%s: not enough space in the buffer (needed %zu, largest block available %zu)\n",
  132. __func__, size, max_avail);
  133. GGML_ASSERT(!"not enough space in the buffer");
  134. return;
  135. }
  136. struct free_block * block = &alloc->free_blocks[best_fit_block];
  137. void * addr = block->addr;
  138. block->addr = (char*)block->addr + size;
  139. block->size -= size;
  140. if (block->size == 0) {
  141. // remove block if empty
  142. alloc->n_free_blocks--;
  143. for (int j = best_fit_block; j < alloc->n_free_blocks; j++) {
  144. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  145. }
  146. }
  147. tensor->data = addr;
  148. #ifdef GGML_ALLOCATOR_DEBUG
  149. add_allocated_tensor(alloc, tensor);
  150. size_t cur_max = (char*)addr - (char*)alloc->data + size;
  151. if (cur_max > alloc->max_size) {
  152. printf("max_size = %.2f MB: tensors: ", cur_max / 1024.0 / 1024.0);
  153. for (int i = 0; i < 1024; i++) {
  154. if (alloc->allocated_tensors[i]) {
  155. printf("%s (%.2f MB) ", alloc->allocated_tensors[i]->name, ggml_nbytes(alloc->allocated_tensors[i]) / 1024.0 / 1024.0);
  156. }
  157. }
  158. printf("\n");
  159. }
  160. #endif
  161. alloc->max_size = MAX(alloc->max_size, (char*)addr - (char*)alloc->data + size);
  162. }
  163. // this is a very naive implementation, but for our case the number of free blocks should be very small
  164. static void ggml_allocator_free_tensor(struct ggml_allocr * alloc, struct ggml_tensor * tensor) {
  165. void * ptr = tensor->data;
  166. if (ptr < alloc->data || (char*)ptr >= (char*)alloc->data + alloc->max_size) {
  167. // the tensor was not allocated in this buffer
  168. // this can happen because the graph allocator will try to free weights and other tensors from different buffers
  169. // the easiest way to deal with this is just to ignore it
  170. return;
  171. }
  172. size_t size = ggml_allocator_get_alloc_size(alloc, tensor);
  173. size = aligned_offset(NULL, size, alloc->alignment);
  174. AT_PRINTF("%s: freeing %s (%zu bytes) - n_free_blocks = %d\n", __func__, tensor->name, size, alloc->n_free_blocks);
  175. #ifdef GGML_ALLOCATOR_DEBUG
  176. remove_allocated_tensor(alloc, tensor);
  177. #endif
  178. // see if we can merge with an existing block
  179. for (int i = 0; i < alloc->n_free_blocks; i++) {
  180. struct free_block * block = &alloc->free_blocks[i];
  181. // check if ptr is at the end of the block
  182. if ((char*)block->addr + block->size == ptr) {
  183. block->size += size;
  184. // check if we can merge with the next block
  185. if (i < alloc->n_free_blocks - 1 && (char*)block->addr + block->size == alloc->free_blocks[i+1].addr) {
  186. block->size += alloc->free_blocks[i+1].size;
  187. alloc->n_free_blocks--;
  188. for (int j = i+1; j < alloc->n_free_blocks; j++) {
  189. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  190. }
  191. }
  192. return;
  193. }
  194. // check if ptr is at the beginning of the block
  195. if ((char*)ptr + size == block->addr) {
  196. block->addr = ptr;
  197. block->size += size;
  198. // check if we can merge with the previous block
  199. if (i > 0 && (char*)alloc->free_blocks[i-1].addr + alloc->free_blocks[i-1].size == block->addr) {
  200. alloc->free_blocks[i-1].size += block->size;
  201. alloc->n_free_blocks--;
  202. for (int j = i; j < alloc->n_free_blocks; j++) {
  203. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  204. }
  205. }
  206. return;
  207. }
  208. }
  209. // otherwise, add a new block
  210. GGML_ASSERT(alloc->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks");
  211. // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster)
  212. int insert_pos = 0;
  213. while (insert_pos < alloc->n_free_blocks && alloc->free_blocks[insert_pos].addr < ptr) {
  214. insert_pos++;
  215. }
  216. // shift all blocks from insert_pos onward to make room for the new block
  217. for (int i = alloc->n_free_blocks; i > insert_pos; i--) {
  218. alloc->free_blocks[i] = alloc->free_blocks[i-1];
  219. }
  220. // insert the new block
  221. alloc->free_blocks[insert_pos].addr = ptr;
  222. alloc->free_blocks[insert_pos].size = size;
  223. alloc->n_free_blocks++;
  224. }
  225. void ggml_allocr_reset(struct ggml_allocr * alloc) {
  226. alloc->n_free_blocks = 1;
  227. size_t align_offset = aligned_offset(alloc->data, 0, alloc->alignment);
  228. alloc->free_blocks[0].addr = (char *)alloc->data + align_offset;
  229. alloc->free_blocks[0].size = alloc->size - align_offset;
  230. }
  231. struct ggml_allocr * ggml_allocr_new(void * data, size_t size, size_t alignment) {
  232. struct ggml_allocr * alloc = (struct ggml_allocr *)malloc(sizeof(struct ggml_allocr) /* + n_free_blocks * sizeof(struct free_block) */);
  233. *alloc = (struct ggml_allocr){
  234. /*.data = */ data,
  235. /*.size = */ size,
  236. /*.alignment = */ alignment,
  237. /*.n_free_blocks = */ 0,
  238. /*.free_blocks = */ {{0}},
  239. /*.hash_table = */ {{0}},
  240. /*.max_size = */ 0,
  241. /*.measure = */ false,
  242. #ifdef GGML_ALLOCATOR_DEBUG
  243. /*.allocated_tensors = */ = {0},
  244. #endif
  245. };
  246. ggml_allocr_reset(alloc);
  247. return alloc;
  248. }
  249. // address and size of the buffer when measuring
  250. // it needs to be large enough to fit all the tensors, but it cannot overlap with other existing buffers
  251. static void * const MEASURE_BASE_ADDR = (void *) 0x1000;
  252. static const size_t MEASURE_MAX_SIZE = 1ULL<<40; // 1 TB
  253. struct ggml_allocr * ggml_allocr_new_measure(size_t alignment) {
  254. struct ggml_allocr * alloc = (struct ggml_allocr *)malloc(sizeof(struct ggml_allocr) /* + n_free_blocks * sizeof(struct free_block) */);
  255. *alloc = (struct ggml_allocr){
  256. /*.data = */ MEASURE_BASE_ADDR,
  257. /*.size = */ MEASURE_MAX_SIZE,
  258. /*.alignment = */ alignment,
  259. /*.n_free_blocks = */ 0,
  260. /*.free_blocks = */ {{0}},
  261. /*.hash_table = */ {{0}},
  262. /*.max_size = */ 0,
  263. /*.measure = */ true,
  264. #ifdef GGML_ALLOCATOR_DEBUG
  265. /*.allocated_tensors = */ = {0},
  266. #endif
  267. };
  268. ggml_allocr_reset(alloc);
  269. return alloc;
  270. }
  271. void ggml_allocr_free(struct ggml_allocr * alloc) {
  272. free(alloc);
  273. }
  274. bool ggml_allocr_is_measure(struct ggml_allocr * alloc) {
  275. return alloc->measure;
  276. }
  277. //////////// compute graph allocator
  278. static bool ggml_is_view(struct ggml_tensor * t) {
  279. return t->op == GGML_OP_RESHAPE || t->op == GGML_OP_VIEW || t->op == GGML_OP_TRANSPOSE ||
  280. t->op == GGML_OP_PERMUTE || t->op == GGML_OP_CPY;
  281. }
  282. static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {
  283. if (a->type != b->type) {
  284. return false;
  285. }
  286. for (int i = 0; i < GGML_MAX_DIMS; i++) {
  287. if (a->ne[i] != b->ne[i]) {
  288. return false;
  289. }
  290. if (a->nb[i] != b->nb[i]) {
  291. return false;
  292. }
  293. }
  294. return true;
  295. }
  296. static struct ggml_tensor * get_view_parent(struct ggml_tensor * t) {
  297. switch (t->op) {
  298. case GGML_OP_PERMUTE:
  299. case GGML_OP_RESHAPE:
  300. case GGML_OP_TRANSPOSE:
  301. case GGML_OP_VIEW:
  302. return t->src[0];
  303. case GGML_OP_CPY:
  304. return t->src[1];
  305. default:
  306. return NULL;
  307. }
  308. }
  309. static struct ggml_tensor * get_view_source(struct ggml_tensor * t) {
  310. struct ggml_tensor * parent = t;
  311. do {
  312. parent = get_view_parent(parent);
  313. } while (ggml_is_view(parent));
  314. return parent;
  315. }
  316. static bool ggml_op_can_inplace(enum ggml_op op) {
  317. switch (op) {
  318. case GGML_OP_SCALE:
  319. case GGML_OP_DIAG_MASK_ZERO:
  320. case GGML_OP_DIAG_MASK_INF:
  321. case GGML_OP_ADD:
  322. case GGML_OP_ADD1:
  323. case GGML_OP_ACC:
  324. case GGML_OP_SUB:
  325. case GGML_OP_MUL:
  326. case GGML_OP_DIV:
  327. case GGML_OP_SQR:
  328. case GGML_OP_SQRT:
  329. case GGML_OP_LOG:
  330. case GGML_OP_UNARY:
  331. case GGML_OP_ROPE:
  332. case GGML_OP_RMS_NORM:
  333. case GGML_OP_SET:
  334. case GGML_OP_SOFT_MAX:
  335. case GGML_OP_CONT:
  336. return true;
  337. default:
  338. return false;
  339. }
  340. }
  341. static void allocate_node(struct ggml_allocr * alloc, struct ggml_tensor * node) {
  342. struct hash_node * ht = alloc->hash_table;
  343. if (node->data == NULL) {
  344. if (ggml_is_view(node)) {
  345. size_t offset;
  346. switch(node->op) {
  347. case GGML_OP_VIEW:
  348. memcpy(&offset, node->op_params, sizeof(size_t));
  349. node->data = (char *) node->src[0]->data + offset;
  350. break;
  351. case GGML_OP_PERMUTE:
  352. case GGML_OP_RESHAPE:
  353. case GGML_OP_TRANSPOSE:
  354. node->data = node->src[0]->data;
  355. break;
  356. case GGML_OP_CPY:
  357. node->data = node->src[1]->data;
  358. break;
  359. default:
  360. GGML_ASSERT(!"unknown view op");
  361. break;
  362. }
  363. } else {
  364. // see if we can reuse a parent's buffer (inplace)
  365. if (ggml_op_can_inplace(node->op)) {
  366. for (int i = 0; i < GGML_MAX_SRC; i++) {
  367. struct ggml_tensor * parent = node->src[i];
  368. if (parent == NULL) {
  369. break;
  370. }
  371. // if the node's data is external, then we cannot re-use it
  372. if ((char *) parent->data < (char *) alloc->data ||
  373. (char *) parent->data >= ((char *) alloc->data + alloc->size)) {
  374. AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data);
  375. continue;
  376. }
  377. struct hash_node * p_hn = hash_get(ht, parent);
  378. if (parent->data != NULL && p_hn->n_children == 1 && p_hn->n_views == 0 && ggml_are_same_layout(node, parent)) {
  379. if (ggml_is_view(parent)) {
  380. struct ggml_tensor * view_src = get_view_source(parent);
  381. struct hash_node * view_src_hn = hash_get(ht, view_src);
  382. if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) {
  383. // TODO: the offset of the view parent must be kept to ensure that the op doesn't overwrite
  384. // the parent's data that it will need later (same layout requirement). the problem is that then
  385. // we cannot free the tensor because the original address of the allocation is lost.
  386. // adding a view_src pointer to the tensor would solve this and simplify the code dealing with views
  387. // for now, we only reuse the parent's data if the offset is zero (view_src->data == parent->data)
  388. AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name);
  389. node->data = parent->data;
  390. return;
  391. }
  392. }
  393. else {
  394. AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name);
  395. node->data = parent->data;
  396. }
  397. return;
  398. }
  399. }
  400. }
  401. ggml_allocr_alloc(alloc, node);
  402. }
  403. }
  404. }
  405. static size_t ggml_allocator_alloc_graph_tensors_n(
  406. struct ggml_allocr * alloc,
  407. struct ggml_cgraph ** graphs, int n_graphs,
  408. struct ggml_tensor *** inputs, struct ggml_tensor *** outputs) {
  409. // reset hash table
  410. struct hash_node * ht = alloc->hash_table;
  411. memset(ht, 0, sizeof(struct hash_node) * GGML_GRAPH_HASHTABLE_SIZE);
  412. // count number of children and views
  413. for (int g = 0; g < n_graphs; g++) {
  414. struct ggml_cgraph * gf = graphs[g];
  415. for (int i = 0; i < gf->n_nodes; i++) {
  416. struct ggml_tensor * node = gf->nodes[i];
  417. if (ggml_is_view(node)) {
  418. struct ggml_tensor * view_src = get_view_source(node);
  419. hash_get(ht, view_src)->n_views += 1;
  420. }
  421. for (int j = 0; j < GGML_MAX_SRC; j++) {
  422. struct ggml_tensor * parent = node->src[j];
  423. if (parent == NULL) {
  424. break;
  425. }
  426. hash_get(ht, parent)->n_children += 1;
  427. }
  428. }
  429. }
  430. // allocate tensors
  431. for (int g = 0; g < n_graphs; g++) {
  432. struct ggml_cgraph * gf = graphs[g];
  433. AT_PRINTF("####### graph %d/%d\n", g, n_graphs);
  434. // graph inputs are allocated first to ensure that they are not overwritten by each other
  435. if (inputs != NULL && inputs[g] != NULL) {
  436. for (int i = 0; inputs[g][i] != NULL; i++) {
  437. struct ggml_tensor * input = inputs[g][i];
  438. AT_PRINTF("input: %s\n", input->name);
  439. allocate_node(alloc, input);
  440. }
  441. }
  442. for (int i = 0; i < gf->n_nodes; i++) {
  443. struct ggml_tensor * node = gf->nodes[i];
  444. // allocate parents (leafs)
  445. for (int j = 0; j < GGML_MAX_SRC; j++) {
  446. struct ggml_tensor * parent = node->src[j];
  447. if (parent == NULL) {
  448. break;
  449. }
  450. allocate_node(alloc, parent);
  451. }
  452. // allocate node
  453. allocate_node(alloc, node);
  454. AT_PRINTF("exec: %s (%s) <= ", ggml_op_name(node->op), node->name);
  455. for (int j = 0; j < GGML_MAX_SRC; j++) {
  456. struct ggml_tensor * parent = node->src[j];
  457. if (parent == NULL) {
  458. break;
  459. }
  460. AT_PRINTF("%s", parent->name);
  461. if (j < GGML_MAX_SRC - 1 && node->src[j + 1] != NULL) {
  462. AT_PRINTF(", ");
  463. }
  464. }
  465. AT_PRINTF("\n");
  466. // update parents
  467. for (int j = 0; j < GGML_MAX_SRC; j++) {
  468. struct ggml_tensor * parent = node->src[j];
  469. if (parent == NULL) {
  470. break;
  471. }
  472. struct hash_node * p_hn = hash_get(ht, parent);
  473. p_hn->n_children -= 1;
  474. //AT_PRINTF("parent %s: %d children, %d views\n", parent->name, parent->n_children, parent->n_views);
  475. if (p_hn->n_children == 0 && p_hn->n_views == 0) {
  476. if (ggml_is_view(parent)) {
  477. struct ggml_tensor * view_src = get_view_source(parent);
  478. struct hash_node * view_src_hn = hash_get(ht, view_src);
  479. view_src_hn->n_views -= 1;
  480. AT_PRINTF("view_src %s: %d children, %d views\n", view_src->name, view_src->n_children, view_src->n_views);
  481. if (view_src_hn->n_views == 0 && view_src_hn->n_children == 0 && view_src->data != node->data) {
  482. ggml_allocator_free_tensor(alloc, view_src);
  483. }
  484. }
  485. else {
  486. if (parent->data != node->data) {
  487. ggml_allocator_free_tensor(alloc, parent);
  488. }
  489. }
  490. }
  491. }
  492. AT_PRINTF("\n");
  493. }
  494. // free graph outputs here that wouldn't be freed otherwise because they have no children
  495. if (outputs != NULL && outputs[g] != NULL) {
  496. for (int i = 0; outputs[g][i] != NULL; i++) {
  497. struct ggml_tensor * output = outputs[g][i];
  498. AT_PRINTF("output: %s\n", output->name);
  499. ggml_allocator_free_tensor(alloc, output);
  500. }
  501. }
  502. }
  503. return alloc->max_size;
  504. }
  505. size_t ggml_allocr_alloc_graph(struct ggml_allocr * alloc, struct ggml_cgraph * graph) {
  506. return ggml_allocator_alloc_graph_tensors_n(alloc, &graph, 1, NULL, NULL);
  507. }