ggml-backend.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. #include "ggml-backend-impl.h"
  2. #include "ggml-alloc.h"
  3. #include "ggml-impl.h"
  4. #include <assert.h>
  5. #include <limits.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  11. // backend buffer type
  12. const char * ggml_backend_buft_name(ggml_backend_buffer_type_t buft) {
  13. return buft->iface.get_name(buft);
  14. }
  15. ggml_backend_buffer_t ggml_backend_buft_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  16. return buft->iface.alloc_buffer(buft, size);
  17. }
  18. size_t ggml_backend_buft_get_alignment(ggml_backend_buffer_type_t buft) {
  19. return buft->iface.get_alignment(buft);
  20. }
  21. size_t ggml_backend_buft_get_alloc_size(ggml_backend_buffer_type_t buft, struct ggml_tensor * tensor) {
  22. // get_alloc_size is optional, defaults to ggml_nbytes
  23. if (buft->iface.get_alloc_size) {
  24. return buft->iface.get_alloc_size(buft, tensor);
  25. }
  26. return ggml_nbytes(tensor);
  27. }
  28. bool ggml_backend_buft_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  29. return buft->iface.supports_backend(buft, backend);
  30. }
  31. bool ggml_backend_buft_is_host(ggml_backend_buffer_type_t buft) {
  32. if (buft->iface.is_host) {
  33. return buft->iface.is_host(buft);
  34. }
  35. return false;
  36. }
  37. // backend buffer
  38. ggml_backend_buffer_t ggml_backend_buffer_init(
  39. ggml_backend_buffer_type_t buft,
  40. struct ggml_backend_buffer_i iface,
  41. ggml_backend_buffer_context_t context,
  42. size_t size) {
  43. ggml_backend_buffer_t buffer = malloc(sizeof(struct ggml_backend_buffer));
  44. GGML_ASSERT(iface.get_base != NULL);
  45. (*buffer) = (struct ggml_backend_buffer) {
  46. /* .interface = */ iface,
  47. /* .buft = */ buft,
  48. /* .context = */ context,
  49. /* .size = */ size,
  50. /* .usage = */ GGML_BACKEND_BUFFER_USAGE_ANY
  51. };
  52. return buffer;
  53. }
  54. const char * ggml_backend_buffer_name(ggml_backend_buffer_t buffer) {
  55. return buffer->iface.get_name(buffer);
  56. }
  57. void ggml_backend_buffer_free(ggml_backend_buffer_t buffer) {
  58. if (buffer == NULL) {
  59. return;
  60. }
  61. if (buffer->iface.free_buffer != NULL) {
  62. buffer->iface.free_buffer(buffer);
  63. }
  64. free(buffer);
  65. }
  66. size_t ggml_backend_buffer_get_size(ggml_backend_buffer_t buffer) {
  67. return buffer->size;
  68. }
  69. void * ggml_backend_buffer_get_base(ggml_backend_buffer_t buffer) {
  70. void * base = buffer->iface.get_base(buffer);
  71. GGML_ASSERT(base != NULL && "backend buffer base cannot be NULL");
  72. return base;
  73. }
  74. void ggml_backend_buffer_init_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) {
  75. // init_tensor is optional
  76. if (buffer->iface.init_tensor) {
  77. buffer->iface.init_tensor(buffer, tensor);
  78. }
  79. }
  80. size_t ggml_backend_buffer_get_alignment (ggml_backend_buffer_t buffer) {
  81. return ggml_backend_buft_get_alignment(ggml_backend_buffer_get_type(buffer));
  82. }
  83. size_t ggml_backend_buffer_get_alloc_size(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) {
  84. return ggml_backend_buft_get_alloc_size(ggml_backend_buffer_get_type(buffer), tensor);
  85. }
  86. void ggml_backend_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  87. buffer->iface.clear(buffer, value);
  88. }
  89. bool ggml_backend_buffer_is_host(ggml_backend_buffer_t buffer) {
  90. return ggml_backend_buft_is_host(ggml_backend_buffer_get_type(buffer));
  91. }
  92. void ggml_backend_buffer_set_usage(ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage) {
  93. buffer->usage = usage;
  94. }
  95. ggml_backend_buffer_type_t ggml_backend_buffer_get_type(ggml_backend_buffer_t buffer) {
  96. return buffer->buft;
  97. }
  98. void ggml_backend_buffer_reset(ggml_backend_buffer_t buffer) {
  99. if (buffer->iface.reset) {
  100. buffer->iface.reset(buffer);
  101. }
  102. }
  103. bool ggml_backend_buffer_copy_tensor(const struct ggml_tensor * src, struct ggml_tensor * dst) {
  104. ggml_backend_buffer_t dst_buf = dst->view_src ? dst->view_src->buffer : dst->buffer;
  105. if (dst_buf->iface.cpy_tensor) {
  106. return src->buffer->iface.cpy_tensor(dst_buf, src, dst);
  107. }
  108. return false;
  109. }
  110. // backend
  111. const char * ggml_backend_name(ggml_backend_t backend) {
  112. if (backend == NULL) {
  113. return "NULL";
  114. }
  115. return backend->iface.get_name(backend);
  116. }
  117. void ggml_backend_free(ggml_backend_t backend) {
  118. if (backend == NULL) {
  119. return;
  120. }
  121. backend->iface.free(backend);
  122. }
  123. ggml_backend_buffer_type_t ggml_backend_get_default_buffer_type(ggml_backend_t backend) {
  124. return backend->iface.get_default_buffer_type(backend);
  125. }
  126. ggml_backend_buffer_t ggml_backend_alloc_buffer(ggml_backend_t backend, size_t size) {
  127. return ggml_backend_buft_alloc_buffer(ggml_backend_get_default_buffer_type(backend), size);
  128. }
  129. size_t ggml_backend_get_alignment(ggml_backend_t backend) {
  130. return ggml_backend_buft_get_alignment(ggml_backend_get_default_buffer_type(backend));
  131. }
  132. void ggml_backend_tensor_set_async(ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  133. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  134. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor write out of bounds");
  135. if (backend->iface.set_tensor_async == NULL) {
  136. ggml_backend_tensor_set(tensor, data, offset, size);
  137. } else {
  138. backend->iface.set_tensor_async(backend, tensor, data, offset, size);
  139. }
  140. }
  141. void ggml_backend_tensor_get_async(ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  142. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  143. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor read out of bounds");
  144. if (backend->iface.get_tensor_async == NULL) {
  145. ggml_backend_tensor_get(tensor, data, offset, size);
  146. } else {
  147. backend->iface.get_tensor_async(backend, tensor, data, offset, size);
  148. }
  149. }
  150. void ggml_backend_tensor_set(struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  151. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  152. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  153. GGML_ASSERT(buf != NULL && "tensor buffer not set");
  154. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor write out of bounds");
  155. tensor->buffer->iface.set_tensor(buf, tensor, data, offset, size);
  156. }
  157. void ggml_backend_tensor_get(const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  158. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  159. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  160. GGML_ASSERT(tensor->buffer != NULL && "tensor buffer not set");
  161. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor read out of bounds");
  162. tensor->buffer->iface.get_tensor(buf, tensor, data, offset, size);
  163. }
  164. void ggml_backend_synchronize(ggml_backend_t backend) {
  165. if (backend->iface.synchronize == NULL) {
  166. return;
  167. }
  168. backend->iface.synchronize(backend);
  169. }
  170. ggml_backend_graph_plan_t ggml_backend_graph_plan_create(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  171. return backend->iface.graph_plan_create(backend, cgraph);
  172. }
  173. void ggml_backend_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  174. backend->iface.graph_plan_free(backend, plan);
  175. }
  176. void ggml_backend_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  177. backend->iface.graph_plan_compute(backend, plan);
  178. }
  179. bool ggml_backend_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  180. return backend->iface.graph_compute(backend, cgraph);
  181. }
  182. bool ggml_backend_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) {
  183. return backend->iface.supports_op(backend, op);
  184. }
  185. // backend copy
  186. static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {
  187. if (a->type != b->type) {
  188. return false;
  189. }
  190. for (int i = 0; i < GGML_MAX_DIMS; i++) {
  191. if (a->ne[i] != b->ne[i]) {
  192. return false;
  193. }
  194. if (a->nb[i] != b->nb[i]) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. void ggml_backend_tensor_copy(struct ggml_tensor * src, struct ggml_tensor * dst) {
  201. GGML_ASSERT(ggml_are_same_layout(src, dst) && "cannot copy tensors with different layouts");
  202. if (src == dst) {
  203. return;
  204. }
  205. if (ggml_backend_buffer_is_host(src->buffer)) {
  206. ggml_backend_tensor_set(dst, src->data, 0, ggml_nbytes(src));
  207. } else if (ggml_backend_buffer_is_host(dst->buffer)) {
  208. ggml_backend_tensor_get(src, dst->data, 0, ggml_nbytes(src));
  209. } else if (!ggml_backend_buffer_copy_tensor(src, dst)) {
  210. #ifndef NDEBUG
  211. fprintf(stderr, "%s: warning: slow copy from %s to %s\n", __func__, ggml_backend_buffer_name(src->buffer), ggml_backend_buffer_name(dst->buffer));
  212. #endif
  213. size_t nbytes = ggml_nbytes(src);
  214. void * data = malloc(nbytes);
  215. ggml_backend_tensor_get(src, data, 0, nbytes);
  216. ggml_backend_tensor_set(dst, data, 0, nbytes);
  217. free(data);
  218. }
  219. }
  220. void ggml_backend_tensor_copy_async(ggml_backend_t backend, struct ggml_tensor * src, struct ggml_tensor * dst) {
  221. GGML_ASSERT(ggml_are_same_layout(src, dst) && "cannot copy tensors with different layouts");
  222. if (src == dst) {
  223. return;
  224. }
  225. if (ggml_backend_buft_supports_backend(src->buffer->buft, backend) && ggml_backend_buft_supports_backend(dst->buffer->buft, backend)) {
  226. if (backend->iface.cpy_tensor_async != NULL) {
  227. if (backend->iface.cpy_tensor_async(backend, src, dst)) {
  228. return;
  229. }
  230. }
  231. }
  232. size_t nbytes = ggml_nbytes(src);
  233. if (ggml_backend_buffer_is_host(src->buffer)) {
  234. ggml_backend_tensor_set_async(backend, dst, src->data, 0, nbytes);
  235. }
  236. else {
  237. ggml_backend_tensor_copy(src, dst);
  238. }
  239. }
  240. // backend registry
  241. #define GGML_MAX_BACKENDS_REG 16
  242. struct ggml_backend_reg {
  243. char name[128];
  244. ggml_backend_init_fn init_fn;
  245. ggml_backend_buffer_type_t default_buffer_type;
  246. void * user_data;
  247. };
  248. static struct ggml_backend_reg ggml_backend_registry[GGML_MAX_BACKENDS_REG];
  249. static size_t ggml_backend_registry_count = 0;
  250. static ggml_backend_t ggml_backend_reg_cpu_init(const char * params, void * user_data);
  251. static void ggml_backend_registry_init(void) {
  252. static bool initialized = false;
  253. if (initialized) {
  254. return;
  255. }
  256. initialized = true;
  257. ggml_backend_register("CPU", ggml_backend_reg_cpu_init, ggml_backend_cpu_buffer_type(), NULL);
  258. // add forward decls here to avoid including the backend headers
  259. #ifdef GGML_USE_CUBLAS
  260. extern void ggml_backend_cuda_reg_devices(void);
  261. ggml_backend_cuda_reg_devices();
  262. #endif
  263. #ifdef GGML_USE_METAL
  264. extern ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data);
  265. extern ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
  266. ggml_backend_register("Metal", ggml_backend_reg_metal_init, ggml_backend_metal_buffer_type(), NULL);
  267. #endif
  268. }
  269. void ggml_backend_register(const char * name, ggml_backend_init_fn init_fn, ggml_backend_buffer_type_t default_buffer_type, void * user_data) {
  270. GGML_ASSERT(ggml_backend_registry_count < GGML_MAX_BACKENDS_REG);
  271. size_t id = ggml_backend_registry_count;
  272. ggml_backend_registry[id] = (struct ggml_backend_reg) {
  273. /* .name = */ {0},
  274. /* .fn = */ init_fn,
  275. /* .default_buffer_type = */ default_buffer_type,
  276. /* .user_data = */ user_data,
  277. };
  278. snprintf(ggml_backend_registry[id].name, sizeof(ggml_backend_registry[id].name), "%s", name);
  279. #ifndef NDEBUG
  280. fprintf(stderr, "%s: registered backend %s\n", __func__, name);
  281. #endif
  282. ggml_backend_registry_count++;
  283. }
  284. size_t ggml_backend_reg_get_count(void) {
  285. ggml_backend_registry_init();
  286. return ggml_backend_registry_count;
  287. }
  288. size_t ggml_backend_reg_find_by_name(const char * name) {
  289. ggml_backend_registry_init();
  290. for (size_t i = 0; i < ggml_backend_registry_count; i++) {
  291. // TODO: case insensitive in a portable way
  292. if (strcmp(ggml_backend_registry[i].name, name) == 0) {
  293. return i;
  294. }
  295. }
  296. // not found
  297. return SIZE_MAX;
  298. }
  299. // init from backend:params string
  300. ggml_backend_t ggml_backend_reg_init_backend_from_str(const char * backend_str) {
  301. ggml_backend_registry_init();
  302. const char * params = strchr(backend_str, ':');
  303. char backend_name[128];
  304. if (params == NULL) {
  305. snprintf(backend_name, sizeof(backend_name), "%s", backend_str);
  306. params = "";
  307. } else {
  308. snprintf(backend_name, sizeof(backend_name), "%.*s", (int)(params - backend_str), backend_str);
  309. params++;
  310. }
  311. size_t backend_i = ggml_backend_reg_find_by_name(backend_name);
  312. if (backend_i == SIZE_MAX) {
  313. fprintf(stderr, "%s: backend %s not found\n", __func__, backend_name);
  314. return NULL;
  315. }
  316. return ggml_backend_reg_init_backend(backend_i, params);
  317. }
  318. const char * ggml_backend_reg_get_name(size_t i) {
  319. ggml_backend_registry_init();
  320. GGML_ASSERT(i < ggml_backend_registry_count);
  321. return ggml_backend_registry[i].name;
  322. }
  323. ggml_backend_t ggml_backend_reg_init_backend(size_t i, const char * params) {
  324. ggml_backend_registry_init();
  325. GGML_ASSERT(i < ggml_backend_registry_count);
  326. return ggml_backend_registry[i].init_fn(params, ggml_backend_registry[i].user_data);
  327. }
  328. ggml_backend_buffer_type_t ggml_backend_reg_get_default_buffer_type(size_t i) {
  329. ggml_backend_registry_init();
  330. GGML_ASSERT(i < ggml_backend_registry_count);
  331. return ggml_backend_registry[i].default_buffer_type;
  332. }
  333. ggml_backend_buffer_t ggml_backend_reg_alloc_buffer(size_t i, size_t size) {
  334. ggml_backend_registry_init();
  335. GGML_ASSERT(i < ggml_backend_registry_count);
  336. return ggml_backend_buft_alloc_buffer(ggml_backend_registry[i].default_buffer_type, size);
  337. }
  338. // backend CPU
  339. static const char * ggml_backend_cpu_buffer_name(ggml_backend_buffer_t buffer) {
  340. return "CPU";
  341. GGML_UNUSED(buffer);
  342. }
  343. static void * ggml_backend_cpu_buffer_get_base(ggml_backend_buffer_t buffer) {
  344. return (void *)buffer->context;
  345. }
  346. static void ggml_backend_cpu_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  347. free(buffer->context);
  348. }
  349. static void ggml_backend_cpu_buffer_set_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  350. memcpy((char *)tensor->data + offset, data, size);
  351. GGML_UNUSED(buffer);
  352. }
  353. static void ggml_backend_cpu_buffer_get_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  354. memcpy(data, (const char *)tensor->data + offset, size);
  355. GGML_UNUSED(buffer);
  356. }
  357. static bool ggml_backend_cpu_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst) {
  358. if (ggml_backend_buffer_is_host(src->buffer)) {
  359. memcpy(dst->data, src->data, ggml_nbytes(src));
  360. return true;
  361. }
  362. return false;
  363. GGML_UNUSED(buffer);
  364. }
  365. static void ggml_backend_cpu_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  366. memset(buffer->context, value, buffer->size);
  367. }
  368. static struct ggml_backend_buffer_i cpu_backend_buffer_i = {
  369. /* .get_name = */ ggml_backend_cpu_buffer_name,
  370. /* .free_buffer = */ ggml_backend_cpu_buffer_free_buffer,
  371. /* .get_base = */ ggml_backend_cpu_buffer_get_base,
  372. /* .init_tensor = */ NULL, // no initialization required
  373. /* .set_tensor = */ ggml_backend_cpu_buffer_set_tensor,
  374. /* .get_tensor = */ ggml_backend_cpu_buffer_get_tensor,
  375. /* .cpy_tensor = */ ggml_backend_cpu_buffer_cpy_tensor,
  376. /* .clear = */ ggml_backend_cpu_buffer_clear,
  377. /* .reset = */ NULL,
  378. };
  379. // for buffers from ptr, free is not called
  380. static struct ggml_backend_buffer_i cpu_backend_buffer_i_from_ptr = {
  381. /* .get_name = */ ggml_backend_cpu_buffer_name,
  382. /* .free_buffer = */ NULL, // ptr is not owned by the buffer, so it does not need to be freed
  383. /* .get_base = */ ggml_backend_cpu_buffer_get_base,
  384. /* .init_tensor = */ NULL, // no initialization required
  385. /* .set_tensor = */ ggml_backend_cpu_buffer_set_tensor,
  386. /* .get_tensor = */ ggml_backend_cpu_buffer_get_tensor,
  387. /* .cpy_tensor = */ ggml_backend_cpu_buffer_cpy_tensor,
  388. /* .clear = */ ggml_backend_cpu_buffer_clear,
  389. /* .reset = */ NULL,
  390. };
  391. static const size_t TENSOR_ALIGNMENT = 64; // should be enough for AVX 512
  392. static const char * ggml_backend_cpu_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  393. return "CPU";
  394. GGML_UNUSED(buft);
  395. }
  396. static ggml_backend_buffer_t ggml_backend_cpu_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  397. size += TENSOR_ALIGNMENT; // malloc may return an address that is not aligned
  398. void * data = malloc(size); // TODO: maybe use GGML_ALIGNED_MALLOC?
  399. GGML_ASSERT(data != NULL && "failed to allocate buffer");
  400. return ggml_backend_buffer_init(buft, cpu_backend_buffer_i, data, size);
  401. }
  402. static size_t ggml_backend_cpu_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  403. return TENSOR_ALIGNMENT;
  404. GGML_UNUSED(buft);
  405. }
  406. static bool ggml_backend_cpu_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  407. return ggml_backend_is_cpu(backend);
  408. GGML_UNUSED(buft);
  409. }
  410. static bool ggml_backend_cpu_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  411. return true;
  412. GGML_UNUSED(buft);
  413. }
  414. ggml_backend_buffer_type_t ggml_backend_cpu_buffer_type(void) {
  415. static struct ggml_backend_buffer_type ggml_backend_cpu_buffer_type = {
  416. /* .iface = */ {
  417. /* .get_name = */ ggml_backend_cpu_buffer_type_get_name,
  418. /* .alloc_buffer = */ ggml_backend_cpu_buffer_type_alloc_buffer,
  419. /* .get_alignment = */ ggml_backend_cpu_buffer_type_get_alignment,
  420. /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
  421. /* .supports_backend = */ ggml_backend_cpu_buffer_type_supports_backend,
  422. /* .is_host = */ ggml_backend_cpu_buffer_type_is_host,
  423. },
  424. /* .context = */ NULL,
  425. };
  426. return &ggml_backend_cpu_buffer_type;
  427. }
  428. #ifdef GGML_USE_CPU_HBM
  429. // buffer type HBM
  430. #include <hbwmalloc.h>
  431. static const char * ggml_backend_cpu_hbm_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  432. return "CPU_HBM";
  433. GGML_UNUSED(buft);
  434. }
  435. static const char * ggml_backend_cpu_hbm_buffer_get_name(ggml_backend_buffer_t buf) {
  436. return "CPU_HBM";
  437. GGML_UNUSED(buf);
  438. }
  439. static void ggml_backend_cpu_hbm_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  440. hbw_free(buffer->context);
  441. }
  442. static ggml_backend_buffer_t ggml_backend_cpu_hbm_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  443. //void * ptr = hbw_malloc(size);
  444. void * ptr;
  445. int result = hbw_posix_memalign(&ptr, ggml_backend_cpu_buffer_type_get_alignment(buft), size);
  446. if (result != 0) {
  447. fprintf(stderr, "failed to allocate HBM buffer of size %zu\n", size);
  448. return NULL;
  449. }
  450. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  451. buffer->buft = buft;
  452. buffer->iface.get_name = ggml_backend_cpu_hbm_buffer_get_name;
  453. buffer->iface.free_buffer = ggml_backend_cpu_hbm_buffer_free_buffer;
  454. return buffer;
  455. }
  456. ggml_backend_buffer_type_t ggml_backend_cpu_hbm_buffer_type(void) {
  457. static struct ggml_backend_buffer_type ggml_backend_cpu_buffer_type_hbm = {
  458. /* .iface = */ {
  459. /* .get_name = */ ggml_backend_cpu_hbm_buffer_type_get_name,
  460. /* .alloc_buffer = */ ggml_backend_cpu_hbm_buffer_type_alloc_buffer,
  461. /* .get_alignment = */ ggml_backend_cpu_buffer_type_get_alignment,
  462. /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
  463. /* .supports_backend = */ ggml_backend_cpu_buffer_type_supports_backend,
  464. /* .is_host = */ ggml_backend_cpu_buffer_type_is_host,
  465. },
  466. /* .context = */ NULL,
  467. };
  468. return &ggml_backend_cpu_buffer_type_hbm;
  469. }
  470. #endif
  471. struct ggml_backend_cpu_context {
  472. int n_threads;
  473. void * work_data;
  474. size_t work_size;
  475. };
  476. static const char * ggml_backend_cpu_name(ggml_backend_t backend) {
  477. return "CPU";
  478. GGML_UNUSED(backend);
  479. }
  480. static void ggml_backend_cpu_free(ggml_backend_t backend) {
  481. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  482. free(cpu_ctx->work_data);
  483. free(cpu_ctx);
  484. free(backend);
  485. }
  486. static ggml_backend_buffer_type_t ggml_backend_cpu_get_default_buffer_type(ggml_backend_t backend) {
  487. return ggml_backend_cpu_buffer_type();
  488. GGML_UNUSED(backend);
  489. }
  490. struct ggml_backend_plan_cpu {
  491. struct ggml_cplan cplan;
  492. struct ggml_cgraph cgraph;
  493. };
  494. static ggml_backend_graph_plan_t ggml_backend_cpu_graph_plan_create(ggml_backend_t backend, const struct ggml_cgraph * cgraph) {
  495. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  496. struct ggml_backend_plan_cpu * cpu_plan = malloc(sizeof(struct ggml_backend_plan_cpu));
  497. cpu_plan->cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads);
  498. cpu_plan->cgraph = *cgraph; // FIXME: deep copy
  499. if (cpu_plan->cplan.work_size > 0) {
  500. cpu_plan->cplan.work_data = malloc(cpu_plan->cplan.work_size);
  501. }
  502. return cpu_plan;
  503. }
  504. static void ggml_backend_cpu_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  505. struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
  506. free(cpu_plan->cplan.work_data);
  507. free(cpu_plan);
  508. GGML_UNUSED(backend);
  509. }
  510. static void ggml_backend_cpu_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  511. struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
  512. ggml_graph_compute(&cpu_plan->cgraph, &cpu_plan->cplan);
  513. GGML_UNUSED(backend);
  514. }
  515. static bool ggml_backend_cpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  516. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  517. struct ggml_cplan cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads);
  518. if (cpu_ctx->work_size < cplan.work_size) {
  519. // TODO: may be faster to free and use malloc to avoid the copy
  520. cpu_ctx->work_data = realloc(cpu_ctx->work_data, cplan.work_size);
  521. cpu_ctx->work_size = cplan.work_size;
  522. }
  523. cplan.work_data = cpu_ctx->work_data;
  524. ggml_graph_compute(cgraph, &cplan);
  525. return true;
  526. }
  527. static bool ggml_backend_cpu_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) {
  528. switch (op->op) {
  529. case GGML_OP_MUL_MAT:
  530. return op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == ggml_internal_get_type_traits(op->src[0]->type).vec_dot_type;
  531. default:
  532. return true;
  533. }
  534. GGML_UNUSED(backend);
  535. }
  536. static struct ggml_backend_i cpu_backend_i = {
  537. /* .get_name = */ ggml_backend_cpu_name,
  538. /* .free = */ ggml_backend_cpu_free,
  539. /* .get_default_buffer_type = */ ggml_backend_cpu_get_default_buffer_type,
  540. /* .set_tensor_async = */ NULL,
  541. /* .get_tensor_async = */ NULL,
  542. /* .cpy_tensor_async = */ NULL,
  543. /* .synchronize = */ NULL,
  544. /* .graph_plan_create = */ ggml_backend_cpu_graph_plan_create,
  545. /* .graph_plan_free = */ ggml_backend_cpu_graph_plan_free,
  546. /* .graph_plan_compute = */ ggml_backend_cpu_graph_plan_compute,
  547. /* .graph_compute = */ ggml_backend_cpu_graph_compute,
  548. /* .supports_op = */ ggml_backend_cpu_supports_op,
  549. };
  550. ggml_backend_t ggml_backend_cpu_init(void) {
  551. struct ggml_backend_cpu_context * ctx = malloc(sizeof(struct ggml_backend_cpu_context));
  552. ctx->n_threads = GGML_DEFAULT_N_THREADS;
  553. ctx->work_data = NULL;
  554. ctx->work_size = 0;
  555. ggml_backend_t cpu_backend = malloc(sizeof(struct ggml_backend));
  556. *cpu_backend = (struct ggml_backend) {
  557. /* .interface = */ cpu_backend_i,
  558. /* .context = */ ctx
  559. };
  560. return cpu_backend;
  561. }
  562. bool ggml_backend_is_cpu(ggml_backend_t backend) {
  563. return backend && backend->iface.get_name == ggml_backend_cpu_name;
  564. }
  565. void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads) {
  566. GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
  567. struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
  568. ctx->n_threads = n_threads;
  569. }
  570. ggml_backend_buffer_t ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size) {
  571. return ggml_backend_buffer_init(ggml_backend_cpu_buffer_type(), cpu_backend_buffer_i_from_ptr, ptr, size);
  572. }
  573. static ggml_backend_t ggml_backend_reg_cpu_init(const char * params, void * user_data) {
  574. return ggml_backend_cpu_init();
  575. GGML_UNUSED(params);
  576. GGML_UNUSED(user_data);
  577. }
  578. // scheduler
  579. #define GGML_MAX_BACKENDS 16
  580. #define GGML_MAX_SPLITS 256
  581. #define GGML_MAX_SPLIT_INPUTS 16
  582. struct ggml_backend_sched_split {
  583. ggml_tallocr_t tallocr;
  584. int i_start;
  585. int i_end;
  586. struct ggml_tensor * inputs[GGML_MAX_SPLIT_INPUTS];
  587. int n_inputs;
  588. // graph view of this split
  589. struct ggml_cgraph graph;
  590. };
  591. struct ggml_backend_sched {
  592. bool is_reset; // true if the scheduler has been reset since the last graph split
  593. int n_backends;
  594. ggml_backend_t backends[GGML_MAX_BACKENDS];
  595. ggml_backend_buffer_type_t bufts[GGML_MAX_BACKENDS];
  596. ggml_tallocr_t tallocs[GGML_MAX_BACKENDS];
  597. ggml_gallocr_t galloc;
  598. // hash keys of the nodes in the graph
  599. struct ggml_hash_set hash_set;
  600. // hash values (arrays of [hash_set.size])
  601. ggml_tallocr_t * node_talloc; // tallocr assigned to each node (indirectly this is the backend)
  602. struct ggml_tensor * (* node_copies)[GGML_MAX_BACKENDS]; // copies of each node for each destination backend
  603. // copy of the graph with modified inputs
  604. struct ggml_cgraph * graph;
  605. struct ggml_backend_sched_split splits[GGML_MAX_SPLITS];
  606. int n_splits;
  607. struct ggml_context * ctx;
  608. // align context_buffer to GGML_MEM_ALIGN
  609. #ifdef _MSC_VER
  610. __declspec(align(GGML_MEM_ALIGN))
  611. #else
  612. __attribute__((aligned(GGML_MEM_ALIGN)))
  613. #endif
  614. char context_buffer[GGML_MAX_SPLITS*GGML_MAX_SPLIT_INPUTS*sizeof(struct ggml_tensor) + sizeof(struct ggml_cgraph)];
  615. };
  616. #define hash_id(node) ggml_hash_find_or_insert(sched->hash_set, node)
  617. #define node_allocr(node) sched->node_talloc[hash_id(node)]
  618. static bool ggml_is_view_op(enum ggml_op op) {
  619. return op == GGML_OP_VIEW || op == GGML_OP_RESHAPE || op == GGML_OP_PERMUTE || op == GGML_OP_TRANSPOSE;
  620. }
  621. // returns the priority of the backend, lower is better
  622. static int sched_backend_prio(ggml_backend_sched_t sched, ggml_backend_t backend) {
  623. for (int i = 0; i < sched->n_backends; i++) {
  624. if (sched->backends[i] == backend) {
  625. return i;
  626. }
  627. }
  628. return INT_MAX;
  629. }
  630. static int sched_allocr_prio(ggml_backend_sched_t sched, ggml_tallocr_t allocr) {
  631. for (int i = 0; i < sched->n_backends; i++) {
  632. if (sched->tallocs[i] == allocr) {
  633. return i;
  634. }
  635. }
  636. return INT_MAX;
  637. }
  638. static ggml_tallocr_t sched_allocr_from_buffer(ggml_backend_sched_t sched, ggml_backend_buffer_t buffer) {
  639. if (buffer == NULL) {
  640. return NULL;
  641. }
  642. // check if this is already allocate in a allocr buffer (from user manual allocations)
  643. for (int i = 0; i < sched->n_backends; i++) {
  644. if (ggml_tallocr_get_buffer(sched->tallocs[i]) == buffer) {
  645. return sched->tallocs[i];
  646. }
  647. }
  648. // find highest prio backend that supports the buffer type
  649. for (int i = 0; i < sched->n_backends; i++) {
  650. if (ggml_backend_buft_supports_backend(buffer->buft, sched->backends[i])) {
  651. return sched->tallocs[i];
  652. }
  653. }
  654. GGML_ASSERT(false && "tensor buffer type not supported by any backend");
  655. }
  656. static ggml_backend_t get_allocr_backend(ggml_backend_sched_t sched, ggml_tallocr_t allocr) {
  657. if (allocr == NULL) {
  658. return NULL;
  659. }
  660. for (int i = 0; i < sched->n_backends; i++) {
  661. if (sched->tallocs[i] == allocr) {
  662. return sched->backends[i];
  663. }
  664. }
  665. GGML_UNREACHABLE();
  666. }
  667. #if 0
  668. static char causes[GGML_DEFAULT_GRAPH_SIZE*16 + GGML_MAX_SPLITS*GGML_MAX_SPLIT_INPUTS][128]; // debug only
  669. #define SET_CAUSE(node, ...) sprintf(causes[hash_id(node)], __VA_ARGS__)
  670. #define GET_CAUSE(node) causes[hash_id(node)]
  671. #else
  672. #define SET_CAUSE(node, ...)
  673. #define GET_CAUSE(node) ""
  674. #endif
  675. // returns the backend that should be used for the node based on the current locations
  676. static ggml_tallocr_t sched_allocr_from_cur(ggml_backend_sched_t sched, struct ggml_tensor * node) {
  677. // assign pre-allocated nodes to their backend
  678. // dst
  679. ggml_tallocr_t cur_allocr = sched_allocr_from_buffer(sched, node->buffer);
  680. if (cur_allocr != NULL) {
  681. SET_CAUSE(node, "1.dst");
  682. return cur_allocr;
  683. }
  684. // view_src
  685. if (node->view_src != NULL) {
  686. cur_allocr = sched_allocr_from_buffer(sched, node->view_src->buffer);
  687. if (cur_allocr != NULL) {
  688. SET_CAUSE(node, "1.vsrc");
  689. return cur_allocr;
  690. }
  691. }
  692. // assign nodes that use weights to the backend of the weights
  693. for (int i = 0; i < GGML_MAX_SRC; i++) {
  694. const struct ggml_tensor * src = node->src[i];
  695. if (src == NULL) {
  696. break;
  697. }
  698. if (src->buffer != NULL && src->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
  699. ggml_tallocr_t src_allocr = sched_allocr_from_buffer(sched, src->buffer);
  700. // operations with weights are always run on the same backend as the weights
  701. SET_CAUSE(node, "1.wgt%d", i);
  702. return src_allocr;
  703. }
  704. }
  705. return NULL;
  706. }
  707. static char * fmt_size(size_t size) {
  708. static char buffer[128];
  709. if (size >= 1024*1024) {
  710. sprintf(buffer, "%zuM", size/1024/1024);
  711. } else {
  712. sprintf(buffer, "%zuK", size/1024);
  713. }
  714. return buffer;
  715. }
  716. static void sched_print_assignments(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  717. int cur_split = 0;
  718. for (int i = 0; i < graph->n_nodes; i++) {
  719. if (cur_split < sched->n_splits && i == sched->splits[cur_split].i_start) {
  720. ggml_backend_t split_backend = get_allocr_backend(sched, sched->splits[cur_split].tallocr);
  721. fprintf(stderr, "\n## SPLIT #%d: %s # %d inputs: ", cur_split, ggml_backend_name(split_backend),
  722. sched->splits[cur_split].n_inputs);
  723. for (int j = 0; j < sched->splits[cur_split].n_inputs; j++) {
  724. fprintf(stderr, "[%s (%5.5s)] ", sched->splits[cur_split].inputs[j]->name,
  725. fmt_size(ggml_nbytes(sched->splits[cur_split].inputs[j])));
  726. }
  727. fprintf(stderr, "\n");
  728. cur_split++;
  729. }
  730. struct ggml_tensor * node = graph->nodes[i];
  731. if (ggml_is_view_op(node->op)) {
  732. continue;
  733. }
  734. ggml_tallocr_t node_allocr = node_allocr(node);
  735. ggml_backend_t node_backend = node_allocr ? get_allocr_backend(sched, node_allocr) : NULL; // FIXME:
  736. fprintf(stderr, "node #%3d (%10.10s): %20.20s (%5.5s) [%5.5s %8.8s]:", i, ggml_op_name(node->op), node->name,
  737. fmt_size(ggml_nbytes(node)), node_allocr ? ggml_backend_name(node_backend) : "NULL", GET_CAUSE(node));
  738. for (int j = 0; j < GGML_MAX_SRC; j++) {
  739. struct ggml_tensor * src = node->src[j];
  740. if (src == NULL) {
  741. break;
  742. }
  743. ggml_tallocr_t src_allocr = node_allocr(src);
  744. ggml_backend_t src_backend = src_allocr ? get_allocr_backend(sched, src_allocr) : NULL;
  745. fprintf(stderr, " %20.20s (%5.5s) [%5.5s %8.8s]", src->name,
  746. fmt_size(ggml_nbytes(src)), src_backend ? ggml_backend_name(src_backend) : "NULL", GET_CAUSE(src));
  747. }
  748. fprintf(stderr, "\n");
  749. }
  750. }
  751. // creates a copy of the tensor with the same memory layout
  752. static struct ggml_tensor * ggml_dup_tensor_layout(struct ggml_context * ctx, const struct ggml_tensor * tensor) {
  753. struct ggml_tensor * dup = ggml_dup_tensor(ctx, tensor);
  754. for (int i = 0; i < GGML_MAX_DIMS; i++) {
  755. dup->nb[i] = tensor->nb[i];
  756. }
  757. return dup;
  758. }
  759. //#define DEBUG_PASS1
  760. //#define DEBUG_PASS2
  761. //#define DEBUG_PASS3
  762. //#define DEBUG_PASS4
  763. // assigns backends to ops and splits the graph into subgraphs that can be computed on the same backend
  764. static void sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  765. // reset splits
  766. sched->n_splits = 0;
  767. sched->is_reset = false;
  768. struct ggml_init_params params = {
  769. /* .mem_size = */ sizeof(sched->context_buffer),
  770. /* .mem_buffer = */ sched->context_buffer,
  771. /* .no_alloc = */ true
  772. };
  773. ggml_free(sched->ctx);
  774. sched->ctx = ggml_init(params);
  775. if (sched->ctx == NULL) {
  776. fprintf(stderr, "%s: failed to initialize context\n", __func__);
  777. GGML_ASSERT(false);
  778. }
  779. // pass 1: assign backends to ops with pre-allocated inputs
  780. for (int i = 0; i < graph->n_leafs; i++) {
  781. struct ggml_tensor * leaf = graph->leafs[i];
  782. if (node_allocr(leaf) != NULL) {
  783. // do not overwrite user assignments
  784. continue;
  785. }
  786. node_allocr(leaf) = sched_allocr_from_cur(sched, leaf);
  787. }
  788. for (int i = 0; i < graph->n_nodes; i++) {
  789. struct ggml_tensor * node = graph->nodes[i];
  790. if (node_allocr(node) != NULL) {
  791. // do not overwrite user assignments
  792. continue;
  793. }
  794. node_allocr(node) = sched_allocr_from_cur(sched, node);
  795. // src
  796. for (int j = 0; j < GGML_MAX_SRC; j++) {
  797. struct ggml_tensor * src = node->src[j];
  798. if (src == NULL) {
  799. break;
  800. }
  801. if (node_allocr(src) == NULL) {
  802. node_allocr(src) = sched_allocr_from_cur(sched, src);
  803. }
  804. }
  805. }
  806. #ifdef DEBUG_PASS1
  807. fprintf(stderr, "PASS 1 ASSIGNMENTS\n"); sched_print_assignments(sched, graph);
  808. #endif
  809. // pass 2: expand current backend assignments
  810. // assign the same backend to adjacent nodes
  811. // expand gpu backends (i.e. non last prio) up and down, ignoring cpu (the lowest priority backend)
  812. // thus, cpu will never be used unless weights are on cpu, or there are no gpu ops between cpu ops
  813. // pass 2.1 expand gpu up
  814. {
  815. ggml_tallocr_t cur_allocr = NULL;
  816. for (int i = graph->n_nodes - 1; i >= 0; i--) {
  817. struct ggml_tensor * node = graph->nodes[i];
  818. if (ggml_is_view_op(node->op)) {
  819. continue;
  820. }
  821. ggml_tallocr_t node_allocr = node_allocr(node);
  822. if (node_allocr != NULL) {
  823. if (sched_allocr_prio(sched, node_allocr) == sched->n_backends - 1) {
  824. // skip cpu (lowest prio backend)
  825. cur_allocr = NULL;
  826. } else {
  827. cur_allocr = node_allocr;
  828. }
  829. } else {
  830. node_allocr(node) = cur_allocr;
  831. SET_CAUSE(node, "2.1");
  832. }
  833. }
  834. }
  835. // pass 2.2 expand gpu down
  836. {
  837. ggml_tallocr_t cur_allocr = NULL;
  838. for (int i = 0; i < graph->n_nodes; i++) {
  839. struct ggml_tensor * node = graph->nodes[i];
  840. if (ggml_is_view_op(node->op)) {
  841. continue;
  842. }
  843. ggml_tallocr_t node_allocr = node_allocr(node);
  844. if (node_allocr != NULL) {
  845. if (sched_allocr_prio(sched, node_allocr) == sched->n_backends - 1) {
  846. // skip cpu (lowest prio backend)
  847. cur_allocr = NULL;
  848. } else {
  849. cur_allocr = node_allocr;
  850. }
  851. } else {
  852. node_allocr(node) = cur_allocr;
  853. SET_CAUSE(node, "2.2");
  854. }
  855. }
  856. }
  857. // pass 2.3 expand rest up
  858. {
  859. ggml_tallocr_t cur_allocr = NULL;
  860. for (int i = graph->n_nodes - 1; i >= 0; i--) {
  861. struct ggml_tensor * node = graph->nodes[i];
  862. if (ggml_is_view_op(node->op)) {
  863. continue;
  864. }
  865. ggml_tallocr_t node_allocr = node_allocr(node);
  866. if (node_allocr != NULL) {
  867. cur_allocr = node_allocr;
  868. } else {
  869. node_allocr(node) = cur_allocr;
  870. SET_CAUSE(node, "2.3");
  871. }
  872. }
  873. }
  874. // pass 2.4 expand rest down
  875. {
  876. ggml_tallocr_t cur_allocr = NULL;
  877. for (int i = 0; i < graph->n_nodes; i++) {
  878. struct ggml_tensor * node = graph->nodes[i];
  879. if (ggml_is_view_op(node->op)) {
  880. continue;
  881. }
  882. ggml_tallocr_t node_allocr = node_allocr(node);
  883. if (node_allocr != NULL) {
  884. cur_allocr = node_allocr;
  885. } else {
  886. node_allocr(node) = cur_allocr;
  887. SET_CAUSE(node, "2.4");
  888. }
  889. }
  890. }
  891. #ifdef DEBUG_PASS2
  892. fprintf(stderr, "PASS 2 ASSIGNMENTS\n"); sched_print_assignments(sched, graph);
  893. #endif
  894. // pass 3: assign backends to remaining src from dst and view_src
  895. for (int i = 0; i < graph->n_nodes; i++) {
  896. struct ggml_tensor * node = graph->nodes[i];
  897. ggml_tallocr_t cur_allocr = node_allocr(node);
  898. if (node->view_src != NULL && cur_allocr == NULL) {
  899. cur_allocr = node_allocr(node) = node_allocr(node->view_src);
  900. SET_CAUSE(node, "3.vsrc");
  901. }
  902. for (int j = 0; j < GGML_MAX_SRC; j++) {
  903. struct ggml_tensor * src = node->src[j];
  904. if (src == NULL) {
  905. break;
  906. }
  907. ggml_tallocr_t src_allocr = node_allocr(src);
  908. if (src_allocr == NULL) {
  909. if (src->view_src != NULL) {
  910. // views are always on the same backend as the source
  911. node_allocr(src) = node_allocr(src->view_src);
  912. SET_CAUSE(src, "3.vsrc");
  913. } else {
  914. node_allocr(src) = cur_allocr;
  915. SET_CAUSE(src, "3.cur");
  916. }
  917. }
  918. }
  919. }
  920. #ifdef DEBUG_PASS3
  921. fprintf(stderr, "PASS 3 ASSIGNMENTS\n"); sched_print_assignments(sched, graph);
  922. #endif
  923. // pass 4: split graph, find tensors that need to be copied
  924. {
  925. int cur_split = 0;
  926. // find the backend of the first split, skipping view ops
  927. for (int i = 0; i < graph->n_nodes; i++) {
  928. struct ggml_tensor * node = graph->nodes[i];
  929. if (!ggml_is_view_op(node->op)) {
  930. sched->splits[0].tallocr = node_allocr(node);
  931. break;
  932. }
  933. }
  934. sched->splits[0].i_start = 0;
  935. sched->splits[0].n_inputs = 0;
  936. memset(sched->splits[0].inputs, 0, sizeof(sched->splits[0].inputs)); //HACK
  937. ggml_tallocr_t cur_allocr = sched->splits[0].tallocr;
  938. size_t cur_backend_id = sched_allocr_prio(sched, cur_allocr);
  939. for (int i = 0; i < graph->n_nodes; i++) {
  940. struct ggml_tensor * node = graph->nodes[i];
  941. if (ggml_is_view_op(node->op)) {
  942. continue;
  943. }
  944. ggml_tallocr_t node_allocr = node_allocr(node);
  945. GGML_ASSERT(node_allocr != NULL); // all nodes should be assigned by now
  946. if (node_allocr != cur_allocr) {
  947. sched->splits[cur_split].i_end = i;
  948. cur_split++;
  949. GGML_ASSERT(cur_split < GGML_MAX_SPLITS);
  950. sched->splits[cur_split].tallocr = node_allocr;
  951. sched->splits[cur_split].i_start = i;
  952. sched->splits[cur_split].n_inputs = 0;
  953. cur_allocr = node_allocr;
  954. cur_backend_id = sched_allocr_prio(sched, cur_allocr);
  955. }
  956. // find inputs that are not on the same backend
  957. for (int j = 0; j < GGML_MAX_SRC; j++) {
  958. struct ggml_tensor * src = node->src[j];
  959. if (src == NULL) {
  960. break;
  961. }
  962. ggml_tallocr_t src_allocr = node_allocr(src);
  963. GGML_ASSERT(src_allocr != NULL); // all inputs should be assigned by now
  964. if (src_allocr != node_allocr) {
  965. // check if the input is already in the split
  966. bool found = false;
  967. for (int k = 0; k < sched->splits[cur_split].n_inputs; k++) {
  968. if (sched->splits[cur_split].inputs[k] == src) {
  969. found = true;
  970. break;
  971. }
  972. }
  973. if (!found) {
  974. int n_inputs = sched->splits[cur_split].n_inputs++;
  975. //printf("split %d input %d: %s (%s)\n", cur_split, n_inputs, src->name, ggml_backend_name(get_allocr_backend(sched, src_allocr)));
  976. GGML_ASSERT(n_inputs < GGML_MAX_SPLIT_INPUTS);
  977. sched->splits[cur_split].inputs[n_inputs] = src;
  978. }
  979. // create a copy of the input in the split's backend
  980. size_t id = hash_id(src);
  981. if (sched->node_copies[id][cur_backend_id] == NULL) {
  982. ggml_backend_t backend = get_allocr_backend(sched, cur_allocr);
  983. struct ggml_tensor * tensor_copy = ggml_dup_tensor_layout(sched->ctx, src);
  984. ggml_format_name(tensor_copy, "%s#%s", ggml_backend_name(backend), src->name);
  985. sched->node_copies[id][cur_backend_id] = tensor_copy;
  986. node_allocr(tensor_copy) = cur_allocr;
  987. SET_CAUSE(tensor_copy, "4.cpy");
  988. }
  989. node->src[j] = sched->node_copies[id][cur_backend_id];
  990. }
  991. }
  992. }
  993. sched->splits[cur_split].i_end = graph->n_nodes;
  994. sched->n_splits = cur_split + 1;
  995. }
  996. #ifdef DEBUG_PASS4
  997. fprintf(stderr, "PASS 4 ASSIGNMENTS\n"); sched_print_assignments(sched, graph);
  998. #endif
  999. #ifndef NDEBUG
  1000. // sanity check: all sources should have the same backend as the node
  1001. for (int i = 0; i < graph->n_nodes; i++) {
  1002. struct ggml_tensor * node = graph->nodes[i];
  1003. ggml_tallocr_t node_allocr = node_allocr(node);
  1004. if (node_allocr == NULL) {
  1005. fprintf(stderr, "!!!!!!! %s has no backend\n", node->name);
  1006. }
  1007. if (node->view_src != NULL && node_allocr != node_allocr(node->view_src)) {
  1008. fprintf(stderr, "!!!!!!! %s has backend %s, view_src %s has backend %s\n",
  1009. node->name, node_allocr ? ggml_backend_name(get_allocr_backend(sched, node_allocr)) : "NULL",
  1010. node->view_src->name, node_allocr(node->view_src) ? ggml_backend_name(get_allocr_backend(sched, node_allocr(node->view_src))) : "NULL");
  1011. }
  1012. for (int j = 0; j < GGML_MAX_SRC; j++) {
  1013. struct ggml_tensor * src = node->src[j];
  1014. if (src == NULL) {
  1015. break;
  1016. }
  1017. ggml_tallocr_t src_allocr = node_allocr(src);
  1018. if (src_allocr != node_allocr /* && src_backend != NULL */) { // ignore nulls for now
  1019. fprintf(stderr, "!!!! %s has backend %s, src %d (%s) has backend %s\n",
  1020. node->name, node_allocr ? ggml_backend_name(get_allocr_backend(sched, node_allocr)) : "NULL",
  1021. j, src->name, src_allocr ? ggml_backend_name(get_allocr_backend(sched, src_allocr)) : "NULL");
  1022. }
  1023. if (src->view_src != NULL && src_allocr != node_allocr(src->view_src)) {
  1024. fprintf(stderr, "!!!!!!! [src] %s has backend %s, view_src %s has backend %s\n",
  1025. src->name, src_allocr ? ggml_backend_name(get_allocr_backend(sched, src_allocr)) : "NULL",
  1026. src->view_src->name, node_allocr(src->view_src) ? ggml_backend_name(get_allocr_backend(sched, node_allocr(src->view_src))) : "NULL");
  1027. }
  1028. }
  1029. }
  1030. fflush(stderr);
  1031. #endif
  1032. // create copies of the graph for each split
  1033. // FIXME: avoid this copy, pass split inputs to ggml_gallocr_alloc_graph_n in some other way
  1034. struct ggml_cgraph * graph_copy = ggml_new_graph_custom(sched->ctx, graph->n_nodes + sched->n_splits*GGML_MAX_SPLIT_INPUTS, false);
  1035. for (int i = 0; i < sched->n_splits; i++) {
  1036. struct ggml_backend_sched_split * split = &sched->splits[i];
  1037. split->graph = ggml_graph_view(graph, split->i_start, split->i_end);
  1038. // add inputs to the graph copy so that they are allocated by ggml-alloc at the start of the split
  1039. for (int j = 0; j < split->n_inputs; j++) {
  1040. struct ggml_tensor * input = split->inputs[j];
  1041. struct ggml_tensor * input_cpy = sched->node_copies[hash_id(input)][sched_allocr_prio(sched, split->tallocr)];
  1042. // add a dependency to the input source so that it is not freed before the copy is done
  1043. GGML_ASSERT(input_cpy->src[0] == NULL || input_cpy->src[0] == input);
  1044. input_cpy->src[0] = input;
  1045. graph_copy->nodes[graph_copy->n_nodes++] = input_cpy;
  1046. }
  1047. for (int j = split->i_start; j < split->i_end; j++) {
  1048. graph_copy->nodes[graph_copy->n_nodes++] = graph->nodes[j];
  1049. }
  1050. }
  1051. sched->graph = graph_copy;
  1052. }
  1053. static void sched_alloc_splits(ggml_backend_sched_t sched) {
  1054. ggml_gallocr_alloc_graph_n(
  1055. sched->galloc,
  1056. sched->graph,
  1057. sched->hash_set,
  1058. sched->node_talloc);
  1059. }
  1060. static void sched_compute_splits(ggml_backend_sched_t sched) {
  1061. uint64_t copy_us[GGML_MAX_BACKENDS] = {0};
  1062. uint64_t compute_us[GGML_MAX_BACKENDS] = {0};
  1063. struct ggml_backend_sched_split * splits = sched->splits;
  1064. for (int i = 0; i < sched->n_splits; i++) {
  1065. struct ggml_backend_sched_split * split = &splits[i];
  1066. ggml_backend_t split_backend = get_allocr_backend(sched, split->tallocr);
  1067. int split_backend_id = sched_backend_prio(sched, split_backend);
  1068. // copy the input tensors to the split backend
  1069. uint64_t copy_start_us = ggml_time_us();
  1070. for (int j = 0; j < split->n_inputs; j++) {
  1071. struct ggml_tensor * input = split->inputs[j];
  1072. struct ggml_tensor * input_cpy = sched->node_copies[hash_id(input)][split_backend_id];
  1073. GGML_ASSERT(input->buffer != NULL);
  1074. GGML_ASSERT(input_cpy->buffer != NULL);
  1075. // TODO: avoid this copy if it was already copied in a previous split, and the input didn't change
  1076. // this is important to avoid copying constants such as KQ_mask and inp_pos multiple times
  1077. ggml_backend_tensor_copy_async(split_backend, input, input_cpy);
  1078. }
  1079. //ggml_backend_synchronize(split_backend); // necessary to measure copy time
  1080. int64_t copy_end_us = ggml_time_us();
  1081. copy_us[split_backend_id] += copy_end_us - copy_start_us;
  1082. #if 0
  1083. char split_filename[GGML_MAX_NAME];
  1084. snprintf(split_filename, GGML_MAX_NAME, "split_%i_%s.dot", i, ggml_backend_name(split_backend));
  1085. ggml_graph_dump_dot(split->graph, NULL, split_filename);
  1086. #endif
  1087. uint64_t compute_start_us = ggml_time_us();
  1088. ggml_backend_graph_compute(split_backend, &split->graph);
  1089. //ggml_backend_synchronize(split_backend); // necessary to measure compute time
  1090. uint64_t compute_end_us = ggml_time_us();
  1091. compute_us[split_backend_id] += compute_end_us - compute_start_us;
  1092. }
  1093. #if 0
  1094. // per-backend timings
  1095. fprintf(stderr, "sched_compute_splits times (%d splits):\n", sched->n_splits);
  1096. for (int i = 0; i < sched->n_backends; i++) {
  1097. if (copy_us[i] > 0 || compute_us[i] > 0) {
  1098. fprintf(stderr, "\t%5.5s: %lu us copy, %lu us compute\n", ggml_backend_name(sched->backends[i]), copy_us[i], compute_us[i]);
  1099. }
  1100. }
  1101. #endif
  1102. }
  1103. static void sched_reset(ggml_backend_sched_t sched) {
  1104. for (int i = 0; i < sched->n_backends; i++) {
  1105. ggml_tallocr_reset(sched->tallocs[i]);
  1106. }
  1107. // reset state for the next run
  1108. size_t hash_size = sched->hash_set.size;
  1109. memset(sched->hash_set.keys, 0, sizeof(sched->hash_set.keys[0]) * hash_size);
  1110. memset(sched->node_talloc, 0, sizeof(sched->node_talloc[0]) * hash_size);
  1111. memset(sched->node_copies, 0, sizeof(sched->node_copies[0]) * hash_size);
  1112. sched->is_reset = true;
  1113. }
  1114. ggml_backend_sched_t ggml_backend_sched_new(ggml_backend_t * backends, ggml_backend_buffer_type_t * bufts, int n_backends, size_t graph_size) {
  1115. GGML_ASSERT(n_backends > 0);
  1116. GGML_ASSERT(n_backends <= GGML_MAX_BACKENDS);
  1117. struct ggml_backend_sched * sched = calloc(sizeof(struct ggml_backend_sched), 1);
  1118. // initialize hash table
  1119. sched->hash_set = ggml_hash_set_new(graph_size + GGML_MAX_SPLITS*GGML_MAX_SPLIT_INPUTS);
  1120. sched->node_talloc = calloc(sizeof(sched->node_talloc[0]) * sched->hash_set.size, 1);
  1121. sched->node_copies = calloc(sizeof(sched->node_copies[0]) * sched->hash_set.size, 1);
  1122. sched->n_backends = n_backends;
  1123. for (int i = 0; i < n_backends; i++) {
  1124. sched->backends[i] = backends[i];
  1125. sched->bufts[i] = bufts ? bufts[i] : ggml_backend_get_default_buffer_type(backends[i]);
  1126. }
  1127. sched->galloc = ggml_gallocr_new();
  1128. // init measure allocs for each backend
  1129. for (int i = 0; i < n_backends; i++) {
  1130. sched->tallocs[i] = ggml_tallocr_new_measure_from_buft(sched->bufts[i]);
  1131. }
  1132. sched_reset(sched);
  1133. return sched;
  1134. }
  1135. void ggml_backend_sched_free(ggml_backend_sched_t sched) {
  1136. if (sched == NULL) {
  1137. return;
  1138. }
  1139. for (int i = 0; i < sched->n_backends; i++) {
  1140. ggml_tallocr_free(sched->tallocs[i]);
  1141. }
  1142. ggml_gallocr_free(sched->galloc);
  1143. ggml_free(sched->ctx);
  1144. free(sched->hash_set.keys);
  1145. free(sched->node_talloc);
  1146. free(sched->node_copies);
  1147. free(sched);
  1148. }
  1149. void ggml_backend_sched_init_measure(ggml_backend_sched_t sched, struct ggml_cgraph * measure_graph) {
  1150. GGML_ASSERT(ggml_tallocr_is_measure(sched->tallocs[0])); // can only be initialized once
  1151. sched_split_graph(sched, measure_graph);
  1152. sched_alloc_splits(sched);
  1153. // allocate buffers and reset allocators
  1154. for (int i = 0; i < sched->n_backends; i++) {
  1155. size_t size = ggml_tallocr_max_size(sched->tallocs[i]);
  1156. ggml_tallocr_free(sched->tallocs[i]);
  1157. sched->tallocs[i] = ggml_tallocr_new_from_buft(sched->bufts[i], size);
  1158. }
  1159. sched_reset(sched);
  1160. }
  1161. void ggml_backend_sched_graph_compute(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  1162. GGML_ASSERT((int)sched->hash_set.size >= graph->n_nodes + GGML_MAX_SPLITS*GGML_MAX_SPLIT_INPUTS);
  1163. if (!sched->is_reset) {
  1164. sched_reset(sched);
  1165. }
  1166. sched_split_graph(sched, graph);
  1167. sched_alloc_splits(sched);
  1168. sched_compute_splits(sched);
  1169. }
  1170. void ggml_backend_sched_reset(ggml_backend_sched_t sched) {
  1171. sched_reset(sched);
  1172. }
  1173. int ggml_backend_sched_get_n_splits(ggml_backend_sched_t sched) {
  1174. return sched->n_splits;
  1175. }
  1176. ggml_tallocr_t ggml_backend_sched_get_tallocr(ggml_backend_sched_t sched, ggml_backend_t backend) {
  1177. int backend_index = sched_backend_prio(sched, backend);
  1178. GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
  1179. return sched->tallocs[backend_index];
  1180. }
  1181. ggml_backend_buffer_t ggml_backend_sched_get_buffer(ggml_backend_sched_t sched, ggml_backend_t backend) {
  1182. int backend_index = sched_backend_prio(sched, backend);
  1183. GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
  1184. return ggml_tallocr_get_buffer(sched->tallocs[backend_index]);
  1185. }
  1186. void ggml_backend_sched_set_node_backend(ggml_backend_sched_t sched, struct ggml_tensor * node, ggml_backend_t backend) {
  1187. int backend_index = sched_backend_prio(sched, backend);
  1188. GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
  1189. node_allocr(node) = sched->tallocs[backend_index];
  1190. }
  1191. ggml_backend_t ggml_backend_sched_get_node_backend(ggml_backend_sched_t sched, struct ggml_tensor * node) {
  1192. ggml_tallocr_t allocr = node_allocr(node);
  1193. if (allocr == NULL) {
  1194. return NULL;
  1195. }
  1196. return get_allocr_backend(sched, allocr);
  1197. }
  1198. // utils
  1199. void ggml_backend_view_init(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) {
  1200. GGML_ASSERT(tensor->buffer == NULL);
  1201. //GGML_ASSERT(tensor->data == NULL); // views of pre-allocated tensors may have the data set in ggml_new_tensor, but still need to be initialized by the backend
  1202. GGML_ASSERT(tensor->view_src != NULL);
  1203. GGML_ASSERT(tensor->view_src->buffer != NULL);
  1204. GGML_ASSERT(tensor->view_src->data != NULL);
  1205. tensor->buffer = buffer;
  1206. tensor->data = (char *)tensor->view_src->data + tensor->view_offs;
  1207. tensor->backend = tensor->view_src->backend;
  1208. ggml_backend_buffer_init_tensor(buffer, tensor);
  1209. }
  1210. void ggml_backend_tensor_alloc(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, void * addr) {
  1211. GGML_ASSERT(tensor->buffer == NULL);
  1212. GGML_ASSERT(tensor->data == NULL);
  1213. GGML_ASSERT(tensor->view_src == NULL);
  1214. GGML_ASSERT(addr >= ggml_backend_buffer_get_base(buffer));
  1215. GGML_ASSERT((char *)addr + ggml_backend_buffer_get_alloc_size(buffer, tensor) <=
  1216. (char *)ggml_backend_buffer_get_base(buffer) + ggml_backend_buffer_get_size(buffer));
  1217. tensor->buffer = buffer;
  1218. tensor->data = addr;
  1219. ggml_backend_buffer_init_tensor(buffer, tensor);
  1220. }
  1221. static struct ggml_tensor * graph_dup_tensor(struct ggml_hash_set hash_set, struct ggml_tensor ** node_copies,
  1222. struct ggml_context * ctx_allocated, struct ggml_context * ctx_unallocated, struct ggml_tensor * src) {
  1223. GGML_ASSERT(src != NULL);
  1224. GGML_ASSERT(src->data && "graph must be allocated");
  1225. size_t id = ggml_hash_insert(hash_set, src);
  1226. if (id == GGML_HASHTABLE_ALREADY_EXISTS) {
  1227. return node_copies[ggml_hash_find(hash_set, src)];
  1228. }
  1229. struct ggml_tensor * dst = ggml_dup_tensor_layout(src->data && !src->view_src ? ctx_allocated : ctx_unallocated, src);
  1230. if (src->view_src != NULL) {
  1231. dst->view_src = graph_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, src->view_src);
  1232. dst->view_offs = src->view_offs;
  1233. }
  1234. dst->op = src->op;
  1235. memcpy(dst->op_params, src->op_params, sizeof(dst->op_params));
  1236. ggml_set_name(dst, src->name);
  1237. // copy src
  1238. for (int i = 0; i < GGML_MAX_SRC; i++) {
  1239. struct ggml_tensor * s = src->src[i];
  1240. if (s == NULL) {
  1241. break;
  1242. }
  1243. dst->src[i] = graph_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, s);
  1244. }
  1245. node_copies[id] = dst;
  1246. return dst;
  1247. }
  1248. static void graph_init_tensor(struct ggml_hash_set hash_set, struct ggml_tensor ** node_copies, bool * node_init, struct ggml_tensor * src) {
  1249. size_t id = ggml_hash_find(hash_set, src);
  1250. if (node_init[id]) {
  1251. return;
  1252. }
  1253. node_init[id] = true;
  1254. struct ggml_tensor * dst = node_copies[id];
  1255. if (dst->view_src != NULL) {
  1256. graph_init_tensor(hash_set, node_copies, node_init, src->view_src);
  1257. ggml_backend_view_init(dst->view_src->buffer, dst);
  1258. }
  1259. else {
  1260. ggml_backend_tensor_copy(src, dst);
  1261. }
  1262. // init src
  1263. for (int i = 0; i < GGML_MAX_SRC; i++) {
  1264. struct ggml_tensor * s = src->src[i];
  1265. if (s == NULL) {
  1266. break;
  1267. }
  1268. graph_init_tensor(hash_set, node_copies, node_init, s);
  1269. }
  1270. }
  1271. struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph) {
  1272. struct ggml_hash_set hash_set = {
  1273. /* .size = */ graph->visited_hash_table.size,
  1274. /* .keys = */ calloc(sizeof(hash_set.keys[0]) * graph->visited_hash_table.size, 1)
  1275. };
  1276. struct ggml_tensor ** node_copies = calloc(sizeof(node_copies[0]) * hash_set.size, 1);
  1277. bool * node_init = calloc(sizeof(node_init[0]) * hash_set.size, 1);
  1278. struct ggml_init_params params = {
  1279. /* .mem_size = */ ggml_tensor_overhead()*hash_set.size + ggml_graph_overhead_custom(graph->size, false),
  1280. /* .mem_buffer = */ NULL,
  1281. /* .no_alloc = */ true
  1282. };
  1283. struct ggml_context * ctx_allocated = ggml_init(params);
  1284. struct ggml_context * ctx_unallocated = ggml_init(params);
  1285. if (ctx_allocated == NULL || ctx_unallocated == NULL) {
  1286. fprintf(stderr, "failed to allocate context for graph copy\n");
  1287. free(hash_set.keys);
  1288. free(node_copies);
  1289. free(node_init);
  1290. ggml_free(ctx_allocated);
  1291. ggml_free(ctx_unallocated);
  1292. return (struct ggml_backend_graph_copy) {
  1293. /* .buffer = */ NULL,
  1294. /* .ctx_allocated = */ NULL,
  1295. /* .ctx_unallocated = */ NULL,
  1296. /* .graph = */ NULL,
  1297. };
  1298. }
  1299. // dup nodes
  1300. for (int i = 0; i < graph->n_nodes; i++) {
  1301. struct ggml_tensor * node = graph->nodes[i];
  1302. graph_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, node);
  1303. }
  1304. // allocate nodes
  1305. ggml_backend_buffer_t buffer = ggml_backend_alloc_ctx_tensors(ctx_allocated, backend);
  1306. if (buffer == NULL) {
  1307. fprintf(stderr, "failed to allocate buffer for graph copy\n");
  1308. free(hash_set.keys);
  1309. free(node_copies);
  1310. free(node_init);
  1311. ggml_free(ctx_allocated);
  1312. ggml_free(ctx_unallocated);
  1313. return (struct ggml_backend_graph_copy) {
  1314. /* .buffer = */ NULL,
  1315. /* .ctx_allocated = */ NULL,
  1316. /* .ctx_unallocated = */ NULL,
  1317. /* .graph = */ NULL,
  1318. };
  1319. }
  1320. //printf("copy buffer size: %zu MB\n", ggml_backend_buffer_get_size(buffer) / 1024 / 1024);
  1321. // copy data and init views
  1322. for (int i = 0; i < graph->n_nodes; i++) {
  1323. struct ggml_tensor * node = graph->nodes[i];
  1324. graph_init_tensor(hash_set, node_copies, node_init, node);
  1325. }
  1326. // build graph copy
  1327. struct ggml_cgraph * graph_copy = ggml_new_graph_custom(ctx_allocated, graph->size, false);
  1328. for (int i = 0; i < graph->n_nodes; i++) {
  1329. struct ggml_tensor * node = graph->nodes[i];
  1330. struct ggml_tensor * node_copy = node_copies[ggml_hash_find(hash_set, node)];
  1331. graph_copy->nodes[i] = node_copy;
  1332. }
  1333. graph_copy->n_nodes = graph->n_nodes;
  1334. free(hash_set.keys);
  1335. free(node_copies);
  1336. free(node_init);
  1337. return (struct ggml_backend_graph_copy) {
  1338. /* .buffer = */ buffer,
  1339. /* .ctx_allocated = */ ctx_allocated,
  1340. /* .ctx_unallocated = */ ctx_unallocated,
  1341. /* .graph = */ graph_copy,
  1342. };
  1343. }
  1344. void ggml_backend_graph_copy_free(struct ggml_backend_graph_copy copy) {
  1345. ggml_backend_buffer_free(copy.buffer);
  1346. ggml_free(copy.ctx_allocated);
  1347. ggml_free(copy.ctx_unallocated);
  1348. }
  1349. bool ggml_backend_compare_graph_backend(ggml_backend_t backend1, ggml_backend_t backend2, struct ggml_cgraph * graph, ggml_backend_eval_callback callback, void * user_data) {
  1350. struct ggml_backend_graph_copy copy = ggml_backend_graph_copy(backend2, graph);
  1351. if (copy.buffer == NULL) {
  1352. return false;
  1353. }
  1354. struct ggml_cgraph * g1 = graph;
  1355. struct ggml_cgraph * g2 = copy.graph;
  1356. assert(g1->n_nodes == g2->n_nodes);
  1357. for (int i = 0; i < g1->n_nodes; i++) {
  1358. //printf("eval %d/%d\n", i, g1->n_nodes);
  1359. struct ggml_tensor * t1 = g1->nodes[i];
  1360. struct ggml_tensor * t2 = g2->nodes[i];
  1361. assert(t1->op == t2->op && ggml_are_same_layout(t1, t2));
  1362. struct ggml_cgraph g1v = ggml_graph_view(g1, i, i + 1);
  1363. struct ggml_cgraph g2v = ggml_graph_view(g2, i, i + 1);
  1364. ggml_backend_graph_compute(backend1, &g1v);
  1365. ggml_backend_graph_compute(backend2, &g2v);
  1366. if (ggml_is_view_op(t1->op)) {
  1367. continue;
  1368. }
  1369. // compare results, calculate rms etc
  1370. if (!callback(i, t1, t2, user_data)) {
  1371. break;
  1372. }
  1373. }
  1374. ggml_backend_graph_copy_free(copy);
  1375. return true;
  1376. }