test-backend-ops.cpp 88 KB

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