ggml-alloc.c 22 KB

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