ggml-alloc.c 22 KB

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