test-backend-ops.cpp 59 KB

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