test-backend-ops.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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. size_t size = ggml_nelements(tensor);
  19. std::vector<float> data(size);
  20. #if 0
  21. std::default_random_engine generator(rd());
  22. std::uniform_real_distribution<float> distribution(min, max);
  23. for (size_t i = 0; i < size; i++) {
  24. data[i] = distribution(generator);
  25. }
  26. #endif
  27. auto init_thread = [&](size_t start, size_t end) {
  28. std::random_device rd;
  29. std::default_random_engine generator(rd());
  30. std::uniform_real_distribution<float> distribution(min, max);
  31. for (size_t i = start; i < end; i++) {
  32. data[i] = distribution(generator);
  33. }
  34. };
  35. size_t n_threads = std::thread::hardware_concurrency();
  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, 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. ggml_quantize_chunk(tensor->type, data.data(), dataq.data(), 0, size, hist);
  53. ggml_backend_tensor_set(tensor, dataq.data(), 0, dataq.size());
  54. } else {
  55. GGML_ASSERT(false);
  56. }
  57. }
  58. static std::vector<float> tensor_to_float(const ggml_tensor * t) {
  59. std::vector<float> tv;
  60. tv.reserve(ggml_nelements(t));
  61. std::vector<uint8_t> buf(ggml_nbytes(t));
  62. ggml_backend_tensor_get(t, buf.data(), 0, ggml_nbytes(t));
  63. ggml_type_traits_t tt = ggml_internal_get_type_traits(t->type);
  64. size_t bs = ggml_blck_size(t->type);
  65. std::vector<float> vq(ggml_blck_size(t->type));
  66. bool quantized = ggml_is_quantized(t->type);
  67. // access elements by index to avoid gaps in views
  68. for (int64_t i3 = 0; i3 < t->ne[3]; i3++) {
  69. for (int64_t i2 = 0; i2 < t->ne[2]; i2++) {
  70. for (int64_t i1 = 0; i1 < t->ne[1]; i1++) {
  71. for (int64_t i0 = 0; i0 < t->ne[0]; i0 += bs) {
  72. size_t i = i3*t->nb[3] + i2*t->nb[2] + i1*t->nb[1] + i0/bs*t->nb[0];
  73. if (t->type == GGML_TYPE_F16) {
  74. tv.push_back(ggml_fp16_to_fp32(*(ggml_fp16_t*)&buf[i]));
  75. } else if (t->type == GGML_TYPE_F32) {
  76. tv.push_back(*(float *) &buf[i]);
  77. } else if (t->type == GGML_TYPE_I32) {
  78. tv.push_back((float)*(int32_t *) &buf[i]);
  79. } else if (quantized) {
  80. tt.to_float(&buf[i], vq.data(), bs);
  81. tv.insert(tv.end(), vq.begin(), vq.end());
  82. } else {
  83. GGML_ASSERT(false);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. return tv;
  90. }
  91. /*
  92. static double cosine_similarity(const float * v1, const float * v2, size_t n) {
  93. double dot = 0.0;
  94. double mag1 = 0.0;
  95. double mag2 = 0.0;
  96. for (size_t i = 0; i < n; i++) {
  97. if (std::isnan(v1[i]) || std::isnan(v2[i])) {
  98. return -1.0f;
  99. }
  100. if (std::isinf(v1[i]) && std::isinf(v2[i])) {
  101. continue;
  102. }
  103. dot += v1[i]*v2[i];
  104. mag1 += v1[i]*v1[i];
  105. mag2 += v2[i]*v2[i];
  106. }
  107. return dot/sqrt(mag1*mag2);
  108. }
  109. static float distance(const float * v1, const float * v2, size_t n) {
  110. double d = 0.0;
  111. for (size_t i = 0; i < n; i++) {
  112. if (std::isnan(v1[i]) || std::isnan(v2[i])) {
  113. return INFINITY;
  114. }
  115. if (std::isinf(v1[i]) && std::isinf(v2[i])) {
  116. continue;
  117. }
  118. d += (v1[i] - v2[i])*(v1[i] - v2[i]);
  119. }
  120. return sqrt(d);
  121. }
  122. static float vec_len(const float * v, size_t n) {
  123. double d = 0.0;
  124. for (size_t i = 0; i < n; i++) {
  125. if (std::isnan(v[i])) {
  126. return INFINITY;
  127. }
  128. if (std::isinf(v[i])) {
  129. continue;
  130. }
  131. d += v[i]*v[i];
  132. }
  133. return sqrt(d);
  134. }
  135. */
  136. // normalized mean squared error = mse(a, b) / mse(a, 0)
  137. static double nmse(const float * a, const float * b, size_t n) {
  138. double mse_a_b = 0.0;
  139. double mse_a_0 = 0.0;
  140. for (size_t i = 0; i < n; i++) {
  141. float a_i = a[i];
  142. float b_i = b[i];
  143. mse_a_b += (a_i - b_i) * (a_i - b_i);
  144. mse_a_0 += a_i * a_i;
  145. }
  146. return mse_a_b / mse_a_0;
  147. }
  148. // utils for printing the variables of the test cases
  149. #define VAR_TO_STR(x) (#x "=" + var_to_str(x))
  150. template<typename T>
  151. static std::string var_to_str(const T & x) {
  152. return std::to_string(x);
  153. }
  154. template<typename T, size_t N>
  155. static std::string var_to_str(const T (&x)[N]) {
  156. std::string s = "[";
  157. for (size_t i = 0; i < N; i++) {
  158. if (i > 0) {
  159. s += ",";
  160. }
  161. s += var_to_str(x[i]);
  162. }
  163. s += "]";
  164. return s;
  165. }
  166. template<typename T, size_t N>
  167. static std::string var_to_str(const std::array<T, N> & x) {
  168. std::string s = "[";
  169. for (size_t i = 0; i < N; i++) {
  170. if (i > 0) {
  171. s += ",";
  172. }
  173. s += var_to_str(x[i]);
  174. }
  175. s += "]";
  176. return s;
  177. }
  178. //static std::string var_to_str(ggml_unary_op unary_op) {
  179. // return ggml_unary_op_name(unary_op);
  180. //}
  181. static std::string var_to_str(ggml_type type) {
  182. return ggml_type_name(type);
  183. }
  184. #define VARS_TO_STR1(a) VAR_TO_STR(a)
  185. #define VARS_TO_STR2(a, b) VAR_TO_STR(a) + "," + VAR_TO_STR(b)
  186. #define VARS_TO_STR3(a, b, c) VAR_TO_STR(a) + "," + VARS_TO_STR2(b, c)
  187. #define VARS_TO_STR4(a, b, c, d) VAR_TO_STR(a) + "," + VARS_TO_STR3(b, c, d)
  188. #define VARS_TO_STR5(a, b, c, d, e) VAR_TO_STR(a) + "," + VARS_TO_STR4(b, c, d, e)
  189. #define VARS_TO_STR6(a, b, c, d, e, f) VAR_TO_STR(a) + "," + VARS_TO_STR5(b, c, d, e, f)
  190. #define VARS_TO_STR7(a, b, c, d, e, f, g) VAR_TO_STR(a) + "," + VARS_TO_STR6(b, c, d, e, f, g)
  191. #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)
  192. #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)
  193. #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)
  194. #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)
  195. // accept FLT_MAX as infinity
  196. static bool isinf_or_max(float f) {
  197. return std::isinf(f) || f == FLT_MAX || f == -FLT_MAX;
  198. }
  199. static bool ggml_is_view_op(enum ggml_op op) {
  200. return op == GGML_OP_VIEW || op == GGML_OP_RESHAPE || op == GGML_OP_PERMUTE || op == GGML_OP_TRANSPOSE;
  201. }
  202. enum test_mode {
  203. MODE_TEST,
  204. MODE_PERF,
  205. };
  206. struct test_case {
  207. virtual ~test_case() {}
  208. virtual std::string op_desc(ggml_tensor * t) {
  209. return ggml_op_desc(t);
  210. }
  211. virtual std::string vars() {
  212. return "";
  213. }
  214. virtual ggml_tensor * build_graph(ggml_context * ctx) = 0;
  215. virtual double max_nmse_err() {
  216. return 1e-7;
  217. }
  218. virtual void initialize_tensors(ggml_context * ctx) {
  219. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
  220. init_tensor_uniform(t);
  221. }
  222. }
  223. virtual size_t op_size(ggml_tensor * t) {
  224. size_t size = ggml_nbytes(t);
  225. // add source tensors
  226. for (int i = 0; i < GGML_MAX_SRC; i++) {
  227. if (t->src[i] != NULL) {
  228. size += ggml_nbytes(t->src[i]);
  229. }
  230. }
  231. return size;
  232. }
  233. ggml_cgraph * gf = nullptr;
  234. static const int sentinel_size = 1024;
  235. test_mode mode;
  236. std::vector<ggml_tensor *> sentinels;
  237. void add_sentinel(ggml_context * ctx) {
  238. if (mode == MODE_PERF) {
  239. return;
  240. }
  241. ggml_tensor * sentinel = ::ggml_new_tensor_1d(ctx, GGML_TYPE_F32, sentinel_size);
  242. ggml_format_name(sentinel, "sent_%zu", sentinels.size());
  243. sentinels.push_back(sentinel);
  244. }
  245. // hijack ggml_new_tensor to add sentinels after each tensor to check for overflows in the backend
  246. ggml_tensor * ggml_new_tensor(ggml_context * ctx, ggml_type type, int n_dims, const int64_t * ne) {
  247. ggml_tensor * t = ::ggml_new_tensor(ctx, type, n_dims, ne);
  248. add_sentinel(ctx);
  249. return t;
  250. }
  251. ggml_tensor * ggml_new_tensor_1d(ggml_context * ctx, ggml_type type, int64_t ne0) {
  252. ggml_tensor * t = ::ggml_new_tensor_1d(ctx, type, ne0);
  253. add_sentinel(ctx);
  254. return t;
  255. }
  256. ggml_tensor * ggml_new_tensor_2d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1) {
  257. ggml_tensor * t = ::ggml_new_tensor_2d(ctx, type, ne0, ne1);
  258. add_sentinel(ctx);
  259. return t;
  260. }
  261. ggml_tensor * ggml_new_tensor_3d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2) {
  262. ggml_tensor * t = ::ggml_new_tensor_3d(ctx, type, ne0, ne1, ne2);
  263. add_sentinel(ctx);
  264. return t;
  265. }
  266. ggml_tensor * ggml_new_tensor_4d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3) {
  267. ggml_tensor * t = ::ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3);
  268. add_sentinel(ctx);
  269. return t;
  270. }
  271. bool eval(ggml_backend_t backend1, ggml_backend_t backend2, const char * op_name) {
  272. mode = MODE_TEST;
  273. ggml_init_params params = {
  274. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead(),
  275. /* .mem_base = */ NULL,
  276. /* .no_alloc = */ true,
  277. };
  278. ggml_context * ctx = ggml_init(params);
  279. gf = ggml_new_graph(ctx);
  280. // pre-graph sentinel
  281. add_sentinel(ctx);
  282. ggml_tensor * out = build_graph(ctx);
  283. if (op_name != nullptr && op_desc(out) != op_name) {
  284. //printf(" %s: skipping\n", op_desc(out).c_str());
  285. ggml_free(ctx);
  286. return true;
  287. }
  288. printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  289. fflush(stdout);
  290. // check if backends support op
  291. for (ggml_backend_t backend : {backend1, backend2}) {
  292. if (!ggml_backend_supports_op(backend, out)) {
  293. printf("not supported\n");
  294. ggml_free(ctx);
  295. return true;
  296. }
  297. }
  298. // post-graph sentinel
  299. add_sentinel(ctx);
  300. // allocate
  301. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend1);
  302. // build graph
  303. ggml_build_forward_expand(gf, out);
  304. // add sentinels as graph nodes so that they are checked in the callback
  305. for (ggml_tensor * sentinel : sentinels) {
  306. gf->nodes[gf->n_nodes++] = sentinel;
  307. }
  308. // randomize tensors
  309. initialize_tensors(ctx);
  310. // compare
  311. struct callback_userdata {
  312. bool ok;
  313. double max_err;
  314. };
  315. callback_userdata ud {
  316. true,
  317. max_nmse_err(),
  318. };
  319. auto callback = [](int index, ggml_tensor * t1, ggml_tensor * t2, void * user_data) -> bool {
  320. callback_userdata * ud = (callback_userdata *) user_data;
  321. if (t1->op == GGML_OP_NONE) {
  322. // sentinels must be unchanged
  323. std::vector<uint8_t> t1_data(ggml_nbytes(t1));
  324. std::vector<uint8_t> t2_data(ggml_nbytes(t2));
  325. ggml_backend_tensor_get(t1, t1_data.data(), 0, ggml_nbytes(t1));
  326. ggml_backend_tensor_get(t2, t2_data.data(), 0, ggml_nbytes(t2));
  327. if (memcmp(t1_data.data(), t2_data.data(), ggml_nbytes(t1)) != 0) {
  328. printf("sentinel mismatch: %s ", t1->name);
  329. ud->ok = false;
  330. return true;
  331. }
  332. }
  333. std::vector<float> f1 = tensor_to_float(t1);
  334. std::vector<float> f2 = tensor_to_float(t2);
  335. for (size_t i = 0; i < f1.size(); i++) {
  336. // check for nans
  337. if (std::isnan(f1[i]) || std::isnan(f2[i])) {
  338. printf("[%s] NaN at index %zu (%f %f) ", ggml_op_desc(t1), i, f1[i], f2[i]);
  339. ud->ok = false;
  340. return true;
  341. }
  342. // check for infs: both must be inf of the same sign, or both must be finite
  343. if (isinf_or_max(f1[i]) || isinf_or_max(f2[i])) {
  344. if (isinf_or_max(f1[i]) && isinf_or_max(f2[i])) {
  345. if (std::signbit(f1[i]) != std::signbit(f2[i])) {
  346. printf("[%s] inf sign mismatch: %f %f ", ggml_op_desc(t1), f1[i], f2[i]);
  347. ud->ok = false;
  348. return true;
  349. }
  350. } else {
  351. printf("[%s] inf mismatch: %f %f ", ggml_op_desc(t1), f1[i], f2[i]);
  352. ud->ok = false;
  353. return true;
  354. }
  355. }
  356. }
  357. double err = nmse(f1.data(), f2.data(), f1.size());
  358. if (err > ud->max_err) {
  359. printf("[%s] NMSE = %f ", ggml_op_desc(t1), err);
  360. //for (int i = 0; i < f1.size(); i++) {
  361. // printf("%5d %9.6f %9.6f, diff = %9.6f\n", i, f1[i], f2[i], f1[i] - f2[i]);
  362. //}
  363. //printf("\n");
  364. //exit(1);
  365. ud->ok = false;
  366. }
  367. return true;
  368. GGML_UNUSED(index);
  369. };
  370. ggml_backend_compare_graph_backend(backend1, backend2, gf, callback, &ud);
  371. if (ud.ok) {
  372. printf("\033[1;32mOK\033[0m\n");
  373. } else {
  374. printf("\033[1;31mFAIL\033[0m\n");
  375. }
  376. ggml_backend_buffer_free(buf);
  377. ggml_free(ctx);
  378. return ud.ok;
  379. }
  380. bool eval_perf(ggml_backend_t backend, const char * op_name) {
  381. mode = MODE_PERF;
  382. static const size_t graph_nodes = 8192;
  383. ggml_init_params params = {
  384. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead_custom(graph_nodes, false),
  385. /* .mem_base = */ NULL,
  386. /* .no_alloc = */ true,
  387. };
  388. ggml_context * ctx = ggml_init(params);
  389. ggml_tensor * out = build_graph(ctx);
  390. if (op_name != nullptr && op_desc(out) != op_name) {
  391. //printf(" %s: skipping\n", op_desc(out).c_str());
  392. ggml_free(ctx);
  393. return true;
  394. }
  395. int len = printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  396. fflush(stdout);
  397. // check if backends support op
  398. if (!ggml_backend_supports_op(backend, out)) {
  399. printf("not supported\n");
  400. ggml_free(ctx);
  401. return true;
  402. }
  403. // align while also leaving some margin for variations in parameters
  404. int align = 20;
  405. int last = (len + align - 1) / align * align;
  406. if (last - len < 5) {
  407. last += align;
  408. }
  409. last = std::max(last, 60);
  410. printf("%*s", last - len, "");
  411. // allocate
  412. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend);
  413. // randomize tensors
  414. initialize_tensors(ctx);
  415. // build graph
  416. ggml_cgraph * gf = ggml_new_graph_custom(ctx, graph_nodes, false);
  417. ggml_build_forward_expand(gf, out);
  418. // warmup run
  419. ggml_backend_graph_compute(backend, gf);
  420. // duplicate the op
  421. size_t target_size = ggml_backend_is_cpu(backend) ? 1ULL << 33 : 1ULL << 35; // 8 GB CPU, 32 GB GPU
  422. int n_runs = std::min((size_t)gf->size - gf->n_nodes, target_size / op_size(out)) + 1;
  423. for (int i = 1; i < n_runs; i++) {
  424. gf->nodes[gf->n_nodes++] = out;
  425. }
  426. // calculate memory
  427. size_t mem = n_runs * op_size(out);
  428. auto tensor_op_size = [](ggml_tensor * t) {
  429. size_t size = ggml_nbytes(t);
  430. // add source tensors
  431. for (int i = 0; i < GGML_MAX_SRC; i++) {
  432. if (t->src[i] != NULL) {
  433. size += ggml_nbytes(t->src[i]);
  434. }
  435. }
  436. return size;
  437. };
  438. for (int i = 0; i < gf->n_nodes; i++) {
  439. if (ggml_is_view_op(gf->nodes[i]->op) || gf->nodes[i] == out) {
  440. continue;
  441. }
  442. mem += tensor_op_size(gf->nodes[i]);
  443. }
  444. // run
  445. ggml_backend_synchronize(backend);
  446. int64_t start_time = ggml_time_us();
  447. ggml_backend_graph_compute(backend, gf);
  448. ggml_backend_synchronize(backend);
  449. int64_t end_time = ggml_time_us();
  450. double time_us = end_time - start_time;
  451. printf(" %5d runs - %8.2f us/run - %8zu kB/run - \033[1;34m%7.2f GB/s\033[0m\n",
  452. n_runs,
  453. time_us / n_runs,
  454. op_size(out) / 1024,
  455. mem / (time_us/1e6) / 1024.0 / 1024.0 / 1024.0);
  456. ggml_backend_buffer_free(buf);
  457. ggml_free(ctx);
  458. return true;
  459. }
  460. };
  461. // GGML_OP_UNARY
  462. struct test_unary : public test_case {
  463. const ggml_unary_op op;
  464. const ggml_type type;
  465. const std::array<int64_t, 4> ne;
  466. std::string vars() override {
  467. return VARS_TO_STR2(type, ne);
  468. }
  469. test_unary(ggml_unary_op op,
  470. ggml_type type = GGML_TYPE_F32,
  471. std::array<int64_t, 4> ne = {128, 10, 10, 10})
  472. : op(op), type(type), ne(ne) {}
  473. ggml_tensor * build_graph(ggml_context * ctx) override {
  474. ggml_tensor * in = ggml_new_tensor(ctx, type, 4, ne.data());
  475. ggml_tensor * out = ggml_unary(ctx, in, op);
  476. return out;
  477. }
  478. };
  479. // GGML_OP_GET_ROWS
  480. struct test_get_rows : public test_case {
  481. const ggml_type type;
  482. const int n; // cols
  483. const int m; // rows
  484. const int r; // rows to get
  485. const int b; // batch size
  486. const bool v; // view (non-contiguous src1)
  487. std::string vars() override {
  488. return VARS_TO_STR6(type, n, m, r, b, v);
  489. }
  490. test_get_rows(ggml_type type = GGML_TYPE_F32, int n = 10, int m = 5, int r = 3, int b = 1, bool v = false)
  491. : type(type), n(n), m(m), r(r), b(b), v(v) {}
  492. ggml_tensor * build_graph(ggml_context * ctx) override {
  493. ggml_tensor * in = ggml_new_tensor_3d(ctx, type, n, m, b);
  494. ggml_tensor * rows = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, r, b);
  495. if (v) {
  496. rows = ggml_view_2d(ctx, rows, r/2, b, rows->nb[1], 0);
  497. }
  498. ggml_tensor * out = ggml_get_rows(ctx, in, rows);
  499. return out;
  500. }
  501. void initialize_tensors(ggml_context * ctx) override {
  502. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  503. if (t->type == GGML_TYPE_I32) {
  504. if (ggml_is_view_op(t->op)) { continue; }
  505. // rows
  506. std::vector<int> data(r*b);
  507. for (int i = 0; i < r*b; i++) {
  508. data[i] = rand() % m;
  509. }
  510. ggml_backend_tensor_set(t, data.data(), 0, r * b * sizeof(int));
  511. } else {
  512. init_tensor_uniform(t);
  513. }
  514. }
  515. }
  516. };
  517. // GGML_OP_REPEAT
  518. struct test_repeat : public test_case {
  519. const ggml_type type;
  520. const std::array<int64_t, 4> ne;
  521. const std::array<int, 4> nr;
  522. std::string vars() override {
  523. return VARS_TO_STR3(type, ne, nr);
  524. }
  525. size_t op_size(ggml_tensor * t) override {
  526. return ggml_nbytes(t) * 2;
  527. }
  528. test_repeat(ggml_type type = GGML_TYPE_F32,
  529. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  530. std::array<int, 4> nr = {2, 2, 2, 2})
  531. : type(type), ne(ne), nr(nr) {}
  532. ggml_tensor * build_graph(ggml_context * ctx) override {
  533. 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]);
  534. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  535. ggml_tensor * out = ggml_repeat(ctx, src, target);
  536. return out;
  537. }
  538. };
  539. // GGML_OP_DUP
  540. struct test_dup : public test_case {
  541. const ggml_type type;
  542. const std::array<int64_t, 4> ne;
  543. std::string vars() override {
  544. return VARS_TO_STR2(type, ne);
  545. }
  546. test_dup(ggml_type type = GGML_TYPE_F32,
  547. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  548. : type(type), ne(ne) {}
  549. ggml_tensor * build_graph(ggml_context * ctx) override {
  550. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  551. ggml_tensor * out = ggml_dup(ctx, src);
  552. return out;
  553. }
  554. };
  555. // GGML_OP_CPY
  556. struct test_cpy : public test_case {
  557. const ggml_type type_src;
  558. const ggml_type type_dst;
  559. const std::array<int64_t, 4> ne;
  560. std::string vars() override {
  561. return VARS_TO_STR3(type_src, type_dst, ne);
  562. }
  563. size_t op_size(ggml_tensor * t) override {
  564. return ggml_nbytes(t) + ggml_nbytes(t->src[0]);
  565. }
  566. test_cpy(ggml_type type_src = GGML_TYPE_F32, ggml_type type_dst = GGML_TYPE_F32,
  567. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  568. : type_src(type_src), type_dst(type_dst), ne(ne) {}
  569. ggml_tensor * build_graph(ggml_context * ctx) override {
  570. ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne.data());
  571. ggml_tensor * dst = ggml_new_tensor(ctx, type_dst, 4, ne.data());
  572. ggml_tensor * out = ggml_cpy(ctx, src, dst);
  573. return out;
  574. }
  575. };
  576. // GGML_OP_CONT
  577. struct test_cont : public test_case {
  578. const ggml_type type;
  579. const std::array<int64_t, 4> ne;
  580. std::string vars() override {
  581. return VARS_TO_STR2(type, ne);
  582. }
  583. test_cont(ggml_type type = GGML_TYPE_F32,
  584. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  585. : type(type), ne(ne) {}
  586. ggml_tensor * build_graph(ggml_context * ctx) override {
  587. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  588. src = ggml_transpose(ctx, src);
  589. ggml_tensor * out = ggml_cont(ctx, src);
  590. return out;
  591. }
  592. };
  593. // GGML_OP_ADD
  594. // GGML_OP_MUL
  595. // GGML_OP_DIV
  596. struct test_bin_bcast : public test_case {
  597. using op_t = ggml_tensor * (*) (ggml_context *, ggml_tensor *, ggml_tensor *);
  598. op_t op;
  599. const ggml_type type;
  600. const std::array<int64_t, 4> ne;
  601. const std::array<int, 4> nr;
  602. std::string vars() override {
  603. return VARS_TO_STR3(type, ne, nr);
  604. }
  605. size_t op_size(ggml_tensor * t) override {
  606. return ggml_nbytes(t) * 3;
  607. }
  608. test_bin_bcast(op_t op, ggml_type type = GGML_TYPE_F32,
  609. std::array<int64_t, 4> ne = {10, 10, 1, 1},
  610. std::array<int, 4> nr = {1, 2, 1, 1})
  611. : op(op), type(type), ne(ne), nr(nr) {}
  612. ggml_tensor * build_graph(ggml_context * ctx) override {
  613. 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]);
  614. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data());
  615. ggml_tensor * out = op(ctx, a, b);
  616. return out;
  617. }
  618. void initialize_tensors(ggml_context * ctx) override {
  619. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  620. if (op == ggml_div) {
  621. // avoid division by zero
  622. init_tensor_uniform(t, 1.0f, 2.0f);
  623. } else {
  624. init_tensor_uniform(t);
  625. }
  626. }
  627. }
  628. };
  629. // GGML_OP_SCALE
  630. struct test_scale : public test_case {
  631. const ggml_type type;
  632. const std::array<int64_t, 4> ne;
  633. float scale;
  634. std::string vars() override {
  635. return VARS_TO_STR3(type, ne, scale);
  636. }
  637. test_scale(ggml_type type = GGML_TYPE_F32,
  638. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  639. float scale = 2.0f)
  640. : type(type), ne(ne), scale(scale) {}
  641. ggml_tensor * build_graph(ggml_context * ctx) override {
  642. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  643. ggml_tensor * out = ggml_scale(ctx, a, scale);
  644. return out;
  645. }
  646. };
  647. // GGML_OP_NORM
  648. struct test_norm : public test_case {
  649. const ggml_type type;
  650. const std::array<int64_t, 4> ne;
  651. float eps;
  652. std::string vars() override {
  653. return VARS_TO_STR3(type, ne, eps);
  654. }
  655. test_norm(ggml_type type = GGML_TYPE_F32,
  656. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  657. float eps = 1e-6f)
  658. : type(type), ne(ne), eps(eps) {}
  659. ggml_tensor * build_graph(ggml_context * ctx) override {
  660. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  661. ggml_tensor * out = ggml_norm(ctx, a, eps);
  662. return out;
  663. }
  664. };
  665. // GGML_OP_RMS_NORM
  666. struct test_rms_norm : public test_case {
  667. const ggml_type type;
  668. const std::array<int64_t, 4> ne;
  669. float eps;
  670. std::string vars() override {
  671. return VARS_TO_STR3(type, ne, eps);
  672. }
  673. test_rms_norm(ggml_type type = GGML_TYPE_F32,
  674. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  675. float eps = 1e-6f)
  676. : type(type), ne(ne), eps(eps) {}
  677. ggml_tensor * build_graph(ggml_context * ctx) override {
  678. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  679. ggml_tensor * out = ggml_rms_norm(ctx, a, eps);
  680. return out;
  681. }
  682. };
  683. // GGML_OP_MUL_MAT
  684. struct test_mul_mat : public test_case {
  685. const ggml_type type_a;
  686. const ggml_type type_b;
  687. const int64_t m;
  688. const int64_t n;
  689. const int64_t k;
  690. const std::array<int64_t, 2> bs; // dims 3 and 4
  691. const std::array<int64_t, 2> nr; // repeat in dims 3 and 4
  692. std::string vars() override {
  693. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, nr);
  694. }
  695. double max_nmse_err() override {
  696. return 5e-4;
  697. }
  698. size_t op_size(ggml_tensor * t) override {
  699. size_t a = ggml_nbytes(t->src[0]) * n * nr[0] * nr[1];
  700. size_t b = ggml_nbytes(t->src[1]) * m;
  701. size_t c = ggml_nbytes(t);
  702. return a + b + c;
  703. GGML_UNUSED(t);
  704. }
  705. test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  706. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  707. std::array<int64_t, 2> bs = {10, 10},
  708. std::array<int64_t, 2> nr = {2, 2})
  709. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr) {}
  710. ggml_tensor * build_graph(ggml_context * ctx) override {
  711. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  712. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, k, m, bs[0] , bs[1]);
  713. ggml_tensor * b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]);
  714. ggml_tensor * out = ggml_mul_mat(ctx, a, b);
  715. return out;
  716. }
  717. };
  718. // GGML_OP_MUL_MAT_ID
  719. struct test_mul_mat_id : public test_case {
  720. const ggml_type type_a;
  721. const ggml_type type_b;
  722. const int n_mats;
  723. const int id;
  724. const int64_t m;
  725. const int64_t n;
  726. const int64_t k;
  727. const bool v; // view (non-contiguous ids)
  728. std::string vars() override {
  729. return VARS_TO_STR8(type_a, type_b, n_mats, id, m, n, k, v);
  730. }
  731. double max_nmse_err() override {
  732. return 5e-4;
  733. }
  734. size_t op_size(ggml_tensor * t) override {
  735. size_t a = ggml_nbytes(t->src[2]) * n;
  736. size_t b = ggml_nbytes(t->src[1]) * m;
  737. size_t c = ggml_nbytes(t);
  738. return a + b + c;
  739. GGML_UNUSED(t);
  740. }
  741. test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  742. int n_mats = 2, int id = 0,
  743. int64_t m = 32, int64_t n = 32, int64_t k = 32, bool v = false)
  744. : type_a(type_a), type_b(type_b), n_mats(n_mats), id(id),
  745. m(m), n(n), k(k), v(v) {}
  746. ggml_tensor * build_graph(ggml_context * ctx) override {
  747. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  748. std::vector<ggml_tensor *> mats;
  749. for (int i = 0; i < n_mats; i++) {
  750. ggml_tensor * a = ggml_new_tensor_2d(ctx, type_a, k, m);
  751. mats.push_back(a);
  752. }
  753. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n);
  754. if (v) {
  755. ids = ggml_view_2d(ctx, ids, n_mats/2, ids->ne[1], ids->nb[1], 0);
  756. }
  757. ggml_tensor * b = ggml_new_tensor_2d(ctx, type_b, k, n);
  758. ggml_tensor * out = ggml_mul_mat_id(ctx, mats.data(), n_mats, ids, v ? id/2 : id, b);
  759. return out;
  760. }
  761. void initialize_tensors(ggml_context * ctx) override {
  762. std::random_device rd;
  763. std::default_random_engine rng(rd());
  764. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  765. if (t->type == GGML_TYPE_I32) {
  766. if (ggml_is_view_op(t->op)) { continue; }
  767. // ids
  768. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  769. std::vector<int32_t> data(t->ne[0]);
  770. for (int i = 0; i < t->ne[0]; i++) {
  771. data[i] = i % n_mats;
  772. }
  773. std::shuffle(data.begin(), data.end(), rng);
  774. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t));
  775. }
  776. } else {
  777. init_tensor_uniform(t);
  778. }
  779. }
  780. }
  781. };
  782. // GGML_OP_SQR
  783. struct test_sqr : public test_case {
  784. const ggml_type type;
  785. const std::array<int64_t, 4> ne;
  786. std::string vars() override {
  787. return VARS_TO_STR2(type, ne);
  788. }
  789. test_sqr(ggml_type type = GGML_TYPE_F32,
  790. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  791. : type(type), ne(ne) {}
  792. ggml_tensor * build_graph(ggml_context * ctx) override {
  793. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  794. ggml_tensor * out = ggml_sqr(ctx, a);
  795. return out;
  796. }
  797. };
  798. // GGML_OP_CLAMP
  799. struct test_clamp : public test_case {
  800. const ggml_type type;
  801. const std::array<int64_t, 4> ne;
  802. float min;
  803. float max;
  804. std::string vars() override {
  805. return VARS_TO_STR4(type, ne, min, max);
  806. }
  807. test_clamp(ggml_type type = GGML_TYPE_F32,
  808. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  809. float min = -0.5f, float max = 0.5f)
  810. : type(type), ne(ne), min(min), max(max) {}
  811. ggml_tensor * build_graph(ggml_context * ctx) override {
  812. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  813. ggml_tensor * out = ggml_clamp(ctx, a, min, max);
  814. return out;
  815. }
  816. };
  817. // GGML_OP_DIAG_MASK_INF
  818. struct test_diag_mask_inf : public test_case {
  819. const ggml_type type;
  820. const std::array<int64_t, 4> ne;
  821. const int n_past;
  822. std::string vars() override {
  823. return VARS_TO_STR3(type, ne, n_past);
  824. }
  825. test_diag_mask_inf(ggml_type type = GGML_TYPE_F32,
  826. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  827. int n_past = 5)
  828. : type(type), ne(ne), n_past(n_past) {}
  829. ggml_tensor * build_graph(ggml_context * ctx) override {
  830. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  831. ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past);
  832. return out;
  833. }
  834. };
  835. // GGML_OP_SOFT_MAX
  836. struct test_soft_max : public test_case {
  837. const ggml_type type;
  838. const std::array<int64_t, 4> ne;
  839. std::string vars() override {
  840. return VARS_TO_STR2(type, ne);
  841. }
  842. test_soft_max(ggml_type type = GGML_TYPE_F32,
  843. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  844. : type(type), ne(ne) {}
  845. ggml_tensor * build_graph(ggml_context * ctx) override {
  846. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  847. ggml_tensor * out = ggml_soft_max(ctx, a);
  848. return out;
  849. }
  850. };
  851. // GGML_OP_ROPE
  852. struct test_rope : public test_case {
  853. const ggml_type type;
  854. const std::array<int64_t, 4> ne;
  855. int n_dims;
  856. int mode;
  857. int n_ctx;
  858. std::string vars() override {
  859. return VARS_TO_STR5(type, ne, n_dims, mode, n_ctx);
  860. }
  861. test_rope(ggml_type type = GGML_TYPE_F32,
  862. std::array<int64_t, 4> ne = {10, 10, 10, 1},
  863. int n_dims = 10, int mode = 0, int n_ctx = 512)
  864. : type(type), ne(ne), n_dims(n_dims), mode(mode), n_ctx(n_ctx) {}
  865. ggml_tensor * build_graph(ggml_context * ctx) override {
  866. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  867. ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne[2]);
  868. ggml_tensor * out = ggml_rope(ctx, a, pos, n_dims, mode, n_ctx);
  869. return out;
  870. }
  871. void initialize_tensors(ggml_context * ctx) override {
  872. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  873. if (t->type == GGML_TYPE_I32) {
  874. // pos
  875. std::vector<int> data(ne[2]);
  876. for (int i = 0; i < ne[2]; i++) {
  877. data[i] = rand() % n_ctx;
  878. }
  879. ggml_backend_tensor_set(t, data.data(), 0, ne[2] * sizeof(int));
  880. } else {
  881. init_tensor_uniform(t);
  882. }
  883. }
  884. }
  885. };
  886. // GGML_OP_ALIBI
  887. struct test_alibi : public test_case {
  888. const ggml_type type;
  889. const std::array<int64_t, 4> ne;
  890. int n_past;
  891. int n_head;
  892. float bias_max;
  893. std::string vars() override {
  894. return VARS_TO_STR5(type, ne, n_past, n_head, bias_max);
  895. }
  896. test_alibi(ggml_type type = GGML_TYPE_F32,
  897. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  898. int n_past = 512, int n_head = 10, float bias_max = 0.5f)
  899. : type(type), ne(ne), n_past(n_past), n_head(n_head), bias_max(bias_max) {}
  900. ggml_tensor * build_graph(ggml_context * ctx) override {
  901. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  902. ggml_tensor * out = ggml_alibi(ctx, a, n_past, n_head, bias_max);
  903. return out;
  904. }
  905. };
  906. // GGML_OP_IM2COL
  907. struct test_im2col : public test_case {
  908. const ggml_type type_input;
  909. const ggml_type type_kernel;
  910. const std::array<int64_t, 4> ne_input;
  911. const std::array<int64_t, 4> ne_kernel;
  912. // stride
  913. const int s0;
  914. const int s1;
  915. // padding
  916. const int p0;
  917. const int p1;
  918. // dilatation
  919. const int d0;
  920. const int d1;
  921. // mode
  922. const bool is_2D;
  923. std::string vars() override {
  924. return VARS_TO_STR11(type_input, type_kernel, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  925. }
  926. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16,
  927. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  928. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  929. int s0 = 1, int s1 = 1,
  930. int p0 = 1, int p1 = 1,
  931. int d0 = 1, int d1 = 1,
  932. bool is_2D = true)
  933. : type_input(type_input), type_kernel(type_kernel), ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), s1(s1), p0(p0), p1(p1), d0(d0), d1(d1), is_2D(is_2D) {}
  934. ggml_tensor * build_graph(ggml_context * ctx) override {
  935. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  936. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  937. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D);
  938. return out;
  939. }
  940. };
  941. // GGML_OP_CONCAT
  942. struct test_concat : public test_case {
  943. const ggml_type type;
  944. const std::array<int64_t, 4> ne;
  945. const int64_t b_ne2;
  946. std::string vars() override {
  947. return VARS_TO_STR3(type, ne, b_ne2);
  948. }
  949. test_concat(ggml_type type = GGML_TYPE_F32,
  950. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  951. int64_t b_ne2 = 10)
  952. : type(type), ne(ne), b_ne2(b_ne2) {}
  953. ggml_tensor * build_graph(ggml_context * ctx) override {
  954. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  955. ggml_tensor * b = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], b_ne2, ne[3]);
  956. ggml_tensor * out = ggml_concat(ctx, a, b);
  957. return out;
  958. }
  959. };
  960. // GGML_OP_ARGSORT
  961. struct test_argsort : public test_case {
  962. const ggml_type type;
  963. const std::array<int64_t, 4> ne;
  964. ggml_sort_order order;
  965. std::string vars() override {
  966. return VARS_TO_STR3(type, ne, order);
  967. }
  968. test_argsort(ggml_type type = GGML_TYPE_F32,
  969. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  970. ggml_sort_order order = GGML_SORT_ASC)
  971. : type(type), ne(ne), order(order) {}
  972. ggml_tensor * build_graph(ggml_context * ctx) override {
  973. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  974. ggml_tensor * out = ggml_argsort(ctx, a, order);
  975. return out;
  976. }
  977. void initialize_tensors(ggml_context * ctx) override {
  978. std::random_device rd;
  979. std::default_random_engine rng(rd());
  980. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  981. if (t->type == GGML_TYPE_I32) {
  982. // indices
  983. std::vector<int> data(ggml_nelements(t));
  984. for (int i = 0; i < ggml_nelements(t); i++) {
  985. data[i] = rand();
  986. }
  987. std::shuffle(data.begin(), data.end(), rng);
  988. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  989. } else if (t->type == GGML_TYPE_F32) {
  990. // initialize with unique values to avoid ties
  991. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  992. std::vector<float> data(t->ne[0]);
  993. for (int i = 0; i < t->ne[0]; i++) {
  994. data[i] = i;
  995. }
  996. std::shuffle(data.begin(), data.end(), rng);
  997. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  998. }
  999. } else {
  1000. GGML_ASSERT(false);
  1001. }
  1002. }
  1003. }
  1004. };
  1005. // GGML_OP_SUM_ROWS
  1006. struct test_sum_rows : public test_case {
  1007. const ggml_type type;
  1008. const std::array<int64_t, 4> ne;
  1009. std::string vars() override {
  1010. return VARS_TO_STR2(type, ne);
  1011. }
  1012. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1013. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  1014. : type(type), ne(ne) {}
  1015. ggml_tensor * build_graph(ggml_context * ctx) override {
  1016. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1017. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1018. return out;
  1019. }
  1020. };
  1021. // GGML_OP_UPSCALE
  1022. struct test_upscale : public test_case {
  1023. const ggml_type type;
  1024. const std::array<int64_t, 4> ne;
  1025. const int32_t scale_factor;
  1026. std::string vars() override {
  1027. return VARS_TO_STR3(type, ne, scale_factor);
  1028. }
  1029. test_upscale(ggml_type type = GGML_TYPE_F32,
  1030. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1031. int32_t scale_factor = 2)
  1032. : type(type), ne(ne), scale_factor(scale_factor) {}
  1033. ggml_tensor * build_graph(ggml_context * ctx) override {
  1034. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1035. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1036. return out;
  1037. }
  1038. };
  1039. // GGML_OP_GROUP_NORM
  1040. struct test_group_norm : public test_case {
  1041. const ggml_type type;
  1042. const std::array<int64_t, 4> ne;
  1043. const int32_t num_groups;
  1044. std::string vars() override {
  1045. return VARS_TO_STR3(type, ne, num_groups);
  1046. }
  1047. test_group_norm(ggml_type type = GGML_TYPE_F32,
  1048. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  1049. int32_t num_groups = 32)
  1050. : type(type), ne(ne), num_groups(num_groups) {}
  1051. ggml_tensor * build_graph(ggml_context * ctx) override {
  1052. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1053. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups);
  1054. return out;
  1055. }
  1056. };
  1057. // GGML_OP_ACC
  1058. struct test_acc : public test_case {
  1059. const ggml_type type;
  1060. const std::array<int64_t, 4> ne_a;
  1061. const std::array<int64_t, 4> ne_b;
  1062. std::string vars() override {
  1063. return VARS_TO_STR3(type, ne_a, ne_b);
  1064. }
  1065. test_acc(ggml_type type = GGML_TYPE_F32,
  1066. std::array<int64_t, 4> ne_a = {1024, 577, 1, 1},
  1067. std::array<int64_t, 4> ne_b = {1024, 576, 1, 1})
  1068. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1069. ggml_tensor * build_graph(ggml_context * ctx) override {
  1070. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1071. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1072. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  1073. return out;
  1074. }
  1075. };
  1076. // GGML_OP_PAD
  1077. struct test_pad : public test_case {
  1078. const ggml_type type;
  1079. const std::array<int64_t, 4> ne_a;
  1080. const int pad_0;
  1081. const int pad_1;
  1082. std::string vars() override {
  1083. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  1084. }
  1085. test_pad(ggml_type type = GGML_TYPE_F32,
  1086. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  1087. int pad_0 = 1, int pad_1 = 1)
  1088. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  1089. ggml_tensor * build_graph(ggml_context * ctx) override {
  1090. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1091. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  1092. return out;
  1093. }
  1094. };
  1095. // GGML_OP_LEAKY_RELU
  1096. struct test_leaky_relu : public test_case {
  1097. const ggml_type type;
  1098. const std::array<int64_t, 4> ne_a;
  1099. const float negative_slope;
  1100. std::string vars() override {
  1101. return VARS_TO_STR3(type, ne_a, negative_slope);
  1102. }
  1103. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  1104. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1105. float negative_slope = 0.1f)
  1106. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  1107. ggml_tensor * build_graph(ggml_context * ctx) override {
  1108. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1109. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  1110. return out;
  1111. }
  1112. };
  1113. // Mixtral MOE
  1114. struct test_moe : public test_case {
  1115. const int n_experts;
  1116. const int n_experts_per_tok;
  1117. const int n_tokens;
  1118. const int n_embd;
  1119. const int n_ff;
  1120. std::string op_desc(ggml_tensor * t) override {
  1121. return "MOE";
  1122. GGML_UNUSED(t);
  1123. }
  1124. std::string vars() override {
  1125. return VARS_TO_STR5(n_experts, n_experts_per_tok, n_tokens, n_embd, n_ff);
  1126. }
  1127. test_moe(int n_experts = 8, int n_experts_per_tok = 2, int n_tokens = 1, int n_embd = 4096, int n_ff = 14336)
  1128. : n_experts(n_experts), n_experts_per_tok(n_experts_per_tok), n_tokens(n_tokens), n_embd(n_embd), n_ff(n_ff) {
  1129. }
  1130. ggml_tensor * build_graph(ggml_context * ctx) override {
  1131. ggml_tensor * ffn_gate_inp = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_experts);
  1132. std::vector<ggml_tensor *> ffn_up_exp(n_experts);
  1133. std::vector<ggml_tensor *> ffn_gate_exp(n_experts);
  1134. std::vector<ggml_tensor *> ffn_down_exp(n_experts);
  1135. for (int i = 0; i < n_experts; ++i) {
  1136. ffn_up_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff);
  1137. ffn_gate_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff);
  1138. ffn_down_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_ff, n_embd);
  1139. }
  1140. ggml_tensor * cur = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_tokens);
  1141. ggml_tensor * logits = ggml_mul_mat(ctx, ffn_gate_inp, cur);
  1142. ggml_tensor * probs = ggml_soft_max_ext(ctx, logits, nullptr, 1.0f/sqrtf(n_embd));
  1143. // select experts
  1144. ggml_tensor * selected_experts = ggml_top_k(ctx, probs, n_experts_per_tok);
  1145. ggml_tensor * weights = ggml_get_rows(ctx,
  1146. ggml_reshape_3d(ctx, probs, 1, n_experts, n_tokens), selected_experts);
  1147. weights = ggml_reshape_2d(ctx, weights, n_experts_per_tok, n_tokens);
  1148. ggml_tensor * weights_sum = ggml_sum_rows(ctx, weights);
  1149. weights = ggml_div(ctx, weights, weights_sum);
  1150. // compute expert outputs
  1151. ggml_tensor * moe_out = nullptr;
  1152. for (int i = 0; i < n_experts_per_tok; ++i) {
  1153. ggml_tensor * cur_expert;
  1154. ggml_tensor * cur_up = ggml_mul_mat_id(ctx, ffn_up_exp.data(), n_experts, selected_experts, i, cur);
  1155. ggml_tensor * cur_gate = ggml_mul_mat_id(ctx, ffn_gate_exp.data(), n_experts, selected_experts, i, cur);
  1156. cur_gate = ggml_silu(ctx, cur_gate);
  1157. cur_expert = ggml_mul(ctx, cur_up, cur_gate);
  1158. cur_expert = ggml_mul_mat_id(ctx, ffn_down_exp.data(), n_experts, selected_experts, i, cur_expert);
  1159. cur_expert = ggml_mul(ctx, cur_expert,
  1160. ggml_view_2d(ctx, weights, 1, n_tokens, weights->nb[1], i*weights->nb[0]));
  1161. if (i == 0) {
  1162. moe_out = cur_expert;
  1163. } else {
  1164. moe_out = ggml_add(ctx, moe_out, cur_expert);
  1165. }
  1166. }
  1167. cur = moe_out;
  1168. return cur;
  1169. }
  1170. };
  1171. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  1172. std::vector<std::unique_ptr<test_case>> test_cases;
  1173. const ggml_type all_types[] = {
  1174. GGML_TYPE_F32, GGML_TYPE_F16,
  1175. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  1176. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1177. GGML_TYPE_Q8_0,
  1178. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1179. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  1180. GGML_TYPE_Q6_K
  1181. };
  1182. // unary ops
  1183. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  1184. test_cases.emplace_back(new test_unary((ggml_unary_op) op));
  1185. }
  1186. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  1187. for (ggml_type type : all_types) {
  1188. for (int b : {1, 7}) {
  1189. for (bool v : {false, true}) {
  1190. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  1191. }
  1192. }
  1193. }
  1194. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 1}));
  1195. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1196. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 2, 1, 1}));
  1197. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 2, 1}));
  1198. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1199. test_cases.emplace_back(new test_dup());
  1200. for (ggml_type type : all_types) {
  1201. test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, type, {256, 10, 10, 1}));
  1202. }
  1203. test_cases.emplace_back(new test_cont());
  1204. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  1205. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  1206. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  1207. }
  1208. };
  1209. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  1210. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  1211. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  1212. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 1, 1}, {1, 1, 1, 1});
  1213. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 1}, {1, 1, 1, 1});
  1214. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 1});
  1215. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 1, 1, 1});
  1216. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 1, 1});
  1217. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 1});
  1218. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 2});
  1219. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 2});
  1220. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 2, 2});
  1221. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 2, 2, 2});
  1222. // stable diffusion
  1223. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  1224. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  1225. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  1226. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  1227. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  1228. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  1229. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  1230. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  1231. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  1232. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  1233. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  1234. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  1235. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  1236. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  1237. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  1238. test_cases.emplace_back(new test_scale());
  1239. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  1240. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1241. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1242. }
  1243. for (ggml_type type_a : all_types) {
  1244. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1245. // FIXME: CPU crashes on f16xf16
  1246. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1247. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  1248. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  1249. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  1250. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  1251. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  1252. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  1253. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  1254. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  1255. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  1256. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  1257. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  1258. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  1259. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  1260. }
  1261. }
  1262. for (ggml_type type_a : all_types) {
  1263. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1264. for (int n_mats : {2, 4, 8}) {
  1265. for (int id = 0; id < n_mats; id++) {
  1266. for (bool v : {false, true}) {
  1267. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, id, 16, 16, 256, v));
  1268. }
  1269. }
  1270. }
  1271. }
  1272. }
  1273. test_cases.emplace_back(new test_sqr());
  1274. test_cases.emplace_back(new test_clamp());
  1275. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  1276. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 1}, 5));
  1277. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 10}, 5));
  1278. test_cases.emplace_back(new test_soft_max());
  1279. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1280. test_cases.emplace_back(new test_rope(type, {128, 32, 10, 1}, 128, 0, 512)); // llama 7B
  1281. test_cases.emplace_back(new test_rope(type, {128, 40, 10, 1}, 128, 0, 512)); // llama 13B
  1282. test_cases.emplace_back(new test_rope(type, {128, 52, 10, 1}, 128, 0, 512)); // llama 30B
  1283. test_cases.emplace_back(new test_rope(type, {128, 64, 10, 1}, 128, 0, 512)); // llama 65B
  1284. test_cases.emplace_back(new test_rope(type, { 64, 1, 10, 1}, 64, 2, 512)); // neox (falcon 7B)
  1285. test_cases.emplace_back(new test_rope(type, { 64, 71, 10, 1}, 64, 2, 512)); // neox (falcon 7B)
  1286. test_cases.emplace_back(new test_rope(type, { 64, 8, 10, 1}, 64, 2, 512)); // neox (falcon 40B)
  1287. test_cases.emplace_back(new test_rope(type, { 64, 128, 10, 1}, 64, 2, 512)); // neox (falcon 40B)
  1288. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 20, 2, 512)); // neox (stablelm)
  1289. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 32, 2, 512)); // neox (phi-2)
  1290. }
  1291. test_cases.emplace_back(new test_alibi());
  1292. test_cases.emplace_back(new test_im2col());
  1293. test_cases.emplace_back(new test_concat());
  1294. for (ggml_sort_order order : {GGML_SORT_ASC, GGML_SORT_DESC}) {
  1295. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  1296. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  1297. }
  1298. test_cases.emplace_back(new test_sum_rows());
  1299. test_cases.emplace_back(new test_upscale());
  1300. test_cases.emplace_back(new test_group_norm());
  1301. test_cases.emplace_back(new test_acc());
  1302. test_cases.emplace_back(new test_pad());
  1303. test_cases.emplace_back(new test_leaky_relu());
  1304. #if !defined(__SANITIZE_THREAD__)
  1305. // FIXME: these tests use too much memory with thread sanitizer
  1306. test_cases.emplace_back(new test_moe(8, 2, 1, 4096, 8*1024));
  1307. //test_cases.emplace_back(new test_moe(8, 2, 8, 4096, 14336));
  1308. #endif
  1309. // run tests
  1310. if (mode == MODE_TEST) {
  1311. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  1312. size_t n_ok = 0;
  1313. for (auto & test : test_cases) {
  1314. if (test->eval(backend, backend_cpu, op_name)) {
  1315. n_ok++;
  1316. }
  1317. }
  1318. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  1319. ggml_backend_free(backend_cpu);
  1320. return n_ok == test_cases.size();
  1321. }
  1322. if (mode == MODE_PERF) {
  1323. for (auto & test : test_cases) {
  1324. test->eval_perf(backend, op_name);
  1325. }
  1326. return true;
  1327. }
  1328. GGML_ASSERT(false);
  1329. return false;
  1330. }
  1331. static void usage(char ** argv) {
  1332. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  1333. printf(" valid modes are: test (compare with CPU backend for correctness) or perf (performance evaluation)\n");
  1334. printf(" op names are as given by ggml_op_desc()\n");
  1335. }
  1336. int main(int argc, char ** argv) {
  1337. test_mode mode = MODE_TEST;
  1338. const char * op_name = NULL;
  1339. const char * backend = NULL;
  1340. for (int i = 1; i < argc; i++) {
  1341. if (strcmp(argv[i], "test") == 0) {
  1342. mode = MODE_TEST;
  1343. } else if (strcmp(argv[i], "perf") == 0) {
  1344. mode = MODE_PERF;
  1345. } else if (strcmp(argv[i], "-o") == 0) {
  1346. if (i + 1 < argc) {
  1347. op_name = argv[++i];
  1348. } else {
  1349. usage(argv);
  1350. return 1;
  1351. }
  1352. } else if (strcmp(argv[i], "-b") == 0) {
  1353. if (i + 1 < argc) {
  1354. backend = argv[++i];
  1355. } else {
  1356. usage(argv);
  1357. return 1;
  1358. }
  1359. } else {
  1360. usage(argv);
  1361. return 1;
  1362. }
  1363. }
  1364. // enumerate backends
  1365. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  1366. size_t n_ok = 0;
  1367. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  1368. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  1369. if (backend != NULL && strcmp(backend, ggml_backend_reg_get_name(i)) != 0) {
  1370. printf(" Skipping\n");
  1371. n_ok++;
  1372. continue;
  1373. }
  1374. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  1375. GGML_ASSERT(backend != NULL);
  1376. printf(" Backend name: %s\n", ggml_backend_name(backend));
  1377. bool ok = test_backend(backend, mode, op_name);
  1378. printf(" Backend %s: ", ggml_backend_name(backend));
  1379. if (ok) {
  1380. printf("\033[1;32mOK\033[0m\n");
  1381. n_ok++;
  1382. } else {
  1383. printf("\033[1;31mFAIL\033[0m\n");
  1384. }
  1385. printf("\n");
  1386. ggml_backend_free(backend);
  1387. }
  1388. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  1389. if (n_ok != ggml_backend_reg_get_count()) {
  1390. printf("\033[1;31mFAIL\033[0m\n");
  1391. return 1;
  1392. }
  1393. printf("\033[1;32mOK\033[0m\n");
  1394. return 0;
  1395. }