test-backend-ops.cpp 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638
  1. #include <ggml.h>
  2. #include <ggml-alloc.h>
  3. #include <ggml-backend.h>
  4. #include <algorithm>
  5. #include <array>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <functional>
  9. #include <memory>
  10. #include <random>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string>
  14. #include <thread>
  15. #include <vector>
  16. static void init_tensor_uniform(ggml_tensor * tensor, float min = -1.0f, float max = 1.0f) {
  17. // static RNG initialization (revisit if n_threads stops being constant)
  18. static const size_t n_threads = std::thread::hardware_concurrency();
  19. static std::vector<std::default_random_engine> generators = []() {
  20. std::random_device rd;
  21. std::vector<std::default_random_engine> vec;
  22. vec.reserve(n_threads);
  23. //for (size_t i = 0; i < n_threads; i++) { vec.emplace_back(1234 + i); } // fixed seed
  24. for (size_t i = 0; i < n_threads; i++) { vec.emplace_back(rd()); }
  25. return vec;
  26. }();
  27. size_t size = ggml_nelements(tensor);
  28. std::vector<float> data(size);
  29. auto init_thread = [&](size_t ith, size_t start, size_t end) {
  30. std::uniform_real_distribution<float> distribution(min, max);
  31. for (size_t i = start; i < end; i++) {
  32. data[i] = distribution(generators[ith]);
  33. }
  34. };
  35. std::vector<std::thread> threads;
  36. threads.reserve(n_threads);
  37. for (size_t i = 0; i < n_threads; i++) {
  38. size_t start = i*size/n_threads;
  39. size_t end = (i+1)*size/n_threads;
  40. threads.emplace_back(init_thread, i, start, end);
  41. }
  42. for (auto & t : threads) {
  43. t.join();
  44. }
  45. #if 0
  46. const char * val_str = getenv("GGML_TEST_EPS");
  47. float val = 1e-9f;
  48. if (val_str != nullptr) {
  49. val = std::stof(val_str);
  50. printf("GGML_TEST_EPS=%e\n", val);
  51. }
  52. // test quantization with very small values that may result in nan scales due to division by zero
  53. if (ggml_is_quantized(tensor->type)) {
  54. for (int i = 0; i < 256; i++) {
  55. data[i] = val;
  56. }
  57. }
  58. #endif
  59. if (tensor->type == GGML_TYPE_F32 || tensor->type == GGML_TYPE_I32) {
  60. ggml_backend_tensor_set(tensor, data.data(), 0, size * sizeof(float));
  61. } else if (ggml_is_quantized(tensor->type) || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_BF16) {
  62. GGML_ASSERT(size % ggml_blck_size(tensor->type) == 0);
  63. std::vector<uint8_t> dataq(ggml_row_size(tensor->type, size));
  64. std::vector<float> imatrix(tensor->ne[0], 1.0f); // dummy importance matrix
  65. const float * im = imatrix.data();
  66. if (!ggml_quantize_requires_imatrix(tensor->type)) {
  67. // when the imatrix is optional, we want to test both quantization with and without imatrix
  68. // use one of the random numbers to decide
  69. if (data[0] > 0.5f*(min + max)) {
  70. im = nullptr;
  71. }
  72. }
  73. ggml_quantize_chunk(tensor->type, data.data(), dataq.data(), 0, size/tensor->ne[0], tensor->ne[0], im);
  74. GGML_ASSERT(ggml_validate_row_data(tensor->type, dataq.data(), dataq.size()));
  75. // TODO: other cases
  76. //#pragma omp parallel for
  77. //for (int i = 0; i < tensor->ne[1]; i++) {
  78. // ggml_quantize_chunk(tensor->type, data.data(), dataq.data(),
  79. // i * tensor->ne[0], 1, tensor->ne[0], im);
  80. //}
  81. ggml_backend_tensor_set(tensor, dataq.data(), 0, dataq.size());
  82. } else if (tensor->type == GGML_TYPE_I8 || tensor->type == GGML_TYPE_I16 || tensor->type == GGML_TYPE_I32) {
  83. // This is going to create some weird integers though.
  84. ggml_backend_tensor_set(tensor, data.data(), 0, ggml_nbytes(tensor));
  85. } else {
  86. GGML_ABORT("fatal error");
  87. }
  88. }
  89. static std::vector<float> tensor_to_float(const ggml_tensor * t) {
  90. std::vector<float> tv;
  91. tv.reserve(ggml_nelements(t));
  92. std::vector<uint8_t> buf(ggml_nbytes(t));
  93. ggml_backend_tensor_get(t, buf.data(), 0, ggml_nbytes(t));
  94. ggml_type_traits_t tt = ggml_internal_get_type_traits(t->type);
  95. size_t bs = ggml_blck_size(t->type);
  96. std::vector<float> vq(ggml_blck_size(t->type));
  97. bool quantized = ggml_is_quantized(t->type);
  98. // access elements by index to avoid gaps in views
  99. for (int64_t i3 = 0; i3 < t->ne[3]; i3++) {
  100. for (int64_t i2 = 0; i2 < t->ne[2]; i2++) {
  101. for (int64_t i1 = 0; i1 < t->ne[1]; i1++) {
  102. for (int64_t i0 = 0; i0 < t->ne[0]; i0 += bs) {
  103. size_t i = i3*t->nb[3] + i2*t->nb[2] + i1*t->nb[1] + i0/bs*t->nb[0];
  104. if (t->type == GGML_TYPE_F16) {
  105. tv.push_back(ggml_fp16_to_fp32(*(ggml_fp16_t*)&buf[i]));
  106. } else if (t->type == GGML_TYPE_BF16) {
  107. tv.push_back(ggml_bf16_to_fp32(*(ggml_bf16_t*)&buf[i]));
  108. } else if (t->type == GGML_TYPE_F32) {
  109. tv.push_back(*(float *) &buf[i]);
  110. } else if (t->type == GGML_TYPE_I32) {
  111. tv.push_back((float)*(int32_t *) &buf[i]);
  112. } else if (t->type == GGML_TYPE_I16) {
  113. tv.push_back((float)*(int16_t *) &buf[i]);
  114. } else if (t->type == GGML_TYPE_I8) {
  115. tv.push_back((float)*(int8_t *) &buf[i]);
  116. } else if (quantized) {
  117. tt.to_float(&buf[i], vq.data(), bs);
  118. tv.insert(tv.end(), vq.begin(), vq.end());
  119. } else {
  120. GGML_ABORT("fatal error");
  121. }
  122. }
  123. }
  124. }
  125. }
  126. return tv;
  127. }
  128. /*
  129. static double cosine_similarity(const float * v1, const float * v2, size_t n) {
  130. double dot = 0.0;
  131. double mag1 = 0.0;
  132. double mag2 = 0.0;
  133. for (size_t i = 0; i < n; i++) {
  134. if (std::isnan(v1[i]) || std::isnan(v2[i])) {
  135. return -1.0f;
  136. }
  137. if (std::isinf(v1[i]) && std::isinf(v2[i])) {
  138. continue;
  139. }
  140. dot += v1[i]*v2[i];
  141. mag1 += v1[i]*v1[i];
  142. mag2 += v2[i]*v2[i];
  143. }
  144. return dot/sqrt(mag1*mag2);
  145. }
  146. static float distance(const float * v1, const float * v2, size_t n) {
  147. double d = 0.0;
  148. for (size_t i = 0; i < n; i++) {
  149. if (std::isnan(v1[i]) || std::isnan(v2[i])) {
  150. return INFINITY;
  151. }
  152. if (std::isinf(v1[i]) && std::isinf(v2[i])) {
  153. continue;
  154. }
  155. d += (v1[i] - v2[i])*(v1[i] - v2[i]);
  156. }
  157. return sqrt(d);
  158. }
  159. static float vec_len(const float * v, size_t n) {
  160. double d = 0.0;
  161. for (size_t i = 0; i < n; i++) {
  162. if (std::isnan(v[i])) {
  163. return INFINITY;
  164. }
  165. if (std::isinf(v[i])) {
  166. continue;
  167. }
  168. d += v[i]*v[i];
  169. }
  170. return sqrt(d);
  171. }
  172. */
  173. // normalized mean squared error = mse(a, b) / mse(a, 0)
  174. static double nmse(const float * a, const float * b, size_t n) {
  175. double mse_a_b = 0.0;
  176. double mse_a_0 = 0.0;
  177. for (size_t i = 0; i < n; i++) {
  178. float a_i = a[i];
  179. float b_i = b[i];
  180. mse_a_b += (a_i - b_i) * (a_i - b_i);
  181. mse_a_0 += a_i * a_i;
  182. }
  183. return mse_a_b / mse_a_0;
  184. }
  185. // utils for printing the variables of the test cases
  186. #define VAR_TO_STR(x) (#x "=" + var_to_str(x))
  187. template<typename T>
  188. static std::string var_to_str(const T & x) {
  189. return std::to_string(x);
  190. }
  191. template<typename T, size_t N>
  192. static std::string var_to_str(const T (&x)[N]) {
  193. std::string s = "[";
  194. for (size_t i = 0; i < N; i++) {
  195. if (i > 0) {
  196. s += ",";
  197. }
  198. s += var_to_str(x[i]);
  199. }
  200. s += "]";
  201. return s;
  202. }
  203. template<typename T, size_t N>
  204. static std::string var_to_str(const std::array<T, N> & x) {
  205. std::string s = "[";
  206. for (size_t i = 0; i < N; i++) {
  207. if (i > 0) {
  208. s += ",";
  209. }
  210. s += var_to_str(x[i]);
  211. }
  212. s += "]";
  213. return s;
  214. }
  215. //static std::string var_to_str(ggml_unary_op unary_op) {
  216. // return ggml_unary_op_name(unary_op);
  217. //}
  218. static std::string var_to_str(ggml_type type) {
  219. return ggml_type_name(type);
  220. }
  221. static std::string var_to_str(ggml_op_pool pool) {
  222. switch (pool) {
  223. case GGML_OP_POOL_AVG: return "avg";
  224. case GGML_OP_POOL_MAX: return "max";
  225. default: return std::to_string(pool);
  226. }
  227. }
  228. #define VARS_TO_STR1(a) VAR_TO_STR(a)
  229. #define VARS_TO_STR2(a, b) VAR_TO_STR(a) + "," + VAR_TO_STR(b)
  230. #define VARS_TO_STR3(a, b, c) VAR_TO_STR(a) + "," + VARS_TO_STR2(b, c)
  231. #define VARS_TO_STR4(a, b, c, d) VAR_TO_STR(a) + "," + VARS_TO_STR3(b, c, d)
  232. #define VARS_TO_STR5(a, b, c, d, e) VAR_TO_STR(a) + "," + VARS_TO_STR4(b, c, d, e)
  233. #define VARS_TO_STR6(a, b, c, d, e, f) VAR_TO_STR(a) + "," + VARS_TO_STR5(b, c, d, e, f)
  234. #define VARS_TO_STR7(a, b, c, d, e, f, g) VAR_TO_STR(a) + "," + VARS_TO_STR6(b, c, d, e, f, g)
  235. #define VARS_TO_STR8(a, b, c, d, e, f, g, h) VAR_TO_STR(a) + "," + VARS_TO_STR7(b, c, d, e, f, g, h)
  236. #define VARS_TO_STR9(a, b, c, d, e, f, g, h, i) VAR_TO_STR(a) + "," + VARS_TO_STR8(b, c, d, e, f, g, h, i)
  237. #define VARS_TO_STR10(a, b, c, d, e, f, g, h, i, j) VAR_TO_STR(a) + "," + VARS_TO_STR9(b, c, d, e, f, g, h, i, j)
  238. #define VARS_TO_STR11(a, b, c, d, e, f, g, h, i, j, k) VAR_TO_STR(a) + "," + VARS_TO_STR10(b, c, d, e, f, g, h, i, j, k)
  239. #define VARS_TO_STR12(a, b, c, d, e, f, g, h, i, j, k, l) VAR_TO_STR(a) + "," + VARS_TO_STR11(b, c, d, e, f, g, h, i, j, k, l)
  240. #ifdef GGML_USE_SYCL
  241. static bool inline _isinf(float f) {
  242. return (*(uint32_t *)&f & 0x7fffffff) == 0x7f800000;
  243. }
  244. #else
  245. static bool inline _isinf(float f) { return std::isinf(f); }
  246. #endif
  247. // accept FLT_MAX as infinity
  248. static bool isinf_or_max(float f) {
  249. return _isinf(f) || f == FLT_MAX || f == -FLT_MAX;
  250. }
  251. static bool ggml_is_view_op(enum ggml_op op) {
  252. return op == GGML_OP_VIEW || op == GGML_OP_RESHAPE || op == GGML_OP_PERMUTE || op == GGML_OP_TRANSPOSE;
  253. }
  254. enum test_mode {
  255. MODE_TEST,
  256. MODE_PERF,
  257. };
  258. struct test_case {
  259. virtual ~test_case() {}
  260. virtual std::string op_desc(ggml_tensor * t) {
  261. return ggml_op_desc(t);
  262. }
  263. virtual std::string vars() {
  264. return "";
  265. }
  266. virtual ggml_tensor * build_graph(ggml_context * ctx) = 0;
  267. virtual double max_nmse_err() {
  268. return 1e-7;
  269. }
  270. virtual void initialize_tensors(ggml_context * ctx) {
  271. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
  272. init_tensor_uniform(t);
  273. }
  274. }
  275. virtual size_t op_size(ggml_tensor * t) {
  276. size_t size = ggml_nbytes(t);
  277. // add source tensors
  278. for (int i = 0; i < GGML_MAX_SRC; i++) {
  279. if (t->src[i] != NULL) {
  280. size += ggml_nbytes(t->src[i]);
  281. }
  282. }
  283. return size;
  284. }
  285. ggml_cgraph * gf = nullptr;
  286. static const int sentinel_size = 1024;
  287. test_mode mode;
  288. std::vector<ggml_tensor *> sentinels;
  289. void add_sentinel(ggml_context * ctx) {
  290. if (mode == MODE_PERF) {
  291. return;
  292. }
  293. ggml_tensor * sentinel = ::ggml_new_tensor_1d(ctx, GGML_TYPE_F32, sentinel_size);
  294. ggml_format_name(sentinel, "sent_%zu", sentinels.size());
  295. sentinels.push_back(sentinel);
  296. }
  297. // hijack ggml_new_tensor to add sentinels after each tensor to check for overflows in the backend
  298. ggml_tensor * ggml_new_tensor(ggml_context * ctx, ggml_type type, int n_dims, const int64_t * ne) {
  299. ggml_tensor * t = ::ggml_new_tensor(ctx, type, n_dims, ne);
  300. add_sentinel(ctx);
  301. return t;
  302. }
  303. ggml_tensor * ggml_new_tensor_1d(ggml_context * ctx, ggml_type type, int64_t ne0) {
  304. ggml_tensor * t = ::ggml_new_tensor_1d(ctx, type, ne0);
  305. add_sentinel(ctx);
  306. return t;
  307. }
  308. ggml_tensor * ggml_new_tensor_2d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1) {
  309. ggml_tensor * t = ::ggml_new_tensor_2d(ctx, type, ne0, ne1);
  310. add_sentinel(ctx);
  311. return t;
  312. }
  313. ggml_tensor * ggml_new_tensor_3d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2) {
  314. ggml_tensor * t = ::ggml_new_tensor_3d(ctx, type, ne0, ne1, ne2);
  315. add_sentinel(ctx);
  316. return t;
  317. }
  318. ggml_tensor * ggml_new_tensor_4d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3) {
  319. ggml_tensor * t = ::ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3);
  320. add_sentinel(ctx);
  321. return t;
  322. }
  323. bool eval(ggml_backend_t backend1, ggml_backend_t backend2, const char * op_name) {
  324. mode = MODE_TEST;
  325. ggml_init_params params = {
  326. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead(),
  327. /* .mem_base = */ NULL,
  328. /* .no_alloc = */ true,
  329. };
  330. ggml_context * ctx = ggml_init(params);
  331. gf = ggml_new_graph(ctx);
  332. // pre-graph sentinel
  333. add_sentinel(ctx);
  334. ggml_tensor * out = build_graph(ctx);
  335. if (op_name != nullptr && op_desc(out) != op_name) {
  336. //printf(" %s: skipping\n", op_desc(out).c_str());
  337. ggml_free(ctx);
  338. return true;
  339. }
  340. printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  341. fflush(stdout);
  342. // check if the backends support the ops
  343. bool supported = true;
  344. for (ggml_backend_t backend : {backend1, backend2}) {
  345. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  346. if (!ggml_backend_supports_op(backend, t)) {
  347. printf("not supported [%s] ", ggml_backend_name(backend));
  348. supported = false;
  349. break;
  350. }
  351. }
  352. }
  353. if (!supported) {
  354. printf("\n");
  355. ggml_free(ctx);
  356. return true;
  357. }
  358. // post-graph sentinel
  359. add_sentinel(ctx);
  360. // allocate
  361. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend1);
  362. if (buf == NULL) {
  363. printf("failed to allocate tensors [%s] ", ggml_backend_name(backend1));
  364. ggml_free(ctx);
  365. return false;
  366. }
  367. // build graph
  368. ggml_build_forward_expand(gf, out);
  369. // add sentinels as graph nodes so that they are checked in the callback
  370. for (ggml_tensor * sentinel : sentinels) {
  371. gf->nodes[gf->n_nodes++] = sentinel;
  372. }
  373. // randomize tensors
  374. initialize_tensors(ctx);
  375. // compare
  376. struct callback_userdata {
  377. bool ok;
  378. double max_err;
  379. ggml_backend_t backend1;
  380. ggml_backend_t backend2;
  381. };
  382. callback_userdata ud {
  383. true,
  384. max_nmse_err(),
  385. backend1,
  386. backend2
  387. };
  388. auto callback = [](int index, ggml_tensor * t1, ggml_tensor * t2, void * user_data) -> bool {
  389. callback_userdata * ud = (callback_userdata *) user_data;
  390. const char * bn1 = ggml_backend_name(ud->backend1);
  391. const char * bn2 = ggml_backend_name(ud->backend2);
  392. if (t1->op == GGML_OP_NONE) {
  393. // sentinels must be unchanged
  394. std::vector<uint8_t> t1_data(ggml_nbytes(t1));
  395. std::vector<uint8_t> t2_data(ggml_nbytes(t2));
  396. ggml_backend_tensor_get(t1, t1_data.data(), 0, ggml_nbytes(t1));
  397. ggml_backend_tensor_get(t2, t2_data.data(), 0, ggml_nbytes(t2));
  398. if (memcmp(t1_data.data(), t2_data.data(), ggml_nbytes(t1)) != 0) {
  399. printf("sentinel mismatch: %s ", t1->name);
  400. ud->ok = false;
  401. return true;
  402. }
  403. }
  404. std::vector<float> f1 = tensor_to_float(t1);
  405. std::vector<float> f2 = tensor_to_float(t2);
  406. for (size_t i = 0; i < f1.size(); i++) {
  407. // check for nans
  408. if (std::isnan(f1[i]) || std::isnan(f2[i])) {
  409. printf("[%s] NaN at index %zu (%s=%f %s=%f) ", ggml_op_desc(t1), i, bn1, f1[i], bn2, f2[i]);
  410. ud->ok = false;
  411. return true;
  412. }
  413. // check for infs: both must be inf of the same sign, or both must be finite
  414. if (isinf_or_max(f1[i]) || isinf_or_max(f2[i])) {
  415. if (isinf_or_max(f1[i]) && isinf_or_max(f2[i])) {
  416. if (std::signbit(f1[i]) != std::signbit(f2[i])) {
  417. printf("[%s] inf sign mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]);
  418. ud->ok = false;
  419. return true;
  420. }
  421. } else {
  422. printf("[%s] inf mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]);
  423. ud->ok = false;
  424. return true;
  425. }
  426. }
  427. }
  428. double err = nmse(f1.data(), f2.data(), f1.size());
  429. if (err > ud->max_err) {
  430. printf("[%s] NMSE = %.9f > %.9f ", ggml_op_desc(t1), err, ud->max_err);
  431. //for (int i = 0; i < (int) f1.size(); i++) {
  432. // printf("%5d %9.6f %9.6f, diff = %9.6f\n", i, f1[i], f2[i], f1[i] - f2[i]);
  433. //}
  434. //printf("\n");
  435. //exit(1);
  436. ud->ok = false;
  437. }
  438. return true;
  439. GGML_UNUSED(index);
  440. };
  441. const bool cmp_ok = ggml_backend_compare_graph_backend(backend1, backend2, gf, callback, &ud);
  442. if (!cmp_ok) {
  443. printf("compare failed ");
  444. }
  445. ggml_backend_buffer_free(buf);
  446. ggml_free(ctx);
  447. if (ud.ok && cmp_ok) {
  448. printf("\033[1;32mOK\033[0m\n");
  449. return true;
  450. }
  451. printf("\033[1;31mFAIL\033[0m\n");
  452. return false;
  453. }
  454. bool eval_perf(ggml_backend_t backend, const char * op_name) {
  455. mode = MODE_PERF;
  456. static const size_t graph_nodes = 8192;
  457. ggml_init_params params = {
  458. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead_custom(graph_nodes, false),
  459. /* .mem_base = */ NULL,
  460. /* .no_alloc = */ true,
  461. };
  462. ggml_context * ctx = ggml_init(params);
  463. ggml_tensor * out = build_graph(ctx);
  464. if (op_name != nullptr && op_desc(out) != op_name) {
  465. //printf(" %s: skipping\n", op_desc(out).c_str());
  466. ggml_free(ctx);
  467. return true;
  468. }
  469. int len = printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  470. fflush(stdout);
  471. // check if backends support op
  472. if (!ggml_backend_supports_op(backend, out)) {
  473. printf("not supported\n");
  474. ggml_free(ctx);
  475. return true;
  476. }
  477. // align while also leaving some margin for variations in parameters
  478. int align = 20;
  479. int last = (len + align - 1) / align * align;
  480. if (last - len < 5) {
  481. last += align;
  482. }
  483. last = std::max(last, 60);
  484. printf("%*s", last - len, "");
  485. // allocate
  486. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend);
  487. if (buf == NULL) {
  488. printf("failed to allocate tensors\n");
  489. ggml_free(ctx);
  490. return false;
  491. }
  492. // randomize tensors
  493. initialize_tensors(ctx);
  494. // build graph
  495. ggml_cgraph * gf = ggml_new_graph_custom(ctx, graph_nodes, false);
  496. ggml_build_forward_expand(gf, out);
  497. // warmup run
  498. ggml_backend_graph_compute(backend, gf);
  499. // duplicate the op
  500. size_t target_size = ggml_backend_is_cpu(backend) ? 1ULL << 33 : 1ULL << 35; // 8 GB CPU, 32 GB GPU
  501. int n_runs = std::min((size_t)gf->size - gf->n_nodes, target_size / op_size(out)) + 1;
  502. for (int i = 1; i < n_runs; i++) {
  503. gf->nodes[gf->n_nodes++] = out;
  504. }
  505. // calculate memory
  506. size_t mem = n_runs * op_size(out);
  507. auto tensor_op_size = [](ggml_tensor * t) {
  508. size_t size = ggml_nbytes(t);
  509. // add source tensors
  510. for (int i = 0; i < GGML_MAX_SRC; i++) {
  511. if (t->src[i] != NULL) {
  512. size += ggml_nbytes(t->src[i]);
  513. }
  514. }
  515. return size;
  516. };
  517. for (int i = 0; i < gf->n_nodes; i++) {
  518. if (ggml_is_view_op(gf->nodes[i]->op) || gf->nodes[i] == out) {
  519. continue;
  520. }
  521. mem += tensor_op_size(gf->nodes[i]);
  522. }
  523. // run
  524. ggml_backend_synchronize(backend);
  525. int64_t start_time = ggml_time_us();
  526. ggml_backend_graph_compute(backend, gf);
  527. ggml_backend_synchronize(backend);
  528. int64_t end_time = ggml_time_us();
  529. double time_us = end_time - start_time;
  530. printf(" %5d runs - %8.2f us/run - %8zu kB/run - \033[1;34m%7.2f GB/s\033[0m\n",
  531. n_runs,
  532. time_us / n_runs,
  533. op_size(out) / 1024,
  534. mem / (time_us/1e6) / 1024.0 / 1024.0 / 1024.0);
  535. ggml_backend_buffer_free(buf);
  536. ggml_free(ctx);
  537. return true;
  538. }
  539. };
  540. // GGML_OP_UNARY
  541. struct test_unary : public test_case {
  542. const ggml_unary_op op;
  543. const ggml_type type;
  544. const std::array<int64_t, 4> ne_a;
  545. int v; // view (1 : non-contiguous a)
  546. std::string vars() override {
  547. return VARS_TO_STR3(type, ne_a, v);
  548. }
  549. test_unary(ggml_unary_op op,
  550. ggml_type type = GGML_TYPE_F32,
  551. std::array<int64_t, 4> ne_a = {128, 10, 10, 10},
  552. int v = 0)
  553. : op(op), type(type), ne_a(ne_a), v(v) {}
  554. ggml_tensor * build_graph(ggml_context * ctx) override {
  555. ggml_tensor * a;
  556. if (v & 1) {
  557. auto ne = ne_a; ne[0] *= 3;
  558. a = ggml_new_tensor(ctx, type, 4, ne.data());
  559. a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
  560. } else {
  561. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  562. }
  563. ggml_tensor * out = ggml_unary(ctx, a, op);
  564. return out;
  565. }
  566. void initialize_tensors(ggml_context * ctx) override {
  567. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  568. // test extended range of values to check for NaNs in GELU
  569. init_tensor_uniform(t, -150.f, 150.f);
  570. }
  571. }
  572. };
  573. // GGML_OP_GET_ROWS
  574. struct test_get_rows : public test_case {
  575. const ggml_type type;
  576. const int n; // cols
  577. const int m; // rows
  578. const int r; // rows to get
  579. const int b; // batch size
  580. const bool v; // view (non-contiguous src1)
  581. std::string vars() override {
  582. return VARS_TO_STR6(type, n, m, r, b, v);
  583. }
  584. test_get_rows(ggml_type type = GGML_TYPE_F32, int n = 10, int m = 5, int r = 3, int b = 1, bool v = false)
  585. : type(type), n(n), m(m), r(r), b(b), v(v) {}
  586. ggml_tensor * build_graph(ggml_context * ctx) override {
  587. ggml_tensor * in = ggml_new_tensor_3d(ctx, type, n, m, b);
  588. ggml_tensor * rows = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, r, b);
  589. if (v) {
  590. rows = ggml_view_2d(ctx, rows, r/2, b, rows->nb[1], 0);
  591. }
  592. ggml_tensor * out = ggml_get_rows(ctx, in, rows);
  593. return out;
  594. }
  595. void initialize_tensors(ggml_context * ctx) override {
  596. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  597. if (t->type == GGML_TYPE_I32) {
  598. if (ggml_is_view_op(t->op)) { continue; }
  599. // rows
  600. std::vector<int> data(r*b);
  601. for (int i = 0; i < r*b; i++) {
  602. data[i] = rand() % m;
  603. }
  604. ggml_backend_tensor_set(t, data.data(), 0, r * b * sizeof(int));
  605. } else {
  606. init_tensor_uniform(t);
  607. }
  608. }
  609. }
  610. };
  611. // GGML_OP_REPEAT
  612. struct test_repeat : public test_case {
  613. const ggml_type type;
  614. const std::array<int64_t, 4> ne;
  615. const std::array<int, 4> nr;
  616. std::string vars() override {
  617. return VARS_TO_STR3(type, ne, nr);
  618. }
  619. size_t op_size(ggml_tensor * t) override {
  620. return ggml_nbytes(t) * 2;
  621. }
  622. test_repeat(ggml_type type = GGML_TYPE_F32,
  623. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  624. std::array<int, 4> nr = {2, 2, 2, 2})
  625. : type(type), ne(ne), nr(nr) {}
  626. ggml_tensor * build_graph(ggml_context * ctx) override {
  627. ggml_tensor * target = ggml_new_tensor_4d(ctx, type, ne[0]*nr[0], ne[1]*nr[1], ne[2]*nr[2], ne[3]*nr[3]);
  628. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  629. ggml_tensor * out = ggml_repeat(ctx, src, target);
  630. return out;
  631. }
  632. };
  633. // GGML_OP_DUP
  634. struct test_dup : public test_case {
  635. const ggml_type type;
  636. const std::array<int64_t, 4> ne;
  637. const std::array<int64_t, 4> permute;
  638. bool _use_permute;
  639. std::string vars() override {
  640. std::string v = VARS_TO_STR2(type, ne);
  641. if (_use_permute) v += "," + VAR_TO_STR(permute);
  642. return v;
  643. }
  644. test_dup(ggml_type type = GGML_TYPE_F32,
  645. std::array<int64_t, 4> ne = {10, 10, 20, 1},
  646. std::array<int64_t, 4> permute = {0, 0, 0, 0})
  647. : type(type), ne(ne), permute(permute),
  648. _use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {}
  649. ggml_tensor * build_graph(ggml_context * ctx) override {
  650. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  651. if (_use_permute) {
  652. src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]);
  653. }
  654. ggml_tensor * out = ggml_dup(ctx, src);
  655. return out;
  656. }
  657. };
  658. // GGML_OP_CPY
  659. struct test_cpy : public test_case {
  660. const ggml_type type_src;
  661. const ggml_type type_dst;
  662. const std::array<int64_t, 4> ne;
  663. const std::array<int64_t, 4> permute;
  664. bool _src_use_permute;
  665. std::string vars() override {
  666. return VARS_TO_STR4(type_src, type_dst, ne, permute);
  667. }
  668. double max_nmse_err() override {
  669. return 1e-6;
  670. }
  671. size_t op_size(ggml_tensor * t) override {
  672. return ggml_nbytes(t) + ggml_nbytes(t->src[0]);
  673. }
  674. test_cpy(ggml_type type_src = GGML_TYPE_F32, ggml_type type_dst = GGML_TYPE_F32,
  675. std::array<int64_t, 4> ne = {10, 10, 10, 1},
  676. std::array<int64_t, 4> permute = {0, 0, 0, 0})
  677. : type_src(type_src), type_dst(type_dst), ne(ne), permute(permute),
  678. _src_use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {}
  679. ggml_tensor * build_graph(ggml_context * ctx) override {
  680. ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne.data());
  681. if (_src_use_permute) {
  682. src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]);
  683. }
  684. ggml_tensor* dst = ggml_new_tensor(ctx, type_dst, 4, src->ne);
  685. ggml_tensor * out = ggml_cpy(ctx, src, dst);
  686. return out;
  687. }
  688. };
  689. // GGML_OP_CONT
  690. struct test_cont : public test_case {
  691. const ggml_type type;
  692. const std::array<int64_t, 4> ne;
  693. std::string vars() override {
  694. return VARS_TO_STR2(type, ne);
  695. }
  696. test_cont(ggml_type type = GGML_TYPE_F32,
  697. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  698. : type(type), ne(ne) {}
  699. ggml_tensor * build_graph(ggml_context * ctx) override {
  700. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  701. src = ggml_transpose(ctx, src);
  702. ggml_tensor * out = ggml_cont(ctx, src);
  703. return out;
  704. }
  705. };
  706. // GGML_OP_ADD
  707. // GGML_OP_MUL
  708. // GGML_OP_DIV
  709. struct test_bin_bcast : public test_case {
  710. using op_t = ggml_tensor * (*) (ggml_context *, ggml_tensor *, ggml_tensor *);
  711. op_t op;
  712. const ggml_type type;
  713. const std::array<int64_t, 4> ne;
  714. const std::array<int, 4> nr;
  715. std::string vars() override {
  716. return VARS_TO_STR3(type, ne, nr);
  717. }
  718. size_t op_size(ggml_tensor * t) override {
  719. return ggml_nbytes(t) * 3;
  720. }
  721. test_bin_bcast(op_t op, ggml_type type = GGML_TYPE_F32,
  722. std::array<int64_t, 4> ne = {10, 10, 1, 1},
  723. std::array<int, 4> nr = {1, 2, 1, 1})
  724. : op(op), type(type), ne(ne), nr(nr) {}
  725. ggml_tensor * build_graph(ggml_context * ctx) override {
  726. ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0]*nr[0], ne[1]*nr[1], ne[2]*nr[2], ne[3]*nr[3]);
  727. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data());
  728. ggml_tensor * out = op(ctx, a, b);
  729. return out;
  730. }
  731. void initialize_tensors(ggml_context * ctx) override {
  732. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  733. if (op == ggml_div) {
  734. // avoid division by zero
  735. init_tensor_uniform(t, 1.0f, 2.0f);
  736. } else {
  737. init_tensor_uniform(t);
  738. }
  739. }
  740. }
  741. };
  742. // GGML_OP_SCALE
  743. struct test_scale : public test_case {
  744. const ggml_type type;
  745. const std::array<int64_t, 4> ne;
  746. float scale;
  747. std::string vars() override {
  748. return VARS_TO_STR3(type, ne, scale);
  749. }
  750. test_scale(ggml_type type = GGML_TYPE_F32,
  751. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  752. float scale = 2.0f)
  753. : type(type), ne(ne), scale(scale) {}
  754. ggml_tensor * build_graph(ggml_context * ctx) override {
  755. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  756. ggml_tensor * out = ggml_scale(ctx, a, scale);
  757. return out;
  758. }
  759. };
  760. // GGML_OP_NORM
  761. struct test_norm : public test_case {
  762. const ggml_type type;
  763. const std::array<int64_t, 4> ne;
  764. float eps;
  765. std::string vars() override {
  766. return VARS_TO_STR3(type, ne, eps);
  767. }
  768. test_norm(ggml_type type = GGML_TYPE_F32,
  769. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  770. float eps = 1e-6f)
  771. : type(type), ne(ne), eps(eps) {}
  772. ggml_tensor * build_graph(ggml_context * ctx) override {
  773. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  774. ggml_tensor * out = ggml_norm(ctx, a, eps);
  775. return out;
  776. }
  777. };
  778. // GGML_OP_RMS_NORM
  779. struct test_rms_norm : public test_case {
  780. const ggml_type type;
  781. const std::array<int64_t, 4> ne;
  782. float eps;
  783. std::string vars() override {
  784. return VARS_TO_STR3(type, ne, eps);
  785. }
  786. test_rms_norm(ggml_type type = GGML_TYPE_F32,
  787. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  788. float eps = 1e-6f)
  789. : type(type), ne(ne), eps(eps) {}
  790. ggml_tensor * build_graph(ggml_context * ctx) override {
  791. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  792. ggml_tensor * out = ggml_rms_norm(ctx, a, eps);
  793. return out;
  794. }
  795. };
  796. // GGML_OP_SSM_CONV
  797. struct test_ssm_conv : public test_case {
  798. const ggml_type type;
  799. const std::array<int64_t, 4> ne_a;
  800. const std::array<int64_t, 4> ne_b;
  801. std::string vars() override {
  802. return VARS_TO_STR3(type, ne_a, ne_b);
  803. }
  804. test_ssm_conv(ggml_type type = GGML_TYPE_F32,
  805. std::array<int64_t, 4> ne_a = {10, 10, 10, 1},
  806. std::array<int64_t, 4> ne_b = {3, 3, 1, 1})
  807. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  808. ggml_tensor * build_graph(ggml_context * ctx) override {
  809. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  810. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  811. ggml_tensor * out = ggml_ssm_conv(ctx, a, b);
  812. return out;
  813. }
  814. };
  815. // GGML_OP_SSM_SCAN
  816. struct test_ssm_scan : public test_case {
  817. const ggml_type type;
  818. const int64_t d_state;
  819. const int64_t d_inner;
  820. const int64_t n_seq_tokens;
  821. const int64_t n_seqs;
  822. std::string vars() override {
  823. return VARS_TO_STR5(type, d_state, d_inner, n_seq_tokens, n_seqs);
  824. }
  825. test_ssm_scan(ggml_type type = GGML_TYPE_F32,
  826. int64_t d_state = 32, int64_t d_inner = 32, int64_t n_seq_tokens = 32, int64_t n_seqs = 32)
  827. : type(type), d_state(d_state), d_inner(d_inner), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs) {}
  828. ggml_tensor * build_graph(ggml_context * ctx) override {
  829. ggml_tensor * s = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_state, d_inner, n_seqs, 1 }.data());
  830. ggml_tensor * x = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_inner, n_seq_tokens, n_seqs, 1 }.data());
  831. ggml_tensor * dt = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_inner, n_seq_tokens, n_seqs, 1 }.data());
  832. ggml_tensor * A = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_state, d_inner, 1 , 1 }.data());
  833. ggml_tensor * B = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_state, n_seq_tokens, n_seqs, 1 }.data());
  834. ggml_tensor * C = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_state, n_seq_tokens, n_seqs, 1 }.data());
  835. ggml_tensor * out = ggml_ssm_scan(ctx, s, x, dt, A, B, C);
  836. return out;
  837. }
  838. };
  839. // GGML_OP_MUL_MAT
  840. struct test_mul_mat : public test_case {
  841. const ggml_type type_a;
  842. const ggml_type type_b;
  843. const int64_t m;
  844. const int64_t n;
  845. const int64_t k;
  846. const std::array<int64_t, 2> bs; // dims 3 and 4
  847. const std::array<int64_t, 2> nr; // repeat in dims 3 and 4
  848. std::string vars() override {
  849. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, nr);
  850. }
  851. double max_nmse_err() override {
  852. return 5e-4;
  853. }
  854. size_t op_size(ggml_tensor * t) override {
  855. size_t a = ggml_nbytes(t->src[0]) * n * nr[0] * nr[1];
  856. size_t b = ggml_nbytes(t->src[1]) * m;
  857. size_t c = ggml_nbytes(t);
  858. return a + b + c;
  859. GGML_UNUSED(t);
  860. }
  861. test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  862. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  863. std::array<int64_t, 2> bs = {10, 10},
  864. std::array<int64_t, 2> nr = {2, 2})
  865. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr) {}
  866. ggml_tensor * build_graph(ggml_context * ctx) override {
  867. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  868. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, k, m, bs[0] , bs[1]);
  869. ggml_tensor * b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]);
  870. ggml_tensor * out = ggml_mul_mat(ctx, a, b);
  871. return out;
  872. }
  873. };
  874. // GGML_OP_MUL_MAT_ID
  875. struct test_mul_mat_id : public test_case {
  876. const ggml_type type_a;
  877. const ggml_type type_b;
  878. const int n_mats;
  879. const int n_used;
  880. const bool b; // brodcast b matrix
  881. const int64_t m;
  882. const int64_t n;
  883. const int64_t k;
  884. std::string vars() override {
  885. return VARS_TO_STR8(type_a, type_b, n_mats, n_used, b, m, n, k);
  886. }
  887. double max_nmse_err() override {
  888. return 5e-4;
  889. }
  890. size_t op_size(ggml_tensor * t) override {
  891. size_t a = ggml_nbytes(t->src[2]) * n;
  892. size_t b = ggml_nbytes(t->src[1]) * m;
  893. size_t c = ggml_nbytes(t);
  894. return a + b + c;
  895. GGML_UNUSED(t);
  896. }
  897. test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  898. int n_mats = 8, int n_used = 2, bool b = false,
  899. int64_t m = 32, int64_t n = 32, int64_t k = 32)
  900. : type_a(type_a), type_b(type_b), n_mats(n_mats), n_used(n_used), b(b),
  901. m(m), n(n), k(k) {
  902. GGML_ASSERT(n_used <= n_mats);
  903. }
  904. ggml_tensor * build_graph(ggml_context * ctx) override {
  905. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  906. ggml_tensor * as = ggml_new_tensor_3d(ctx, type_a, k, m, n_mats);
  907. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n);
  908. if (n_used != n_mats) {
  909. ids = ggml_view_2d(ctx, ids, n_used, n, ids->nb[1], 0);
  910. }
  911. ggml_tensor * b = ggml_new_tensor_3d(ctx, type_b, k, this->b ? 1 : n_used, n);
  912. ggml_tensor * out = ggml_mul_mat_id(ctx, as, b, ids);
  913. return out;
  914. }
  915. void initialize_tensors(ggml_context * ctx) override {
  916. std::random_device rd;
  917. std::default_random_engine rng(rd());
  918. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  919. if (t->type == GGML_TYPE_I32) {
  920. if (ggml_is_view_op(t->op)) { continue; }
  921. // ids
  922. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  923. std::vector<int32_t> data(t->ne[0]);
  924. for (int i = 0; i < t->ne[0]; i++) {
  925. data[i] = i % n_mats;
  926. }
  927. std::shuffle(data.begin(), data.end(), rng);
  928. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t));
  929. }
  930. } else {
  931. init_tensor_uniform(t);
  932. }
  933. }
  934. }
  935. };
  936. // GGML_OP_SQR
  937. struct test_sqr : public test_case {
  938. const ggml_type type;
  939. const std::array<int64_t, 4> ne;
  940. std::string vars() override {
  941. return VARS_TO_STR2(type, ne);
  942. }
  943. test_sqr(ggml_type type = GGML_TYPE_F32,
  944. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  945. : type(type), ne(ne) {}
  946. ggml_tensor * build_graph(ggml_context * ctx) override {
  947. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  948. ggml_tensor * out = ggml_sqr(ctx, a);
  949. return out;
  950. }
  951. };
  952. // GGML_OP_SQRT
  953. struct test_sqrt : public test_case {
  954. const ggml_type type;
  955. const std::array<int64_t, 4> ne;
  956. std::string vars() override {
  957. return VARS_TO_STR2(type, ne);
  958. }
  959. test_sqrt(ggml_type type = GGML_TYPE_F32,
  960. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  961. : type(type), ne(ne) {}
  962. ggml_tensor * build_graph(ggml_context * ctx) override {
  963. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  964. ggml_tensor * out = ggml_sqrt(ctx, a);
  965. return out;
  966. }
  967. void initialize_tensors(ggml_context * ctx) override {
  968. // fill with positive values
  969. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  970. init_tensor_uniform(t, 0.0f, 100.0f);
  971. }
  972. }
  973. };
  974. // GGML_OP_CLAMP
  975. struct test_clamp : public test_case {
  976. const ggml_type type;
  977. const std::array<int64_t, 4> ne;
  978. float min;
  979. float max;
  980. std::string vars() override {
  981. return VARS_TO_STR4(type, ne, min, max);
  982. }
  983. test_clamp(ggml_type type = GGML_TYPE_F32,
  984. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  985. float min = -0.5f, float max = 0.5f)
  986. : type(type), ne(ne), min(min), max(max) {}
  987. ggml_tensor * build_graph(ggml_context * ctx) override {
  988. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  989. ggml_tensor * out = ggml_clamp(ctx, a, min, max);
  990. return out;
  991. }
  992. };
  993. // GGML_OP_DIAG_MASK_INF
  994. struct test_diag_mask_inf : public test_case {
  995. const ggml_type type;
  996. const std::array<int64_t, 4> ne;
  997. const int n_past;
  998. std::string vars() override {
  999. return VARS_TO_STR3(type, ne, n_past);
  1000. }
  1001. test_diag_mask_inf(ggml_type type = GGML_TYPE_F32,
  1002. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  1003. int n_past = 5)
  1004. : type(type), ne(ne), n_past(n_past) {}
  1005. ggml_tensor * build_graph(ggml_context * ctx) override {
  1006. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1007. ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past);
  1008. return out;
  1009. }
  1010. };
  1011. // GGML_OP_SOFT_MAX
  1012. struct test_soft_max : public test_case {
  1013. const ggml_type type;
  1014. const std::array<int64_t, 4> ne;
  1015. const bool mask;
  1016. const float scale;
  1017. const float max_bias;
  1018. std::string vars() override {
  1019. return VARS_TO_STR5(type, ne, mask, scale, max_bias);
  1020. }
  1021. // the 1024 test with bias occasionally fails:
  1022. // SOFT_MAX(type=f32,ne=[1024,16,1,1],mask=1,scale=1.000000,max_bias=8.000000): [SOFT_MAX] NMSE = 0.000000103 > 0.000000100 FAIL
  1023. virtual double max_nmse_err() override {
  1024. return 1e-6;
  1025. }
  1026. test_soft_max(ggml_type type = GGML_TYPE_F32,
  1027. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  1028. bool mask = false,
  1029. float scale = 1.0f,
  1030. float max_bias = 0.0f)
  1031. : type(type), ne(ne), mask(mask), scale(scale), max_bias(max_bias) {}
  1032. ggml_tensor * build_graph(ggml_context * ctx) override {
  1033. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1034. ggml_tensor * mask = nullptr;
  1035. if (this->mask) {
  1036. mask = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ne[0], ne[1]);
  1037. }
  1038. ggml_tensor * out = ggml_soft_max_ext(ctx, a, mask, scale, max_bias);
  1039. return out;
  1040. }
  1041. };
  1042. // GGML_OP_ROPE
  1043. struct test_rope : public test_case {
  1044. const ggml_type type;
  1045. const std::array<int64_t, 4> ne_a;
  1046. int n_dims;
  1047. int mode;
  1048. int n_ctx; // used to generate positions
  1049. float fs; // freq_scale
  1050. float ef; // ext_factor
  1051. float af; // attn_factor
  1052. bool ff;
  1053. int v; // view (1 : non-contiguous a)
  1054. std::string vars() override {
  1055. return VARS_TO_STR10(type, ne_a, n_dims, mode, n_ctx, fs, ef, af, ff, v);
  1056. }
  1057. test_rope(ggml_type type = GGML_TYPE_F32,
  1058. std::array<int64_t, 4> ne_a = {10, 10, 10, 1},
  1059. int n_dims = 10, int mode = 0, int n_ctx = 512, float fs = 1.0f, float ef = 0.0f, float af = 0.0f, bool ff = false, int v = 0)
  1060. : type(type), ne_a(ne_a), n_dims(n_dims), mode(mode), n_ctx(n_ctx), fs(fs), ef(ef), af(af), ff(ff), v(v) {}
  1061. ggml_tensor * build_graph(ggml_context * ctx) override {
  1062. ggml_tensor * a;
  1063. if (v & 1) {
  1064. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1065. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1066. a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
  1067. } else {
  1068. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1069. }
  1070. ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2]);
  1071. ggml_tensor * freq = ff ? ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_dims/2) : nullptr;
  1072. ggml_tensor * out = ggml_rope_ext(ctx, a, pos, freq, n_dims, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f);
  1073. return out;
  1074. }
  1075. void initialize_tensors(ggml_context * ctx) override {
  1076. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1077. if (t->type == GGML_TYPE_I32) {
  1078. // pos
  1079. std::vector<int> data(ne_a[2]);
  1080. for (int i = 0; i < ne_a[2]; i++) {
  1081. data[i] = rand() % n_ctx;
  1082. }
  1083. ggml_backend_tensor_set(t, data.data(), 0, ne_a[2] * sizeof(int));
  1084. } else {
  1085. if (t->ne[0] == n_dims/2) {
  1086. // frequency factors in the range [0.9f, 1.1f]
  1087. init_tensor_uniform(t, 0.9f, 1.1f);
  1088. } else {
  1089. init_tensor_uniform(t);
  1090. }
  1091. }
  1092. }
  1093. }
  1094. };
  1095. // GGML_OP_POOL2D
  1096. struct test_pool2d : public test_case {
  1097. enum ggml_op_pool pool_type;
  1098. const ggml_type type_input;
  1099. const std::array<int64_t, 4> ne_input;
  1100. // kernel size
  1101. const int k0;
  1102. const int k1;
  1103. // stride
  1104. const int s0;
  1105. const int s1;
  1106. // padding
  1107. const int p0;
  1108. const int p1;
  1109. std::string vars() override {
  1110. return VARS_TO_STR9(pool_type, type_input, ne_input, k0, k1, s0, s1, p0, p1);
  1111. }
  1112. test_pool2d(ggml_op_pool pool_type = GGML_OP_POOL_AVG,
  1113. ggml_type type_input = GGML_TYPE_F32,
  1114. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1115. int k0 = 3, int k1 = 3,
  1116. int s0 = 1, int s1 = 1,
  1117. int p0 = 1, int p1 = 1)
  1118. : pool_type(pool_type), type_input(type_input), ne_input(ne_input), k0(k0), k1(k1), s0(s0), s1(s1), p0(p0), p1(p1) {}
  1119. ggml_tensor * build_graph(ggml_context * ctx) override {
  1120. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1121. ggml_tensor * out = ggml_pool_2d(ctx, input, pool_type, k0, k1, s0, s1, p0, p1);
  1122. return out;
  1123. }
  1124. };
  1125. // GGML_OP_CONV_TRANSPOSE_1D
  1126. struct test_conv_transpose_1d : public test_case {
  1127. const std::array<int64_t, 4> ne_input;
  1128. const std::array<int64_t, 4> ne_kernel;
  1129. const int s0; // stride
  1130. const int p0; // padding
  1131. const int d0; // dilation
  1132. std::string vars() override {
  1133. return VARS_TO_STR5(ne_input, ne_kernel, s0, p0, d0);
  1134. }
  1135. test_conv_transpose_1d(std::array<int64_t, 4> ne_input = {197, 32, 1, 1}, // [input_width, input_height, input_channels, 1]
  1136. std::array<int64_t, 4> ne_kernel = {16, 32, 32, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1137. int s0 = 1, int p0 = 0, int d0 = 1)
  1138. : ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), p0(p0), d0(d0) {}
  1139. ggml_tensor * build_graph(ggml_context * ctx) override {
  1140. ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data());
  1141. ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data());
  1142. ggml_tensor * out = ggml_conv_transpose_1d(ctx, kernel, input, s0, p0, d0);
  1143. return out;
  1144. }
  1145. };
  1146. // GGML_OP_IM2COL
  1147. struct test_im2col : public test_case {
  1148. const ggml_type type_input;
  1149. const ggml_type type_kernel;
  1150. const ggml_type dst_type;
  1151. const std::array<int64_t, 4> ne_input;
  1152. const std::array<int64_t, 4> ne_kernel;
  1153. // stride
  1154. const int s0;
  1155. const int s1;
  1156. // padding
  1157. const int p0;
  1158. const int p1;
  1159. // dilation
  1160. const int d0;
  1161. const int d1;
  1162. // mode
  1163. const bool is_2D;
  1164. std::string vars() override {
  1165. return VARS_TO_STR12(type_input, type_kernel, dst_type, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  1166. }
  1167. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32,
  1168. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1169. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1170. int s0 = 1, int s1 = 1,
  1171. int p0 = 1, int p1 = 1,
  1172. int d0 = 1, int d1 = 1,
  1173. bool is_2D = true)
  1174. : type_input(type_input), type_kernel(type_kernel), dst_type(dst_type), ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), s1(s1), p0(p0), p1(p1), d0(d0), d1(d1), is_2D(is_2D) {}
  1175. ggml_tensor * build_graph(ggml_context * ctx) override {
  1176. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1177. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  1178. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D, dst_type);
  1179. return out;
  1180. }
  1181. };
  1182. // GGML_OP_CONCAT
  1183. struct test_concat : public test_case {
  1184. const ggml_type type;
  1185. const std::array<int64_t, 4> ne_a;
  1186. const int64_t ne_b_d;
  1187. const int dim;
  1188. const int v; // view (1 << 0: non-cont a, 1 << 1: non-cont b)
  1189. std::string vars() override {
  1190. return VARS_TO_STR5(type, ne_a, ne_b_d, dim, v);
  1191. }
  1192. test_concat(ggml_type type = GGML_TYPE_F32,
  1193. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1194. int64_t ne_b_d = 10,
  1195. int dim = 2, int v = 0)
  1196. : type(type), ne_a(ne_a), ne_b_d(ne_b_d), dim(dim), v(v) {}
  1197. ggml_tensor * build_graph(ggml_context * ctx) override {
  1198. auto ne_b = ne_a;
  1199. ne_b[dim] = ne_b_d;
  1200. ggml_tensor * a;
  1201. if (v & 1) {
  1202. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1203. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1204. a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
  1205. } else {
  1206. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1207. }
  1208. ggml_tensor * b;
  1209. if (v & 2) {
  1210. auto ne = ne_b; ne[0] *= 3; ne[1] *= 2; ne[2] *= 4;
  1211. b = ggml_new_tensor(ctx, type, 4, ne.data());
  1212. b = ggml_view_4d(ctx, b, ne_b[0], ne_b[1], ne_b[2], ne_b[3], b->nb[1], b->nb[2], b->nb[3], 0);
  1213. } else {
  1214. b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1215. }
  1216. ggml_tensor * out = ggml_concat(ctx, a, b, dim);
  1217. return out;
  1218. }
  1219. };
  1220. // GGML_OP_ARGSORT
  1221. struct test_argsort : public test_case {
  1222. const ggml_type type;
  1223. const std::array<int64_t, 4> ne;
  1224. ggml_sort_order order;
  1225. std::string vars() override {
  1226. return VARS_TO_STR3(type, ne, order);
  1227. }
  1228. test_argsort(ggml_type type = GGML_TYPE_F32,
  1229. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  1230. ggml_sort_order order = GGML_SORT_ORDER_ASC)
  1231. : type(type), ne(ne), order(order) {}
  1232. ggml_tensor * build_graph(ggml_context * ctx) override {
  1233. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1234. ggml_tensor * out = ggml_argsort(ctx, a, order);
  1235. return out;
  1236. }
  1237. void initialize_tensors(ggml_context * ctx) override {
  1238. std::random_device rd;
  1239. std::default_random_engine rng(rd());
  1240. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1241. if (t->type == GGML_TYPE_I32) {
  1242. // indices
  1243. std::vector<int> data(ggml_nelements(t));
  1244. for (int i = 0; i < ggml_nelements(t); i++) {
  1245. data[i] = rand();
  1246. }
  1247. std::shuffle(data.begin(), data.end(), rng);
  1248. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  1249. } else if (t->type == GGML_TYPE_F32) {
  1250. // initialize with unique values to avoid ties
  1251. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1252. std::vector<float> data(t->ne[0]);
  1253. for (int i = 0; i < t->ne[0]; i++) {
  1254. data[i] = i;
  1255. }
  1256. std::shuffle(data.begin(), data.end(), rng);
  1257. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  1258. }
  1259. } else {
  1260. GGML_ABORT("fatal error");
  1261. }
  1262. }
  1263. }
  1264. };
  1265. // GGML_OP_SUM_ROWS
  1266. struct test_sum_rows : public test_case {
  1267. const ggml_type type;
  1268. const std::array<int64_t, 4> ne;
  1269. std::string vars() override {
  1270. return VARS_TO_STR2(type, ne);
  1271. }
  1272. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1273. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  1274. : type(type), ne(ne) {}
  1275. ggml_tensor * build_graph(ggml_context * ctx) override {
  1276. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1277. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1278. return out;
  1279. }
  1280. };
  1281. // GGML_OP_UPSCALE
  1282. struct test_upscale : public test_case {
  1283. const ggml_type type;
  1284. const std::array<int64_t, 4> ne;
  1285. const int32_t scale_factor;
  1286. const bool transpose;
  1287. std::string vars() override {
  1288. return VARS_TO_STR4(type, ne, scale_factor, transpose);
  1289. }
  1290. test_upscale(ggml_type type = GGML_TYPE_F32,
  1291. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1292. int32_t scale_factor = 2, bool transpose = false)
  1293. : type(type), ne(ne), scale_factor(scale_factor), transpose(transpose) {}
  1294. ggml_tensor * build_graph(ggml_context * ctx) override {
  1295. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1296. if (transpose) a = ggml_transpose(ctx, a);
  1297. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1298. return out;
  1299. }
  1300. };
  1301. // GGML_OP_UPSCALE (ext)
  1302. struct test_upscale_ext : public test_case {
  1303. const ggml_type type;
  1304. const std::array<int64_t, 4> ne;
  1305. const std::array<int64_t, 4> ne_tgt;
  1306. std::string vars() override {
  1307. return VARS_TO_STR3(type, ne, ne_tgt);
  1308. }
  1309. test_upscale_ext(ggml_type type = GGML_TYPE_F32,
  1310. std::array<int64_t, 4> ne = {2, 5, 7, 11},
  1311. std::array<int64_t, 4> ne_tgt = {5, 7, 11, 13})
  1312. : type(type), ne(ne), ne_tgt(ne_tgt) {}
  1313. ggml_tensor * build_graph(ggml_context * ctx) override {
  1314. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1315. ggml_tensor * out = ggml_upscale_ext(ctx, a, ne_tgt[0], ne_tgt[1],ne_tgt[2], ne_tgt[3]);
  1316. return out;
  1317. }
  1318. };
  1319. // GGML_OP_GROUP_NORM
  1320. struct test_group_norm : public test_case {
  1321. const ggml_type type;
  1322. const std::array<int64_t, 4> ne;
  1323. const int32_t num_groups;
  1324. const float eps;
  1325. std::string vars() override {
  1326. return VARS_TO_STR3(type, ne, num_groups);
  1327. }
  1328. test_group_norm(ggml_type type = GGML_TYPE_F32,
  1329. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  1330. int32_t num_groups = 32,
  1331. float eps = 1e-6f)
  1332. : type(type), ne(ne), num_groups(num_groups), eps(eps) {}
  1333. ggml_tensor * build_graph(ggml_context * ctx) override {
  1334. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1335. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups, eps);
  1336. return out;
  1337. }
  1338. };
  1339. // GGML_OP_ACC
  1340. struct test_acc : public test_case {
  1341. const ggml_type type;
  1342. const std::array<int64_t, 4> ne_a;
  1343. const std::array<int64_t, 4> ne_b;
  1344. std::string vars() override {
  1345. return VARS_TO_STR3(type, ne_a, ne_b);
  1346. }
  1347. test_acc(ggml_type type = GGML_TYPE_F32,
  1348. std::array<int64_t, 4> ne_a = {1024, 577, 1, 1},
  1349. std::array<int64_t, 4> ne_b = {1024, 576, 1, 1})
  1350. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1351. ggml_tensor * build_graph(ggml_context * ctx) override {
  1352. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1353. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1354. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  1355. return out;
  1356. }
  1357. };
  1358. // GGML_OP_PAD
  1359. struct test_pad : public test_case {
  1360. const ggml_type type;
  1361. const std::array<int64_t, 4> ne_a;
  1362. const int pad_0;
  1363. const int pad_1;
  1364. std::string vars() override {
  1365. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  1366. }
  1367. test_pad(ggml_type type = GGML_TYPE_F32,
  1368. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  1369. int pad_0 = 1, int pad_1 = 1)
  1370. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  1371. ggml_tensor * build_graph(ggml_context * ctx) override {
  1372. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1373. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  1374. return out;
  1375. }
  1376. };
  1377. // GGML_OP_ARANGE
  1378. struct test_arange : public test_case {
  1379. const ggml_type type;
  1380. const float start;
  1381. const float stop;
  1382. const float step;
  1383. std::string vars() override {
  1384. return VARS_TO_STR4(type, start, stop, step);
  1385. }
  1386. test_arange(ggml_type type = GGML_TYPE_F32,
  1387. float start = 0.f, float stop = 10.f, float step = 1.f)
  1388. : type(type), start(start), stop(stop), step(step) {}
  1389. ggml_tensor * build_graph(ggml_context * ctx) override {
  1390. ggml_tensor * out = ggml_arange(ctx, start, stop, step);
  1391. return out;
  1392. }
  1393. };
  1394. // GGML_OP_TIMESTEP_EMBEDDING
  1395. struct test_timestep_embedding : public test_case {
  1396. const ggml_type type;
  1397. const std::array<int64_t, 4> ne_a;
  1398. const int dim;
  1399. const int max_period;
  1400. std::string vars() override {
  1401. return VARS_TO_STR4(type, ne_a, dim, max_period);
  1402. }
  1403. test_timestep_embedding(ggml_type type = GGML_TYPE_F32,
  1404. std::array<int64_t, 4> ne_a = {2, 1, 1, 1},
  1405. int dim = 320, int max_period=10000)
  1406. : type(type), ne_a(ne_a), dim(dim), max_period(max_period) {}
  1407. ggml_tensor * build_graph(ggml_context * ctx) override {
  1408. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1409. ggml_tensor * out = ggml_timestep_embedding(ctx, a, dim, max_period);
  1410. return out;
  1411. }
  1412. };
  1413. // GGML_OP_LEAKY_RELU
  1414. struct test_leaky_relu : public test_case {
  1415. const ggml_type type;
  1416. const std::array<int64_t, 4> ne_a;
  1417. const float negative_slope;
  1418. std::string vars() override {
  1419. return VARS_TO_STR3(type, ne_a, negative_slope);
  1420. }
  1421. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  1422. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1423. float negative_slope = 0.1f)
  1424. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  1425. ggml_tensor * build_graph(ggml_context * ctx) override {
  1426. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1427. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  1428. return out;
  1429. }
  1430. };
  1431. // GGML_OP_FLASH_ATTN_EXT
  1432. struct test_flash_attn_ext : public test_case {
  1433. const int64_t hs; // head size
  1434. const int64_t nh; // num heads
  1435. const int64_t kv; // kv size
  1436. const int64_t nb; // batch size
  1437. const bool mask; // use mask
  1438. const float max_bias; // ALiBi
  1439. const float logit_softcap; // Gemma 2
  1440. const ggml_type type_KV;
  1441. std::string vars() override {
  1442. return VARS_TO_STR8(hs, nh, kv, nb, mask, max_bias, logit_softcap, type_KV);
  1443. }
  1444. double max_nmse_err() override {
  1445. return 5e-4;
  1446. }
  1447. test_flash_attn_ext(int64_t hs = 128, int64_t nh = 32, int64_t kv = 96, int64_t nb = 8, bool mask = true, float max_bias = 0.0f, float logit_softcap = 0.0f, ggml_type type_KV = GGML_TYPE_F16)
  1448. : hs(hs), nh(nh), kv(kv), nb(nb), mask(mask), max_bias(max_bias), logit_softcap(logit_softcap), type_KV(type_KV) {}
  1449. ggml_tensor * build_graph(ggml_context * ctx) override {
  1450. const int64_t hs_padded = GGML_PAD(hs, ggml_blck_size(type_KV));
  1451. ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs_padded, nb, nh, 1);
  1452. ggml_tensor * k = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  1453. ggml_tensor * v = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  1454. ggml_tensor * m = mask ? ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, GGML_PAD(nb, GGML_KQ_MASK_PAD), 1, 1) : nullptr;
  1455. ggml_tensor * out = ggml_flash_attn_ext(ctx, q, k, v, m, 1.0f/sqrtf(hs), max_bias, logit_softcap);
  1456. return out;
  1457. }
  1458. };
  1459. enum llm_norm_type {
  1460. LLM_NORM,
  1461. LLM_NORM_RMS,
  1462. };
  1463. struct llama_hparams {
  1464. uint32_t n_vocab;
  1465. uint32_t n_embd;
  1466. uint32_t n_head;
  1467. uint32_t n_head_kv;
  1468. static constexpr uint32_t n_layer = 1;
  1469. uint32_t n_rot;
  1470. uint32_t n_embd_head; // dimension of values (d_v)
  1471. uint32_t n_ff;
  1472. float f_norm_eps;
  1473. float f_norm_rms_eps;
  1474. // cparams
  1475. static constexpr uint32_t n_ctx = 512; // user-specified context size
  1476. static constexpr uint32_t n_ctx_orig = n_ctx;
  1477. // batch
  1478. int32_t n_tokens;
  1479. // llm_build_context
  1480. static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx
  1481. static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache
  1482. uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads
  1483. return n_embd_head * n_head_kv;
  1484. }
  1485. };
  1486. // LLM base class
  1487. struct test_llm : public test_case {
  1488. llama_hparams hp;
  1489. protected:
  1490. test_llm(llama_hparams hp)
  1491. : hp(std::move(hp)) {
  1492. }
  1493. public:
  1494. struct ggml_tensor * llm_build_norm(
  1495. struct ggml_context * ctx,
  1496. struct ggml_tensor * cur,
  1497. struct ggml_tensor * mw,
  1498. struct ggml_tensor * mb,
  1499. llm_norm_type type) {
  1500. switch (type) {
  1501. case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break;
  1502. case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break;
  1503. }
  1504. cur = ggml_mul(ctx, cur, mw);
  1505. if (mb) {
  1506. cur = ggml_add(ctx, cur, mb);
  1507. }
  1508. return cur;
  1509. }
  1510. void llm_build_kv_store(
  1511. struct ggml_context * ctx,
  1512. struct ggml_tensor * k_l,
  1513. struct ggml_tensor * v_l,
  1514. struct ggml_tensor * k_cur,
  1515. struct ggml_tensor * v_cur) {
  1516. // compute the transposed [n_tokens, n_embd] V matrix
  1517. struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens));
  1518. struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(),
  1519. (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head);
  1520. struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(),
  1521. ( hp.n_ctx)*ggml_element_size(v_l),
  1522. (hp.kv_head)*ggml_element_size(v_l));
  1523. // important: storing RoPE-ed version of K in the KV cache!
  1524. ggml_cpy(ctx, k_cur, k_cache_view);
  1525. ggml_cpy(ctx, v_cur_t, v_cache_view);
  1526. }
  1527. struct ggml_tensor * llm_build_kqv(
  1528. struct ggml_context * ctx,
  1529. struct ggml_tensor * k_l,
  1530. struct ggml_tensor * v_l,
  1531. struct ggml_tensor * q_cur,
  1532. struct ggml_tensor * kq_mask,
  1533. float kq_scale) {
  1534. struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3);
  1535. struct ggml_tensor * k =
  1536. ggml_view_3d(ctx, k_l,
  1537. hp.n_embd_head, hp.n_kv, hp.n_head_kv,
  1538. ggml_row_size(k_l->type, hp.n_embd_gqa()),
  1539. ggml_row_size(k_l->type, hp.n_embd_head),
  1540. 0);
  1541. struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q);
  1542. kq = ggml_soft_max_ext(ctx, kq, kq_mask, kq_scale, 0.0f);
  1543. // split cached v into n_head heads
  1544. struct ggml_tensor * v =
  1545. ggml_view_3d(ctx, v_l,
  1546. hp.n_kv, hp.n_embd_head, hp.n_head_kv,
  1547. ggml_element_size(v_l)*hp.n_ctx,
  1548. ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head,
  1549. 0);
  1550. struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq);
  1551. struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3);
  1552. struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens);
  1553. struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1554. cur = ggml_mul_mat(ctx, wo, cur);
  1555. return cur;
  1556. }
  1557. void initialize_tensors(ggml_context * ctx) override {
  1558. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1559. if (t->type == GGML_TYPE_I32) {
  1560. // pos
  1561. std::vector<int> data(hp.n_tokens);
  1562. for (int i = 0; i < hp.n_tokens; i++) {
  1563. data[i] = rand() % hp.n_ctx;
  1564. }
  1565. ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int));
  1566. } else {
  1567. init_tensor_uniform(t);
  1568. }
  1569. }
  1570. }
  1571. };
  1572. // Llama
  1573. struct test_llama : public test_llm {
  1574. static constexpr float freq_base = 10000.0f;
  1575. static constexpr float freq_scale = 1.0f;
  1576. static constexpr float ext_factor = 0.0f;
  1577. static constexpr float attn_factor = 1.0f;
  1578. static constexpr float beta_fast = 32.0f;
  1579. static constexpr float beta_slow = 1.0f;
  1580. std::string op_desc(ggml_tensor * t) override {
  1581. GGML_UNUSED(t);
  1582. return "LLAMA";
  1583. }
  1584. std::string vars() override {
  1585. auto n_tokens = hp.n_tokens;
  1586. return VARS_TO_STR1(n_tokens);
  1587. }
  1588. double max_nmse_err() override {
  1589. return 2e-3;
  1590. }
  1591. test_llama(int n_tokens = 1)
  1592. : test_llm({
  1593. /*n_vocab =*/ 32000,
  1594. /*n_embd =*/ 3200,
  1595. /*n_head =*/ 32,
  1596. /*n_head_kv =*/ 32,
  1597. /*n_rot =*/ 100,
  1598. /*n_embd_head =*/ 100,
  1599. /*n_ff =*/ 8640,
  1600. /*f_norm_eps =*/ 0.f,
  1601. /*f_norm_rms_eps =*/ 1e-5f,
  1602. /*n_tokens =*/ n_tokens,
  1603. }) {
  1604. }
  1605. ggml_tensor * build_graph(ggml_context * ctx) override {
  1606. struct ggml_tensor * cur;
  1607. struct ggml_tensor * inpL;
  1608. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1609. // inp_pos - contains the positions
  1610. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1611. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1612. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  1613. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1614. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1615. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1616. struct ggml_tensor * inpSA = inpL;
  1617. // norm
  1618. ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1619. cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS);
  1620. // self-attention
  1621. {
  1622. ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1623. ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1624. ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1625. // compute Q and K and RoPE them
  1626. struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur);
  1627. struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur);
  1628. struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur);
  1629. Qcur = ggml_rope_ext(
  1630. ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos, nullptr,
  1631. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  1632. ext_factor, attn_factor, beta_fast, beta_slow
  1633. );
  1634. Kcur = ggml_rope_ext(
  1635. ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos, nullptr,
  1636. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  1637. ext_factor, attn_factor, beta_fast, beta_slow
  1638. );
  1639. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1640. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1641. }
  1642. struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA);
  1643. // feed-forward network
  1644. ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1645. cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS);
  1646. ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1647. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1648. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1649. struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur);
  1650. cur = ggml_mul_mat(ctx, ffn_gate, cur);
  1651. cur = ggml_silu(ctx, cur);
  1652. cur = ggml_mul(ctx, cur, tmp);
  1653. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1654. cur = ggml_add(ctx, cur, ffn_inp);
  1655. // input for next layer
  1656. inpL = cur;
  1657. }
  1658. cur = inpL;
  1659. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1660. cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS);
  1661. // lm_head
  1662. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab);
  1663. cur = ggml_mul_mat(ctx, output, cur);
  1664. return cur;
  1665. }
  1666. };
  1667. // Falcon
  1668. struct test_falcon : public test_llm {
  1669. static constexpr float freq_base = 10000.0f;
  1670. static constexpr float freq_scale = 1.0f;
  1671. static constexpr float ext_factor = 0.0f;
  1672. static constexpr float attn_factor = 1.0f;
  1673. static constexpr float beta_fast = 32.0f;
  1674. static constexpr float beta_slow = 1.0f;
  1675. std::string op_desc(ggml_tensor * t) override {
  1676. GGML_UNUSED(t);
  1677. return "FALCON";
  1678. }
  1679. std::string vars() override {
  1680. auto n_tokens = hp.n_tokens;
  1681. return VARS_TO_STR1(n_tokens);
  1682. }
  1683. double max_nmse_err() override {
  1684. return 2e-3;
  1685. }
  1686. test_falcon(int n_tokens = 1)
  1687. : test_llm({
  1688. /*n_vocab =*/ 32000,
  1689. /*n_embd =*/ 3200,
  1690. /*n_head =*/ 50,
  1691. /*n_head_kv =*/ 1,
  1692. /*n_rot =*/ 64,
  1693. /*n_embd_head =*/ 64,
  1694. /*n_ff =*/ 8640,
  1695. /*f_norm_eps =*/ 1e-5f,
  1696. /*f_norm_rms_eps =*/ 0.f,
  1697. /*n_tokens =*/ n_tokens,
  1698. }) {
  1699. }
  1700. ggml_tensor * build_graph(ggml_context * ctx) override {
  1701. struct ggml_tensor * cur;
  1702. struct ggml_tensor * inpL;
  1703. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1704. // inp_pos - contains the positions
  1705. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1706. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1707. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  1708. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1709. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1710. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1711. // norm
  1712. ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1713. ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1714. ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM);
  1715. // self-attention
  1716. {
  1717. cur = attn_norm;
  1718. ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa());
  1719. cur = ggml_mul_mat(ctx, wqkv, cur);
  1720. struct ggml_tensor * Qcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd, hp.n_tokens, cur->nb[1], 0*sizeof(float)*(hp.n_embd)));
  1721. struct ggml_tensor * Kcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd_gqa(), hp.n_tokens, cur->nb[1], 1*sizeof(float)*(hp.n_embd)));
  1722. struct ggml_tensor * Vcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd_gqa(), hp.n_tokens, cur->nb[1], 1*sizeof(float)*(hp.n_embd + hp.n_embd_gqa())));
  1723. Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens);
  1724. Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens);
  1725. // using mode = 2 for neox mode
  1726. Qcur = ggml_rope_ext(
  1727. ctx, Qcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  1728. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1729. );
  1730. Kcur = ggml_rope_ext(
  1731. ctx, Kcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  1732. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1733. );
  1734. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1735. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1736. }
  1737. struct ggml_tensor * ffn_inp = cur;
  1738. // feed forward
  1739. {
  1740. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1741. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1742. cur = attn_norm;
  1743. cur = ggml_mul_mat(ctx, ffn_up, cur);
  1744. cur = ggml_gelu(ctx, cur);
  1745. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1746. }
  1747. cur = ggml_add(ctx, cur, ffn_inp);
  1748. cur = ggml_add(ctx, cur, inpL);
  1749. // input for next layer
  1750. inpL = cur;
  1751. }
  1752. cur = inpL;
  1753. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1754. ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1755. cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM);
  1756. // lm_head
  1757. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab);
  1758. cur = ggml_mul_mat(ctx, output, cur);
  1759. return cur;
  1760. }
  1761. };
  1762. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  1763. std::vector<std::unique_ptr<test_case>> test_cases;
  1764. std::default_random_engine rng(0);
  1765. const ggml_type all_types[] = {
  1766. GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16,
  1767. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  1768. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1769. GGML_TYPE_Q8_0,
  1770. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1771. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  1772. GGML_TYPE_Q6_K,
  1773. GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  1774. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  1775. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  1776. };
  1777. const ggml_type base_types[] = {
  1778. GGML_TYPE_F32, GGML_TYPE_F16,
  1779. GGML_TYPE_Q4_0,
  1780. GGML_TYPE_Q4_K,
  1781. GGML_TYPE_IQ2_XXS
  1782. };
  1783. const ggml_type other_types[] = {
  1784. GGML_TYPE_Q4_1,
  1785. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1786. GGML_TYPE_Q8_0,
  1787. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1788. GGML_TYPE_Q5_K,
  1789. GGML_TYPE_Q6_K,
  1790. GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  1791. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  1792. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  1793. GGML_TYPE_BF16,
  1794. };
  1795. // unary ops
  1796. for (int v : {0, 1}) {
  1797. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  1798. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 128, 10, 10, 10 }, v));
  1799. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 7, 13, 19, 23 }, v));
  1800. }
  1801. }
  1802. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  1803. for (ggml_type type : all_types) {
  1804. for (int b : {1, 7}) {
  1805. for (bool v : {false, true}) {
  1806. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  1807. }
  1808. }
  1809. }
  1810. for (int b : {1, 7}) {
  1811. for (bool v : {false, true}) {
  1812. test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, v));
  1813. }
  1814. }
  1815. for (ggml_type type_input : {GGML_TYPE_F32}) {
  1816. for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) {
  1817. for (int k0 : {1, 3}) {
  1818. for (int k1 : {1, 3}) {
  1819. for (int s0 : {1, 2}) {
  1820. for (int s1 : {1, 2}) {
  1821. for (int p0 : {0, 1}) {
  1822. for (int p1 : {0, 1}) {
  1823. test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1));
  1824. }
  1825. }
  1826. }
  1827. }
  1828. }
  1829. }
  1830. }
  1831. }
  1832. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32));
  1833. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16));
  1834. // test cases for 1D im2col
  1835. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {3000, 128, 1, 1}, {3, 128, 1280, 1}, 1, 0, 1, 0, 1, 0, false));
  1836. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {3000, 128, 1, 1}, {3, 128, 1280, 1}, 1, 0, 1, 0, 1, 0, false));
  1837. // sycl backend will limit task global_range < MAX_INT
  1838. // test cases for 2D im2col with large input W and H (occurs in stable-diffusion)
  1839. // however these cases need to alloc more memory which may fail in some devices (Intel Arc770, etc.)
  1840. // these cases are verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend)
  1841. // test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {1024, 1024, 256, 1}, {3, 3, 256, 1}, 1, 1, 1, 1, 1, 1, true));
  1842. // test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {1024, 1024, 256, 1}, {3, 3, 256, 1}, 1, 1, 1, 1, 1, 1, true));
  1843. test_cases.emplace_back(new test_conv_transpose_1d());
  1844. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 3, 0, 1));
  1845. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 2, 0, 1));
  1846. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 1, 0, 1));
  1847. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 2, 0, 1));
  1848. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 1, 0, 1));
  1849. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,1,2,1}, 1, 0, 1));
  1850. test_cases.emplace_back(new test_conv_transpose_1d({2,1,1,1}, {3,1,1,1}, 1, 0, 1));
  1851. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 1}));
  1852. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1853. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 2, 1, 1}));
  1854. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 2, 1}));
  1855. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1856. test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1857. test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1858. test_cases.emplace_back(new test_dup(GGML_TYPE_F32));
  1859. test_cases.emplace_back(new test_dup(GGML_TYPE_F16));
  1860. test_cases.emplace_back(new test_dup(GGML_TYPE_I32));
  1861. test_cases.emplace_back(new test_dup(GGML_TYPE_I16));
  1862. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {0, 2, 1, 3}));
  1863. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {0, 2, 1, 3})); // dup by rows
  1864. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {1, 0, 2, 3}));
  1865. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {1, 0, 2, 3})); // dup dst not-contiguous
  1866. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3}));
  1867. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3}));
  1868. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1869. for (ggml_type type_dst : all_types) {
  1870. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4}));
  1871. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {0, 2, 1, 3})); // cpy by rows
  1872. }
  1873. }
  1874. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1875. for (ggml_type type_dst : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1876. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {1, 0, 2, 3})); // cpy not-contiguous
  1877. }
  1878. }
  1879. test_cases.emplace_back(new test_cont());
  1880. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  1881. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  1882. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  1883. }
  1884. };
  1885. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  1886. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  1887. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  1888. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 1, 1}, {1, 1, 1, 1});
  1889. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 1}, {1, 1, 1, 1});
  1890. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 1});
  1891. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 1, 1, 1});
  1892. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 1, 1});
  1893. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 1});
  1894. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 2});
  1895. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 2});
  1896. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 2, 2});
  1897. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 2, 2, 2});
  1898. // stable diffusion
  1899. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  1900. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  1901. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  1902. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  1903. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  1904. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  1905. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  1906. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  1907. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  1908. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  1909. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  1910. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  1911. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  1912. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  1913. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  1914. test_cases.emplace_back(new test_scale());
  1915. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  1916. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1917. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1918. }
  1919. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 1536, 1, 1}, {4, 1536, 1, 1}));
  1920. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {8, 1536, 1, 1}, {4, 1536, 1, 1}));
  1921. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 1536, 4, 1}, {4, 1536, 1, 1}));
  1922. test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 16, 1024, 32, 4));
  1923. #if 1
  1924. for (ggml_type type_a : base_types) {
  1925. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1926. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1927. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  1928. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  1929. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  1930. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  1931. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  1932. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  1933. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  1934. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  1935. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  1936. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  1937. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  1938. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  1939. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  1940. }
  1941. }
  1942. #else
  1943. // m = a rows
  1944. // n = b rows
  1945. // k = cols
  1946. std::uniform_int_distribution<> dist_m(1, 128);
  1947. std::uniform_int_distribution<> dist_n(16, 128);
  1948. std::uniform_int_distribution<> dist_k(1, 16);
  1949. for (int i = 0; i < 1000; i++) {
  1950. for (ggml_type type_a : all_types) {
  1951. for (ggml_type type_b : {GGML_TYPE_F32}) {
  1952. int m = dist_m(rng);
  1953. int n = dist_n(rng);
  1954. int k = dist_k(rng) * ggml_blck_size(type_a);
  1955. test_cases.emplace_back(new test_mul_mat(type_a, type_b, m, n, k, { 1, 1}, {1, 1}));
  1956. }
  1957. }
  1958. }
  1959. #endif
  1960. for (ggml_type type_a : other_types) {
  1961. for (ggml_type type_b : {GGML_TYPE_F32}) {
  1962. if (ggml_blck_size(type_a) != 256) {
  1963. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, ggml_blck_size(type_a), {1, 1}, {1, 1}));
  1964. }
  1965. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {1, 1}, {1, 1}));
  1966. }
  1967. }
  1968. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 128, { 8, 1}, {1, 1}));
  1969. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 128, { 8, 1}, {4, 1}));
  1970. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 64, { 8, 1}, {4, 1}));
  1971. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 64, { 8, 1}, {4, 1}));
  1972. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 45, 128, { 8, 1}, {4, 1}));
  1973. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 128, 45, 64, { 8, 1}, {4, 1}));
  1974. // sycl backend will limit task global_range < MAX_INT
  1975. // test case for f16-type-convert-to-fp32 kernel with large k under fp32 compute dtype (occurs in stable-diffusion)
  1976. // however this case needs to alloc more memory which may fail in some devices (Intel Arc770, etc.)
  1977. // this case is verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend)
  1978. // test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 512, 262144, 9216, {1, 1}, {1, 1}));
  1979. for (ggml_type type_a : base_types) {
  1980. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1981. for (int n_mats : {4, 8}) {
  1982. for (int n_used : {1, 2, 4}) {
  1983. for (bool b : {false, true}) {
  1984. for (int n : {1, 32}) {
  1985. int m = 512;
  1986. int k = 256;
  1987. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  1988. }
  1989. }
  1990. }
  1991. }
  1992. }
  1993. }
  1994. for (ggml_type type_a : other_types) {
  1995. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1996. for (int n_mats : {4}) {
  1997. for (int n_used : {2}) {
  1998. for (bool b : {false}) {
  1999. for (int n : {1}) {
  2000. int m = 512;
  2001. int k = 256;
  2002. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  2003. }
  2004. }
  2005. }
  2006. }
  2007. }
  2008. }
  2009. test_cases.emplace_back(new test_sqr());
  2010. test_cases.emplace_back(new test_sqrt());
  2011. test_cases.emplace_back(new test_clamp());
  2012. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  2013. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 1}, 5));
  2014. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 10}, 5));
  2015. #if 0
  2016. std::uniform_int_distribution<> dist_ne1(1, 50);
  2017. int exponent = 1;
  2018. while (exponent < (1 << 17)) {
  2019. std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent);
  2020. for (int n = 0; n < 10; ++n) {
  2021. int64_t ne0 = dist_ne0(rng);
  2022. int64_t ne1 = dist_ne1(rng);
  2023. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, GGML_TYPE_F32, {ne0, ne1, 1, 1}, n/2 == 0, 0.1f, ne0 < 1000 ? 4.0f : 0.0f));
  2024. }
  2025. exponent <<= 1;
  2026. }
  2027. #endif
  2028. for (bool mask : {false, true}) {
  2029. for (float max_bias : {0.0f, 8.0f}) {
  2030. if (!mask && max_bias > 0.0f) continue;
  2031. for (float scale : {1.0f, 0.1f}) {
  2032. for (int64_t ne0 : {16, 1024}) {
  2033. for (int64_t ne1 : {16, 1024}) {
  2034. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, scale, max_bias));
  2035. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, scale, max_bias));
  2036. }
  2037. }
  2038. }
  2039. }
  2040. }
  2041. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, true, 0.1f, 0.0f));
  2042. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 0.0f));
  2043. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 0.0f));
  2044. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 8.0f));
  2045. {
  2046. bool all = true;
  2047. for (float v : { 0, 1 }) {
  2048. for (float fs : { 1.0f, 1.4245f }) {
  2049. for (float ef : { 0.0f, 0.7465f }) {
  2050. for (float af : { 1.0f, 1.4245f }) {
  2051. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  2052. for (bool ff : {false, true}) { // freq_factors
  2053. test_cases.emplace_back(new test_rope(type, {128, 32, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 7B
  2054. if (all) {
  2055. test_cases.emplace_back(new test_rope(type, {128, 40, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 13B
  2056. test_cases.emplace_back(new test_rope(type, {128, 52, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 30B
  2057. test_cases.emplace_back(new test_rope(type, {128, 64, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 65B
  2058. }
  2059. if (all) {
  2060. test_cases.emplace_back(new test_rope(type, { 64, 1, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  2061. test_cases.emplace_back(new test_rope(type, { 64, 71, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  2062. test_cases.emplace_back(new test_rope(type, { 64, 8, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2063. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 20, 2, 512, fs, ef, af, ff, v)); // neox (stablelm)
  2064. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 32, 2, 512, fs, ef, af, ff, v)); // neox (phi-2)
  2065. }
  2066. test_cases.emplace_back(new test_rope(type, { 64, 128, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2067. }
  2068. }
  2069. all = false;
  2070. }
  2071. }
  2072. }
  2073. }
  2074. }
  2075. for (int v : { 0, 1, 2, 3 }) {
  2076. for (int dim : { 0, 1, 2, 3, }) {
  2077. test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {11, 12, 13, 14}, 7, dim, v));
  2078. test_cases.emplace_back(new test_concat(GGML_TYPE_I32, {11, 12, 13, 14}, 7, dim, v));
  2079. }
  2080. }
  2081. for (ggml_sort_order order : {GGML_SORT_ORDER_ASC, GGML_SORT_ORDER_DESC}) {
  2082. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  2083. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  2084. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {60, 10, 10, 10}, order)); // qwen
  2085. }
  2086. test_cases.emplace_back(new test_sum_rows());
  2087. test_cases.emplace_back(new test_upscale());
  2088. test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, { 512, 512, 3, 1 }, 2, true));
  2089. test_cases.emplace_back(new test_upscale_ext());
  2090. test_cases.emplace_back(new test_group_norm());
  2091. test_cases.emplace_back(new test_acc());
  2092. test_cases.emplace_back(new test_pad());
  2093. test_cases.emplace_back(new test_arange());
  2094. test_cases.emplace_back(new test_timestep_embedding());
  2095. test_cases.emplace_back(new test_leaky_relu());
  2096. for (int hs : { 64, 80, 128, 256, }) {
  2097. for (bool mask : { true, false } ) {
  2098. for (float max_bias : { 0.0f, 8.0f }) {
  2099. if (!mask && max_bias > 0.0f) continue;
  2100. for (float logit_softcap : {0.0f, 10.0f}) {
  2101. if (hs != 128 && logit_softcap != 0.0f) continue;
  2102. for (int nh : { 32, }) {
  2103. for (int kv : { 512, 1024, }) {
  2104. for (int nb : { 1, 2, 4, 8, }) {
  2105. for (ggml_type type_KV : {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
  2106. test_cases.emplace_back(new test_flash_attn_ext(hs, nh, kv, nb, mask, max_bias, logit_softcap, type_KV));
  2107. }
  2108. }
  2109. }
  2110. }
  2111. }
  2112. }
  2113. }
  2114. }
  2115. // these tests are disabled to save execution time, but they can be handy for debugging
  2116. #if 0
  2117. test_cases.emplace_back(new test_llama(1));
  2118. test_cases.emplace_back(new test_llama(2));
  2119. test_cases.emplace_back(new test_falcon(1));
  2120. test_cases.emplace_back(new test_falcon(2));
  2121. #endif
  2122. // run tests
  2123. if (mode == MODE_TEST) {
  2124. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  2125. size_t n_ok = 0;
  2126. for (auto & test : test_cases) {
  2127. if (test->eval(backend, backend_cpu, op_name)) {
  2128. n_ok++;
  2129. }
  2130. }
  2131. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  2132. ggml_backend_free(backend_cpu);
  2133. return n_ok == test_cases.size();
  2134. }
  2135. if (mode == MODE_PERF) {
  2136. for (auto & test : test_cases) {
  2137. test->eval_perf(backend, op_name);
  2138. }
  2139. return true;
  2140. }
  2141. GGML_ABORT("fatal error");
  2142. }
  2143. static void usage(char ** argv) {
  2144. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  2145. printf(" valid modes are: test (compare with CPU backend for correctness) or perf (performance evaluation)\n");
  2146. printf(" op names are as given by ggml_op_desc()\n");
  2147. }
  2148. int main(int argc, char ** argv) {
  2149. test_mode mode = MODE_TEST;
  2150. const char * op_name_filter = NULL;
  2151. const char * backend_filter = NULL;
  2152. for (int i = 1; i < argc; i++) {
  2153. if (strcmp(argv[i], "test") == 0) {
  2154. mode = MODE_TEST;
  2155. } else if (strcmp(argv[i], "perf") == 0) {
  2156. mode = MODE_PERF;
  2157. } else if (strcmp(argv[i], "-o") == 0) {
  2158. if (i + 1 < argc) {
  2159. op_name_filter = argv[++i];
  2160. } else {
  2161. usage(argv);
  2162. return 1;
  2163. }
  2164. } else if (strcmp(argv[i], "-b") == 0) {
  2165. if (i + 1 < argc) {
  2166. backend_filter = argv[++i];
  2167. } else {
  2168. usage(argv);
  2169. return 1;
  2170. }
  2171. } else {
  2172. usage(argv);
  2173. return 1;
  2174. }
  2175. }
  2176. // enumerate backends
  2177. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  2178. size_t n_ok = 0;
  2179. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  2180. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  2181. if (backend_filter != NULL && strcmp(backend_filter, ggml_backend_reg_get_name(i)) != 0) {
  2182. printf(" Skipping\n");
  2183. n_ok++;
  2184. continue;
  2185. }
  2186. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  2187. GGML_ASSERT(backend != NULL);
  2188. if (backend_filter == NULL && ggml_backend_is_cpu(backend)) {
  2189. printf(" Skipping CPU backend\n");
  2190. ggml_backend_free(backend);
  2191. n_ok++;
  2192. continue;
  2193. }
  2194. printf(" Backend name: %s\n", ggml_backend_name(backend));
  2195. bool ok = test_backend(backend, mode, op_name_filter);
  2196. printf(" Backend %s: ", ggml_backend_name(backend));
  2197. if (ok) {
  2198. printf("\033[1;32mOK\033[0m\n");
  2199. n_ok++;
  2200. } else {
  2201. printf("\033[1;31mFAIL\033[0m\n");
  2202. }
  2203. printf("\n");
  2204. ggml_backend_free(backend);
  2205. }
  2206. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  2207. if (n_ok != ggml_backend_reg_get_count()) {
  2208. printf("\033[1;31mFAIL\033[0m\n");
  2209. return 1;
  2210. }
  2211. ggml_quantize_free();
  2212. printf("\033[1;32mOK\033[0m\n");
  2213. return 0;
  2214. }