test-backend-ops.cpp 58 KB

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