1
0

test-backend-ops.cpp 79 KB

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