ggml-alloc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // defines MAP_ANONYMOUS
  2. #ifndef _GNU_SOURCE
  3. #define _GNU_SOURCE
  4. #endif
  5. #include "ggml-alloc.h"
  6. #include "ggml.h"
  7. #include <assert.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #ifdef __has_include
  13. #if __has_include(<unistd.h>)
  14. #include <unistd.h>
  15. #if defined(_POSIX_MAPPED_FILES)
  16. #include <sys/types.h>
  17. #include <sys/mman.h>
  18. #endif
  19. #endif
  20. #endif
  21. #if defined(_WIN32)
  22. #define WIN32_LEAN_AND_MEAN
  23. #ifndef NOMINMAX
  24. #define NOMINMAX
  25. #endif
  26. #include <windows.h>
  27. #include <memoryapi.h>
  28. #endif
  29. #define UNUSED(x) (void)(x)
  30. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  31. #define GGML_MAX_CONCUR (2*GGML_MAX_NODES)
  32. //#define GGML_ALLOCATOR_DEBUG
  33. //#define AT_PRINTF printf
  34. #define AT_PRINTF(...) ((void)0)
  35. struct hash_node {
  36. struct ggml_tensor * t;
  37. int n_children;
  38. int n_views;
  39. };
  40. static size_t hash(void * p) {
  41. return (size_t)p % GGML_GRAPH_HASHTABLE_SIZE;
  42. }
  43. static struct hash_node * hash_get(struct hash_node hash_table[], struct ggml_tensor * t) {
  44. size_t h = hash(t);
  45. // linear probing
  46. size_t i = h;
  47. while (hash_table[i].t != NULL) {
  48. if (hash_table[i].t == t) {
  49. return &hash_table[i];
  50. }
  51. i = (i + 1) % GGML_GRAPH_HASHTABLE_SIZE;
  52. if (i == h) {
  53. // hash table is full
  54. GGML_ASSERT(false);
  55. }
  56. }
  57. hash_table[i].t = t;
  58. return &hash_table[i];
  59. }
  60. // TODO: GGML_PAD ?
  61. static size_t aligned_offset(const void * buffer, size_t offset, size_t alignment) {
  62. assert(alignment && !(alignment & (alignment - 1))); // power of 2
  63. size_t align = (alignment - (((uintptr_t)buffer + offset) % alignment)) % alignment;
  64. return offset + align;
  65. }
  66. struct free_block {
  67. void * addr;
  68. size_t size;
  69. };
  70. #define MAX_FREE_BLOCKS 128
  71. struct ggml_allocr {
  72. void * data;
  73. size_t size;
  74. size_t alignment;
  75. int n_free_blocks;
  76. struct free_block free_blocks[MAX_FREE_BLOCKS];
  77. struct hash_node hash_table[GGML_GRAPH_HASHTABLE_SIZE];
  78. size_t max_size;
  79. bool measure;
  80. int parse_seq[GGML_MAX_CONCUR];
  81. int parse_seq_len;
  82. #ifdef GGML_ALLOCATOR_DEBUG
  83. struct ggml_tensor * allocated_tensors[1024];
  84. #endif
  85. };
  86. #ifdef GGML_ALLOCATOR_DEBUG
  87. static void add_allocated_tensor(struct ggml_allocr * alloc, struct ggml_tensor * tensor) {
  88. for (int i = 0; i < 1024; i++) {
  89. if (alloc->allocated_tensors[i] == NULL) {
  90. alloc->allocated_tensors[i] = tensor;
  91. return;
  92. }
  93. }
  94. GGML_ASSERT(!"out of allocated_tensors");
  95. }
  96. static void remove_allocated_tensor(struct ggml_allocr * alloc, struct ggml_tensor * tensor) {
  97. for (int i = 0; i < 1024; i++) {
  98. if (alloc->allocated_tensors[i] == tensor ||
  99. (alloc->allocated_tensors[i] != NULL && alloc->allocated_tensors[i]->data == tensor->data)) {
  100. alloc->allocated_tensors[i] = NULL;
  101. return;
  102. }
  103. }
  104. printf("tried to free tensor %s not found\n", tensor->name);
  105. GGML_ASSERT(!"tensor not found");
  106. }
  107. #endif
  108. static size_t ggml_allocr_get_alloc_size(struct ggml_allocr * alloc, struct ggml_tensor * tensor) {
  109. return ggml_nbytes(tensor);
  110. UNUSED(alloc);
  111. }
  112. // check if a tensor is allocated by this buffer
  113. static bool ggml_allocr_is_own(struct ggml_allocr * alloc, const struct ggml_tensor * tensor) {
  114. void * ptr = tensor->data;
  115. return ptr >= alloc->data && (char *)ptr < (char *)alloc->data + alloc->max_size;
  116. }
  117. void ggml_allocr_alloc(struct ggml_allocr * alloc, struct ggml_tensor * tensor) {
  118. #ifdef GGML_ALLOCATOR_DEBUG
  119. GGML_ASSERT(ggml_is_view(tensor) == false); // views generally get data pointer from one of their sources
  120. GGML_ASSERT(tensor->data == NULL); // avoid allocating tensor which already has memory allocated
  121. #endif
  122. size_t size = ggml_allocr_get_alloc_size(alloc, tensor);
  123. size = aligned_offset(NULL, size, alloc->alignment);
  124. AT_PRINTF("%s: allocating %s (%zu bytes) - ", __func__, tensor->name, size);
  125. size_t max_avail = 0;
  126. // find the best fitting free block besides the last block
  127. int best_fit_block = -1;
  128. size_t best_fit_size = SIZE_MAX;
  129. for (int i = 0; i < alloc->n_free_blocks - 1; i++) {
  130. struct free_block * block = &alloc->free_blocks[i];
  131. max_avail = MAX(max_avail, block->size);
  132. if (block->size >= size && block->size <= best_fit_size) {
  133. best_fit_block = i;
  134. best_fit_size = block->size;
  135. }
  136. }
  137. AT_PRINTF("block %d\n", best_fit_block);
  138. if (best_fit_block == -1) {
  139. // the last block is our last resort
  140. struct free_block * block = &alloc->free_blocks[alloc->n_free_blocks - 1];
  141. if (block->size >= size) {
  142. best_fit_block = alloc->n_free_blocks - 1;
  143. max_avail = MAX(max_avail, block->size);
  144. } else {
  145. fprintf(stderr, "%s: not enough space in the buffer (needed %zu, largest block available %zu)\n",
  146. __func__, size, max_avail);
  147. GGML_ASSERT(!"not enough space in the buffer");
  148. return;
  149. }
  150. }
  151. struct free_block * block = &alloc->free_blocks[best_fit_block];
  152. void * addr = block->addr;
  153. block->addr = (char*)block->addr + size;
  154. block->size -= size;
  155. if (block->size == 0) {
  156. // remove block if empty
  157. alloc->n_free_blocks--;
  158. for (int j = best_fit_block; j < alloc->n_free_blocks; j++) {
  159. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  160. }
  161. }
  162. tensor->data = addr;
  163. #ifdef GGML_ALLOCATOR_DEBUG
  164. add_allocated_tensor(alloc, tensor);
  165. size_t cur_max = (char*)addr - (char*)alloc->data + size;
  166. if (cur_max > alloc->max_size) {
  167. printf("max_size = %.2f MB: tensors: ", cur_max / 1024.0 / 1024.0);
  168. for (int i = 0; i < 1024; i++) {
  169. if (alloc->allocated_tensors[i]) {
  170. printf("%s (%.2f MB) ", alloc->allocated_tensors[i]->name, ggml_nbytes(alloc->allocated_tensors[i]) / 1024.0 / 1024.0);
  171. }
  172. }
  173. printf("\n");
  174. }
  175. #endif
  176. alloc->max_size = MAX(alloc->max_size, (char*)addr - (char*)alloc->data + size);
  177. }
  178. // this is a very naive implementation, but for our case the number of free blocks should be very small
  179. static void ggml_allocr_free_tensor(struct ggml_allocr * alloc, struct ggml_tensor * tensor) {
  180. void * ptr = tensor->data;
  181. if (ggml_allocr_is_own(alloc, tensor) == false) {
  182. // the tensor was not allocated in this buffer
  183. // this can happen because the graph allocator will try to free weights and other tensors from different buffers
  184. // the easiest way to deal with this is just to ignore it
  185. return;
  186. }
  187. size_t size = ggml_allocr_get_alloc_size(alloc, tensor);
  188. size = aligned_offset(NULL, size, alloc->alignment);
  189. AT_PRINTF("%s: freeing %s (%zu bytes) - n_free_blocks = %d\n", __func__, tensor->name, size, alloc->n_free_blocks);
  190. #ifdef GGML_ALLOCATOR_DEBUG
  191. remove_allocated_tensor(alloc, tensor);
  192. #endif
  193. // see if we can merge with an existing block
  194. for (int i = 0; i < alloc->n_free_blocks; i++) {
  195. struct free_block * block = &alloc->free_blocks[i];
  196. // check if ptr is at the end of the block
  197. if ((char*)block->addr + block->size == ptr) {
  198. block->size += size;
  199. // check if we can merge with the next block
  200. if (i < alloc->n_free_blocks - 1 && (char*)block->addr + block->size == alloc->free_blocks[i+1].addr) {
  201. block->size += alloc->free_blocks[i+1].size;
  202. alloc->n_free_blocks--;
  203. for (int j = i+1; j < alloc->n_free_blocks; j++) {
  204. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  205. }
  206. }
  207. return;
  208. }
  209. // check if ptr is at the beginning of the block
  210. if ((char*)ptr + size == block->addr) {
  211. block->addr = ptr;
  212. block->size += size;
  213. // check if we can merge with the previous block
  214. if (i > 0 && (char*)alloc->free_blocks[i-1].addr + alloc->free_blocks[i-1].size == block->addr) {
  215. alloc->free_blocks[i-1].size += block->size;
  216. alloc->n_free_blocks--;
  217. for (int j = i; j < alloc->n_free_blocks; j++) {
  218. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  219. }
  220. }
  221. return;
  222. }
  223. }
  224. // otherwise, add a new block
  225. GGML_ASSERT(alloc->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks");
  226. // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster)
  227. int insert_pos = 0;
  228. while (insert_pos < alloc->n_free_blocks && alloc->free_blocks[insert_pos].addr < ptr) {
  229. insert_pos++;
  230. }
  231. // shift all blocks from insert_pos onward to make room for the new block
  232. for (int i = alloc->n_free_blocks; i > insert_pos; i--) {
  233. alloc->free_blocks[i] = alloc->free_blocks[i-1];
  234. }
  235. // insert the new block
  236. alloc->free_blocks[insert_pos].addr = ptr;
  237. alloc->free_blocks[insert_pos].size = size;
  238. alloc->n_free_blocks++;
  239. }
  240. void ggml_allocr_set_parse_seq(struct ggml_allocr * alloc, const int * list, int n) {
  241. for (int i = 0; i < n; i++) {
  242. alloc->parse_seq[i] = list[i];
  243. }
  244. alloc->parse_seq_len = n;
  245. }
  246. void ggml_allocr_reset(struct ggml_allocr * alloc) {
  247. alloc->n_free_blocks = 1;
  248. size_t align_offset = aligned_offset(alloc->data, 0, alloc->alignment);
  249. alloc->free_blocks[0].addr = (char *)alloc->data + align_offset;
  250. alloc->free_blocks[0].size = alloc->size - align_offset;
  251. }
  252. struct ggml_allocr * ggml_allocr_new(void * data, size_t size, size_t alignment) {
  253. struct ggml_allocr * alloc = (struct ggml_allocr *)malloc(sizeof(struct ggml_allocr) /* + n_free_blocks * sizeof(struct free_block) */);
  254. *alloc = (struct ggml_allocr){
  255. /*.data = */ data,
  256. /*.size = */ size,
  257. /*.alignment = */ alignment,
  258. /*.n_free_blocks = */ 0,
  259. /*.free_blocks = */ {{0}},
  260. /*.hash_table = */ {{0}},
  261. /*.max_size = */ 0,
  262. /*.measure = */ false,
  263. /*.parse_seq = */ {0},
  264. /*.parse_seq_len = */ 0,
  265. #ifdef GGML_ALLOCATOR_DEBUG
  266. /*.allocated_tensors = */ {0},
  267. #endif
  268. };
  269. ggml_allocr_reset(alloc);
  270. return alloc;
  271. }
  272. // OS specific functions to allocate and free uncommitted virtual memory
  273. static void * alloc_vmem(size_t size) {
  274. #if defined(_WIN32)
  275. return VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS);
  276. #elif defined(_POSIX_MAPPED_FILES)
  277. return mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
  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. }