test-backend-ops.cpp 93 KB

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