test-backend-ops.cpp 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557
  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. bool _dst_use_permute = false)
  678. : type_src(type_src), type_dst(type_dst), ne(ne), permute(permute),
  679. _src_use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {}
  680. ggml_tensor * build_graph(ggml_context * ctx) override {
  681. ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne.data());
  682. if (_src_use_permute) {
  683. src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]);
  684. }
  685. ggml_tensor* dst = ggml_new_tensor(ctx, type_dst, 4, src->ne);
  686. ggml_tensor * out = ggml_cpy(ctx, src, dst);
  687. return out;
  688. }
  689. };
  690. // GGML_OP_CONT
  691. struct test_cont : public test_case {
  692. const ggml_type type;
  693. const std::array<int64_t, 4> ne;
  694. std::string vars() override {
  695. return VARS_TO_STR2(type, ne);
  696. }
  697. test_cont(ggml_type type = GGML_TYPE_F32,
  698. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  699. : type(type), ne(ne) {}
  700. ggml_tensor * build_graph(ggml_context * ctx) override {
  701. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  702. src = ggml_transpose(ctx, src);
  703. ggml_tensor * out = ggml_cont(ctx, src);
  704. return out;
  705. }
  706. };
  707. // GGML_OP_ADD
  708. // GGML_OP_MUL
  709. // GGML_OP_DIV
  710. struct test_bin_bcast : public test_case {
  711. using op_t = ggml_tensor * (*) (ggml_context *, ggml_tensor *, ggml_tensor *);
  712. op_t op;
  713. const ggml_type type;
  714. const std::array<int64_t, 4> ne;
  715. const std::array<int, 4> nr;
  716. std::string vars() override {
  717. return VARS_TO_STR3(type, ne, nr);
  718. }
  719. size_t op_size(ggml_tensor * t) override {
  720. return ggml_nbytes(t) * 3;
  721. }
  722. test_bin_bcast(op_t op, ggml_type type = GGML_TYPE_F32,
  723. std::array<int64_t, 4> ne = {10, 10, 1, 1},
  724. std::array<int, 4> nr = {1, 2, 1, 1})
  725. : op(op), type(type), ne(ne), nr(nr) {}
  726. ggml_tensor * build_graph(ggml_context * ctx) override {
  727. 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]);
  728. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data());
  729. ggml_tensor * out = op(ctx, a, b);
  730. return out;
  731. }
  732. void initialize_tensors(ggml_context * ctx) override {
  733. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  734. if (op == ggml_div) {
  735. // avoid division by zero
  736. init_tensor_uniform(t, 1.0f, 2.0f);
  737. } else {
  738. init_tensor_uniform(t);
  739. }
  740. }
  741. }
  742. };
  743. // GGML_OP_SCALE
  744. struct test_scale : public test_case {
  745. const ggml_type type;
  746. const std::array<int64_t, 4> ne;
  747. float scale;
  748. std::string vars() override {
  749. return VARS_TO_STR3(type, ne, scale);
  750. }
  751. test_scale(ggml_type type = GGML_TYPE_F32,
  752. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  753. float scale = 2.0f)
  754. : type(type), ne(ne), scale(scale) {}
  755. ggml_tensor * build_graph(ggml_context * ctx) override {
  756. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  757. ggml_tensor * out = ggml_scale(ctx, a, scale);
  758. return out;
  759. }
  760. };
  761. // GGML_OP_NORM
  762. struct test_norm : public test_case {
  763. const ggml_type type;
  764. const std::array<int64_t, 4> ne;
  765. float eps;
  766. std::string vars() override {
  767. return VARS_TO_STR3(type, ne, eps);
  768. }
  769. test_norm(ggml_type type = GGML_TYPE_F32,
  770. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  771. float eps = 1e-6f)
  772. : type(type), ne(ne), eps(eps) {}
  773. ggml_tensor * build_graph(ggml_context * ctx) override {
  774. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  775. ggml_tensor * out = ggml_norm(ctx, a, eps);
  776. return out;
  777. }
  778. };
  779. // GGML_OP_RMS_NORM
  780. struct test_rms_norm : public test_case {
  781. const ggml_type type;
  782. const std::array<int64_t, 4> ne;
  783. float eps;
  784. std::string vars() override {
  785. return VARS_TO_STR3(type, ne, eps);
  786. }
  787. test_rms_norm(ggml_type type = GGML_TYPE_F32,
  788. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  789. float eps = 1e-6f)
  790. : type(type), ne(ne), eps(eps) {}
  791. ggml_tensor * build_graph(ggml_context * ctx) override {
  792. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  793. ggml_tensor * out = ggml_rms_norm(ctx, a, eps);
  794. return out;
  795. }
  796. };
  797. // GGML_OP_MUL_MAT
  798. struct test_mul_mat : public test_case {
  799. const ggml_type type_a;
  800. const ggml_type type_b;
  801. const int64_t m;
  802. const int64_t n;
  803. const int64_t k;
  804. const std::array<int64_t, 2> bs; // dims 3 and 4
  805. const std::array<int64_t, 2> nr; // repeat in dims 3 and 4
  806. std::string vars() override {
  807. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, nr);
  808. }
  809. double max_nmse_err() override {
  810. return 5e-4;
  811. }
  812. size_t op_size(ggml_tensor * t) override {
  813. size_t a = ggml_nbytes(t->src[0]) * n * nr[0] * nr[1];
  814. size_t b = ggml_nbytes(t->src[1]) * m;
  815. size_t c = ggml_nbytes(t);
  816. return a + b + c;
  817. GGML_UNUSED(t);
  818. }
  819. test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  820. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  821. std::array<int64_t, 2> bs = {10, 10},
  822. std::array<int64_t, 2> nr = {2, 2})
  823. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr) {}
  824. ggml_tensor * build_graph(ggml_context * ctx) override {
  825. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  826. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, k, m, bs[0] , bs[1]);
  827. ggml_tensor * b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]);
  828. ggml_tensor * out = ggml_mul_mat(ctx, a, b);
  829. return out;
  830. }
  831. };
  832. // GGML_OP_MUL_MAT_ID
  833. struct test_mul_mat_id : public test_case {
  834. const ggml_type type_a;
  835. const ggml_type type_b;
  836. const int n_mats;
  837. const int n_used;
  838. const bool b; // brodcast b matrix
  839. const int64_t m;
  840. const int64_t n;
  841. const int64_t k;
  842. std::string vars() override {
  843. return VARS_TO_STR8(type_a, type_b, n_mats, n_used, b, m, n, k);
  844. }
  845. double max_nmse_err() override {
  846. return 5e-4;
  847. }
  848. size_t op_size(ggml_tensor * t) override {
  849. size_t a = ggml_nbytes(t->src[2]) * n;
  850. size_t b = ggml_nbytes(t->src[1]) * m;
  851. size_t c = ggml_nbytes(t);
  852. return a + b + c;
  853. GGML_UNUSED(t);
  854. }
  855. test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  856. int n_mats = 8, int n_used = 2, bool b = false,
  857. int64_t m = 32, int64_t n = 32, int64_t k = 32)
  858. : type_a(type_a), type_b(type_b), n_mats(n_mats), n_used(n_used), b(b),
  859. m(m), n(n), k(k) {
  860. GGML_ASSERT(n_used <= n_mats);
  861. }
  862. ggml_tensor * build_graph(ggml_context * ctx) override {
  863. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  864. ggml_tensor * as = ggml_new_tensor_3d(ctx, type_a, k, m, n_mats);
  865. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n);
  866. if (n_used != n_mats) {
  867. ids = ggml_view_2d(ctx, ids, n_used, n, ids->nb[1], 0);
  868. }
  869. ggml_tensor * b = ggml_new_tensor_3d(ctx, type_b, k, this->b ? 1 : n_used, n);
  870. ggml_tensor * out = ggml_mul_mat_id(ctx, as, b, ids);
  871. return out;
  872. }
  873. void initialize_tensors(ggml_context * ctx) override {
  874. std::random_device rd;
  875. std::default_random_engine rng(rd());
  876. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  877. if (t->type == GGML_TYPE_I32) {
  878. if (ggml_is_view_op(t->op)) { continue; }
  879. // ids
  880. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  881. std::vector<int32_t> data(t->ne[0]);
  882. for (int i = 0; i < t->ne[0]; i++) {
  883. data[i] = i % n_mats;
  884. }
  885. std::shuffle(data.begin(), data.end(), rng);
  886. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t));
  887. }
  888. } else {
  889. init_tensor_uniform(t);
  890. }
  891. }
  892. }
  893. };
  894. // GGML_OP_SQR
  895. struct test_sqr : public test_case {
  896. const ggml_type type;
  897. const std::array<int64_t, 4> ne;
  898. std::string vars() override {
  899. return VARS_TO_STR2(type, ne);
  900. }
  901. test_sqr(ggml_type type = GGML_TYPE_F32,
  902. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  903. : type(type), ne(ne) {}
  904. ggml_tensor * build_graph(ggml_context * ctx) override {
  905. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  906. ggml_tensor * out = ggml_sqr(ctx, a);
  907. return out;
  908. }
  909. };
  910. // GGML_OP_SQRT
  911. struct test_sqrt : public test_case {
  912. const ggml_type type;
  913. const std::array<int64_t, 4> ne;
  914. std::string vars() override {
  915. return VARS_TO_STR2(type, ne);
  916. }
  917. test_sqrt(ggml_type type = GGML_TYPE_F32,
  918. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  919. : type(type), ne(ne) {}
  920. ggml_tensor * build_graph(ggml_context * ctx) override {
  921. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  922. ggml_tensor * out = ggml_sqrt(ctx, a);
  923. return out;
  924. }
  925. void initialize_tensors(ggml_context * ctx) override {
  926. // fill with positive values
  927. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  928. init_tensor_uniform(t, 0.0f, 100.0f);
  929. }
  930. }
  931. };
  932. // GGML_OP_CLAMP
  933. struct test_clamp : public test_case {
  934. const ggml_type type;
  935. const std::array<int64_t, 4> ne;
  936. float min;
  937. float max;
  938. std::string vars() override {
  939. return VARS_TO_STR4(type, ne, min, max);
  940. }
  941. test_clamp(ggml_type type = GGML_TYPE_F32,
  942. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  943. float min = -0.5f, float max = 0.5f)
  944. : type(type), ne(ne), min(min), max(max) {}
  945. ggml_tensor * build_graph(ggml_context * ctx) override {
  946. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  947. ggml_tensor * out = ggml_clamp(ctx, a, min, max);
  948. return out;
  949. }
  950. };
  951. // GGML_OP_DIAG_MASK_INF
  952. struct test_diag_mask_inf : public test_case {
  953. const ggml_type type;
  954. const std::array<int64_t, 4> ne;
  955. const int n_past;
  956. std::string vars() override {
  957. return VARS_TO_STR3(type, ne, n_past);
  958. }
  959. test_diag_mask_inf(ggml_type type = GGML_TYPE_F32,
  960. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  961. int n_past = 5)
  962. : type(type), ne(ne), n_past(n_past) {}
  963. ggml_tensor * build_graph(ggml_context * ctx) override {
  964. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  965. ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past);
  966. return out;
  967. }
  968. };
  969. // GGML_OP_SOFT_MAX
  970. struct test_soft_max : public test_case {
  971. const ggml_type type;
  972. const std::array<int64_t, 4> ne;
  973. const bool mask;
  974. const float scale;
  975. const float max_bias;
  976. std::string vars() override {
  977. return VARS_TO_STR5(type, ne, mask, scale, max_bias);
  978. }
  979. // the 1024 test with bias occasionally fails:
  980. // 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
  981. virtual double max_nmse_err() override {
  982. return 1e-6;
  983. }
  984. test_soft_max(ggml_type type = GGML_TYPE_F32,
  985. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  986. bool mask = false,
  987. float scale = 1.0f,
  988. float max_bias = 0.0f)
  989. : type(type), ne(ne), mask(mask), scale(scale), max_bias(max_bias) {}
  990. ggml_tensor * build_graph(ggml_context * ctx) override {
  991. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  992. ggml_tensor * mask = nullptr;
  993. if (this->mask) {
  994. mask = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ne[0], ne[1]);
  995. }
  996. ggml_tensor * out = ggml_soft_max_ext(ctx, a, mask, scale, max_bias);
  997. return out;
  998. }
  999. };
  1000. // GGML_OP_ROPE
  1001. struct test_rope : public test_case {
  1002. const ggml_type type;
  1003. const std::array<int64_t, 4> ne_a;
  1004. int n_dims;
  1005. int mode;
  1006. int n_ctx; // used to generate positions
  1007. float fs; // freq_scale
  1008. float ef; // ext_factor
  1009. float af; // attn_factor
  1010. bool ff;
  1011. int v; // view (1 : non-contiguous a)
  1012. std::string vars() override {
  1013. return VARS_TO_STR10(type, ne_a, n_dims, mode, n_ctx, fs, ef, af, ff, v);
  1014. }
  1015. test_rope(ggml_type type = GGML_TYPE_F32,
  1016. std::array<int64_t, 4> ne_a = {10, 10, 10, 1},
  1017. 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)
  1018. : 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) {}
  1019. ggml_tensor * build_graph(ggml_context * ctx) override {
  1020. ggml_tensor * a;
  1021. if (v & 1) {
  1022. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1023. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1024. 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);
  1025. } else {
  1026. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1027. }
  1028. ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2]);
  1029. ggml_tensor * freq = ff ? ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_dims/2) : nullptr;
  1030. ggml_tensor * out = ggml_rope_ext(ctx, a, pos, freq, n_dims, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f);
  1031. return out;
  1032. }
  1033. void initialize_tensors(ggml_context * ctx) override {
  1034. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1035. if (t->type == GGML_TYPE_I32) {
  1036. // pos
  1037. std::vector<int> data(ne_a[2]);
  1038. for (int i = 0; i < ne_a[2]; i++) {
  1039. data[i] = rand() % n_ctx;
  1040. }
  1041. ggml_backend_tensor_set(t, data.data(), 0, ne_a[2] * sizeof(int));
  1042. } else {
  1043. if (t->ne[0] == n_dims/2) {
  1044. // frequency factors in the range [0.9f, 1.1f]
  1045. init_tensor_uniform(t, 0.9f, 1.1f);
  1046. } else {
  1047. init_tensor_uniform(t);
  1048. }
  1049. }
  1050. }
  1051. }
  1052. };
  1053. // GGML_OP_POOL2D
  1054. struct test_pool2d : public test_case {
  1055. enum ggml_op_pool pool_type;
  1056. const ggml_type type_input;
  1057. const std::array<int64_t, 4> ne_input;
  1058. // kernel size
  1059. const int k0;
  1060. const int k1;
  1061. // stride
  1062. const int s0;
  1063. const int s1;
  1064. // padding
  1065. const int p0;
  1066. const int p1;
  1067. std::string vars() override {
  1068. return VARS_TO_STR9(pool_type, type_input, ne_input, k0, k1, s0, s1, p0, p1);
  1069. }
  1070. test_pool2d(ggml_op_pool pool_type = GGML_OP_POOL_AVG,
  1071. ggml_type type_input = GGML_TYPE_F32,
  1072. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1073. int k0 = 3, int k1 = 3,
  1074. int s0 = 1, int s1 = 1,
  1075. int p0 = 1, int p1 = 1)
  1076. : pool_type(pool_type), type_input(type_input), ne_input(ne_input), k0(k0), k1(k1), s0(s0), s1(s1), p0(p0), p1(p1) {}
  1077. ggml_tensor * build_graph(ggml_context * ctx) override {
  1078. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1079. ggml_tensor * out = ggml_pool_2d(ctx, input, pool_type, k0, k1, s0, s1, p0, p1);
  1080. return out;
  1081. }
  1082. };
  1083. // GGML_OP_CONV_TRANSPOSE_1D
  1084. struct test_conv_transpose_1d : public test_case {
  1085. const std::array<int64_t, 4> ne_input;
  1086. const std::array<int64_t, 4> ne_kernel;
  1087. const int s0; // stride
  1088. const int p0; // padding
  1089. const int d0; // dilation
  1090. std::string vars() override {
  1091. return VARS_TO_STR5(ne_input, ne_kernel, s0, p0, d0);
  1092. }
  1093. test_conv_transpose_1d(std::array<int64_t, 4> ne_input = {197, 32, 1, 1}, // [input_width, input_height, input_channels, 1]
  1094. std::array<int64_t, 4> ne_kernel = {16, 32, 32, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1095. int s0 = 1, int p0 = 0, int d0 = 1)
  1096. : ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), p0(p0), d0(d0) {}
  1097. ggml_tensor * build_graph(ggml_context * ctx) override {
  1098. ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data());
  1099. ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data());
  1100. ggml_tensor * out = ggml_conv_transpose_1d(ctx, kernel, input, s0, p0, d0);
  1101. return out;
  1102. }
  1103. };
  1104. // GGML_OP_IM2COL
  1105. struct test_im2col : public test_case {
  1106. const ggml_type type_input;
  1107. const ggml_type type_kernel;
  1108. const ggml_type dst_type;
  1109. const std::array<int64_t, 4> ne_input;
  1110. const std::array<int64_t, 4> ne_kernel;
  1111. // stride
  1112. const int s0;
  1113. const int s1;
  1114. // padding
  1115. const int p0;
  1116. const int p1;
  1117. // dilation
  1118. const int d0;
  1119. const int d1;
  1120. // mode
  1121. const bool is_2D;
  1122. std::string vars() override {
  1123. return VARS_TO_STR12(type_input, type_kernel, dst_type, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  1124. }
  1125. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32,
  1126. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1127. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1128. int s0 = 1, int s1 = 1,
  1129. int p0 = 1, int p1 = 1,
  1130. int d0 = 1, int d1 = 1,
  1131. bool is_2D = true)
  1132. : 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) {}
  1133. ggml_tensor * build_graph(ggml_context * ctx) override {
  1134. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1135. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  1136. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D, dst_type);
  1137. return out;
  1138. }
  1139. };
  1140. // GGML_OP_CONCAT
  1141. struct test_concat : public test_case {
  1142. const ggml_type type;
  1143. const std::array<int64_t, 4> ne_a;
  1144. const int64_t ne_b_d;
  1145. const int dim;
  1146. const int v; // view (1 << 0: non-cont a, 1 << 1: non-cont b)
  1147. std::string vars() override {
  1148. return VARS_TO_STR5(type, ne_a, ne_b_d, dim, v);
  1149. }
  1150. test_concat(ggml_type type = GGML_TYPE_F32,
  1151. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1152. int64_t ne_b_d = 10,
  1153. int dim = 2, int v = 0)
  1154. : type(type), ne_a(ne_a), ne_b_d(ne_b_d), dim(dim), v(v) {}
  1155. ggml_tensor * build_graph(ggml_context * ctx) override {
  1156. auto ne_b = ne_a;
  1157. ne_b[dim] = ne_b_d;
  1158. ggml_tensor * a;
  1159. if (v & 1) {
  1160. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1161. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1162. 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);
  1163. } else {
  1164. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1165. }
  1166. ggml_tensor * b;
  1167. if (v & 2) {
  1168. auto ne = ne_b; ne[0] *= 3; ne[1] *= 2; ne[2] *= 4;
  1169. b = ggml_new_tensor(ctx, type, 4, ne.data());
  1170. 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);
  1171. } else {
  1172. b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1173. }
  1174. ggml_tensor * out = ggml_concat(ctx, a, b, dim);
  1175. return out;
  1176. }
  1177. };
  1178. // GGML_OP_ARGSORT
  1179. struct test_argsort : public test_case {
  1180. const ggml_type type;
  1181. const std::array<int64_t, 4> ne;
  1182. ggml_sort_order order;
  1183. std::string vars() override {
  1184. return VARS_TO_STR3(type, ne, order);
  1185. }
  1186. test_argsort(ggml_type type = GGML_TYPE_F32,
  1187. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  1188. ggml_sort_order order = GGML_SORT_ORDER_ASC)
  1189. : type(type), ne(ne), order(order) {}
  1190. ggml_tensor * build_graph(ggml_context * ctx) override {
  1191. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1192. ggml_tensor * out = ggml_argsort(ctx, a, order);
  1193. return out;
  1194. }
  1195. void initialize_tensors(ggml_context * ctx) override {
  1196. std::random_device rd;
  1197. std::default_random_engine rng(rd());
  1198. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1199. if (t->type == GGML_TYPE_I32) {
  1200. // indices
  1201. std::vector<int> data(ggml_nelements(t));
  1202. for (int i = 0; i < ggml_nelements(t); i++) {
  1203. data[i] = rand();
  1204. }
  1205. std::shuffle(data.begin(), data.end(), rng);
  1206. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  1207. } else if (t->type == GGML_TYPE_F32) {
  1208. // initialize with unique values to avoid ties
  1209. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1210. std::vector<float> data(t->ne[0]);
  1211. for (int i = 0; i < t->ne[0]; i++) {
  1212. data[i] = i;
  1213. }
  1214. std::shuffle(data.begin(), data.end(), rng);
  1215. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  1216. }
  1217. } else {
  1218. GGML_ABORT("fatal error");
  1219. }
  1220. }
  1221. }
  1222. };
  1223. // GGML_OP_SUM_ROWS
  1224. struct test_sum_rows : public test_case {
  1225. const ggml_type type;
  1226. const std::array<int64_t, 4> ne;
  1227. std::string vars() override {
  1228. return VARS_TO_STR2(type, ne);
  1229. }
  1230. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1231. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  1232. : type(type), ne(ne) {}
  1233. ggml_tensor * build_graph(ggml_context * ctx) override {
  1234. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1235. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1236. return out;
  1237. }
  1238. };
  1239. // GGML_OP_UPSCALE
  1240. struct test_upscale : public test_case {
  1241. const ggml_type type;
  1242. const std::array<int64_t, 4> ne;
  1243. const int32_t scale_factor;
  1244. const bool transpose;
  1245. std::string vars() override {
  1246. return VARS_TO_STR4(type, ne, scale_factor, transpose);
  1247. }
  1248. test_upscale(ggml_type type = GGML_TYPE_F32,
  1249. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1250. int32_t scale_factor = 2, bool transpose = false)
  1251. : type(type), ne(ne), scale_factor(scale_factor), transpose(transpose) {}
  1252. ggml_tensor * build_graph(ggml_context * ctx) override {
  1253. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1254. if (transpose) a = ggml_transpose(ctx, a);
  1255. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1256. return out;
  1257. }
  1258. };
  1259. // GGML_OP_UPSCALE (ext)
  1260. struct test_upscale_ext : public test_case {
  1261. const ggml_type type;
  1262. const std::array<int64_t, 4> ne;
  1263. const std::array<int64_t, 4> ne_tgt;
  1264. std::string vars() override {
  1265. return VARS_TO_STR3(type, ne, ne_tgt);
  1266. }
  1267. test_upscale_ext(ggml_type type = GGML_TYPE_F32,
  1268. std::array<int64_t, 4> ne = {2, 5, 7, 11},
  1269. std::array<int64_t, 4> ne_tgt = {5, 7, 11, 13})
  1270. : type(type), ne(ne), ne_tgt(ne_tgt) {}
  1271. ggml_tensor * build_graph(ggml_context * ctx) override {
  1272. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1273. ggml_tensor * out = ggml_upscale_ext(ctx, a, ne_tgt[0], ne_tgt[1],ne_tgt[2], ne_tgt[3]);
  1274. return out;
  1275. }
  1276. };
  1277. // GGML_OP_GROUP_NORM
  1278. struct test_group_norm : public test_case {
  1279. const ggml_type type;
  1280. const std::array<int64_t, 4> ne;
  1281. const int32_t num_groups;
  1282. std::string vars() override {
  1283. return VARS_TO_STR3(type, ne, num_groups);
  1284. }
  1285. test_group_norm(ggml_type type = GGML_TYPE_F32,
  1286. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  1287. int32_t num_groups = 32)
  1288. : type(type), ne(ne), num_groups(num_groups) {}
  1289. ggml_tensor * build_graph(ggml_context * ctx) override {
  1290. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1291. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups);
  1292. return out;
  1293. }
  1294. };
  1295. // GGML_OP_ACC
  1296. struct test_acc : public test_case {
  1297. const ggml_type type;
  1298. const std::array<int64_t, 4> ne_a;
  1299. const std::array<int64_t, 4> ne_b;
  1300. std::string vars() override {
  1301. return VARS_TO_STR3(type, ne_a, ne_b);
  1302. }
  1303. test_acc(ggml_type type = GGML_TYPE_F32,
  1304. std::array<int64_t, 4> ne_a = {1024, 577, 1, 1},
  1305. std::array<int64_t, 4> ne_b = {1024, 576, 1, 1})
  1306. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1307. ggml_tensor * build_graph(ggml_context * ctx) override {
  1308. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1309. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1310. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  1311. return out;
  1312. }
  1313. };
  1314. // GGML_OP_PAD
  1315. struct test_pad : public test_case {
  1316. const ggml_type type;
  1317. const std::array<int64_t, 4> ne_a;
  1318. const int pad_0;
  1319. const int pad_1;
  1320. std::string vars() override {
  1321. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  1322. }
  1323. test_pad(ggml_type type = GGML_TYPE_F32,
  1324. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  1325. int pad_0 = 1, int pad_1 = 1)
  1326. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  1327. ggml_tensor * build_graph(ggml_context * ctx) override {
  1328. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1329. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  1330. return out;
  1331. }
  1332. };
  1333. // GGML_OP_ARANGE
  1334. struct test_arange : public test_case {
  1335. const ggml_type type;
  1336. const float start;
  1337. const float stop;
  1338. const float step;
  1339. std::string vars() override {
  1340. return VARS_TO_STR4(type, start, stop, step);
  1341. }
  1342. test_arange(ggml_type type = GGML_TYPE_F32,
  1343. float start = 0.f, float stop = 10.f, float step = 1.f)
  1344. : type(type), start(start), stop(stop), step(step) {}
  1345. ggml_tensor * build_graph(ggml_context * ctx) override {
  1346. ggml_tensor * out = ggml_arange(ctx, start, stop, step);
  1347. return out;
  1348. }
  1349. };
  1350. // GGML_OP_TIMESTEP_EMBEDDING
  1351. struct test_timestep_embedding : public test_case {
  1352. const ggml_type type;
  1353. const std::array<int64_t, 4> ne_a;
  1354. const int dim;
  1355. const int max_period;
  1356. std::string vars() override {
  1357. return VARS_TO_STR4(type, ne_a, dim, max_period);
  1358. }
  1359. test_timestep_embedding(ggml_type type = GGML_TYPE_F32,
  1360. std::array<int64_t, 4> ne_a = {2, 1, 1, 1},
  1361. int dim = 320, int max_period=10000)
  1362. : type(type), ne_a(ne_a), dim(dim), max_period(max_period) {}
  1363. ggml_tensor * build_graph(ggml_context * ctx) override {
  1364. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1365. ggml_tensor * out = ggml_timestep_embedding(ctx, a, dim, max_period);
  1366. return out;
  1367. }
  1368. };
  1369. // GGML_OP_LEAKY_RELU
  1370. struct test_leaky_relu : public test_case {
  1371. const ggml_type type;
  1372. const std::array<int64_t, 4> ne_a;
  1373. const float negative_slope;
  1374. std::string vars() override {
  1375. return VARS_TO_STR3(type, ne_a, negative_slope);
  1376. }
  1377. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  1378. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1379. float negative_slope = 0.1f)
  1380. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  1381. ggml_tensor * build_graph(ggml_context * ctx) override {
  1382. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1383. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  1384. return out;
  1385. }
  1386. };
  1387. // GGML_OP_FLASH_ATTN_EXT
  1388. struct test_flash_attn_ext : public test_case {
  1389. const int64_t hs; // head size
  1390. const int64_t nh; // num heads
  1391. const int64_t kv; // kv size
  1392. const int64_t nb; // batch size
  1393. const bool mask; // use mask
  1394. const float max_bias; // ALiBi
  1395. const ggml_type type_KV;
  1396. std::string vars() override {
  1397. return VARS_TO_STR7(hs, nh, kv, nb, mask, max_bias, type_KV);
  1398. }
  1399. double max_nmse_err() override {
  1400. return 5e-4;
  1401. }
  1402. 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, ggml_type type_KV = GGML_TYPE_F16)
  1403. : hs(hs), nh(nh), kv(kv), nb(nb), mask(mask), max_bias(max_bias), type_KV(type_KV) {}
  1404. ggml_tensor * build_graph(ggml_context * ctx) override {
  1405. const int64_t hs_padded = GGML_PAD(hs, ggml_blck_size(type_KV));
  1406. ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs_padded, nb, nh, 1);
  1407. ggml_tensor * k = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  1408. ggml_tensor * v = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  1409. ggml_tensor * m = mask ? ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, GGML_PAD(nb, GGML_KQ_MASK_PAD), 1, 1) : nullptr;
  1410. ggml_tensor * out = ggml_flash_attn_ext(ctx, q, k, v, m, 1.0f/sqrtf(hs), max_bias);
  1411. return out;
  1412. }
  1413. };
  1414. enum llm_norm_type {
  1415. LLM_NORM,
  1416. LLM_NORM_RMS,
  1417. };
  1418. struct llama_hparams {
  1419. uint32_t n_vocab;
  1420. uint32_t n_embd;
  1421. uint32_t n_head;
  1422. uint32_t n_head_kv;
  1423. static constexpr uint32_t n_layer = 1;
  1424. uint32_t n_rot;
  1425. uint32_t n_embd_head; // dimension of values (d_v)
  1426. uint32_t n_ff;
  1427. float f_norm_eps;
  1428. float f_norm_rms_eps;
  1429. // cparams
  1430. static constexpr uint32_t n_ctx = 512; // user-specified context size
  1431. static constexpr uint32_t n_ctx_orig = n_ctx;
  1432. // batch
  1433. int32_t n_tokens;
  1434. // llm_build_context
  1435. static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx
  1436. static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache
  1437. uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads
  1438. return n_embd_head * n_head_kv;
  1439. }
  1440. };
  1441. // LLM base class
  1442. struct test_llm : public test_case {
  1443. llama_hparams hp;
  1444. protected:
  1445. test_llm(llama_hparams hp)
  1446. : hp(std::move(hp)) {
  1447. }
  1448. public:
  1449. struct ggml_tensor * llm_build_norm(
  1450. struct ggml_context * ctx,
  1451. struct ggml_tensor * cur,
  1452. struct ggml_tensor * mw,
  1453. struct ggml_tensor * mb,
  1454. llm_norm_type type) {
  1455. switch (type) {
  1456. case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break;
  1457. case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break;
  1458. }
  1459. cur = ggml_mul(ctx, cur, mw);
  1460. if (mb) {
  1461. cur = ggml_add(ctx, cur, mb);
  1462. }
  1463. return cur;
  1464. }
  1465. void llm_build_kv_store(
  1466. struct ggml_context * ctx,
  1467. struct ggml_tensor * k_l,
  1468. struct ggml_tensor * v_l,
  1469. struct ggml_tensor * k_cur,
  1470. struct ggml_tensor * v_cur) {
  1471. // compute the transposed [n_tokens, n_embd] V matrix
  1472. struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens));
  1473. struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(),
  1474. (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head);
  1475. struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(),
  1476. ( hp.n_ctx)*ggml_element_size(v_l),
  1477. (hp.kv_head)*ggml_element_size(v_l));
  1478. // important: storing RoPE-ed version of K in the KV cache!
  1479. ggml_cpy(ctx, k_cur, k_cache_view);
  1480. ggml_cpy(ctx, v_cur_t, v_cache_view);
  1481. }
  1482. struct ggml_tensor * llm_build_kqv(
  1483. struct ggml_context * ctx,
  1484. struct ggml_tensor * k_l,
  1485. struct ggml_tensor * v_l,
  1486. struct ggml_tensor * q_cur,
  1487. struct ggml_tensor * kq_mask,
  1488. float kq_scale) {
  1489. struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3);
  1490. struct ggml_tensor * k =
  1491. ggml_view_3d(ctx, k_l,
  1492. hp.n_embd_head, hp.n_kv, hp.n_head_kv,
  1493. ggml_row_size(k_l->type, hp.n_embd_gqa()),
  1494. ggml_row_size(k_l->type, hp.n_embd_head),
  1495. 0);
  1496. struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q);
  1497. kq = ggml_soft_max_ext(ctx, kq, kq_mask, kq_scale, 0.0f);
  1498. // split cached v into n_head heads
  1499. struct ggml_tensor * v =
  1500. ggml_view_3d(ctx, v_l,
  1501. hp.n_kv, hp.n_embd_head, hp.n_head_kv,
  1502. ggml_element_size(v_l)*hp.n_ctx,
  1503. ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head,
  1504. 0);
  1505. struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq);
  1506. struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3);
  1507. struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens);
  1508. struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1509. cur = ggml_mul_mat(ctx, wo, cur);
  1510. return cur;
  1511. }
  1512. void initialize_tensors(ggml_context * ctx) override {
  1513. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1514. if (t->type == GGML_TYPE_I32) {
  1515. // pos
  1516. std::vector<int> data(hp.n_tokens);
  1517. for (int i = 0; i < hp.n_tokens; i++) {
  1518. data[i] = rand() % hp.n_ctx;
  1519. }
  1520. ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int));
  1521. } else {
  1522. init_tensor_uniform(t);
  1523. }
  1524. }
  1525. }
  1526. };
  1527. // Llama
  1528. struct test_llama : public test_llm {
  1529. static constexpr float freq_base = 10000.0f;
  1530. static constexpr float freq_scale = 1.0f;
  1531. static constexpr float ext_factor = 0.0f;
  1532. static constexpr float attn_factor = 1.0f;
  1533. static constexpr float beta_fast = 32.0f;
  1534. static constexpr float beta_slow = 1.0f;
  1535. std::string op_desc(ggml_tensor * t) override {
  1536. GGML_UNUSED(t);
  1537. return "LLAMA";
  1538. }
  1539. std::string vars() override {
  1540. auto n_tokens = hp.n_tokens;
  1541. return VARS_TO_STR1(n_tokens);
  1542. }
  1543. double max_nmse_err() override {
  1544. return 2e-3;
  1545. }
  1546. test_llama(int n_tokens = 1)
  1547. : test_llm({
  1548. /*n_vocab =*/ 32000,
  1549. /*n_embd =*/ 3200,
  1550. /*n_head =*/ 32,
  1551. /*n_head_kv =*/ 32,
  1552. /*n_rot =*/ 100,
  1553. /*n_embd_head =*/ 100,
  1554. /*n_ff =*/ 8640,
  1555. /*f_norm_eps =*/ 0.f,
  1556. /*f_norm_rms_eps =*/ 1e-5f,
  1557. /*n_tokens =*/ n_tokens,
  1558. }) {
  1559. }
  1560. ggml_tensor * build_graph(ggml_context * ctx) override {
  1561. struct ggml_tensor * cur;
  1562. struct ggml_tensor * inpL;
  1563. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1564. // inp_pos - contains the positions
  1565. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1566. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1567. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  1568. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1569. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1570. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1571. struct ggml_tensor * inpSA = inpL;
  1572. // norm
  1573. ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1574. cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS);
  1575. // self-attention
  1576. {
  1577. ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1578. ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1579. ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1580. // compute Q and K and RoPE them
  1581. struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur);
  1582. struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur);
  1583. struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur);
  1584. Qcur = ggml_rope_ext(
  1585. ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos, nullptr,
  1586. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  1587. ext_factor, attn_factor, beta_fast, beta_slow
  1588. );
  1589. Kcur = ggml_rope_ext(
  1590. ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos, nullptr,
  1591. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  1592. ext_factor, attn_factor, beta_fast, beta_slow
  1593. );
  1594. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1595. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1596. }
  1597. struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA);
  1598. // feed-forward network
  1599. ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1600. cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS);
  1601. ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1602. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1603. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1604. struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur);
  1605. cur = ggml_mul_mat(ctx, ffn_gate, cur);
  1606. cur = ggml_silu(ctx, cur);
  1607. cur = ggml_mul(ctx, cur, tmp);
  1608. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1609. cur = ggml_add(ctx, cur, ffn_inp);
  1610. // input for next layer
  1611. inpL = cur;
  1612. }
  1613. cur = inpL;
  1614. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1615. cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS);
  1616. // lm_head
  1617. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab);
  1618. cur = ggml_mul_mat(ctx, output, cur);
  1619. return cur;
  1620. }
  1621. };
  1622. // Falcon
  1623. struct test_falcon : public test_llm {
  1624. static constexpr float freq_base = 10000.0f;
  1625. static constexpr float freq_scale = 1.0f;
  1626. static constexpr float ext_factor = 0.0f;
  1627. static constexpr float attn_factor = 1.0f;
  1628. static constexpr float beta_fast = 32.0f;
  1629. static constexpr float beta_slow = 1.0f;
  1630. std::string op_desc(ggml_tensor * t) override {
  1631. GGML_UNUSED(t);
  1632. return "FALCON";
  1633. }
  1634. std::string vars() override {
  1635. auto n_tokens = hp.n_tokens;
  1636. return VARS_TO_STR1(n_tokens);
  1637. }
  1638. double max_nmse_err() override {
  1639. return 2e-3;
  1640. }
  1641. test_falcon(int n_tokens = 1)
  1642. : test_llm({
  1643. /*n_vocab =*/ 32000,
  1644. /*n_embd =*/ 3200,
  1645. /*n_head =*/ 50,
  1646. /*n_head_kv =*/ 1,
  1647. /*n_rot =*/ 64,
  1648. /*n_embd_head =*/ 64,
  1649. /*n_ff =*/ 8640,
  1650. /*f_norm_eps =*/ 1e-5f,
  1651. /*f_norm_rms_eps =*/ 0.f,
  1652. /*n_tokens =*/ n_tokens,
  1653. }) {
  1654. }
  1655. ggml_tensor * build_graph(ggml_context * ctx) override {
  1656. struct ggml_tensor * cur;
  1657. struct ggml_tensor * inpL;
  1658. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1659. // inp_pos - contains the positions
  1660. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1661. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1662. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  1663. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1664. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1665. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1666. // norm
  1667. ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1668. ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1669. ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM);
  1670. // self-attention
  1671. {
  1672. cur = attn_norm;
  1673. ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa());
  1674. cur = ggml_mul_mat(ctx, wqkv, cur);
  1675. 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)));
  1676. 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)));
  1677. 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())));
  1678. Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens);
  1679. Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens);
  1680. // using mode = 2 for neox mode
  1681. Qcur = ggml_rope_ext(
  1682. ctx, Qcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  1683. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1684. );
  1685. Kcur = ggml_rope_ext(
  1686. ctx, Kcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  1687. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1688. );
  1689. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1690. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1691. }
  1692. struct ggml_tensor * ffn_inp = cur;
  1693. // feed forward
  1694. {
  1695. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1696. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1697. cur = attn_norm;
  1698. cur = ggml_mul_mat(ctx, ffn_up, cur);
  1699. cur = ggml_gelu(ctx, cur);
  1700. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1701. }
  1702. cur = ggml_add(ctx, cur, ffn_inp);
  1703. cur = ggml_add(ctx, cur, inpL);
  1704. // input for next layer
  1705. inpL = cur;
  1706. }
  1707. cur = inpL;
  1708. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1709. ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1710. cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM);
  1711. // lm_head
  1712. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab);
  1713. cur = ggml_mul_mat(ctx, output, cur);
  1714. return cur;
  1715. }
  1716. };
  1717. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  1718. std::vector<std::unique_ptr<test_case>> test_cases;
  1719. std::default_random_engine rng(0);
  1720. const ggml_type all_types[] = {
  1721. GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16,
  1722. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  1723. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1724. GGML_TYPE_Q8_0,
  1725. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1726. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  1727. GGML_TYPE_Q6_K,
  1728. GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  1729. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  1730. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  1731. };
  1732. const ggml_type base_types[] = {
  1733. GGML_TYPE_F32, GGML_TYPE_F16,
  1734. GGML_TYPE_Q4_0,
  1735. GGML_TYPE_Q4_K,
  1736. GGML_TYPE_IQ2_XXS
  1737. };
  1738. const ggml_type other_types[] = {
  1739. GGML_TYPE_Q4_1,
  1740. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1741. GGML_TYPE_Q8_0,
  1742. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1743. GGML_TYPE_Q5_K,
  1744. GGML_TYPE_Q6_K,
  1745. GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  1746. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  1747. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  1748. GGML_TYPE_BF16,
  1749. };
  1750. // unary ops
  1751. for (int v : {0, 1}) {
  1752. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  1753. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 128, 10, 10, 10 }, v));
  1754. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 7, 13, 19, 23 }, v));
  1755. }
  1756. }
  1757. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  1758. for (ggml_type type : all_types) {
  1759. for (int b : {1, 7}) {
  1760. for (bool v : {false, true}) {
  1761. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  1762. }
  1763. }
  1764. }
  1765. for (int b : {1, 7}) {
  1766. for (bool v : {false, true}) {
  1767. test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, v));
  1768. }
  1769. }
  1770. for (ggml_type type_input : {GGML_TYPE_F32}) {
  1771. for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) {
  1772. for (int k0 : {1, 3}) {
  1773. for (int k1 : {1, 3}) {
  1774. for (int s0 : {1, 2}) {
  1775. for (int s1 : {1, 2}) {
  1776. for (int p0 : {0, 1}) {
  1777. for (int p1 : {0, 1}) {
  1778. test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1));
  1779. }
  1780. }
  1781. }
  1782. }
  1783. }
  1784. }
  1785. }
  1786. }
  1787. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32));
  1788. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16));
  1789. test_cases.emplace_back(new test_conv_transpose_1d());
  1790. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 3, 0, 1));
  1791. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 2, 0, 1));
  1792. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 1, 0, 1));
  1793. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 2, 0, 1));
  1794. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 1, 0, 1));
  1795. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,1,2,1}, 1, 0, 1));
  1796. test_cases.emplace_back(new test_conv_transpose_1d({2,1,1,1}, {3,1,1,1}, 1, 0, 1));
  1797. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 1}));
  1798. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1799. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 2, 1, 1}));
  1800. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 2, 1}));
  1801. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1802. test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1803. test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1804. test_cases.emplace_back(new test_dup(GGML_TYPE_F32));
  1805. test_cases.emplace_back(new test_dup(GGML_TYPE_F16));
  1806. test_cases.emplace_back(new test_dup(GGML_TYPE_I32));
  1807. test_cases.emplace_back(new test_dup(GGML_TYPE_I16));
  1808. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {0, 2, 1, 3}));
  1809. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {0, 2, 1, 3})); // dup by rows
  1810. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {1, 0, 2, 3}));
  1811. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {1, 0, 2, 3})); // dup dst not-contiguous
  1812. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3}));
  1813. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3}));
  1814. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1815. for (ggml_type type_dst : all_types) {
  1816. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4}));
  1817. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {0, 2, 1, 3})); // cpy by rows
  1818. }
  1819. }
  1820. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1821. for (ggml_type type_dst : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1822. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {1, 0, 2, 3})); // cpy not-contiguous
  1823. }
  1824. }
  1825. test_cases.emplace_back(new test_cont());
  1826. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  1827. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  1828. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  1829. }
  1830. };
  1831. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  1832. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  1833. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  1834. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 1, 1}, {1, 1, 1, 1});
  1835. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 1}, {1, 1, 1, 1});
  1836. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 1});
  1837. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 1, 1, 1});
  1838. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 1, 1});
  1839. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 1});
  1840. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 2});
  1841. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 2});
  1842. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 2, 2});
  1843. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 2, 2, 2});
  1844. // stable diffusion
  1845. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  1846. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  1847. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  1848. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  1849. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  1850. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  1851. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  1852. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  1853. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  1854. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  1855. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  1856. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  1857. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  1858. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  1859. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  1860. test_cases.emplace_back(new test_scale());
  1861. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  1862. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1863. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1864. }
  1865. #if 1
  1866. for (ggml_type type_a : base_types) {
  1867. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1868. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1869. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  1870. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  1871. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  1872. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  1873. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  1874. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  1875. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  1876. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  1877. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  1878. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  1879. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  1880. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  1881. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  1882. }
  1883. }
  1884. #else
  1885. // m = a rows
  1886. // n = b rows
  1887. // k = cols
  1888. std::uniform_int_distribution<> dist_m(1, 128);
  1889. std::uniform_int_distribution<> dist_n(16, 128);
  1890. std::uniform_int_distribution<> dist_k(1, 16);
  1891. for (int i = 0; i < 1000; i++) {
  1892. for (ggml_type type_a : all_types) {
  1893. for (ggml_type type_b : {GGML_TYPE_F32}) {
  1894. int m = dist_m(rng);
  1895. int n = dist_n(rng);
  1896. int k = dist_k(rng) * ggml_blck_size(type_a);
  1897. test_cases.emplace_back(new test_mul_mat(type_a, type_b, m, n, k, { 1, 1}, {1, 1}));
  1898. }
  1899. }
  1900. }
  1901. #endif
  1902. for (ggml_type type_a : other_types) {
  1903. for (ggml_type type_b : {GGML_TYPE_F32}) {
  1904. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1905. }
  1906. }
  1907. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 128, { 8, 1}, {1, 1}));
  1908. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 128, { 8, 1}, {4, 1}));
  1909. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 64, { 8, 1}, {4, 1}));
  1910. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 64, { 8, 1}, {4, 1}));
  1911. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 45, 128, { 8, 1}, {4, 1}));
  1912. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 128, 45, 64, { 8, 1}, {4, 1}));
  1913. for (ggml_type type_a : base_types) {
  1914. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1915. for (int n_mats : {4, 8}) {
  1916. for (int n_used : {1, 2, 4}) {
  1917. for (bool b : {false, true}) {
  1918. for (int n : {1, 32}) {
  1919. int m = 512;
  1920. int k = 256;
  1921. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  1922. }
  1923. }
  1924. }
  1925. }
  1926. }
  1927. }
  1928. for (ggml_type type_a : other_types) {
  1929. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1930. for (int n_mats : {4}) {
  1931. for (int n_used : {2}) {
  1932. for (bool b : {false}) {
  1933. for (int n : {1}) {
  1934. int m = 512;
  1935. int k = 256;
  1936. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  1937. }
  1938. }
  1939. }
  1940. }
  1941. }
  1942. }
  1943. test_cases.emplace_back(new test_sqr());
  1944. test_cases.emplace_back(new test_sqrt());
  1945. test_cases.emplace_back(new test_clamp());
  1946. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  1947. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 1}, 5));
  1948. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 10}, 5));
  1949. #if 0
  1950. std::uniform_int_distribution<> dist_ne1(1, 50);
  1951. int exponent = 1;
  1952. while (exponent < (1 << 17)) {
  1953. std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent);
  1954. for (int n = 0; n < 10; ++n) {
  1955. int64_t ne0 = dist_ne0(rng);
  1956. int64_t ne1 = dist_ne1(rng);
  1957. 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));
  1958. }
  1959. exponent <<= 1;
  1960. }
  1961. #endif
  1962. for (bool mask : {false, true}) {
  1963. for (float max_bias : {0.0f, 8.0f}) {
  1964. if (!mask && max_bias > 0.0f) continue;
  1965. for (float scale : {1.0f, 0.1f}) {
  1966. for (int64_t ne0 : {16, 1024}) {
  1967. for (int64_t ne1 : {16, 1024}) {
  1968. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, scale, max_bias));
  1969. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, scale, max_bias));
  1970. }
  1971. }
  1972. }
  1973. }
  1974. }
  1975. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, true, 0.1f, 0.0f));
  1976. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 0.0f));
  1977. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 0.0f));
  1978. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 8.0f));
  1979. {
  1980. bool all = true;
  1981. for (float v : { 0, 1 }) {
  1982. for (float fs : { 1.0f, 1.4245f }) {
  1983. for (float ef : { 0.0f, 0.7465f }) {
  1984. for (float af : { 1.0f, 1.4245f }) {
  1985. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1986. for (bool ff : {false, true}) { // freq_factors
  1987. test_cases.emplace_back(new test_rope(type, {128, 32, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 7B
  1988. if (all) {
  1989. test_cases.emplace_back(new test_rope(type, {128, 40, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 13B
  1990. test_cases.emplace_back(new test_rope(type, {128, 52, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 30B
  1991. test_cases.emplace_back(new test_rope(type, {128, 64, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 65B
  1992. }
  1993. if (all) {
  1994. test_cases.emplace_back(new test_rope(type, { 64, 1, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  1995. test_cases.emplace_back(new test_rope(type, { 64, 71, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  1996. test_cases.emplace_back(new test_rope(type, { 64, 8, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  1997. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 20, 2, 512, fs, ef, af, ff, v)); // neox (stablelm)
  1998. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 32, 2, 512, fs, ef, af, ff, v)); // neox (phi-2)
  1999. }
  2000. test_cases.emplace_back(new test_rope(type, { 64, 128, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2001. }
  2002. }
  2003. all = false;
  2004. }
  2005. }
  2006. }
  2007. }
  2008. }
  2009. for (int v : { 0, 1, 2, 3 }) {
  2010. for (int dim : { 0, 1, 2, 3, }) {
  2011. test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {11, 12, 13, 14}, 7, dim, v));
  2012. test_cases.emplace_back(new test_concat(GGML_TYPE_I32, {11, 12, 13, 14}, 7, dim, v));
  2013. }
  2014. }
  2015. for (ggml_sort_order order : {GGML_SORT_ORDER_ASC, GGML_SORT_ORDER_DESC}) {
  2016. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  2017. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  2018. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {60, 10, 10, 10}, order)); // qwen
  2019. }
  2020. test_cases.emplace_back(new test_sum_rows());
  2021. test_cases.emplace_back(new test_upscale());
  2022. test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, { 512, 512, 3, 1 }, 2, true));
  2023. test_cases.emplace_back(new test_upscale_ext());
  2024. test_cases.emplace_back(new test_group_norm());
  2025. test_cases.emplace_back(new test_acc());
  2026. test_cases.emplace_back(new test_pad());
  2027. test_cases.emplace_back(new test_arange());
  2028. test_cases.emplace_back(new test_timestep_embedding());
  2029. test_cases.emplace_back(new test_leaky_relu());
  2030. for (int hs : { 64, 80, 128, 256, }) {
  2031. for (bool mask : { true, false } ) {
  2032. for (float max_bias : { 0.0f, 8.0f }) {
  2033. if (!mask && max_bias > 0.0f) continue;
  2034. for (int nh : { 32, }) {
  2035. for (int kv : { 512, 1024, }) {
  2036. for (int nb : { 1, 2, 4, 8, }) {
  2037. for (ggml_type type_KV : {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
  2038. test_cases.emplace_back(new test_flash_attn_ext(hs, nh, kv, nb, mask, max_bias, type_KV));
  2039. }
  2040. }
  2041. }
  2042. }
  2043. }
  2044. }
  2045. }
  2046. // these tests are disabled to save execution time, but they can be handy for debugging
  2047. #if 0
  2048. test_cases.emplace_back(new test_llama(1));
  2049. test_cases.emplace_back(new test_llama(2));
  2050. test_cases.emplace_back(new test_falcon(1));
  2051. test_cases.emplace_back(new test_falcon(2));
  2052. #endif
  2053. // run tests
  2054. if (mode == MODE_TEST) {
  2055. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  2056. size_t n_ok = 0;
  2057. for (auto & test : test_cases) {
  2058. if (test->eval(backend, backend_cpu, op_name)) {
  2059. n_ok++;
  2060. }
  2061. }
  2062. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  2063. ggml_backend_free(backend_cpu);
  2064. return n_ok == test_cases.size();
  2065. }
  2066. if (mode == MODE_PERF) {
  2067. for (auto & test : test_cases) {
  2068. test->eval_perf(backend, op_name);
  2069. }
  2070. return true;
  2071. }
  2072. GGML_ABORT("fatal error");
  2073. return false;
  2074. }
  2075. static void usage(char ** argv) {
  2076. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  2077. printf(" valid modes are: test (compare with CPU backend for correctness) or perf (performance evaluation)\n");
  2078. printf(" op names are as given by ggml_op_desc()\n");
  2079. }
  2080. int main(int argc, char ** argv) {
  2081. test_mode mode = MODE_TEST;
  2082. const char * op_name_filter = NULL;
  2083. const char * backend_filter = NULL;
  2084. for (int i = 1; i < argc; i++) {
  2085. if (strcmp(argv[i], "test") == 0) {
  2086. mode = MODE_TEST;
  2087. } else if (strcmp(argv[i], "perf") == 0) {
  2088. mode = MODE_PERF;
  2089. } else if (strcmp(argv[i], "-o") == 0) {
  2090. if (i + 1 < argc) {
  2091. op_name_filter = argv[++i];
  2092. } else {
  2093. usage(argv);
  2094. return 1;
  2095. }
  2096. } else if (strcmp(argv[i], "-b") == 0) {
  2097. if (i + 1 < argc) {
  2098. backend_filter = argv[++i];
  2099. } else {
  2100. usage(argv);
  2101. return 1;
  2102. }
  2103. } else {
  2104. usage(argv);
  2105. return 1;
  2106. }
  2107. }
  2108. // enumerate backends
  2109. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  2110. size_t n_ok = 0;
  2111. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  2112. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  2113. if (backend_filter != NULL && strcmp(backend_filter, ggml_backend_reg_get_name(i)) != 0) {
  2114. printf(" Skipping\n");
  2115. n_ok++;
  2116. continue;
  2117. }
  2118. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  2119. GGML_ASSERT(backend != NULL);
  2120. if (backend_filter == NULL && ggml_backend_is_cpu(backend)) {
  2121. printf(" Skipping CPU backend\n");
  2122. ggml_backend_free(backend);
  2123. n_ok++;
  2124. continue;
  2125. }
  2126. printf(" Backend name: %s\n", ggml_backend_name(backend));
  2127. bool ok = test_backend(backend, mode, op_name_filter);
  2128. printf(" Backend %s: ", ggml_backend_name(backend));
  2129. if (ok) {
  2130. printf("\033[1;32mOK\033[0m\n");
  2131. n_ok++;
  2132. } else {
  2133. printf("\033[1;31mFAIL\033[0m\n");
  2134. }
  2135. printf("\n");
  2136. ggml_backend_free(backend);
  2137. }
  2138. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  2139. if (n_ok != ggml_backend_reg_get_count()) {
  2140. printf("\033[1;31mFAIL\033[0m\n");
  2141. return 1;
  2142. }
  2143. ggml_quantize_free();
  2144. printf("\033[1;32mOK\033[0m\n");
  2145. return 0;
  2146. }