test-backend-ops.cpp 58 KB

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