ggml-alloc.c 22 KB

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