ggml-alloc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. #include "ggml-alloc.h"
  2. #include "ggml-backend-impl.h"
  3. #include "ggml.h"
  4. #include "ggml-impl.h"
  5. #include <assert.h>
  6. #include <limits.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  12. #define MAX_FREE_BLOCKS 256
  13. //#define GGML_ALLOCATOR_DEBUG
  14. //#define AT_PRINTF(...) fprintf(stderr, __VA_ARGS__)
  15. #define AT_PRINTF(...)
  16. // TODO: GGML_PAD ?
  17. static size_t aligned_offset(const void * buffer, size_t offset, size_t alignment) {
  18. assert(alignment && !(alignment & (alignment - 1))); // power of 2
  19. size_t align = (alignment - (((uintptr_t)buffer + offset) % alignment)) % alignment;
  20. return offset + align;
  21. }
  22. struct free_block {
  23. void * addr;
  24. size_t size;
  25. };
  26. struct ggml_tallocr {
  27. struct ggml_backend_buffer * buffer;
  28. bool buffer_owned;
  29. void * base;
  30. size_t alignment;
  31. int n_free_blocks;
  32. struct free_block free_blocks[MAX_FREE_BLOCKS];
  33. size_t max_size;
  34. bool measure;
  35. #ifdef GGML_ALLOCATOR_DEBUG
  36. struct ggml_tensor * allocated_tensors[1024];
  37. #endif
  38. };
  39. #ifdef GGML_ALLOCATOR_DEBUG
  40. static void add_allocated_tensor(ggml_tallocr_t alloc, struct ggml_tensor * tensor) {
  41. for (int i = 0; i < 1024; i++) {
  42. if (alloc->allocated_tensors[i] == NULL) {
  43. alloc->allocated_tensors[i] = tensor;
  44. return;
  45. }
  46. }
  47. GGML_ASSERT(!"out of allocated_tensors");
  48. }
  49. static void remove_allocated_tensor(ggml_tallocr_t alloc, struct ggml_tensor * tensor) {
  50. for (int i = 0; i < 1024; i++) {
  51. if (alloc->allocated_tensors[i] == tensor ||
  52. (alloc->allocated_tensors[i] != NULL && alloc->allocated_tensors[i]->data == tensor->data)) {
  53. alloc->allocated_tensors[i] = NULL;
  54. return;
  55. }
  56. }
  57. printf("tried to free tensor %s not found\n", tensor->name);
  58. GGML_ASSERT(!"tensor not found");
  59. }
  60. #endif
  61. // check if a tensor is allocated by this buffer
  62. static bool ggml_tallocr_is_own(ggml_tallocr_t alloc, const struct ggml_tensor * tensor) {
  63. return tensor->buffer == alloc->buffer;
  64. }
  65. static bool ggml_is_view(struct ggml_tensor * t) {
  66. return t->view_src != NULL;
  67. }
  68. void ggml_tallocr_alloc(ggml_tallocr_t alloc, struct ggml_tensor * tensor) {
  69. GGML_ASSERT(!ggml_is_view(tensor)); // views generally get data pointer from one of their sources
  70. GGML_ASSERT(tensor->data == NULL); // avoid allocating tensor which already has memory allocated
  71. size_t size = ggml_backend_buffer_get_alloc_size(alloc->buffer, tensor);
  72. size = aligned_offset(NULL, size, alloc->alignment);
  73. AT_PRINTF("%s: allocating %s (%zu bytes) - ", __func__, tensor->name, size);
  74. size_t max_avail = 0;
  75. // find the best fitting free block besides the last block
  76. int best_fit_block = -1;
  77. size_t best_fit_size = SIZE_MAX;
  78. for (int i = 0; i < alloc->n_free_blocks - 1; i++) {
  79. struct free_block * block = &alloc->free_blocks[i];
  80. max_avail = MAX(max_avail, block->size);
  81. if (block->size >= size && block->size <= best_fit_size) {
  82. best_fit_block = i;
  83. best_fit_size = block->size;
  84. }
  85. }
  86. AT_PRINTF("block %d\n", best_fit_block);
  87. if (best_fit_block == -1) {
  88. // the last block is our last resort
  89. struct free_block * block = &alloc->free_blocks[alloc->n_free_blocks - 1];
  90. max_avail = MAX(max_avail, block->size);
  91. if (block->size >= size) {
  92. best_fit_block = alloc->n_free_blocks - 1;
  93. } else {
  94. fprintf(stderr, "%s: not enough space in the buffer (needed %zu, largest block available %zu)\n",
  95. __func__, size, max_avail);
  96. GGML_ASSERT(!"not enough space in the buffer");
  97. return;
  98. }
  99. }
  100. struct free_block * block = &alloc->free_blocks[best_fit_block];
  101. void * addr = block->addr;
  102. block->addr = (char*)block->addr + size;
  103. block->size -= size;
  104. if (block->size == 0) {
  105. // remove block if empty
  106. alloc->n_free_blocks--;
  107. for (int j = best_fit_block; j < alloc->n_free_blocks; j++) {
  108. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  109. }
  110. }
  111. tensor->data = addr;
  112. tensor->buffer = alloc->buffer;
  113. if (!alloc->measure) {
  114. ggml_backend_buffer_init_tensor(alloc->buffer, tensor);
  115. }
  116. #ifdef GGML_ALLOCATOR_DEBUG
  117. add_allocated_tensor(alloc, tensor);
  118. size_t cur_max = (char*)addr - (char*)alloc->base + size;
  119. if (cur_max > alloc->max_size) {
  120. printf("max_size = %.2f MB: tensors: ", cur_max / 1024.0 / 1024.0);
  121. for (int i = 0; i < 1024; i++) {
  122. if (alloc->allocated_tensors[i]) {
  123. printf("%s (%.2f MB) ", alloc->allocated_tensors[i]->name, ggml_nbytes(alloc->allocated_tensors[i]) / 1024.0 / 1024.0);
  124. }
  125. }
  126. printf("\n");
  127. }
  128. #endif
  129. alloc->max_size = MAX(alloc->max_size, (char*)addr - (char*)alloc->base + size);
  130. }
  131. // this is a very naive implementation, but for our case the number of free blocks should be very small
  132. static void ggml_tallocr_free_tensor(ggml_tallocr_t alloc, struct ggml_tensor * tensor) {
  133. if (ggml_tallocr_is_own(alloc, tensor) == false) {
  134. // the tensor was not allocated in this buffer
  135. // this can happen because the graph allocator will try to free weights and other tensors from different buffers
  136. // the easiest way to deal with this is just to ignore it
  137. // AT_PRINTF("ignoring %s (their buffer: %p, our buffer: %p)\n", tensor->name, (void *)tensor->buffer, (void *)alloc->buffer);
  138. return;
  139. }
  140. void * ptr = tensor->data;
  141. size_t size = ggml_backend_buffer_get_alloc_size(alloc->buffer, tensor);
  142. size = aligned_offset(NULL, size, alloc->alignment);
  143. AT_PRINTF("%s: freeing %s at %p (%zu bytes) - n_free_blocks = %d\n", __func__, tensor->name, ptr, size, alloc->n_free_blocks);
  144. #ifdef GGML_ALLOCATOR_DEBUG
  145. remove_allocated_tensor(alloc, tensor);
  146. #endif
  147. // see if we can merge with an existing block
  148. for (int i = 0; i < alloc->n_free_blocks; i++) {
  149. struct free_block * block = &alloc->free_blocks[i];
  150. // check if ptr is at the end of the block
  151. if ((char*)block->addr + block->size == ptr) {
  152. block->size += size;
  153. // check if we can merge with the next block
  154. if (i < alloc->n_free_blocks - 1 && (char*)block->addr + block->size == alloc->free_blocks[i+1].addr) {
  155. block->size += alloc->free_blocks[i+1].size;
  156. alloc->n_free_blocks--;
  157. for (int j = i+1; j < alloc->n_free_blocks; j++) {
  158. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  159. }
  160. }
  161. return;
  162. }
  163. // check if ptr is at the beginning of the block
  164. if ((char*)ptr + size == block->addr) {
  165. block->addr = ptr;
  166. block->size += size;
  167. // check if we can merge with the previous block
  168. if (i > 0 && (char*)alloc->free_blocks[i-1].addr + alloc->free_blocks[i-1].size == block->addr) {
  169. alloc->free_blocks[i-1].size += block->size;
  170. alloc->n_free_blocks--;
  171. for (int j = i; j < alloc->n_free_blocks; j++) {
  172. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  173. }
  174. }
  175. return;
  176. }
  177. }
  178. // otherwise, add a new block
  179. GGML_ASSERT(alloc->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks");
  180. // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster)
  181. int insert_pos = 0;
  182. while (insert_pos < alloc->n_free_blocks && alloc->free_blocks[insert_pos].addr < ptr) {
  183. insert_pos++;
  184. }
  185. // shift all blocks from insert_pos onward to make room for the new block
  186. for (int i = alloc->n_free_blocks; i > insert_pos; i--) {
  187. alloc->free_blocks[i] = alloc->free_blocks[i-1];
  188. }
  189. // insert the new block
  190. alloc->free_blocks[insert_pos].addr = ptr;
  191. alloc->free_blocks[insert_pos].size = size;
  192. alloc->n_free_blocks++;
  193. }
  194. void ggml_tallocr_reset(ggml_tallocr_t alloc) {
  195. alloc->n_free_blocks = 1;
  196. size_t align_offset = aligned_offset(alloc->base, 0, alloc->alignment);
  197. alloc->free_blocks[0].addr = (char *)alloc->base + align_offset;
  198. if (alloc->measure) {
  199. alloc->free_blocks[0].size = SIZE_MAX/2; // restrict maximum size of a measure allocator to half size_t max to avoid overflows
  200. } else {
  201. alloc->free_blocks[0].size = ggml_backend_buffer_get_size(alloc->buffer) - align_offset;
  202. }
  203. }
  204. ggml_tallocr_t ggml_tallocr_new(void * data, size_t size, size_t alignment) {
  205. struct ggml_backend_buffer * buffer = ggml_backend_cpu_buffer_from_ptr(data, size);
  206. ggml_tallocr_t alloc = (ggml_tallocr_t)malloc(sizeof(struct ggml_tallocr));
  207. *alloc = (struct ggml_tallocr) {
  208. /*.buffer = */ buffer,
  209. /*.buffer_owned = */ true,
  210. /*.base = */ ggml_backend_buffer_get_base(buffer),
  211. /*.alignment = */ alignment,
  212. /*.n_free_blocks = */ 0,
  213. /*.free_blocks = */ {{0}},
  214. /*.max_size = */ 0,
  215. /*.measure = */ false,
  216. #ifdef GGML_ALLOCATOR_DEBUG
  217. /*.allocated_tensors = */ {0},
  218. #endif
  219. };
  220. ggml_tallocr_reset(alloc);
  221. return alloc;
  222. }
  223. ggml_tallocr_t ggml_tallocr_new_measure(size_t alignment) {
  224. ggml_tallocr_t alloc = ggml_tallocr_new((void *)0x1000, SIZE_MAX/2, alignment);
  225. alloc->measure = true;
  226. return alloc;
  227. }
  228. ggml_tallocr_t ggml_tallocr_new_measure_from_backend(struct ggml_backend * backend) {
  229. // create a backend buffer to get the correct tensor allocation sizes
  230. ggml_backend_buffer_t buffer = ggml_backend_alloc_buffer(backend, 1);
  231. // TODO: move alloc initialization to a common ggml_tallocr_new_impl function
  232. ggml_tallocr_t alloc = ggml_tallocr_new_from_buffer(buffer);
  233. alloc->buffer_owned = true;
  234. alloc->measure = true;
  235. ggml_tallocr_reset(alloc);
  236. return alloc;
  237. }
  238. ggml_tallocr_t ggml_tallocr_new_from_backend(struct ggml_backend * backend, size_t size) {
  239. ggml_backend_buffer_t buffer = ggml_backend_alloc_buffer(backend, size);
  240. ggml_tallocr_t alloc = ggml_tallocr_new_from_buffer(buffer);
  241. alloc->buffer_owned = true;
  242. return alloc;
  243. }
  244. ggml_tallocr_t ggml_tallocr_new_from_buffer(struct ggml_backend_buffer * buffer) {
  245. ggml_tallocr_t alloc = (ggml_tallocr_t)malloc(sizeof(struct ggml_tallocr));
  246. *alloc = (struct ggml_tallocr) {
  247. /*.buffer = */ buffer,
  248. /*.buffer_owned = */ false,
  249. /*.base = */ ggml_backend_buffer_get_base(buffer),
  250. /*.alignment = */ ggml_backend_buffer_get_alignment(buffer),
  251. /*.n_free_blocks = */ 0,
  252. /*.free_blocks = */ {{0}},
  253. /*.max_size = */ 0,
  254. /*.measure = */ false,
  255. #ifdef GGML_ALLOCATOR_DEBUG
  256. /*.allocated_tensors = */ {0},
  257. #endif
  258. };
  259. ggml_tallocr_reset(alloc);
  260. return alloc;
  261. }
  262. struct ggml_backend_buffer * ggml_tallocr_get_buffer(ggml_tallocr_t alloc) {
  263. return alloc->buffer;
  264. }
  265. void ggml_tallocr_free(ggml_tallocr_t alloc) {
  266. if (alloc == NULL) {
  267. return;
  268. }
  269. if (alloc->buffer_owned) {
  270. ggml_backend_buffer_free(alloc->buffer);
  271. }
  272. free(alloc);
  273. }
  274. bool ggml_tallocr_is_measure(ggml_tallocr_t alloc) {
  275. return alloc->measure;
  276. }
  277. size_t ggml_tallocr_max_size(ggml_tallocr_t alloc) {
  278. return alloc->max_size;
  279. }
  280. // graph allocator
  281. struct hash_node {
  282. int n_children;
  283. int n_views;
  284. };
  285. struct ggml_gallocr {
  286. ggml_tallocr_t talloc;
  287. struct ggml_hash_set hash_set;
  288. struct hash_node * hash_values;
  289. size_t hash_values_size;
  290. ggml_tallocr_t * hash_allocs;
  291. int * parse_seq;
  292. int parse_seq_len;
  293. };
  294. ggml_gallocr_t ggml_gallocr_new(void) {
  295. ggml_gallocr_t galloc = (ggml_gallocr_t)malloc(sizeof(struct ggml_gallocr));
  296. *galloc = (struct ggml_gallocr) {
  297. /*.talloc = */ NULL,
  298. /*.hash_set = */ {0},
  299. /*.hash_values = */ NULL,
  300. /*.hash_values_size = */ 0,
  301. /*.hash_allocs = */ NULL,
  302. /*.parse_seq = */ NULL,
  303. /*.parse_seq_len = */ 0,
  304. };
  305. return galloc;
  306. }
  307. void ggml_gallocr_free(ggml_gallocr_t galloc) {
  308. if (galloc == NULL) {
  309. return;
  310. }
  311. if (galloc->hash_set.keys != NULL) {
  312. free(galloc->hash_set.keys);
  313. }
  314. if (galloc->hash_values != NULL) {
  315. free(galloc->hash_values);
  316. }
  317. if (galloc->hash_allocs != NULL) {
  318. free(galloc->hash_allocs);
  319. }
  320. if (galloc->parse_seq != NULL) {
  321. free(galloc->parse_seq);
  322. }
  323. free(galloc);
  324. }
  325. void ggml_gallocr_set_parse_seq(ggml_gallocr_t galloc, const int * list, int n) {
  326. free(galloc->parse_seq);
  327. galloc->parse_seq = malloc(sizeof(int) * n);
  328. for (int i = 0; i < n; i++) {
  329. galloc->parse_seq[i] = list[i];
  330. }
  331. galloc->parse_seq_len = n;
  332. }
  333. static struct hash_node * hash_get(ggml_gallocr_t galloc, struct ggml_tensor * t) {
  334. size_t i = ggml_hash_find_or_insert(galloc->hash_set, t);
  335. return &galloc->hash_values[i];
  336. }
  337. static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {
  338. if (a->type != b->type) {
  339. return false;
  340. }
  341. for (int i = 0; i < GGML_MAX_DIMS; i++) {
  342. if (a->ne[i] != b->ne[i]) {
  343. return false;
  344. }
  345. if (a->nb[i] != b->nb[i]) {
  346. return false;
  347. }
  348. }
  349. return true;
  350. }
  351. static bool ggml_op_can_inplace(enum ggml_op op) {
  352. switch (op) {
  353. case GGML_OP_SCALE:
  354. case GGML_OP_DIAG_MASK_ZERO:
  355. case GGML_OP_DIAG_MASK_INF:
  356. case GGML_OP_ADD:
  357. case GGML_OP_ADD1:
  358. case GGML_OP_SUB:
  359. case GGML_OP_MUL:
  360. case GGML_OP_DIV:
  361. case GGML_OP_SQR:
  362. case GGML_OP_SQRT:
  363. case GGML_OP_LOG:
  364. case GGML_OP_UNARY:
  365. case GGML_OP_ROPE:
  366. case GGML_OP_RMS_NORM:
  367. case GGML_OP_SOFT_MAX:
  368. return true;
  369. default:
  370. return false;
  371. }
  372. }
  373. static ggml_tallocr_t node_tallocr(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  374. if (galloc->talloc != NULL) {
  375. return galloc->talloc;
  376. }
  377. return galloc->hash_allocs[ggml_hash_find_or_insert(galloc->hash_set, node)];
  378. }
  379. static void init_view(ggml_gallocr_t galloc, struct ggml_tensor * view, bool update_backend) {
  380. ggml_tallocr_t alloc = node_tallocr(galloc, view);
  381. GGML_ASSERT(view->view_src != NULL && view->view_src->data != NULL);
  382. if (update_backend) {
  383. view->backend = view->view_src->backend;
  384. }
  385. view->buffer = view->view_src->buffer;
  386. view->data = (char *)view->view_src->data + view->view_offs;
  387. // FIXME: the view should be initialized by the owning buffer, but currently this breaks the CUDA backend
  388. // due to the ggml_tensor_extra_gpu ring buffer overwriting the KV cache extras
  389. assert(ggml_tallocr_is_measure(alloc) || !view->buffer || view->buffer->buft == alloc->buffer->buft);
  390. if (!alloc->measure) {
  391. ggml_backend_buffer_init_tensor(alloc->buffer, view);
  392. }
  393. }
  394. static void allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  395. ggml_tallocr_t alloc = node_tallocr(galloc, node);
  396. if (node->data == NULL) {
  397. if (ggml_is_view(node)) {
  398. init_view(galloc, node, true);
  399. } else {
  400. // see if we can reuse a parent's buffer (inplace)
  401. if (ggml_op_can_inplace(node->op)) {
  402. for (int i = 0; i < GGML_MAX_SRC; i++) {
  403. struct ggml_tensor * parent = node->src[i];
  404. if (parent == NULL) {
  405. break;
  406. }
  407. // if the node's data is external, then we cannot re-use it
  408. if (ggml_tallocr_is_own(alloc, parent) == false) {
  409. AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data);
  410. continue;
  411. }
  412. struct hash_node * p_hn = hash_get(galloc, parent);
  413. if (parent->data != NULL && p_hn->n_children == 1 && p_hn->n_views == 0 && ggml_are_same_layout(node, parent)) {
  414. if (ggml_is_view(parent)) {
  415. struct ggml_tensor * view_src = parent->view_src;
  416. struct hash_node * view_src_hn = hash_get(galloc, view_src);
  417. if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) {
  418. // TODO: the offset of the view parent must be kept to ensure that the op doesn't overwrite
  419. // the parent's data that it will need later (same layout requirement). the problem is that then
  420. // we cannot free the tensor because the original address of the allocation is lost.
  421. // adding a view_src pointer to the tensor would solve this and simplify the code dealing with views
  422. // for now, we only reuse the parent's data if the offset is zero (view_src->data == parent->data)
  423. AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name);
  424. node->view_src = view_src;
  425. view_src_hn->n_views += 1;
  426. init_view(galloc, node, false);
  427. return;
  428. }
  429. } else {
  430. AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name);
  431. node->view_src = parent;
  432. p_hn->n_views += 1;
  433. init_view(galloc, node, false);
  434. return;
  435. }
  436. }
  437. }
  438. }
  439. ggml_tallocr_alloc(alloc, node);
  440. }
  441. }
  442. }
  443. static void free_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  444. ggml_tallocr_t alloc = node_tallocr(galloc, node);
  445. ggml_tallocr_free_tensor(alloc, node);
  446. }
  447. static void ggml_tallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgraph * gf) {
  448. const int * parse_seq = galloc->parse_seq;
  449. int parse_seq_len = galloc->parse_seq_len;
  450. // count number of children and views
  451. for (int i = 0; i < gf->n_nodes; i++) {
  452. struct ggml_tensor * node = gf->nodes[i];
  453. if (ggml_is_view(node)) {
  454. struct ggml_tensor * view_src = node->view_src;
  455. hash_get(galloc, view_src)->n_views += 1;
  456. if (node->buffer == NULL && node->data != NULL) {
  457. // view of a pre-allocated tensor, didn't call init_view() yet
  458. init_view(galloc, node, true);
  459. }
  460. }
  461. for (int j = 0; j < GGML_MAX_SRC; j++) {
  462. struct ggml_tensor * parent = node->src[j];
  463. if (parent == NULL) {
  464. break;
  465. }
  466. hash_get(galloc, parent)->n_children += 1;
  467. if (ggml_is_view(parent) && parent->buffer == NULL && parent->data != NULL) {
  468. init_view(galloc, parent, true);
  469. }
  470. }
  471. }
  472. // allocate tensors
  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 = parse_seq_len ? 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 (parse_seq_len == 0 || parse_seq[ind] != -1) {
  479. int i = parse_seq_len ? 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(galloc, parent);
  488. }
  489. // allocate node
  490. allocate_node(galloc, 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 ((parse_seq_len == 0) || parse_seq[ind] == -1) {
  508. int update_start = parse_seq_len ? last_barrier_pos : ind;
  509. int update_end = parse_seq_len ? ind : ind + 1;
  510. for (int i = update_start; i < update_end; i++) {
  511. int node_i = parse_seq_len ? 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(galloc, 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(galloc, 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) {
  528. free_node(galloc, view_src);
  529. }
  530. }
  531. else {
  532. free_node(galloc, parent);
  533. }
  534. }
  535. }
  536. }
  537. AT_PRINTF("\n");
  538. if (parse_seq_len) {
  539. last_barrier_pos = ind + 1;
  540. }
  541. }
  542. }
  543. }
  544. size_t ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, ggml_tallocr_t talloc, struct ggml_cgraph * graph) {
  545. size_t hash_size = graph->visited_hash_table.size;
  546. // check if the hash table is initialized and large enough
  547. if (galloc->hash_set.size < hash_size) {
  548. if (galloc->hash_set.keys != NULL) {
  549. free(galloc->hash_set.keys);
  550. }
  551. if (galloc->hash_values != NULL) {
  552. free(galloc->hash_values);
  553. }
  554. galloc->hash_set.keys = malloc(sizeof(struct ggml_tensor *) * hash_size);
  555. galloc->hash_set.size = hash_size;
  556. galloc->hash_values = malloc(sizeof(struct hash_node) * hash_size);
  557. }
  558. // reset hash table
  559. memset(galloc->hash_set.keys, 0, sizeof(struct ggml_tensor *) * hash_size);
  560. memset(galloc->hash_values, 0, sizeof(struct hash_node) * hash_size);
  561. galloc->talloc = talloc;
  562. ggml_tallocr_alloc_graph_impl(galloc, graph);
  563. galloc->talloc = NULL;
  564. size_t max_size = ggml_tallocr_max_size(talloc);
  565. return max_size;
  566. }
  567. void ggml_gallocr_alloc_graph_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, struct ggml_hash_set hash_set, ggml_tallocr_t * hash_node_talloc) {
  568. const size_t hash_size = hash_set.size;
  569. GGML_ASSERT(hash_size >= (size_t)(graph->n_nodes + graph->n_leafs));
  570. galloc->talloc = NULL;
  571. // alloc hash_values if needed
  572. if (galloc->hash_values == NULL || galloc->hash_values_size < hash_size) {
  573. free(galloc->hash_values);
  574. galloc->hash_values = malloc(sizeof(struct hash_node) * hash_size);
  575. galloc->hash_values_size = hash_size;
  576. }
  577. // free hash_set.keys if needed
  578. if (galloc->hash_set.keys != NULL) {
  579. free(galloc->hash_set.keys);
  580. }
  581. galloc->hash_set = hash_set;
  582. // reset hash values
  583. memset(galloc->hash_values, 0, sizeof(struct hash_node) * hash_size);
  584. galloc->hash_allocs = hash_node_talloc;
  585. ggml_tallocr_alloc_graph_impl(galloc, graph);
  586. // remove unowned resources
  587. galloc->hash_set.keys = NULL;
  588. galloc->hash_allocs = NULL;
  589. }
  590. // legacy API wrapper
  591. struct ggml_allocr {
  592. ggml_tallocr_t talloc;
  593. ggml_gallocr_t galloc;
  594. };
  595. static ggml_allocr_t ggml_allocr_new_impl(ggml_tallocr_t talloc) {
  596. ggml_allocr_t alloc = (ggml_allocr_t)malloc(sizeof(struct ggml_allocr));
  597. *alloc = (struct ggml_allocr) {
  598. /*.talloc = */ talloc,
  599. /*.galloc = */ ggml_gallocr_new(),
  600. };
  601. return alloc;
  602. }
  603. ggml_allocr_t ggml_allocr_new(void * data, size_t size, size_t alignment) {
  604. return ggml_allocr_new_impl(ggml_tallocr_new(data, size, alignment));
  605. }
  606. ggml_allocr_t ggml_allocr_new_measure(size_t alignment) {
  607. return ggml_allocr_new_impl(ggml_tallocr_new_measure(alignment));
  608. }
  609. ggml_allocr_t ggml_allocr_new_from_buffer(struct ggml_backend_buffer * buffer) {
  610. return ggml_allocr_new_impl(ggml_tallocr_new_from_buffer(buffer));
  611. }
  612. ggml_allocr_t ggml_allocr_new_from_backend(struct ggml_backend * backend, size_t size) {
  613. return ggml_allocr_new_impl(ggml_tallocr_new_from_backend(backend, size));
  614. }
  615. ggml_allocr_t ggml_allocr_new_measure_from_backend(struct ggml_backend * backend) {
  616. return ggml_allocr_new_impl(ggml_tallocr_new_measure_from_backend(backend));
  617. }
  618. struct ggml_backend_buffer * ggml_allocr_get_buffer(ggml_allocr_t alloc) {
  619. return ggml_tallocr_get_buffer(alloc->talloc);
  620. }
  621. void ggml_allocr_set_parse_seq(ggml_allocr_t alloc, const int * list, int n) {
  622. ggml_gallocr_set_parse_seq(alloc->galloc, list, n);
  623. }
  624. void ggml_allocr_free(ggml_allocr_t alloc) {
  625. ggml_gallocr_free(alloc->galloc);
  626. ggml_tallocr_free(alloc->talloc);
  627. free(alloc);
  628. }
  629. bool ggml_allocr_is_measure(ggml_allocr_t alloc) {
  630. return ggml_tallocr_is_measure(alloc->talloc);
  631. }
  632. void ggml_allocr_reset(ggml_allocr_t alloc) {
  633. ggml_tallocr_reset(alloc->talloc);
  634. }
  635. void ggml_allocr_alloc(ggml_allocr_t alloc, struct ggml_tensor * tensor) {
  636. ggml_tallocr_alloc(alloc->talloc, tensor);
  637. }
  638. size_t ggml_allocr_max_size(ggml_allocr_t alloc) {
  639. return ggml_tallocr_max_size(alloc->talloc);
  640. }
  641. size_t ggml_allocr_alloc_graph(ggml_allocr_t alloc, struct ggml_cgraph * graph) {
  642. return ggml_gallocr_alloc_graph(alloc->galloc, alloc->talloc, graph);
  643. }
  644. // utils
  645. ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) {
  646. GGML_ASSERT(ggml_get_no_alloc(ctx) == true);
  647. size_t alignment = ggml_backend_buft_get_alignment(buft);
  648. size_t nbytes = 0;
  649. for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  650. if (t->data == NULL && t->view_src == NULL) {
  651. nbytes += GGML_PAD(ggml_backend_buft_get_alloc_size(buft, t), alignment);
  652. }
  653. }
  654. if (nbytes == 0) {
  655. fprintf(stderr, "%s: no tensors to allocate\n", __func__);
  656. return NULL;
  657. }
  658. ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buft, nbytes);
  659. ggml_tallocr_t tallocr = ggml_tallocr_new_from_buffer(buffer);
  660. for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  661. if (t->data == NULL) {
  662. if (t->view_src == NULL) {
  663. ggml_tallocr_alloc(tallocr, t);
  664. } else {
  665. ggml_backend_view_init(buffer, t);
  666. }
  667. }
  668. }
  669. ggml_tallocr_free(tallocr);
  670. return buffer;
  671. }
  672. ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, ggml_backend_t backend) {
  673. return ggml_backend_alloc_ctx_tensors_from_buft(ctx, ggml_backend_get_default_buffer_type(backend));
  674. }