ggml-alloc.c 23 KB

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