test-backend-ops.cpp 50 KB

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