test-backend-ops.cpp 56 KB

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