test-backend-ops.cpp 90 KB

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