ggml-backend.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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_CALL 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_max_size(ggml_backend_buffer_type_t buft) {
  22. // get_max_size is optional, defaults to SIZE_MAX
  23. if (buft->iface.get_max_size) {
  24. return buft->iface.get_max_size(buft);
  25. }
  26. return SIZE_MAX;
  27. }
  28. GGML_CALL size_t ggml_backend_buft_get_alloc_size(ggml_backend_buffer_type_t buft, struct ggml_tensor * tensor) {
  29. // get_alloc_size is optional, defaults to ggml_nbytes
  30. if (buft->iface.get_alloc_size) {
  31. size_t size = buft->iface.get_alloc_size(buft, tensor);
  32. assert(size >= ggml_nbytes(tensor));
  33. return size;
  34. }
  35. return ggml_nbytes(tensor);
  36. }
  37. bool ggml_backend_buft_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  38. return buft->iface.supports_backend(buft, backend);
  39. }
  40. bool ggml_backend_buft_is_host(ggml_backend_buffer_type_t buft) {
  41. if (buft->iface.is_host) {
  42. return buft->iface.is_host(buft);
  43. }
  44. return false;
  45. }
  46. // backend buffer
  47. GGML_CALL ggml_backend_buffer_t ggml_backend_buffer_init(
  48. ggml_backend_buffer_type_t buft,
  49. struct ggml_backend_buffer_i iface,
  50. ggml_backend_buffer_context_t context,
  51. size_t size) {
  52. ggml_backend_buffer_t buffer = malloc(sizeof(struct ggml_backend_buffer));
  53. (*buffer) = (struct ggml_backend_buffer) {
  54. /* .interface = */ iface,
  55. /* .buft = */ buft,
  56. /* .context = */ context,
  57. /* .size = */ size,
  58. /* .usage = */ GGML_BACKEND_BUFFER_USAGE_ANY
  59. };
  60. return buffer;
  61. }
  62. const char * ggml_backend_buffer_name(ggml_backend_buffer_t buffer) {
  63. return buffer->iface.get_name(buffer);
  64. }
  65. void ggml_backend_buffer_free(ggml_backend_buffer_t buffer) {
  66. if (buffer == NULL) {
  67. return;
  68. }
  69. if (buffer->iface.free_buffer != NULL) {
  70. buffer->iface.free_buffer(buffer);
  71. }
  72. free(buffer);
  73. }
  74. size_t ggml_backend_buffer_get_size(ggml_backend_buffer_t buffer) {
  75. return buffer->size;
  76. }
  77. void * ggml_backend_buffer_get_base(ggml_backend_buffer_t buffer) {
  78. void * base = buffer->iface.get_base(buffer);
  79. GGML_ASSERT(base != NULL && "backend buffer base cannot be NULL");
  80. return base;
  81. }
  82. GGML_CALL void ggml_backend_buffer_init_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) {
  83. // init_tensor is optional
  84. if (buffer->iface.init_tensor) {
  85. buffer->iface.init_tensor(buffer, tensor);
  86. }
  87. }
  88. size_t ggml_backend_buffer_get_alignment (ggml_backend_buffer_t buffer) {
  89. return ggml_backend_buft_get_alignment(ggml_backend_buffer_get_type(buffer));
  90. }
  91. size_t ggml_backend_buffer_get_max_size(ggml_backend_buffer_t buffer) {
  92. return ggml_backend_buft_get_max_size(ggml_backend_buffer_get_type(buffer));
  93. }
  94. size_t ggml_backend_buffer_get_alloc_size(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) {
  95. return ggml_backend_buft_get_alloc_size(ggml_backend_buffer_get_type(buffer), tensor);
  96. }
  97. void ggml_backend_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  98. buffer->iface.clear(buffer, value);
  99. }
  100. bool ggml_backend_buffer_is_host(ggml_backend_buffer_t buffer) {
  101. return ggml_backend_buft_is_host(ggml_backend_buffer_get_type(buffer));
  102. }
  103. void ggml_backend_buffer_set_usage(ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage) {
  104. buffer->usage = usage;
  105. // FIXME: add a generic callback to the buffer interface
  106. if (ggml_backend_buffer_is_multi_buffer(buffer)) {
  107. ggml_backend_multi_buffer_set_usage(buffer, usage);
  108. }
  109. }
  110. ggml_backend_buffer_type_t ggml_backend_buffer_get_type(ggml_backend_buffer_t buffer) {
  111. return buffer->buft;
  112. }
  113. void ggml_backend_buffer_reset(ggml_backend_buffer_t buffer) {
  114. if (buffer->iface.reset) {
  115. buffer->iface.reset(buffer);
  116. }
  117. }
  118. bool ggml_backend_buffer_copy_tensor(const struct ggml_tensor * src, struct ggml_tensor * dst) {
  119. ggml_backend_buffer_t dst_buf = dst->view_src ? dst->view_src->buffer : dst->buffer;
  120. if (dst_buf->iface.cpy_tensor) {
  121. return src->buffer->iface.cpy_tensor(dst_buf, src, dst);
  122. }
  123. return false;
  124. }
  125. // backend
  126. ggml_guid_t ggml_backend_guid(ggml_backend_t backend) {
  127. if (backend == NULL) {
  128. return NULL;
  129. }
  130. return backend->guid;
  131. }
  132. const char * ggml_backend_name(ggml_backend_t backend) {
  133. if (backend == NULL) {
  134. return "NULL";
  135. }
  136. return backend->iface.get_name(backend);
  137. }
  138. void ggml_backend_free(ggml_backend_t backend) {
  139. if (backend == NULL) {
  140. return;
  141. }
  142. backend->iface.free(backend);
  143. }
  144. ggml_backend_buffer_type_t ggml_backend_get_default_buffer_type(ggml_backend_t backend) {
  145. return backend->iface.get_default_buffer_type(backend);
  146. }
  147. ggml_backend_buffer_t ggml_backend_alloc_buffer(ggml_backend_t backend, size_t size) {
  148. return ggml_backend_buft_alloc_buffer(ggml_backend_get_default_buffer_type(backend), size);
  149. }
  150. size_t ggml_backend_get_alignment(ggml_backend_t backend) {
  151. return ggml_backend_buft_get_alignment(ggml_backend_get_default_buffer_type(backend));
  152. }
  153. size_t ggml_backend_get_max_size(ggml_backend_t backend) {
  154. return ggml_backend_buft_get_max_size(ggml_backend_get_default_buffer_type(backend));
  155. }
  156. void ggml_backend_tensor_set_async(ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  157. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  158. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor write out of bounds");
  159. if (backend->iface.set_tensor_async == NULL) {
  160. ggml_backend_tensor_set(tensor, data, offset, size);
  161. } else {
  162. backend->iface.set_tensor_async(backend, tensor, data, offset, size);
  163. }
  164. }
  165. void ggml_backend_tensor_get_async(ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  166. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  167. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor read out of bounds");
  168. if (backend->iface.get_tensor_async == NULL) {
  169. ggml_backend_tensor_get(tensor, data, offset, size);
  170. } else {
  171. backend->iface.get_tensor_async(backend, tensor, data, offset, size);
  172. }
  173. }
  174. GGML_CALL void ggml_backend_tensor_set(struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  175. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  176. GGML_ASSERT(buf != NULL && "tensor buffer not set");
  177. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  178. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor write out of bounds");
  179. if (!size) {
  180. return;
  181. }
  182. buf->iface.set_tensor(buf, tensor, data, offset, size);
  183. }
  184. GGML_CALL void ggml_backend_tensor_get(const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
  185. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  186. GGML_ASSERT(buf != NULL && "tensor buffer not set");
  187. GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
  188. GGML_ASSERT(offset + size <= ggml_nbytes(tensor) && "tensor read out of bounds");
  189. if (!size) {
  190. return;
  191. }
  192. buf->iface.get_tensor(buf, tensor, data, offset, size);
  193. }
  194. void ggml_backend_synchronize(ggml_backend_t backend) {
  195. if (backend->iface.synchronize == NULL) {
  196. return;
  197. }
  198. backend->iface.synchronize(backend);
  199. }
  200. ggml_backend_graph_plan_t ggml_backend_graph_plan_create(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  201. GGML_ASSERT(backend->iface.graph_plan_create != NULL);
  202. return backend->iface.graph_plan_create(backend, cgraph);
  203. }
  204. void ggml_backend_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  205. GGML_ASSERT(backend->iface.graph_plan_free != NULL);
  206. backend->iface.graph_plan_free(backend, plan);
  207. }
  208. enum ggml_status ggml_backend_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  209. GGML_ASSERT(backend->iface.graph_plan_compute != NULL);
  210. return backend->iface.graph_plan_compute(backend, plan);
  211. }
  212. enum ggml_status ggml_backend_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  213. enum ggml_status err = ggml_backend_graph_compute_async(backend, cgraph);
  214. ggml_backend_synchronize(backend);
  215. return err;
  216. }
  217. bool ggml_backend_graph_compute_async(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  218. return backend->iface.graph_compute(backend, cgraph);
  219. }
  220. bool ggml_backend_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) {
  221. return backend->iface.supports_op(backend, op);
  222. }
  223. // backend copy
  224. static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {
  225. if (a->type != b->type) {
  226. return false;
  227. }
  228. for (int i = 0; i < GGML_MAX_DIMS; i++) {
  229. if (a->ne[i] != b->ne[i]) {
  230. return false;
  231. }
  232. if (a->nb[i] != b->nb[i]) {
  233. return false;
  234. }
  235. }
  236. return true;
  237. }
  238. void ggml_backend_tensor_copy(struct ggml_tensor * src, struct ggml_tensor * dst) {
  239. GGML_ASSERT(ggml_are_same_layout(src, dst) && "cannot copy tensors with different layouts");
  240. if (src == dst) {
  241. return;
  242. }
  243. if (ggml_backend_buffer_is_host(src->buffer)) {
  244. ggml_backend_tensor_set(dst, src->data, 0, ggml_nbytes(src));
  245. } else if (ggml_backend_buffer_is_host(dst->buffer)) {
  246. ggml_backend_tensor_get(src, dst->data, 0, ggml_nbytes(src));
  247. } else if (!ggml_backend_buffer_copy_tensor(src, dst)) {
  248. #ifndef NDEBUG
  249. fprintf(stderr, "%s: warning: slow copy from %s to %s\n", __func__, ggml_backend_buffer_name(src->buffer), ggml_backend_buffer_name(dst->buffer));
  250. #endif
  251. size_t nbytes = ggml_nbytes(src);
  252. void * data = malloc(nbytes);
  253. ggml_backend_tensor_get(src, data, 0, nbytes);
  254. ggml_backend_tensor_set(dst, data, 0, nbytes);
  255. free(data);
  256. }
  257. }
  258. void ggml_backend_tensor_copy_async(ggml_backend_t backend_src, ggml_backend_t backend_dst, struct ggml_tensor * src, struct ggml_tensor * dst) {
  259. GGML_ASSERT(ggml_are_same_layout(src, dst) && "cannot copy tensors with different layouts");
  260. if (src == dst) {
  261. return;
  262. }
  263. if (backend_dst->iface.cpy_tensor_async != NULL) {
  264. if (backend_dst->iface.cpy_tensor_async(backend_src, backend_dst, src, dst)) {
  265. return;
  266. }
  267. }
  268. // an async copy would normally happen after all the queued operations on both backends are completed
  269. // sync src, set_async dst
  270. if (ggml_backend_buffer_is_host(src->buffer)) {
  271. ggml_backend_synchronize(backend_src);
  272. ggml_backend_tensor_set_async(backend_dst, dst, src->data, 0, ggml_nbytes(src));
  273. } else {
  274. ggml_backend_synchronize(backend_src);
  275. ggml_backend_tensor_copy(src, dst);
  276. ggml_backend_synchronize(backend_dst);
  277. }
  278. }
  279. // events
  280. ggml_backend_event_t ggml_backend_event_new(ggml_backend_t backend) {
  281. if (backend->iface.event_new == NULL) {
  282. return NULL;
  283. }
  284. return backend->iface.event_new(backend);
  285. }
  286. void ggml_backend_event_free(ggml_backend_event_t event) {
  287. if (event == NULL) {
  288. return;
  289. }
  290. event->backend->iface.event_free(event);
  291. }
  292. void ggml_backend_event_record(ggml_backend_event_t event) {
  293. GGML_ASSERT(event->backend->iface.event_record != NULL);
  294. event->backend->iface.event_record(event);
  295. }
  296. void ggml_backend_event_synchronize(ggml_backend_event_t event) {
  297. GGML_ASSERT(event->backend->iface.event_synchronize != NULL);
  298. event->backend->iface.event_synchronize(event);
  299. }
  300. void ggml_backend_event_wait(ggml_backend_t backend, ggml_backend_event_t event) {
  301. GGML_ASSERT(backend->iface.event_wait != NULL);
  302. backend->iface.event_wait(backend, event);
  303. }
  304. // backend registry
  305. #define GGML_REG_MAX_BACKENDS 16
  306. struct ggml_backend_reg {
  307. char name[128];
  308. ggml_backend_init_fn init_fn;
  309. ggml_backend_buffer_type_t default_buffer_type;
  310. void * user_data;
  311. };
  312. static struct ggml_backend_reg ggml_backend_registry[GGML_REG_MAX_BACKENDS];
  313. static size_t ggml_backend_registry_count = 0;
  314. GGML_CALL static ggml_backend_t ggml_backend_reg_cpu_init(const char * params, void * user_data);
  315. GGML_CALL static void ggml_backend_registry_init(void) {
  316. static bool initialized = false;
  317. if (initialized) {
  318. return;
  319. }
  320. initialized = true;
  321. ggml_backend_register("CPU", ggml_backend_reg_cpu_init, ggml_backend_cpu_buffer_type(), NULL);
  322. // add forward decls here to avoid including the backend headers
  323. #ifdef GGML_USE_CUBLAS
  324. extern GGML_CALL void ggml_backend_cuda_reg_devices(void);
  325. ggml_backend_cuda_reg_devices();
  326. #endif
  327. #ifdef GGML_USE_SYCL
  328. extern void ggml_backend_sycl_reg_devices(void);
  329. ggml_backend_sycl_reg_devices();
  330. #endif
  331. #ifdef GGML_USE_METAL
  332. extern GGML_CALL ggml_backend_t ggml_backend_reg_metal_init(const char * params, void * user_data);
  333. extern GGML_CALL ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
  334. ggml_backend_register("Metal", ggml_backend_reg_metal_init, ggml_backend_metal_buffer_type(), NULL);
  335. #endif
  336. #ifdef GGML_USE_VULKAN
  337. extern GGML_CALL int ggml_backend_vk_reg_devices(void);
  338. ggml_backend_vk_reg_devices();
  339. #endif
  340. #ifdef GGML_USE_KOMPUTE
  341. extern GGML_CALL void ggml_backend_kompute_reg_devices(void);
  342. ggml_backend_kompute_reg_devices();
  343. #endif
  344. }
  345. GGML_CALL void ggml_backend_register(const char * name, ggml_backend_init_fn init_fn, ggml_backend_buffer_type_t default_buffer_type, void * user_data) {
  346. GGML_ASSERT(ggml_backend_registry_count < GGML_REG_MAX_BACKENDS);
  347. size_t id = ggml_backend_registry_count;
  348. ggml_backend_registry[id] = (struct ggml_backend_reg) {
  349. /* .name = */ {0},
  350. /* .fn = */ init_fn,
  351. /* .default_buffer_type = */ default_buffer_type,
  352. /* .user_data = */ user_data,
  353. };
  354. snprintf(ggml_backend_registry[id].name, sizeof(ggml_backend_registry[id].name), "%s", name);
  355. #ifndef NDEBUG
  356. fprintf(stderr, "%s: registered backend %s\n", __func__, name);
  357. #endif
  358. ggml_backend_registry_count++;
  359. }
  360. size_t ggml_backend_reg_get_count(void) {
  361. ggml_backend_registry_init();
  362. return ggml_backend_registry_count;
  363. }
  364. size_t ggml_backend_reg_find_by_name(const char * name) {
  365. ggml_backend_registry_init();
  366. for (size_t i = 0; i < ggml_backend_registry_count; i++) {
  367. // TODO: case insensitive in a portable way
  368. if (strcmp(ggml_backend_registry[i].name, name) == 0) {
  369. return i;
  370. }
  371. }
  372. // not found
  373. return SIZE_MAX;
  374. }
  375. // init from backend:params string
  376. ggml_backend_t ggml_backend_reg_init_backend_from_str(const char * backend_str) {
  377. ggml_backend_registry_init();
  378. const char * params = strchr(backend_str, ':');
  379. char backend_name[128];
  380. if (params == NULL) {
  381. snprintf(backend_name, sizeof(backend_name), "%s", backend_str);
  382. params = "";
  383. } else {
  384. snprintf(backend_name, sizeof(backend_name), "%.*s", (int)(params - backend_str), backend_str);
  385. params++;
  386. }
  387. size_t backend_i = ggml_backend_reg_find_by_name(backend_name);
  388. if (backend_i == SIZE_MAX) {
  389. fprintf(stderr, "%s: backend %s not found\n", __func__, backend_name);
  390. return NULL;
  391. }
  392. return ggml_backend_reg_init_backend(backend_i, params);
  393. }
  394. const char * ggml_backend_reg_get_name(size_t i) {
  395. ggml_backend_registry_init();
  396. GGML_ASSERT(i < ggml_backend_registry_count);
  397. return ggml_backend_registry[i].name;
  398. }
  399. ggml_backend_t ggml_backend_reg_init_backend(size_t i, const char * params) {
  400. ggml_backend_registry_init();
  401. GGML_ASSERT(i < ggml_backend_registry_count);
  402. return ggml_backend_registry[i].init_fn(params, ggml_backend_registry[i].user_data);
  403. }
  404. ggml_backend_buffer_type_t ggml_backend_reg_get_default_buffer_type(size_t i) {
  405. ggml_backend_registry_init();
  406. GGML_ASSERT(i < ggml_backend_registry_count);
  407. return ggml_backend_registry[i].default_buffer_type;
  408. }
  409. ggml_backend_buffer_t ggml_backend_reg_alloc_buffer(size_t i, size_t size) {
  410. ggml_backend_registry_init();
  411. GGML_ASSERT(i < ggml_backend_registry_count);
  412. return ggml_backend_buft_alloc_buffer(ggml_backend_registry[i].default_buffer_type, size);
  413. }
  414. // backend CPU
  415. static const size_t TENSOR_ALIGNMENT = 32; // required for mmap as gguf only guarantees 32-byte alignment
  416. GGML_CALL static const char * ggml_backend_cpu_buffer_name(ggml_backend_buffer_t buffer) {
  417. return "CPU";
  418. GGML_UNUSED(buffer);
  419. }
  420. GGML_CALL static void * ggml_backend_cpu_buffer_get_base(ggml_backend_buffer_t buffer) {
  421. uintptr_t data = (uintptr_t)buffer->context;
  422. // align the buffer
  423. if (data % TENSOR_ALIGNMENT != 0) {
  424. data = GGML_PAD(data, TENSOR_ALIGNMENT);
  425. }
  426. return (void *)data;
  427. }
  428. GGML_CALL static void ggml_backend_cpu_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  429. free(buffer->context);
  430. }
  431. GGML_CALL 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) {
  432. memcpy((char *)tensor->data + offset, data, size);
  433. GGML_UNUSED(buffer);
  434. }
  435. GGML_CALL 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) {
  436. memcpy(data, (const char *)tensor->data + offset, size);
  437. GGML_UNUSED(buffer);
  438. }
  439. GGML_CALL static bool ggml_backend_cpu_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst) {
  440. if (ggml_backend_buffer_is_host(src->buffer)) {
  441. memcpy(dst->data, src->data, ggml_nbytes(src));
  442. return true;
  443. }
  444. return false;
  445. GGML_UNUSED(buffer);
  446. }
  447. GGML_CALL static void ggml_backend_cpu_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  448. memset(buffer->context, value, buffer->size);
  449. }
  450. static struct ggml_backend_buffer_i cpu_backend_buffer_i = {
  451. /* .get_name = */ ggml_backend_cpu_buffer_name,
  452. /* .free_buffer = */ ggml_backend_cpu_buffer_free_buffer,
  453. /* .get_base = */ ggml_backend_cpu_buffer_get_base,
  454. /* .init_tensor = */ NULL, // no initialization required
  455. /* .set_tensor = */ ggml_backend_cpu_buffer_set_tensor,
  456. /* .get_tensor = */ ggml_backend_cpu_buffer_get_tensor,
  457. /* .cpy_tensor = */ ggml_backend_cpu_buffer_cpy_tensor,
  458. /* .clear = */ ggml_backend_cpu_buffer_clear,
  459. /* .reset = */ NULL,
  460. };
  461. // for buffers from ptr, free is not called
  462. static struct ggml_backend_buffer_i cpu_backend_buffer_i_from_ptr = {
  463. /* .get_name = */ ggml_backend_cpu_buffer_name,
  464. /* .free_buffer = */ NULL, // ptr is not owned by the buffer, so it does not need to be freed
  465. /* .get_base = */ ggml_backend_cpu_buffer_get_base,
  466. /* .init_tensor = */ NULL, // no initialization required
  467. /* .set_tensor = */ ggml_backend_cpu_buffer_set_tensor,
  468. /* .get_tensor = */ ggml_backend_cpu_buffer_get_tensor,
  469. /* .cpy_tensor = */ ggml_backend_cpu_buffer_cpy_tensor,
  470. /* .clear = */ ggml_backend_cpu_buffer_clear,
  471. /* .reset = */ NULL,
  472. };
  473. GGML_CALL static const char * ggml_backend_cpu_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  474. return "CPU";
  475. GGML_UNUSED(buft);
  476. }
  477. GGML_CALL static ggml_backend_buffer_t ggml_backend_cpu_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  478. size += TENSOR_ALIGNMENT; // malloc may return an address that is not aligned
  479. void * data = malloc(size); // TODO: use GGML_ALIGNED_MALLOC (move to ggml-impl.h)
  480. if (data == NULL) {
  481. fprintf(stderr, "%s: failed to allocate buffer of size %zu\n", __func__, size);
  482. return NULL;
  483. }
  484. return ggml_backend_buffer_init(buft, cpu_backend_buffer_i, data, size);
  485. }
  486. GGML_CALL static size_t ggml_backend_cpu_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  487. return TENSOR_ALIGNMENT;
  488. GGML_UNUSED(buft);
  489. }
  490. GGML_CALL static bool ggml_backend_cpu_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  491. return ggml_backend_is_cpu(backend);
  492. GGML_UNUSED(buft);
  493. }
  494. GGML_CALL static bool ggml_backend_cpu_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  495. return true;
  496. GGML_UNUSED(buft);
  497. }
  498. GGML_CALL ggml_backend_buffer_type_t ggml_backend_cpu_buffer_type(void) {
  499. static struct ggml_backend_buffer_type ggml_backend_cpu_buffer_type = {
  500. /* .iface = */ {
  501. /* .get_name = */ ggml_backend_cpu_buffer_type_get_name,
  502. /* .alloc_buffer = */ ggml_backend_cpu_buffer_type_alloc_buffer,
  503. /* .get_alignment = */ ggml_backend_cpu_buffer_type_get_alignment,
  504. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  505. /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
  506. /* .supports_backend = */ ggml_backend_cpu_buffer_type_supports_backend,
  507. /* .is_host = */ ggml_backend_cpu_buffer_type_is_host,
  508. },
  509. /* .context = */ NULL,
  510. };
  511. return &ggml_backend_cpu_buffer_type;
  512. }
  513. #ifdef GGML_USE_CPU_HBM
  514. // buffer type HBM
  515. #include <hbwmalloc.h>
  516. GGML_CALL static const char * ggml_backend_cpu_hbm_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  517. return "CPU_HBM";
  518. GGML_UNUSED(buft);
  519. }
  520. GGML_CALL static const char * ggml_backend_cpu_hbm_buffer_get_name(ggml_backend_buffer_t buf) {
  521. return "CPU_HBM";
  522. GGML_UNUSED(buf);
  523. }
  524. GGML_CALL static void ggml_backend_cpu_hbm_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  525. hbw_free(buffer->context);
  526. }
  527. GGML_CALL static ggml_backend_buffer_t ggml_backend_cpu_hbm_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  528. //void * ptr = hbw_malloc(size);
  529. void * ptr;
  530. int result = hbw_posix_memalign(&ptr, ggml_backend_cpu_buffer_type_get_alignment(buft), size);
  531. if (result != 0) {
  532. fprintf(stderr, "failed to allocate HBM buffer of size %zu\n", size);
  533. return NULL;
  534. }
  535. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  536. buffer->buft = buft;
  537. buffer->iface.get_name = ggml_backend_cpu_hbm_buffer_get_name;
  538. buffer->iface.free_buffer = ggml_backend_cpu_hbm_buffer_free_buffer;
  539. return buffer;
  540. }
  541. ggml_backend_buffer_type_t ggml_backend_cpu_hbm_buffer_type(void) {
  542. static struct ggml_backend_buffer_type ggml_backend_cpu_buffer_type_hbm = {
  543. /* .iface = */ {
  544. /* .get_name = */ ggml_backend_cpu_hbm_buffer_type_get_name,
  545. /* .alloc_buffer = */ ggml_backend_cpu_hbm_buffer_type_alloc_buffer,
  546. /* .get_alignment = */ ggml_backend_cpu_buffer_type_get_alignment,
  547. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  548. /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
  549. /* .supports_backend = */ ggml_backend_cpu_buffer_type_supports_backend,
  550. /* .is_host = */ ggml_backend_cpu_buffer_type_is_host,
  551. },
  552. /* .context = */ NULL,
  553. };
  554. return &ggml_backend_cpu_buffer_type_hbm;
  555. }
  556. #endif
  557. struct ggml_backend_cpu_context {
  558. int n_threads;
  559. void * work_data;
  560. size_t work_size;
  561. ggml_abort_callback abort_callback;
  562. void * abort_callback_data;
  563. };
  564. GGML_CALL static const char * ggml_backend_cpu_name(ggml_backend_t backend) {
  565. return "CPU";
  566. GGML_UNUSED(backend);
  567. }
  568. GGML_CALL static void ggml_backend_cpu_free(ggml_backend_t backend) {
  569. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  570. free(cpu_ctx->work_data);
  571. free(cpu_ctx);
  572. free(backend);
  573. }
  574. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_cpu_get_default_buffer_type(ggml_backend_t backend) {
  575. return ggml_backend_cpu_buffer_type();
  576. GGML_UNUSED(backend);
  577. }
  578. struct ggml_backend_plan_cpu {
  579. struct ggml_cplan cplan;
  580. struct ggml_cgraph cgraph;
  581. };
  582. GGML_CALL static ggml_backend_graph_plan_t ggml_backend_cpu_graph_plan_create(ggml_backend_t backend, const struct ggml_cgraph * cgraph) {
  583. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  584. struct ggml_backend_plan_cpu * cpu_plan = malloc(sizeof(struct ggml_backend_plan_cpu));
  585. cpu_plan->cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads);
  586. cpu_plan->cgraph = *cgraph; // FIXME: deep copy
  587. if (cpu_plan->cplan.work_size > 0) {
  588. cpu_plan->cplan.work_data = malloc(cpu_plan->cplan.work_size);
  589. }
  590. cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback;
  591. cpu_plan->cplan.abort_callback_data = cpu_ctx->abort_callback_data;
  592. return cpu_plan;
  593. }
  594. GGML_CALL static void ggml_backend_cpu_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  595. struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
  596. free(cpu_plan->cplan.work_data);
  597. free(cpu_plan);
  598. GGML_UNUSED(backend);
  599. }
  600. GGML_CALL static enum ggml_status ggml_backend_cpu_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  601. struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
  602. return ggml_graph_compute(&cpu_plan->cgraph, &cpu_plan->cplan);
  603. GGML_UNUSED(backend);
  604. }
  605. GGML_CALL static enum ggml_status ggml_backend_cpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  606. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  607. struct ggml_cplan cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads);
  608. if (cpu_ctx->work_size < cplan.work_size) {
  609. free(cpu_ctx->work_data);
  610. cpu_ctx->work_data = malloc(cplan.work_size);
  611. if (cpu_ctx->work_data == NULL) {
  612. cpu_ctx->work_size = 0;
  613. return GGML_STATUS_ALLOC_FAILED;
  614. }
  615. cpu_ctx->work_size = cplan.work_size;
  616. }
  617. cplan.work_data = cpu_ctx->work_data;
  618. cplan.abort_callback = cpu_ctx->abort_callback;
  619. cplan.abort_callback_data = cpu_ctx->abort_callback_data;
  620. return ggml_graph_compute(cgraph, &cplan);
  621. }
  622. GGML_CALL static bool ggml_backend_cpu_supports_op(ggml_backend_t backend, const struct ggml_tensor * op) {
  623. switch (op->op) {
  624. case GGML_OP_CPY:
  625. return op->type != GGML_TYPE_IQ2_XXS && op->type != GGML_TYPE_IQ2_XS && op->type != GGML_TYPE_IQ1_S; // missing type_traits.from_float
  626. case GGML_OP_MUL_MAT:
  627. return op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == ggml_internal_get_type_traits(op->src[0]->type).vec_dot_type;
  628. default:
  629. return true;
  630. }
  631. GGML_UNUSED(backend);
  632. }
  633. static struct ggml_backend_i cpu_backend_i = {
  634. /* .get_name = */ ggml_backend_cpu_name,
  635. /* .free = */ ggml_backend_cpu_free,
  636. /* .get_default_buffer_type = */ ggml_backend_cpu_get_default_buffer_type,
  637. /* .set_tensor_async = */ NULL,
  638. /* .get_tensor_async = */ NULL,
  639. /* .cpy_tensor_async = */ NULL,
  640. /* .synchronize = */ NULL,
  641. /* .graph_plan_create = */ ggml_backend_cpu_graph_plan_create,
  642. /* .graph_plan_free = */ ggml_backend_cpu_graph_plan_free,
  643. /* .graph_plan_compute = */ ggml_backend_cpu_graph_plan_compute,
  644. /* .graph_compute = */ ggml_backend_cpu_graph_compute,
  645. /* .supports_op = */ ggml_backend_cpu_supports_op,
  646. /* .event_new = */ NULL,
  647. /* .event_free = */ NULL,
  648. /* .event_record = */ NULL,
  649. /* .event_wait = */ NULL,
  650. /* .event_synchronize = */ NULL,
  651. };
  652. static ggml_guid_t ggml_backend_cpu_guid(void) {
  653. static ggml_guid guid = { 0xaa, 0x67, 0xc7, 0x43, 0x96, 0xe6, 0xa3, 0x8a, 0xe3, 0xaf, 0xea, 0x92, 0x36, 0xbc, 0xfc, 0x89 };
  654. return &guid;
  655. }
  656. ggml_backend_t ggml_backend_cpu_init(void) {
  657. struct ggml_backend_cpu_context * ctx = malloc(sizeof(struct ggml_backend_cpu_context));
  658. if (ctx == NULL) {
  659. return NULL;
  660. }
  661. ctx->n_threads = GGML_DEFAULT_N_THREADS;
  662. ctx->work_data = NULL;
  663. ctx->work_size = 0;
  664. ctx->abort_callback = NULL;
  665. ctx->abort_callback_data = NULL;
  666. ggml_backend_t cpu_backend = malloc(sizeof(struct ggml_backend));
  667. if (cpu_backend == NULL) {
  668. free(ctx);
  669. return NULL;
  670. }
  671. *cpu_backend = (struct ggml_backend) {
  672. /* .guid = */ ggml_backend_cpu_guid(),
  673. /* .interface = */ cpu_backend_i,
  674. /* .context = */ ctx
  675. };
  676. return cpu_backend;
  677. }
  678. GGML_CALL bool ggml_backend_is_cpu(ggml_backend_t backend) {
  679. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cpu_guid());
  680. }
  681. void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads) {
  682. GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
  683. struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
  684. ctx->n_threads = n_threads;
  685. }
  686. void ggml_backend_cpu_set_abort_callback(ggml_backend_t backend_cpu, ggml_abort_callback abort_callback, void * abort_callback_data) {
  687. GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
  688. struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
  689. ctx->abort_callback = abort_callback;
  690. ctx->abort_callback_data = abort_callback_data;
  691. }
  692. GGML_CALL ggml_backend_buffer_t ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size) {
  693. GGML_ASSERT((uintptr_t)ptr % TENSOR_ALIGNMENT == 0 && "buffer pointer must be aligned");
  694. return ggml_backend_buffer_init(ggml_backend_cpu_buffer_type(), cpu_backend_buffer_i_from_ptr, ptr, size);
  695. }
  696. GGML_CALL static ggml_backend_t ggml_backend_reg_cpu_init(const char * params, void * user_data) {
  697. return ggml_backend_cpu_init();
  698. GGML_UNUSED(params);
  699. GGML_UNUSED(user_data);
  700. }
  701. // multi-buffer buffer
  702. struct ggml_backend_multi_buffer_context {
  703. ggml_backend_buffer_t * buffers;
  704. size_t n_buffers;
  705. };
  706. typedef struct ggml_backend_multi_buffer_context * ggml_backend_multi_buffer_context_t;
  707. GGML_CALL static const char * ggml_backend_multi_buffer_get_name(ggml_backend_buffer_t buffer) {
  708. ggml_backend_multi_buffer_context_t ctx = (ggml_backend_multi_buffer_context_t) buffer->context;
  709. return ctx->buffers[0]->iface.get_name(ctx->buffers[0]);
  710. }
  711. GGML_CALL static void ggml_backend_multi_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  712. ggml_backend_multi_buffer_context_t ctx = (ggml_backend_multi_buffer_context_t) buffer->context;
  713. for (size_t i = 0; i < ctx->n_buffers; i++) {
  714. ggml_backend_buffer_free(ctx->buffers[i]);
  715. }
  716. free(ctx->buffers);
  717. free(ctx);
  718. }
  719. GGML_CALL static void ggml_backend_multi_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  720. ggml_backend_multi_buffer_context_t ctx = (ggml_backend_multi_buffer_context_t) buffer->context;
  721. for (size_t i = 0; i < ctx->n_buffers; i++) {
  722. ggml_backend_buffer_clear(ctx->buffers[i], value);
  723. }
  724. }
  725. static struct ggml_backend_buffer_i ggml_backend_multi_buffer_context_interface(void) {
  726. static struct ggml_backend_buffer_i multi_backend_buffer_i = {
  727. /* .get_name = */ ggml_backend_multi_buffer_get_name,
  728. /* .free_buffer = */ ggml_backend_multi_buffer_free_buffer,
  729. /* .get_base = */ NULL,
  730. /* .init_tensor = */ NULL,
  731. /* .set_tensor = */ NULL,
  732. /* .get_tensor = */ NULL,
  733. /* .cpy_tensor = */ NULL,
  734. /* .clear = */ ggml_backend_multi_buffer_clear,
  735. /* .reset = */ NULL,
  736. };
  737. return multi_backend_buffer_i;
  738. }
  739. GGML_CALL ggml_backend_buffer_t ggml_backend_multi_buffer_alloc_buffer(ggml_backend_buffer_t * buffers, size_t n_buffers) {
  740. ggml_backend_multi_buffer_context_t ctx = (ggml_backend_multi_buffer_context_t) malloc(sizeof(struct ggml_backend_multi_buffer_context));
  741. ctx->n_buffers = n_buffers;
  742. ctx->buffers = (ggml_backend_buffer_t *) malloc(n_buffers * sizeof(ggml_backend_buffer_t));
  743. GGML_ASSERT(ctx->buffers != NULL);
  744. size_t total_size = 0;
  745. for (size_t i = 0; i < n_buffers; i++) {
  746. ctx->buffers[i] = buffers[i];
  747. total_size += ggml_backend_buffer_get_size(buffers[i]);
  748. }
  749. return ggml_backend_buffer_init(buffers[0]->buft, ggml_backend_multi_buffer_context_interface(), ctx, total_size);
  750. }
  751. GGML_CALL bool ggml_backend_buffer_is_multi_buffer(ggml_backend_buffer_t buffer) {
  752. return buffer->iface.get_name == ggml_backend_multi_buffer_get_name;
  753. }
  754. GGML_CALL void ggml_backend_multi_buffer_set_usage(ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage) {
  755. GGML_ASSERT(ggml_backend_buffer_is_multi_buffer(buffer));
  756. ggml_backend_multi_buffer_context_t ctx = (ggml_backend_multi_buffer_context_t) buffer->context;
  757. for (size_t i = 0; i < ctx->n_buffers; i++) {
  758. ggml_backend_buffer_set_usage(ctx->buffers[i], usage);
  759. }
  760. }
  761. // creates a copy of the tensor with the same memory layout
  762. static struct ggml_tensor * ggml_dup_tensor_layout(struct ggml_context * ctx, const struct ggml_tensor * tensor) {
  763. struct ggml_tensor * dup = ggml_dup_tensor(ctx, tensor);
  764. for (int i = 0; i < GGML_MAX_DIMS; i++) {
  765. dup->nb[i] = tensor->nb[i];
  766. }
  767. return dup;
  768. }
  769. static bool ggml_is_view_op(enum ggml_op op) {
  770. return op == GGML_OP_VIEW || op == GGML_OP_RESHAPE || op == GGML_OP_PERMUTE || op == GGML_OP_TRANSPOSE;
  771. }
  772. // scheduler
  773. #ifndef GGML_SCHED_MAX_BACKENDS
  774. #define GGML_SCHED_MAX_BACKENDS 16
  775. #endif
  776. #ifndef GGML_SCHED_MAX_SPLITS
  777. #define GGML_SCHED_MAX_SPLITS 256
  778. #endif
  779. #ifndef GGML_SCHED_MAX_SPLIT_INPUTS
  780. #define GGML_SCHED_MAX_SPLIT_INPUTS 16
  781. #endif
  782. #ifndef GGML_SCHED_MAX_COPIES
  783. #define GGML_SCHED_MAX_COPIES 4
  784. #endif
  785. struct ggml_backend_sched_split {
  786. int backend_id;
  787. int i_start;
  788. int i_end;
  789. struct ggml_tensor * inputs[GGML_SCHED_MAX_SPLIT_INPUTS];
  790. int n_inputs;
  791. // graph view of this split
  792. struct ggml_cgraph graph;
  793. };
  794. struct ggml_backend_sched {
  795. bool is_reset; // true if the scheduler has been reset since the last graph split
  796. bool is_alloc;
  797. int n_backends;
  798. ggml_backend_t backends[GGML_SCHED_MAX_BACKENDS];
  799. ggml_backend_buffer_type_t bufts[GGML_SCHED_MAX_BACKENDS];
  800. ggml_gallocr_t galloc;
  801. // hash keys of the nodes in the graph
  802. struct ggml_hash_set hash_set;
  803. // hash values
  804. int * tensor_backend_id;
  805. struct ggml_tensor * (* tensor_copies)[GGML_SCHED_MAX_BACKENDS][GGML_SCHED_MAX_COPIES];
  806. int * node_backend_ids; // [graph_size]
  807. int * leaf_backend_ids; // [graph_size]
  808. // copy of the graph with modified inputs
  809. struct ggml_cgraph * graph;
  810. // graph splits
  811. struct ggml_backend_sched_split splits[GGML_SCHED_MAX_SPLITS];
  812. int n_splits;
  813. // pipeline parallelism support
  814. int n_copies;
  815. int cur_copy;
  816. ggml_backend_event_t events[GGML_SCHED_MAX_BACKENDS][GGML_SCHED_MAX_COPIES];
  817. struct ggml_tensor * graph_inputs[GGML_SCHED_MAX_SPLIT_INPUTS];
  818. int n_graph_inputs;
  819. struct ggml_context * ctx;
  820. ggml_backend_sched_eval_callback callback_eval;
  821. void * callback_eval_user_data;
  822. // align context_buffer to GGML_MEM_ALIGN
  823. #ifdef _MSC_VER
  824. __declspec(align(GGML_MEM_ALIGN))
  825. #else
  826. __attribute__((aligned(GGML_MEM_ALIGN)))
  827. #endif
  828. char context_buffer[GGML_SCHED_MAX_SPLITS*GGML_SCHED_MAX_SPLIT_INPUTS*2*sizeof(struct ggml_tensor) + sizeof(struct ggml_cgraph)];
  829. };
  830. #define hash_id(tensor) ggml_hash_find_or_insert(sched->hash_set, tensor)
  831. #define tensor_backend_id(tensor) sched->tensor_backend_id[hash_id(tensor)]
  832. // returns the priority of the backend, lower id is higher priority
  833. static int ggml_backend_sched_backend_id(ggml_backend_sched_t sched, ggml_backend_t backend) {
  834. for (int i = 0; i < sched->n_backends; i++) {
  835. if (sched->backends[i] == backend) {
  836. return i;
  837. }
  838. }
  839. return -1;
  840. }
  841. static int ggml_backend_sched_backend_from_buffer(ggml_backend_sched_t sched, const struct ggml_tensor * tensor) {
  842. ggml_backend_buffer_t buffer = tensor->buffer;
  843. if (buffer == NULL) {
  844. return -1;
  845. }
  846. // find highest prio backend that supports the buffer type
  847. for (int i = 0; i < sched->n_backends; i++) {
  848. if (ggml_backend_buft_supports_backend(buffer->buft, sched->backends[i])) {
  849. return i;
  850. }
  851. }
  852. fprintf(stderr, "%s: error: no backend supports buffer type %s used in tensor %s\n",
  853. __func__, ggml_backend_buffer_name(buffer), tensor->name);
  854. GGML_ASSERT(false);
  855. return -1;
  856. }
  857. #if 0
  858. static char causes[GGML_DEFAULT_GRAPH_SIZE*16 + GGML_SCHED_MAX_SPLITS*GGML_SCHED_MAX_SPLIT_INPUTS][128]; // debug only
  859. #define SET_CAUSE(node, ...) sprintf(causes[hash_id(node)], __VA_ARGS__)
  860. #define GET_CAUSE(node) causes[hash_id(node)]
  861. #else
  862. #define SET_CAUSE(node, ...)
  863. #define GET_CAUSE(node) ""
  864. #endif
  865. // returns the backend that should be used for the node based on the current locations
  866. static int ggml_backend_sched_backend_id_from_cur(ggml_backend_sched_t sched, struct ggml_tensor * tensor) {
  867. // TODO: use supports_op to check if the backend supports the op
  868. // assign pre-allocated nodes to their backend
  869. // dst
  870. int cur_backend = ggml_backend_sched_backend_from_buffer(sched, tensor);
  871. if (cur_backend != -1) {
  872. SET_CAUSE(tensor, "1.dst");
  873. return cur_backend;
  874. }
  875. // view_src
  876. if (tensor->view_src != NULL) {
  877. cur_backend = ggml_backend_sched_backend_from_buffer(sched, tensor->view_src);
  878. if (cur_backend != -1) {
  879. SET_CAUSE(tensor, "1.vsrc");
  880. return cur_backend;
  881. }
  882. }
  883. // input
  884. if (tensor->flags & GGML_TENSOR_FLAG_INPUT) {
  885. cur_backend = sched->n_backends - 1; // last backend (assumed CPU)
  886. SET_CAUSE(tensor, "1.inp");
  887. return cur_backend;
  888. }
  889. // assign nodes that use weights to the backend of the weights
  890. for (int i = 0; i < GGML_MAX_SRC; i++) {
  891. const struct ggml_tensor * src = tensor->src[i];
  892. if (src == NULL) {
  893. continue;
  894. }
  895. if (src->buffer != NULL && src->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
  896. int src_backend = ggml_backend_sched_backend_from_buffer(sched, src);
  897. // operations with weights are always run on the same backend as the weights
  898. SET_CAUSE(tensor, "1.wgt%d", i);
  899. return src_backend;
  900. }
  901. }
  902. return -1;
  903. }
  904. static char * fmt_size(size_t size) {
  905. static char buffer[128];
  906. if (size >= 1024*1024) {
  907. sprintf(buffer, "%zuM", size/1024/1024);
  908. } else {
  909. sprintf(buffer, "%zuK", size/1024);
  910. }
  911. return buffer;
  912. }
  913. static void ggml_backend_sched_print_assignments(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  914. int cur_split = 0;
  915. for (int i = 0; i < graph->n_nodes; i++) {
  916. if (cur_split < sched->n_splits && i == sched->splits[cur_split].i_start) {
  917. ggml_backend_t split_backend = sched->backends[sched->splits[cur_split].backend_id];
  918. fprintf(stderr, "\n## SPLIT #%d: %s # %d inputs: ", cur_split, ggml_backend_name(split_backend),
  919. sched->splits[cur_split].n_inputs);
  920. for (int j = 0; j < sched->splits[cur_split].n_inputs; j++) {
  921. fprintf(stderr, "[%s (%5.5s)] ", sched->splits[cur_split].inputs[j]->name,
  922. fmt_size(ggml_nbytes(sched->splits[cur_split].inputs[j])));
  923. }
  924. fprintf(stderr, "\n");
  925. cur_split++;
  926. }
  927. struct ggml_tensor * node = graph->nodes[i];
  928. if (ggml_is_view_op(node->op)) {
  929. continue;
  930. }
  931. ggml_backend_t tensor_backend = ggml_backend_sched_get_tensor_backend(sched, node);
  932. fprintf(stderr, "node #%3d (%10.10s): %20.20s (%5.5s) [%5.5s %8.8s]:", i, ggml_op_name(node->op), node->name,
  933. fmt_size(ggml_nbytes(node)), tensor_backend ? ggml_backend_name(tensor_backend) : "NULL", GET_CAUSE(node));
  934. for (int j = 0; j < GGML_MAX_SRC; j++) {
  935. struct ggml_tensor * src = node->src[j];
  936. if (src == NULL) {
  937. continue;
  938. }
  939. ggml_backend_t src_backend = ggml_backend_sched_get_tensor_backend(sched, src);
  940. fprintf(stderr, " %20.20s (%5.5s) [%5.5s %8.8s]", src->name,
  941. fmt_size(ggml_nbytes(src)), src_backend ? ggml_backend_name(src_backend) : "NULL", GET_CAUSE(src));
  942. }
  943. fprintf(stderr, "\n");
  944. }
  945. }
  946. //#define DEBUG_PASS1
  947. //#define DEBUG_PASS2
  948. //#define DEBUG_PASS3
  949. //#define DEBUG_PASS4
  950. // assigns backends to ops and splits the graph into subgraphs that can be computed on the same backend
  951. static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  952. // reset splits
  953. sched->n_splits = 0;
  954. sched->n_graph_inputs = 0;
  955. sched->is_reset = false;
  956. struct ggml_init_params params = {
  957. /* .mem_size = */ sizeof(sched->context_buffer),
  958. /* .mem_buffer = */ sched->context_buffer,
  959. /* .no_alloc = */ true
  960. };
  961. ggml_free(sched->ctx);
  962. sched->ctx = ggml_init(params);
  963. if (sched->ctx == NULL) {
  964. fprintf(stderr, "%s: failed to initialize context\n", __func__);
  965. GGML_ASSERT(false);
  966. }
  967. // pass 1: assign backends to ops with pre-allocated inputs
  968. for (int i = 0; i < graph->n_leafs; i++) {
  969. struct ggml_tensor * leaf = graph->leafs[i];
  970. if (tensor_backend_id(leaf) != -1) {
  971. // do not overwrite user assignments
  972. continue;
  973. }
  974. tensor_backend_id(leaf) = ggml_backend_sched_backend_id_from_cur(sched, leaf);
  975. }
  976. for (int i = 0; i < graph->n_nodes; i++) {
  977. struct ggml_tensor * node = graph->nodes[i];
  978. if (tensor_backend_id(node) != -1) {
  979. // do not overwrite user assignments
  980. continue;
  981. }
  982. tensor_backend_id(node) = ggml_backend_sched_backend_id_from_cur(sched, node);
  983. // src
  984. for (int j = 0; j < GGML_MAX_SRC; j++) {
  985. struct ggml_tensor * src = node->src[j];
  986. if (src == NULL) {
  987. continue;
  988. }
  989. if (tensor_backend_id(src) == -1) {
  990. tensor_backend_id(src) = ggml_backend_sched_backend_id_from_cur(sched, src);
  991. }
  992. }
  993. }
  994. #ifdef DEBUG_PASS1
  995. fprintf(stderr, "PASS 1 ASSIGNMENTS\n"); ggml_backend_sched_print_assignments(sched, graph);
  996. #endif
  997. // pass 2: expand current backend assignments
  998. // assign the same backend to adjacent nodes
  999. // expand gpu backends (i.e. non last prio) up and down, ignoring cpu (the lowest priority backend)
  1000. // thus, cpu will never be used unless weights are on cpu, or there are no gpu ops between cpu ops
  1001. // pass 2.2 expand gpu down
  1002. {
  1003. int cur_backend_id = -1;
  1004. for (int i = 0; i < graph->n_nodes; i++) {
  1005. struct ggml_tensor * node = graph->nodes[i];
  1006. if (ggml_is_view_op(node->op)) {
  1007. continue;
  1008. }
  1009. int tensor_backend_id = tensor_backend_id(node);
  1010. if (tensor_backend_id != -1) {
  1011. if (tensor_backend_id == sched->n_backends - 1) {
  1012. // skip cpu (lowest prio backend)
  1013. cur_backend_id = -1;
  1014. } else {
  1015. cur_backend_id = tensor_backend_id;
  1016. }
  1017. } else {
  1018. tensor_backend_id(node) = cur_backend_id;
  1019. SET_CAUSE(node, "2.2");
  1020. }
  1021. }
  1022. }
  1023. // pass 2.1 expand gpu up
  1024. {
  1025. int cur_backend_id = -1;
  1026. for (int i = graph->n_nodes - 1; i >= 0; i--) {
  1027. struct ggml_tensor * node = graph->nodes[i];
  1028. if (ggml_is_view_op(node->op)) {
  1029. continue;
  1030. }
  1031. int tensor_backend_id = tensor_backend_id(node);
  1032. if (tensor_backend_id != -1) {
  1033. if (tensor_backend_id == sched->n_backends - 1) {
  1034. // skip cpu (lowest prio backend)
  1035. cur_backend_id = -1;
  1036. } else {
  1037. cur_backend_id = tensor_backend_id;
  1038. }
  1039. } else {
  1040. tensor_backend_id(node) = cur_backend_id;
  1041. SET_CAUSE(node, "2.1");
  1042. }
  1043. }
  1044. }
  1045. // pass 2.4 expand rest down
  1046. {
  1047. int cur_backend_id = -1;
  1048. for (int i = 0; i < graph->n_nodes; i++) {
  1049. struct ggml_tensor * node = graph->nodes[i];
  1050. if (ggml_is_view_op(node->op)) {
  1051. continue;
  1052. }
  1053. int tensor_backend_id = tensor_backend_id(node);
  1054. if (tensor_backend_id != -1) {
  1055. cur_backend_id = tensor_backend_id;
  1056. } else {
  1057. tensor_backend_id(node) = cur_backend_id;
  1058. SET_CAUSE(node, "2.4");
  1059. }
  1060. }
  1061. }
  1062. // pass 2.3 expand rest up
  1063. {
  1064. int cur_backend_id = -1;
  1065. for (int i = graph->n_nodes - 1; i >= 0; i--) {
  1066. struct ggml_tensor * node = graph->nodes[i];
  1067. if (ggml_is_view_op(node->op)) {
  1068. continue;
  1069. }
  1070. int tensor_backend_id = tensor_backend_id(node);
  1071. if (tensor_backend_id != -1) {
  1072. cur_backend_id = tensor_backend_id;
  1073. } else {
  1074. tensor_backend_id(node) = cur_backend_id;
  1075. SET_CAUSE(node, "2.3");
  1076. }
  1077. }
  1078. }
  1079. #ifdef DEBUG_PASS2
  1080. fprintf(stderr, "PASS 2 ASSIGNMENTS\n"); ggml_backend_sched_print_assignments(sched, graph);
  1081. #endif
  1082. // pass 3: assign backends to remaining src from dst and view_src
  1083. for (int i = 0; i < graph->n_nodes; i++) {
  1084. struct ggml_tensor * node = graph->nodes[i];
  1085. int cur_backend_id = tensor_backend_id(node);
  1086. if (node->view_src != NULL && cur_backend_id == -1) {
  1087. cur_backend_id = tensor_backend_id(node) = tensor_backend_id(node->view_src);
  1088. SET_CAUSE(node, "3.vsrc");
  1089. }
  1090. for (int j = 0; j < GGML_MAX_SRC; j++) {
  1091. struct ggml_tensor * src = node->src[j];
  1092. if (src == NULL) {
  1093. continue;
  1094. }
  1095. int src_backend_id = tensor_backend_id(src);
  1096. if (src_backend_id == -1) {
  1097. if (src->view_src != NULL) {
  1098. // views are always on the same backend as the source
  1099. tensor_backend_id(src) = tensor_backend_id(src->view_src);
  1100. SET_CAUSE(src, "3.vsrc");
  1101. } else {
  1102. tensor_backend_id(src) = cur_backend_id;
  1103. SET_CAUSE(src, "3.cur");
  1104. }
  1105. }
  1106. }
  1107. }
  1108. #ifdef DEBUG_PASS3
  1109. fprintf(stderr, "PASS 3 ASSIGNMENTS\n"); ggml_backend_sched_print_assignments(sched, graph);
  1110. #endif
  1111. // pass 4: split graph, find tensors that need to be copied
  1112. {
  1113. int cur_split = 0;
  1114. // find the backend of the first split, skipping view ops
  1115. for (int i = 0; i < graph->n_nodes; i++) {
  1116. struct ggml_tensor * node = graph->nodes[i];
  1117. if (!ggml_is_view_op(node->op)) {
  1118. sched->splits[0].backend_id = tensor_backend_id(node);
  1119. break;
  1120. }
  1121. }
  1122. sched->splits[0].i_start = 0;
  1123. sched->splits[0].n_inputs = 0;
  1124. memset(sched->splits[0].inputs, 0, sizeof(sched->splits[0].inputs)); //HACK
  1125. int cur_backend_id = sched->splits[0].backend_id;
  1126. for (int i = 0; i < graph->n_nodes; i++) {
  1127. struct ggml_tensor * node = graph->nodes[i];
  1128. if (ggml_is_view_op(node->op)) {
  1129. continue;
  1130. }
  1131. int tensor_backend_id = tensor_backend_id(node);
  1132. GGML_ASSERT(tensor_backend_id != -1); // all nodes should be assigned by now
  1133. if (tensor_backend_id != cur_backend_id) {
  1134. sched->splits[cur_split].i_end = i;
  1135. cur_split++;
  1136. GGML_ASSERT(cur_split < GGML_SCHED_MAX_SPLITS);
  1137. sched->splits[cur_split].backend_id = tensor_backend_id;
  1138. sched->splits[cur_split].i_start = i;
  1139. sched->splits[cur_split].n_inputs = 0;
  1140. cur_backend_id = tensor_backend_id;
  1141. }
  1142. // find inputs that are not on the same backend
  1143. for (int j = 0; j < GGML_MAX_SRC; j++) {
  1144. struct ggml_tensor * src = node->src[j];
  1145. if (src == NULL) {
  1146. continue;
  1147. }
  1148. int src_backend_id = tensor_backend_id(src);
  1149. assert(src_backend_id != -1); // all inputs should be assigned by now
  1150. if (src->flags & GGML_TENSOR_FLAG_INPUT) {
  1151. size_t id = hash_id(src);
  1152. if (sched->tensor_copies[id][src_backend_id][0] == NULL) {
  1153. ggml_backend_t backend = sched->backends[src_backend_id];
  1154. for (int c = 0; c < sched->n_copies; c++) {
  1155. struct ggml_tensor * tensor_copy;
  1156. if (c == sched->cur_copy) {
  1157. tensor_copy = src; // use the original tensor as the current copy
  1158. } else {
  1159. tensor_copy = ggml_dup_tensor_layout(sched->ctx, src);
  1160. ggml_format_name(tensor_copy, "%s#%s#%d", ggml_backend_name(backend), src->name, c);
  1161. }
  1162. if (sched->n_copies > 1) {
  1163. ggml_set_input(tensor_copy);
  1164. ggml_set_output(tensor_copy); // prevent ggml-alloc from overwriting the tensor
  1165. }
  1166. sched->tensor_copies[id][src_backend_id][c] = tensor_copy;
  1167. tensor_backend_id(tensor_copy) = src_backend_id;
  1168. SET_CAUSE(tensor_copy, "4.cpy");
  1169. }
  1170. int n_graph_inputs = sched->n_graph_inputs++;
  1171. GGML_ASSERT(n_graph_inputs < GGML_SCHED_MAX_SPLIT_INPUTS);
  1172. sched->graph_inputs[n_graph_inputs] = src;
  1173. }
  1174. }
  1175. if (src_backend_id != tensor_backend_id) {
  1176. // create a copy of the input in the split's backend
  1177. size_t id = hash_id(src);
  1178. if (sched->tensor_copies[id][cur_backend_id][0] == NULL) {
  1179. ggml_backend_t backend = sched->backends[cur_backend_id];
  1180. for (int c = 0; c < sched->n_copies; c++) {
  1181. struct ggml_tensor * tensor_copy = ggml_dup_tensor_layout(sched->ctx, src);
  1182. ggml_format_name(tensor_copy, "%s#%s#%d", ggml_backend_name(backend), src->name, c);
  1183. if (sched->n_copies > 1) {
  1184. ggml_set_input(tensor_copy);
  1185. ggml_set_output(tensor_copy); // prevent ggml-alloc from overwriting the tensor
  1186. }
  1187. sched->tensor_copies[id][cur_backend_id][c] = tensor_copy;
  1188. tensor_backend_id(tensor_copy) = cur_backend_id;
  1189. SET_CAUSE(tensor_copy, "4.cpy");
  1190. }
  1191. int n_inputs = sched->splits[cur_split].n_inputs++;
  1192. GGML_ASSERT(n_inputs < GGML_SCHED_MAX_SPLIT_INPUTS);
  1193. sched->splits[cur_split].inputs[n_inputs] = src;
  1194. }
  1195. node->src[j] = sched->tensor_copies[id][cur_backend_id][sched->cur_copy];
  1196. }
  1197. }
  1198. }
  1199. sched->splits[cur_split].i_end = graph->n_nodes;
  1200. sched->n_splits = cur_split + 1;
  1201. }
  1202. #ifdef DEBUG_PASS4
  1203. fprintf(stderr, "PASS 4 ASSIGNMENTS\n"); ggml_backend_sched_print_assignments(sched, graph);
  1204. #endif
  1205. #ifndef NDEBUG
  1206. // sanity check: all sources should have the same backend as the node
  1207. for (int i = 0; i < graph->n_nodes; i++) {
  1208. struct ggml_tensor * node = graph->nodes[i];
  1209. ggml_backend_t tensor_backend = ggml_backend_sched_get_tensor_backend(sched, node);
  1210. if (tensor_backend == NULL) {
  1211. fprintf(stderr, "!!!!!!! %s has no backend\n", node->name);
  1212. }
  1213. if (node->view_src != NULL && tensor_backend != ggml_backend_sched_get_tensor_backend(sched, node->view_src)) {
  1214. fprintf(stderr, "!!!!!!! %s has backend %s, view_src %s has backend %s\n",
  1215. node->name, tensor_backend ? ggml_backend_name(tensor_backend) : "NULL",
  1216. node->view_src->name, ggml_backend_sched_get_tensor_backend(sched, node->view_src) ?
  1217. ggml_backend_name(ggml_backend_sched_get_tensor_backend(sched, node->view_src)) : "NULL");
  1218. }
  1219. for (int j = 0; j < GGML_MAX_SRC; j++) {
  1220. struct ggml_tensor * src = node->src[j];
  1221. if (src == NULL) {
  1222. continue;
  1223. }
  1224. ggml_backend_t src_backend = ggml_backend_sched_get_tensor_backend(sched, src);
  1225. if (src_backend != tensor_backend /* && src_backend != NULL */) {
  1226. fprintf(stderr, "!!!! %s has backend %s, src %d (%s) has backend %s\n",
  1227. node->name, tensor_backend ? ggml_backend_name(tensor_backend) : "NULL",
  1228. j, src->name, src_backend ? ggml_backend_name(src_backend) : "NULL");
  1229. }
  1230. if (src->view_src != NULL && src_backend != ggml_backend_sched_get_tensor_backend(sched, src->view_src)) {
  1231. fprintf(stderr, "!!!!!!! [src] %s has backend %s, view_src %s has backend %s\n",
  1232. src->name, src_backend ? ggml_backend_name(src_backend) : "NULL",
  1233. src->view_src->name, ggml_backend_sched_get_tensor_backend(sched, src->view_src) ?
  1234. ggml_backend_name(ggml_backend_sched_get_tensor_backend(sched, src->view_src)) : "NULL");
  1235. }
  1236. }
  1237. }
  1238. fflush(stderr);
  1239. #endif
  1240. // create copies of the graph for each split
  1241. // TODO: avoid this copy
  1242. struct ggml_cgraph * graph_copy = ggml_new_graph_custom(sched->ctx, graph->n_nodes + sched->n_splits*GGML_SCHED_MAX_SPLIT_INPUTS, false);
  1243. for (int i = 0; i < sched->n_splits; i++) {
  1244. struct ggml_backend_sched_split * split = &sched->splits[i];
  1245. split->graph = ggml_graph_view(graph, split->i_start, split->i_end);
  1246. // add inputs to the graph copy so that they are allocated by ggml-alloc at the start of the split
  1247. for (int j = 0; j < split->n_inputs; j++) {
  1248. struct ggml_tensor * input = split->inputs[j];
  1249. struct ggml_tensor * input_cpy = sched->tensor_copies[hash_id(input)][split->backend_id][sched->cur_copy];
  1250. // add a dependency to the input source so that it is not freed before the copy is done
  1251. struct ggml_tensor * input_dep = ggml_view_tensor(sched->ctx, input);
  1252. input_dep->src[0] = input;
  1253. sched->node_backend_ids[graph_copy->n_nodes] = tensor_backend_id(input);
  1254. graph_copy->nodes[graph_copy->n_nodes++] = input_dep;
  1255. // add a dependency to the input copy so that it is allocated at the start of the split
  1256. sched->node_backend_ids[graph_copy->n_nodes] = split->backend_id;
  1257. graph_copy->nodes[graph_copy->n_nodes++] = input_cpy;
  1258. }
  1259. for (int j = split->i_start; j < split->i_end; j++) {
  1260. sched->node_backend_ids[graph_copy->n_nodes] = tensor_backend_id(graph->nodes[j]);
  1261. graph_copy->nodes[graph_copy->n_nodes++] = graph->nodes[j];
  1262. }
  1263. }
  1264. if (sched->n_copies > 1) {
  1265. // add input copies as leafs so that they are allocated first
  1266. for (int i = 0; i < sched->n_graph_inputs; i++) {
  1267. struct ggml_tensor * input = sched->graph_inputs[i];
  1268. size_t id = hash_id(input);
  1269. int backend_id = tensor_backend_id(input);
  1270. for (int c = 0; c < sched->n_copies; c++) {
  1271. struct ggml_tensor * input_cpy = sched->tensor_copies[id][backend_id][c];
  1272. sched->leaf_backend_ids[graph_copy->n_leafs] = backend_id;
  1273. graph_copy->leafs[graph_copy->n_leafs++] = input_cpy;
  1274. }
  1275. }
  1276. for (int i = 0; i < sched->n_splits; i++) {
  1277. struct ggml_backend_sched_split * split = &sched->splits[i];
  1278. int backend_id = split->backend_id;
  1279. for (int j = 0; j < split->n_inputs; j++) {
  1280. struct ggml_tensor * input = split->inputs[j];
  1281. size_t id = hash_id(input);
  1282. for (int c = 0; c < sched->n_copies; c++) {
  1283. struct ggml_tensor * input_cpy = sched->tensor_copies[id][backend_id][c];
  1284. sched->leaf_backend_ids[graph_copy->n_leafs] = backend_id;
  1285. graph_copy->leafs[graph_copy->n_leafs++] = input_cpy;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. // add leafs from the original graph
  1291. for (int i = 0; i < graph->n_leafs; i++) {
  1292. struct ggml_tensor * leaf = graph->leafs[i];
  1293. sched->leaf_backend_ids[graph_copy->n_leafs] = tensor_backend_id(leaf);
  1294. graph_copy->leafs[graph_copy->n_leafs++] = leaf;
  1295. }
  1296. sched->graph = graph_copy;
  1297. }
  1298. static bool ggml_backend_sched_alloc_splits(ggml_backend_sched_t sched) {
  1299. // allocate graph
  1300. if (!ggml_gallocr_alloc_graph(sched->galloc, sched->graph)) {
  1301. // the re-allocation may cause the split inputs to be moved to a different address
  1302. ggml_backend_sched_synchronize(sched);
  1303. #ifndef NDEBUG
  1304. fprintf(stderr, "%s: failed to allocate graph, reserving\n", __func__);
  1305. #endif
  1306. ggml_gallocr_reserve_n(sched->galloc, sched->graph, sched->node_backend_ids, sched->leaf_backend_ids);
  1307. if (!ggml_gallocr_alloc_graph(sched->galloc, sched->graph)) {
  1308. fprintf(stderr, "%s: failed to allocate graph\n", __func__);
  1309. return false;
  1310. }
  1311. }
  1312. return true;
  1313. }
  1314. static enum ggml_status ggml_backend_sched_compute_splits(ggml_backend_sched_t sched) {
  1315. struct ggml_backend_sched_split * splits = sched->splits;
  1316. for (int i = 0; i < sched->n_splits; i++) {
  1317. struct ggml_backend_sched_split * split = &splits[i];
  1318. int split_backend_id = split->backend_id;
  1319. ggml_backend_t split_backend = sched->backends[split_backend_id];
  1320. // copy the input tensors to the split backend
  1321. for (int j = 0; j < split->n_inputs; j++) {
  1322. ggml_backend_t input_backend = ggml_backend_sched_get_tensor_backend(sched, split->inputs[j]);
  1323. struct ggml_tensor * input = split->inputs[j];
  1324. struct ggml_tensor * input_cpy = sched->tensor_copies[hash_id(input)][split_backend_id][sched->cur_copy];
  1325. if (input->flags & GGML_TENSOR_FLAG_INPUT) {
  1326. // inputs from the user must be copied immediately to prevent the user overwriting the data before the copy is done
  1327. if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
  1328. ggml_backend_event_synchronize(sched->events[split_backend_id][sched->cur_copy]);
  1329. } else {
  1330. ggml_backend_synchronize(split_backend);
  1331. }
  1332. ggml_backend_tensor_copy(input, input_cpy);
  1333. } else {
  1334. if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
  1335. ggml_backend_event_wait(split_backend, sched->events[split_backend_id][sched->cur_copy]);
  1336. } else {
  1337. ggml_backend_synchronize(split_backend);
  1338. ggml_backend_synchronize(input_backend);
  1339. }
  1340. ggml_backend_tensor_copy_async(input_backend, split_backend, input, input_cpy);
  1341. }
  1342. }
  1343. if (!sched->callback_eval) {
  1344. enum ggml_status ec = ggml_backend_graph_compute_async(split_backend, &split->graph);
  1345. if (ec != GGML_STATUS_SUCCESS) {
  1346. return ec;
  1347. }
  1348. } else {
  1349. // similar to ggml_backend_compare_graph_backend
  1350. for (int j0 = 0; j0 < split->graph.n_nodes; j0++) {
  1351. struct ggml_tensor * t = split->graph.nodes[j0];
  1352. // check if the user needs data from this node
  1353. bool need = sched->callback_eval(t, true, sched->callback_eval_user_data);
  1354. int j1 = j0;
  1355. // determine the range [j0, j1] of nodes that can be computed together
  1356. while (!need && j1 < split->graph.n_nodes - 1) {
  1357. t = split->graph.nodes[++j1];
  1358. need = sched->callback_eval(t, true, sched->callback_eval_user_data);
  1359. }
  1360. struct ggml_cgraph gv = ggml_graph_view(&split->graph, j0, j1 + 1);
  1361. enum ggml_status ec = ggml_backend_graph_compute_async(split_backend, &gv);
  1362. if (ec != GGML_STATUS_SUCCESS) {
  1363. return ec;
  1364. }
  1365. // TODO: pass backend to the callback, then the user can decide if they want to synchronize
  1366. ggml_backend_synchronize(split_backend);
  1367. if (need && !sched->callback_eval(t, false, sched->callback_eval_user_data)) {
  1368. break;
  1369. }
  1370. j0 = j1;
  1371. }
  1372. }
  1373. // record the event of this copy
  1374. if (split->n_inputs > 0) {
  1375. if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
  1376. ggml_backend_event_record(sched->events[split_backend_id][sched->cur_copy]);
  1377. }
  1378. }
  1379. }
  1380. sched->cur_copy = (sched->cur_copy + 1) % sched->n_copies;
  1381. return GGML_STATUS_SUCCESS;
  1382. }
  1383. ggml_backend_sched_t ggml_backend_sched_new(
  1384. ggml_backend_t * backends,
  1385. ggml_backend_buffer_type_t * bufts,
  1386. int n_backends,
  1387. size_t graph_size,
  1388. bool parallel) {
  1389. GGML_ASSERT(n_backends > 0);
  1390. GGML_ASSERT(n_backends <= GGML_SCHED_MAX_BACKENDS);
  1391. GGML_ASSERT(ggml_backend_is_cpu(backends[n_backends - 1])); // last backend must be CPU
  1392. struct ggml_backend_sched * sched = calloc(sizeof(struct ggml_backend_sched), 1);
  1393. // initialize hash table
  1394. sched->hash_set = ggml_hash_set_new(graph_size + GGML_SCHED_MAX_SPLITS*GGML_SCHED_MAX_SPLIT_INPUTS);
  1395. sched->tensor_backend_id = calloc(sizeof(sched->tensor_backend_id[0]), sched->hash_set.size);
  1396. sched->tensor_copies = calloc(sizeof(sched->tensor_copies[0]), sched->hash_set.size);
  1397. sched->node_backend_ids = calloc(sizeof(sched->node_backend_ids[0]), graph_size);
  1398. sched->leaf_backend_ids = calloc(sizeof(sched->leaf_backend_ids[0]), graph_size);
  1399. sched->n_backends = n_backends;
  1400. sched->n_copies = parallel ? GGML_SCHED_MAX_COPIES : 1;
  1401. GGML_ASSERT(sched->n_copies <= GGML_SCHED_MAX_COPIES);
  1402. for (int b = 0; b < n_backends; b++) {
  1403. sched->backends[b] = backends[b];
  1404. sched->bufts[b] = bufts ? bufts[b] : ggml_backend_get_default_buffer_type(backends[b]);
  1405. GGML_ASSERT(ggml_backend_buft_supports_backend(sched->bufts[b], backends[b]));
  1406. if (sched->n_copies > 1) {
  1407. for (int c = 0; c < sched->n_copies; c++) {
  1408. sched->events[b][c] = ggml_backend_event_new(backends[b]);
  1409. }
  1410. }
  1411. }
  1412. sched->galloc = ggml_gallocr_new_n(sched->bufts, n_backends);
  1413. ggml_backend_sched_reset(sched);
  1414. return sched;
  1415. }
  1416. void ggml_backend_sched_free(ggml_backend_sched_t sched) {
  1417. if (sched == NULL) {
  1418. return;
  1419. }
  1420. for (int b = 0; b < sched->n_backends; b++) {
  1421. for (int c = 0; c < sched->n_copies; c++) {
  1422. ggml_backend_event_free(sched->events[b][c]);
  1423. }
  1424. }
  1425. ggml_gallocr_free(sched->galloc);
  1426. ggml_free(sched->ctx);
  1427. free(sched->hash_set.keys);
  1428. free(sched->tensor_backend_id);
  1429. free(sched->tensor_copies);
  1430. free(sched->node_backend_ids);
  1431. free(sched->leaf_backend_ids);
  1432. free(sched);
  1433. }
  1434. void ggml_backend_sched_reset(ggml_backend_sched_t sched) {
  1435. // reset state for the next run
  1436. size_t hash_size = sched->hash_set.size;
  1437. memset(sched->hash_set.keys, 0, sizeof(sched->hash_set.keys[0]) * hash_size); // NOLINT
  1438. memset(sched->tensor_backend_id, -1, sizeof(sched->tensor_backend_id[0]) * hash_size);
  1439. memset(sched->tensor_copies, 0, sizeof(sched->tensor_copies[0]) * hash_size);
  1440. sched->is_reset = true;
  1441. sched->is_alloc = false;
  1442. }
  1443. bool ggml_backend_sched_reserve(ggml_backend_sched_t sched, struct ggml_cgraph * measure_graph) {
  1444. ggml_backend_sched_split_graph(sched, measure_graph);
  1445. // TODO: extract this to a separate function
  1446. if (!ggml_gallocr_reserve_n(sched->galloc, sched->graph, sched->node_backend_ids, sched->leaf_backend_ids)) {
  1447. return false;
  1448. }
  1449. ggml_backend_sched_reset(sched);
  1450. ggml_backend_sched_synchronize(sched);
  1451. return true;
  1452. }
  1453. bool ggml_backend_sched_alloc_graph(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  1454. GGML_ASSERT((int)sched->hash_set.size >= graph->n_nodes + GGML_SCHED_MAX_SPLITS*GGML_SCHED_MAX_SPLIT_INPUTS);
  1455. ggml_backend_sched_split_graph(sched, graph);
  1456. if (!ggml_backend_sched_alloc_splits(sched)) {
  1457. return false;
  1458. }
  1459. sched->is_alloc = true;
  1460. return true;
  1461. }
  1462. enum ggml_status ggml_backend_sched_graph_compute(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  1463. enum ggml_status err = ggml_backend_sched_graph_compute_async(sched, graph);
  1464. ggml_backend_sched_synchronize(sched);
  1465. return err;
  1466. }
  1467. enum ggml_status ggml_backend_sched_graph_compute_async(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  1468. if (!sched->is_reset && !sched->is_alloc) {
  1469. ggml_backend_sched_reset(sched);
  1470. }
  1471. if (!sched->is_alloc) {
  1472. if (!ggml_backend_sched_alloc_graph(sched, graph)) {
  1473. return GGML_STATUS_ALLOC_FAILED;
  1474. }
  1475. }
  1476. return ggml_backend_sched_compute_splits(sched);
  1477. }
  1478. void ggml_backend_sched_synchronize(ggml_backend_sched_t sched) {
  1479. for (int i = 0; i < sched->n_backends; i++) {
  1480. ggml_backend_synchronize(sched->backends[i]);
  1481. }
  1482. }
  1483. void ggml_backend_sched_set_eval_callback(ggml_backend_sched_t sched, ggml_backend_sched_eval_callback callback, void * user_data) {
  1484. sched->callback_eval = callback;
  1485. sched->callback_eval_user_data = user_data;
  1486. }
  1487. int ggml_backend_sched_get_n_splits(ggml_backend_sched_t sched) {
  1488. return sched->n_splits;
  1489. }
  1490. int ggml_backend_sched_get_n_copies(ggml_backend_sched_t sched) {
  1491. return sched->n_copies;
  1492. }
  1493. size_t ggml_backend_sched_get_buffer_size(ggml_backend_sched_t sched, ggml_backend_t backend) {
  1494. int backend_index = ggml_backend_sched_backend_id(sched, backend);
  1495. GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
  1496. return ggml_gallocr_get_buffer_size(sched->galloc, backend_index);
  1497. }
  1498. void ggml_backend_sched_set_tensor_backend(ggml_backend_sched_t sched, struct ggml_tensor * node, ggml_backend_t backend) {
  1499. int backend_index = ggml_backend_sched_backend_id(sched, backend);
  1500. GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
  1501. tensor_backend_id(node) = backend_index;
  1502. }
  1503. ggml_backend_t ggml_backend_sched_get_tensor_backend(ggml_backend_sched_t sched, struct ggml_tensor * node) {
  1504. int backend_index = tensor_backend_id(node);
  1505. if (backend_index == -1) {
  1506. return NULL;
  1507. }
  1508. return sched->backends[backend_index];
  1509. }
  1510. // utils
  1511. void ggml_backend_view_init(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) {
  1512. GGML_ASSERT(tensor->buffer == NULL);
  1513. GGML_ASSERT(tensor->view_src != NULL);
  1514. GGML_ASSERT(tensor->view_src->buffer != NULL);
  1515. GGML_ASSERT(tensor->view_src->data != NULL);
  1516. tensor->buffer = buffer;
  1517. tensor->data = (char *)tensor->view_src->data + tensor->view_offs;
  1518. tensor->backend = tensor->view_src->backend;
  1519. ggml_backend_buffer_init_tensor(buffer, tensor);
  1520. }
  1521. void ggml_backend_tensor_alloc(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, void * addr) {
  1522. GGML_ASSERT(tensor->buffer == NULL);
  1523. GGML_ASSERT(tensor->data == NULL);
  1524. GGML_ASSERT(tensor->view_src == NULL);
  1525. GGML_ASSERT(addr >= ggml_backend_buffer_get_base(buffer));
  1526. GGML_ASSERT((char *)addr + ggml_backend_buffer_get_alloc_size(buffer, tensor) <=
  1527. (char *)ggml_backend_buffer_get_base(buffer) + ggml_backend_buffer_get_size(buffer));
  1528. tensor->buffer = buffer;
  1529. tensor->data = addr;
  1530. ggml_backend_buffer_init_tensor(buffer, tensor);
  1531. }
  1532. static struct ggml_tensor * graph_copy_dup_tensor(struct ggml_hash_set hash_set, struct ggml_tensor ** node_copies,
  1533. struct ggml_context * ctx_allocated, struct ggml_context * ctx_unallocated, struct ggml_tensor * src) {
  1534. GGML_ASSERT(src != NULL);
  1535. GGML_ASSERT(src->data && "graph must be allocated");
  1536. size_t id = ggml_hash_insert(hash_set, src);
  1537. if (id == GGML_HASHTABLE_ALREADY_EXISTS) {
  1538. return node_copies[ggml_hash_find(hash_set, src)];
  1539. }
  1540. struct ggml_tensor * dst = ggml_dup_tensor_layout(src->data && !src->view_src ? ctx_allocated : ctx_unallocated, src);
  1541. if (src->view_src != NULL) {
  1542. dst->view_src = graph_copy_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, src->view_src);
  1543. dst->view_offs = src->view_offs;
  1544. }
  1545. dst->op = src->op;
  1546. memcpy(dst->op_params, src->op_params, sizeof(dst->op_params));
  1547. ggml_set_name(dst, src->name);
  1548. // copy src
  1549. for (int i = 0; i < GGML_MAX_SRC; i++) {
  1550. struct ggml_tensor * s = src->src[i];
  1551. if (s == NULL) {
  1552. continue;
  1553. }
  1554. dst->src[i] = graph_copy_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, s);
  1555. }
  1556. node_copies[id] = dst;
  1557. return dst;
  1558. }
  1559. static void graph_copy_init_tensor(struct ggml_hash_set hash_set, struct ggml_tensor ** node_copies, bool * node_init, struct ggml_tensor * src) {
  1560. size_t id = ggml_hash_find(hash_set, src);
  1561. if (node_init[id]) {
  1562. return;
  1563. }
  1564. node_init[id] = true;
  1565. struct ggml_tensor * dst = node_copies[id];
  1566. if (dst->view_src != NULL) {
  1567. graph_copy_init_tensor(hash_set, node_copies, node_init, src->view_src);
  1568. ggml_backend_view_init(dst->view_src->buffer, dst);
  1569. }
  1570. else {
  1571. ggml_backend_tensor_copy(src, dst);
  1572. }
  1573. // init src
  1574. for (int i = 0; i < GGML_MAX_SRC; i++) {
  1575. struct ggml_tensor * s = src->src[i];
  1576. if (s == NULL) {
  1577. continue;
  1578. }
  1579. graph_copy_init_tensor(hash_set, node_copies, node_init, s);
  1580. }
  1581. }
  1582. struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph) {
  1583. struct ggml_hash_set hash_set = {
  1584. /* .size = */ graph->visited_hash_table.size,
  1585. /* .keys = */ calloc(sizeof(hash_set.keys[0]), graph->visited_hash_table.size) // NOLINT
  1586. };
  1587. struct ggml_tensor ** node_copies = calloc(sizeof(node_copies[0]), hash_set.size); // NOLINT
  1588. bool * node_init = calloc(sizeof(node_init[0]), hash_set.size);
  1589. struct ggml_init_params params = {
  1590. /* .mem_size = */ ggml_tensor_overhead()*hash_set.size + ggml_graph_overhead_custom(graph->size, false),
  1591. /* .mem_buffer = */ NULL,
  1592. /* .no_alloc = */ true
  1593. };
  1594. struct ggml_context * ctx_allocated = ggml_init(params);
  1595. struct ggml_context * ctx_unallocated = ggml_init(params);
  1596. if (ctx_allocated == NULL || ctx_unallocated == NULL) {
  1597. fprintf(stderr, "failed to allocate context for graph copy\n");
  1598. free(hash_set.keys);
  1599. free(node_copies);
  1600. free(node_init);
  1601. ggml_free(ctx_allocated);
  1602. ggml_free(ctx_unallocated);
  1603. return (struct ggml_backend_graph_copy) {
  1604. /* .buffer = */ NULL,
  1605. /* .ctx_allocated = */ NULL,
  1606. /* .ctx_unallocated = */ NULL,
  1607. /* .graph = */ NULL,
  1608. };
  1609. }
  1610. // dup nodes
  1611. for (int i = 0; i < graph->n_nodes; i++) {
  1612. struct ggml_tensor * node = graph->nodes[i];
  1613. graph_copy_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, node);
  1614. }
  1615. // allocate nodes
  1616. ggml_backend_buffer_t buffer = ggml_backend_alloc_ctx_tensors(ctx_allocated, backend);
  1617. if (buffer == NULL) {
  1618. fprintf(stderr, "failed to allocate buffer for graph copy\n");
  1619. free(hash_set.keys);
  1620. free(node_copies);
  1621. free(node_init);
  1622. ggml_free(ctx_allocated);
  1623. ggml_free(ctx_unallocated);
  1624. return (struct ggml_backend_graph_copy) {
  1625. /* .buffer = */ NULL,
  1626. /* .ctx_allocated = */ NULL,
  1627. /* .ctx_unallocated = */ NULL,
  1628. /* .graph = */ NULL,
  1629. };
  1630. }
  1631. //printf("copy buffer size: %zu MB\n", ggml_backend_buffer_get_size(buffer) / 1024 / 1024);
  1632. // copy data and init views
  1633. for (int i = 0; i < graph->n_nodes; i++) {
  1634. struct ggml_tensor * node = graph->nodes[i];
  1635. graph_copy_init_tensor(hash_set, node_copies, node_init, node);
  1636. }
  1637. // build graph copy
  1638. struct ggml_cgraph * graph_copy = ggml_new_graph_custom(ctx_allocated, graph->size, false);
  1639. for (int i = 0; i < graph->n_nodes; i++) {
  1640. struct ggml_tensor * node = graph->nodes[i];
  1641. struct ggml_tensor * node_copy = node_copies[ggml_hash_find(hash_set, node)];
  1642. graph_copy->nodes[i] = node_copy;
  1643. }
  1644. graph_copy->n_nodes = graph->n_nodes;
  1645. free(hash_set.keys);
  1646. free(node_copies);
  1647. free(node_init);
  1648. return (struct ggml_backend_graph_copy) {
  1649. /* .buffer = */ buffer,
  1650. /* .ctx_allocated = */ ctx_allocated,
  1651. /* .ctx_unallocated = */ ctx_unallocated,
  1652. /* .graph = */ graph_copy,
  1653. };
  1654. }
  1655. void ggml_backend_graph_copy_free(struct ggml_backend_graph_copy copy) {
  1656. ggml_backend_buffer_free(copy.buffer);
  1657. ggml_free(copy.ctx_allocated);
  1658. ggml_free(copy.ctx_unallocated);
  1659. }
  1660. 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) {
  1661. struct ggml_backend_graph_copy copy = ggml_backend_graph_copy(backend2, graph);
  1662. if (copy.buffer == NULL) {
  1663. return false;
  1664. }
  1665. struct ggml_cgraph * g1 = graph;
  1666. struct ggml_cgraph * g2 = copy.graph;
  1667. assert(g1->n_nodes == g2->n_nodes);
  1668. for (int i = 0; i < g1->n_nodes; i++) {
  1669. //printf("eval %d/%d\n", i, g1->n_nodes);
  1670. struct ggml_tensor * t1 = g1->nodes[i];
  1671. struct ggml_tensor * t2 = g2->nodes[i];
  1672. assert(t1->op == t2->op && ggml_are_same_layout(t1, t2));
  1673. struct ggml_cgraph g1v = ggml_graph_view(g1, i, i + 1);
  1674. struct ggml_cgraph g2v = ggml_graph_view(g2, i, i + 1);
  1675. ggml_backend_graph_compute(backend1, &g1v);
  1676. ggml_backend_graph_compute(backend2, &g2v);
  1677. if (ggml_is_view_op(t1->op)) {
  1678. continue;
  1679. }
  1680. // compare results, calculate rms etc
  1681. if (!callback(i, t1, t2, user_data)) {
  1682. break;
  1683. }
  1684. }
  1685. ggml_backend_graph_copy_free(copy);
  1686. return true;
  1687. }