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