ggml-opencl.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. //go:build opencl
  2. /**
  3. * llama.cpp - git 465219b9143ac01db0990bbcb0a081ef72ec2008
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2023 Georgi Gerganov
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. #include "ggml-opencl.h"
  28. #include <array>
  29. #include <atomic>
  30. #include <sstream>
  31. #include <vector>
  32. #include <limits>
  33. #define CL_TARGET_OPENCL_VERSION 110
  34. #include <clblast.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include "ggml.h"
  39. #if defined(_MSC_VER)
  40. #pragma warning(disable: 4244 4267) // possible loss of data
  41. #endif
  42. #define CL_DMMV_LOCAL_SIZE 32
  43. #ifndef K_QUANTS_PER_ITERATION
  44. #define K_QUANTS_PER_ITERATION 1
  45. #else
  46. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  47. #endif
  48. #define MULTILINE_QUOTE(...) #__VA_ARGS__
  49. static std::string program_source = MULTILINE_QUOTE(
  50. typedef char int8_t;
  51. typedef uchar uint8_t;
  52. typedef short int16_t;
  53. typedef ushort uint16_t;
  54. typedef int int32_t;
  55. typedef uint uint32_t;
  56. struct __attribute__ ((packed)) block_q4_0
  57. {
  58. half d;
  59. uint8_t qs[QK4_0 / 2];
  60. };
  61. struct __attribute__ ((packed)) block_q4_1
  62. {
  63. half d;
  64. half m;
  65. uint8_t qs[QK4_1 / 2];
  66. };
  67. struct __attribute__ ((packed)) block_q5_0
  68. {
  69. half d;
  70. uint32_t qh;
  71. uint8_t qs[QK5_0 / 2];
  72. };
  73. struct __attribute__ ((packed)) block_q5_1
  74. {
  75. half d;
  76. half m;
  77. uint32_t qh;
  78. uint8_t qs[QK5_1 / 2];
  79. };
  80. struct __attribute__ ((packed)) block_q8_0
  81. {
  82. half d;
  83. int8_t qs[QK8_0];
  84. };
  85. struct __attribute__((packed)) block_q2_K
  86. {
  87. uint8_t scales[16];
  88. uint8_t qs[64];
  89. half d;
  90. half dmin;
  91. };
  92. struct __attribute__((packed)) block_q3_K
  93. {
  94. uint8_t hmask[32];
  95. uint8_t qs[64];
  96. uint8_t scales[12];
  97. half d;
  98. };
  99. struct __attribute__((packed)) block_q4_K
  100. {
  101. half d;
  102. half dmin;
  103. uint8_t scales[12];
  104. uint8_t qs[128];
  105. };
  106. struct __attribute__((packed)) block_q5_K
  107. {
  108. half d;
  109. half dmin;
  110. uint8_t scales[12];
  111. uint8_t qh[32];
  112. uint8_t qs[128];
  113. };
  114. struct __attribute__((packed)) block_q6_K
  115. {
  116. uint8_t ql[128];
  117. uint8_t qh[64];
  118. int8_t scales[16];
  119. half d;
  120. };
  121. __kernel void convert_fp16_to_fp32(__global half* x, __global float* y) {
  122. const uint i = get_global_id(0);
  123. y[i] = vload_half(0, &x[i]);
  124. }
  125. void dequantize_q4_0(__global const struct block_q4_0* x, const int ib, const int iqs, float* v0, float* v1) {
  126. const float d = vload_half(0, &x[ib].d);
  127. const uint8_t vui = x[ib].qs[iqs];
  128. const int8_t vi0 = vui & 0xF;
  129. const int8_t vi1 = vui >> 4;
  130. *v0 = (vi0 - 8)*d;
  131. *v1 = (vi1 - 8)*d;
  132. }
  133. void dequantize_q4_1(__global const struct block_q4_1* x, const int ib, const int iqs, float* v0, float* v1) {
  134. const float d = vload_half(0, &x[ib].d);
  135. const float m = vload_half(0, &x[ib].m);
  136. const uint8_t vui = x[ib].qs[iqs];
  137. const int8_t vi0 = vui & 0xF;
  138. const int8_t vi1 = vui >> 4;
  139. *v0 = vi0*d + m;
  140. *v1 = vi1*d + m;
  141. }
  142. void dequantize_q5_0(__global const struct block_q5_0* x, const int ib, const int iqs, float* v0, float* v1) {
  143. const float d = vload_half(0, &x[ib].d);
  144. uint32_t qh = x[ib].qh;
  145. const uint8_t xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  146. const uint8_t xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  147. const int32_t x0 = ((x[ib].qs[iqs] & 0xf) | xh_0) - 16;
  148. const int32_t x1 = ((x[ib].qs[iqs] >> 4) | xh_1) - 16;
  149. *v0 = x0*d;
  150. *v1 = x1*d;
  151. }
  152. void dequantize_q5_1(__global const struct block_q5_1* x, const int ib, const int iqs, float* v0, float* v1) {
  153. const float d = vload_half(0, &x[ib].d);
  154. const float m = vload_half(0, &x[ib].m);
  155. uint32_t qh = x[ib].qh;
  156. const uint8_t xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  157. const uint8_t xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  158. const int32_t x0 = ((x[ib].qs[iqs] & 0xf) | xh_0);
  159. const int32_t x1 = ((x[ib].qs[iqs] >> 4) | xh_1);
  160. *v0 = x0*d + m;
  161. *v1 = x1*d + m;
  162. }
  163. void dequantize_q8_0(__global const struct block_q8_0* x, const int ib, const int iqs, float* v0, float* v1) {
  164. const float d = vload_half(0, &x[ib].d);
  165. const int8_t vi0 = x[ib].qs[iqs + 0];
  166. const int8_t vi1 = x[ib].qs[iqs + 1];
  167. *v0 = vi0*d;
  168. *v1 = vi1*d;
  169. }
  170. void convert_f16(__global half* x, const int ib, const int iqs, float* v0, float* v1){
  171. *v0 = vload_half(0, &x[ib + 0]);
  172. *v1 = vload_half(0, &x[ib + 1]);
  173. }
  174. );
  175. static std::string k_quants_source = MULTILINE_QUOTE(
  176. inline void get_scale_min_k4(int j, const __global uint8_t *q, uint8_t *d, uint8_t *m)
  177. {
  178. if (j < 4)
  179. {
  180. *d = q[j] & 63;
  181. *m = q[j + 4] & 63;
  182. }
  183. else
  184. {
  185. *d = (q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4);
  186. *m = (q[j + 4] >> 4) | ((q[j - 0] >> 6) << 4);
  187. }
  188. }
  189. __kernel void dequantize_block_q2_K(__global const struct block_q2_K *x, __global float *yy)
  190. {
  191. const int i = get_group_id(0) + get_global_offset(0);
  192. const int tid = get_local_id(0);
  193. const int n = tid / 32;
  194. const int l = tid - 32 * n;
  195. const int is = 8 * n + l / 16;
  196. const uint8_t q = x[i].qs[32 * n + l];
  197. __global float *y = yy + get_group_id(0) * QK_K + 128 * n;
  198. const float dall = vload_half(0, &x[i].d);
  199. const float dmin = vload_half(0, &x[i].dmin);
  200. y[l + 0] = dall * (x[i].scales[is + 0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is + 0] >> 4);
  201. y[l + 32] = dall * (x[i].scales[is + 2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is + 2] >> 4);
  202. y[l + 64] = dall * (x[i].scales[is + 4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is + 4] >> 4);
  203. y[l + 96] = dall * (x[i].scales[is + 6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is + 6] >> 4);
  204. }
  205. __kernel void dequantize_block_q3_K(__global const struct block_q3_K *x, __global float *yy)
  206. {
  207. int r = get_local_id(0) / 4;
  208. int i = get_group_id(0) + get_global_offset(0);
  209. int tid = r / 2;
  210. int is0 = r % 2;
  211. int l0 = 16 * is0 + 4 * (get_local_id(0) % 4);
  212. int n = tid / 4;
  213. int j = tid - 4 * n;
  214. uint8_t m = 1 << (4 * n + j);
  215. int is = 8 * n + 2 * j + is0;
  216. int shift = 2 * j;
  217. int8_t us = is < 4 ? (x[i].scales[is - 0] & 0xF) | (((x[i].scales[is + 8] >> 0) & 3) << 4)
  218. : is < 8 ? (x[i].scales[is - 0] & 0xF) | (((x[i].scales[is + 4] >> 2) & 3) << 4)
  219. : is < 12 ? (x[i].scales[is - 8] >> 4) | (((x[i].scales[is + 0] >> 4) & 3) << 4)
  220. : (x[i].scales[is - 8] >> 4) | (((x[i].scales[is - 4] >> 6) & 3) << 4);
  221. float d_all = vload_half(0, &x[i].d);
  222. float dl = d_all * (us - 32);
  223. __global float *y = yy + get_group_id(0) * QK_K + 128 * n + 32 * j;
  224. const __global uint8_t *q = x[i].qs + 32 * n;
  225. const __global uint8_t *hm = x[i].hmask;
  226. for (int l = l0; l < l0 + 4; ++l)
  227. y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  228. }
  229. __kernel void dequantize_block_q4_K(__global const struct block_q4_K *x, __global float *yy)
  230. {
  231. const int i = get_group_id(0) + get_global_offset(0);
  232. const int tid = get_local_id(0);
  233. const int il = tid / 8;
  234. const int ir = tid % 8;
  235. const int is = 2 * il;
  236. const int n = 4;
  237. __global float *y = yy + get_group_id(0) * QK_K + 64 * il + n * ir;
  238. const float dall = vload_half(0, &x[i].d);
  239. const float dmin = vload_half(0, &x[i].dmin);
  240. __global const uint8_t *q = x[i].qs + 32 * il + n * ir;
  241. uint8_t sc, m;
  242. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  243. float d1 = dall * sc;
  244. float m1 = dmin * m;
  245. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  246. float d2 = dall * sc;
  247. float m2 = dmin * m;
  248. for (int l = 0; l < n; ++l)
  249. {
  250. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  251. y[l + 32] = d2 * (q[l] >> 4) - m2;
  252. }
  253. }
  254. __kernel void dequantize_block_q5_K(__global const struct block_q5_K *x, __global float *yy)
  255. {
  256. const int i = get_group_id(0) + get_global_offset(0);
  257. const int tid = get_local_id(0);
  258. const int il = tid / 16;
  259. const int ir = tid % 16;
  260. const int is = 2 * il;
  261. __global float *y = yy + get_group_id(0) * QK_K + 64 * il + 2 * ir;
  262. const float dall = vload_half(0, &x[i].d);
  263. const float dmin = vload_half(0, &x[i].dmin);
  264. __global const uint8_t *ql = x[i].qs + 32 * il + 2 * ir;
  265. __global const uint8_t *qh = x[i].qh + 2 * ir;
  266. uint8_t sc, m;
  267. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  268. const float d1 = dall * sc;
  269. const float m1 = dmin * m;
  270. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  271. const float d2 = dall * sc;
  272. const float m2 = dmin * m;
  273. uint8_t hm = 1 << (2 * il);
  274. y[0] = d1 * ((ql[0] & 0xF) + (qh[0] & hm ? 16 : 0)) - m1;
  275. y[1] = d1 * ((ql[1] & 0xF) + (qh[1] & hm ? 16 : 0)) - m1;
  276. hm <<= 1;
  277. y[32] = d2 * ((ql[0] >> 4) + (qh[0] & hm ? 16 : 0)) - m2;
  278. y[33] = d2 * ((ql[1] >> 4) + (qh[1] & hm ? 16 : 0)) - m2;
  279. }
  280. __kernel void dequantize_block_q6_K(__global const struct block_q6_K *x, __global float *yy)
  281. {
  282. const int i = get_group_id(0) + get_global_offset(0);
  283. const int tid = get_local_id(0);
  284. const int ip = tid / 32;
  285. const int il = tid - 32 * ip;
  286. const int is = 8 * ip + il / 16;
  287. __global float *y = yy + get_group_id(0) * QK_K + 128 * ip + il;
  288. const float d = vload_half(0, &x[i].d);
  289. __global const uint8_t *ql = x[i].ql + 64 * ip + il;
  290. const uint8_t qh = x[i].qh[32 * ip + il];
  291. __global const int8_t *sc = x[i].scales + is;
  292. y[0] = d * sc[0] * ((int8_t)((ql[0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  293. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  294. y[64] = d * sc[4] * ((int8_t)((ql[0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  295. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  296. }
  297. __kernel void dequantize_mul_mat_vec_q2_K(__global const struct block_q2_K * xx, __local float* tmp, __global float* yy, __global float* dst, const int ncols) {
  298. const int row = get_group_id(0);
  299. const int num_blocks_per_row = ncols / QK_K;
  300. const int ib0 = row*num_blocks_per_row + get_global_offset(0);
  301. __global const struct block_q2_K * x = xx + ib0;
  302. const int tid = get_local_id(0)/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  303. const int ix = get_local_id(0)%K_QUANTS_PER_ITERATION; // 0 or 0,1
  304. const int step = 16/K_QUANTS_PER_ITERATION;
  305. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  306. const int in = tid - step*im; // 0...15 or 0...7
  307. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  308. const int q_offset = 32*im + l0;
  309. const int s_offset = 8*im;
  310. const int y_offset = 128*im + l0;
  311. tmp[16 * ix + tid] = 0;
  312. uint32_t aux[4];
  313. const uint8_t * d = (const uint8_t *)aux;
  314. const uint8_t * m = (const uint8_t *)(aux + 2);
  315. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  316. __global const float * y = yy + i * QK_K + y_offset;
  317. __global const uint8_t * q = x[i].qs + q_offset;
  318. const float dall = vload_half(0, &x[i].d);
  319. const float dmin = vload_half(0, &x[i].dmin);
  320. __global const uint32_t * a = (__global const uint32_t *)(x[i].scales + s_offset);
  321. aux[0] = a[0] & 0x0f0f0f0f;
  322. aux[1] = a[1] & 0x0f0f0f0f;
  323. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  324. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  325. float sum1 = 0, sum2 = 0;
  326. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  327. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  328. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  329. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  330. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  331. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  332. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  333. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  334. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  335. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  336. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  337. }
  338. tmp[16 * ix + tid] += dall * sum1 - dmin * sum2;
  339. }
  340. // sum up partial sums and write back result
  341. barrier(CLK_LOCAL_MEM_FENCE);
  342. for (int s=16; s>0; s>>=1) {
  343. if (tid < s) {
  344. tmp[tid] += tmp[tid + s];
  345. }
  346. barrier(CLK_LOCAL_MEM_FENCE);
  347. }
  348. if (tid == 0) {
  349. dst[row] = tmp[0];
  350. }
  351. }
  352. __kernel void dequantize_mul_mat_vec_q3_K(__global const struct block_q3_K * xx, __local float* tmp, __global float* yy, __global float* dst, const int ncols) {
  353. const uint16_t kmask1 = 0x0303;
  354. const uint16_t kmask2 = 0x0f0f;
  355. const int row = get_group_id(0);
  356. const int num_blocks_per_row = ncols / QK_K;
  357. const int ib0 = row*num_blocks_per_row + get_global_offset(0);
  358. __global const struct block_q3_K * x = xx + ib0;
  359. const int tid = get_local_id(0)/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  360. const int ix = get_local_id(0)%K_QUANTS_PER_ITERATION; // 0 or 0,1
  361. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  362. const int step = 16/K_QUANTS_PER_ITERATION;
  363. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  364. const int in = tid - step*im; // 0....15 or 0...7
  365. const uint8_t m = 1 << (4*im);
  366. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  367. const int q_offset = 32*im + l0;
  368. const int y_offset = 128*im + l0;
  369. uint16_t utmp[4];
  370. const int8_t * s = (const int8_t *)utmp;
  371. const uint16_t s_shift = 4*im;
  372. tmp[16 * ix + tid] = 0;
  373. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  374. __global const float * y = yy + i * QK_K + y_offset;
  375. __global const uint8_t * q = x[i].qs + q_offset;
  376. __global const uint8_t * h = x[i].hmask + l0;
  377. __global const uint16_t * a = (__global const uint16_t *)x[i].scales;
  378. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  379. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  380. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  381. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  382. const float d = vload_half(0, &x[i].d);
  383. float sum = 0;
  384. for (int l = 0; l < n; ++l) {
  385. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  386. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  387. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  388. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  389. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  390. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  391. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  392. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  393. }
  394. tmp[16 * ix + tid] += d * sum;
  395. }
  396. // sum up partial sums and write back result
  397. barrier(CLK_LOCAL_MEM_FENCE);
  398. for (int s=16; s>0; s>>=1) {
  399. if (tid < s) {
  400. tmp[tid] += tmp[tid + s];
  401. }
  402. barrier(CLK_LOCAL_MEM_FENCE);
  403. }
  404. if (tid == 0) {
  405. dst[row] = tmp[0];
  406. }
  407. }
  408. __kernel void dequantize_mul_mat_vec_q4_K(__global const struct block_q4_K * xx, __local float* tmp, __global float* yy, __global float* dst, const int ncols) {
  409. //to rename it later, just to test now
  410. const uint16_t kmask1 = 0x3f3f;
  411. const uint16_t kmask2 = 0x0f0f;
  412. const uint16_t kmask3 = 0xc0c0;
  413. const int row = get_group_id(0);
  414. const int num_blocks_per_row = ncols / QK_K;
  415. const int ib0 = row*num_blocks_per_row + get_global_offset(0);
  416. const int tid = get_local_id(0)/K_QUANTS_PER_ITERATION; // 0...15
  417. const int ix = get_local_id(0)%K_QUANTS_PER_ITERATION;
  418. const int step = 8/K_QUANTS_PER_ITERATION;
  419. const int il = tid/step; // 0...3
  420. const int ir = tid - step*il;// 0...3
  421. const int n = 2*K_QUANTS_PER_ITERATION;
  422. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  423. const int in = il%2;
  424. const int l0 = n*(2*ir + in);
  425. const int q_offset = 32*im + l0;
  426. const int y_offset = 64*im + l0;
  427. uint16_t aux[4];
  428. const uint8_t * sc = (const uint8_t *)aux;
  429. __global const struct block_q4_K * x = xx + ib0;
  430. tmp[16 * ix + tid] = 0;
  431. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  432. __global const uint8_t * q1 = x[i].qs + q_offset;
  433. __global const uint8_t * q2 = q1 + 64;
  434. __global const float * y1 = yy + i*QK_K + y_offset;
  435. __global const float * y2 = y1 + 128;
  436. const float dall = vload_half(0, &x[i].d);
  437. const float dmin = vload_half(0, &x[i].dmin);
  438. __global const uint16_t * a = (__global const uint16_t *)x[i].scales;
  439. aux[0] = a[im+0] & kmask1;
  440. aux[1] = a[im+2] & kmask1;
  441. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  442. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  443. float4 s = (float4)(0.f);
  444. float smin = 0;
  445. for (int l = 0; l < n; ++l) {
  446. s.x += y1[l] * (q1[l] & 0xF); s.y += y1[l+32] * (q1[l] >> 4);
  447. s.z += y2[l] * (q2[l] & 0xF); s.w += y2[l+32] * (q2[l] >> 4);
  448. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  449. }
  450. tmp[16 * ix + tid] += dall * (s.x * sc[0] + s.y * sc[1] + s.z * sc[4] + s.w * sc[5]) - dmin * smin;
  451. }
  452. // sum up partial sums and write back result
  453. barrier(CLK_LOCAL_MEM_FENCE);
  454. for (int s=16; s>0; s>>=1) {
  455. if (tid < s) {
  456. tmp[tid] += tmp[tid + s];
  457. }
  458. barrier(CLK_LOCAL_MEM_FENCE);
  459. }
  460. if (tid == 0) {
  461. dst[row] = tmp[0];
  462. }
  463. }
  464. __kernel void dequantize_mul_mat_vec_q5_K(__global const struct block_q5_K * xx, __local float* tmp, __global float* yy, __global float* dst, const int ncols) {
  465. const uint16_t kmask1 = 0x3f3f;
  466. const uint16_t kmask2 = 0x0f0f;
  467. const uint16_t kmask3 = 0xc0c0;
  468. const int row = get_group_id(0);
  469. const int num_blocks_per_row = ncols / QK_K;
  470. const int ib0 = row*num_blocks_per_row + get_global_offset(0);
  471. const int tid = get_local_id(0)/2; // 0...15
  472. const int ix = get_local_id(0)%2;
  473. const int il = tid/4; // 0...3
  474. const int ir = tid - 4*il;// 0...3
  475. const int n = 2;
  476. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  477. const int in = il%2;
  478. const int l0 = n*(2*ir + in);
  479. const int q_offset = 32*im + l0;
  480. const int y_offset = 64*im + l0;
  481. const uint8_t hm1 = 1 << (2*im);
  482. const uint8_t hm2 = hm1 << 4;
  483. uint16_t aux[4];
  484. const uint8_t * sc = (const uint8_t *)aux;
  485. __global const struct block_q5_K * x = xx + ib0;
  486. tmp[16 * ix + tid] = 0;
  487. for (int i = ix; i < num_blocks_per_row; i += 2) {
  488. __global const uint8_t * ql1 = x[i].qs + q_offset;
  489. __global const uint8_t * ql2 = ql1 + 64;
  490. __global const uint8_t * qh = x[i].qh + l0;
  491. __global const float * y1 = yy + i*QK_K + y_offset;
  492. __global const float * y2 = y1 + 128;
  493. const float dall = vload_half(0, &x[i].d);
  494. const float dmin = vload_half(0, &x[i].dmin);
  495. __global const uint16_t * a = (__global const uint16_t *)x[i].scales;
  496. aux[0] = a[im+0] & kmask1;
  497. aux[1] = a[im+2] & kmask1;
  498. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  499. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  500. float4 sum = (float4)(0.f);
  501. float smin = 0;
  502. for (int l = 0; l < n; ++l) {
  503. sum.x += y1[l+ 0] * ((ql1[l+ 0] & 0xF) + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
  504. + y1[l+16] * ((ql1[l+16] & 0xF) + (qh[l+16] & (hm1 << 0) ? 16 : 0));
  505. sum.y += y1[l+32] * ((ql1[l+ 0] >> 4) + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
  506. + y1[l+48] * ((ql1[l+16] >> 4) + (qh[l+16] & (hm1 << 1) ? 16 : 0));
  507. sum.z += y2[l+ 0] * ((ql2[l+ 0] & 0xF) + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
  508. + y2[l+16] * ((ql2[l+16] & 0xF) + (qh[l+16] & (hm2 << 0) ? 16 : 0));
  509. sum.w += y2[l+32] * ((ql2[l+ 0] >> 4) + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
  510. + y2[l+48] * ((ql2[l+16] >> 4) + (qh[l+16] & (hm2 << 1) ? 16 : 0));
  511. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  512. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  513. }
  514. tmp[16 * ix + tid] += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
  515. }
  516. // sum up partial sums and write back result
  517. barrier(CLK_LOCAL_MEM_FENCE);
  518. for (int s=16; s>0; s>>=1) {
  519. if (tid < s) {
  520. tmp[tid] += tmp[tid + s];
  521. }
  522. barrier(CLK_LOCAL_MEM_FENCE);
  523. }
  524. if (tid == 0) {
  525. dst[row] = tmp[0];
  526. }
  527. }
  528. __kernel void dequantize_mul_mat_vec_q6_K(__global const struct block_q6_K * xx, __local float* tmp, __global const float * yy, __global float * dst, const int ncols) {
  529. const int row = get_group_id(0);
  530. const int num_blocks_per_row = ncols / QK_K;
  531. const int ib0 = row*num_blocks_per_row + get_global_offset(0);
  532. __global const struct block_q6_K * x = xx + ib0;
  533. const int tid = get_local_id(0)/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  534. const int ix = get_local_id(0)%K_QUANTS_PER_ITERATION; // 0 or 0, 1
  535. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  536. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  537. const int in = tid - step*im; // 0...15 or 0...7
  538. \n#if K_QUANTS_PER_ITERATION == 1\n
  539. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  540. const int is = 0;
  541. \n#else\n
  542. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  543. const int is = in / 4;
  544. \n#endif\n
  545. const int ql_offset = 64*im + l0;
  546. const int qh_offset = 32*im + l0;
  547. const int s_offset = 8*im + is;
  548. const int y_offset = 128*im + l0;
  549. tmp[16 * ix + tid] = 0; // partial sum for thread in warp
  550. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  551. __global const float * y = yy + i * QK_K + y_offset;
  552. __global const uint8_t * ql = x[i].ql + ql_offset;
  553. __global const uint8_t * qh = x[i].qh + qh_offset;
  554. __global const int8_t * s = x[i].scales + s_offset;
  555. const float d = vload_half(0, &x[i].d);
  556. \n#if K_QUANTS_PER_ITERATION == 1\n
  557. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  558. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  559. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  560. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  561. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  562. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  563. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  564. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  565. tmp[16 * ix + tid] += sum;
  566. \n#else\n
  567. float sum = 0;
  568. for (int l = 0; l < 4; ++l) {
  569. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  570. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  571. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  572. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  573. }
  574. tmp[16 * ix + tid] += sum;
  575. \n#endif\n
  576. }
  577. // sum up partial sums and write back result
  578. barrier(CLK_LOCAL_MEM_FENCE);
  579. for (int s=16; s>0; s>>=1) {
  580. if (tid < s) {
  581. tmp[tid] += tmp[tid + s];
  582. }
  583. barrier(CLK_LOCAL_MEM_FENCE);
  584. }
  585. if (tid == 0) {
  586. dst[row] = tmp[0];
  587. }
  588. }
  589. );
  590. std::string dequant_template = MULTILINE_QUOTE(
  591. __kernel void KERNEL_NAME(__global X_TYPE* x, __global float* y) {
  592. const int i = get_group_id(0)*get_local_size(0) + get_local_id(0)*2;
  593. if (i >= get_global_size(0)) {
  594. return;
  595. }
  596. const uint qk = QUANT_K;
  597. const uint qr = QUANT_R;
  598. const int ib = i/qk + get_global_offset(0); // block index
  599. const int iqs = (i%qk)/qr; // quant index
  600. const int iybs = i - i%qk; // y block start index
  601. const int y_offset = qr == 1 ? 1 : qk/2;
  602. // dequantize
  603. float v0, v1;
  604. DEQUANT_FUNC(x, ib, iqs, &v0, &v1);
  605. y[iybs + iqs + 0] = v0;
  606. y[iybs + iqs + y_offset] = v1;
  607. }
  608. );
  609. std::string dequant_mul_mat_vec_template = MULTILINE_QUOTE(
  610. __kernel void KERNEL_NAME(__global X_TYPE* x, __local float* tmp, __global float* y, __global float* dst, const int ncols) {
  611. const int local_size = get_local_size(0);
  612. const int row = get_group_id(0);
  613. const int tid = get_local_id(0);
  614. const uint qk = QUANT_K;
  615. const uint qr = QUANT_R;
  616. const int col_step = local_size * 2;
  617. const int y_offset = qr == 1 ? 1 : qk/2;
  618. x += get_global_offset(0);
  619. tmp[tid] = 0;
  620. for (int col = tid*2; col < ncols; col += col_step) {
  621. const int ib = (row*ncols + col)/qk; // block index
  622. const int iqs = (col%qk)/qr; // quant index
  623. const int iybs = col - col%qk; // y block start index
  624. // dequantize
  625. float v0, v1;
  626. DEQUANT_FUNC(x, ib, iqs, &v0, &v1);
  627. // matrix multiplication
  628. tmp[tid] += v0 * y[iybs + iqs + 0];
  629. tmp[tid] += v1 * y[iybs + iqs + y_offset];
  630. }
  631. // sum up partial sums and write back result
  632. barrier(CLK_LOCAL_MEM_FENCE);
  633. for (int s=local_size/2; s>0; s>>=1) {
  634. if (tid < s) {
  635. tmp[tid] += tmp[tid + s];
  636. }
  637. barrier(CLK_LOCAL_MEM_FENCE);
  638. }
  639. if (tid == 0) {
  640. dst[row] = tmp[0];
  641. }
  642. }
  643. );
  644. std::string mul_template = MULTILINE_QUOTE(
  645. __kernel void KERNEL_NAME(__global TYPE* x, const int x_offset, __global TYPE* y, const int y_offset, __global TYPE* dst, const int dst_offset, const int ky) {
  646. const int i = get_group_id(0)*get_local_size(0) + get_local_id(0);
  647. if (i >= get_global_size(0)) {
  648. return;
  649. }
  650. dst[dst_offset + i] = x[x_offset + i] * y[y_offset + i%ky];
  651. }
  652. );
  653. #define CL_CHECK(err) \
  654. do { \
  655. cl_int err_ = (err); \
  656. if (err_ != CL_SUCCESS) { \
  657. fprintf(stderr, "ggml_opencl: %s error %d at %s:%d\n", \
  658. #err, err_, __FILE__, __LINE__); \
  659. exit(1); \
  660. } \
  661. } while (0)
  662. #define CLBLAST_CHECK(err) \
  663. do { \
  664. CLBlastStatusCode err_ = (err); \
  665. if (err_ != CLBlastSuccess) { \
  666. fprintf(stderr, "ggml_opencl: %s error %d at %s:%d\n", \
  667. #err, err_, __FILE__, __LINE__); \
  668. exit(1); \
  669. } \
  670. } while (0)
  671. std::array<std::string, 5> dequant_str_keys = {
  672. "KERNEL_NAME", "X_TYPE", "QUANT_K", "QUANT_R", "DEQUANT_FUNC"
  673. };
  674. std::array<std::string, 30> dequant_str_values = {
  675. "dequantize_row_q4_0", "struct block_q4_0", "QK4_0", "QR4_0", "dequantize_q4_0",
  676. "dequantize_row_q4_1", "struct block_q4_1", "QK4_1", "QR4_1", "dequantize_q4_1",
  677. "dequantize_row_q5_0", "struct block_q5_0", "QK5_0", "QR5_0", "dequantize_q5_0",
  678. "dequantize_row_q5_1", "struct block_q5_1", "QK5_1", "QR5_1", "dequantize_q5_1",
  679. "dequantize_row_q8_0", "struct block_q8_0", "QK8_0", "QR8_0", "dequantize_q8_0",
  680. "convert_row_f16", "half", "1", "1", "convert_f16"
  681. };
  682. std::array<std::string, 30> dequant_mul_mat_vec_str_values = {
  683. "dequantize_mul_mat_vec_q4_0", "struct block_q4_0", "QK4_0", "QR4_0", "dequantize_q4_0",
  684. "dequantize_mul_mat_vec_q4_1", "struct block_q4_1", "QK4_1", "QR4_1", "dequantize_q4_1",
  685. "dequantize_mul_mat_vec_q5_0", "struct block_q5_0", "QK5_0", "QR5_0", "dequantize_q5_0",
  686. "dequantize_mul_mat_vec_q5_1", "struct block_q5_1", "QK5_1", "QR5_1", "dequantize_q5_1",
  687. "dequantize_mul_mat_vec_q8_0", "struct block_q8_0", "QK8_0", "QR8_0", "dequantize_q8_0",
  688. "convert_mul_mat_vec_f16", "half", "1", "1", "convert_f16"
  689. };
  690. std::array<std::string, 2> mul_str_keys = {
  691. "KERNEL_NAME", "TYPE"
  692. };
  693. std::array<std::string, 2> mul_str_values = {
  694. "mul_f32", "float"
  695. };
  696. static std::string& replace(std::string& s, const std::string& from, const std::string& to) {
  697. size_t pos = 0;
  698. while ((pos = s.find(from, pos)) != std::string::npos) {
  699. s.replace(pos, from.length(), to);
  700. pos += to.length();
  701. }
  702. return s;
  703. }
  704. static std::string generate_kernels() {
  705. std::stringstream src;
  706. src << program_source << '\n';
  707. src << k_quants_source << '\n';
  708. for (size_t i = 0; i < dequant_str_values.size(); i += dequant_str_keys.size()) {
  709. std::string dequant_kernel = dequant_template;
  710. std::string dmmv_kernel = dequant_mul_mat_vec_template;
  711. for (size_t j = 0; j < dequant_str_keys.size(); j++) {
  712. replace(dequant_kernel, dequant_str_keys[j], dequant_str_values[i + j]);
  713. replace(dmmv_kernel, dequant_str_keys[j], dequant_mul_mat_vec_str_values[i + j]);
  714. }
  715. src << dequant_kernel << '\n';
  716. src << dmmv_kernel << '\n';
  717. }
  718. for (size_t i = 0; i < mul_str_values.size(); i += mul_str_keys.size()) {
  719. std::string mul_kernel = mul_template;
  720. for (size_t j = 0; j < mul_str_keys.size(); j++) {
  721. replace(mul_kernel, mul_str_keys[j], mul_str_values[i + j]);
  722. }
  723. src << mul_kernel << '\n';
  724. }
  725. return src.str();
  726. }
  727. static cl_platform_id platform;
  728. static cl_device_id device;
  729. static cl_context context;
  730. static cl_command_queue queue;
  731. static cl_program program;
  732. static cl_kernel convert_row_f16_cl;
  733. static cl_kernel dequantize_row_q4_0_cl, dequantize_row_q4_1_cl, dequantize_row_q5_0_cl, dequantize_row_q5_1_cl, dequantize_row_q8_0_cl;
  734. static cl_kernel dequantize_mul_mat_vec_q4_0_cl, dequantize_mul_mat_vec_q4_1_cl, dequantize_mul_mat_vec_q5_0_cl, dequantize_mul_mat_vec_q5_1_cl, dequantize_mul_mat_vec_q8_0_cl, convert_mul_mat_vec_f16_cl;
  735. static cl_kernel dequantize_block_q2_k_cl, dequantize_block_q3_k_cl, dequantize_block_q4_k_cl, dequantize_block_q5_k_cl, dequantize_block_q6_k_cl;
  736. static cl_kernel dequantize_mul_mat_vec_q2_K_cl, dequantize_mul_mat_vec_q3_K_cl, dequantize_mul_mat_vec_q4_K_cl, dequantize_mul_mat_vec_q5_K_cl, dequantize_mul_mat_vec_q6_K_cl;
  737. static cl_kernel mul_f32_cl;
  738. static bool fp16_support;
  739. static cl_program build_program_from_source(cl_context ctx, cl_device_id dev, const char* program_buffer) {
  740. cl_program p;
  741. char *program_log;
  742. size_t program_size;
  743. size_t log_size;
  744. int err;
  745. program_size = strlen(program_buffer);
  746. p = clCreateProgramWithSource(ctx, 1, (const char**)&program_buffer, &program_size, &err);
  747. if(err < 0) {
  748. fprintf(stderr, "OpenCL error creating program");
  749. exit(1);
  750. }
  751. std::string compile_opts = "-cl-mad-enable -cl-unsafe-math-optimizations -cl-finite-math-only -cl-fast-relaxed-math "
  752. "-DQK4_0=32 -DQR4_0=2 -DQK4_1=32 -DQR4_1=2 -DQK5_0=32 -DQR5_0=2 -DQK5_1=32 -DQR5_1=2 -DQK8_0=32 -DQR8_0=1 "
  753. "-DQK_K=256 -DK_QUANTS_PER_ITERATION=" + std::to_string(K_QUANTS_PER_ITERATION);
  754. err = clBuildProgram(p, 0, NULL, compile_opts.c_str(), NULL, NULL);
  755. if(err < 0) {
  756. clGetProgramBuildInfo(p, dev, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
  757. program_log = (char*) malloc(log_size + 1);
  758. program_log[log_size] = '\0';
  759. clGetProgramBuildInfo(p, dev, CL_PROGRAM_BUILD_LOG, log_size + 1, program_log, NULL);
  760. fprintf(stderr, "ggml_opencl: kernel compile error:\n\n%s\n", program_log);
  761. free(program_log);
  762. exit(1);
  763. }
  764. return p;
  765. }
  766. void ggml_cl_init(void) {
  767. cl_int err;
  768. struct cl_device;
  769. struct cl_platform {
  770. cl_platform_id id;
  771. unsigned number;
  772. char name[128];
  773. char vendor[128];
  774. struct cl_device * devices;
  775. unsigned n_devices;
  776. struct cl_device * default_device;
  777. };
  778. struct cl_device {
  779. struct cl_platform * platform;
  780. cl_device_id id;
  781. unsigned number;
  782. cl_device_type type;
  783. char name[128];
  784. };
  785. enum { NPLAT = 16, NDEV = 16 };
  786. struct cl_platform platforms[NPLAT];
  787. unsigned n_platforms = 0;
  788. struct cl_device devices[NDEV];
  789. unsigned n_devices = 0;
  790. struct cl_device * default_device = NULL;
  791. platform = NULL;
  792. device = NULL;
  793. cl_platform_id platform_ids[NPLAT];
  794. CL_CHECK(clGetPlatformIDs(NPLAT, platform_ids, &n_platforms));
  795. for (unsigned i = 0; i < n_platforms; i++) {
  796. struct cl_platform * p = &platforms[i];
  797. p->number = i;
  798. p->id = platform_ids[i];
  799. CL_CHECK(clGetPlatformInfo(p->id, CL_PLATFORM_NAME, sizeof(p->name), &p->name, NULL));
  800. CL_CHECK(clGetPlatformInfo(p->id, CL_PLATFORM_VENDOR, sizeof(p->vendor), &p->vendor, NULL));
  801. cl_device_id device_ids[NDEV];
  802. cl_int clGetDeviceIDsError = clGetDeviceIDs(p->id, CL_DEVICE_TYPE_ALL, NDEV, device_ids, &p->n_devices);
  803. if (clGetDeviceIDsError == CL_DEVICE_NOT_FOUND) {
  804. p->n_devices = 0;
  805. } else {
  806. CL_CHECK(clGetDeviceIDsError);
  807. }
  808. p->devices = p->n_devices > 0 ? &devices[n_devices] : NULL;
  809. p->default_device = NULL;
  810. for (unsigned j = 0; j < p->n_devices; j++) {
  811. struct cl_device * d = &devices[n_devices];
  812. d->number = n_devices++;
  813. d->id = device_ids[j];
  814. d->platform = p;
  815. CL_CHECK(clGetDeviceInfo(d->id, CL_DEVICE_NAME, sizeof(d->name), &d->name, NULL));
  816. CL_CHECK(clGetDeviceInfo(d->id, CL_DEVICE_TYPE, sizeof(d->type), &d->type, NULL));
  817. if (p->default_device == NULL && d->type == CL_DEVICE_TYPE_GPU) {
  818. p->default_device = d;
  819. }
  820. }
  821. if (default_device == NULL && p->default_device != NULL) {
  822. default_device = p->default_device;
  823. }
  824. }
  825. if (n_devices == 0) {
  826. fprintf(stderr, "ggml_opencl: could find any OpenCL devices.\n");
  827. exit(1);
  828. }
  829. char * user_platform_string = getenv("GGML_OPENCL_PLATFORM");
  830. char * user_device_string = getenv("GGML_OPENCL_DEVICE");
  831. int user_platform_number = -1;
  832. int user_device_number = -1;
  833. unsigned n;
  834. if (user_platform_string != NULL && sscanf(user_platform_string, " %u", &n) == 1 && n < n_platforms) {
  835. user_platform_number = (int)n;
  836. }
  837. if (user_device_string != NULL && sscanf(user_device_string, " %u", &n) == 1 && n < n_devices) {
  838. user_device_number = (int)n;
  839. }
  840. if (user_platform_number != -1 && user_device_number != -1) {
  841. cl_platform* platform = &platforms[user_platform_number];
  842. if ((unsigned)user_device_number >= platform->n_devices) {
  843. fprintf(stderr, "ggml_opencl: invalid device number %d\n", user_device_number);
  844. exit(1);
  845. }
  846. default_device = &platform->devices[user_device_number];
  847. } else {
  848. struct cl_device * selected_devices = devices;
  849. unsigned n_selected_devices = n_devices;
  850. if (user_platform_number == -1 && user_platform_string != NULL && user_platform_string[0] != 0) {
  851. for (unsigned i = 0; i < n_platforms; i++) {
  852. struct cl_platform * p = &platforms[i];
  853. if (strstr(p->name, user_platform_string) != NULL ||
  854. strstr(p->vendor, user_platform_string) != NULL) {
  855. user_platform_number = (int)i;
  856. break;
  857. }
  858. }
  859. if (user_platform_number == -1) {
  860. fprintf(stderr, "ggml_opencl: no platform matching '%s' was found.\n", user_platform_string);
  861. exit(1);
  862. }
  863. }
  864. if (user_platform_number != -1) {
  865. struct cl_platform * p = &platforms[user_platform_number];
  866. selected_devices = p->devices;
  867. n_selected_devices = p->n_devices;
  868. default_device = p->default_device;
  869. if (n_selected_devices == 0) {
  870. fprintf(stderr, "ggml_opencl: selected platform '%s' does not have any devices.\n", p->name);
  871. exit(1);
  872. }
  873. }
  874. if (user_device_number == -1 && user_device_string != NULL && user_device_string[0] != 0) {
  875. for (unsigned i = 0; i < n_selected_devices; i++) {
  876. struct cl_device * d = &selected_devices[i];
  877. if (strstr(d->name, user_device_string) != NULL) {
  878. user_device_number = d->number;
  879. break;
  880. }
  881. }
  882. if (user_device_number == -1) {
  883. fprintf(stderr, "ggml_opencl: no device matching '%s' was found.\n", user_device_string);
  884. exit(1);
  885. }
  886. }
  887. if (user_device_number != -1) {
  888. selected_devices = &devices[user_device_number];
  889. n_selected_devices = 1;
  890. default_device = &selected_devices[0];
  891. }
  892. GGML_ASSERT(n_selected_devices > 0);
  893. if (default_device == NULL) {
  894. default_device = &selected_devices[0];
  895. }
  896. }
  897. fprintf(stderr, "ggml_opencl: selecting platform: '%s'\n", default_device->platform->name);
  898. fprintf(stderr, "ggml_opencl: selecting device: '%s'\n", default_device->name);
  899. if (default_device->type != CL_DEVICE_TYPE_GPU) {
  900. fprintf(stderr, "ggml_opencl: warning, not a GPU: '%s'.\n", default_device->name);
  901. }
  902. platform = default_device->platform->id;
  903. device = default_device->id;
  904. size_t ext_str_size;
  905. clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, NULL, &ext_str_size);
  906. char *ext_buffer = (char *)alloca(ext_str_size + 1);
  907. clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, ext_str_size, ext_buffer, NULL);
  908. ext_buffer[ext_str_size] = '\0'; // ensure it is null terminated
  909. // Check if ext_buffer contains cl_khr_fp16
  910. fp16_support = strstr(ext_buffer, "cl_khr_fp16") != NULL;
  911. fprintf(stderr, "ggml_opencl: device FP16 support: %s\n", fp16_support ? "true" : "false");
  912. cl_context_properties properties[] = {
  913. (intptr_t)CL_CONTEXT_PLATFORM, (intptr_t)platform, 0
  914. };
  915. CL_CHECK((context = clCreateContext(properties, 1, &device, NULL, NULL, &err), err));
  916. CL_CHECK((queue = clCreateCommandQueue(context, device, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err),
  917. (err != CL_INVALID_QUEUE_PROPERTIES && err != CL_INVALID_VALUE ? err :
  918. (queue = clCreateCommandQueue(context, device, 0, &err), err)
  919. )));
  920. const std::string kernel_src = generate_kernels();
  921. program = build_program_from_source(context, device, kernel_src.c_str());
  922. // FP16 to FP32 kernel
  923. CL_CHECK((convert_row_f16_cl = clCreateKernel(program, "convert_row_f16", &err), err));
  924. // Dequantize kernels
  925. CL_CHECK((dequantize_row_q4_0_cl = clCreateKernel(program, "dequantize_row_q4_0", &err), err));
  926. CL_CHECK((dequantize_row_q4_1_cl = clCreateKernel(program, "dequantize_row_q4_1", &err), err));
  927. CL_CHECK((dequantize_row_q5_0_cl = clCreateKernel(program, "dequantize_row_q5_0", &err), err));
  928. CL_CHECK((dequantize_row_q5_1_cl = clCreateKernel(program, "dequantize_row_q5_1", &err), err));
  929. CL_CHECK((dequantize_row_q8_0_cl = clCreateKernel(program, "dequantize_row_q8_0", &err), err));
  930. CL_CHECK((dequantize_row_q8_0_cl = clCreateKernel(program, "dequantize_row_q8_0", &err), err));
  931. CL_CHECK((dequantize_block_q2_k_cl = clCreateKernel(program, "dequantize_block_q2_K", &err), err));
  932. CL_CHECK((dequantize_block_q3_k_cl = clCreateKernel(program, "dequantize_block_q3_K", &err), err));
  933. CL_CHECK((dequantize_block_q4_k_cl = clCreateKernel(program, "dequantize_block_q4_K", &err), err));
  934. CL_CHECK((dequantize_block_q5_k_cl = clCreateKernel(program, "dequantize_block_q5_K", &err), err));
  935. CL_CHECK((dequantize_block_q6_k_cl = clCreateKernel(program, "dequantize_block_q6_K", &err), err));
  936. // dequant mul mat kernel
  937. CL_CHECK((dequantize_mul_mat_vec_q4_0_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q4_0", &err), err));
  938. CL_CHECK((dequantize_mul_mat_vec_q4_1_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q4_1", &err), err));
  939. CL_CHECK((dequantize_mul_mat_vec_q5_0_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q5_0", &err), err));
  940. CL_CHECK((dequantize_mul_mat_vec_q5_1_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q5_1", &err), err));
  941. CL_CHECK((dequantize_mul_mat_vec_q8_0_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q8_0", &err), err));
  942. CL_CHECK((convert_mul_mat_vec_f16_cl = clCreateKernel(program, "convert_mul_mat_vec_f16", &err), err));
  943. CL_CHECK((dequantize_mul_mat_vec_q2_K_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q2_K", &err), err));
  944. CL_CHECK((dequantize_mul_mat_vec_q3_K_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q3_K", &err), err));
  945. CL_CHECK((dequantize_mul_mat_vec_q4_K_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q4_K", &err), err));
  946. CL_CHECK((dequantize_mul_mat_vec_q5_K_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q5_K", &err), err));
  947. CL_CHECK((dequantize_mul_mat_vec_q6_K_cl = clCreateKernel(program, "dequantize_mul_mat_vec_q6_K", &err), err));
  948. // mul kernel
  949. CL_CHECK((mul_f32_cl = clCreateKernel(program, "mul_f32", &err), err));
  950. }
  951. static cl_kernel* ggml_get_to_fp32_cl(ggml_type type) {
  952. switch (type) {
  953. case GGML_TYPE_Q4_0:
  954. return &dequantize_row_q4_0_cl;
  955. case GGML_TYPE_Q4_1:
  956. return &dequantize_row_q4_1_cl;
  957. case GGML_TYPE_Q5_0:
  958. return &dequantize_row_q5_0_cl;
  959. case GGML_TYPE_Q5_1:
  960. return &dequantize_row_q5_1_cl;
  961. case GGML_TYPE_Q8_0:
  962. return &dequantize_row_q8_0_cl;
  963. case GGML_TYPE_Q2_K:
  964. return &dequantize_block_q2_k_cl;
  965. case GGML_TYPE_Q3_K:
  966. return &dequantize_block_q3_k_cl;
  967. case GGML_TYPE_Q4_K:
  968. return &dequantize_block_q4_k_cl;
  969. case GGML_TYPE_Q5_K:
  970. return &dequantize_block_q5_k_cl;
  971. case GGML_TYPE_Q6_K:
  972. return &dequantize_block_q6_k_cl;
  973. case GGML_TYPE_F16:
  974. return &convert_row_f16_cl;
  975. default:
  976. return nullptr;
  977. }
  978. }
  979. static size_t ggml_cl_global_denom(ggml_type type) {
  980. switch (type) {
  981. case GGML_TYPE_Q4_0:
  982. case GGML_TYPE_Q4_1:
  983. case GGML_TYPE_Q5_0:
  984. case GGML_TYPE_Q5_1:
  985. case GGML_TYPE_Q8_0:
  986. return 1;
  987. case GGML_TYPE_Q2_K:
  988. case GGML_TYPE_Q3_K:
  989. return 4;
  990. case GGML_TYPE_Q4_K:
  991. return 8;
  992. case GGML_TYPE_Q5_K:
  993. case GGML_TYPE_Q6_K:
  994. return 4;
  995. case GGML_TYPE_F16:
  996. default:
  997. return 1;
  998. }
  999. }
  1000. static size_t ggml_cl_local_size(ggml_type type) {
  1001. switch (type) {
  1002. case GGML_TYPE_Q4_0:
  1003. case GGML_TYPE_Q4_1:
  1004. case GGML_TYPE_Q5_0:
  1005. case GGML_TYPE_Q5_1:
  1006. case GGML_TYPE_Q8_0:
  1007. return 0;
  1008. case GGML_TYPE_Q2_K:
  1009. case GGML_TYPE_Q3_K:
  1010. return 64;
  1011. case GGML_TYPE_Q4_K:
  1012. return 32;
  1013. case GGML_TYPE_Q5_K:
  1014. case GGML_TYPE_Q6_K:
  1015. return 64;
  1016. case GGML_TYPE_F16:
  1017. default:
  1018. return 0;
  1019. }
  1020. }
  1021. static cl_kernel* ggml_get_dequantize_mul_mat_vec_cl(ggml_type type) {
  1022. switch (type) {
  1023. case GGML_TYPE_Q4_0:
  1024. return &dequantize_mul_mat_vec_q4_0_cl;
  1025. case GGML_TYPE_Q4_1:
  1026. return &dequantize_mul_mat_vec_q4_1_cl;
  1027. case GGML_TYPE_Q5_0:
  1028. return &dequantize_mul_mat_vec_q5_0_cl;
  1029. case GGML_TYPE_Q5_1:
  1030. return &dequantize_mul_mat_vec_q5_1_cl;
  1031. case GGML_TYPE_Q8_0:
  1032. return &dequantize_mul_mat_vec_q8_0_cl;
  1033. case GGML_TYPE_F16:
  1034. return &convert_mul_mat_vec_f16_cl;
  1035. case GGML_TYPE_Q2_K:
  1036. return &dequantize_mul_mat_vec_q2_K_cl;
  1037. case GGML_TYPE_Q3_K:
  1038. return &dequantize_mul_mat_vec_q3_K_cl;
  1039. case GGML_TYPE_Q4_K:
  1040. return &dequantize_mul_mat_vec_q4_K_cl;
  1041. case GGML_TYPE_Q5_K:
  1042. return &dequantize_mul_mat_vec_q5_K_cl;
  1043. case GGML_TYPE_Q6_K:
  1044. return &dequantize_mul_mat_vec_q6_K_cl;
  1045. default:
  1046. return nullptr;
  1047. }
  1048. }
  1049. // buffer pool for cl
  1050. #define MAX_CL_BUFFERS 256
  1051. struct scoped_spin_lock {
  1052. std::atomic_flag& lock;
  1053. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  1054. while (lock.test_and_set(std::memory_order_acquire)) {
  1055. ; // spin
  1056. }
  1057. }
  1058. ~scoped_spin_lock() {
  1059. lock.clear(std::memory_order_release);
  1060. }
  1061. scoped_spin_lock(const scoped_spin_lock&) = delete;
  1062. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  1063. };
  1064. struct cl_buffer {
  1065. cl_mem mem;
  1066. size_t size = 0;
  1067. };
  1068. static cl_buffer g_cl_buffer_pool[MAX_CL_BUFFERS];
  1069. static std::atomic_flag g_cl_pool_lock = ATOMIC_FLAG_INIT;
  1070. static cl_mem ggml_cl_pool_malloc(size_t size, size_t * actual_size) {
  1071. scoped_spin_lock lock(g_cl_pool_lock);
  1072. cl_int err;
  1073. int best_i = -1;
  1074. size_t best_size = std::numeric_limits<size_t>::max(); //smallest unused buffer that fits our needs
  1075. int worst_i = -1;
  1076. size_t worst_size = 0; //largest unused buffer seen so far
  1077. for (int i = 0; i < MAX_CL_BUFFERS; ++i) {
  1078. cl_buffer &b = g_cl_buffer_pool[i];
  1079. if (b.size > 0 && b.size >= size && b.size < best_size)
  1080. {
  1081. best_i = i;
  1082. best_size = b.size;
  1083. }
  1084. if (b.size > 0 && b.size > worst_size)
  1085. {
  1086. worst_i = i;
  1087. worst_size = b.size;
  1088. }
  1089. }
  1090. if(best_i!=-1) //found the smallest buffer that fits our needs
  1091. {
  1092. cl_buffer& b = g_cl_buffer_pool[best_i];
  1093. cl_mem mem = b.mem;
  1094. *actual_size = b.size;
  1095. b.size = 0;
  1096. return mem;
  1097. }
  1098. if(worst_i!=-1) //no buffer that fits our needs, resize largest one to save memory
  1099. {
  1100. cl_buffer& b = g_cl_buffer_pool[worst_i];
  1101. cl_mem mem = b.mem;
  1102. b.size = 0;
  1103. clReleaseMemObject(mem);
  1104. }
  1105. cl_mem mem;
  1106. CL_CHECK((mem = clCreateBuffer(context, CL_MEM_READ_WRITE, size, NULL, &err), err));
  1107. *actual_size = size;
  1108. return mem;
  1109. }
  1110. static void ggml_cl_pool_free(cl_mem mem, size_t size) {
  1111. scoped_spin_lock lock(g_cl_pool_lock);
  1112. for (int i = 0; i < MAX_CL_BUFFERS; ++i) {
  1113. cl_buffer& b = g_cl_buffer_pool[i];
  1114. if (b.size == 0) {
  1115. b.mem = mem;
  1116. b.size = size;
  1117. return;
  1118. }
  1119. }
  1120. fprintf(stderr, "WARNING: cl buffer pool full, increase MAX_CL_BUFFERS\n");
  1121. clReleaseMemObject(mem);
  1122. }
  1123. void ggml_cl_free_data(const struct ggml_tensor* tensor) {
  1124. if (tensor->backend != GGML_BACKEND_GPU) {
  1125. return;
  1126. }
  1127. cl_mem mem = (cl_mem)tensor->extra;
  1128. clReleaseMemObject(mem);
  1129. }
  1130. static cl_int ggml_cl_h2d_tensor_2d(cl_command_queue queue, cl_mem dst, size_t offset, const struct ggml_tensor * src, uint64_t i3, uint64_t i2, cl_event* ev) {
  1131. cl_int err;
  1132. const uint64_t ne0 = src->ne[0];
  1133. const uint64_t ne1 = src->ne[1];
  1134. const uint64_t nb0 = src->nb[0];
  1135. const uint64_t nb1 = src->nb[1];
  1136. const uint64_t nb2 = src->nb[2];
  1137. const uint64_t nb3 = src->nb[3];
  1138. const enum ggml_type type = src->type;
  1139. const size_t ts = ggml_type_size(type);
  1140. const size_t bs = ggml_blck_size(type);
  1141. const uint64_t row_size = ts*ne0/bs;
  1142. const char * x = (const char *) src->data + i2*nb2 + i3*nb3;
  1143. if (nb0 == ts && nb1 == row_size) {
  1144. return clEnqueueWriteBuffer(queue, dst, CL_FALSE, offset, ne1*row_size, x, 0, NULL, ev);
  1145. }
  1146. if (nb0 == ts) {
  1147. const size_t buffer_origin[3] = { offset, 0, 0 };
  1148. const size_t host_origin[3] = { 0, 0, 0 };
  1149. const size_t region[3] = { row_size, ne1, 1 };
  1150. return clEnqueueWriteBufferRect(queue, dst, CL_FALSE, buffer_origin, host_origin, region, row_size, 0, nb1, 0, x, 0, NULL, ev);
  1151. }
  1152. std::vector<cl_event> events;
  1153. if (ev && ne1>1) events.reserve(ne1-1);
  1154. for (uint64_t i1 = 0; i1 < ne1; i1++) {
  1155. // pretend the row is a matrix with cols=1
  1156. const size_t buffer_origin[3] = { offset + i1*row_size, 0, 0 };
  1157. const size_t host_origin[3] = { 0, 0, 0 };
  1158. const size_t region[3] = { ts, ne0/bs, 1 };
  1159. // if an event is requested, make the last write wait for all previous writes to complete
  1160. if (ev && i1) {
  1161. events.push_back(*ev);
  1162. }
  1163. cl_uint nevents = i1 == ne1-1 ? events.size() : 0U;
  1164. err = clEnqueueWriteBufferRect(queue, dst, CL_FALSE, buffer_origin, host_origin, region, ts, 0, nb0, 0, x + i1*nb1, nevents, nevents ? events.data() : nullptr, ev);
  1165. if (err != CL_SUCCESS) {
  1166. for (auto event : events) {
  1167. clReleaseEvent(event);
  1168. }
  1169. return err;
  1170. }
  1171. }
  1172. for (auto event : events) {
  1173. CL_CHECK(clReleaseEvent(event));
  1174. }
  1175. return CL_SUCCESS;
  1176. }
  1177. static void ggml_cl_mul_f32(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  1178. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  1179. const int64_t ne00 = src0->ne[0];
  1180. const int64_t ne01 = src0->ne[1];
  1181. const int64_t ne02 = src0->ne[2];
  1182. const int64_t ne03 = src0->ne[3];
  1183. const int64_t ne10 = src1->ne[0];
  1184. const int64_t ne11 = src1->ne[1];
  1185. const int64_t ne12 = src1->ne[2];
  1186. const int64_t ne13 = src1->ne[3];
  1187. const int nb2 = dst->nb[2];
  1188. const int nb3 = dst->nb[3];
  1189. size_t x_size;
  1190. size_t d_size;
  1191. cl_mem d_X = ggml_cl_pool_malloc(ne00 * ne01 * sizeof(float), &x_size); // src0
  1192. cl_mem d_Y = (cl_mem) src1->extra; // src1 is already on device, broadcasted.
  1193. cl_mem d_D = ggml_cl_pool_malloc(ne00 * ne01 * sizeof(float), &d_size); // dst
  1194. for (int64_t i03 = 0; i03 < ne03; i03++) {
  1195. for (int64_t i02 = 0; i02 < ne02; i02++) {
  1196. cl_event ev;
  1197. // copy src0 to device
  1198. CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_X, 0, src0, i03, i02, &ev));
  1199. const int64_t i13 = i03%ne13;
  1200. const int64_t i12 = i02%ne12;
  1201. const int i1 = i13*ne12*ne11 + i12*ne11;
  1202. cl_int x_offset = 0;
  1203. cl_int y_offset = i1*ne10;
  1204. cl_int d_offset = 0;
  1205. size_t global = ne00 * ne01;
  1206. cl_int ky = ne10 * ne11;
  1207. CL_CHECK(clSetKernelArg(mul_f32_cl, 0, sizeof(cl_mem), &d_X));
  1208. CL_CHECK(clSetKernelArg(mul_f32_cl, 1, sizeof(cl_int), &x_offset));
  1209. CL_CHECK(clSetKernelArg(mul_f32_cl, 2, sizeof(cl_mem), &d_Y));
  1210. CL_CHECK(clSetKernelArg(mul_f32_cl, 3, sizeof(cl_int), &y_offset));
  1211. CL_CHECK(clSetKernelArg(mul_f32_cl, 4, sizeof(cl_mem), &d_D));
  1212. CL_CHECK(clSetKernelArg(mul_f32_cl, 5, sizeof(cl_int), &d_offset));
  1213. CL_CHECK(clSetKernelArg(mul_f32_cl, 6, sizeof(cl_int), &ky));
  1214. CL_CHECK(clEnqueueNDRangeKernel(queue, mul_f32_cl, 1, NULL, &global, NULL, 1, &ev, NULL));
  1215. CL_CHECK(clReleaseEvent(ev));
  1216. CL_CHECK(clFinish(queue));
  1217. // copy dst to host
  1218. float * d = (float *) ((char *) dst->data + i02*nb2 + i03*nb3);
  1219. CL_CHECK(clEnqueueReadBuffer(queue, d_D, true, 0, sizeof(float) * ne00*ne01, d, 0, NULL, NULL));
  1220. }
  1221. }
  1222. ggml_cl_pool_free(d_X, x_size);
  1223. ggml_cl_pool_free(d_D, d_size);
  1224. }
  1225. void ggml_cl_mul(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  1226. GGML_ASSERT(src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32);
  1227. ggml_cl_mul_f32(src0, src1, dst);
  1228. }
  1229. static void ggml_cl_mul_mat_f32(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  1230. const int64_t ne00 = src0->ne[0];
  1231. const int64_t ne01 = src0->ne[1];
  1232. const int64_t ne02 = src0->ne[2];
  1233. const int64_t ne03 = src0->ne[3];
  1234. const int64_t ne10 = src1->ne[0];
  1235. const int64_t ne11 = src1->ne[1];
  1236. const int64_t ne12 = src1->ne[2];
  1237. const int64_t ne13 = src1->ne[3];
  1238. const int nb2 = dst->nb[2];
  1239. const int nb3 = dst->nb[3];
  1240. const int64_t r2 = ne12 / ne02;
  1241. const int64_t r3 = ne13 / ne03;
  1242. const float alpha = 1.0f;
  1243. const float beta = 0.0f;
  1244. const int x_ne = ne01 * ne00;
  1245. const int y_ne = ne11 * ne10;
  1246. const int d_ne = ne11 * ne01;
  1247. size_t x_size;
  1248. size_t y_size;
  1249. size_t d_size;
  1250. cl_mem d_X;
  1251. if (src0->backend == GGML_BACKEND_GPU) { // NOLINT
  1252. d_X = (cl_mem) src0->extra;
  1253. } else {
  1254. d_X = ggml_cl_pool_malloc(sizeof(float) * x_ne, &x_size);
  1255. }
  1256. cl_mem d_Y = ggml_cl_pool_malloc(sizeof(float) * y_ne, &y_size);
  1257. cl_mem d_D = ggml_cl_pool_malloc(sizeof(float) * d_ne, &d_size);
  1258. size_t x_offset = 0;
  1259. for (int64_t i03 = 0; i03 < ne03; i03++) {
  1260. // TODO: copy src0 here when r3>1
  1261. for (int64_t i13 = i03 * r3, e13 = i13 + r3; i13 < e13; i13++) {
  1262. for (int64_t i02 = 0; i02 < ne02; i02++) {
  1263. if (src0->backend == GGML_BACKEND_GPU) {
  1264. x_offset = (i03 * ne02 + i02) * x_ne;
  1265. } else {
  1266. // copy src0 to device
  1267. CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_X, 0, src0, i03, i02, NULL));
  1268. }
  1269. for (int64_t i12 = i02 * r2, e12 = i12 + r2; i12 < e12; i12++) {
  1270. // copy src1 to device
  1271. CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_Y, 0, src1, i13, i12, NULL));
  1272. CL_CHECK(clFinish(queue));
  1273. // compute
  1274. cl_event ev_sgemm;
  1275. clblast::StatusCode status = clblast::Gemm<cl_float>(clblast::Layout::kColMajor,
  1276. clblast::Transpose::kYes, clblast::Transpose::kNo,
  1277. ne01, ne11, ne10,
  1278. alpha,
  1279. d_X, x_offset, ne00,
  1280. d_Y, 0, ne10,
  1281. beta,
  1282. d_D, 0, ne01,
  1283. &queue, &ev_sgemm);
  1284. if (status != clblast::StatusCode::kSuccess) {
  1285. GGML_ASSERT(false);
  1286. }
  1287. // copy dst to host
  1288. float * d = (float *) ((char *) dst->data + i12*nb2 + i13*nb3);
  1289. CL_CHECK(clEnqueueReadBuffer(queue, d_D, true, 0, sizeof(float) * d_ne, d, 1, &ev_sgemm, NULL));
  1290. }
  1291. }
  1292. }
  1293. }
  1294. if (src0->backend != GGML_BACKEND_GPU) {
  1295. ggml_cl_pool_free(d_X, x_size);
  1296. }
  1297. ggml_cl_pool_free(d_Y, y_size);
  1298. ggml_cl_pool_free(d_D, d_size);
  1299. }
  1300. static void ggml_cl_mul_mat_f16(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, void * wdata, size_t wsize) {
  1301. GGML_ASSERT(fp16_support);
  1302. const int64_t ne00 = src0->ne[0];
  1303. const int64_t ne01 = src0->ne[1];
  1304. const int64_t ne02 = src0->ne[2];
  1305. const int64_t ne03 = src0->ne[3];
  1306. const int64_t ne10 = src1->ne[0];
  1307. const int64_t ne11 = src1->ne[1];
  1308. const int64_t ne12 = src1->ne[2];
  1309. const int64_t ne13 = src1->ne[3];
  1310. const int nb10 = src1->nb[0];
  1311. const int nb11 = src1->nb[1];
  1312. const int nb12 = src1->nb[2];
  1313. const int nb13 = src1->nb[3];
  1314. const int nb2 = dst->nb[2];
  1315. const int nb3 = dst->nb[3];
  1316. const int64_t r2 = ne12 / ne02;
  1317. const int64_t r3 = ne13 / ne03;
  1318. const ggml_fp16_t alpha = ggml_fp32_to_fp16(1.0f);
  1319. const ggml_fp16_t beta = ggml_fp32_to_fp16(0.0f);
  1320. const int x_ne = ne01 * ne00;
  1321. const int y_ne = ne11 * ne10;
  1322. const int d_ne = ne11 * ne01;
  1323. GGML_ASSERT(wsize >= sizeof(ggml_fp16_t) * y_ne);
  1324. GGML_ASSERT(wsize >= sizeof(ggml_fp16_t) * d_ne);
  1325. ggml_fp16_t * const tmp = (ggml_fp16_t *) wdata;
  1326. size_t x_size;
  1327. size_t y_size;
  1328. size_t d_size;
  1329. cl_mem d_X;
  1330. if (src0->backend == GGML_BACKEND_GPU) { // NOLINT
  1331. d_X = (cl_mem) src0->extra;
  1332. } else {
  1333. d_X = ggml_cl_pool_malloc(sizeof(ggml_fp16_t) * x_ne, &x_size);
  1334. }
  1335. cl_mem d_Y = ggml_cl_pool_malloc(sizeof(ggml_fp16_t) * y_ne, &y_size);
  1336. cl_mem d_D = ggml_cl_pool_malloc(sizeof(ggml_fp16_t) * d_ne, &d_size);
  1337. bool src1_cont_rows = nb10 == sizeof(float);
  1338. bool src1_cont_cols = (size_t)nb11 == ne11*sizeof(float);
  1339. size_t x_offset = 0;
  1340. for (int64_t i03 = 0; i03 < ne03; i03++) {
  1341. // TODO: copy src0 here when r3>1
  1342. for (int64_t i13 = i03 * r3, e13 = i13 + r3; i13 < e13; i13++) {
  1343. for (int64_t i02 = 0; i02 < ne02; i02++) {
  1344. if (src0->backend == GGML_BACKEND_GPU) {
  1345. x_offset = (i03 * ne02 + i02) * x_ne;
  1346. } else {
  1347. // copy src0 to device
  1348. CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_X, 0, src0, i03, i02, NULL));
  1349. }
  1350. for (int64_t i12 = i02 * r2, e12 = i12 + r2; i12 < e12; i12++) {
  1351. // convert src1 to fp16
  1352. // TODO: use multiple threads
  1353. char * src1i = (char *) src1->data + i13*nb13 + i12*nb12;
  1354. if (src1_cont_rows) {
  1355. if (src1_cont_cols) {
  1356. ggml_fp32_to_fp16_row((float *) src1i, tmp, ne10*ne11);
  1357. }
  1358. else {
  1359. for (int64_t i11 = 0; i11 < ne11; i11++) {
  1360. ggml_fp32_to_fp16_row((float *) (src1i + i11*nb11), tmp + i11*ne10, ne10);
  1361. }
  1362. }
  1363. }
  1364. else {
  1365. for (int64_t i11 = 0; i11 < ne11; i11++) {
  1366. for (int64_t i10 = 0; i10 < ne10; i10++) {
  1367. // very slow due to no inlining
  1368. tmp[i11*ne10 + i10] = ggml_fp32_to_fp16(*(float *) (src1i + i11*nb11 + i10*nb10));
  1369. }
  1370. }
  1371. }
  1372. // copy src1 to device
  1373. CL_CHECK(clEnqueueWriteBuffer(queue, d_Y, false, 0, sizeof(ggml_fp16_t) * y_ne, tmp, 0, NULL, NULL));
  1374. CL_CHECK(clFinish(queue));
  1375. // compute
  1376. cl_event ev_sgemm;
  1377. clblast::StatusCode status = clblast::Gemm<cl_half>(clblast::Layout::kColMajor,
  1378. clblast::Transpose::kYes, clblast::Transpose::kNo,
  1379. ne01, ne11, ne10,
  1380. alpha,
  1381. d_X, x_offset, ne00,
  1382. d_Y, 0, ne10,
  1383. beta,
  1384. d_D, 0, ne01,
  1385. &queue, &ev_sgemm);
  1386. if (status != clblast::StatusCode::kSuccess) {
  1387. GGML_ASSERT(false);
  1388. }
  1389. // copy dst to host, then convert to float
  1390. CL_CHECK(clEnqueueReadBuffer(queue, d_D, true, 0, sizeof(ggml_fp16_t) * d_ne, tmp, 1, &ev_sgemm, NULL));
  1391. float * d = (float *) ((char *) dst->data + i12*nb2 + i13*nb3);
  1392. ggml_fp16_to_fp32_row(tmp, d, d_ne);
  1393. }
  1394. }
  1395. }
  1396. }
  1397. if (src0->backend != GGML_BACKEND_GPU) {
  1398. ggml_cl_pool_free(d_X, x_size);
  1399. }
  1400. ggml_cl_pool_free(d_Y, y_size);
  1401. ggml_cl_pool_free(d_D, d_size);
  1402. }
  1403. static void ggml_cl_mul_mat_q_f32(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  1404. const int64_t ne00 = src0->ne[0];
  1405. const int64_t ne01 = src0->ne[1];
  1406. const int64_t ne02 = src0->ne[2];
  1407. const int64_t ne03 = src0->ne[3];
  1408. const int64_t ne10 = src1->ne[0];
  1409. const int64_t ne11 = src1->ne[1];
  1410. const int64_t ne12 = src1->ne[2];
  1411. const int64_t ne13 = src1->ne[3];
  1412. const int nb2 = dst->nb[2];
  1413. const int nb3 = dst->nb[3];
  1414. const ggml_type type = src0->type;
  1415. const bool mul_mat_vec = ne11 == 1 && ne00%2 == 0;
  1416. const int64_t r2 = ne12 / ne02;
  1417. const int64_t r3 = ne13 / ne03;
  1418. const float alpha = 1.0f;
  1419. const float beta = 0.0f;
  1420. const int x_ne = ne01 * ne00;
  1421. const int y_ne = ne11 * ne10;
  1422. const int d_ne = ne11 * ne01;
  1423. const int x_bps = x_ne / ggml_blck_size(type); // blocks per 2D slice
  1424. const size_t q_sz = ggml_type_size(type) * x_bps;
  1425. size_t x_size;
  1426. size_t y_size;
  1427. size_t d_size;
  1428. size_t q_size;
  1429. cl_mem d_X;
  1430. if (!mul_mat_vec) {
  1431. d_X = ggml_cl_pool_malloc(sizeof(float) * x_ne, &x_size);
  1432. }
  1433. cl_mem d_Y = ggml_cl_pool_malloc(sizeof(float) * y_ne, &y_size);
  1434. cl_mem d_D = ggml_cl_pool_malloc(sizeof(float) * d_ne, &d_size);
  1435. cl_mem d_Q;
  1436. if (src0->backend == GGML_BACKEND_CPU) {
  1437. d_Q = ggml_cl_pool_malloc(q_sz, &q_size);
  1438. }
  1439. cl_kernel* to_fp32_cl = ggml_get_to_fp32_cl(type);
  1440. cl_kernel* dmmv = ggml_get_dequantize_mul_mat_vec_cl(type);
  1441. GGML_ASSERT(to_fp32_cl != nullptr);
  1442. const size_t global_denom = ggml_cl_global_denom(type);
  1443. const size_t local = mul_mat_vec ? CL_DMMV_LOCAL_SIZE : ggml_cl_local_size(type);
  1444. size_t ev_idx = 0;
  1445. std::vector<cl_event> events;
  1446. for (int64_t i03 = 0; i03 < ne03; i03++) {
  1447. // TODO: copy and dequantize src0 here when r3>1
  1448. for (int64_t i13 = i03 * r3, e13 = i13 + r3; i13 < e13; i13++) {
  1449. for (int64_t i02 = 0; i02 < ne02; i02++) {
  1450. // copy src0 to device if necessary
  1451. if (src0->backend == GGML_BACKEND_CPU) {
  1452. events.emplace_back();
  1453. CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_Q, 0, src0, i03, i02, events.data() + ev_idx++));
  1454. } else if (src0->backend == GGML_BACKEND_GPU) {
  1455. d_Q = (cl_mem) src0->extra;
  1456. } else {
  1457. GGML_ASSERT(false);
  1458. }
  1459. if (!mul_mat_vec) {
  1460. // convert src0 to fp32 on device
  1461. const size_t global = x_ne / global_denom;
  1462. const size_t offset = src0->backend == GGML_BACKEND_GPU ? (i03 * ne02 + i02) * x_bps : 0;
  1463. CL_CHECK(clSetKernelArg(*to_fp32_cl, 0, sizeof(cl_mem), &d_Q));
  1464. CL_CHECK(clSetKernelArg(*to_fp32_cl, 1, sizeof(cl_mem), &d_X));
  1465. CL_CHECK(clEnqueueNDRangeKernel(queue, *to_fp32_cl, 1, &offset, &global, local > 0 ? &local : NULL, events.size(), !events.empty() ? events.data() : NULL, NULL));
  1466. }
  1467. for (int64_t i12 = i02 * r2, e12 = i12 + r2; i12 < e12; i12++) {
  1468. if (mul_mat_vec) { // specialized dequantize_mul_mat_vec kernel
  1469. // copy src1 to device
  1470. events.emplace_back();
  1471. CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_Y, 0, src1, i13, i12, events.data() + ev_idx++));
  1472. // compute
  1473. const size_t global = ne01 * local;
  1474. const size_t offset = src0->backend == GGML_BACKEND_GPU ? (i03 * ne02 + i02) * x_bps : 0;
  1475. const cl_int ncols = ne00;
  1476. events.emplace_back();
  1477. CL_CHECK(clSetKernelArg(*dmmv, 0, sizeof(cl_mem), &d_Q));
  1478. CL_CHECK(clSetKernelArg(*dmmv, 1, sizeof(float) * local, NULL));
  1479. CL_CHECK(clSetKernelArg(*dmmv, 2, sizeof(cl_mem), &d_Y));
  1480. CL_CHECK(clSetKernelArg(*dmmv, 3, sizeof(cl_mem), &d_D));
  1481. CL_CHECK(clSetKernelArg(*dmmv, 4, sizeof(cl_int), &ncols));
  1482. CL_CHECK(clEnqueueNDRangeKernel(queue, *dmmv, 1, &offset, &global, &local, events.size() - 1, events.data(), events.data() + ev_idx++));
  1483. } else { // CLBlast matrix matrix multiplication
  1484. // copy src1 to device
  1485. CL_CHECK(ggml_cl_h2d_tensor_2d(queue, d_Y, 0, src1, i13, i12, NULL));
  1486. // wait for conversion
  1487. CL_CHECK(clFinish(queue));
  1488. // compute
  1489. events.emplace_back();
  1490. clblast::StatusCode status = clblast::Gemm<cl_float>(clblast::Layout::kColMajor,
  1491. clblast::Transpose::kYes, clblast::Transpose::kNo,
  1492. ne01, ne11, ne10,
  1493. alpha,
  1494. d_X, 0, ne00,
  1495. d_Y, 0, ne10,
  1496. beta,
  1497. d_D, 0, ne01,
  1498. &queue, events.data() + ev_idx++);
  1499. if (status != clblast::StatusCode::kSuccess) {
  1500. GGML_ASSERT(false);
  1501. }
  1502. }
  1503. // copy dst to host
  1504. float * d = (float *) ((char *) dst->data + i12*nb2 + i13*nb3);
  1505. CL_CHECK(clEnqueueReadBuffer(queue, d_D, true, 0, sizeof(float) * d_ne, d, 1, &events[events.size() - 1], NULL));
  1506. for (auto *event : events) {
  1507. clReleaseEvent(event);
  1508. }
  1509. ev_idx = 0;
  1510. events.clear();
  1511. }
  1512. }
  1513. }
  1514. }
  1515. if (!mul_mat_vec) {
  1516. ggml_cl_pool_free(d_X, x_size);
  1517. }
  1518. ggml_cl_pool_free(d_Y, y_size);
  1519. ggml_cl_pool_free(d_D, d_size);
  1520. if (src0->backend == GGML_BACKEND_CPU) {
  1521. ggml_cl_pool_free(d_Q, q_size);
  1522. }
  1523. }
  1524. bool ggml_cl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  1525. const int64_t ne10 = src1->ne[0];
  1526. const int64_t ne0 = dst->ne[0];
  1527. const int64_t ne1 = dst->ne[1];
  1528. // TODO: find the optimal values for these
  1529. if ((src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  1530. src1->type == GGML_TYPE_F32 &&
  1531. dst->type == GGML_TYPE_F32 &&
  1532. ((ne0 >= 32 && ne1 >= 32 && ne10 >= 32) || src0->backend == GGML_BACKEND_GPU)) {
  1533. return true;
  1534. }
  1535. return false;
  1536. }
  1537. static bool ggml_cl_mul_mat_use_f16(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * /* dst */) {
  1538. // If device doesn't support FP16
  1539. if (!fp16_support) {
  1540. return false;
  1541. }
  1542. size_t src0_sz = ggml_nbytes(src0);
  1543. size_t src1_sz = ggml_nbytes(src1);
  1544. // mul_mat_q: src0 is converted to fp32 on device
  1545. size_t mul_mat_q_transfer = src0_sz + src1_sz;
  1546. // mul_mat_f16: src1 is converted to fp16 on cpu
  1547. size_t mul_mat_f16_transfer = src0_sz + sizeof(ggml_fp16_t) * ggml_nelements(src1);
  1548. // choose the smaller one to transfer to the device
  1549. // TODO: this is not always the best choice due to the overhead of converting to fp16
  1550. return mul_mat_f16_transfer < mul_mat_q_transfer;
  1551. }
  1552. void ggml_cl_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst, void * wdata, size_t wsize) {
  1553. GGML_ASSERT(ggml_cl_can_mul_mat(src0, src1, dst));
  1554. if (src0->type == GGML_TYPE_F32) {
  1555. ggml_cl_mul_mat_f32(src0, src1, dst);
  1556. }
  1557. else if (src0->type == GGML_TYPE_F16) {
  1558. if (ggml_cl_mul_mat_use_f16(src0, src1, dst)) {
  1559. ggml_cl_mul_mat_f16(src0, src1, dst, wdata, wsize);
  1560. }
  1561. else {
  1562. ggml_cl_mul_mat_q_f32(src0, src1, dst);
  1563. }
  1564. }
  1565. else if (ggml_is_quantized(src0->type)) {
  1566. ggml_cl_mul_mat_q_f32(src0, src1, dst);
  1567. }
  1568. else {
  1569. GGML_ASSERT(false);
  1570. }
  1571. }
  1572. size_t ggml_cl_mul_mat_get_wsize(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  1573. if (src0->type == GGML_TYPE_F16 && ggml_cl_mul_mat_use_f16(src0, src1, dst)) {
  1574. return sizeof(ggml_fp16_t) * std::max(src1->ne[0] * src1->ne[1], dst->ne[0] * dst->ne[1]);
  1575. }
  1576. return 0;
  1577. }
  1578. void ggml_cl_transform_tensor(void * data, ggml_tensor * tensor) {
  1579. const int64_t ne0 = tensor->ne[0];
  1580. const int64_t ne1 = tensor->ne[1];
  1581. const int64_t ne2 = tensor->ne[2];
  1582. const int64_t ne3 = tensor->ne[3];
  1583. const ggml_type type = tensor->type;
  1584. const size_t s_sz = ggml_type_size(type) * (size_t) (ne0 * ne1 / ggml_blck_size(type));
  1585. const size_t q_sz = s_sz * (size_t) (ne2 * ne3);
  1586. size_t q_size;
  1587. cl_mem dst = ggml_cl_pool_malloc(q_sz, &q_size);
  1588. tensor->data = data;
  1589. // copy tensor to device
  1590. size_t offset = 0;
  1591. for (int64_t i3 = 0; i3 < ne3; i3++) {
  1592. for (int64_t i2 = 0; i2 < ne2; i2++) {
  1593. CL_CHECK(ggml_cl_h2d_tensor_2d(queue, dst, offset, tensor, i3, i2, NULL));
  1594. offset += s_sz;
  1595. }
  1596. }
  1597. CL_CHECK(clFinish(queue));
  1598. tensor->extra = dst;
  1599. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  1600. }