ggml-alloc.c 23 KB

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