test-backend-ops.cpp 60 KB

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