1
0

ggml-alloc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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->data + 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. if (!alloc->measure) {
  145. ggml_backend_buffer_free_tensor(alloc->buffer, tensor);
  146. }
  147. #ifdef GGML_ALLOCATOR_DEBUG
  148. remove_allocated_tensor(alloc, tensor);
  149. #endif
  150. // see if we can merge with an existing block
  151. for (int i = 0; i < alloc->n_free_blocks; i++) {
  152. struct free_block * block = &alloc->free_blocks[i];
  153. // check if ptr is at the end of the block
  154. if ((char*)block->addr + block->size == ptr) {
  155. block->size += size;
  156. // check if we can merge with the next block
  157. if (i < alloc->n_free_blocks - 1 && (char*)block->addr + block->size == alloc->free_blocks[i+1].addr) {
  158. block->size += alloc->free_blocks[i+1].size;
  159. alloc->n_free_blocks--;
  160. for (int j = i+1; j < alloc->n_free_blocks; j++) {
  161. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  162. }
  163. }
  164. return;
  165. }
  166. // check if ptr is at the beginning of the block
  167. if ((char*)ptr + size == block->addr) {
  168. block->addr = ptr;
  169. block->size += size;
  170. // check if we can merge with the previous block
  171. if (i > 0 && (char*)alloc->free_blocks[i-1].addr + alloc->free_blocks[i-1].size == block->addr) {
  172. alloc->free_blocks[i-1].size += block->size;
  173. alloc->n_free_blocks--;
  174. for (int j = i; j < alloc->n_free_blocks; j++) {
  175. alloc->free_blocks[j] = alloc->free_blocks[j+1];
  176. }
  177. }
  178. return;
  179. }
  180. }
  181. // otherwise, add a new block
  182. GGML_ASSERT(alloc->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks");
  183. // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster)
  184. int insert_pos = 0;
  185. while (insert_pos < alloc->n_free_blocks && alloc->free_blocks[insert_pos].addr < ptr) {
  186. insert_pos++;
  187. }
  188. // shift all blocks from insert_pos onward to make room for the new block
  189. for (int i = alloc->n_free_blocks; i > insert_pos; i--) {
  190. alloc->free_blocks[i] = alloc->free_blocks[i-1];
  191. }
  192. // insert the new block
  193. alloc->free_blocks[insert_pos].addr = ptr;
  194. alloc->free_blocks[insert_pos].size = size;
  195. alloc->n_free_blocks++;
  196. }
  197. void ggml_tallocr_reset(ggml_tallocr_t alloc) {
  198. alloc->n_free_blocks = 1;
  199. size_t align_offset = aligned_offset(alloc->base, 0, alloc->alignment);
  200. alloc->free_blocks[0].addr = (char *)alloc->base + align_offset;
  201. if (alloc->measure) {
  202. alloc->free_blocks[0].size = SIZE_MAX/2; // restrict maximum size of a measure allocator to half size_t max to avoid overflows
  203. } else {
  204. alloc->free_blocks[0].size = ggml_backend_buffer_get_size(alloc->buffer) - align_offset;
  205. }
  206. }
  207. ggml_tallocr_t ggml_tallocr_new(void * data, size_t size, size_t alignment) {
  208. struct ggml_backend_buffer * buffer = ggml_backend_cpu_buffer_from_ptr(NULL, data, size);
  209. ggml_tallocr_t alloc = (ggml_tallocr_t)malloc(sizeof(struct ggml_tallocr));
  210. *alloc = (struct ggml_tallocr) {
  211. /*.buffer = */ buffer,
  212. /*.buffer_owned = */ true,
  213. /*.base = */ ggml_backend_buffer_get_base(buffer),
  214. /*.alignment = */ alignment,
  215. /*.n_free_blocks = */ 0,
  216. /*.free_blocks = */ {{0}},
  217. /*.max_size = */ 0,
  218. /*.measure = */ false,
  219. #ifdef GGML_ALLOCATOR_DEBUG
  220. /*.allocated_tensors = */ {0},
  221. #endif
  222. };
  223. ggml_tallocr_reset(alloc);
  224. return alloc;
  225. }
  226. ggml_tallocr_t ggml_tallocr_new_measure(size_t alignment) {
  227. ggml_tallocr_t alloc = ggml_tallocr_new((void *)0x1000, SIZE_MAX/2, alignment);
  228. alloc->measure = true;
  229. return alloc;
  230. }
  231. ggml_tallocr_t ggml_tallocr_new_measure_from_backend(struct ggml_backend * backend) {
  232. // create a backend buffer to get the correct tensor allocation sizes
  233. ggml_backend_buffer_t buffer = ggml_backend_alloc_buffer(backend, 1);
  234. // TODO: move alloc initialization to a common ggml_tallocr_new_impl function
  235. ggml_tallocr_t alloc = ggml_tallocr_new_from_buffer(buffer);
  236. alloc->buffer_owned = true;
  237. alloc->measure = true;
  238. ggml_tallocr_reset(alloc);
  239. return alloc;
  240. }
  241. ggml_tallocr_t ggml_tallocr_new_from_backend(struct ggml_backend * backend, size_t size) {
  242. ggml_backend_buffer_t buffer = ggml_backend_alloc_buffer(backend, size);
  243. ggml_tallocr_t alloc = ggml_tallocr_new_from_buffer(buffer);
  244. alloc->buffer_owned = true;
  245. return alloc;
  246. }
  247. ggml_tallocr_t ggml_tallocr_new_from_buffer(struct ggml_backend_buffer * buffer) {
  248. ggml_tallocr_t alloc = (ggml_tallocr_t)malloc(sizeof(struct ggml_tallocr));
  249. *alloc = (struct ggml_tallocr) {
  250. /*.buffer = */ buffer,
  251. /*.buffer_owned = */ false,
  252. /*.base = */ ggml_backend_buffer_get_base(buffer),
  253. /*.alignment = */ ggml_backend_buffer_get_alignment(buffer),
  254. /*.n_free_blocks = */ 0,
  255. /*.free_blocks = */ {{0}},
  256. /*.max_size = */ 0,
  257. /*.measure = */ false,
  258. #ifdef GGML_ALLOCATOR_DEBUG
  259. /*.allocated_tensors = */ {0},
  260. #endif
  261. };
  262. ggml_tallocr_reset(alloc);
  263. return alloc;
  264. }
  265. struct ggml_backend_buffer * ggml_tallocr_get_buffer(ggml_tallocr_t alloc) {
  266. return alloc->buffer;
  267. }
  268. void ggml_tallocr_free(ggml_tallocr_t alloc) {
  269. if (alloc == NULL) {
  270. return;
  271. }
  272. if (alloc->buffer_owned) {
  273. ggml_backend_buffer_free(alloc->buffer);
  274. }
  275. free(alloc);
  276. }
  277. bool ggml_tallocr_is_measure(ggml_tallocr_t alloc) {
  278. return alloc->measure;
  279. }
  280. size_t ggml_tallocr_max_size(ggml_tallocr_t alloc) {
  281. return alloc->max_size;
  282. }
  283. // graph allocator
  284. struct hash_node {
  285. int n_children;
  286. int n_views;
  287. };
  288. struct ggml_gallocr {
  289. ggml_tallocr_t talloc;
  290. struct ggml_hash_set hash_set;
  291. struct hash_node * hash_values;
  292. size_t hash_values_size;
  293. ggml_tallocr_t * hash_allocs;
  294. int * parse_seq;
  295. int parse_seq_len;
  296. };
  297. ggml_gallocr_t ggml_gallocr_new(void) {
  298. ggml_gallocr_t galloc = (ggml_gallocr_t)malloc(sizeof(struct ggml_gallocr));
  299. *galloc = (struct ggml_gallocr) {
  300. /*.talloc = */ NULL,
  301. /*.hash_set = */ {0},
  302. /*.hash_values = */ NULL,
  303. /*.hash_values_size = */ 0,
  304. /*.hash_allocs = */ NULL,
  305. /*.parse_seq = */ NULL,
  306. /*.parse_seq_len = */ 0,
  307. };
  308. return galloc;
  309. }
  310. void ggml_gallocr_free(ggml_gallocr_t galloc) {
  311. if (galloc == NULL) {
  312. return;
  313. }
  314. if (galloc->hash_set.keys != NULL) {
  315. free(galloc->hash_set.keys);
  316. }
  317. if (galloc->hash_values != NULL) {
  318. free(galloc->hash_values);
  319. }
  320. if (galloc->hash_allocs != NULL) {
  321. free(galloc->hash_allocs);
  322. }
  323. if (galloc->parse_seq != NULL) {
  324. free(galloc->parse_seq);
  325. }
  326. free(galloc);
  327. }
  328. void ggml_gallocr_set_parse_seq(ggml_gallocr_t galloc, const int * list, int n) {
  329. free(galloc->parse_seq);
  330. galloc->parse_seq = malloc(sizeof(int) * n);
  331. for (int i = 0; i < n; i++) {
  332. galloc->parse_seq[i] = list[i];
  333. }
  334. galloc->parse_seq_len = n;
  335. }
  336. static struct hash_node * hash_get(ggml_gallocr_t galloc, struct ggml_tensor * t) {
  337. size_t i = ggml_hash_find_or_insert(galloc->hash_set, t);
  338. return &galloc->hash_values[i];
  339. }
  340. static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {
  341. if (a->type != b->type) {
  342. return false;
  343. }
  344. for (int i = 0; i < GGML_MAX_DIMS; i++) {
  345. if (a->ne[i] != b->ne[i]) {
  346. return false;
  347. }
  348. if (a->nb[i] != b->nb[i]) {
  349. return false;
  350. }
  351. }
  352. return true;
  353. }
  354. static bool ggml_op_can_inplace(enum ggml_op op) {
  355. switch (op) {
  356. case GGML_OP_SCALE:
  357. case GGML_OP_DIAG_MASK_ZERO:
  358. case GGML_OP_DIAG_MASK_INF:
  359. case GGML_OP_ADD:
  360. case GGML_OP_ADD1:
  361. case GGML_OP_SUB:
  362. case GGML_OP_MUL:
  363. case GGML_OP_DIV:
  364. case GGML_OP_SQR:
  365. case GGML_OP_SQRT:
  366. case GGML_OP_LOG:
  367. case GGML_OP_UNARY:
  368. case GGML_OP_ROPE:
  369. case GGML_OP_RMS_NORM:
  370. case GGML_OP_SOFT_MAX:
  371. return true;
  372. default:
  373. return false;
  374. }
  375. }
  376. static ggml_tallocr_t node_tallocr(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  377. if (galloc->talloc != NULL) {
  378. return galloc->talloc;
  379. }
  380. return galloc->hash_allocs[ggml_hash_find_or_insert(galloc->hash_set, node)];
  381. }
  382. static void init_view(ggml_gallocr_t galloc, struct ggml_tensor * view, bool update_backend) {
  383. ggml_tallocr_t alloc = node_tallocr(galloc, view);
  384. //printf("init_view: %s from src %s\n", view->name, view->view_src->name);
  385. GGML_ASSERT(view->view_src != NULL && view->view_src->data != NULL);
  386. if (update_backend) {
  387. view->backend = view->view_src->backend;
  388. }
  389. view->buffer = view->view_src->buffer;
  390. view->data = (char *)view->view_src->data + view->view_offs;
  391. // FIXME: the view should be initialized by the owning buffer, but currently this breaks the CUDA backend
  392. // due to the ggml_tensor_extra_gpu ring buffer overwriting the KV cache extras
  393. assert(ggml_tallocr_is_measure(alloc) || !view->buffer || view->buffer->backend == alloc->buffer->backend);
  394. if (!alloc->measure) {
  395. ggml_backend_buffer_init_tensor(alloc->buffer, view);
  396. }
  397. }
  398. static void allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  399. ggml_tallocr_t alloc = node_tallocr(galloc, node);
  400. if (node->data == NULL) {
  401. if (ggml_is_view(node)) {
  402. init_view(galloc, node, true);
  403. } else {
  404. // see if we can reuse a parent's buffer (inplace)
  405. if (ggml_op_can_inplace(node->op)) {
  406. for (int i = 0; i < GGML_MAX_SRC; i++) {
  407. struct ggml_tensor * parent = node->src[i];
  408. if (parent == NULL) {
  409. break;
  410. }
  411. // if the node's data is external, then we cannot re-use it
  412. if (ggml_tallocr_is_own(alloc, parent) == false) {
  413. AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data);
  414. continue;
  415. }
  416. struct hash_node * p_hn = hash_get(galloc, parent);
  417. if (parent->data != NULL && p_hn->n_children == 1 && p_hn->n_views == 0 && ggml_are_same_layout(node, parent)) {
  418. if (ggml_is_view(parent)) {
  419. struct ggml_tensor * view_src = parent->view_src;
  420. struct hash_node * view_src_hn = hash_get(galloc, view_src);
  421. if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) {
  422. // TODO: the offset of the view parent must be kept to ensure that the op doesn't overwrite
  423. // the parent's data that it will need later (same layout requirement). the problem is that then
  424. // we cannot free the tensor because the original address of the allocation is lost.
  425. // adding a view_src pointer to the tensor would solve this and simplify the code dealing with views
  426. // for now, we only reuse the parent's data if the offset is zero (view_src->data == parent->data)
  427. AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name);
  428. node->view_src = view_src;
  429. view_src_hn->n_views += 1;
  430. init_view(galloc, node, false);
  431. return;
  432. }
  433. } else {
  434. AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name);
  435. node->view_src = parent;
  436. p_hn->n_views += 1;
  437. init_view(galloc, node, false);
  438. return;
  439. }
  440. }
  441. }
  442. }
  443. ggml_tallocr_alloc(alloc, node);
  444. }
  445. }
  446. }
  447. static void free_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  448. ggml_tallocr_t alloc = node_tallocr(galloc, node);
  449. ggml_tallocr_free_tensor(alloc, node);
  450. }
  451. static void ggml_tallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgraph * gf) {
  452. const int * parse_seq = galloc->parse_seq;
  453. int parse_seq_len = galloc->parse_seq_len;
  454. // count number of children and views
  455. for (int i = 0; i < gf->n_nodes; i++) {
  456. struct ggml_tensor * node = gf->nodes[i];
  457. if (ggml_is_view(node)) {
  458. struct ggml_tensor * view_src = node->view_src;
  459. hash_get(galloc, view_src)->n_views += 1;
  460. if (node->buffer == NULL && node->data != NULL) {
  461. // view of a pre-allocated tensor, didn't call init_view() yet
  462. init_view(galloc, node, true);
  463. }
  464. }
  465. for (int j = 0; j < GGML_MAX_SRC; j++) {
  466. struct ggml_tensor * parent = node->src[j];
  467. if (parent == NULL) {
  468. break;
  469. }
  470. hash_get(galloc, parent)->n_children += 1;
  471. if (ggml_is_view(parent) && parent->buffer == NULL && parent->data != NULL) {
  472. init_view(galloc, parent, true);
  473. }
  474. }
  475. }
  476. // allocate tensors
  477. // if we have parse_seq then we allocate nodes following the list, and we only free nodes at barriers
  478. int last_barrier_pos = 0;
  479. int n_nodes = parse_seq_len ? parse_seq_len : gf->n_nodes;
  480. for (int ind = 0; ind < n_nodes; ind++) {
  481. // allocate a node if there is no parse_seq or this is not a barrier
  482. if (parse_seq_len == 0 || parse_seq[ind] != -1) {
  483. int i = parse_seq_len ? parse_seq[ind] : ind;
  484. struct ggml_tensor * node = gf->nodes[i];
  485. // allocate parents (leafs)
  486. for (int j = 0; j < GGML_MAX_SRC; j++) {
  487. struct ggml_tensor * parent = node->src[j];
  488. if (parent == NULL) {
  489. break;
  490. }
  491. allocate_node(galloc, parent);
  492. }
  493. // allocate node
  494. allocate_node(galloc, node);
  495. AT_PRINTF("exec: %s (%s) <= ", ggml_op_name(node->op), node->name);
  496. for (int j = 0; j < GGML_MAX_SRC; j++) {
  497. struct ggml_tensor * parent = node->src[j];
  498. if (parent == NULL) {
  499. break;
  500. }
  501. AT_PRINTF("%s", parent->name);
  502. if (j < GGML_MAX_SRC - 1 && node->src[j + 1] != NULL) {
  503. AT_PRINTF(", ");
  504. }
  505. }
  506. AT_PRINTF("\n");
  507. }
  508. // update parents
  509. // update immediately if there is no parse_seq
  510. // update only at barriers if there is parse_seq
  511. if ((parse_seq_len == 0) || parse_seq[ind] == -1) {
  512. int update_start = parse_seq_len ? last_barrier_pos : ind;
  513. int update_end = parse_seq_len ? ind : ind + 1;
  514. for (int i = update_start; i < update_end; i++) {
  515. int node_i = parse_seq_len ? parse_seq[i] : i;
  516. struct ggml_tensor * node = gf->nodes[node_i];
  517. for (int j = 0; j < GGML_MAX_SRC; j++) {
  518. struct ggml_tensor * parent = node->src[j];
  519. if (parent == NULL) {
  520. break;
  521. }
  522. struct hash_node * p_hn = hash_get(galloc, parent);
  523. p_hn->n_children -= 1;
  524. //AT_PRINTF("parent %s: %d children, %d views\n", parent->name, parent->n_children, parent->n_views);
  525. if (p_hn->n_children == 0 && p_hn->n_views == 0) {
  526. if (ggml_is_view(parent)) {
  527. struct ggml_tensor * view_src = parent->view_src;
  528. struct hash_node * view_src_hn = hash_get(galloc, view_src);
  529. view_src_hn->n_views -= 1;
  530. AT_PRINTF("view_src %s: %d children, %d views\n", view_src->name, view_src_hn->n_children, view_src_hn->n_views);
  531. if (view_src_hn->n_views == 0 && view_src_hn->n_children == 0) {
  532. free_node(galloc, view_src);
  533. }
  534. }
  535. else {
  536. free_node(galloc, parent);
  537. }
  538. }
  539. }
  540. }
  541. AT_PRINTF("\n");
  542. if (parse_seq_len) {
  543. last_barrier_pos = ind + 1;
  544. }
  545. }
  546. }
  547. }
  548. size_t ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, ggml_tallocr_t talloc, struct ggml_cgraph * graph) {
  549. size_t hash_size = graph->visited_hash_table.size;
  550. // check if the hash table is initialized and large enough
  551. if (galloc->hash_set.size < hash_size) {
  552. if (galloc->hash_set.keys != NULL) {
  553. free(galloc->hash_set.keys);
  554. }
  555. if (galloc->hash_values != NULL) {
  556. free(galloc->hash_values);
  557. }
  558. galloc->hash_set.keys = malloc(sizeof(struct ggml_tensor *) * hash_size);
  559. galloc->hash_set.size = hash_size;
  560. galloc->hash_values = malloc(sizeof(struct hash_node) * hash_size);
  561. }
  562. // reset hash table
  563. memset(galloc->hash_set.keys, 0, sizeof(struct ggml_tensor *) * hash_size);
  564. memset(galloc->hash_values, 0, sizeof(struct hash_node) * hash_size);
  565. galloc->talloc = talloc;
  566. ggml_tallocr_alloc_graph_impl(galloc, graph);
  567. galloc->talloc = NULL;
  568. size_t max_size = ggml_tallocr_max_size(talloc);
  569. return max_size;
  570. }
  571. 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) {
  572. const size_t hash_size = hash_set.size;
  573. GGML_ASSERT(hash_size >= (size_t)(graph->n_nodes + graph->n_leafs));
  574. galloc->talloc = NULL;
  575. // alloc hash_values if needed
  576. if (galloc->hash_values == NULL || galloc->hash_values_size < hash_size) {
  577. free(galloc->hash_values);
  578. galloc->hash_values = malloc(sizeof(struct hash_node) * hash_size);
  579. galloc->hash_values_size = hash_size;
  580. }
  581. // free hash_set.keys if needed
  582. if (galloc->hash_set.keys != NULL) {
  583. free(galloc->hash_set.keys);
  584. }
  585. galloc->hash_set = hash_set;
  586. // reset hash values
  587. memset(galloc->hash_values, 0, sizeof(struct hash_node) * hash_size);
  588. galloc->hash_allocs = hash_node_talloc;
  589. ggml_tallocr_alloc_graph_impl(galloc, graph);
  590. // remove unowned resources
  591. galloc->hash_set.keys = NULL;
  592. galloc->hash_allocs = NULL;
  593. }
  594. // legacy API wrapper
  595. struct ggml_allocr {
  596. ggml_tallocr_t talloc;
  597. ggml_gallocr_t galloc;
  598. };
  599. static ggml_allocr_t ggml_allocr_new_impl(ggml_tallocr_t talloc) {
  600. ggml_allocr_t alloc = (ggml_allocr_t)malloc(sizeof(struct ggml_allocr));
  601. *alloc = (struct ggml_allocr) {
  602. /*.talloc = */ talloc,
  603. /*.galloc = */ ggml_gallocr_new(),
  604. };
  605. return alloc;
  606. }
  607. ggml_allocr_t ggml_allocr_new(void * data, size_t size, size_t alignment) {
  608. return ggml_allocr_new_impl(ggml_tallocr_new(data, size, alignment));
  609. }
  610. ggml_allocr_t ggml_allocr_new_measure(size_t alignment) {
  611. return ggml_allocr_new_impl(ggml_tallocr_new_measure(alignment));
  612. }
  613. ggml_allocr_t ggml_allocr_new_from_buffer(struct ggml_backend_buffer * buffer) {
  614. return ggml_allocr_new_impl(ggml_tallocr_new_from_buffer(buffer));
  615. }
  616. ggml_allocr_t ggml_allocr_new_from_backend(struct ggml_backend * backend, size_t size) {
  617. return ggml_allocr_new_impl(ggml_tallocr_new_from_backend(backend, size));
  618. }
  619. ggml_allocr_t ggml_allocr_new_measure_from_backend(struct ggml_backend * backend) {
  620. return ggml_allocr_new_impl(ggml_tallocr_new_measure_from_backend(backend));
  621. }
  622. struct ggml_backend_buffer * ggml_allocr_get_buffer(ggml_allocr_t alloc) {
  623. return ggml_tallocr_get_buffer(alloc->talloc);
  624. }
  625. void ggml_allocr_set_parse_seq(ggml_allocr_t alloc, const int * list, int n) {
  626. ggml_gallocr_set_parse_seq(alloc->galloc, list, n);
  627. }
  628. void ggml_allocr_free(ggml_allocr_t alloc) {
  629. ggml_gallocr_free(alloc->galloc);
  630. ggml_tallocr_free(alloc->talloc);
  631. free(alloc);
  632. }
  633. bool ggml_allocr_is_measure(ggml_allocr_t alloc) {
  634. return ggml_tallocr_is_measure(alloc->talloc);
  635. }
  636. void ggml_allocr_reset(ggml_allocr_t alloc) {
  637. ggml_tallocr_reset(alloc->talloc);
  638. }
  639. void ggml_allocr_alloc(ggml_allocr_t alloc, struct ggml_tensor * tensor) {
  640. ggml_tallocr_alloc(alloc->talloc, tensor);
  641. }
  642. size_t ggml_allocr_max_size(ggml_allocr_t alloc) {
  643. return ggml_tallocr_max_size(alloc->talloc);
  644. }
  645. size_t ggml_allocr_alloc_graph(ggml_allocr_t alloc, struct ggml_cgraph * graph) {
  646. return ggml_gallocr_alloc_graph(alloc->galloc, alloc->talloc, graph);
  647. }