ggml-alloc.c 21 KB

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