test-backend-ops.cpp 56 KB

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