test-backend-ops.cpp 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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_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_LEAKY_RELU
  1194. struct test_leaky_relu : public test_case {
  1195. const ggml_type type;
  1196. const std::array<int64_t, 4> ne_a;
  1197. const float negative_slope;
  1198. std::string vars() override {
  1199. return VARS_TO_STR3(type, ne_a, negative_slope);
  1200. }
  1201. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  1202. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1203. float negative_slope = 0.1f)
  1204. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  1205. ggml_tensor * build_graph(ggml_context * ctx) override {
  1206. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1207. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  1208. return out;
  1209. }
  1210. };
  1211. // Mixtral MOE
  1212. struct test_moe : public test_case {
  1213. const int n_experts;
  1214. const int n_experts_per_tok;
  1215. const int n_tokens;
  1216. const int n_embd;
  1217. const int n_ff;
  1218. std::string op_desc(ggml_tensor * t) override {
  1219. return "MOE";
  1220. GGML_UNUSED(t);
  1221. }
  1222. std::string vars() override {
  1223. return VARS_TO_STR5(n_experts, n_experts_per_tok, n_tokens, n_embd, n_ff);
  1224. }
  1225. test_moe(int n_experts = 8, int n_experts_per_tok = 2, int n_tokens = 1, int n_embd = 4096, int n_ff = 14336)
  1226. : n_experts(n_experts), n_experts_per_tok(n_experts_per_tok), n_tokens(n_tokens), n_embd(n_embd), n_ff(n_ff) {
  1227. }
  1228. ggml_tensor * build_graph(ggml_context * ctx) override {
  1229. ggml_tensor * ffn_gate_inp = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_experts);
  1230. std::vector<ggml_tensor *> ffn_up_exp(n_experts);
  1231. std::vector<ggml_tensor *> ffn_gate_exp(n_experts);
  1232. std::vector<ggml_tensor *> ffn_down_exp(n_experts);
  1233. for (int i = 0; i < n_experts; ++i) {
  1234. ffn_up_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff);
  1235. ffn_gate_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff);
  1236. ffn_down_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_ff, n_embd);
  1237. }
  1238. ggml_tensor * cur = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_tokens);
  1239. ggml_tensor * logits = ggml_mul_mat(ctx, ffn_gate_inp, cur);
  1240. ggml_tensor * probs = ggml_soft_max_ext(ctx, logits, nullptr, nullptr, 1.0f/sqrtf(n_embd), 0.0f);
  1241. // select experts
  1242. ggml_tensor * selected_experts = ggml_top_k(ctx, probs, n_experts_per_tok);
  1243. ggml_tensor * weights = ggml_get_rows(ctx,
  1244. ggml_reshape_3d(ctx, probs, 1, n_experts, n_tokens), selected_experts);
  1245. weights = ggml_reshape_2d(ctx, weights, n_experts_per_tok, n_tokens);
  1246. ggml_tensor * weights_sum = ggml_sum_rows(ctx, weights);
  1247. weights = ggml_div(ctx, weights, weights_sum);
  1248. // compute expert outputs
  1249. ggml_tensor * moe_out = nullptr;
  1250. for (int i = 0; i < n_experts_per_tok; ++i) {
  1251. ggml_tensor * cur_expert;
  1252. ggml_tensor * cur_up = ggml_mul_mat_id(ctx, ffn_up_exp.data(), n_experts, selected_experts, i, cur);
  1253. ggml_tensor * cur_gate = ggml_mul_mat_id(ctx, ffn_gate_exp.data(), n_experts, selected_experts, i, cur);
  1254. cur_gate = ggml_silu(ctx, cur_gate);
  1255. cur_expert = ggml_mul(ctx, cur_up, cur_gate);
  1256. cur_expert = ggml_mul_mat_id(ctx, ffn_down_exp.data(), n_experts, selected_experts, i, cur_expert);
  1257. cur_expert = ggml_mul(ctx, cur_expert,
  1258. ggml_view_2d(ctx, weights, 1, n_tokens, weights->nb[1], i*weights->nb[0]));
  1259. if (i == 0) {
  1260. moe_out = cur_expert;
  1261. } else {
  1262. moe_out = ggml_add(ctx, moe_out, cur_expert);
  1263. }
  1264. }
  1265. cur = moe_out;
  1266. return cur;
  1267. }
  1268. };
  1269. enum llm_norm_type {
  1270. LLM_NORM,
  1271. LLM_NORM_RMS,
  1272. };
  1273. struct llama_hparams {
  1274. uint32_t n_vocab;
  1275. uint32_t n_embd;
  1276. uint32_t n_head;
  1277. uint32_t n_head_kv;
  1278. static constexpr uint32_t n_layer = 1;
  1279. uint32_t n_rot;
  1280. uint32_t n_embd_head; // dimension of values (d_v)
  1281. uint32_t n_ff;
  1282. float f_norm_eps;
  1283. float f_norm_rms_eps;
  1284. // cparams
  1285. static constexpr uint32_t n_ctx = 512; // user-specified context size
  1286. static constexpr uint32_t n_orig_ctx = n_ctx;
  1287. // batch
  1288. int32_t n_tokens;
  1289. // llm_build_context
  1290. static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx
  1291. static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache
  1292. uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads
  1293. return n_embd_head * n_head_kv;
  1294. }
  1295. };
  1296. // LLM base class
  1297. struct test_llm : public test_case {
  1298. llama_hparams hp;
  1299. protected:
  1300. test_llm(llama_hparams hp)
  1301. : hp(std::move(hp)) {
  1302. }
  1303. public:
  1304. struct ggml_tensor * llm_build_norm(
  1305. struct ggml_context * ctx,
  1306. struct ggml_tensor * cur,
  1307. struct ggml_tensor * mw,
  1308. struct ggml_tensor * mb,
  1309. llm_norm_type type) {
  1310. switch (type) {
  1311. case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break;
  1312. case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break;
  1313. }
  1314. cur = ggml_mul(ctx, cur, mw);
  1315. if (mb) {
  1316. cur = ggml_add(ctx, cur, mb);
  1317. }
  1318. return cur;
  1319. }
  1320. void llm_build_kv_store(
  1321. struct ggml_context * ctx,
  1322. struct ggml_tensor * k_l,
  1323. struct ggml_tensor * v_l,
  1324. struct ggml_tensor * k_cur,
  1325. struct ggml_tensor * v_cur) {
  1326. // compute the transposed [n_tokens, n_embd] V matrix
  1327. struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens));
  1328. struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(),
  1329. (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head);
  1330. struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(),
  1331. ( hp.n_ctx)*ggml_element_size(v_l),
  1332. (hp.kv_head)*ggml_element_size(v_l));
  1333. // important: storing RoPE-ed version of K in the KV cache!
  1334. ggml_cpy(ctx, k_cur, k_cache_view);
  1335. ggml_cpy(ctx, v_cur_t, v_cache_view);
  1336. }
  1337. struct ggml_tensor * llm_build_kqv(
  1338. struct ggml_context * ctx,
  1339. struct ggml_tensor * k_l,
  1340. struct ggml_tensor * v_l,
  1341. struct ggml_tensor * q_cur,
  1342. struct ggml_tensor * kq_mask,
  1343. float kq_scale) {
  1344. struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3);
  1345. struct ggml_tensor * k =
  1346. ggml_view_3d(ctx, k_l,
  1347. hp.n_embd_head, hp.n_kv, hp.n_head_kv,
  1348. ggml_row_size(k_l->type, hp.n_embd_gqa()),
  1349. ggml_row_size(k_l->type, hp.n_embd_head),
  1350. 0);
  1351. struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q);
  1352. kq = ggml_soft_max_ext(ctx, kq, kq_mask, nullptr, kq_scale, 0.0f);
  1353. // split cached v into n_head heads
  1354. struct ggml_tensor * v =
  1355. ggml_view_3d(ctx, v_l,
  1356. hp.n_kv, hp.n_embd_head, hp.n_head_kv,
  1357. ggml_element_size(v_l)*hp.n_ctx,
  1358. ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head,
  1359. 0);
  1360. struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq);
  1361. struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3);
  1362. struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens);
  1363. struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1364. cur = ggml_mul_mat(ctx, wo, cur);
  1365. return cur;
  1366. }
  1367. void initialize_tensors(ggml_context * ctx) override {
  1368. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1369. if (t->type == GGML_TYPE_I32) {
  1370. // pos
  1371. std::vector<int> data(hp.n_tokens);
  1372. for (int i = 0; i < hp.n_tokens; i++) {
  1373. data[i] = rand() % hp.n_ctx;
  1374. }
  1375. ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int));
  1376. } else {
  1377. init_tensor_uniform(t);
  1378. }
  1379. }
  1380. }
  1381. };
  1382. // Llama
  1383. struct test_llama : public test_llm {
  1384. static constexpr float freq_base = 10000.0f;
  1385. static constexpr float freq_scale = 1.0f;
  1386. static constexpr float ext_factor = 0.0f;
  1387. static constexpr float attn_factor = 1.0f;
  1388. static constexpr float beta_fast = 32.0f;
  1389. static constexpr float beta_slow = 1.0f;
  1390. std::string op_desc(ggml_tensor * t) override {
  1391. GGML_UNUSED(t);
  1392. return "LLAMA";
  1393. }
  1394. std::string vars() override {
  1395. auto n_tokens = hp.n_tokens;
  1396. return VARS_TO_STR1(n_tokens);
  1397. }
  1398. double max_nmse_err() override {
  1399. return 2e-3;
  1400. }
  1401. test_llama(int n_tokens = 1)
  1402. : test_llm({
  1403. /*n_vocab =*/ 32000,
  1404. /*n_embd =*/ 3200,
  1405. /*n_head =*/ 32,
  1406. /*n_head_kv =*/ 32,
  1407. /*n_rot =*/ 100,
  1408. /*n_embd_head =*/ 100,
  1409. /*n_ff =*/ 8640,
  1410. /*f_norm_eps =*/ 0.f,
  1411. /*f_norm_rms_eps =*/ 1e-5f,
  1412. /*n_tokens =*/ n_tokens,
  1413. }) {
  1414. }
  1415. ggml_tensor * build_graph(ggml_context * ctx) override {
  1416. struct ggml_tensor * cur;
  1417. struct ggml_tensor * inpL;
  1418. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1419. // inp_pos - contains the positions
  1420. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1421. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1422. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hp.n_kv, hp.n_tokens, 1);
  1423. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1424. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1425. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1426. struct ggml_tensor * inpSA = inpL;
  1427. // norm
  1428. ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1429. cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS);
  1430. // self-attention
  1431. {
  1432. ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1433. ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1434. ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1435. // compute Q and K and RoPE them
  1436. struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur);
  1437. struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur);
  1438. struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur);
  1439. Qcur = ggml_rope_custom(
  1440. ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos,
  1441. hp.n_rot, 0, 0, hp.n_orig_ctx, freq_base, freq_scale,
  1442. ext_factor, attn_factor, beta_fast, beta_slow
  1443. );
  1444. Kcur = ggml_rope_custom(
  1445. ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos,
  1446. hp.n_rot, 0, 0, hp.n_orig_ctx, freq_base, freq_scale,
  1447. ext_factor, attn_factor, beta_fast, beta_slow
  1448. );
  1449. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1450. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1451. }
  1452. struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA);
  1453. // feed-forward network
  1454. ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1455. cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS);
  1456. ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1457. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1458. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1459. struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur);
  1460. cur = ggml_mul_mat(ctx, ffn_gate, cur);
  1461. cur = ggml_silu(ctx, cur);
  1462. cur = ggml_mul(ctx, cur, tmp);
  1463. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1464. cur = ggml_add(ctx, cur, ffn_inp);
  1465. // input for next layer
  1466. inpL = cur;
  1467. }
  1468. cur = inpL;
  1469. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1470. cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS);
  1471. // lm_head
  1472. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab);
  1473. cur = ggml_mul_mat(ctx, output, cur);
  1474. return cur;
  1475. }
  1476. };
  1477. // Falcon
  1478. struct test_falcon : public test_llm {
  1479. static constexpr float freq_base = 10000.0f;
  1480. static constexpr float freq_scale = 1.0f;
  1481. static constexpr float ext_factor = 0.0f;
  1482. static constexpr float attn_factor = 1.0f;
  1483. static constexpr float beta_fast = 32.0f;
  1484. static constexpr float beta_slow = 1.0f;
  1485. std::string op_desc(ggml_tensor * t) override {
  1486. GGML_UNUSED(t);
  1487. return "FALCON";
  1488. }
  1489. std::string vars() override {
  1490. auto n_tokens = hp.n_tokens;
  1491. return VARS_TO_STR1(n_tokens);
  1492. }
  1493. double max_nmse_err() override {
  1494. return 2e-3;
  1495. }
  1496. test_falcon(int n_tokens = 1)
  1497. : test_llm({
  1498. /*n_vocab =*/ 32000,
  1499. /*n_embd =*/ 3200,
  1500. /*n_head =*/ 50,
  1501. /*n_head_kv =*/ 1,
  1502. /*n_rot =*/ 64,
  1503. /*n_embd_head =*/ 64,
  1504. /*n_ff =*/ 8640,
  1505. /*f_norm_eps =*/ 1e-5f,
  1506. /*f_norm_rms_eps =*/ 0.f,
  1507. /*n_tokens =*/ n_tokens,
  1508. }) {
  1509. }
  1510. ggml_tensor * build_graph(ggml_context * ctx) override {
  1511. struct ggml_tensor * cur;
  1512. struct ggml_tensor * inpL;
  1513. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1514. // inp_pos - contains the positions
  1515. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1516. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1517. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hp.n_kv, hp.n_tokens, 1);
  1518. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1519. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1520. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1521. // norm
  1522. ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1523. ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1524. ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM);
  1525. // self-attention
  1526. {
  1527. cur = attn_norm;
  1528. ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa());
  1529. cur = ggml_mul_mat(ctx, wqkv, cur);
  1530. 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)));
  1531. 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)));
  1532. 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())));
  1533. Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens);
  1534. Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens);
  1535. // using mode = 2 for neox mode
  1536. Qcur = ggml_rope_custom(
  1537. ctx, Qcur, inp_pos, hp.n_rot, 2, 0, hp.n_orig_ctx,
  1538. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1539. );
  1540. Kcur = ggml_rope_custom(
  1541. ctx, Kcur, inp_pos, hp.n_rot, 2, 0, hp.n_orig_ctx,
  1542. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1543. );
  1544. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1545. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1546. }
  1547. struct ggml_tensor * ffn_inp = cur;
  1548. // feed forward
  1549. {
  1550. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1551. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1552. cur = attn_norm;
  1553. cur = ggml_mul_mat(ctx, ffn_up, cur);
  1554. cur = ggml_gelu(ctx, cur);
  1555. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1556. }
  1557. cur = ggml_add(ctx, cur, ffn_inp);
  1558. cur = ggml_add(ctx, cur, inpL);
  1559. // input for next layer
  1560. inpL = cur;
  1561. }
  1562. cur = inpL;
  1563. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1564. ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1565. cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM);
  1566. // lm_head
  1567. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab);
  1568. cur = ggml_mul_mat(ctx, output, cur);
  1569. return cur;
  1570. }
  1571. };
  1572. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  1573. std::vector<std::unique_ptr<test_case>> test_cases;
  1574. std::default_random_engine rng(0);
  1575. const ggml_type all_types[] = {
  1576. GGML_TYPE_F32, GGML_TYPE_F16,
  1577. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  1578. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1579. GGML_TYPE_Q8_0,
  1580. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1581. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  1582. GGML_TYPE_Q6_K,
  1583. GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS,
  1584. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S,
  1585. };
  1586. // unary ops
  1587. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  1588. test_cases.emplace_back(new test_unary((ggml_unary_op) op));
  1589. }
  1590. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  1591. for (ggml_type type : all_types) {
  1592. for (int b : {1, 7}) {
  1593. for (bool v : {false, true}) {
  1594. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  1595. }
  1596. }
  1597. }
  1598. for (int b : {1, 7}) {
  1599. for (bool v : {false, true}) {
  1600. test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, v));
  1601. }
  1602. }
  1603. for (ggml_type type_input : {GGML_TYPE_F32}) {
  1604. for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) {
  1605. for (int k0 : {1, 3}) {
  1606. for (int k1 : {1, 3}) {
  1607. for (int s0 : {1, 2}) {
  1608. for (int s1 : {1, 2}) {
  1609. for (int p0 : {0, 1}) {
  1610. for (int p1 : {0, 1}) {
  1611. test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1));
  1612. }
  1613. }
  1614. }
  1615. }
  1616. }
  1617. }
  1618. }
  1619. }
  1620. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32));
  1621. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16));
  1622. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 1}));
  1623. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1624. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 2, 1, 1}));
  1625. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 2, 1}));
  1626. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1627. test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1628. test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1629. test_cases.emplace_back(new test_dup(GGML_TYPE_F32));
  1630. test_cases.emplace_back(new test_dup(GGML_TYPE_F16));
  1631. test_cases.emplace_back(new test_dup(GGML_TYPE_I32));
  1632. test_cases.emplace_back(new test_dup(GGML_TYPE_I16));
  1633. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3}));
  1634. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3}));
  1635. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1636. for (ggml_type type_dst : all_types) {
  1637. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4}));
  1638. }
  1639. }
  1640. test_cases.emplace_back(new test_cont());
  1641. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  1642. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  1643. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  1644. }
  1645. };
  1646. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  1647. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  1648. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  1649. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 1, 1}, {1, 1, 1, 1});
  1650. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 1}, {1, 1, 1, 1});
  1651. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 1});
  1652. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 1, 1, 1});
  1653. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 1, 1});
  1654. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 1});
  1655. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 2});
  1656. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 2});
  1657. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 2, 2});
  1658. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 2, 2, 2});
  1659. // stable diffusion
  1660. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  1661. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  1662. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  1663. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  1664. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  1665. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  1666. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  1667. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  1668. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  1669. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  1670. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  1671. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  1672. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  1673. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  1674. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  1675. test_cases.emplace_back(new test_scale());
  1676. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  1677. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1678. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1679. }
  1680. for (ggml_type type_a : all_types) {
  1681. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1682. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1683. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  1684. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  1685. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  1686. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  1687. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  1688. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  1689. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  1690. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  1691. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  1692. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  1693. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  1694. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  1695. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  1696. }
  1697. }
  1698. for (ggml_type type_a : all_types) {
  1699. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1700. for (int n_mats : {2, 4, 8}) {
  1701. for (int id = 0; id < n_mats; id++) {
  1702. for (bool v : {false, true}) {
  1703. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, id, 16, 16, 256, v));
  1704. }
  1705. }
  1706. }
  1707. }
  1708. }
  1709. test_cases.emplace_back(new test_sqr());
  1710. test_cases.emplace_back(new test_clamp());
  1711. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  1712. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 1}, 5));
  1713. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 10}, 5));
  1714. #if 0
  1715. std::uniform_int_distribution<> dist_ne1(1, 50);
  1716. int exponent = 1;
  1717. while (exponent < (1 << 17)) {
  1718. std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent);
  1719. for (int n = 0; n < 10; ++n) {
  1720. int64_t ne0 = dist_ne0(rng);
  1721. int64_t ne1 = dist_ne1(rng);
  1722. 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));
  1723. }
  1724. exponent <<= 1;
  1725. }
  1726. #endif
  1727. for (bool mask : {false, true}) {
  1728. for (float max_bias : {0.0f, 8.0f}) {
  1729. for (float scale : {1.0f, 0.1f}) {
  1730. for (int64_t ne0 : {16, 1024}) {
  1731. for (int64_t ne1 : {16, 1024}) {
  1732. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, scale, max_bias));
  1733. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, scale, max_bias));
  1734. }
  1735. }
  1736. }
  1737. }
  1738. }
  1739. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 0.0f));
  1740. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 0.0f));
  1741. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 8.0f));
  1742. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 8.0f));
  1743. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1744. test_cases.emplace_back(new test_rope(type, {128, 32, 10, 1}, 128, 0, 512)); // llama 7B
  1745. test_cases.emplace_back(new test_rope(type, {128, 40, 10, 1}, 128, 0, 512)); // llama 13B
  1746. test_cases.emplace_back(new test_rope(type, {128, 52, 10, 1}, 128, 0, 512)); // llama 30B
  1747. test_cases.emplace_back(new test_rope(type, {128, 64, 10, 1}, 128, 0, 512)); // llama 65B
  1748. test_cases.emplace_back(new test_rope(type, { 64, 1, 10, 1}, 64, 2, 512)); // neox (falcon 7B)
  1749. test_cases.emplace_back(new test_rope(type, { 64, 71, 10, 1}, 64, 2, 512)); // neox (falcon 7B)
  1750. test_cases.emplace_back(new test_rope(type, { 64, 8, 10, 1}, 64, 2, 512)); // neox (falcon 40B)
  1751. test_cases.emplace_back(new test_rope(type, { 64, 128, 10, 1}, 64, 2, 512)); // neox (falcon 40B)
  1752. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 20, 2, 512)); // neox (stablelm)
  1753. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 32, 2, 512)); // neox (phi-2)
  1754. }
  1755. test_cases.emplace_back(new test_concat(GGML_TYPE_F32));
  1756. test_cases.emplace_back(new test_concat(GGML_TYPE_I32));
  1757. for (ggml_sort_order order : {GGML_SORT_ASC, GGML_SORT_DESC}) {
  1758. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  1759. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  1760. }
  1761. test_cases.emplace_back(new test_sum_rows());
  1762. test_cases.emplace_back(new test_upscale());
  1763. test_cases.emplace_back(new test_group_norm());
  1764. test_cases.emplace_back(new test_acc());
  1765. test_cases.emplace_back(new test_pad());
  1766. test_cases.emplace_back(new test_leaky_relu());
  1767. // these tests are disabled to save execution time, but they can be handy for debugging
  1768. #if 0
  1769. #if !defined(__SANITIZE_THREAD__)
  1770. // FIXME: these tests use too much memory with thread sanitizer
  1771. test_cases.emplace_back(new test_moe(8, 2, 1, 4096, 8*1024));
  1772. //test_cases.emplace_back(new test_moe(8, 2, 8, 4096, 14336));
  1773. #endif
  1774. test_cases.emplace_back(new test_llama(1));
  1775. test_cases.emplace_back(new test_llama(2));
  1776. test_cases.emplace_back(new test_falcon(1));
  1777. test_cases.emplace_back(new test_falcon(2));
  1778. #endif
  1779. // run tests
  1780. if (mode == MODE_TEST) {
  1781. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  1782. size_t n_ok = 0;
  1783. for (auto & test : test_cases) {
  1784. if (test->eval(backend, backend_cpu, op_name)) {
  1785. n_ok++;
  1786. }
  1787. }
  1788. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  1789. ggml_backend_free(backend_cpu);
  1790. return n_ok == test_cases.size();
  1791. }
  1792. if (mode == MODE_PERF) {
  1793. for (auto & test : test_cases) {
  1794. test->eval_perf(backend, op_name);
  1795. }
  1796. return true;
  1797. }
  1798. GGML_ASSERT(false);
  1799. return false;
  1800. }
  1801. static void usage(char ** argv) {
  1802. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  1803. printf(" valid modes are: test (compare with CPU backend for correctness) or perf (performance evaluation)\n");
  1804. printf(" op names are as given by ggml_op_desc()\n");
  1805. }
  1806. int main(int argc, char ** argv) {
  1807. test_mode mode = MODE_TEST;
  1808. const char * op_name = NULL;
  1809. const char * backend = NULL;
  1810. for (int i = 1; i < argc; i++) {
  1811. if (strcmp(argv[i], "test") == 0) {
  1812. mode = MODE_TEST;
  1813. } else if (strcmp(argv[i], "perf") == 0) {
  1814. mode = MODE_PERF;
  1815. } else if (strcmp(argv[i], "-o") == 0) {
  1816. if (i + 1 < argc) {
  1817. op_name = argv[++i];
  1818. } else {
  1819. usage(argv);
  1820. return 1;
  1821. }
  1822. } else if (strcmp(argv[i], "-b") == 0) {
  1823. if (i + 1 < argc) {
  1824. backend = argv[++i];
  1825. } else {
  1826. usage(argv);
  1827. return 1;
  1828. }
  1829. } else {
  1830. usage(argv);
  1831. return 1;
  1832. }
  1833. }
  1834. // enumerate backends
  1835. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  1836. size_t n_ok = 0;
  1837. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  1838. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  1839. if (backend != NULL && strcmp(backend, ggml_backend_reg_get_name(i)) != 0) {
  1840. printf(" Skipping\n");
  1841. n_ok++;
  1842. continue;
  1843. }
  1844. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  1845. GGML_ASSERT(backend != NULL);
  1846. printf(" Backend name: %s\n", ggml_backend_name(backend));
  1847. bool ok = test_backend(backend, mode, op_name);
  1848. printf(" Backend %s: ", ggml_backend_name(backend));
  1849. if (ok) {
  1850. printf("\033[1;32mOK\033[0m\n");
  1851. n_ok++;
  1852. } else {
  1853. printf("\033[1;31mFAIL\033[0m\n");
  1854. }
  1855. printf("\n");
  1856. ggml_backend_free(backend);
  1857. }
  1858. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  1859. if (n_ok != ggml_backend_reg_get_count()) {
  1860. printf("\033[1;31mFAIL\033[0m\n");
  1861. return 1;
  1862. }
  1863. ggml_quantize_free();
  1864. printf("\033[1;32mOK\033[0m\n");
  1865. return 0;
  1866. }