test-backend-ops.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  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. std::string vars() override {
  634. return VARS_TO_STR2(type, ne);
  635. }
  636. test_scale(ggml_type type = GGML_TYPE_F32,
  637. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  638. : type(type), ne(ne) {}
  639. ggml_tensor * build_graph(ggml_context * ctx) override {
  640. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  641. ggml_tensor * scale = ggml_new_tensor_1d(ctx, type, 1);
  642. ggml_tensor * out = ggml_scale(ctx, a, scale);
  643. return out;
  644. }
  645. };
  646. // GGML_OP_NORM
  647. struct test_norm : public test_case {
  648. const ggml_type type;
  649. const std::array<int64_t, 4> ne;
  650. float eps;
  651. std::string vars() override {
  652. return VARS_TO_STR3(type, ne, eps);
  653. }
  654. test_norm(ggml_type type = GGML_TYPE_F32,
  655. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  656. float eps = 1e-6f)
  657. : type(type), ne(ne), eps(eps) {}
  658. ggml_tensor * build_graph(ggml_context * ctx) override {
  659. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  660. ggml_tensor * out = ggml_norm(ctx, a, eps);
  661. return out;
  662. }
  663. };
  664. // GGML_OP_RMS_NORM
  665. struct test_rms_norm : public test_case {
  666. const ggml_type type;
  667. const std::array<int64_t, 4> ne;
  668. float eps;
  669. std::string vars() override {
  670. return VARS_TO_STR3(type, ne, eps);
  671. }
  672. test_rms_norm(ggml_type type = GGML_TYPE_F32,
  673. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  674. float eps = 1e-6f)
  675. : type(type), ne(ne), eps(eps) {}
  676. ggml_tensor * build_graph(ggml_context * ctx) override {
  677. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  678. ggml_tensor * out = ggml_rms_norm(ctx, a, eps);
  679. return out;
  680. }
  681. };
  682. // GGML_OP_MUL_MAT
  683. struct test_mul_mat : public test_case {
  684. const ggml_type type_a;
  685. const ggml_type type_b;
  686. const int64_t m;
  687. const int64_t n;
  688. const int64_t k;
  689. const std::array<int64_t, 2> bs; // dims 3 and 4
  690. const std::array<int64_t, 2> nr; // repeat in dims 3 and 4
  691. std::string vars() override {
  692. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, nr);
  693. }
  694. double max_nmse_err() override {
  695. return 5e-4;
  696. }
  697. size_t op_size(ggml_tensor * t) override {
  698. size_t a = ggml_nbytes(t->src[0]) * n * nr[0] * nr[1];
  699. size_t b = ggml_nbytes(t->src[1]) * m;
  700. size_t c = ggml_nbytes(t);
  701. return a + b + c;
  702. GGML_UNUSED(t);
  703. }
  704. test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  705. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  706. std::array<int64_t, 2> bs = {10, 10},
  707. std::array<int64_t, 2> nr = {2, 2})
  708. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr) {}
  709. ggml_tensor * build_graph(ggml_context * ctx) override {
  710. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  711. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, k, m, bs[0] , bs[1]);
  712. ggml_tensor * b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]);
  713. ggml_tensor * out = ggml_mul_mat(ctx, a, b);
  714. return out;
  715. }
  716. };
  717. // GGML_OP_MUL_MAT_ID
  718. struct test_mul_mat_id : public test_case {
  719. const ggml_type type_a;
  720. const ggml_type type_b;
  721. const int n_mats;
  722. const int id;
  723. const int64_t m;
  724. const int64_t n;
  725. const int64_t k;
  726. const bool v; // view (non-contiguous ids)
  727. std::string vars() override {
  728. return VARS_TO_STR8(type_a, type_b, n_mats, id, m, n, k, v);
  729. }
  730. double max_nmse_err() override {
  731. return 5e-4;
  732. }
  733. size_t op_size(ggml_tensor * t) override {
  734. size_t a = ggml_nbytes(t->src[2]) * n;
  735. size_t b = ggml_nbytes(t->src[1]) * m;
  736. size_t c = ggml_nbytes(t);
  737. return a + b + c;
  738. GGML_UNUSED(t);
  739. }
  740. test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  741. int n_mats = 2, int id = 0,
  742. int64_t m = 32, int64_t n = 32, int64_t k = 32, bool v = false)
  743. : type_a(type_a), type_b(type_b), n_mats(n_mats), id(id),
  744. m(m), n(n), k(k), v(v) {}
  745. ggml_tensor * build_graph(ggml_context * ctx) override {
  746. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  747. std::vector<ggml_tensor *> mats;
  748. for (int i = 0; i < n_mats; i++) {
  749. ggml_tensor * a = ggml_new_tensor_2d(ctx, type_a, k, m);
  750. mats.push_back(a);
  751. }
  752. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n);
  753. if (v) {
  754. ids = ggml_view_2d(ctx, ids, n_mats/2, ids->ne[1], ids->nb[1], 0);
  755. }
  756. ggml_tensor * b = ggml_new_tensor_2d(ctx, type_b, k, n);
  757. ggml_tensor * out = ggml_mul_mat_id(ctx, mats.data(), n_mats, ids, v ? id/2 : id, b);
  758. return out;
  759. }
  760. void initialize_tensors(ggml_context * ctx) override {
  761. std::random_device rd;
  762. std::default_random_engine rng(rd());
  763. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  764. if (t->type == GGML_TYPE_I32) {
  765. if (ggml_is_view_op(t->op)) { continue; }
  766. // ids
  767. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  768. std::vector<int32_t> data(t->ne[0]);
  769. for (int i = 0; i < t->ne[0]; i++) {
  770. data[i] = i % n_mats;
  771. }
  772. std::shuffle(data.begin(), data.end(), rng);
  773. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t));
  774. }
  775. } else {
  776. init_tensor_uniform(t);
  777. }
  778. }
  779. }
  780. };
  781. // GGML_OP_SQR
  782. struct test_sqr : public test_case {
  783. const ggml_type type;
  784. const std::array<int64_t, 4> ne;
  785. std::string vars() override {
  786. return VARS_TO_STR2(type, ne);
  787. }
  788. test_sqr(ggml_type type = GGML_TYPE_F32,
  789. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  790. : type(type), ne(ne) {}
  791. ggml_tensor * build_graph(ggml_context * ctx) override {
  792. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  793. ggml_tensor * out = ggml_sqr(ctx, a);
  794. return out;
  795. }
  796. };
  797. // GGML_OP_CLAMP
  798. struct test_clamp : public test_case {
  799. const ggml_type type;
  800. const std::array<int64_t, 4> ne;
  801. float min;
  802. float max;
  803. std::string vars() override {
  804. return VARS_TO_STR4(type, ne, min, max);
  805. }
  806. test_clamp(ggml_type type = GGML_TYPE_F32,
  807. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  808. float min = -0.5f, float max = 0.5f)
  809. : type(type), ne(ne), min(min), max(max) {}
  810. ggml_tensor * build_graph(ggml_context * ctx) override {
  811. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  812. ggml_tensor * out = ggml_clamp(ctx, a, min, max);
  813. return out;
  814. }
  815. };
  816. // GGML_OP_DIAG_MASK_INF
  817. struct test_diag_mask_inf : public test_case {
  818. const ggml_type type;
  819. const std::array<int64_t, 4> ne;
  820. const int n_past;
  821. std::string vars() override {
  822. return VARS_TO_STR3(type, ne, n_past);
  823. }
  824. test_diag_mask_inf(ggml_type type = GGML_TYPE_F32,
  825. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  826. int n_past = 5)
  827. : type(type), ne(ne), n_past(n_past) {}
  828. ggml_tensor * build_graph(ggml_context * ctx) override {
  829. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  830. ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past);
  831. return out;
  832. }
  833. };
  834. // GGML_OP_SOFT_MAX
  835. struct test_soft_max : public test_case {
  836. const ggml_type type;
  837. const std::array<int64_t, 4> ne;
  838. std::string vars() override {
  839. return VARS_TO_STR2(type, ne);
  840. }
  841. test_soft_max(ggml_type type = GGML_TYPE_F32,
  842. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  843. : type(type), ne(ne) {}
  844. ggml_tensor * build_graph(ggml_context * ctx) override {
  845. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  846. ggml_tensor * out = ggml_soft_max(ctx, a);
  847. return out;
  848. }
  849. };
  850. // GGML_OP_ROPE
  851. struct test_rope : public test_case {
  852. const ggml_type type;
  853. const std::array<int64_t, 4> ne;
  854. int n_dims;
  855. int mode;
  856. int n_ctx;
  857. std::string vars() override {
  858. return VARS_TO_STR5(type, ne, n_dims, mode, n_ctx);
  859. }
  860. test_rope(ggml_type type = GGML_TYPE_F32,
  861. std::array<int64_t, 4> ne = {10, 10, 10, 1},
  862. int n_dims = 10, int mode = 0, int n_ctx = 512)
  863. : type(type), ne(ne), n_dims(n_dims), mode(mode), n_ctx(n_ctx) {}
  864. ggml_tensor * build_graph(ggml_context * ctx) override {
  865. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  866. ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne[2]);
  867. ggml_tensor * out = ggml_rope(ctx, a, pos, n_dims, mode, n_ctx);
  868. return out;
  869. }
  870. void initialize_tensors(ggml_context * ctx) override {
  871. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  872. if (t->type == GGML_TYPE_I32) {
  873. // pos
  874. std::vector<int> data(ne[2]);
  875. for (int i = 0; i < ne[2]; i++) {
  876. data[i] = rand() % n_ctx;
  877. }
  878. ggml_backend_tensor_set(t, data.data(), 0, ne[2] * sizeof(int));
  879. } else {
  880. init_tensor_uniform(t);
  881. }
  882. }
  883. }
  884. };
  885. // GGML_OP_ALIBI
  886. struct test_alibi : public test_case {
  887. const ggml_type type;
  888. const std::array<int64_t, 4> ne;
  889. int n_past;
  890. int n_head;
  891. float bias_max;
  892. std::string vars() override {
  893. return VARS_TO_STR5(type, ne, n_past, n_head, bias_max);
  894. }
  895. test_alibi(ggml_type type = GGML_TYPE_F32,
  896. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  897. int n_past = 512, int n_head = 10, float bias_max = 0.5f)
  898. : type(type), ne(ne), n_past(n_past), n_head(n_head), bias_max(bias_max) {}
  899. ggml_tensor * build_graph(ggml_context * ctx) override {
  900. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  901. ggml_tensor * out = ggml_alibi(ctx, a, n_past, n_head, bias_max);
  902. return out;
  903. }
  904. };
  905. // GGML_OP_IM2COL
  906. struct test_im2col : public test_case {
  907. const ggml_type type_input;
  908. const ggml_type type_kernel;
  909. const std::array<int64_t, 4> ne_input;
  910. const std::array<int64_t, 4> ne_kernel;
  911. // stride
  912. const int s0;
  913. const int s1;
  914. // padding
  915. const int p0;
  916. const int p1;
  917. // dilatation
  918. const int d0;
  919. const int d1;
  920. // mode
  921. const bool is_2D;
  922. std::string vars() override {
  923. return VARS_TO_STR11(type_input, type_kernel, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  924. }
  925. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16,
  926. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  927. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  928. int s0 = 1, int s1 = 1,
  929. int p0 = 1, int p1 = 1,
  930. int d0 = 1, int d1 = 1,
  931. bool is_2D = true)
  932. : 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) {}
  933. ggml_tensor * build_graph(ggml_context * ctx) override {
  934. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  935. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  936. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D);
  937. return out;
  938. }
  939. };
  940. // GGML_OP_CONCAT
  941. struct test_concat : public test_case {
  942. const ggml_type type;
  943. const std::array<int64_t, 4> ne;
  944. const int64_t b_ne2;
  945. std::string vars() override {
  946. return VARS_TO_STR3(type, ne, b_ne2);
  947. }
  948. test_concat(ggml_type type = GGML_TYPE_F32,
  949. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  950. int64_t b_ne2 = 10)
  951. : type(type), ne(ne), b_ne2(b_ne2) {}
  952. ggml_tensor * build_graph(ggml_context * ctx) override {
  953. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  954. ggml_tensor * b = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], b_ne2, ne[3]);
  955. ggml_tensor * out = ggml_concat(ctx, a, b);
  956. return out;
  957. }
  958. };
  959. // GGML_OP_ARGSORT
  960. struct test_argsort : public test_case {
  961. const ggml_type type;
  962. const std::array<int64_t, 4> ne;
  963. ggml_sort_order order;
  964. std::string vars() override {
  965. return VARS_TO_STR3(type, ne, order);
  966. }
  967. test_argsort(ggml_type type = GGML_TYPE_F32,
  968. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  969. ggml_sort_order order = GGML_SORT_ASC)
  970. : type(type), ne(ne), order(order) {}
  971. ggml_tensor * build_graph(ggml_context * ctx) override {
  972. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  973. ggml_tensor * out = ggml_argsort(ctx, a, order);
  974. return out;
  975. }
  976. void initialize_tensors(ggml_context * ctx) override {
  977. std::random_device rd;
  978. std::default_random_engine rng(rd());
  979. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  980. if (t->type == GGML_TYPE_I32) {
  981. // indices
  982. std::vector<int> data(ggml_nelements(t));
  983. for (int i = 0; i < ggml_nelements(t); i++) {
  984. data[i] = rand();
  985. }
  986. std::shuffle(data.begin(), data.end(), rng);
  987. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  988. } else if (t->type == GGML_TYPE_F32) {
  989. // initialize with unique values to avoid ties
  990. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  991. std::vector<float> data(t->ne[0]);
  992. for (int i = 0; i < t->ne[0]; i++) {
  993. data[i] = i;
  994. }
  995. std::shuffle(data.begin(), data.end(), rng);
  996. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  997. }
  998. } else {
  999. GGML_ASSERT(false);
  1000. }
  1001. }
  1002. }
  1003. };
  1004. // GGML_OP_SUM_ROWS
  1005. struct test_sum_rows : public test_case {
  1006. const ggml_type type;
  1007. const std::array<int64_t, 4> ne;
  1008. std::string vars() override {
  1009. return VARS_TO_STR2(type, ne);
  1010. }
  1011. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1012. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  1013. : type(type), ne(ne) {}
  1014. ggml_tensor * build_graph(ggml_context * ctx) override {
  1015. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1016. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1017. return out;
  1018. }
  1019. };
  1020. // GGML_OP_UPSCALE
  1021. struct test_upscale : public test_case {
  1022. const ggml_type type;
  1023. const std::array<int64_t, 4> ne;
  1024. const int32_t scale_factor;
  1025. std::string vars() override {
  1026. return VARS_TO_STR3(type, ne, scale_factor);
  1027. }
  1028. test_upscale(ggml_type type = GGML_TYPE_F32,
  1029. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1030. int32_t scale_factor = 2)
  1031. : type(type), ne(ne), scale_factor(scale_factor) {}
  1032. ggml_tensor * build_graph(ggml_context * ctx) override {
  1033. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1034. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1035. return out;
  1036. }
  1037. };
  1038. // GGML_OP_GROUP_NORM
  1039. struct test_group_norm : public test_case {
  1040. const ggml_type type;
  1041. const std::array<int64_t, 4> ne;
  1042. const int32_t num_groups;
  1043. std::string vars() override {
  1044. return VARS_TO_STR3(type, ne, num_groups);
  1045. }
  1046. test_group_norm(ggml_type type = GGML_TYPE_F32,
  1047. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  1048. int32_t num_groups = 32)
  1049. : type(type), ne(ne), num_groups(num_groups) {}
  1050. ggml_tensor * build_graph(ggml_context * ctx) override {
  1051. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1052. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups);
  1053. return out;
  1054. }
  1055. };
  1056. // GGML_OP_ACC
  1057. struct test_acc : public test_case {
  1058. const ggml_type type;
  1059. const std::array<int64_t, 4> ne_a;
  1060. const std::array<int64_t, 4> ne_b;
  1061. std::string vars() override {
  1062. return VARS_TO_STR3(type, ne_a, ne_b);
  1063. }
  1064. test_acc(ggml_type type = GGML_TYPE_F32,
  1065. std::array<int64_t, 4> ne_a = {1024, 577, 1, 1},
  1066. std::array<int64_t, 4> ne_b = {1024, 576, 1, 1})
  1067. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1068. ggml_tensor * build_graph(ggml_context * ctx) override {
  1069. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1070. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1071. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  1072. return out;
  1073. }
  1074. };
  1075. // GGML_OP_PAD
  1076. struct test_pad : public test_case {
  1077. const ggml_type type;
  1078. const std::array<int64_t, 4> ne_a;
  1079. const int pad_0;
  1080. const int pad_1;
  1081. std::string vars() override {
  1082. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  1083. }
  1084. test_pad(ggml_type type = GGML_TYPE_F32,
  1085. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  1086. int pad_0 = 1, int pad_1 = 1)
  1087. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  1088. ggml_tensor * build_graph(ggml_context * ctx) override {
  1089. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1090. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  1091. return out;
  1092. }
  1093. };
  1094. // GGML_OP_LEAKY_RELU
  1095. struct test_leaky_relu : public test_case {
  1096. const ggml_type type;
  1097. const std::array<int64_t, 4> ne_a;
  1098. const float negative_slope;
  1099. std::string vars() override {
  1100. return VARS_TO_STR3(type, ne_a, negative_slope);
  1101. }
  1102. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  1103. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1104. float negative_slope = 0.1f)
  1105. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  1106. ggml_tensor * build_graph(ggml_context * ctx) override {
  1107. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1108. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  1109. return out;
  1110. }
  1111. };
  1112. // Mixtral MOE
  1113. struct test_moe : public test_case {
  1114. const int n_experts;
  1115. const int n_experts_per_tok;
  1116. const int n_tokens;
  1117. const int n_embd;
  1118. const int n_ff;
  1119. std::string op_desc(ggml_tensor * t) override {
  1120. return "MOE";
  1121. GGML_UNUSED(t);
  1122. }
  1123. std::string vars() override {
  1124. return VARS_TO_STR5(n_experts, n_experts_per_tok, n_tokens, n_embd, n_ff);
  1125. }
  1126. test_moe(int n_experts = 8, int n_experts_per_tok = 2, int n_tokens = 1, int n_embd = 4096, int n_ff = 14336)
  1127. : n_experts(n_experts), n_experts_per_tok(n_experts_per_tok), n_tokens(n_tokens), n_embd(n_embd), n_ff(n_ff) {
  1128. }
  1129. ggml_tensor * build_graph(ggml_context * ctx) override {
  1130. ggml_tensor * ffn_gate_inp = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_experts);
  1131. std::vector<ggml_tensor *> ffn_up_exp(n_experts);
  1132. std::vector<ggml_tensor *> ffn_gate_exp(n_experts);
  1133. std::vector<ggml_tensor *> ffn_down_exp(n_experts);
  1134. for (int i = 0; i < n_experts; ++i) {
  1135. ffn_up_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff);
  1136. ffn_gate_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff);
  1137. ffn_down_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_ff, n_embd);
  1138. }
  1139. ggml_tensor * cur = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_tokens);
  1140. ggml_tensor * logits = ggml_mul_mat(ctx, ffn_gate_inp, cur);
  1141. ggml_tensor * probs = ggml_soft_max_ext(ctx, logits, nullptr, 1.0f/sqrtf(n_embd));
  1142. // select experts
  1143. ggml_tensor * selected_experts = ggml_top_k(ctx, probs, n_experts_per_tok);
  1144. ggml_tensor * weights = ggml_get_rows(ctx,
  1145. ggml_reshape_3d(ctx, probs, 1, n_experts, n_tokens), selected_experts);
  1146. weights = ggml_reshape_2d(ctx, weights, n_experts_per_tok, n_tokens);
  1147. ggml_tensor * weights_sum = ggml_sum_rows(ctx, weights);
  1148. weights = ggml_div(ctx, weights, weights_sum);
  1149. // compute expert outputs
  1150. ggml_tensor * moe_out = nullptr;
  1151. for (int i = 0; i < n_experts_per_tok; ++i) {
  1152. ggml_tensor * cur_expert;
  1153. ggml_tensor * cur_up = ggml_mul_mat_id(ctx, ffn_up_exp.data(), n_experts, selected_experts, i, cur);
  1154. ggml_tensor * cur_gate = ggml_mul_mat_id(ctx, ffn_gate_exp.data(), n_experts, selected_experts, i, cur);
  1155. cur_gate = ggml_silu(ctx, cur_gate);
  1156. cur_expert = ggml_mul(ctx, cur_up, cur_gate);
  1157. cur_expert = ggml_mul_mat_id(ctx, ffn_down_exp.data(), n_experts, selected_experts, i, cur_expert);
  1158. cur_expert = ggml_mul(ctx, cur_expert,
  1159. ggml_view_2d(ctx, weights, 1, n_tokens, weights->nb[1], i*weights->nb[0]));
  1160. if (i == 0) {
  1161. moe_out = cur_expert;
  1162. } else {
  1163. moe_out = ggml_add(ctx, moe_out, cur_expert);
  1164. }
  1165. }
  1166. cur = moe_out;
  1167. return cur;
  1168. }
  1169. };
  1170. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  1171. std::vector<std::unique_ptr<test_case>> test_cases;
  1172. const ggml_type all_types[] = {
  1173. GGML_TYPE_F32, GGML_TYPE_F16,
  1174. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  1175. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1176. GGML_TYPE_Q8_0,
  1177. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1178. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  1179. GGML_TYPE_Q6_K
  1180. };
  1181. // unary ops
  1182. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  1183. test_cases.emplace_back(new test_unary((ggml_unary_op) op));
  1184. }
  1185. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  1186. for (ggml_type type : all_types) {
  1187. for (int b : {1, 7}) {
  1188. for (bool v : {false, true}) {
  1189. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  1190. }
  1191. }
  1192. }
  1193. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 1}));
  1194. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1195. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 2, 1, 1}));
  1196. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 2, 1}));
  1197. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1198. test_cases.emplace_back(new test_dup());
  1199. for (ggml_type type : all_types) {
  1200. test_cases.emplace_back(new test_cpy(GGML_TYPE_F32, type, {256, 10, 10, 1}));
  1201. }
  1202. test_cases.emplace_back(new test_cont());
  1203. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  1204. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  1205. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  1206. }
  1207. };
  1208. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  1209. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  1210. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  1211. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 1, 1}, {1, 1, 1, 1});
  1212. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 1}, {1, 1, 1, 1});
  1213. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 1});
  1214. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 1, 1, 1});
  1215. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 1, 1});
  1216. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 1});
  1217. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 2});
  1218. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 2});
  1219. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 2, 2});
  1220. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 2, 2, 2});
  1221. // stable diffusion
  1222. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  1223. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  1224. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  1225. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  1226. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  1227. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  1228. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  1229. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  1230. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  1231. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  1232. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  1233. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  1234. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  1235. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  1236. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  1237. test_cases.emplace_back(new test_scale());
  1238. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  1239. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1240. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1241. }
  1242. for (ggml_type type_a : all_types) {
  1243. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1244. // FIXME: CPU crashes on f16xf16
  1245. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1246. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  1247. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  1248. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  1249. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  1250. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  1251. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  1252. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  1253. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  1254. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  1255. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  1256. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  1257. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  1258. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  1259. }
  1260. }
  1261. for (ggml_type type_a : all_types) {
  1262. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1263. for (int n_mats : {2, 4, 8}) {
  1264. for (int id = 0; id < n_mats; id++) {
  1265. for (bool v : {false, true}) {
  1266. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, id, 16, 16, 256, v));
  1267. }
  1268. }
  1269. }
  1270. }
  1271. }
  1272. test_cases.emplace_back(new test_sqr());
  1273. test_cases.emplace_back(new test_clamp());
  1274. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  1275. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 1}, 5));
  1276. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 10}, 5));
  1277. test_cases.emplace_back(new test_soft_max());
  1278. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1279. test_cases.emplace_back(new test_rope(type, {128, 32, 10, 1}, 128, 0, 512)); // llama 7B
  1280. test_cases.emplace_back(new test_rope(type, {128, 40, 10, 1}, 128, 0, 512)); // llama 13B
  1281. test_cases.emplace_back(new test_rope(type, {128, 52, 10, 1}, 128, 0, 512)); // llama 30B
  1282. test_cases.emplace_back(new test_rope(type, {128, 64, 10, 1}, 128, 0, 512)); // llama 65B
  1283. test_cases.emplace_back(new test_rope(type, { 64, 1, 10, 1}, 64, 2, 512)); // neox (falcon 7B)
  1284. test_cases.emplace_back(new test_rope(type, { 64, 71, 10, 1}, 64, 2, 512)); // neox (falcon 7B)
  1285. test_cases.emplace_back(new test_rope(type, { 64, 8, 10, 1}, 64, 2, 512)); // neox (falcon 40B)
  1286. test_cases.emplace_back(new test_rope(type, { 64, 128, 10, 1}, 64, 2, 512)); // neox (falcon 40B)
  1287. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 20, 2, 512)); // neox (stablelm)
  1288. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 32, 2, 512)); // neox (phi-2)
  1289. }
  1290. test_cases.emplace_back(new test_alibi());
  1291. test_cases.emplace_back(new test_im2col());
  1292. test_cases.emplace_back(new test_concat());
  1293. for (ggml_sort_order order : {GGML_SORT_ASC, GGML_SORT_DESC}) {
  1294. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  1295. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  1296. }
  1297. test_cases.emplace_back(new test_sum_rows());
  1298. test_cases.emplace_back(new test_upscale());
  1299. test_cases.emplace_back(new test_group_norm());
  1300. test_cases.emplace_back(new test_acc());
  1301. test_cases.emplace_back(new test_pad());
  1302. test_cases.emplace_back(new test_leaky_relu());
  1303. #if !defined(__SANITIZE_THREAD__)
  1304. // FIXME: these tests use too much memory with thread sanitizer
  1305. test_cases.emplace_back(new test_moe(8, 2, 1, 4096, 8*1024));
  1306. //test_cases.emplace_back(new test_moe(8, 2, 8, 4096, 14336));
  1307. #endif
  1308. // run tests
  1309. if (mode == MODE_TEST) {
  1310. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  1311. size_t n_ok = 0;
  1312. for (auto & test : test_cases) {
  1313. if (test->eval(backend, backend_cpu, op_name)) {
  1314. n_ok++;
  1315. }
  1316. }
  1317. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  1318. ggml_backend_free(backend_cpu);
  1319. return n_ok == test_cases.size();
  1320. }
  1321. if (mode == MODE_PERF) {
  1322. for (auto & test : test_cases) {
  1323. test->eval_perf(backend, op_name);
  1324. }
  1325. return true;
  1326. }
  1327. GGML_ASSERT(false);
  1328. return false;
  1329. }
  1330. static void usage(char ** argv) {
  1331. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  1332. printf(" valid modes are: test (compare with CPU backend for correctness) or perf (performance evaluation)\n");
  1333. printf(" op names are as given by ggml_op_desc()\n");
  1334. }
  1335. int main(int argc, char ** argv) {
  1336. test_mode mode = MODE_TEST;
  1337. const char * op_name = NULL;
  1338. const char * backend = NULL;
  1339. for (int i = 1; i < argc; i++) {
  1340. if (strcmp(argv[i], "test") == 0) {
  1341. mode = MODE_TEST;
  1342. } else if (strcmp(argv[i], "perf") == 0) {
  1343. mode = MODE_PERF;
  1344. } else if (strcmp(argv[i], "-o") == 0) {
  1345. if (i + 1 < argc) {
  1346. op_name = argv[++i];
  1347. } else {
  1348. usage(argv);
  1349. return 1;
  1350. }
  1351. } else if (strcmp(argv[i], "-b") == 0) {
  1352. if (i + 1 < argc) {
  1353. backend = argv[++i];
  1354. } else {
  1355. usage(argv);
  1356. return 1;
  1357. }
  1358. } else {
  1359. usage(argv);
  1360. return 1;
  1361. }
  1362. }
  1363. // enumerate backends
  1364. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  1365. size_t n_ok = 0;
  1366. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  1367. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  1368. if (backend != NULL && strcmp(backend, ggml_backend_reg_get_name(i)) != 0) {
  1369. printf(" Skipping\n");
  1370. n_ok++;
  1371. continue;
  1372. }
  1373. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  1374. GGML_ASSERT(backend != NULL);
  1375. printf(" Backend name: %s\n", ggml_backend_name(backend));
  1376. bool ok = test_backend(backend, mode, op_name);
  1377. printf(" Backend %s: ", ggml_backend_name(backend));
  1378. if (ok) {
  1379. printf("\033[1;32mOK\033[0m\n");
  1380. n_ok++;
  1381. } else {
  1382. printf("\033[1;31mFAIL\033[0m\n");
  1383. }
  1384. printf("\n");
  1385. ggml_backend_free(backend);
  1386. }
  1387. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  1388. if (n_ok != ggml_backend_reg_get_count()) {
  1389. printf("\033[1;31mFAIL\033[0m\n");
  1390. return 1;
  1391. }
  1392. printf("\033[1;32mOK\033[0m\n");
  1393. return 0;
  1394. }