test-backend-ops.cpp 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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. static std::string var_to_str(ggml_op_pool pool) {
  201. switch (pool) {
  202. case GGML_OP_POOL_AVG: return "avg";
  203. case GGML_OP_POOL_MAX: return "max";
  204. default: return std::to_string(pool);
  205. }
  206. }
  207. #define VARS_TO_STR1(a) VAR_TO_STR(a)
  208. #define VARS_TO_STR2(a, b) VAR_TO_STR(a) + "," + VAR_TO_STR(b)
  209. #define VARS_TO_STR3(a, b, c) VAR_TO_STR(a) + "," + VARS_TO_STR2(b, c)
  210. #define VARS_TO_STR4(a, b, c, d) VAR_TO_STR(a) + "," + VARS_TO_STR3(b, c, d)
  211. #define VARS_TO_STR5(a, b, c, d, e) VAR_TO_STR(a) + "," + VARS_TO_STR4(b, c, d, e)
  212. #define VARS_TO_STR6(a, b, c, d, e, f) VAR_TO_STR(a) + "," + VARS_TO_STR5(b, c, d, e, f)
  213. #define VARS_TO_STR7(a, b, c, d, e, f, g) VAR_TO_STR(a) + "," + VARS_TO_STR6(b, c, d, e, f, g)
  214. #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)
  215. #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)
  216. #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)
  217. #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)
  218. #define VARS_TO_STR12(a, b, c, d, e, f, g, h, i, j, k, l) VAR_TO_STR(a) + "," + VARS_TO_STR11(b, c, d, e, f, g, h, i, j, k, l)
  219. #ifdef GGML_USE_SYCL
  220. static bool inline _isinf(float f) {
  221. return (*(uint32_t *)&f & 0x7fffffff) == 0x7f800000;
  222. }
  223. #else
  224. static bool inline _isinf(float f) { return std::isinf(f); }
  225. #endif
  226. // accept FLT_MAX as infinity
  227. static bool isinf_or_max(float f) {
  228. return _isinf(f) || f == FLT_MAX || f == -FLT_MAX;
  229. }
  230. static bool ggml_is_view_op(enum ggml_op op) {
  231. return op == GGML_OP_VIEW || op == GGML_OP_RESHAPE || op == GGML_OP_PERMUTE || op == GGML_OP_TRANSPOSE;
  232. }
  233. enum test_mode {
  234. MODE_TEST,
  235. MODE_PERF,
  236. };
  237. struct test_case {
  238. virtual ~test_case() {}
  239. virtual std::string op_desc(ggml_tensor * t) {
  240. return ggml_op_desc(t);
  241. }
  242. virtual std::string vars() {
  243. return "";
  244. }
  245. virtual ggml_tensor * build_graph(ggml_context * ctx) = 0;
  246. virtual double max_nmse_err() {
  247. return 1e-7;
  248. }
  249. virtual void initialize_tensors(ggml_context * ctx) {
  250. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
  251. init_tensor_uniform(t);
  252. }
  253. }
  254. virtual size_t op_size(ggml_tensor * t) {
  255. size_t size = ggml_nbytes(t);
  256. // add source tensors
  257. for (int i = 0; i < GGML_MAX_SRC; i++) {
  258. if (t->src[i] != NULL) {
  259. size += ggml_nbytes(t->src[i]);
  260. }
  261. }
  262. return size;
  263. }
  264. ggml_cgraph * gf = nullptr;
  265. static const int sentinel_size = 1024;
  266. test_mode mode;
  267. std::vector<ggml_tensor *> sentinels;
  268. void add_sentinel(ggml_context * ctx) {
  269. if (mode == MODE_PERF) {
  270. return;
  271. }
  272. ggml_tensor * sentinel = ::ggml_new_tensor_1d(ctx, GGML_TYPE_F32, sentinel_size);
  273. ggml_format_name(sentinel, "sent_%zu", sentinels.size());
  274. sentinels.push_back(sentinel);
  275. }
  276. // hijack ggml_new_tensor to add sentinels after each tensor to check for overflows in the backend
  277. ggml_tensor * ggml_new_tensor(ggml_context * ctx, ggml_type type, int n_dims, const int64_t * ne) {
  278. ggml_tensor * t = ::ggml_new_tensor(ctx, type, n_dims, ne);
  279. add_sentinel(ctx);
  280. return t;
  281. }
  282. ggml_tensor * ggml_new_tensor_1d(ggml_context * ctx, ggml_type type, int64_t ne0) {
  283. ggml_tensor * t = ::ggml_new_tensor_1d(ctx, type, ne0);
  284. add_sentinel(ctx);
  285. return t;
  286. }
  287. ggml_tensor * ggml_new_tensor_2d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1) {
  288. ggml_tensor * t = ::ggml_new_tensor_2d(ctx, type, ne0, ne1);
  289. add_sentinel(ctx);
  290. return t;
  291. }
  292. ggml_tensor * ggml_new_tensor_3d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2) {
  293. ggml_tensor * t = ::ggml_new_tensor_3d(ctx, type, ne0, ne1, ne2);
  294. add_sentinel(ctx);
  295. return t;
  296. }
  297. ggml_tensor * ggml_new_tensor_4d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3) {
  298. ggml_tensor * t = ::ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3);
  299. add_sentinel(ctx);
  300. return t;
  301. }
  302. bool eval(ggml_backend_t backend1, ggml_backend_t backend2, const char * op_name) {
  303. mode = MODE_TEST;
  304. ggml_init_params params = {
  305. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead(),
  306. /* .mem_base = */ NULL,
  307. /* .no_alloc = */ true,
  308. };
  309. ggml_context * ctx = ggml_init(params);
  310. gf = ggml_new_graph(ctx);
  311. // pre-graph sentinel
  312. add_sentinel(ctx);
  313. ggml_tensor * out = build_graph(ctx);
  314. if (op_name != nullptr && op_desc(out) != op_name) {
  315. //printf(" %s: skipping\n", op_desc(out).c_str());
  316. ggml_free(ctx);
  317. return true;
  318. }
  319. printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  320. fflush(stdout);
  321. // check if the backends support the ops
  322. bool supported = true;
  323. for (ggml_backend_t backend : {backend1, backend2}) {
  324. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  325. if (!ggml_backend_supports_op(backend, t)) {
  326. printf("not supported [%s] ", ggml_backend_name(backend));
  327. supported = false;
  328. break;
  329. }
  330. }
  331. }
  332. if (!supported) {
  333. printf("\n");
  334. ggml_free(ctx);
  335. return true;
  336. }
  337. // post-graph sentinel
  338. add_sentinel(ctx);
  339. // allocate
  340. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend1);
  341. if (buf == NULL) {
  342. printf("failed to allocate tensors [%s] ", ggml_backend_name(backend1));
  343. ggml_free(ctx);
  344. return false;
  345. }
  346. // build graph
  347. ggml_build_forward_expand(gf, out);
  348. // add sentinels as graph nodes so that they are checked in the callback
  349. for (ggml_tensor * sentinel : sentinels) {
  350. gf->nodes[gf->n_nodes++] = sentinel;
  351. }
  352. // randomize tensors
  353. initialize_tensors(ctx);
  354. // compare
  355. struct callback_userdata {
  356. bool ok;
  357. double max_err;
  358. ggml_backend_t backend1;
  359. ggml_backend_t backend2;
  360. };
  361. callback_userdata ud {
  362. true,
  363. max_nmse_err(),
  364. backend1,
  365. backend2
  366. };
  367. auto callback = [](int index, ggml_tensor * t1, ggml_tensor * t2, void * user_data) -> bool {
  368. callback_userdata * ud = (callback_userdata *) user_data;
  369. const char * bn1 = ggml_backend_name(ud->backend1);
  370. const char * bn2 = ggml_backend_name(ud->backend2);
  371. if (t1->op == GGML_OP_NONE) {
  372. // sentinels must be unchanged
  373. std::vector<uint8_t> t1_data(ggml_nbytes(t1));
  374. std::vector<uint8_t> t2_data(ggml_nbytes(t2));
  375. ggml_backend_tensor_get(t1, t1_data.data(), 0, ggml_nbytes(t1));
  376. ggml_backend_tensor_get(t2, t2_data.data(), 0, ggml_nbytes(t2));
  377. if (memcmp(t1_data.data(), t2_data.data(), ggml_nbytes(t1)) != 0) {
  378. printf("sentinel mismatch: %s ", t1->name);
  379. ud->ok = false;
  380. return true;
  381. }
  382. }
  383. std::vector<float> f1 = tensor_to_float(t1);
  384. std::vector<float> f2 = tensor_to_float(t2);
  385. for (size_t i = 0; i < f1.size(); i++) {
  386. // check for nans
  387. if (std::isnan(f1[i]) || std::isnan(f2[i])) {
  388. printf("[%s] NaN at index %zu (%s=%f %s=%f) ", ggml_op_desc(t1), i, bn1, f1[i], bn2, f2[i]);
  389. ud->ok = false;
  390. return true;
  391. }
  392. // check for infs: both must be inf of the same sign, or both must be finite
  393. if (isinf_or_max(f1[i]) || isinf_or_max(f2[i])) {
  394. if (isinf_or_max(f1[i]) && isinf_or_max(f2[i])) {
  395. if (std::signbit(f1[i]) != std::signbit(f2[i])) {
  396. printf("[%s] inf sign mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]);
  397. ud->ok = false;
  398. return true;
  399. }
  400. } else {
  401. printf("[%s] inf mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]);
  402. ud->ok = false;
  403. return true;
  404. }
  405. }
  406. }
  407. double err = nmse(f1.data(), f2.data(), f1.size());
  408. if (err > ud->max_err) {
  409. printf("[%s] NMSE = %.9f > %.9f ", ggml_op_desc(t1), err, ud->max_err);
  410. //for (int i = 0; i < (int) f1.size(); i++) {
  411. // printf("%5d %9.6f %9.6f, diff = %9.6f\n", i, f1[i], f2[i], f1[i] - f2[i]);
  412. //}
  413. //printf("\n");
  414. //exit(1);
  415. ud->ok = false;
  416. }
  417. return true;
  418. GGML_UNUSED(index);
  419. };
  420. const bool cmp_ok = ggml_backend_compare_graph_backend(backend1, backend2, gf, callback, &ud);
  421. if (!cmp_ok) {
  422. printf("compare failed ");
  423. }
  424. ggml_backend_buffer_free(buf);
  425. ggml_free(ctx);
  426. if (ud.ok && cmp_ok) {
  427. printf("\033[1;32mOK\033[0m\n");
  428. return true;
  429. }
  430. printf("\033[1;31mFAIL\033[0m\n");
  431. return false;
  432. }
  433. bool eval_perf(ggml_backend_t backend, const char * op_name) {
  434. mode = MODE_PERF;
  435. static const size_t graph_nodes = 8192;
  436. ggml_init_params params = {
  437. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead_custom(graph_nodes, false),
  438. /* .mem_base = */ NULL,
  439. /* .no_alloc = */ true,
  440. };
  441. ggml_context * ctx = ggml_init(params);
  442. ggml_tensor * out = build_graph(ctx);
  443. if (op_name != nullptr && op_desc(out) != op_name) {
  444. //printf(" %s: skipping\n", op_desc(out).c_str());
  445. ggml_free(ctx);
  446. return true;
  447. }
  448. int len = printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  449. fflush(stdout);
  450. // check if backends support op
  451. if (!ggml_backend_supports_op(backend, out)) {
  452. printf("not supported\n");
  453. ggml_free(ctx);
  454. return true;
  455. }
  456. // align while also leaving some margin for variations in parameters
  457. int align = 20;
  458. int last = (len + align - 1) / align * align;
  459. if (last - len < 5) {
  460. last += align;
  461. }
  462. last = std::max(last, 60);
  463. printf("%*s", last - len, "");
  464. // allocate
  465. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend);
  466. if (buf == NULL) {
  467. printf("failed to allocate tensors\n");
  468. ggml_free(ctx);
  469. return false;
  470. }
  471. // randomize tensors
  472. initialize_tensors(ctx);
  473. // build graph
  474. ggml_cgraph * gf = ggml_new_graph_custom(ctx, graph_nodes, false);
  475. ggml_build_forward_expand(gf, out);
  476. // warmup run
  477. ggml_backend_graph_compute(backend, gf);
  478. // duplicate the op
  479. size_t target_size = ggml_backend_is_cpu(backend) ? 1ULL << 33 : 1ULL << 35; // 8 GB CPU, 32 GB GPU
  480. int n_runs = std::min((size_t)gf->size - gf->n_nodes, target_size / op_size(out)) + 1;
  481. for (int i = 1; i < n_runs; i++) {
  482. gf->nodes[gf->n_nodes++] = out;
  483. }
  484. // calculate memory
  485. size_t mem = n_runs * op_size(out);
  486. auto tensor_op_size = [](ggml_tensor * t) {
  487. size_t size = ggml_nbytes(t);
  488. // add source tensors
  489. for (int i = 0; i < GGML_MAX_SRC; i++) {
  490. if (t->src[i] != NULL) {
  491. size += ggml_nbytes(t->src[i]);
  492. }
  493. }
  494. return size;
  495. };
  496. for (int i = 0; i < gf->n_nodes; i++) {
  497. if (ggml_is_view_op(gf->nodes[i]->op) || gf->nodes[i] == out) {
  498. continue;
  499. }
  500. mem += tensor_op_size(gf->nodes[i]);
  501. }
  502. // run
  503. ggml_backend_synchronize(backend);
  504. int64_t start_time = ggml_time_us();
  505. ggml_backend_graph_compute(backend, gf);
  506. ggml_backend_synchronize(backend);
  507. int64_t end_time = ggml_time_us();
  508. double time_us = end_time - start_time;
  509. printf(" %5d runs - %8.2f us/run - %8zu kB/run - \033[1;34m%7.2f GB/s\033[0m\n",
  510. n_runs,
  511. time_us / n_runs,
  512. op_size(out) / 1024,
  513. mem / (time_us/1e6) / 1024.0 / 1024.0 / 1024.0);
  514. ggml_backend_buffer_free(buf);
  515. ggml_free(ctx);
  516. return true;
  517. }
  518. };
  519. // GGML_OP_UNARY
  520. struct test_unary : public test_case {
  521. const ggml_unary_op op;
  522. const ggml_type type;
  523. const std::array<int64_t, 4> ne;
  524. std::string vars() override {
  525. return VARS_TO_STR2(type, ne);
  526. }
  527. test_unary(ggml_unary_op op,
  528. ggml_type type = GGML_TYPE_F32,
  529. std::array<int64_t, 4> ne = {128, 10, 10, 10})
  530. : op(op), type(type), ne(ne) {}
  531. ggml_tensor * build_graph(ggml_context * ctx) override {
  532. ggml_tensor * in = ggml_new_tensor(ctx, type, 4, ne.data());
  533. ggml_tensor * out = ggml_unary(ctx, in, op);
  534. return out;
  535. }
  536. void initialize_tensors(ggml_context * ctx) override {
  537. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  538. // test extended range of values to check for NaNs in GELU
  539. init_tensor_uniform(t, -150.f, 150.f);
  540. }
  541. }
  542. };
  543. // GGML_OP_GET_ROWS
  544. struct test_get_rows : public test_case {
  545. const ggml_type type;
  546. const int n; // cols
  547. const int m; // rows
  548. const int r; // rows to get
  549. const int b; // batch size
  550. const bool v; // view (non-contiguous src1)
  551. std::string vars() override {
  552. return VARS_TO_STR6(type, n, m, r, b, v);
  553. }
  554. test_get_rows(ggml_type type = GGML_TYPE_F32, int n = 10, int m = 5, int r = 3, int b = 1, bool v = false)
  555. : type(type), n(n), m(m), r(r), b(b), v(v) {}
  556. ggml_tensor * build_graph(ggml_context * ctx) override {
  557. ggml_tensor * in = ggml_new_tensor_3d(ctx, type, n, m, b);
  558. ggml_tensor * rows = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, r, b);
  559. if (v) {
  560. rows = ggml_view_2d(ctx, rows, r/2, b, rows->nb[1], 0);
  561. }
  562. ggml_tensor * out = ggml_get_rows(ctx, in, rows);
  563. return out;
  564. }
  565. void initialize_tensors(ggml_context * ctx) override {
  566. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  567. if (t->type == GGML_TYPE_I32) {
  568. if (ggml_is_view_op(t->op)) { continue; }
  569. // rows
  570. std::vector<int> data(r*b);
  571. for (int i = 0; i < r*b; i++) {
  572. data[i] = rand() % m;
  573. }
  574. ggml_backend_tensor_set(t, data.data(), 0, r * b * sizeof(int));
  575. } else {
  576. init_tensor_uniform(t);
  577. }
  578. }
  579. }
  580. };
  581. // GGML_OP_REPEAT
  582. struct test_repeat : public test_case {
  583. const ggml_type type;
  584. const std::array<int64_t, 4> ne;
  585. const std::array<int, 4> nr;
  586. std::string vars() override {
  587. return VARS_TO_STR3(type, ne, nr);
  588. }
  589. size_t op_size(ggml_tensor * t) override {
  590. return ggml_nbytes(t) * 2;
  591. }
  592. test_repeat(ggml_type type = GGML_TYPE_F32,
  593. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  594. std::array<int, 4> nr = {2, 2, 2, 2})
  595. : type(type), ne(ne), nr(nr) {}
  596. ggml_tensor * build_graph(ggml_context * ctx) override {
  597. 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]);
  598. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  599. ggml_tensor * out = ggml_repeat(ctx, src, target);
  600. return out;
  601. }
  602. };
  603. // GGML_OP_DUP
  604. struct test_dup : public test_case {
  605. const ggml_type type;
  606. const std::array<int64_t, 4> ne;
  607. const std::array<int64_t, 4> permute;
  608. bool _use_permute;
  609. std::string vars() override {
  610. std::string v = VARS_TO_STR2(type, ne);
  611. if (_use_permute) v += "," + VAR_TO_STR(permute);
  612. return v;
  613. }
  614. test_dup(ggml_type type = GGML_TYPE_F32,
  615. std::array<int64_t, 4> ne = {10, 10, 10, 1},
  616. std::array<int64_t, 4> permute = {0, 0, 0, 0})
  617. : type(type), ne(ne), permute(permute),
  618. _use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {}
  619. ggml_tensor * build_graph(ggml_context * ctx) override {
  620. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  621. if (_use_permute) {
  622. src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]);
  623. }
  624. ggml_tensor * out = ggml_dup(ctx, src);
  625. return out;
  626. }
  627. };
  628. // GGML_OP_CPY
  629. struct test_cpy : public test_case {
  630. const ggml_type type_src;
  631. const ggml_type type_dst;
  632. const std::array<int64_t, 4> ne;
  633. std::string vars() override {
  634. return VARS_TO_STR3(type_src, type_dst, ne);
  635. }
  636. size_t op_size(ggml_tensor * t) override {
  637. return ggml_nbytes(t) + ggml_nbytes(t->src[0]);
  638. }
  639. test_cpy(ggml_type type_src = GGML_TYPE_F32, ggml_type type_dst = GGML_TYPE_F32,
  640. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  641. : type_src(type_src), type_dst(type_dst), ne(ne) {}
  642. ggml_tensor * build_graph(ggml_context * ctx) override {
  643. ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne.data());
  644. ggml_tensor * dst = ggml_new_tensor(ctx, type_dst, 4, ne.data());
  645. ggml_tensor * out = ggml_cpy(ctx, src, dst);
  646. return out;
  647. }
  648. };
  649. // GGML_OP_CONT
  650. struct test_cont : public test_case {
  651. const ggml_type type;
  652. const std::array<int64_t, 4> ne;
  653. std::string vars() override {
  654. return VARS_TO_STR2(type, ne);
  655. }
  656. test_cont(ggml_type type = GGML_TYPE_F32,
  657. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  658. : type(type), ne(ne) {}
  659. ggml_tensor * build_graph(ggml_context * ctx) override {
  660. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  661. src = ggml_transpose(ctx, src);
  662. ggml_tensor * out = ggml_cont(ctx, src);
  663. return out;
  664. }
  665. };
  666. // GGML_OP_ADD
  667. // GGML_OP_MUL
  668. // GGML_OP_DIV
  669. struct test_bin_bcast : public test_case {
  670. using op_t = ggml_tensor * (*) (ggml_context *, ggml_tensor *, ggml_tensor *);
  671. op_t op;
  672. const ggml_type type;
  673. const std::array<int64_t, 4> ne;
  674. const std::array<int, 4> nr;
  675. std::string vars() override {
  676. return VARS_TO_STR3(type, ne, nr);
  677. }
  678. size_t op_size(ggml_tensor * t) override {
  679. return ggml_nbytes(t) * 3;
  680. }
  681. test_bin_bcast(op_t op, ggml_type type = GGML_TYPE_F32,
  682. std::array<int64_t, 4> ne = {10, 10, 1, 1},
  683. std::array<int, 4> nr = {1, 2, 1, 1})
  684. : op(op), type(type), ne(ne), nr(nr) {}
  685. ggml_tensor * build_graph(ggml_context * ctx) override {
  686. 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]);
  687. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data());
  688. ggml_tensor * out = op(ctx, a, b);
  689. return out;
  690. }
  691. void initialize_tensors(ggml_context * ctx) override {
  692. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  693. if (op == ggml_div) {
  694. // avoid division by zero
  695. init_tensor_uniform(t, 1.0f, 2.0f);
  696. } else {
  697. init_tensor_uniform(t);
  698. }
  699. }
  700. }
  701. };
  702. // GGML_OP_SCALE
  703. struct test_scale : public test_case {
  704. const ggml_type type;
  705. const std::array<int64_t, 4> ne;
  706. float scale;
  707. std::string vars() override {
  708. return VARS_TO_STR3(type, ne, scale);
  709. }
  710. test_scale(ggml_type type = GGML_TYPE_F32,
  711. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  712. float scale = 2.0f)
  713. : type(type), ne(ne), scale(scale) {}
  714. ggml_tensor * build_graph(ggml_context * ctx) override {
  715. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  716. ggml_tensor * out = ggml_scale(ctx, a, scale);
  717. return out;
  718. }
  719. };
  720. // GGML_OP_NORM
  721. struct test_norm : public test_case {
  722. const ggml_type type;
  723. const std::array<int64_t, 4> ne;
  724. float eps;
  725. std::string vars() override {
  726. return VARS_TO_STR3(type, ne, eps);
  727. }
  728. test_norm(ggml_type type = GGML_TYPE_F32,
  729. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  730. float eps = 1e-6f)
  731. : type(type), ne(ne), eps(eps) {}
  732. ggml_tensor * build_graph(ggml_context * ctx) override {
  733. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  734. ggml_tensor * out = ggml_norm(ctx, a, eps);
  735. return out;
  736. }
  737. };
  738. // GGML_OP_RMS_NORM
  739. struct test_rms_norm : public test_case {
  740. const ggml_type type;
  741. const std::array<int64_t, 4> ne;
  742. float eps;
  743. std::string vars() override {
  744. return VARS_TO_STR3(type, ne, eps);
  745. }
  746. test_rms_norm(ggml_type type = GGML_TYPE_F32,
  747. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  748. float eps = 1e-6f)
  749. : type(type), ne(ne), eps(eps) {}
  750. ggml_tensor * build_graph(ggml_context * ctx) override {
  751. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  752. ggml_tensor * out = ggml_rms_norm(ctx, a, eps);
  753. return out;
  754. }
  755. };
  756. // GGML_OP_MUL_MAT
  757. struct test_mul_mat : public test_case {
  758. const ggml_type type_a;
  759. const ggml_type type_b;
  760. const int64_t m;
  761. const int64_t n;
  762. const int64_t k;
  763. const std::array<int64_t, 2> bs; // dims 3 and 4
  764. const std::array<int64_t, 2> nr; // repeat in dims 3 and 4
  765. std::string vars() override {
  766. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, nr);
  767. }
  768. double max_nmse_err() override {
  769. return 5e-4;
  770. }
  771. size_t op_size(ggml_tensor * t) override {
  772. size_t a = ggml_nbytes(t->src[0]) * n * nr[0] * nr[1];
  773. size_t b = ggml_nbytes(t->src[1]) * m;
  774. size_t c = ggml_nbytes(t);
  775. return a + b + c;
  776. GGML_UNUSED(t);
  777. }
  778. test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  779. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  780. std::array<int64_t, 2> bs = {10, 10},
  781. std::array<int64_t, 2> nr = {2, 2})
  782. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr) {}
  783. ggml_tensor * build_graph(ggml_context * ctx) override {
  784. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  785. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, k, m, bs[0] , bs[1]);
  786. ggml_tensor * b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]);
  787. ggml_tensor * out = ggml_mul_mat(ctx, a, b);
  788. return out;
  789. }
  790. };
  791. // GGML_OP_MUL_MAT_ID
  792. struct test_mul_mat_id : public test_case {
  793. const ggml_type type_a;
  794. const ggml_type type_b;
  795. const int n_mats;
  796. const int id;
  797. const int64_t m;
  798. const int64_t n;
  799. const int64_t k;
  800. const bool v; // view (non-contiguous ids)
  801. std::string vars() override {
  802. return VARS_TO_STR8(type_a, type_b, n_mats, id, m, n, k, v);
  803. }
  804. double max_nmse_err() override {
  805. return 5e-4;
  806. }
  807. size_t op_size(ggml_tensor * t) override {
  808. size_t a = ggml_nbytes(t->src[2]) * n;
  809. size_t b = ggml_nbytes(t->src[1]) * m;
  810. size_t c = ggml_nbytes(t);
  811. return a + b + c;
  812. GGML_UNUSED(t);
  813. }
  814. test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  815. int n_mats = 2, int id = 0,
  816. int64_t m = 32, int64_t n = 32, int64_t k = 32, bool v = false)
  817. : type_a(type_a), type_b(type_b), n_mats(n_mats), id(id),
  818. m(m), n(n), k(k), v(v) {}
  819. ggml_tensor * build_graph(ggml_context * ctx) override {
  820. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  821. std::vector<ggml_tensor *> mats;
  822. for (int i = 0; i < n_mats; i++) {
  823. ggml_tensor * a = ggml_new_tensor_2d(ctx, type_a, k, m);
  824. mats.push_back(a);
  825. }
  826. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n);
  827. if (v) {
  828. ids = ggml_view_2d(ctx, ids, n_mats/2, ids->ne[1], ids->nb[1], 0);
  829. }
  830. ggml_tensor * b = ggml_new_tensor_2d(ctx, type_b, k, n);
  831. ggml_tensor * out = ggml_mul_mat_id(ctx, mats.data(), n_mats, ids, v ? id/2 : id, b);
  832. return out;
  833. }
  834. void initialize_tensors(ggml_context * ctx) override {
  835. std::random_device rd;
  836. std::default_random_engine rng(rd());
  837. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  838. if (t->type == GGML_TYPE_I32) {
  839. if (ggml_is_view_op(t->op)) { continue; }
  840. // ids
  841. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  842. std::vector<int32_t> data(t->ne[0]);
  843. for (int i = 0; i < t->ne[0]; i++) {
  844. data[i] = i % n_mats;
  845. }
  846. std::shuffle(data.begin(), data.end(), rng);
  847. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t));
  848. }
  849. } else {
  850. init_tensor_uniform(t);
  851. }
  852. }
  853. }
  854. };
  855. // GGML_OP_SQR
  856. struct test_sqr : public test_case {
  857. const ggml_type type;
  858. const std::array<int64_t, 4> ne;
  859. std::string vars() override {
  860. return VARS_TO_STR2(type, ne);
  861. }
  862. test_sqr(ggml_type type = GGML_TYPE_F32,
  863. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  864. : type(type), ne(ne) {}
  865. ggml_tensor * build_graph(ggml_context * ctx) override {
  866. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  867. ggml_tensor * out = ggml_sqr(ctx, a);
  868. return out;
  869. }
  870. };
  871. // GGML_OP_CLAMP
  872. struct test_clamp : public test_case {
  873. const ggml_type type;
  874. const std::array<int64_t, 4> ne;
  875. float min;
  876. float max;
  877. std::string vars() override {
  878. return VARS_TO_STR4(type, ne, min, max);
  879. }
  880. test_clamp(ggml_type type = GGML_TYPE_F32,
  881. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  882. float min = -0.5f, float max = 0.5f)
  883. : type(type), ne(ne), min(min), max(max) {}
  884. ggml_tensor * build_graph(ggml_context * ctx) override {
  885. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  886. ggml_tensor * out = ggml_clamp(ctx, a, min, max);
  887. return out;
  888. }
  889. };
  890. // GGML_OP_DIAG_MASK_INF
  891. struct test_diag_mask_inf : public test_case {
  892. const ggml_type type;
  893. const std::array<int64_t, 4> ne;
  894. const int n_past;
  895. std::string vars() override {
  896. return VARS_TO_STR3(type, ne, n_past);
  897. }
  898. test_diag_mask_inf(ggml_type type = GGML_TYPE_F32,
  899. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  900. int n_past = 5)
  901. : type(type), ne(ne), n_past(n_past) {}
  902. ggml_tensor * build_graph(ggml_context * ctx) override {
  903. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  904. ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past);
  905. return out;
  906. }
  907. };
  908. // GGML_OP_SOFT_MAX
  909. struct test_soft_max : public test_case {
  910. const ggml_type type;
  911. const std::array<int64_t, 4> ne;
  912. const bool mask;
  913. const float scale;
  914. const float max_bias;
  915. std::string vars() override {
  916. return VARS_TO_STR5(type, ne, mask, scale, max_bias);
  917. }
  918. test_soft_max(ggml_type type = GGML_TYPE_F32,
  919. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  920. bool mask = false,
  921. float scale = 1.0f,
  922. float max_bias = 0.0f)
  923. : type(type), ne(ne), mask(mask), scale(scale), max_bias(max_bias) {}
  924. ggml_tensor * build_graph(ggml_context * ctx) override {
  925. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  926. ggml_tensor * mask = nullptr;
  927. if (this->mask) {
  928. mask = ggml_new_tensor_2d(ctx, type, ne[0], ne[1]);
  929. }
  930. ggml_tensor * pos = nullptr;
  931. if (max_bias > 0.0f) {
  932. pos = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, ne[0]);
  933. }
  934. ggml_tensor * out = ggml_soft_max_ext(ctx, a, mask, pos, scale, max_bias);
  935. return out;
  936. }
  937. };
  938. // GGML_OP_ROPE
  939. struct test_rope : public test_case {
  940. const ggml_type type;
  941. const std::array<int64_t, 4> ne;
  942. int n_dims;
  943. int mode;
  944. int n_ctx;
  945. std::string vars() override {
  946. return VARS_TO_STR5(type, ne, n_dims, mode, n_ctx);
  947. }
  948. test_rope(ggml_type type = GGML_TYPE_F32,
  949. std::array<int64_t, 4> ne = {10, 10, 10, 1},
  950. int n_dims = 10, int mode = 0, int n_ctx = 512)
  951. : type(type), ne(ne), n_dims(n_dims), mode(mode), n_ctx(n_ctx) {}
  952. ggml_tensor * build_graph(ggml_context * ctx) override {
  953. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  954. ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne[2]);
  955. ggml_tensor * out = ggml_rope(ctx, a, pos, n_dims, mode, n_ctx);
  956. return out;
  957. }
  958. void initialize_tensors(ggml_context * ctx) override {
  959. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  960. if (t->type == GGML_TYPE_I32) {
  961. // pos
  962. std::vector<int> data(ne[2]);
  963. for (int i = 0; i < ne[2]; i++) {
  964. data[i] = rand() % n_ctx;
  965. }
  966. ggml_backend_tensor_set(t, data.data(), 0, ne[2] * sizeof(int));
  967. } else {
  968. init_tensor_uniform(t);
  969. }
  970. }
  971. }
  972. };
  973. // GGML_OP_POOL2D
  974. struct test_pool2d : public test_case {
  975. enum ggml_op_pool pool_type;
  976. const ggml_type type_input;
  977. const std::array<int64_t, 4> ne_input;
  978. // kernel size
  979. const int k0;
  980. const int k1;
  981. // stride
  982. const int s0;
  983. const int s1;
  984. // padding
  985. const int p0;
  986. const int p1;
  987. std::string vars() override {
  988. return VARS_TO_STR9(pool_type, type_input, ne_input, k0, k1, s0, s1, p0, p1);
  989. }
  990. test_pool2d(ggml_op_pool pool_type = GGML_OP_POOL_AVG,
  991. ggml_type type_input = GGML_TYPE_F32,
  992. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  993. int k0 = 3, int k1 = 3,
  994. int s0 = 1, int s1 = 1,
  995. int p0 = 1, int p1 = 1)
  996. : pool_type(pool_type), type_input(type_input), ne_input(ne_input), k0(k0), k1(k1), s0(s0), s1(s1), p0(p0), p1(p1) {}
  997. ggml_tensor * build_graph(ggml_context * ctx) override {
  998. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  999. ggml_tensor * out = ggml_pool_2d(ctx, input, pool_type, k0, k1, s0, s1, p0, p1);
  1000. return out;
  1001. }
  1002. };
  1003. // GGML_OP_IM2COL
  1004. struct test_im2col : public test_case {
  1005. const ggml_type type_input;
  1006. const ggml_type type_kernel;
  1007. const ggml_type dst_type;
  1008. const std::array<int64_t, 4> ne_input;
  1009. const std::array<int64_t, 4> ne_kernel;
  1010. // stride
  1011. const int s0;
  1012. const int s1;
  1013. // padding
  1014. const int p0;
  1015. const int p1;
  1016. // dilatation
  1017. const int d0;
  1018. const int d1;
  1019. // mode
  1020. const bool is_2D;
  1021. std::string vars() override {
  1022. return VARS_TO_STR12(type_input, type_kernel, dst_type, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  1023. }
  1024. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32,
  1025. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1026. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1027. int s0 = 1, int s1 = 1,
  1028. int p0 = 1, int p1 = 1,
  1029. int d0 = 1, int d1 = 1,
  1030. bool is_2D = true)
  1031. : type_input(type_input), type_kernel(type_kernel), dst_type(dst_type), ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), s1(s1), p0(p0), p1(p1), d0(d0), d1(d1), is_2D(is_2D) {}
  1032. ggml_tensor * build_graph(ggml_context * ctx) override {
  1033. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1034. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  1035. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D, dst_type);
  1036. return out;
  1037. }
  1038. };
  1039. // GGML_OP_CONCAT
  1040. struct test_concat : public test_case {
  1041. const ggml_type type;
  1042. const std::array<int64_t, 4> ne;
  1043. const int64_t b_ne2;
  1044. std::string vars() override {
  1045. return VARS_TO_STR3(type, ne, b_ne2);
  1046. }
  1047. test_concat(ggml_type type = GGML_TYPE_F32,
  1048. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  1049. int64_t b_ne2 = 10)
  1050. : type(type), ne(ne), b_ne2(b_ne2) {}
  1051. ggml_tensor * build_graph(ggml_context * ctx) override {
  1052. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1053. ggml_tensor * b = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], b_ne2, ne[3]);
  1054. ggml_tensor * out = ggml_concat(ctx, a, b);
  1055. return out;
  1056. }
  1057. };
  1058. // GGML_OP_ARGSORT
  1059. struct test_argsort : public test_case {
  1060. const ggml_type type;
  1061. const std::array<int64_t, 4> ne;
  1062. ggml_sort_order order;
  1063. std::string vars() override {
  1064. return VARS_TO_STR3(type, ne, order);
  1065. }
  1066. test_argsort(ggml_type type = GGML_TYPE_F32,
  1067. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  1068. ggml_sort_order order = GGML_SORT_ASC)
  1069. : type(type), ne(ne), order(order) {}
  1070. ggml_tensor * build_graph(ggml_context * ctx) override {
  1071. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1072. ggml_tensor * out = ggml_argsort(ctx, a, order);
  1073. return out;
  1074. }
  1075. void initialize_tensors(ggml_context * ctx) override {
  1076. std::random_device rd;
  1077. std::default_random_engine rng(rd());
  1078. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1079. if (t->type == GGML_TYPE_I32) {
  1080. // indices
  1081. std::vector<int> data(ggml_nelements(t));
  1082. for (int i = 0; i < ggml_nelements(t); i++) {
  1083. data[i] = rand();
  1084. }
  1085. std::shuffle(data.begin(), data.end(), rng);
  1086. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  1087. } else if (t->type == GGML_TYPE_F32) {
  1088. // initialize with unique values to avoid ties
  1089. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1090. std::vector<float> data(t->ne[0]);
  1091. for (int i = 0; i < t->ne[0]; i++) {
  1092. data[i] = i;
  1093. }
  1094. std::shuffle(data.begin(), data.end(), rng);
  1095. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  1096. }
  1097. } else {
  1098. GGML_ASSERT(false);
  1099. }
  1100. }
  1101. }
  1102. };
  1103. // GGML_OP_SUM_ROWS
  1104. struct test_sum_rows : public test_case {
  1105. const ggml_type type;
  1106. const std::array<int64_t, 4> ne;
  1107. std::string vars() override {
  1108. return VARS_TO_STR2(type, ne);
  1109. }
  1110. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1111. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  1112. : type(type), ne(ne) {}
  1113. ggml_tensor * build_graph(ggml_context * ctx) override {
  1114. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1115. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1116. return out;
  1117. }
  1118. };
  1119. // GGML_OP_UPSCALE
  1120. struct test_upscale : public test_case {
  1121. const ggml_type type;
  1122. const std::array<int64_t, 4> ne;
  1123. const int32_t scale_factor;
  1124. std::string vars() override {
  1125. return VARS_TO_STR3(type, ne, scale_factor);
  1126. }
  1127. test_upscale(ggml_type type = GGML_TYPE_F32,
  1128. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1129. int32_t scale_factor = 2)
  1130. : type(type), ne(ne), scale_factor(scale_factor) {}
  1131. ggml_tensor * build_graph(ggml_context * ctx) override {
  1132. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1133. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1134. return out;
  1135. }
  1136. };
  1137. // GGML_OP_GROUP_NORM
  1138. struct test_group_norm : public test_case {
  1139. const ggml_type type;
  1140. const std::array<int64_t, 4> ne;
  1141. const int32_t num_groups;
  1142. std::string vars() override {
  1143. return VARS_TO_STR3(type, ne, num_groups);
  1144. }
  1145. test_group_norm(ggml_type type = GGML_TYPE_F32,
  1146. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  1147. int32_t num_groups = 32)
  1148. : type(type), ne(ne), num_groups(num_groups) {}
  1149. ggml_tensor * build_graph(ggml_context * ctx) override {
  1150. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1151. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups);
  1152. return out;
  1153. }
  1154. };
  1155. // GGML_OP_ACC
  1156. struct test_acc : public test_case {
  1157. const ggml_type type;
  1158. const std::array<int64_t, 4> ne_a;
  1159. const std::array<int64_t, 4> ne_b;
  1160. std::string vars() override {
  1161. return VARS_TO_STR3(type, ne_a, ne_b);
  1162. }
  1163. test_acc(ggml_type type = GGML_TYPE_F32,
  1164. std::array<int64_t, 4> ne_a = {1024, 577, 1, 1},
  1165. std::array<int64_t, 4> ne_b = {1024, 576, 1, 1})
  1166. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1167. ggml_tensor * build_graph(ggml_context * ctx) override {
  1168. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1169. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1170. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  1171. return out;
  1172. }
  1173. };
  1174. // GGML_OP_PAD
  1175. struct test_pad : public test_case {
  1176. const ggml_type type;
  1177. const std::array<int64_t, 4> ne_a;
  1178. const int pad_0;
  1179. const int pad_1;
  1180. std::string vars() override {
  1181. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  1182. }
  1183. test_pad(ggml_type type = GGML_TYPE_F32,
  1184. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  1185. int pad_0 = 1, int pad_1 = 1)
  1186. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  1187. ggml_tensor * build_graph(ggml_context * ctx) override {
  1188. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1189. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  1190. return out;
  1191. }
  1192. };
  1193. // GGML_OP_LEAKY_RELU
  1194. struct test_leaky_relu : public test_case {
  1195. const ggml_type type;
  1196. const std::array<int64_t, 4> ne_a;
  1197. const float negative_slope;
  1198. std::string vars() override {
  1199. return VARS_TO_STR3(type, ne_a, negative_slope);
  1200. }
  1201. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  1202. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1203. float negative_slope = 0.1f)
  1204. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  1205. ggml_tensor * build_graph(ggml_context * ctx) override {
  1206. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1207. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  1208. return out;
  1209. }
  1210. };
  1211. // Mixtral MOE
  1212. struct test_moe : public test_case {
  1213. const int n_experts;
  1214. const int n_experts_per_tok;
  1215. const int n_tokens;
  1216. const int n_embd;
  1217. const int n_ff;
  1218. std::string op_desc(ggml_tensor * t) override {
  1219. return "MOE";
  1220. GGML_UNUSED(t);
  1221. }
  1222. std::string vars() override {
  1223. return VARS_TO_STR5(n_experts, n_experts_per_tok, n_tokens, n_embd, n_ff);
  1224. }
  1225. test_moe(int n_experts = 8, int n_experts_per_tok = 2, int n_tokens = 1, int n_embd = 4096, int n_ff = 14336)
  1226. : n_experts(n_experts), n_experts_per_tok(n_experts_per_tok), n_tokens(n_tokens), n_embd(n_embd), n_ff(n_ff) {
  1227. }
  1228. ggml_tensor * build_graph(ggml_context * ctx) override {
  1229. ggml_tensor * ffn_gate_inp = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_experts);
  1230. std::vector<ggml_tensor *> ffn_up_exp(n_experts);
  1231. std::vector<ggml_tensor *> ffn_gate_exp(n_experts);
  1232. std::vector<ggml_tensor *> ffn_down_exp(n_experts);
  1233. for (int i = 0; i < n_experts; ++i) {
  1234. ffn_up_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff);
  1235. ffn_gate_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff);
  1236. ffn_down_exp[i] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_ff, n_embd);
  1237. }
  1238. ggml_tensor * cur = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_tokens);
  1239. ggml_tensor * logits = ggml_mul_mat(ctx, ffn_gate_inp, cur);
  1240. ggml_tensor * probs = ggml_soft_max_ext(ctx, logits, nullptr, nullptr, 1.0f/sqrtf(n_embd), 0.0f);
  1241. // select experts
  1242. ggml_tensor * selected_experts = ggml_top_k(ctx, probs, n_experts_per_tok);
  1243. ggml_tensor * weights = ggml_get_rows(ctx,
  1244. ggml_reshape_3d(ctx, probs, 1, n_experts, n_tokens), selected_experts);
  1245. weights = ggml_reshape_2d(ctx, weights, n_experts_per_tok, n_tokens);
  1246. ggml_tensor * weights_sum = ggml_sum_rows(ctx, weights);
  1247. weights = ggml_div(ctx, weights, weights_sum);
  1248. // compute expert outputs
  1249. ggml_tensor * moe_out = nullptr;
  1250. for (int i = 0; i < n_experts_per_tok; ++i) {
  1251. ggml_tensor * cur_expert;
  1252. ggml_tensor * cur_up = ggml_mul_mat_id(ctx, ffn_up_exp.data(), n_experts, selected_experts, i, cur);
  1253. ggml_tensor * cur_gate = ggml_mul_mat_id(ctx, ffn_gate_exp.data(), n_experts, selected_experts, i, cur);
  1254. cur_gate = ggml_silu(ctx, cur_gate);
  1255. cur_expert = ggml_mul(ctx, cur_up, cur_gate);
  1256. cur_expert = ggml_mul_mat_id(ctx, ffn_down_exp.data(), n_experts, selected_experts, i, cur_expert);
  1257. cur_expert = ggml_mul(ctx, cur_expert,
  1258. ggml_view_2d(ctx, weights, 1, n_tokens, weights->nb[1], i*weights->nb[0]));
  1259. if (i == 0) {
  1260. moe_out = cur_expert;
  1261. } else {
  1262. moe_out = ggml_add(ctx, moe_out, cur_expert);
  1263. }
  1264. }
  1265. cur = moe_out;
  1266. return cur;
  1267. }
  1268. };
  1269. enum llm_norm_type {
  1270. LLM_NORM,
  1271. LLM_NORM_RMS,
  1272. };
  1273. struct llama_hparams {
  1274. uint32_t n_vocab;
  1275. uint32_t n_embd;
  1276. uint32_t n_head;
  1277. uint32_t n_head_kv;
  1278. static constexpr uint32_t n_layer = 1;
  1279. uint32_t n_rot;
  1280. uint32_t n_embd_head; // dimension of values (d_v)
  1281. uint32_t n_ff;
  1282. float f_norm_eps;
  1283. float f_norm_rms_eps;
  1284. // cparams
  1285. static constexpr uint32_t n_ctx = 512; // user-specified context size
  1286. static constexpr uint32_t n_orig_ctx = n_ctx;
  1287. // batch
  1288. int32_t n_tokens;
  1289. // llm_build_context
  1290. static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx
  1291. static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache
  1292. uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads
  1293. return n_embd_head * n_head_kv;
  1294. }
  1295. };
  1296. // LLM base class
  1297. struct test_llm : public test_case {
  1298. llama_hparams hp;
  1299. protected:
  1300. test_llm(llama_hparams hp)
  1301. : hp(std::move(hp)) {
  1302. }
  1303. public:
  1304. struct ggml_tensor * llm_build_norm(
  1305. struct ggml_context * ctx,
  1306. struct ggml_tensor * cur,
  1307. struct ggml_tensor * mw,
  1308. struct ggml_tensor * mb,
  1309. llm_norm_type type) {
  1310. switch (type) {
  1311. case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break;
  1312. case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break;
  1313. }
  1314. cur = ggml_mul(ctx, cur, mw);
  1315. if (mb) {
  1316. cur = ggml_add(ctx, cur, mb);
  1317. }
  1318. return cur;
  1319. }
  1320. void llm_build_kv_store(
  1321. struct ggml_context * ctx,
  1322. struct ggml_tensor * k_l,
  1323. struct ggml_tensor * v_l,
  1324. struct ggml_tensor * k_cur,
  1325. struct ggml_tensor * v_cur) {
  1326. // compute the transposed [n_tokens, n_embd] V matrix
  1327. struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens));
  1328. struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(),
  1329. (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head);
  1330. struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(),
  1331. ( hp.n_ctx)*ggml_element_size(v_l),
  1332. (hp.kv_head)*ggml_element_size(v_l));
  1333. // important: storing RoPE-ed version of K in the KV cache!
  1334. ggml_cpy(ctx, k_cur, k_cache_view);
  1335. ggml_cpy(ctx, v_cur_t, v_cache_view);
  1336. }
  1337. struct ggml_tensor * llm_build_kqv(
  1338. struct ggml_context * ctx,
  1339. struct ggml_tensor * k_l,
  1340. struct ggml_tensor * v_l,
  1341. struct ggml_tensor * q_cur,
  1342. struct ggml_tensor * kq_mask,
  1343. float kq_scale) {
  1344. struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3);
  1345. struct ggml_tensor * k =
  1346. ggml_view_3d(ctx, k_l,
  1347. hp.n_embd_head, hp.n_kv, hp.n_head_kv,
  1348. ggml_row_size(k_l->type, hp.n_embd_gqa()),
  1349. ggml_row_size(k_l->type, hp.n_embd_head),
  1350. 0);
  1351. struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q);
  1352. kq = ggml_soft_max_ext(ctx, kq, kq_mask, nullptr, kq_scale, 0.0f);
  1353. // split cached v into n_head heads
  1354. struct ggml_tensor * v =
  1355. ggml_view_3d(ctx, v_l,
  1356. hp.n_kv, hp.n_embd_head, hp.n_head_kv,
  1357. ggml_element_size(v_l)*hp.n_ctx,
  1358. ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head,
  1359. 0);
  1360. struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq);
  1361. struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3);
  1362. struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens);
  1363. struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1364. cur = ggml_mul_mat(ctx, wo, cur);
  1365. return cur;
  1366. }
  1367. void initialize_tensors(ggml_context * ctx) override {
  1368. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1369. if (t->type == GGML_TYPE_I32) {
  1370. // pos
  1371. std::vector<int> data(hp.n_tokens);
  1372. for (int i = 0; i < hp.n_tokens; i++) {
  1373. data[i] = rand() % hp.n_ctx;
  1374. }
  1375. ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int));
  1376. } else {
  1377. init_tensor_uniform(t);
  1378. }
  1379. }
  1380. }
  1381. };
  1382. // Llama
  1383. struct test_llama : public test_llm {
  1384. static constexpr float freq_base = 10000.0f;
  1385. static constexpr float freq_scale = 1.0f;
  1386. static constexpr float ext_factor = 0.0f;
  1387. static constexpr float attn_factor = 1.0f;
  1388. static constexpr float beta_fast = 32.0f;
  1389. static constexpr float beta_slow = 1.0f;
  1390. std::string op_desc(ggml_tensor * t) override {
  1391. GGML_UNUSED(t);
  1392. return "LLAMA";
  1393. }
  1394. std::string vars() override {
  1395. auto n_tokens = hp.n_tokens;
  1396. return VARS_TO_STR1(n_tokens);
  1397. }
  1398. double max_nmse_err() override {
  1399. return 2e-3;
  1400. }
  1401. test_llama(int n_tokens = 1)
  1402. : test_llm({
  1403. /*n_vocab =*/ 32000,
  1404. /*n_embd =*/ 3200,
  1405. /*n_head =*/ 32,
  1406. /*n_head_kv =*/ 32,
  1407. /*n_rot =*/ 100,
  1408. /*n_embd_head =*/ 100,
  1409. /*n_ff =*/ 8640,
  1410. /*f_norm_eps =*/ 0.f,
  1411. /*f_norm_rms_eps =*/ 1e-5f,
  1412. /*n_tokens =*/ n_tokens,
  1413. }) {
  1414. }
  1415. ggml_tensor * build_graph(ggml_context * ctx) override {
  1416. struct ggml_tensor * cur;
  1417. struct ggml_tensor * inpL;
  1418. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1419. // inp_pos - contains the positions
  1420. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1421. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1422. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hp.n_kv, hp.n_tokens, 1);
  1423. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1424. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1425. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1426. struct ggml_tensor * inpSA = inpL;
  1427. // norm
  1428. ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1429. cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS);
  1430. // self-attention
  1431. {
  1432. ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1433. ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1434. ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1435. // compute Q and K and RoPE them
  1436. struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur);
  1437. struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur);
  1438. struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur);
  1439. Qcur = ggml_rope_custom(
  1440. ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos,
  1441. hp.n_rot, 0, 0, hp.n_orig_ctx, freq_base, freq_scale,
  1442. ext_factor, attn_factor, beta_fast, beta_slow
  1443. );
  1444. Kcur = ggml_rope_custom(
  1445. ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos,
  1446. hp.n_rot, 0, 0, hp.n_orig_ctx, freq_base, freq_scale,
  1447. ext_factor, attn_factor, beta_fast, beta_slow
  1448. );
  1449. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1450. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1451. }
  1452. struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA);
  1453. // feed-forward network
  1454. ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1455. cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS);
  1456. ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1457. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1458. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1459. struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur);
  1460. cur = ggml_mul_mat(ctx, ffn_gate, cur);
  1461. cur = ggml_silu(ctx, cur);
  1462. cur = ggml_mul(ctx, cur, tmp);
  1463. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1464. cur = ggml_add(ctx, cur, ffn_inp);
  1465. // input for next layer
  1466. inpL = cur;
  1467. }
  1468. cur = inpL;
  1469. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1470. cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS);
  1471. // lm_head
  1472. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab);
  1473. cur = ggml_mul_mat(ctx, output, cur);
  1474. return cur;
  1475. }
  1476. };
  1477. // Falcon
  1478. struct test_falcon : public test_llm {
  1479. static constexpr float freq_base = 10000.0f;
  1480. static constexpr float freq_scale = 1.0f;
  1481. static constexpr float ext_factor = 0.0f;
  1482. static constexpr float attn_factor = 1.0f;
  1483. static constexpr float beta_fast = 32.0f;
  1484. static constexpr float beta_slow = 1.0f;
  1485. std::string op_desc(ggml_tensor * t) override {
  1486. GGML_UNUSED(t);
  1487. return "FALCON";
  1488. }
  1489. std::string vars() override {
  1490. auto n_tokens = hp.n_tokens;
  1491. return VARS_TO_STR1(n_tokens);
  1492. }
  1493. double max_nmse_err() override {
  1494. return 2e-3;
  1495. }
  1496. test_falcon(int n_tokens = 1)
  1497. : test_llm({
  1498. /*n_vocab =*/ 32000,
  1499. /*n_embd =*/ 3200,
  1500. /*n_head =*/ 50,
  1501. /*n_head_kv =*/ 1,
  1502. /*n_rot =*/ 64,
  1503. /*n_embd_head =*/ 64,
  1504. /*n_ff =*/ 8640,
  1505. /*f_norm_eps =*/ 1e-5f,
  1506. /*f_norm_rms_eps =*/ 0.f,
  1507. /*n_tokens =*/ n_tokens,
  1508. }) {
  1509. }
  1510. ggml_tensor * build_graph(ggml_context * ctx) override {
  1511. struct ggml_tensor * cur;
  1512. struct ggml_tensor * inpL;
  1513. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1514. // inp_pos - contains the positions
  1515. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1516. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1517. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, hp.n_kv, hp.n_tokens, 1);
  1518. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1519. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1520. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1521. // norm
  1522. ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1523. ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1524. ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM);
  1525. // self-attention
  1526. {
  1527. cur = attn_norm;
  1528. ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa());
  1529. cur = ggml_mul_mat(ctx, wqkv, cur);
  1530. struct ggml_tensor * Qcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd, hp.n_tokens, cur->nb[1], 0*sizeof(float)*(hp.n_embd)));
  1531. struct ggml_tensor * Kcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd_gqa(), hp.n_tokens, cur->nb[1], 1*sizeof(float)*(hp.n_embd)));
  1532. struct ggml_tensor * Vcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd_gqa(), hp.n_tokens, cur->nb[1], 1*sizeof(float)*(hp.n_embd + hp.n_embd_gqa())));
  1533. Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens);
  1534. Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens);
  1535. // using mode = 2 for neox mode
  1536. Qcur = ggml_rope_custom(
  1537. ctx, Qcur, inp_pos, hp.n_rot, 2, 0, hp.n_orig_ctx,
  1538. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1539. );
  1540. Kcur = ggml_rope_custom(
  1541. ctx, Kcur, inp_pos, hp.n_rot, 2, 0, hp.n_orig_ctx,
  1542. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1543. );
  1544. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1545. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1546. }
  1547. struct ggml_tensor * ffn_inp = cur;
  1548. // feed forward
  1549. {
  1550. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1551. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1552. cur = attn_norm;
  1553. cur = ggml_mul_mat(ctx, ffn_up, cur);
  1554. cur = ggml_gelu(ctx, cur);
  1555. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1556. }
  1557. cur = ggml_add(ctx, cur, ffn_inp);
  1558. cur = ggml_add(ctx, cur, inpL);
  1559. // input for next layer
  1560. inpL = cur;
  1561. }
  1562. cur = inpL;
  1563. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1564. ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1565. cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM);
  1566. // lm_head
  1567. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab);
  1568. cur = ggml_mul_mat(ctx, output, cur);
  1569. return cur;
  1570. }
  1571. };
  1572. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  1573. std::vector<std::unique_ptr<test_case>> test_cases;
  1574. std::default_random_engine rng(0);
  1575. const ggml_type all_types[] = {
  1576. GGML_TYPE_F32, GGML_TYPE_F16,
  1577. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  1578. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1579. GGML_TYPE_Q8_0,
  1580. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1581. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  1582. GGML_TYPE_Q6_K,
  1583. GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS,
  1584. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S,
  1585. GGML_TYPE_IQ4_NL,
  1586. };
  1587. // unary ops
  1588. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  1589. test_cases.emplace_back(new test_unary((ggml_unary_op) op));
  1590. }
  1591. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  1592. for (ggml_type type : all_types) {
  1593. for (int b : {1, 7}) {
  1594. for (bool v : {false, true}) {
  1595. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  1596. }
  1597. }
  1598. }
  1599. for (int b : {1, 7}) {
  1600. for (bool v : {false, true}) {
  1601. test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, v));
  1602. }
  1603. }
  1604. for (ggml_type type_input : {GGML_TYPE_F32}) {
  1605. for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) {
  1606. for (int k0 : {1, 3}) {
  1607. for (int k1 : {1, 3}) {
  1608. for (int s0 : {1, 2}) {
  1609. for (int s1 : {1, 2}) {
  1610. for (int p0 : {0, 1}) {
  1611. for (int p1 : {0, 1}) {
  1612. test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1));
  1613. }
  1614. }
  1615. }
  1616. }
  1617. }
  1618. }
  1619. }
  1620. }
  1621. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32));
  1622. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16));
  1623. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 1}));
  1624. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1625. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 2, 1, 1}));
  1626. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 2, 1}));
  1627. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1628. test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1629. test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1630. test_cases.emplace_back(new test_dup(GGML_TYPE_F32));
  1631. test_cases.emplace_back(new test_dup(GGML_TYPE_F16));
  1632. test_cases.emplace_back(new test_dup(GGML_TYPE_I32));
  1633. test_cases.emplace_back(new test_dup(GGML_TYPE_I16));
  1634. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3}));
  1635. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3}));
  1636. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1637. for (ggml_type type_dst : all_types) {
  1638. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4}));
  1639. }
  1640. }
  1641. test_cases.emplace_back(new test_cont());
  1642. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  1643. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  1644. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  1645. }
  1646. };
  1647. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  1648. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  1649. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  1650. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 1, 1}, {1, 1, 1, 1});
  1651. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 1}, {1, 1, 1, 1});
  1652. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 1});
  1653. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 1, 1, 1});
  1654. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 1, 1});
  1655. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 1});
  1656. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 2});
  1657. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 2});
  1658. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 2, 2});
  1659. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 2, 2, 2});
  1660. // stable diffusion
  1661. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  1662. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  1663. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  1664. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  1665. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  1666. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  1667. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  1668. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  1669. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  1670. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  1671. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  1672. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  1673. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  1674. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  1675. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  1676. test_cases.emplace_back(new test_scale());
  1677. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  1678. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1679. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1680. }
  1681. for (ggml_type type_a : all_types) {
  1682. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1683. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1684. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  1685. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  1686. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  1687. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  1688. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  1689. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  1690. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  1691. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  1692. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  1693. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  1694. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  1695. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  1696. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  1697. }
  1698. }
  1699. for (ggml_type type_a : all_types) {
  1700. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1701. for (int n_mats : {2, 4, 8}) {
  1702. for (int id = 0; id < n_mats; id++) {
  1703. for (bool v : {false, true}) {
  1704. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, id, 16, 16, 256, v));
  1705. }
  1706. }
  1707. }
  1708. }
  1709. }
  1710. test_cases.emplace_back(new test_sqr());
  1711. test_cases.emplace_back(new test_clamp());
  1712. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  1713. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 1}, 5));
  1714. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 10}, 5));
  1715. #if 0
  1716. std::uniform_int_distribution<> dist_ne1(1, 50);
  1717. int exponent = 1;
  1718. while (exponent < (1 << 17)) {
  1719. std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent);
  1720. for (int n = 0; n < 10; ++n) {
  1721. int64_t ne0 = dist_ne0(rng);
  1722. int64_t ne1 = dist_ne1(rng);
  1723. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, n/2 == 0, 0.1f, ne0 < 1000 ? 4.0f : 0.0f));
  1724. }
  1725. exponent <<= 1;
  1726. }
  1727. #endif
  1728. for (bool mask : {false, true}) {
  1729. for (float max_bias : {0.0f, 8.0f}) {
  1730. for (float scale : {1.0f, 0.1f}) {
  1731. for (int64_t ne0 : {16, 1024}) {
  1732. for (int64_t ne1 : {16, 1024}) {
  1733. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, scale, max_bias));
  1734. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, scale, max_bias));
  1735. }
  1736. }
  1737. }
  1738. }
  1739. }
  1740. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 0.0f));
  1741. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 0.0f));
  1742. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 8.0f));
  1743. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 8.0f));
  1744. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1745. test_cases.emplace_back(new test_rope(type, {128, 32, 10, 1}, 128, 0, 512)); // llama 7B
  1746. test_cases.emplace_back(new test_rope(type, {128, 40, 10, 1}, 128, 0, 512)); // llama 13B
  1747. test_cases.emplace_back(new test_rope(type, {128, 52, 10, 1}, 128, 0, 512)); // llama 30B
  1748. test_cases.emplace_back(new test_rope(type, {128, 64, 10, 1}, 128, 0, 512)); // llama 65B
  1749. test_cases.emplace_back(new test_rope(type, { 64, 1, 10, 1}, 64, 2, 512)); // neox (falcon 7B)
  1750. test_cases.emplace_back(new test_rope(type, { 64, 71, 10, 1}, 64, 2, 512)); // neox (falcon 7B)
  1751. test_cases.emplace_back(new test_rope(type, { 64, 8, 10, 1}, 64, 2, 512)); // neox (falcon 40B)
  1752. test_cases.emplace_back(new test_rope(type, { 64, 128, 10, 1}, 64, 2, 512)); // neox (falcon 40B)
  1753. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 20, 2, 512)); // neox (stablelm)
  1754. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 32, 2, 512)); // neox (phi-2)
  1755. }
  1756. test_cases.emplace_back(new test_concat(GGML_TYPE_F32));
  1757. test_cases.emplace_back(new test_concat(GGML_TYPE_I32));
  1758. for (ggml_sort_order order : {GGML_SORT_ASC, GGML_SORT_DESC}) {
  1759. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  1760. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  1761. }
  1762. test_cases.emplace_back(new test_sum_rows());
  1763. test_cases.emplace_back(new test_upscale());
  1764. test_cases.emplace_back(new test_group_norm());
  1765. test_cases.emplace_back(new test_acc());
  1766. test_cases.emplace_back(new test_pad());
  1767. test_cases.emplace_back(new test_leaky_relu());
  1768. // these tests are disabled to save execution time, but they can be handy for debugging
  1769. #if 0
  1770. #if !defined(__SANITIZE_THREAD__)
  1771. // FIXME: these tests use too much memory with thread sanitizer
  1772. test_cases.emplace_back(new test_moe(8, 2, 1, 4096, 8*1024));
  1773. //test_cases.emplace_back(new test_moe(8, 2, 8, 4096, 14336));
  1774. #endif
  1775. test_cases.emplace_back(new test_llama(1));
  1776. test_cases.emplace_back(new test_llama(2));
  1777. test_cases.emplace_back(new test_falcon(1));
  1778. test_cases.emplace_back(new test_falcon(2));
  1779. #endif
  1780. // run tests
  1781. if (mode == MODE_TEST) {
  1782. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  1783. size_t n_ok = 0;
  1784. for (auto & test : test_cases) {
  1785. if (test->eval(backend, backend_cpu, op_name)) {
  1786. n_ok++;
  1787. }
  1788. }
  1789. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  1790. ggml_backend_free(backend_cpu);
  1791. return n_ok == test_cases.size();
  1792. }
  1793. if (mode == MODE_PERF) {
  1794. for (auto & test : test_cases) {
  1795. test->eval_perf(backend, op_name);
  1796. }
  1797. return true;
  1798. }
  1799. GGML_ASSERT(false);
  1800. return false;
  1801. }
  1802. static void usage(char ** argv) {
  1803. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  1804. printf(" valid modes are: test (compare with CPU backend for correctness) or perf (performance evaluation)\n");
  1805. printf(" op names are as given by ggml_op_desc()\n");
  1806. }
  1807. int main(int argc, char ** argv) {
  1808. test_mode mode = MODE_TEST;
  1809. const char * op_name = NULL;
  1810. const char * backend = NULL;
  1811. for (int i = 1; i < argc; i++) {
  1812. if (strcmp(argv[i], "test") == 0) {
  1813. mode = MODE_TEST;
  1814. } else if (strcmp(argv[i], "perf") == 0) {
  1815. mode = MODE_PERF;
  1816. } else if (strcmp(argv[i], "-o") == 0) {
  1817. if (i + 1 < argc) {
  1818. op_name = argv[++i];
  1819. } else {
  1820. usage(argv);
  1821. return 1;
  1822. }
  1823. } else if (strcmp(argv[i], "-b") == 0) {
  1824. if (i + 1 < argc) {
  1825. backend = argv[++i];
  1826. } else {
  1827. usage(argv);
  1828. return 1;
  1829. }
  1830. } else {
  1831. usage(argv);
  1832. return 1;
  1833. }
  1834. }
  1835. // enumerate backends
  1836. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  1837. size_t n_ok = 0;
  1838. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  1839. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  1840. if (backend != NULL && strcmp(backend, ggml_backend_reg_get_name(i)) != 0) {
  1841. printf(" Skipping\n");
  1842. n_ok++;
  1843. continue;
  1844. }
  1845. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  1846. GGML_ASSERT(backend != NULL);
  1847. printf(" Backend name: %s\n", ggml_backend_name(backend));
  1848. bool ok = test_backend(backend, mode, op_name);
  1849. printf(" Backend %s: ", ggml_backend_name(backend));
  1850. if (ok) {
  1851. printf("\033[1;32mOK\033[0m\n");
  1852. n_ok++;
  1853. } else {
  1854. printf("\033[1;31mFAIL\033[0m\n");
  1855. }
  1856. printf("\n");
  1857. ggml_backend_free(backend);
  1858. }
  1859. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  1860. if (n_ok != ggml_backend_reg_get_count()) {
  1861. printf("\033[1;31mFAIL\033[0m\n");
  1862. return 1;
  1863. }
  1864. ggml_quantize_free();
  1865. printf("\033[1;32mOK\033[0m\n");
  1866. return 0;
  1867. }