ggml-alloc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. // views are initialized in the alloc buffer rather than the view_src buffer
  386. view->buffer = alloc->buffer;
  387. view->data = (char *)view->view_src->data + view->view_offs;
  388. assert(ggml_tallocr_is_measure(alloc) || !view->buffer || view->buffer->buft == alloc->buffer->buft);
  389. if (!alloc->measure) {
  390. ggml_backend_buffer_init_tensor(alloc->buffer, view);
  391. }
  392. }
  393. static void allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  394. ggml_tallocr_t alloc = node_tallocr(galloc, node);
  395. if (node->data == NULL) {
  396. if (ggml_is_view(node)) {
  397. init_view(galloc, node, true);
  398. } else {
  399. // see if we can reuse a parent's buffer (inplace)
  400. if (ggml_op_can_inplace(node->op)) {
  401. for (int i = 0; i < GGML_MAX_SRC; i++) {
  402. struct ggml_tensor * parent = node->src[i];
  403. if (parent == NULL) {
  404. break;
  405. }
  406. // if the node's data is external, then we cannot re-use it
  407. if (ggml_tallocr_is_own(alloc, parent) == false) {
  408. AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data);
  409. continue;
  410. }
  411. struct hash_node * p_hn = hash_get(galloc, parent);
  412. if (parent->data != NULL && p_hn->n_children == 1 && p_hn->n_views == 0 && ggml_are_same_layout(node, parent)) {
  413. if (ggml_is_view(parent)) {
  414. struct ggml_tensor * view_src = parent->view_src;
  415. struct hash_node * view_src_hn = hash_get(galloc, view_src);
  416. if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) {
  417. // TODO: the offset of the view parent must be kept to ensure that the op doesn't overwrite
  418. // the parent's data that it will need later (same layout requirement). the problem is that then
  419. // we cannot free the tensor because the original address of the allocation is lost.
  420. // adding a view_src pointer to the tensor would solve this and simplify the code dealing with views
  421. // for now, we only reuse the parent's data if the offset is zero (view_src->data == parent->data)
  422. AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name);
  423. node->view_src = view_src;
  424. view_src_hn->n_views += 1;
  425. init_view(galloc, node, false);
  426. return;
  427. }
  428. } else {
  429. AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name);
  430. node->view_src = parent;
  431. p_hn->n_views += 1;
  432. init_view(galloc, node, false);
  433. return;
  434. }
  435. }
  436. }
  437. }
  438. ggml_tallocr_alloc(alloc, node);
  439. }
  440. }
  441. }
  442. static void free_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
  443. ggml_tallocr_t alloc = node_tallocr(galloc, node);
  444. ggml_tallocr_free_tensor(alloc, node);
  445. }
  446. static void ggml_tallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgraph * gf) {
  447. const int * parse_seq = galloc->parse_seq;
  448. int parse_seq_len = galloc->parse_seq_len;
  449. // count number of children and views
  450. for (int i = 0; i < gf->n_nodes; i++) {
  451. struct ggml_tensor * node = gf->nodes[i];
  452. if (ggml_is_view(node)) {
  453. struct ggml_tensor * view_src = node->view_src;
  454. hash_get(galloc, view_src)->n_views += 1;
  455. if (node->buffer == NULL && node->data != NULL) {
  456. // view of a pre-allocated tensor, didn't call init_view() yet
  457. init_view(galloc, node, true);
  458. }
  459. }
  460. for (int j = 0; j < GGML_MAX_SRC; j++) {
  461. struct ggml_tensor * parent = node->src[j];
  462. if (parent == NULL) {
  463. break;
  464. }
  465. hash_get(galloc, parent)->n_children += 1;
  466. if (ggml_is_view(parent) && parent->buffer == NULL && parent->data != NULL) {
  467. init_view(galloc, parent, true);
  468. }
  469. }
  470. }
  471. // allocate tensors
  472. // if we have parse_seq then we allocate nodes following the list, and we only free nodes at barriers
  473. int last_barrier_pos = 0;
  474. int n_nodes = parse_seq_len ? parse_seq_len : gf->n_nodes;
  475. for (int ind = 0; ind < n_nodes; ind++) {
  476. // allocate a node if there is no parse_seq or this is not a barrier
  477. if (parse_seq_len == 0 || parse_seq[ind] != -1) {
  478. int i = parse_seq_len ? parse_seq[ind] : ind;
  479. struct ggml_tensor * node = gf->nodes[i];
  480. // allocate parents (leafs)
  481. for (int j = 0; j < GGML_MAX_SRC; j++) {
  482. struct ggml_tensor * parent = node->src[j];
  483. if (parent == NULL) {
  484. break;
  485. }
  486. allocate_node(galloc, parent);
  487. }
  488. // allocate node
  489. allocate_node(galloc, node);
  490. AT_PRINTF("exec: %s (%s) <= ", ggml_op_name(node->op), node->name);
  491. for (int j = 0; j < GGML_MAX_SRC; j++) {
  492. struct ggml_tensor * parent = node->src[j];
  493. if (parent == NULL) {
  494. break;
  495. }
  496. AT_PRINTF("%s", parent->name);
  497. if (j < GGML_MAX_SRC - 1 && node->src[j + 1] != NULL) {
  498. AT_PRINTF(", ");
  499. }
  500. }
  501. AT_PRINTF("\n");
  502. }
  503. // update parents
  504. // update immediately if there is no parse_seq
  505. // update only at barriers if there is parse_seq
  506. if ((parse_seq_len == 0) || parse_seq[ind] == -1) {
  507. int update_start = parse_seq_len ? last_barrier_pos : ind;
  508. int update_end = parse_seq_len ? ind : ind + 1;
  509. for (int i = update_start; i < update_end; i++) {
  510. int node_i = parse_seq_len ? parse_seq[i] : i;
  511. struct ggml_tensor * node = gf->nodes[node_i];
  512. for (int j = 0; j < GGML_MAX_SRC; j++) {
  513. struct ggml_tensor * parent = node->src[j];
  514. if (parent == NULL) {
  515. break;
  516. }
  517. struct hash_node * p_hn = hash_get(galloc, parent);
  518. p_hn->n_children -= 1;
  519. //AT_PRINTF("parent %s: %d children, %d views\n", parent->name, parent->n_children, parent->n_views);
  520. if (p_hn->n_children == 0 && p_hn->n_views == 0) {
  521. if (ggml_is_view(parent)) {
  522. struct ggml_tensor * view_src = parent->view_src;
  523. struct hash_node * view_src_hn = hash_get(galloc, view_src);
  524. view_src_hn->n_views -= 1;
  525. AT_PRINTF("view_src %s: %d children, %d views\n", view_src->name, view_src_hn->n_children, view_src_hn->n_views);
  526. if (view_src_hn->n_views == 0 && view_src_hn->n_children == 0) {
  527. free_node(galloc, view_src);
  528. }
  529. }
  530. else {
  531. free_node(galloc, parent);
  532. }
  533. }
  534. }
  535. }
  536. AT_PRINTF("\n");
  537. if (parse_seq_len) {
  538. last_barrier_pos = ind + 1;
  539. }
  540. }
  541. }
  542. }
  543. size_t ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, ggml_tallocr_t talloc, struct ggml_cgraph * graph) {
  544. size_t hash_size = graph->visited_hash_table.size;
  545. // check if the hash table is initialized and large enough
  546. if (galloc->hash_set.size < hash_size) {
  547. if (galloc->hash_set.keys != NULL) {
  548. free(galloc->hash_set.keys);
  549. }
  550. if (galloc->hash_values != NULL) {
  551. free(galloc->hash_values);
  552. }
  553. galloc->hash_set.keys = malloc(sizeof(struct ggml_tensor *) * hash_size);
  554. galloc->hash_set.size = hash_size;
  555. galloc->hash_values = malloc(sizeof(struct hash_node) * hash_size);
  556. }
  557. // reset hash table
  558. memset(galloc->hash_set.keys, 0, sizeof(struct ggml_tensor *) * hash_size);
  559. memset(galloc->hash_values, 0, sizeof(struct hash_node) * hash_size);
  560. galloc->talloc = talloc;
  561. ggml_tallocr_alloc_graph_impl(galloc, graph);
  562. galloc->talloc = NULL;
  563. size_t max_size = ggml_tallocr_max_size(talloc);
  564. return max_size;
  565. }
  566. 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) {
  567. const size_t hash_size = hash_set.size;
  568. GGML_ASSERT(hash_size >= (size_t)(graph->n_nodes + graph->n_leafs));
  569. galloc->talloc = NULL;
  570. // alloc hash_values if needed
  571. if (galloc->hash_values == NULL || galloc->hash_values_size < hash_size) {
  572. free(galloc->hash_values);
  573. galloc->hash_values = malloc(sizeof(struct hash_node) * hash_size);
  574. galloc->hash_values_size = hash_size;
  575. }
  576. // free hash_set.keys if needed
  577. if (galloc->hash_set.keys != NULL) {
  578. free(galloc->hash_set.keys);
  579. }
  580. galloc->hash_set = hash_set;
  581. // reset hash values
  582. memset(galloc->hash_values, 0, sizeof(struct hash_node) * hash_size);
  583. galloc->hash_allocs = hash_node_talloc;
  584. ggml_tallocr_alloc_graph_impl(galloc, graph);
  585. // remove unowned resources
  586. galloc->hash_set.keys = NULL;
  587. galloc->hash_allocs = NULL;
  588. }
  589. // legacy API wrapper
  590. struct ggml_allocr {
  591. ggml_tallocr_t talloc;
  592. ggml_gallocr_t galloc;
  593. };
  594. static ggml_allocr_t ggml_allocr_new_impl(ggml_tallocr_t talloc) {
  595. ggml_allocr_t alloc = (ggml_allocr_t)malloc(sizeof(struct ggml_allocr));
  596. *alloc = (struct ggml_allocr) {
  597. /*.talloc = */ talloc,
  598. /*.galloc = */ ggml_gallocr_new(),
  599. };
  600. return alloc;
  601. }
  602. ggml_allocr_t ggml_allocr_new(void * data, size_t size, size_t alignment) {
  603. return ggml_allocr_new_impl(ggml_tallocr_new(data, size, alignment));
  604. }
  605. ggml_allocr_t ggml_allocr_new_measure(size_t alignment) {
  606. return ggml_allocr_new_impl(ggml_tallocr_new_measure(alignment));
  607. }
  608. ggml_allocr_t ggml_allocr_new_from_buffer(struct ggml_backend_buffer * buffer) {
  609. return ggml_allocr_new_impl(ggml_tallocr_new_from_buffer(buffer));
  610. }
  611. ggml_allocr_t ggml_allocr_new_from_backend(struct ggml_backend * backend, size_t size) {
  612. return ggml_allocr_new_impl(ggml_tallocr_new_from_backend(backend, size));
  613. }
  614. ggml_allocr_t ggml_allocr_new_measure_from_backend(struct ggml_backend * backend) {
  615. return ggml_allocr_new_impl(ggml_tallocr_new_measure_from_backend(backend));
  616. }
  617. struct ggml_backend_buffer * ggml_allocr_get_buffer(ggml_allocr_t alloc) {
  618. return ggml_tallocr_get_buffer(alloc->talloc);
  619. }
  620. void ggml_allocr_set_parse_seq(ggml_allocr_t alloc, const int * list, int n) {
  621. ggml_gallocr_set_parse_seq(alloc->galloc, list, n);
  622. }
  623. void ggml_allocr_free(ggml_allocr_t alloc) {
  624. if (alloc == NULL) {
  625. return;
  626. }
  627. ggml_gallocr_free(alloc->galloc);
  628. ggml_tallocr_free(alloc->talloc);
  629. free(alloc);
  630. }
  631. bool ggml_allocr_is_measure(ggml_allocr_t alloc) {
  632. return ggml_tallocr_is_measure(alloc->talloc);
  633. }
  634. void ggml_allocr_reset(ggml_allocr_t alloc) {
  635. ggml_tallocr_reset(alloc->talloc);
  636. }
  637. void ggml_allocr_alloc(ggml_allocr_t alloc, struct ggml_tensor * tensor) {
  638. ggml_tallocr_alloc(alloc->talloc, tensor);
  639. }
  640. size_t ggml_allocr_max_size(ggml_allocr_t alloc) {
  641. return ggml_tallocr_max_size(alloc->talloc);
  642. }
  643. size_t ggml_allocr_alloc_graph(ggml_allocr_t alloc, struct ggml_cgraph * graph) {
  644. return ggml_gallocr_alloc_graph(alloc->galloc, alloc->talloc, graph);
  645. }
  646. // utils
  647. ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) {
  648. GGML_ASSERT(ggml_get_no_alloc(ctx) == true);
  649. size_t alignment = ggml_backend_buft_get_alignment(buft);
  650. size_t nbytes = 0;
  651. for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  652. if (t->data == NULL && t->view_src == NULL) {
  653. nbytes += GGML_PAD(ggml_backend_buft_get_alloc_size(buft, t), alignment);
  654. }
  655. }
  656. if (nbytes == 0) {
  657. // all the tensors in the context are already allocated
  658. return NULL;
  659. }
  660. ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buft, nbytes);
  661. ggml_tallocr_t tallocr = ggml_tallocr_new_from_buffer(buffer);
  662. for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  663. if (t->data == NULL) {
  664. if (t->view_src == NULL) {
  665. ggml_tallocr_alloc(tallocr, t);
  666. } else {
  667. ggml_backend_view_init(buffer, t);
  668. }
  669. } else {
  670. if (t->view_src != NULL) {
  671. // view of a pre-allocated tensor
  672. ggml_backend_view_init(buffer, t);
  673. }
  674. }
  675. }
  676. ggml_tallocr_free(tallocr);
  677. return buffer;
  678. }
  679. ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, ggml_backend_t backend) {
  680. return ggml_backend_alloc_ctx_tensors_from_buft(ctx, ggml_backend_get_default_buffer_type(backend));
  681. }