test-backend-ops.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511
  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. const int s0; // stride
  1074. const int p0; // padding
  1075. const int d0; // dilation
  1076. std::string vars() override {
  1077. return VARS_TO_STR5(ne_input, ne_kernel, s0, p0, d0);
  1078. }
  1079. test_conv_transpose_1d(std::array<int64_t, 4> ne_input = {197, 32, 1, 1}, // [input_width, input_height, input_channels, 1]
  1080. std::array<int64_t, 4> ne_kernel = {16, 32, 32, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1081. int s0 = 1, int p0 = 0, int d0 = 1)
  1082. : ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), p0(p0), d0(d0) {}
  1083. ggml_tensor * build_graph(ggml_context * ctx) override {
  1084. ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data());
  1085. ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data());
  1086. ggml_tensor * out = ggml_conv_transpose_1d(ctx, kernel, input, s0, p0, d0);
  1087. return out;
  1088. }
  1089. };
  1090. // GGML_OP_IM2COL
  1091. struct test_im2col : public test_case {
  1092. const ggml_type type_input;
  1093. const ggml_type type_kernel;
  1094. const ggml_type dst_type;
  1095. const std::array<int64_t, 4> ne_input;
  1096. const std::array<int64_t, 4> ne_kernel;
  1097. // stride
  1098. const int s0;
  1099. const int s1;
  1100. // padding
  1101. const int p0;
  1102. const int p1;
  1103. // dilation
  1104. const int d0;
  1105. const int d1;
  1106. // mode
  1107. const bool is_2D;
  1108. std::string vars() override {
  1109. return VARS_TO_STR12(type_input, type_kernel, dst_type, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  1110. }
  1111. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32,
  1112. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1113. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1114. int s0 = 1, int s1 = 1,
  1115. int p0 = 1, int p1 = 1,
  1116. int d0 = 1, int d1 = 1,
  1117. bool is_2D = true)
  1118. : 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) {}
  1119. ggml_tensor * build_graph(ggml_context * ctx) override {
  1120. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1121. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  1122. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D, dst_type);
  1123. return out;
  1124. }
  1125. };
  1126. // GGML_OP_CONCAT
  1127. struct test_concat : public test_case {
  1128. const ggml_type type;
  1129. const std::array<int64_t, 4> ne_a;
  1130. const int64_t ne_b_d;
  1131. const int dim;
  1132. const int v; // view (1 << 0: non-cont a, 1 << 1: non-cont b)
  1133. std::string vars() override {
  1134. return VARS_TO_STR5(type, ne_a, ne_b_d, dim, v);
  1135. }
  1136. test_concat(ggml_type type = GGML_TYPE_F32,
  1137. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1138. int64_t ne_b_d = 10,
  1139. int dim = 2, int v = 0)
  1140. : type(type), ne_a(ne_a), ne_b_d(ne_b_d), dim(dim), v(v) {}
  1141. ggml_tensor * build_graph(ggml_context * ctx) override {
  1142. auto ne_b = ne_a;
  1143. ne_b[dim] = ne_b_d;
  1144. ggml_tensor * a;
  1145. if (v & 1) {
  1146. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1147. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1148. 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);
  1149. } else {
  1150. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1151. }
  1152. ggml_tensor * b;
  1153. if (v & 2) {
  1154. auto ne = ne_b; ne[0] *= 3; ne[1] *= 2; ne[2] *= 4;
  1155. b = ggml_new_tensor(ctx, type, 4, ne.data());
  1156. 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);
  1157. } else {
  1158. b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1159. }
  1160. ggml_tensor * out = ggml_concat(ctx, a, b, dim);
  1161. return out;
  1162. }
  1163. };
  1164. // GGML_OP_ARGSORT
  1165. struct test_argsort : public test_case {
  1166. const ggml_type type;
  1167. const std::array<int64_t, 4> ne;
  1168. ggml_sort_order order;
  1169. std::string vars() override {
  1170. return VARS_TO_STR3(type, ne, order);
  1171. }
  1172. test_argsort(ggml_type type = GGML_TYPE_F32,
  1173. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  1174. ggml_sort_order order = GGML_SORT_ORDER_ASC)
  1175. : type(type), ne(ne), order(order) {}
  1176. ggml_tensor * build_graph(ggml_context * ctx) override {
  1177. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1178. ggml_tensor * out = ggml_argsort(ctx, a, order);
  1179. return out;
  1180. }
  1181. void initialize_tensors(ggml_context * ctx) override {
  1182. std::random_device rd;
  1183. std::default_random_engine rng(rd());
  1184. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1185. if (t->type == GGML_TYPE_I32) {
  1186. // indices
  1187. std::vector<int> data(ggml_nelements(t));
  1188. for (int i = 0; i < ggml_nelements(t); i++) {
  1189. data[i] = rand();
  1190. }
  1191. std::shuffle(data.begin(), data.end(), rng);
  1192. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  1193. } else if (t->type == GGML_TYPE_F32) {
  1194. // initialize with unique values to avoid ties
  1195. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1196. std::vector<float> data(t->ne[0]);
  1197. for (int i = 0; i < t->ne[0]; i++) {
  1198. data[i] = i;
  1199. }
  1200. std::shuffle(data.begin(), data.end(), rng);
  1201. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  1202. }
  1203. } else {
  1204. GGML_ASSERT(false);
  1205. }
  1206. }
  1207. }
  1208. };
  1209. // GGML_OP_SUM_ROWS
  1210. struct test_sum_rows : public test_case {
  1211. const ggml_type type;
  1212. const std::array<int64_t, 4> ne;
  1213. std::string vars() override {
  1214. return VARS_TO_STR2(type, ne);
  1215. }
  1216. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1217. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  1218. : type(type), ne(ne) {}
  1219. ggml_tensor * build_graph(ggml_context * ctx) override {
  1220. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1221. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1222. return out;
  1223. }
  1224. };
  1225. // GGML_OP_UPSCALE
  1226. struct test_upscale : public test_case {
  1227. const ggml_type type;
  1228. const std::array<int64_t, 4> ne;
  1229. const int32_t scale_factor;
  1230. const bool transpose;
  1231. std::string vars() override {
  1232. return VARS_TO_STR4(type, ne, scale_factor, transpose);
  1233. }
  1234. test_upscale(ggml_type type = GGML_TYPE_F32,
  1235. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1236. int32_t scale_factor = 2, bool transpose = false)
  1237. : type(type), ne(ne), scale_factor(scale_factor), transpose(transpose) {}
  1238. ggml_tensor * build_graph(ggml_context * ctx) override {
  1239. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1240. if (transpose) a = ggml_transpose(ctx, a);
  1241. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1242. return out;
  1243. }
  1244. };
  1245. // GGML_OP_UPSCALE (ext)
  1246. struct test_upscale_ext : public test_case {
  1247. const ggml_type type;
  1248. const std::array<int64_t, 4> ne;
  1249. const std::array<int64_t, 4> ne_tgt;
  1250. std::string vars() override {
  1251. return VARS_TO_STR3(type, ne, ne_tgt);
  1252. }
  1253. test_upscale_ext(ggml_type type = GGML_TYPE_F32,
  1254. std::array<int64_t, 4> ne = {2, 5, 7, 11},
  1255. std::array<int64_t, 4> ne_tgt = {5, 7, 11, 13})
  1256. : type(type), ne(ne), ne_tgt(ne_tgt) {}
  1257. ggml_tensor * build_graph(ggml_context * ctx) override {
  1258. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1259. ggml_tensor * out = ggml_upscale_ext(ctx, a, ne_tgt[0], ne_tgt[1],ne_tgt[2], ne_tgt[3]);
  1260. return out;
  1261. }
  1262. };
  1263. // GGML_OP_GROUP_NORM
  1264. struct test_group_norm : public test_case {
  1265. const ggml_type type;
  1266. const std::array<int64_t, 4> ne;
  1267. const int32_t num_groups;
  1268. std::string vars() override {
  1269. return VARS_TO_STR3(type, ne, num_groups);
  1270. }
  1271. test_group_norm(ggml_type type = GGML_TYPE_F32,
  1272. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  1273. int32_t num_groups = 32)
  1274. : type(type), ne(ne), num_groups(num_groups) {}
  1275. ggml_tensor * build_graph(ggml_context * ctx) override {
  1276. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1277. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups);
  1278. return out;
  1279. }
  1280. };
  1281. // GGML_OP_ACC
  1282. struct test_acc : public test_case {
  1283. const ggml_type type;
  1284. const std::array<int64_t, 4> ne_a;
  1285. const std::array<int64_t, 4> ne_b;
  1286. std::string vars() override {
  1287. return VARS_TO_STR3(type, ne_a, ne_b);
  1288. }
  1289. test_acc(ggml_type type = GGML_TYPE_F32,
  1290. std::array<int64_t, 4> ne_a = {1024, 577, 1, 1},
  1291. std::array<int64_t, 4> ne_b = {1024, 576, 1, 1})
  1292. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1293. ggml_tensor * build_graph(ggml_context * ctx) override {
  1294. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1295. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1296. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  1297. return out;
  1298. }
  1299. };
  1300. // GGML_OP_PAD
  1301. struct test_pad : public test_case {
  1302. const ggml_type type;
  1303. const std::array<int64_t, 4> ne_a;
  1304. const int pad_0;
  1305. const int pad_1;
  1306. std::string vars() override {
  1307. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  1308. }
  1309. test_pad(ggml_type type = GGML_TYPE_F32,
  1310. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  1311. int pad_0 = 1, int pad_1 = 1)
  1312. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  1313. ggml_tensor * build_graph(ggml_context * ctx) override {
  1314. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1315. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  1316. return out;
  1317. }
  1318. };
  1319. // GGML_OP_ARANGE
  1320. struct test_arange : public test_case {
  1321. const ggml_type type;
  1322. const float start;
  1323. const float stop;
  1324. const float step;
  1325. std::string vars() override {
  1326. return VARS_TO_STR4(type, start, stop, step);
  1327. }
  1328. test_arange(ggml_type type = GGML_TYPE_F32,
  1329. float start = 0.f, float stop = 10.f, float step = 1.f)
  1330. : type(type), start(start), stop(stop), step(step) {}
  1331. ggml_tensor * build_graph(ggml_context * ctx) override {
  1332. ggml_tensor * out = ggml_arange(ctx, start, stop, step);
  1333. return out;
  1334. }
  1335. };
  1336. // GGML_OP_TIMESTEP_EMBEDDING
  1337. struct test_timestep_embedding : public test_case {
  1338. const ggml_type type;
  1339. const std::array<int64_t, 4> ne_a;
  1340. const int dim;
  1341. const int max_period;
  1342. std::string vars() override {
  1343. return VARS_TO_STR4(type, ne_a, dim, max_period);
  1344. }
  1345. test_timestep_embedding(ggml_type type = GGML_TYPE_F32,
  1346. std::array<int64_t, 4> ne_a = {2, 1, 1, 1},
  1347. int dim = 320, int max_period=10000)
  1348. : type(type), ne_a(ne_a), dim(dim), max_period(max_period) {}
  1349. ggml_tensor * build_graph(ggml_context * ctx) override {
  1350. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1351. ggml_tensor * out = ggml_timestep_embedding(ctx, a, dim, max_period);
  1352. return out;
  1353. }
  1354. };
  1355. // GGML_OP_LEAKY_RELU
  1356. struct test_leaky_relu : public test_case {
  1357. const ggml_type type;
  1358. const std::array<int64_t, 4> ne_a;
  1359. const float negative_slope;
  1360. std::string vars() override {
  1361. return VARS_TO_STR3(type, ne_a, negative_slope);
  1362. }
  1363. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  1364. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1365. float negative_slope = 0.1f)
  1366. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  1367. ggml_tensor * build_graph(ggml_context * ctx) override {
  1368. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1369. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  1370. return out;
  1371. }
  1372. };
  1373. // GGML_OP_FLASH_ATTN_EXT
  1374. struct test_flash_attn_ext : public test_case {
  1375. const int64_t hs; // head size
  1376. const int64_t nh; // num heads
  1377. const int64_t kv; // kv size
  1378. const int64_t nb; // batch size
  1379. const bool mask; // use mask
  1380. const float max_bias; // ALiBi
  1381. const ggml_type type_KV;
  1382. std::string vars() override {
  1383. return VARS_TO_STR7(hs, nh, kv, nb, mask, max_bias, type_KV);
  1384. }
  1385. double max_nmse_err() override {
  1386. return 5e-4;
  1387. }
  1388. 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)
  1389. : hs(hs), nh(nh), kv(kv), nb(nb), mask(mask), max_bias(max_bias), type_KV(type_KV) {}
  1390. ggml_tensor * build_graph(ggml_context * ctx) override {
  1391. const int64_t hs_padded = GGML_PAD(hs, ggml_blck_size(type_KV));
  1392. ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs_padded, nb, nh, 1);
  1393. ggml_tensor * k = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  1394. ggml_tensor * v = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  1395. ggml_tensor * m = mask ? ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, GGML_PAD(nb, GGML_KQ_MASK_PAD), 1, 1) : nullptr;
  1396. ggml_tensor * out = ggml_flash_attn_ext(ctx, q, k, v, m, 1.0f/sqrtf(hs), max_bias);
  1397. return out;
  1398. }
  1399. };
  1400. enum llm_norm_type {
  1401. LLM_NORM,
  1402. LLM_NORM_RMS,
  1403. };
  1404. struct llama_hparams {
  1405. uint32_t n_vocab;
  1406. uint32_t n_embd;
  1407. uint32_t n_head;
  1408. uint32_t n_head_kv;
  1409. static constexpr uint32_t n_layer = 1;
  1410. uint32_t n_rot;
  1411. uint32_t n_embd_head; // dimension of values (d_v)
  1412. uint32_t n_ff;
  1413. float f_norm_eps;
  1414. float f_norm_rms_eps;
  1415. // cparams
  1416. static constexpr uint32_t n_ctx = 512; // user-specified context size
  1417. static constexpr uint32_t n_ctx_orig = n_ctx;
  1418. // batch
  1419. int32_t n_tokens;
  1420. // llm_build_context
  1421. static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx
  1422. static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache
  1423. uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads
  1424. return n_embd_head * n_head_kv;
  1425. }
  1426. };
  1427. // LLM base class
  1428. struct test_llm : public test_case {
  1429. llama_hparams hp;
  1430. protected:
  1431. test_llm(llama_hparams hp)
  1432. : hp(std::move(hp)) {
  1433. }
  1434. public:
  1435. struct ggml_tensor * llm_build_norm(
  1436. struct ggml_context * ctx,
  1437. struct ggml_tensor * cur,
  1438. struct ggml_tensor * mw,
  1439. struct ggml_tensor * mb,
  1440. llm_norm_type type) {
  1441. switch (type) {
  1442. case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break;
  1443. case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break;
  1444. }
  1445. cur = ggml_mul(ctx, cur, mw);
  1446. if (mb) {
  1447. cur = ggml_add(ctx, cur, mb);
  1448. }
  1449. return cur;
  1450. }
  1451. void llm_build_kv_store(
  1452. struct ggml_context * ctx,
  1453. struct ggml_tensor * k_l,
  1454. struct ggml_tensor * v_l,
  1455. struct ggml_tensor * k_cur,
  1456. struct ggml_tensor * v_cur) {
  1457. // compute the transposed [n_tokens, n_embd] V matrix
  1458. struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens));
  1459. struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(),
  1460. (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head);
  1461. struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(),
  1462. ( hp.n_ctx)*ggml_element_size(v_l),
  1463. (hp.kv_head)*ggml_element_size(v_l));
  1464. // important: storing RoPE-ed version of K in the KV cache!
  1465. ggml_cpy(ctx, k_cur, k_cache_view);
  1466. ggml_cpy(ctx, v_cur_t, v_cache_view);
  1467. }
  1468. struct ggml_tensor * llm_build_kqv(
  1469. struct ggml_context * ctx,
  1470. struct ggml_tensor * k_l,
  1471. struct ggml_tensor * v_l,
  1472. struct ggml_tensor * q_cur,
  1473. struct ggml_tensor * kq_mask,
  1474. float kq_scale) {
  1475. struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3);
  1476. struct ggml_tensor * k =
  1477. ggml_view_3d(ctx, k_l,
  1478. hp.n_embd_head, hp.n_kv, hp.n_head_kv,
  1479. ggml_row_size(k_l->type, hp.n_embd_gqa()),
  1480. ggml_row_size(k_l->type, hp.n_embd_head),
  1481. 0);
  1482. struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q);
  1483. kq = ggml_soft_max_ext(ctx, kq, kq_mask, kq_scale, 0.0f);
  1484. // split cached v into n_head heads
  1485. struct ggml_tensor * v =
  1486. ggml_view_3d(ctx, v_l,
  1487. hp.n_kv, hp.n_embd_head, hp.n_head_kv,
  1488. ggml_element_size(v_l)*hp.n_ctx,
  1489. ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head,
  1490. 0);
  1491. struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq);
  1492. struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3);
  1493. struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens);
  1494. struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1495. cur = ggml_mul_mat(ctx, wo, cur);
  1496. return cur;
  1497. }
  1498. void initialize_tensors(ggml_context * ctx) override {
  1499. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1500. if (t->type == GGML_TYPE_I32) {
  1501. // pos
  1502. std::vector<int> data(hp.n_tokens);
  1503. for (int i = 0; i < hp.n_tokens; i++) {
  1504. data[i] = rand() % hp.n_ctx;
  1505. }
  1506. ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int));
  1507. } else {
  1508. init_tensor_uniform(t);
  1509. }
  1510. }
  1511. }
  1512. };
  1513. // Llama
  1514. struct test_llama : public test_llm {
  1515. static constexpr float freq_base = 10000.0f;
  1516. static constexpr float freq_scale = 1.0f;
  1517. static constexpr float ext_factor = 0.0f;
  1518. static constexpr float attn_factor = 1.0f;
  1519. static constexpr float beta_fast = 32.0f;
  1520. static constexpr float beta_slow = 1.0f;
  1521. std::string op_desc(ggml_tensor * t) override {
  1522. GGML_UNUSED(t);
  1523. return "LLAMA";
  1524. }
  1525. std::string vars() override {
  1526. auto n_tokens = hp.n_tokens;
  1527. return VARS_TO_STR1(n_tokens);
  1528. }
  1529. double max_nmse_err() override {
  1530. return 2e-3;
  1531. }
  1532. test_llama(int n_tokens = 1)
  1533. : test_llm({
  1534. /*n_vocab =*/ 32000,
  1535. /*n_embd =*/ 3200,
  1536. /*n_head =*/ 32,
  1537. /*n_head_kv =*/ 32,
  1538. /*n_rot =*/ 100,
  1539. /*n_embd_head =*/ 100,
  1540. /*n_ff =*/ 8640,
  1541. /*f_norm_eps =*/ 0.f,
  1542. /*f_norm_rms_eps =*/ 1e-5f,
  1543. /*n_tokens =*/ n_tokens,
  1544. }) {
  1545. }
  1546. ggml_tensor * build_graph(ggml_context * ctx) override {
  1547. struct ggml_tensor * cur;
  1548. struct ggml_tensor * inpL;
  1549. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1550. // inp_pos - contains the positions
  1551. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1552. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1553. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  1554. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1555. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1556. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1557. struct ggml_tensor * inpSA = inpL;
  1558. // norm
  1559. ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1560. cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS);
  1561. // self-attention
  1562. {
  1563. ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1564. ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1565. ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1566. // compute Q and K and RoPE them
  1567. struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur);
  1568. struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur);
  1569. struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur);
  1570. Qcur = ggml_rope_ext(
  1571. ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos, nullptr,
  1572. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  1573. ext_factor, attn_factor, beta_fast, beta_slow
  1574. );
  1575. Kcur = ggml_rope_ext(
  1576. ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos, nullptr,
  1577. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  1578. ext_factor, attn_factor, beta_fast, beta_slow
  1579. );
  1580. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1581. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1582. }
  1583. struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA);
  1584. // feed-forward network
  1585. ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1586. cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS);
  1587. ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1588. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1589. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1590. struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur);
  1591. cur = ggml_mul_mat(ctx, ffn_gate, cur);
  1592. cur = ggml_silu(ctx, cur);
  1593. cur = ggml_mul(ctx, cur, tmp);
  1594. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1595. cur = ggml_add(ctx, cur, ffn_inp);
  1596. // input for next layer
  1597. inpL = cur;
  1598. }
  1599. cur = inpL;
  1600. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1601. cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS);
  1602. // lm_head
  1603. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab);
  1604. cur = ggml_mul_mat(ctx, output, cur);
  1605. return cur;
  1606. }
  1607. };
  1608. // Falcon
  1609. struct test_falcon : public test_llm {
  1610. static constexpr float freq_base = 10000.0f;
  1611. static constexpr float freq_scale = 1.0f;
  1612. static constexpr float ext_factor = 0.0f;
  1613. static constexpr float attn_factor = 1.0f;
  1614. static constexpr float beta_fast = 32.0f;
  1615. static constexpr float beta_slow = 1.0f;
  1616. std::string op_desc(ggml_tensor * t) override {
  1617. GGML_UNUSED(t);
  1618. return "FALCON";
  1619. }
  1620. std::string vars() override {
  1621. auto n_tokens = hp.n_tokens;
  1622. return VARS_TO_STR1(n_tokens);
  1623. }
  1624. double max_nmse_err() override {
  1625. return 2e-3;
  1626. }
  1627. test_falcon(int n_tokens = 1)
  1628. : test_llm({
  1629. /*n_vocab =*/ 32000,
  1630. /*n_embd =*/ 3200,
  1631. /*n_head =*/ 50,
  1632. /*n_head_kv =*/ 1,
  1633. /*n_rot =*/ 64,
  1634. /*n_embd_head =*/ 64,
  1635. /*n_ff =*/ 8640,
  1636. /*f_norm_eps =*/ 1e-5f,
  1637. /*f_norm_rms_eps =*/ 0.f,
  1638. /*n_tokens =*/ n_tokens,
  1639. }) {
  1640. }
  1641. ggml_tensor * build_graph(ggml_context * ctx) override {
  1642. struct ggml_tensor * cur;
  1643. struct ggml_tensor * inpL;
  1644. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1645. // inp_pos - contains the positions
  1646. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1647. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1648. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  1649. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1650. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1651. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1652. // norm
  1653. ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1654. ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1655. ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM);
  1656. // self-attention
  1657. {
  1658. cur = attn_norm;
  1659. ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa());
  1660. cur = ggml_mul_mat(ctx, wqkv, cur);
  1661. 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)));
  1662. 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)));
  1663. 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())));
  1664. Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens);
  1665. Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens);
  1666. // using mode = 2 for neox mode
  1667. Qcur = ggml_rope_ext(
  1668. ctx, Qcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  1669. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1670. );
  1671. Kcur = ggml_rope_ext(
  1672. ctx, Kcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  1673. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1674. );
  1675. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1676. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1677. }
  1678. struct ggml_tensor * ffn_inp = cur;
  1679. // feed forward
  1680. {
  1681. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1682. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1683. cur = attn_norm;
  1684. cur = ggml_mul_mat(ctx, ffn_up, cur);
  1685. cur = ggml_gelu(ctx, cur);
  1686. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1687. }
  1688. cur = ggml_add(ctx, cur, ffn_inp);
  1689. cur = ggml_add(ctx, cur, inpL);
  1690. // input for next layer
  1691. inpL = cur;
  1692. }
  1693. cur = inpL;
  1694. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1695. ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1696. cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM);
  1697. // lm_head
  1698. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab);
  1699. cur = ggml_mul_mat(ctx, output, cur);
  1700. return cur;
  1701. }
  1702. };
  1703. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  1704. std::vector<std::unique_ptr<test_case>> test_cases;
  1705. std::default_random_engine rng(0);
  1706. const ggml_type all_types[] = {
  1707. GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16,
  1708. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  1709. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1710. GGML_TYPE_Q8_0,
  1711. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1712. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  1713. GGML_TYPE_Q6_K,
  1714. GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  1715. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  1716. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  1717. };
  1718. const ggml_type base_types[] = {
  1719. GGML_TYPE_F32, GGML_TYPE_F16,
  1720. GGML_TYPE_Q4_0,
  1721. GGML_TYPE_Q4_K,
  1722. GGML_TYPE_IQ2_XXS
  1723. };
  1724. const ggml_type other_types[] = {
  1725. GGML_TYPE_Q4_1,
  1726. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1727. GGML_TYPE_Q8_0,
  1728. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1729. GGML_TYPE_Q5_K,
  1730. GGML_TYPE_Q6_K,
  1731. GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  1732. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  1733. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  1734. GGML_TYPE_BF16,
  1735. };
  1736. // unary ops
  1737. for (int v : {0, 1}) {
  1738. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  1739. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 128, 10, 10, 10 }, v));
  1740. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 7, 13, 19, 23 }, v));
  1741. }
  1742. }
  1743. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  1744. for (ggml_type type : all_types) {
  1745. for (int b : {1, 7}) {
  1746. for (bool v : {false, true}) {
  1747. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  1748. }
  1749. }
  1750. }
  1751. for (int b : {1, 7}) {
  1752. for (bool v : {false, true}) {
  1753. test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, v));
  1754. }
  1755. }
  1756. for (ggml_type type_input : {GGML_TYPE_F32}) {
  1757. for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) {
  1758. for (int k0 : {1, 3}) {
  1759. for (int k1 : {1, 3}) {
  1760. for (int s0 : {1, 2}) {
  1761. for (int s1 : {1, 2}) {
  1762. for (int p0 : {0, 1}) {
  1763. for (int p1 : {0, 1}) {
  1764. test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1));
  1765. }
  1766. }
  1767. }
  1768. }
  1769. }
  1770. }
  1771. }
  1772. }
  1773. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32));
  1774. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16));
  1775. test_cases.emplace_back(new test_conv_transpose_1d());
  1776. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 3, 0, 1));
  1777. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 2, 0, 1));
  1778. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 1, 0, 1));
  1779. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 2, 0, 1));
  1780. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 1, 0, 1));
  1781. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,1,2,1}, 1, 0, 1));
  1782. test_cases.emplace_back(new test_conv_transpose_1d({2,1,1,1}, {3,1,1,1}, 1, 0, 1));
  1783. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 1}));
  1784. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1785. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 2, 1, 1}));
  1786. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 2, 1}));
  1787. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1788. test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1789. test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1790. test_cases.emplace_back(new test_dup(GGML_TYPE_F32));
  1791. test_cases.emplace_back(new test_dup(GGML_TYPE_F16));
  1792. test_cases.emplace_back(new test_dup(GGML_TYPE_I32));
  1793. test_cases.emplace_back(new test_dup(GGML_TYPE_I16));
  1794. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3}));
  1795. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3}));
  1796. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1797. for (ggml_type type_dst : all_types) {
  1798. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4}));
  1799. }
  1800. }
  1801. test_cases.emplace_back(new test_cont());
  1802. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  1803. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  1804. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  1805. }
  1806. };
  1807. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  1808. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  1809. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  1810. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 1, 1}, {1, 1, 1, 1});
  1811. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 1}, {1, 1, 1, 1});
  1812. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 1});
  1813. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 1, 1, 1});
  1814. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 1, 1});
  1815. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 1});
  1816. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 2});
  1817. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 2});
  1818. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 2, 2});
  1819. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 2, 2, 2});
  1820. // stable diffusion
  1821. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  1822. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  1823. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  1824. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  1825. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  1826. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  1827. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  1828. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  1829. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  1830. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  1831. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  1832. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  1833. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  1834. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  1835. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  1836. test_cases.emplace_back(new test_scale());
  1837. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  1838. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1839. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1840. }
  1841. for (ggml_type type_a : base_types) {
  1842. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1843. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1844. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  1845. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  1846. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  1847. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  1848. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  1849. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  1850. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  1851. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  1852. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  1853. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  1854. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  1855. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  1856. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  1857. }
  1858. }
  1859. for (ggml_type type_a : other_types) {
  1860. for (ggml_type type_b : {GGML_TYPE_F32}) {
  1861. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1862. }
  1863. }
  1864. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 128, { 8, 1}, {1, 1}));
  1865. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 128, { 8, 1}, {4, 1}));
  1866. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 64, { 8, 1}, {4, 1}));
  1867. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 64, { 8, 1}, {4, 1}));
  1868. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 45, 128, { 8, 1}, {4, 1}));
  1869. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 128, 45, 64, { 8, 1}, {4, 1}));
  1870. for (ggml_type type_a : base_types) {
  1871. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1872. for (int n_mats : {4, 8}) {
  1873. for (int n_used : {1, 2, 4}) {
  1874. for (bool b : {false, true}) {
  1875. for (int n : {1, 32}) {
  1876. int m = 512;
  1877. int k = 256;
  1878. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  1879. }
  1880. }
  1881. }
  1882. }
  1883. }
  1884. }
  1885. for (ggml_type type_a : other_types) {
  1886. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1887. for (int n_mats : {4}) {
  1888. for (int n_used : {2}) {
  1889. for (bool b : {false}) {
  1890. for (int n : {1}) {
  1891. int m = 512;
  1892. int k = 256;
  1893. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  1894. }
  1895. }
  1896. }
  1897. }
  1898. }
  1899. }
  1900. test_cases.emplace_back(new test_sqr());
  1901. test_cases.emplace_back(new test_sqrt());
  1902. test_cases.emplace_back(new test_clamp());
  1903. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  1904. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 1}, 5));
  1905. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 10}, 5));
  1906. #if 0
  1907. std::uniform_int_distribution<> dist_ne1(1, 50);
  1908. int exponent = 1;
  1909. while (exponent < (1 << 17)) {
  1910. std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent);
  1911. for (int n = 0; n < 10; ++n) {
  1912. int64_t ne0 = dist_ne0(rng);
  1913. int64_t ne1 = dist_ne1(rng);
  1914. 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));
  1915. }
  1916. exponent <<= 1;
  1917. }
  1918. #endif
  1919. for (bool mask : {false, true}) {
  1920. for (float max_bias : {0.0f, 8.0f}) {
  1921. if (!mask && max_bias > 0.0f) continue;
  1922. for (float scale : {1.0f, 0.1f}) {
  1923. for (int64_t ne0 : {16, 1024}) {
  1924. for (int64_t ne1 : {16, 1024}) {
  1925. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, scale, max_bias));
  1926. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, scale, max_bias));
  1927. }
  1928. }
  1929. }
  1930. }
  1931. }
  1932. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 0.0f));
  1933. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 0.0f));
  1934. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 8.0f));
  1935. {
  1936. bool all = true;
  1937. for (float v : { 0, 1 }) {
  1938. for (float fs : { 1.0f, 1.4245f }) {
  1939. for (float ef : { 0.0f, 0.7465f }) {
  1940. for (float af : { 1.0f, 1.4245f }) {
  1941. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1942. for (bool ff : {false, true}) { // freq_factors
  1943. test_cases.emplace_back(new test_rope(type, {128, 32, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 7B
  1944. if (all) {
  1945. test_cases.emplace_back(new test_rope(type, {128, 40, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 13B
  1946. test_cases.emplace_back(new test_rope(type, {128, 52, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 30B
  1947. test_cases.emplace_back(new test_rope(type, {128, 64, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 65B
  1948. }
  1949. if (all) {
  1950. test_cases.emplace_back(new test_rope(type, { 64, 1, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  1951. test_cases.emplace_back(new test_rope(type, { 64, 71, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  1952. test_cases.emplace_back(new test_rope(type, { 64, 8, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  1953. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 20, 2, 512, fs, ef, af, ff, v)); // neox (stablelm)
  1954. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 32, 2, 512, fs, ef, af, ff, v)); // neox (phi-2)
  1955. }
  1956. test_cases.emplace_back(new test_rope(type, { 64, 128, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  1957. }
  1958. }
  1959. all = false;
  1960. }
  1961. }
  1962. }
  1963. }
  1964. }
  1965. for (int v : { 0, 1, 2, 3 }) {
  1966. for (int dim : { 0, 1, 2, 3, }) {
  1967. test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {11, 12, 13, 14}, 7, dim, v));
  1968. test_cases.emplace_back(new test_concat(GGML_TYPE_I32, {11, 12, 13, 14}, 7, dim, v));
  1969. }
  1970. }
  1971. for (ggml_sort_order order : {GGML_SORT_ORDER_ASC, GGML_SORT_ORDER_DESC}) {
  1972. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  1973. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  1974. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {60, 10, 10, 10}, order)); // qwen
  1975. }
  1976. test_cases.emplace_back(new test_sum_rows());
  1977. test_cases.emplace_back(new test_upscale());
  1978. test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, { 512, 512, 3, 1 }, 2, true));
  1979. test_cases.emplace_back(new test_upscale_ext());
  1980. test_cases.emplace_back(new test_group_norm());
  1981. test_cases.emplace_back(new test_acc());
  1982. test_cases.emplace_back(new test_pad());
  1983. test_cases.emplace_back(new test_arange());
  1984. test_cases.emplace_back(new test_timestep_embedding());
  1985. test_cases.emplace_back(new test_leaky_relu());
  1986. for (int hs : { 64, 80, 128, 256, }) {
  1987. for (bool mask : { true, false } ) {
  1988. for (float max_bias : { 0.0f, 8.0f }) {
  1989. if (!mask && max_bias > 0.0f) continue;
  1990. for (int nh : { 32, }) {
  1991. for (int kv : { 512, 1024, }) {
  1992. for (int nb : { 1, 2, 4, 8, }) {
  1993. for (ggml_type type_KV : {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
  1994. test_cases.emplace_back(new test_flash_attn_ext(hs, nh, kv, nb, mask, max_bias, type_KV));
  1995. }
  1996. }
  1997. }
  1998. }
  1999. }
  2000. }
  2001. }
  2002. // these tests are disabled to save execution time, but they can be handy for debugging
  2003. #if 0
  2004. test_cases.emplace_back(new test_llama(1));
  2005. test_cases.emplace_back(new test_llama(2));
  2006. test_cases.emplace_back(new test_falcon(1));
  2007. test_cases.emplace_back(new test_falcon(2));
  2008. #endif
  2009. // run tests
  2010. if (mode == MODE_TEST) {
  2011. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  2012. size_t n_ok = 0;
  2013. for (auto & test : test_cases) {
  2014. if (test->eval(backend, backend_cpu, op_name)) {
  2015. n_ok++;
  2016. }
  2017. }
  2018. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  2019. ggml_backend_free(backend_cpu);
  2020. return n_ok == test_cases.size();
  2021. }
  2022. if (mode == MODE_PERF) {
  2023. for (auto & test : test_cases) {
  2024. test->eval_perf(backend, op_name);
  2025. }
  2026. return true;
  2027. }
  2028. GGML_ASSERT(false);
  2029. return false;
  2030. }
  2031. static void usage(char ** argv) {
  2032. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  2033. printf(" valid modes are: test (compare with CPU backend for correctness) or perf (performance evaluation)\n");
  2034. printf(" op names are as given by ggml_op_desc()\n");
  2035. }
  2036. int main(int argc, char ** argv) {
  2037. test_mode mode = MODE_TEST;
  2038. const char * op_name_filter = NULL;
  2039. const char * backend_filter = NULL;
  2040. for (int i = 1; i < argc; i++) {
  2041. if (strcmp(argv[i], "test") == 0) {
  2042. mode = MODE_TEST;
  2043. } else if (strcmp(argv[i], "perf") == 0) {
  2044. mode = MODE_PERF;
  2045. } else if (strcmp(argv[i], "-o") == 0) {
  2046. if (i + 1 < argc) {
  2047. op_name_filter = argv[++i];
  2048. } else {
  2049. usage(argv);
  2050. return 1;
  2051. }
  2052. } else if (strcmp(argv[i], "-b") == 0) {
  2053. if (i + 1 < argc) {
  2054. backend_filter = argv[++i];
  2055. } else {
  2056. usage(argv);
  2057. return 1;
  2058. }
  2059. } else {
  2060. usage(argv);
  2061. return 1;
  2062. }
  2063. }
  2064. // enumerate backends
  2065. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  2066. size_t n_ok = 0;
  2067. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  2068. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  2069. if (backend_filter != NULL && strcmp(backend_filter, ggml_backend_reg_get_name(i)) != 0) {
  2070. printf(" Skipping\n");
  2071. n_ok++;
  2072. continue;
  2073. }
  2074. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  2075. GGML_ASSERT(backend != NULL);
  2076. if (backend_filter == NULL && ggml_backend_is_cpu(backend)) {
  2077. printf(" Skipping CPU backend\n");
  2078. ggml_backend_free(backend);
  2079. n_ok++;
  2080. continue;
  2081. }
  2082. printf(" Backend name: %s\n", ggml_backend_name(backend));
  2083. bool ok = test_backend(backend, mode, op_name_filter);
  2084. printf(" Backend %s: ", ggml_backend_name(backend));
  2085. if (ok) {
  2086. printf("\033[1;32mOK\033[0m\n");
  2087. n_ok++;
  2088. } else {
  2089. printf("\033[1;31mFAIL\033[0m\n");
  2090. }
  2091. printf("\n");
  2092. ggml_backend_free(backend);
  2093. }
  2094. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  2095. if (n_ok != ggml_backend_reg_get_count()) {
  2096. printf("\033[1;31mFAIL\033[0m\n");
  2097. return 1;
  2098. }
  2099. ggml_quantize_free();
  2100. printf("\033[1;32mOK\033[0m\n");
  2101. return 0;
  2102. }