test-backend-ops.cpp 75 KB

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