ggml-backend.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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. #ifdef DEBUG_PASS2
  875. fprintf(stderr, "PASS 2 ASSIGNMENTS\n"); sched_print_assignments(sched, graph);
  876. #endif
  877. // pass 3: assign backends to remaining src from dst and view_src
  878. for (int i = 0; i < graph->n_nodes; i++) {
  879. struct ggml_tensor * node = graph->nodes[i];
  880. ggml_tallocr_t cur_allocr = node_allocr(node);
  881. if (node->view_src != NULL && cur_allocr == NULL) {
  882. cur_allocr = node_allocr(node) = node_allocr(node->view_src);
  883. SET_CAUSE(node, "3.vsrc");
  884. }
  885. for (int j = 0; j < GGML_MAX_SRC; j++) {
  886. struct ggml_tensor * src = node->src[j];
  887. if (src == NULL) {
  888. break;
  889. }
  890. ggml_tallocr_t src_allocr = node_allocr(src);
  891. if (src_allocr == NULL) {
  892. if (src->view_src != NULL) {
  893. // views are always on the same backend as the source
  894. node_allocr(src) = node_allocr(src->view_src);
  895. SET_CAUSE(src, "3.vsrc");
  896. } else {
  897. node_allocr(src) = cur_allocr;
  898. SET_CAUSE(src, "3.cur");
  899. }
  900. }
  901. }
  902. }
  903. #ifdef DEBUG_PASS3
  904. fprintf(stderr, "PASS 3 ASSIGNMENTS\n"); sched_print_assignments(sched, graph);
  905. #endif
  906. // pass 4: split graph, find tensors that need to be copied
  907. {
  908. int cur_split = 0;
  909. // find the backend of the first split, skipping view ops
  910. for (int i = 0; i < graph->n_nodes; i++) {
  911. struct ggml_tensor * node = graph->nodes[i];
  912. if (!ggml_is_view_op(node->op)) {
  913. sched->splits[0].tallocr = node_allocr(node);
  914. break;
  915. }
  916. }
  917. sched->splits[0].i_start = 0;
  918. sched->splits[0].n_inputs = 0;
  919. memset(sched->splits[0].inputs, 0, sizeof(sched->splits[0].inputs)); //HACK
  920. ggml_tallocr_t cur_allocr = sched->splits[0].tallocr;
  921. size_t cur_backend_id = sched_allocr_prio(sched, cur_allocr);
  922. for (int i = 0; i < graph->n_nodes; i++) {
  923. struct ggml_tensor * node = graph->nodes[i];
  924. if (ggml_is_view_op(node->op)) {
  925. continue;
  926. }
  927. ggml_tallocr_t node_allocr = node_allocr(node);
  928. if (node_allocr != cur_allocr) {
  929. sched->splits[cur_split].i_end = i;
  930. cur_split++;
  931. GGML_ASSERT(cur_split < GGML_MAX_SPLITS);
  932. sched->splits[cur_split].tallocr = node_allocr;
  933. sched->splits[cur_split].i_start = i;
  934. sched->splits[cur_split].n_inputs = 0;
  935. cur_allocr = node_allocr;
  936. cur_backend_id = sched_allocr_prio(sched, cur_allocr);
  937. }
  938. // find inputs that are not on the same backend
  939. for (int j = 0; j < GGML_MAX_SRC; j++) {
  940. struct ggml_tensor * src = node->src[j];
  941. if (src == NULL) {
  942. break;
  943. }
  944. ggml_tallocr_t src_allocr = node_allocr(src);
  945. GGML_ASSERT(src_allocr != NULL); // all inputs should be assigned by now
  946. if (src_allocr != node_allocr) {
  947. // check if the input is already in the split
  948. bool found = false;
  949. for (int k = 0; k < sched->splits[cur_split].n_inputs; k++) {
  950. if (sched->splits[cur_split].inputs[k] == src) {
  951. found = true;
  952. break;
  953. }
  954. }
  955. if (!found) {
  956. int n_inputs = sched->splits[cur_split].n_inputs++;
  957. //printf("split %d input %d: %s (%s)\n", cur_split, n_inputs, src->name, ggml_backend_name(get_allocr_backend(sched, src_allocr)));
  958. GGML_ASSERT(n_inputs < GGML_MAX_SPLIT_INPUTS);
  959. sched->splits[cur_split].inputs[n_inputs] = src;
  960. }
  961. // create a copy of the input in the split's backend
  962. size_t id = hash_id(src);
  963. if (sched->node_copies[id][cur_backend_id] == NULL) {
  964. ggml_backend_t backend = get_allocr_backend(sched, cur_allocr);
  965. struct ggml_tensor * tensor_copy = ggml_dup_tensor_layout(sched->ctx, src);
  966. ggml_format_name(tensor_copy, "%s#%s", ggml_backend_name(backend), src->name);
  967. sched->node_copies[id][cur_backend_id] = tensor_copy;
  968. node_allocr(tensor_copy) = cur_allocr;
  969. SET_CAUSE(tensor_copy, "4.cpy");
  970. }
  971. node->src[j] = sched->node_copies[id][cur_backend_id];
  972. }
  973. }
  974. }
  975. sched->splits[cur_split].i_end = graph->n_nodes;
  976. sched->n_splits = cur_split + 1;
  977. }
  978. #ifdef DEBUG_PASS4
  979. fprintf(stderr, "PASS 4 ASSIGNMENTS\n"); sched_print_assignments(sched, graph);
  980. #endif
  981. #ifndef NDEBUG
  982. // sanity check: all sources should have the same backend as the node
  983. for (int i = 0; i < graph->n_nodes; i++) {
  984. struct ggml_tensor * node = graph->nodes[i];
  985. ggml_tallocr_t node_allocr = node_allocr(node);
  986. if (node_allocr == NULL) {
  987. fprintf(stderr, "!!!!!!! %s has no backend\n", node->name);
  988. }
  989. if (node->view_src != NULL && node_allocr != node_allocr(node->view_src)) {
  990. fprintf(stderr, "!!!!!!! %s has backend %s, view_src %s has backend %s\n",
  991. node->name, node_allocr ? ggml_backend_name(get_allocr_backend(sched, node_allocr)) : "NULL",
  992. node->view_src->name, node_allocr(node->view_src) ? ggml_backend_name(get_allocr_backend(sched, node_allocr(node->view_src))) : "NULL");
  993. }
  994. for (int j = 0; j < GGML_MAX_SRC; j++) {
  995. struct ggml_tensor * src = node->src[j];
  996. if (src == NULL) {
  997. break;
  998. }
  999. ggml_tallocr_t src_allocr = node_allocr(src);
  1000. if (src_allocr != node_allocr /* && src_backend != NULL */) { // ignore nulls for now
  1001. fprintf(stderr, "!!!! %s has backend %s, src %d (%s) has backend %s\n",
  1002. node->name, node_allocr ? ggml_backend_name(get_allocr_backend(sched, node_allocr)) : "NULL",
  1003. j, src->name, src_allocr ? ggml_backend_name(get_allocr_backend(sched, src_allocr)) : "NULL");
  1004. }
  1005. if (src->view_src != NULL && src_allocr != node_allocr(src->view_src)) {
  1006. fprintf(stderr, "!!!!!!! [src] %s has backend %s, view_src %s has backend %s\n",
  1007. src->name, src_allocr ? ggml_backend_name(get_allocr_backend(sched, src_allocr)) : "NULL",
  1008. src->view_src->name, node_allocr(src->view_src) ? ggml_backend_name(get_allocr_backend(sched, node_allocr(src->view_src))) : "NULL");
  1009. }
  1010. }
  1011. }
  1012. fflush(stderr);
  1013. #endif
  1014. // create copies of the graph for each split
  1015. // FIXME: avoid this copy, pass split inputs to ggml_gallocr_alloc_graph_n in some other way
  1016. struct ggml_cgraph * graph_copy = ggml_new_graph_custom(sched->ctx, graph->n_nodes + sched->n_splits*GGML_MAX_SPLIT_INPUTS, false);
  1017. for (int i = 0; i < sched->n_splits; i++) {
  1018. struct ggml_backend_sched_split * split = &sched->splits[i];
  1019. split->graph = ggml_graph_view(graph, split->i_start, split->i_end);
  1020. // add inputs to the graph copy so that they are allocated by ggml-alloc at the start of the split
  1021. for (int j = 0; j < split->n_inputs; j++) {
  1022. struct ggml_tensor * input = split->inputs[j];
  1023. struct ggml_tensor * input_cpy = sched->node_copies[hash_id(input)][sched_allocr_prio(sched, split->tallocr)];
  1024. // add a dependency to the input source so that it is not freed before the copy is done
  1025. GGML_ASSERT(input_cpy->src[0] == NULL || input_cpy->src[0] == input);
  1026. input_cpy->src[0] = input;
  1027. graph_copy->nodes[graph_copy->n_nodes++] = input_cpy;
  1028. }
  1029. for (int j = split->i_start; j < split->i_end; j++) {
  1030. graph_copy->nodes[graph_copy->n_nodes++] = graph->nodes[j];
  1031. }
  1032. }
  1033. sched->graph = graph_copy;
  1034. }
  1035. static void sched_alloc_splits(ggml_backend_sched_t sched) {
  1036. ggml_gallocr_alloc_graph_n(
  1037. sched->galloc,
  1038. sched->graph,
  1039. sched->hash_set,
  1040. sched->node_talloc);
  1041. }
  1042. static void sched_compute_splits(ggml_backend_sched_t sched) {
  1043. uint64_t copy_us[GGML_MAX_BACKENDS] = {0};
  1044. uint64_t compute_us[GGML_MAX_BACKENDS] = {0};
  1045. struct ggml_backend_sched_split * splits = sched->splits;
  1046. for (int i = 0; i < sched->n_splits; i++) {
  1047. struct ggml_backend_sched_split * split = &splits[i];
  1048. ggml_backend_t split_backend = get_allocr_backend(sched, split->tallocr);
  1049. int split_backend_id = sched_backend_prio(sched, split_backend);
  1050. // copy the input tensors to the split backend
  1051. uint64_t copy_start_us = ggml_time_us();
  1052. for (int j = 0; j < split->n_inputs; j++) {
  1053. struct ggml_tensor * input = split->inputs[j];
  1054. struct ggml_tensor * input_cpy = sched->node_copies[hash_id(input)][split_backend_id];
  1055. GGML_ASSERT(input->buffer != NULL);
  1056. GGML_ASSERT(input_cpy->buffer != NULL);
  1057. // TODO: avoid this copy if it was already copied in a previous split, and the input didn't change
  1058. // this is important to avoid copying constants such as KQ_mask and inp_pos multiple times
  1059. ggml_backend_tensor_copy_async(split_backend, input, input_cpy);
  1060. }
  1061. //ggml_backend_synchronize(split_backend); // necessary to measure copy time
  1062. int64_t copy_end_us = ggml_time_us();
  1063. copy_us[split_backend_id] += copy_end_us - copy_start_us;
  1064. #if 0
  1065. char split_filename[GGML_MAX_NAME];
  1066. snprintf(split_filename, GGML_MAX_NAME, "split_%i_%s.dot", i, ggml_backend_name(split_backend));
  1067. ggml_graph_dump_dot(split->graph, NULL, split_filename);
  1068. #endif
  1069. uint64_t compute_start_us = ggml_time_us();
  1070. ggml_backend_graph_compute(split_backend, &split->graph);
  1071. //ggml_backend_synchronize(split_backend); // necessary to measure compute time
  1072. uint64_t compute_end_us = ggml_time_us();
  1073. compute_us[split_backend_id] += compute_end_us - compute_start_us;
  1074. }
  1075. #if 0
  1076. // per-backend timings
  1077. fprintf(stderr, "sched_compute_splits times (%d splits):\n", sched->n_splits);
  1078. for (int i = 0; i < sched->n_backends; i++) {
  1079. if (copy_us[i] > 0 || compute_us[i] > 0) {
  1080. fprintf(stderr, "\t%5.5s: %lu us copy, %lu us compute\n", ggml_backend_name(sched->backends[i]), copy_us[i], compute_us[i]);
  1081. }
  1082. }
  1083. #endif
  1084. }
  1085. static void sched_reset(ggml_backend_sched_t sched) {
  1086. for (int i = 0; i < sched->n_backends; i++) {
  1087. ggml_tallocr_reset(sched->tallocs[i]);
  1088. }
  1089. // reset state for the next run
  1090. size_t hash_size = sched->hash_set.size;
  1091. memset(sched->hash_set.keys, 0, sizeof(sched->hash_set.keys[0]) * hash_size);
  1092. memset(sched->node_talloc, 0, sizeof(sched->node_talloc[0]) * hash_size);
  1093. memset(sched->node_copies, 0, sizeof(sched->node_copies[0]) * hash_size);
  1094. sched->is_reset = true;
  1095. }
  1096. 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) {
  1097. GGML_ASSERT(n_backends > 0);
  1098. GGML_ASSERT(n_backends <= GGML_MAX_BACKENDS);
  1099. struct ggml_backend_sched * sched = calloc(sizeof(struct ggml_backend_sched), 1);
  1100. // initialize hash table
  1101. sched->hash_set = ggml_hash_set_new(graph_size + GGML_MAX_SPLITS*GGML_MAX_SPLIT_INPUTS);
  1102. sched->node_talloc = calloc(sizeof(sched->node_talloc[0]) * sched->hash_set.size, 1);
  1103. sched->node_copies = calloc(sizeof(sched->node_copies[0]) * sched->hash_set.size, 1);
  1104. sched->n_backends = n_backends;
  1105. for (int i = 0; i < n_backends; i++) {
  1106. sched->backends[i] = backends[i];
  1107. sched->bufts[i] = bufts ? bufts[i] : ggml_backend_get_default_buffer_type(backends[i]);
  1108. }
  1109. sched->galloc = ggml_gallocr_new();
  1110. // init measure allocs for each backend
  1111. for (int i = 0; i < n_backends; i++) {
  1112. sched->tallocs[i] = ggml_tallocr_new_measure_from_buft(sched->bufts[i]);
  1113. }
  1114. sched_reset(sched);
  1115. return sched;
  1116. }
  1117. void ggml_backend_sched_free(ggml_backend_sched_t sched) {
  1118. if (sched == NULL) {
  1119. return;
  1120. }
  1121. for (int i = 0; i < sched->n_backends; i++) {
  1122. ggml_tallocr_free(sched->tallocs[i]);
  1123. }
  1124. ggml_gallocr_free(sched->galloc);
  1125. ggml_free(sched->ctx);
  1126. free(sched->hash_set.keys);
  1127. free(sched->node_talloc);
  1128. free(sched->node_copies);
  1129. free(sched);
  1130. }
  1131. void ggml_backend_sched_init_measure(ggml_backend_sched_t sched, struct ggml_cgraph * measure_graph) {
  1132. GGML_ASSERT(ggml_tallocr_is_measure(sched->tallocs[0])); // can only be initialized once
  1133. sched_split_graph(sched, measure_graph);
  1134. sched_alloc_splits(sched);
  1135. // allocate buffers and reset allocators
  1136. for (int i = 0; i < sched->n_backends; i++) {
  1137. size_t size = ggml_tallocr_max_size(sched->tallocs[i]);
  1138. ggml_tallocr_free(sched->tallocs[i]);
  1139. sched->tallocs[i] = ggml_tallocr_new_from_buft(sched->bufts[i], size);
  1140. }
  1141. sched_reset(sched);
  1142. }
  1143. void ggml_backend_sched_graph_compute(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
  1144. GGML_ASSERT((int)sched->hash_set.size >= graph->n_nodes + GGML_MAX_SPLITS*GGML_MAX_SPLIT_INPUTS);
  1145. if (!sched->is_reset) {
  1146. sched_reset(sched);
  1147. }
  1148. sched_split_graph(sched, graph);
  1149. sched_alloc_splits(sched);
  1150. sched_compute_splits(sched);
  1151. }
  1152. void ggml_backend_sched_reset(ggml_backend_sched_t sched) {
  1153. sched_reset(sched);
  1154. }
  1155. int ggml_backend_sched_get_n_splits(ggml_backend_sched_t sched) {
  1156. return sched->n_splits;
  1157. }
  1158. ggml_tallocr_t ggml_backend_sched_get_tallocr(ggml_backend_sched_t sched, ggml_backend_t backend) {
  1159. int backend_index = sched_backend_prio(sched, backend);
  1160. GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
  1161. return sched->tallocs[backend_index];
  1162. }
  1163. ggml_backend_buffer_t ggml_backend_sched_get_buffer(ggml_backend_sched_t sched, ggml_backend_t backend) {
  1164. int backend_index = sched_backend_prio(sched, backend);
  1165. GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
  1166. return ggml_tallocr_get_buffer(sched->tallocs[backend_index]);
  1167. }
  1168. void ggml_backend_sched_set_node_backend(ggml_backend_sched_t sched, struct ggml_tensor * node, ggml_backend_t backend) {
  1169. int backend_index = sched_backend_prio(sched, backend);
  1170. GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
  1171. node_allocr(node) = sched->tallocs[backend_index];
  1172. }
  1173. ggml_backend_t ggml_backend_sched_get_node_backend(ggml_backend_sched_t sched, struct ggml_tensor * node) {
  1174. ggml_tallocr_t allocr = node_allocr(node);
  1175. if (allocr == NULL) {
  1176. return NULL;
  1177. }
  1178. return get_allocr_backend(sched, allocr);
  1179. }
  1180. // utils
  1181. void ggml_backend_view_init(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) {
  1182. GGML_ASSERT(tensor->buffer == NULL);
  1183. //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
  1184. GGML_ASSERT(tensor->view_src != NULL);
  1185. GGML_ASSERT(tensor->view_src->buffer != NULL);
  1186. GGML_ASSERT(tensor->view_src->data != NULL);
  1187. tensor->buffer = buffer;
  1188. tensor->data = (char *)tensor->view_src->data + tensor->view_offs;
  1189. tensor->backend = tensor->view_src->backend;
  1190. ggml_backend_buffer_init_tensor(buffer, tensor);
  1191. }
  1192. void ggml_backend_tensor_alloc(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, void * addr) {
  1193. GGML_ASSERT(tensor->buffer == NULL);
  1194. GGML_ASSERT(tensor->data == NULL);
  1195. GGML_ASSERT(tensor->view_src == NULL);
  1196. GGML_ASSERT(addr >= ggml_backend_buffer_get_base(buffer));
  1197. GGML_ASSERT((char *)addr + ggml_backend_buffer_get_alloc_size(buffer, tensor) <=
  1198. (char *)ggml_backend_buffer_get_base(buffer) + ggml_backend_buffer_get_size(buffer));
  1199. tensor->buffer = buffer;
  1200. tensor->data = addr;
  1201. ggml_backend_buffer_init_tensor(buffer, tensor);
  1202. }
  1203. static struct ggml_tensor * graph_dup_tensor(struct ggml_hash_set hash_set, struct ggml_tensor ** node_copies,
  1204. struct ggml_context * ctx_allocated, struct ggml_context * ctx_unallocated, struct ggml_tensor * src) {
  1205. GGML_ASSERT(src != NULL);
  1206. GGML_ASSERT(src->data && "graph must be allocated");
  1207. size_t id = ggml_hash_insert(hash_set, src);
  1208. if (id == GGML_HASHTABLE_ALREADY_EXISTS) {
  1209. return node_copies[ggml_hash_find(hash_set, src)];
  1210. }
  1211. struct ggml_tensor * dst = ggml_dup_tensor_layout(src->data && !src->view_src ? ctx_allocated : ctx_unallocated, src);
  1212. if (src->view_src != NULL) {
  1213. dst->view_src = graph_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, src->view_src);
  1214. dst->view_offs = src->view_offs;
  1215. }
  1216. dst->op = src->op;
  1217. memcpy(dst->op_params, src->op_params, sizeof(dst->op_params));
  1218. ggml_set_name(dst, src->name);
  1219. // copy src
  1220. for (int i = 0; i < GGML_MAX_SRC; i++) {
  1221. struct ggml_tensor * s = src->src[i];
  1222. if (s == NULL) {
  1223. break;
  1224. }
  1225. dst->src[i] = graph_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, s);
  1226. }
  1227. node_copies[id] = dst;
  1228. return dst;
  1229. }
  1230. static void graph_init_tensor(struct ggml_hash_set hash_set, struct ggml_tensor ** node_copies, bool * node_init, struct ggml_tensor * src) {
  1231. size_t id = ggml_hash_find(hash_set, src);
  1232. if (node_init[id]) {
  1233. return;
  1234. }
  1235. node_init[id] = true;
  1236. struct ggml_tensor * dst = node_copies[id];
  1237. if (dst->view_src != NULL) {
  1238. graph_init_tensor(hash_set, node_copies, node_init, src->view_src);
  1239. ggml_backend_view_init(dst->view_src->buffer, dst);
  1240. }
  1241. else {
  1242. ggml_backend_tensor_copy(src, dst);
  1243. }
  1244. // init src
  1245. for (int i = 0; i < GGML_MAX_SRC; i++) {
  1246. struct ggml_tensor * s = src->src[i];
  1247. if (s == NULL) {
  1248. break;
  1249. }
  1250. graph_init_tensor(hash_set, node_copies, node_init, s);
  1251. }
  1252. }
  1253. struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph) {
  1254. struct ggml_hash_set hash_set = {
  1255. /* .size = */ graph->visited_hash_table.size,
  1256. /* .keys = */ calloc(sizeof(hash_set.keys[0]) * graph->visited_hash_table.size, 1)
  1257. };
  1258. struct ggml_tensor ** node_copies = calloc(sizeof(node_copies[0]) * hash_set.size, 1);
  1259. bool * node_init = calloc(sizeof(node_init[0]) * hash_set.size, 1);
  1260. struct ggml_init_params params = {
  1261. /* .mem_size = */ ggml_tensor_overhead()*hash_set.size + ggml_graph_overhead_custom(graph->size, false),
  1262. /* .mem_buffer = */ NULL,
  1263. /* .no_alloc = */ true
  1264. };
  1265. struct ggml_context * ctx_allocated = ggml_init(params);
  1266. struct ggml_context * ctx_unallocated = ggml_init(params);
  1267. if (ctx_allocated == NULL || ctx_unallocated == NULL) {
  1268. fprintf(stderr, "failed to allocate context for graph copy\n");
  1269. free(hash_set.keys);
  1270. free(node_copies);
  1271. free(node_init);
  1272. ggml_free(ctx_allocated);
  1273. ggml_free(ctx_unallocated);
  1274. return (struct ggml_backend_graph_copy) {
  1275. /* .buffer = */ NULL,
  1276. /* .ctx_allocated = */ NULL,
  1277. /* .ctx_unallocated = */ NULL,
  1278. /* .graph = */ NULL,
  1279. };
  1280. }
  1281. // dup nodes
  1282. for (int i = 0; i < graph->n_nodes; i++) {
  1283. struct ggml_tensor * node = graph->nodes[i];
  1284. graph_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, node);
  1285. }
  1286. // allocate nodes
  1287. ggml_backend_buffer_t buffer = ggml_backend_alloc_ctx_tensors(ctx_allocated, backend);
  1288. if (buffer == NULL) {
  1289. fprintf(stderr, "failed to allocate buffer for graph copy\n");
  1290. free(hash_set.keys);
  1291. free(node_copies);
  1292. free(node_init);
  1293. ggml_free(ctx_allocated);
  1294. ggml_free(ctx_unallocated);
  1295. return (struct ggml_backend_graph_copy) {
  1296. /* .buffer = */ NULL,
  1297. /* .ctx_allocated = */ NULL,
  1298. /* .ctx_unallocated = */ NULL,
  1299. /* .graph = */ NULL,
  1300. };
  1301. }
  1302. //printf("copy buffer size: %zu MB\n", ggml_backend_buffer_get_size(buffer) / 1024 / 1024);
  1303. // copy data and init views
  1304. for (int i = 0; i < graph->n_nodes; i++) {
  1305. struct ggml_tensor * node = graph->nodes[i];
  1306. graph_init_tensor(hash_set, node_copies, node_init, node);
  1307. }
  1308. // build graph copy
  1309. struct ggml_cgraph * graph_copy = ggml_new_graph_custom(ctx_allocated, graph->size, false);
  1310. for (int i = 0; i < graph->n_nodes; i++) {
  1311. struct ggml_tensor * node = graph->nodes[i];
  1312. struct ggml_tensor * node_copy = node_copies[ggml_hash_find(hash_set, node)];
  1313. graph_copy->nodes[i] = node_copy;
  1314. }
  1315. graph_copy->n_nodes = graph->n_nodes;
  1316. free(hash_set.keys);
  1317. free(node_copies);
  1318. free(node_init);
  1319. return (struct ggml_backend_graph_copy) {
  1320. /* .buffer = */ buffer,
  1321. /* .ctx_allocated = */ ctx_allocated,
  1322. /* .ctx_unallocated = */ ctx_unallocated,
  1323. /* .graph = */ graph_copy,
  1324. };
  1325. }
  1326. void ggml_backend_graph_copy_free(struct ggml_backend_graph_copy copy) {
  1327. ggml_backend_buffer_free(copy.buffer);
  1328. ggml_free(copy.ctx_allocated);
  1329. ggml_free(copy.ctx_unallocated);
  1330. }
  1331. 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) {
  1332. struct ggml_backend_graph_copy copy = ggml_backend_graph_copy(backend2, graph);
  1333. if (copy.buffer == NULL) {
  1334. return false;
  1335. }
  1336. struct ggml_cgraph * g1 = graph;
  1337. struct ggml_cgraph * g2 = copy.graph;
  1338. assert(g1->n_nodes == g2->n_nodes);
  1339. for (int i = 0; i < g1->n_nodes; i++) {
  1340. //printf("eval %d/%d\n", i, g1->n_nodes);
  1341. struct ggml_tensor * t1 = g1->nodes[i];
  1342. struct ggml_tensor * t2 = g2->nodes[i];
  1343. assert(t1->op == t2->op && ggml_are_same_layout(t1, t2));
  1344. struct ggml_cgraph g1v = ggml_graph_view(g1, i, i + 1);
  1345. struct ggml_cgraph g2v = ggml_graph_view(g2, i, i + 1);
  1346. ggml_backend_graph_compute(backend1, &g1v);
  1347. ggml_backend_graph_compute(backend2, &g2v);
  1348. if (ggml_is_view_op(t1->op)) {
  1349. continue;
  1350. }
  1351. // compare results, calculate rms etc
  1352. if (!callback(i, t1, t2, user_data)) {
  1353. break;
  1354. }
  1355. }
  1356. ggml_backend_graph_copy_free(copy);
  1357. return true;
  1358. }