test-backend-ops.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  1. #include <ggml.h>
  2. #include <ggml-alloc.h>
  3. #include <ggml-backend.h>
  4. #include <algorithm>
  5. #include <array>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <functional>
  9. #include <memory>
  10. #include <random>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string>
  14. #include <thread>
  15. #include <vector>
  16. static void init_tensor_uniform(ggml_tensor * tensor, float min = -1.0f, float max = 1.0f) {
  17. // static RNG initialization (revisit if n_threads stops being constant)
  18. static const size_t n_threads = std::thread::hardware_concurrency();
  19. static std::vector<std::default_random_engine> generators = []() {
  20. std::random_device rd;
  21. std::vector<std::default_random_engine> vec;
  22. vec.reserve(n_threads);
  23. //for (size_t i = 0; i < n_threads; i++) { vec.emplace_back(1234 + i); } // fixed seed
  24. for (size_t i = 0; i < n_threads; i++) { vec.emplace_back(rd()); }
  25. return vec;
  26. }();
  27. size_t size = ggml_nelements(tensor);
  28. std::vector<float> data(size);
  29. auto init_thread = [&](size_t ith, size_t start, size_t end) {
  30. std::uniform_real_distribution<float> distribution(min, max);
  31. for (size_t i = start; i < end; i++) {
  32. data[i] = distribution(generators[ith]);
  33. }
  34. };
  35. std::vector<std::thread> threads;
  36. threads.reserve(n_threads);
  37. for (size_t i = 0; i < n_threads; i++) {
  38. size_t start = i*size/n_threads;
  39. size_t end = (i+1)*size/n_threads;
  40. threads.emplace_back(init_thread, i, start, end);
  41. }
  42. for (auto & t : threads) {
  43. t.join();
  44. }
  45. #if 0
  46. const char * val_str = getenv("GGML_TEST_EPS");
  47. float val = 1e-9f;
  48. if (val_str != nullptr) {
  49. val = std::stof(val_str);
  50. printf("GGML_TEST_EPS=%e\n", val);
  51. }
  52. // test quantization with very small values that may result in nan scales due to division by zero
  53. if (ggml_is_quantized(tensor->type)) {
  54. for (int i = 0; i < 256; i++) {
  55. data[i] = val;
  56. }
  57. }
  58. #endif
  59. if (tensor->type == GGML_TYPE_F32 || tensor->type == GGML_TYPE_I32) {
  60. ggml_backend_tensor_set(tensor, data.data(), 0, size * sizeof(float));
  61. } else if (ggml_is_quantized(tensor->type) || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_BF16) {
  62. GGML_ASSERT(size % ggml_blck_size(tensor->type) == 0);
  63. std::vector<uint8_t> dataq(ggml_row_size(tensor->type, size));
  64. std::vector<float> imatrix(tensor->ne[0], 1.0f); // dummy importance matrix
  65. const float * im = imatrix.data();
  66. if (!ggml_quantize_requires_imatrix(tensor->type)) {
  67. // when the imatrix is optional, we want to test both quantization with and without imatrix
  68. // use one of the random numbers to decide
  69. if (data[0] > 0.5f*(min + max)) {
  70. im = nullptr;
  71. }
  72. }
  73. ggml_quantize_chunk(tensor->type, data.data(), dataq.data(), 0, size/tensor->ne[0], tensor->ne[0], im);
  74. GGML_ASSERT(ggml_validate_row_data(tensor->type, dataq.data(), dataq.size()));
  75. // TODO: other cases
  76. //#pragma omp parallel for
  77. //for (int i = 0; i < tensor->ne[1]; i++) {
  78. // ggml_quantize_chunk(tensor->type, data.data(), dataq.data(),
  79. // i * tensor->ne[0], 1, tensor->ne[0], im);
  80. //}
  81. ggml_backend_tensor_set(tensor, dataq.data(), 0, dataq.size());
  82. } else if (tensor->type == GGML_TYPE_I8 || tensor->type == GGML_TYPE_I16 || tensor->type == GGML_TYPE_I32) {
  83. // This is going to create some weird integers though.
  84. ggml_backend_tensor_set(tensor, data.data(), 0, ggml_nbytes(tensor));
  85. } else {
  86. GGML_ABORT("fatal error");
  87. }
  88. }
  89. static std::vector<float> tensor_to_float(const ggml_tensor * t) {
  90. std::vector<float> tv;
  91. tv.reserve(ggml_nelements(t));
  92. std::vector<uint8_t> buf(ggml_nbytes(t));
  93. ggml_backend_tensor_get(t, buf.data(), 0, ggml_nbytes(t));
  94. ggml_type_traits_t tt = ggml_internal_get_type_traits(t->type);
  95. size_t bs = ggml_blck_size(t->type);
  96. std::vector<float> vq(ggml_blck_size(t->type));
  97. bool quantized = ggml_is_quantized(t->type);
  98. // access elements by index to avoid gaps in views
  99. for (int64_t i3 = 0; i3 < t->ne[3]; i3++) {
  100. for (int64_t i2 = 0; i2 < t->ne[2]; i2++) {
  101. for (int64_t i1 = 0; i1 < t->ne[1]; i1++) {
  102. for (int64_t i0 = 0; i0 < t->ne[0]; i0 += bs) {
  103. size_t i = i3*t->nb[3] + i2*t->nb[2] + i1*t->nb[1] + i0/bs*t->nb[0];
  104. if (t->type == GGML_TYPE_F16) {
  105. tv.push_back(ggml_fp16_to_fp32(*(ggml_fp16_t*)&buf[i]));
  106. } else if (t->type == GGML_TYPE_BF16) {
  107. tv.push_back(ggml_bf16_to_fp32(*(ggml_bf16_t*)&buf[i]));
  108. } else if (t->type == GGML_TYPE_F32) {
  109. tv.push_back(*(float *) &buf[i]);
  110. } else if (t->type == GGML_TYPE_I32) {
  111. tv.push_back((float)*(int32_t *) &buf[i]);
  112. } else if (t->type == GGML_TYPE_I16) {
  113. tv.push_back((float)*(int16_t *) &buf[i]);
  114. } else if (t->type == GGML_TYPE_I8) {
  115. tv.push_back((float)*(int8_t *) &buf[i]);
  116. } else if (quantized) {
  117. tt.to_float(&buf[i], vq.data(), bs);
  118. tv.insert(tv.end(), vq.begin(), vq.end());
  119. } else {
  120. GGML_ABORT("fatal error");
  121. }
  122. }
  123. }
  124. }
  125. }
  126. return tv;
  127. }
  128. /*
  129. static double cosine_similarity(const float * v1, const float * v2, size_t n) {
  130. double dot = 0.0;
  131. double mag1 = 0.0;
  132. double mag2 = 0.0;
  133. for (size_t i = 0; i < n; i++) {
  134. if (std::isnan(v1[i]) || std::isnan(v2[i])) {
  135. return -1.0f;
  136. }
  137. if (std::isinf(v1[i]) && std::isinf(v2[i])) {
  138. continue;
  139. }
  140. dot += v1[i]*v2[i];
  141. mag1 += v1[i]*v1[i];
  142. mag2 += v2[i]*v2[i];
  143. }
  144. return dot/sqrt(mag1*mag2);
  145. }
  146. static float distance(const float * v1, const float * v2, size_t n) {
  147. double d = 0.0;
  148. for (size_t i = 0; i < n; i++) {
  149. if (std::isnan(v1[i]) || std::isnan(v2[i])) {
  150. return INFINITY;
  151. }
  152. if (std::isinf(v1[i]) && std::isinf(v2[i])) {
  153. continue;
  154. }
  155. d += (v1[i] - v2[i])*(v1[i] - v2[i]);
  156. }
  157. return sqrt(d);
  158. }
  159. static float vec_len(const float * v, size_t n) {
  160. double d = 0.0;
  161. for (size_t i = 0; i < n; i++) {
  162. if (std::isnan(v[i])) {
  163. return INFINITY;
  164. }
  165. if (std::isinf(v[i])) {
  166. continue;
  167. }
  168. d += v[i]*v[i];
  169. }
  170. return sqrt(d);
  171. }
  172. */
  173. // normalized mean squared error = mse(a, b) / mse(a, 0)
  174. static double nmse(const float * a, const float * b, size_t n) {
  175. double mse_a_b = 0.0;
  176. double mse_a_0 = 0.0;
  177. for (size_t i = 0; i < n; i++) {
  178. float a_i = a[i];
  179. float b_i = b[i];
  180. mse_a_b += (a_i - b_i) * (a_i - b_i);
  181. mse_a_0 += a_i * a_i;
  182. }
  183. return mse_a_b / mse_a_0;
  184. }
  185. // utils for printing the variables of the test cases
  186. #define VAR_TO_STR(x) (#x "=" + var_to_str(x))
  187. template<typename T>
  188. static std::string var_to_str(const T & x) {
  189. return std::to_string(x);
  190. }
  191. template<typename T, size_t N>
  192. static std::string var_to_str(const T (&x)[N]) {
  193. std::string s = "[";
  194. for (size_t i = 0; i < N; i++) {
  195. if (i > 0) {
  196. s += ",";
  197. }
  198. s += var_to_str(x[i]);
  199. }
  200. s += "]";
  201. return s;
  202. }
  203. template<typename T, size_t N>
  204. static std::string var_to_str(const std::array<T, N> & x) {
  205. std::string s = "[";
  206. for (size_t i = 0; i < N; i++) {
  207. if (i > 0) {
  208. s += ",";
  209. }
  210. s += var_to_str(x[i]);
  211. }
  212. s += "]";
  213. return s;
  214. }
  215. //static std::string var_to_str(ggml_unary_op unary_op) {
  216. // return ggml_unary_op_name(unary_op);
  217. //}
  218. static std::string var_to_str(ggml_type type) {
  219. return ggml_type_name(type);
  220. }
  221. static std::string var_to_str(ggml_op_pool pool) {
  222. switch (pool) {
  223. case GGML_OP_POOL_AVG: return "avg";
  224. case GGML_OP_POOL_MAX: return "max";
  225. default: return std::to_string(pool);
  226. }
  227. }
  228. #define VARS_TO_STR1(a) VAR_TO_STR(a)
  229. #define VARS_TO_STR2(a, b) VAR_TO_STR(a) + "," + VAR_TO_STR(b)
  230. #define VARS_TO_STR3(a, b, c) VAR_TO_STR(a) + "," + VARS_TO_STR2(b, c)
  231. #define VARS_TO_STR4(a, b, c, d) VAR_TO_STR(a) + "," + VARS_TO_STR3(b, c, d)
  232. #define VARS_TO_STR5(a, b, c, d, e) VAR_TO_STR(a) + "," + VARS_TO_STR4(b, c, d, e)
  233. #define VARS_TO_STR6(a, b, c, d, e, f) VAR_TO_STR(a) + "," + VARS_TO_STR5(b, c, d, e, f)
  234. #define VARS_TO_STR7(a, b, c, d, e, f, g) VAR_TO_STR(a) + "," + VARS_TO_STR6(b, c, d, e, f, g)
  235. #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)
  236. #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)
  237. #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)
  238. #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)
  239. #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)
  240. #ifdef GGML_USE_SYCL
  241. static bool inline _isinf(float f) {
  242. return (*(uint32_t *)&f & 0x7fffffff) == 0x7f800000;
  243. }
  244. #else
  245. static bool inline _isinf(float f) { return std::isinf(f); }
  246. #endif
  247. // accept FLT_MAX as infinity
  248. static bool isinf_or_max(float f) {
  249. return _isinf(f) || f == FLT_MAX || f == -FLT_MAX;
  250. }
  251. static bool ggml_is_view_op(enum ggml_op op) {
  252. return op == GGML_OP_VIEW || op == GGML_OP_RESHAPE || op == GGML_OP_PERMUTE || op == GGML_OP_TRANSPOSE;
  253. }
  254. enum test_mode {
  255. MODE_TEST,
  256. MODE_PERF,
  257. };
  258. struct test_case {
  259. virtual ~test_case() {}
  260. virtual std::string op_desc(ggml_tensor * t) {
  261. return ggml_op_desc(t);
  262. }
  263. virtual std::string vars() {
  264. return "";
  265. }
  266. virtual ggml_tensor * build_graph(ggml_context * ctx) = 0;
  267. virtual double max_nmse_err() {
  268. return 1e-7;
  269. }
  270. virtual void initialize_tensors(ggml_context * ctx) {
  271. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
  272. init_tensor_uniform(t);
  273. }
  274. }
  275. virtual size_t op_size(ggml_tensor * t) {
  276. size_t size = ggml_nbytes(t);
  277. // add source tensors
  278. for (int i = 0; i < GGML_MAX_SRC; i++) {
  279. if (t->src[i] != NULL) {
  280. size += ggml_nbytes(t->src[i]);
  281. }
  282. }
  283. return size;
  284. }
  285. ggml_cgraph * gf = nullptr;
  286. static const int sentinel_size = 1024;
  287. test_mode mode;
  288. std::vector<ggml_tensor *> sentinels;
  289. void add_sentinel(ggml_context * ctx) {
  290. if (mode == MODE_PERF) {
  291. return;
  292. }
  293. ggml_tensor * sentinel = ::ggml_new_tensor_1d(ctx, GGML_TYPE_F32, sentinel_size);
  294. ggml_format_name(sentinel, "sent_%zu", sentinels.size());
  295. sentinels.push_back(sentinel);
  296. }
  297. // hijack ggml_new_tensor to add sentinels after each tensor to check for overflows in the backend
  298. ggml_tensor * ggml_new_tensor(ggml_context * ctx, ggml_type type, int n_dims, const int64_t * ne) {
  299. ggml_tensor * t = ::ggml_new_tensor(ctx, type, n_dims, ne);
  300. add_sentinel(ctx);
  301. return t;
  302. }
  303. ggml_tensor * ggml_new_tensor_1d(ggml_context * ctx, ggml_type type, int64_t ne0) {
  304. ggml_tensor * t = ::ggml_new_tensor_1d(ctx, type, ne0);
  305. add_sentinel(ctx);
  306. return t;
  307. }
  308. ggml_tensor * ggml_new_tensor_2d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1) {
  309. ggml_tensor * t = ::ggml_new_tensor_2d(ctx, type, ne0, ne1);
  310. add_sentinel(ctx);
  311. return t;
  312. }
  313. ggml_tensor * ggml_new_tensor_3d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2) {
  314. ggml_tensor * t = ::ggml_new_tensor_3d(ctx, type, ne0, ne1, ne2);
  315. add_sentinel(ctx);
  316. return t;
  317. }
  318. ggml_tensor * ggml_new_tensor_4d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3) {
  319. ggml_tensor * t = ::ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3);
  320. add_sentinel(ctx);
  321. return t;
  322. }
  323. bool eval(ggml_backend_t backend1, ggml_backend_t backend2, const char * op_name) {
  324. mode = MODE_TEST;
  325. ggml_init_params params = {
  326. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead(),
  327. /* .mem_base = */ NULL,
  328. /* .no_alloc = */ true,
  329. };
  330. ggml_context * ctx = ggml_init(params);
  331. gf = ggml_new_graph(ctx);
  332. // pre-graph sentinel
  333. add_sentinel(ctx);
  334. ggml_tensor * out = build_graph(ctx);
  335. if (op_name != nullptr && op_desc(out) != op_name) {
  336. //printf(" %s: skipping\n", op_desc(out).c_str());
  337. ggml_free(ctx);
  338. return true;
  339. }
  340. printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  341. fflush(stdout);
  342. // check if the backends support the ops
  343. bool supported = true;
  344. for (ggml_backend_t backend : {backend1, backend2}) {
  345. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  346. if (!ggml_backend_supports_op(backend, t)) {
  347. printf("not supported [%s] ", ggml_backend_name(backend));
  348. supported = false;
  349. break;
  350. }
  351. }
  352. }
  353. if (!supported) {
  354. printf("\n");
  355. ggml_free(ctx);
  356. return true;
  357. }
  358. // post-graph sentinel
  359. add_sentinel(ctx);
  360. // allocate
  361. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend1);
  362. if (buf == NULL) {
  363. printf("failed to allocate tensors [%s] ", ggml_backend_name(backend1));
  364. ggml_free(ctx);
  365. return false;
  366. }
  367. // build graph
  368. ggml_build_forward_expand(gf, out);
  369. // add sentinels as graph nodes so that they are checked in the callback
  370. for (ggml_tensor * sentinel : sentinels) {
  371. gf->nodes[gf->n_nodes++] = sentinel;
  372. }
  373. // randomize tensors
  374. initialize_tensors(ctx);
  375. // compare
  376. struct callback_userdata {
  377. bool ok;
  378. double max_err;
  379. ggml_backend_t backend1;
  380. ggml_backend_t backend2;
  381. };
  382. callback_userdata ud {
  383. true,
  384. max_nmse_err(),
  385. backend1,
  386. backend2
  387. };
  388. auto callback = [](int index, ggml_tensor * t1, ggml_tensor * t2, void * user_data) -> bool {
  389. callback_userdata * ud = (callback_userdata *) user_data;
  390. const char * bn1 = ggml_backend_name(ud->backend1);
  391. const char * bn2 = ggml_backend_name(ud->backend2);
  392. if (t1->op == GGML_OP_NONE) {
  393. // sentinels must be unchanged
  394. std::vector<uint8_t> t1_data(ggml_nbytes(t1));
  395. std::vector<uint8_t> t2_data(ggml_nbytes(t2));
  396. ggml_backend_tensor_get(t1, t1_data.data(), 0, ggml_nbytes(t1));
  397. ggml_backend_tensor_get(t2, t2_data.data(), 0, ggml_nbytes(t2));
  398. if (memcmp(t1_data.data(), t2_data.data(), ggml_nbytes(t1)) != 0) {
  399. printf("sentinel mismatch: %s ", t1->name);
  400. ud->ok = false;
  401. return true;
  402. }
  403. }
  404. std::vector<float> f1 = tensor_to_float(t1);
  405. std::vector<float> f2 = tensor_to_float(t2);
  406. for (size_t i = 0; i < f1.size(); i++) {
  407. // check for nans
  408. if (std::isnan(f1[i]) || std::isnan(f2[i])) {
  409. printf("[%s] NaN at index %zu (%s=%f %s=%f) ", ggml_op_desc(t1), i, bn1, f1[i], bn2, f2[i]);
  410. ud->ok = false;
  411. return true;
  412. }
  413. // check for infs: both must be inf of the same sign, or both must be finite
  414. if (isinf_or_max(f1[i]) || isinf_or_max(f2[i])) {
  415. if (isinf_or_max(f1[i]) && isinf_or_max(f2[i])) {
  416. if (std::signbit(f1[i]) != std::signbit(f2[i])) {
  417. printf("[%s] inf sign mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]);
  418. ud->ok = false;
  419. return true;
  420. }
  421. } else {
  422. printf("[%s] inf mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]);
  423. ud->ok = false;
  424. return true;
  425. }
  426. }
  427. }
  428. double err = nmse(f1.data(), f2.data(), f1.size());
  429. if (err > ud->max_err) {
  430. printf("[%s] NMSE = %.9f > %.9f ", ggml_op_desc(t1), err, ud->max_err);
  431. //for (int i = 0; i < (int) f1.size(); i++) {
  432. // printf("%5d %9.6f %9.6f, diff = %9.6f\n", i, f1[i], f2[i], f1[i] - f2[i]);
  433. //}
  434. //printf("\n");
  435. //exit(1);
  436. ud->ok = false;
  437. }
  438. return true;
  439. GGML_UNUSED(index);
  440. };
  441. const bool cmp_ok = ggml_backend_compare_graph_backend(backend1, backend2, gf, callback, &ud);
  442. if (!cmp_ok) {
  443. printf("compare failed ");
  444. }
  445. ggml_backend_buffer_free(buf);
  446. ggml_free(ctx);
  447. if (ud.ok && cmp_ok) {
  448. printf("\033[1;32mOK\033[0m\n");
  449. return true;
  450. }
  451. printf("\033[1;31mFAIL\033[0m\n");
  452. return false;
  453. }
  454. bool eval_perf(ggml_backend_t backend, const char * op_name) {
  455. mode = MODE_PERF;
  456. static const size_t graph_nodes = 8192;
  457. ggml_init_params params = {
  458. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead_custom(graph_nodes, false),
  459. /* .mem_base = */ NULL,
  460. /* .no_alloc = */ true,
  461. };
  462. ggml_context * ctx = ggml_init(params);
  463. ggml_tensor * out = build_graph(ctx);
  464. if (op_name != nullptr && op_desc(out) != op_name) {
  465. //printf(" %s: skipping\n", op_desc(out).c_str());
  466. ggml_free(ctx);
  467. return true;
  468. }
  469. int len = printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  470. fflush(stdout);
  471. // check if backends support op
  472. if (!ggml_backend_supports_op(backend, out)) {
  473. printf("not supported\n");
  474. ggml_free(ctx);
  475. return true;
  476. }
  477. // align while also leaving some margin for variations in parameters
  478. int align = 20;
  479. int last = (len + align - 1) / align * align;
  480. if (last - len < 5) {
  481. last += align;
  482. }
  483. last = std::max(last, 60);
  484. printf("%*s", last - len, "");
  485. // allocate
  486. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend);
  487. if (buf == NULL) {
  488. printf("failed to allocate tensors\n");
  489. ggml_free(ctx);
  490. return false;
  491. }
  492. // randomize tensors
  493. initialize_tensors(ctx);
  494. // build graph
  495. ggml_cgraph * gf = ggml_new_graph_custom(ctx, graph_nodes, false);
  496. ggml_build_forward_expand(gf, out);
  497. // warmup run
  498. ggml_backend_graph_compute(backend, gf);
  499. // duplicate the op
  500. size_t target_size = ggml_backend_is_cpu(backend) ? 1ULL << 33 : 1ULL << 35; // 8 GB CPU, 32 GB GPU
  501. int n_runs = std::min((size_t)gf->size - gf->n_nodes, target_size / op_size(out)) + 1;
  502. for (int i = 1; i < n_runs; i++) {
  503. gf->nodes[gf->n_nodes++] = out;
  504. }
  505. // calculate memory
  506. size_t mem = n_runs * op_size(out);
  507. auto tensor_op_size = [](ggml_tensor * t) {
  508. size_t size = ggml_nbytes(t);
  509. // add source tensors
  510. for (int i = 0; i < GGML_MAX_SRC; i++) {
  511. if (t->src[i] != NULL) {
  512. size += ggml_nbytes(t->src[i]);
  513. }
  514. }
  515. return size;
  516. };
  517. for (int i = 0; i < gf->n_nodes; i++) {
  518. if (ggml_is_view_op(gf->nodes[i]->op) || gf->nodes[i] == out) {
  519. continue;
  520. }
  521. mem += tensor_op_size(gf->nodes[i]);
  522. }
  523. // run
  524. ggml_backend_synchronize(backend);
  525. int64_t start_time = ggml_time_us();
  526. ggml_backend_graph_compute(backend, gf);
  527. ggml_backend_synchronize(backend);
  528. int64_t end_time = ggml_time_us();
  529. double time_us = end_time - start_time;
  530. printf(" %5d runs - %8.2f us/run - %8zu kB/run - \033[1;34m%7.2f GB/s\033[0m\n",
  531. n_runs,
  532. time_us / n_runs,
  533. op_size(out) / 1024,
  534. mem / (time_us/1e6) / 1024.0 / 1024.0 / 1024.0);
  535. ggml_backend_buffer_free(buf);
  536. ggml_free(ctx);
  537. return true;
  538. }
  539. };
  540. // GGML_OP_UNARY
  541. struct test_unary : public test_case {
  542. const ggml_unary_op op;
  543. const ggml_type type;
  544. const std::array<int64_t, 4> ne_a;
  545. int v; // view (1 : non-contiguous a)
  546. std::string vars() override {
  547. return VARS_TO_STR3(type, ne_a, v);
  548. }
  549. test_unary(ggml_unary_op op,
  550. ggml_type type = GGML_TYPE_F32,
  551. std::array<int64_t, 4> ne_a = {128, 10, 10, 10},
  552. int v = 0)
  553. : op(op), type(type), ne_a(ne_a), v(v) {}
  554. ggml_tensor * build_graph(ggml_context * ctx) override {
  555. ggml_tensor * a;
  556. if (v & 1) {
  557. auto ne = ne_a; ne[0] *= 3;
  558. a = ggml_new_tensor(ctx, type, 4, ne.data());
  559. a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
  560. } else {
  561. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  562. }
  563. ggml_tensor * out = ggml_unary(ctx, a, op);
  564. return out;
  565. }
  566. void initialize_tensors(ggml_context * ctx) override {
  567. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  568. // test extended range of values to check for NaNs in GELU
  569. init_tensor_uniform(t, -150.f, 150.f);
  570. }
  571. }
  572. };
  573. // GGML_OP_GET_ROWS
  574. struct test_get_rows : public test_case {
  575. const ggml_type type;
  576. const int n; // cols
  577. const int m; // rows
  578. const int r; // rows to get
  579. const int b; // batch size
  580. const bool v; // view (non-contiguous src1)
  581. std::string vars() override {
  582. return VARS_TO_STR6(type, n, m, r, b, v);
  583. }
  584. test_get_rows(ggml_type type = GGML_TYPE_F32, int n = 10, int m = 5, int r = 3, int b = 1, bool v = false)
  585. : type(type), n(n), m(m), r(r), b(b), v(v) {}
  586. ggml_tensor * build_graph(ggml_context * ctx) override {
  587. ggml_tensor * in = ggml_new_tensor_3d(ctx, type, n, m, b);
  588. ggml_tensor * rows = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, r, b);
  589. if (v) {
  590. rows = ggml_view_2d(ctx, rows, r/2, b, rows->nb[1], 0);
  591. }
  592. ggml_tensor * out = ggml_get_rows(ctx, in, rows);
  593. return out;
  594. }
  595. void initialize_tensors(ggml_context * ctx) override {
  596. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  597. if (t->type == GGML_TYPE_I32) {
  598. if (ggml_is_view_op(t->op)) { continue; }
  599. // rows
  600. std::vector<int> data(r*b);
  601. for (int i = 0; i < r*b; i++) {
  602. data[i] = rand() % m;
  603. }
  604. ggml_backend_tensor_set(t, data.data(), 0, r * b * sizeof(int));
  605. } else {
  606. init_tensor_uniform(t);
  607. }
  608. }
  609. }
  610. };
  611. // GGML_OP_REPEAT
  612. struct test_repeat : public test_case {
  613. const ggml_type type;
  614. const std::array<int64_t, 4> ne;
  615. const std::array<int, 4> nr;
  616. std::string vars() override {
  617. return VARS_TO_STR3(type, ne, nr);
  618. }
  619. size_t op_size(ggml_tensor * t) override {
  620. return ggml_nbytes(t) * 2;
  621. }
  622. test_repeat(ggml_type type = GGML_TYPE_F32,
  623. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  624. std::array<int, 4> nr = {2, 2, 2, 2})
  625. : type(type), ne(ne), nr(nr) {}
  626. ggml_tensor * build_graph(ggml_context * ctx) override {
  627. 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]);
  628. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  629. ggml_tensor * out = ggml_repeat(ctx, src, target);
  630. return out;
  631. }
  632. };
  633. // GGML_OP_DUP
  634. struct test_dup : public test_case {
  635. const ggml_type type;
  636. const std::array<int64_t, 4> ne;
  637. const std::array<int64_t, 4> permute;
  638. bool _use_permute;
  639. std::string vars() override {
  640. std::string v = VARS_TO_STR2(type, ne);
  641. if (_use_permute) v += "," + VAR_TO_STR(permute);
  642. return v;
  643. }
  644. test_dup(ggml_type type = GGML_TYPE_F32,
  645. std::array<int64_t, 4> ne = {10, 10, 20, 1},
  646. std::array<int64_t, 4> permute = {0, 0, 0, 0})
  647. : type(type), ne(ne), permute(permute),
  648. _use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {}
  649. ggml_tensor * build_graph(ggml_context * ctx) override {
  650. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  651. if (_use_permute) {
  652. src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]);
  653. }
  654. ggml_tensor * out = ggml_dup(ctx, src);
  655. return out;
  656. }
  657. };
  658. // GGML_OP_CPY
  659. struct test_cpy : public test_case {
  660. const ggml_type type_src;
  661. const ggml_type type_dst;
  662. const std::array<int64_t, 4> ne;
  663. const std::array<int64_t, 4> permute;
  664. bool _src_use_permute;
  665. std::string vars() override {
  666. return VARS_TO_STR4(type_src, type_dst, ne, permute);
  667. }
  668. double max_nmse_err() override {
  669. return 1e-6;
  670. }
  671. size_t op_size(ggml_tensor * t) override {
  672. return ggml_nbytes(t) + ggml_nbytes(t->src[0]);
  673. }
  674. test_cpy(ggml_type type_src = GGML_TYPE_F32, ggml_type type_dst = GGML_TYPE_F32,
  675. std::array<int64_t, 4> ne = {10, 10, 10, 1},
  676. std::array<int64_t, 4> permute = {0, 0, 0, 0})
  677. : type_src(type_src), type_dst(type_dst), ne(ne), permute(permute),
  678. _src_use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {}
  679. ggml_tensor * build_graph(ggml_context * ctx) override {
  680. ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne.data());
  681. if (_src_use_permute) {
  682. src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]);
  683. }
  684. ggml_tensor* dst = ggml_new_tensor(ctx, type_dst, 4, src->ne);
  685. ggml_tensor * out = ggml_cpy(ctx, src, dst);
  686. return out;
  687. }
  688. };
  689. // GGML_OP_CONT
  690. struct test_cont : public test_case {
  691. const ggml_type type;
  692. const std::array<int64_t, 4> ne;
  693. std::string vars() override {
  694. return VARS_TO_STR2(type, ne);
  695. }
  696. test_cont(ggml_type type = GGML_TYPE_F32,
  697. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  698. : type(type), ne(ne) {}
  699. ggml_tensor * build_graph(ggml_context * ctx) override {
  700. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  701. src = ggml_transpose(ctx, src);
  702. ggml_tensor * out = ggml_cont(ctx, src);
  703. return out;
  704. }
  705. };
  706. // GGML_OP_ADD
  707. // GGML_OP_MUL
  708. // GGML_OP_DIV
  709. struct test_bin_bcast : public test_case {
  710. using op_t = ggml_tensor * (*) (ggml_context *, ggml_tensor *, ggml_tensor *);
  711. op_t op;
  712. const ggml_type type;
  713. const std::array<int64_t, 4> ne;
  714. const std::array<int, 4> nr;
  715. std::string vars() override {
  716. return VARS_TO_STR3(type, ne, nr);
  717. }
  718. size_t op_size(ggml_tensor * t) override {
  719. return ggml_nbytes(t) * 3;
  720. }
  721. test_bin_bcast(op_t op, ggml_type type = GGML_TYPE_F32,
  722. std::array<int64_t, 4> ne = {10, 10, 1, 1},
  723. std::array<int, 4> nr = {1, 2, 1, 1})
  724. : op(op), type(type), ne(ne), nr(nr) {}
  725. ggml_tensor * build_graph(ggml_context * ctx) override {
  726. 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]);
  727. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data());
  728. ggml_tensor * out = op(ctx, a, b);
  729. return out;
  730. }
  731. void initialize_tensors(ggml_context * ctx) override {
  732. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  733. if (op == ggml_div) {
  734. // avoid division by zero
  735. init_tensor_uniform(t, 1.0f, 2.0f);
  736. } else {
  737. init_tensor_uniform(t);
  738. }
  739. }
  740. }
  741. };
  742. // GGML_OP_SCALE
  743. struct test_scale : public test_case {
  744. const ggml_type type;
  745. const std::array<int64_t, 4> ne;
  746. float scale;
  747. std::string vars() override {
  748. return VARS_TO_STR3(type, ne, scale);
  749. }
  750. test_scale(ggml_type type = GGML_TYPE_F32,
  751. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  752. float scale = 2.0f)
  753. : type(type), ne(ne), scale(scale) {}
  754. ggml_tensor * build_graph(ggml_context * ctx) override {
  755. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  756. ggml_tensor * out = ggml_scale(ctx, a, scale);
  757. return out;
  758. }
  759. };
  760. // GGML_OP_NORM
  761. struct test_norm : public test_case {
  762. const ggml_type type;
  763. const std::array<int64_t, 4> ne;
  764. float eps;
  765. std::string vars() override {
  766. return VARS_TO_STR3(type, ne, eps);
  767. }
  768. test_norm(ggml_type type = GGML_TYPE_F32,
  769. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  770. float eps = 1e-6f)
  771. : type(type), ne(ne), eps(eps) {}
  772. ggml_tensor * build_graph(ggml_context * ctx) override {
  773. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  774. ggml_tensor * out = ggml_norm(ctx, a, eps);
  775. return out;
  776. }
  777. };
  778. // GGML_OP_RMS_NORM
  779. struct test_rms_norm : public test_case {
  780. const ggml_type type;
  781. const std::array<int64_t, 4> ne;
  782. float eps;
  783. std::string vars() override {
  784. return VARS_TO_STR3(type, ne, eps);
  785. }
  786. test_rms_norm(ggml_type type = GGML_TYPE_F32,
  787. std::array<int64_t, 4> ne = {64, 10, 10, 10},
  788. float eps = 1e-6f)
  789. : type(type), ne(ne), eps(eps) {}
  790. ggml_tensor * build_graph(ggml_context * ctx) override {
  791. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  792. ggml_tensor * out = ggml_rms_norm(ctx, a, eps);
  793. return out;
  794. }
  795. };
  796. // GGML_OP_MUL_MAT
  797. struct test_mul_mat : public test_case {
  798. const ggml_type type_a;
  799. const ggml_type type_b;
  800. const int64_t m;
  801. const int64_t n;
  802. const int64_t k;
  803. const std::array<int64_t, 2> bs; // dims 3 and 4
  804. const std::array<int64_t, 2> nr; // repeat in dims 3 and 4
  805. std::string vars() override {
  806. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, nr);
  807. }
  808. double max_nmse_err() override {
  809. return 5e-4;
  810. }
  811. size_t op_size(ggml_tensor * t) override {
  812. size_t a = ggml_nbytes(t->src[0]) * n * nr[0] * nr[1];
  813. size_t b = ggml_nbytes(t->src[1]) * m;
  814. size_t c = ggml_nbytes(t);
  815. return a + b + c;
  816. GGML_UNUSED(t);
  817. }
  818. test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  819. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  820. std::array<int64_t, 2> bs = {10, 10},
  821. std::array<int64_t, 2> nr = {2, 2})
  822. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr) {}
  823. ggml_tensor * build_graph(ggml_context * ctx) override {
  824. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  825. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, k, m, bs[0] , bs[1]);
  826. ggml_tensor * b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]);
  827. ggml_tensor * out = ggml_mul_mat(ctx, a, b);
  828. return out;
  829. }
  830. };
  831. // GGML_OP_MUL_MAT_ID
  832. struct test_mul_mat_id : public test_case {
  833. const ggml_type type_a;
  834. const ggml_type type_b;
  835. const int n_mats;
  836. const int n_used;
  837. const bool b; // brodcast b matrix
  838. const int64_t m;
  839. const int64_t n;
  840. const int64_t k;
  841. std::string vars() override {
  842. return VARS_TO_STR8(type_a, type_b, n_mats, n_used, b, m, n, k);
  843. }
  844. double max_nmse_err() override {
  845. return 5e-4;
  846. }
  847. size_t op_size(ggml_tensor * t) override {
  848. size_t a = ggml_nbytes(t->src[2]) * n;
  849. size_t b = ggml_nbytes(t->src[1]) * m;
  850. size_t c = ggml_nbytes(t);
  851. return a + b + c;
  852. GGML_UNUSED(t);
  853. }
  854. test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  855. int n_mats = 8, int n_used = 2, bool b = false,
  856. int64_t m = 32, int64_t n = 32, int64_t k = 32)
  857. : type_a(type_a), type_b(type_b), n_mats(n_mats), n_used(n_used), b(b),
  858. m(m), n(n), k(k) {
  859. GGML_ASSERT(n_used <= n_mats);
  860. }
  861. ggml_tensor * build_graph(ggml_context * ctx) override {
  862. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  863. ggml_tensor * as = ggml_new_tensor_3d(ctx, type_a, k, m, n_mats);
  864. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n);
  865. if (n_used != n_mats) {
  866. ids = ggml_view_2d(ctx, ids, n_used, n, ids->nb[1], 0);
  867. }
  868. ggml_tensor * b = ggml_new_tensor_3d(ctx, type_b, k, this->b ? 1 : n_used, n);
  869. ggml_tensor * out = ggml_mul_mat_id(ctx, as, b, ids);
  870. return out;
  871. }
  872. void initialize_tensors(ggml_context * ctx) override {
  873. std::random_device rd;
  874. std::default_random_engine rng(rd());
  875. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  876. if (t->type == GGML_TYPE_I32) {
  877. if (ggml_is_view_op(t->op)) { continue; }
  878. // ids
  879. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  880. std::vector<int32_t> data(t->ne[0]);
  881. for (int i = 0; i < t->ne[0]; i++) {
  882. data[i] = i % n_mats;
  883. }
  884. std::shuffle(data.begin(), data.end(), rng);
  885. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t));
  886. }
  887. } else {
  888. init_tensor_uniform(t);
  889. }
  890. }
  891. }
  892. };
  893. // GGML_OP_SQR
  894. struct test_sqr : public test_case {
  895. const ggml_type type;
  896. const std::array<int64_t, 4> ne;
  897. std::string vars() override {
  898. return VARS_TO_STR2(type, ne);
  899. }
  900. test_sqr(ggml_type type = GGML_TYPE_F32,
  901. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  902. : type(type), ne(ne) {}
  903. ggml_tensor * build_graph(ggml_context * ctx) override {
  904. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  905. ggml_tensor * out = ggml_sqr(ctx, a);
  906. return out;
  907. }
  908. };
  909. // GGML_OP_SQRT
  910. struct test_sqrt : public test_case {
  911. const ggml_type type;
  912. const std::array<int64_t, 4> ne;
  913. std::string vars() override {
  914. return VARS_TO_STR2(type, ne);
  915. }
  916. test_sqrt(ggml_type type = GGML_TYPE_F32,
  917. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  918. : type(type), ne(ne) {}
  919. ggml_tensor * build_graph(ggml_context * ctx) override {
  920. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  921. ggml_tensor * out = ggml_sqrt(ctx, a);
  922. return out;
  923. }
  924. void initialize_tensors(ggml_context * ctx) override {
  925. // fill with positive values
  926. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  927. init_tensor_uniform(t, 0.0f, 100.0f);
  928. }
  929. }
  930. };
  931. // GGML_OP_CLAMP
  932. struct test_clamp : public test_case {
  933. const ggml_type type;
  934. const std::array<int64_t, 4> ne;
  935. float min;
  936. float max;
  937. std::string vars() override {
  938. return VARS_TO_STR4(type, ne, min, max);
  939. }
  940. test_clamp(ggml_type type = GGML_TYPE_F32,
  941. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  942. float min = -0.5f, float max = 0.5f)
  943. : type(type), ne(ne), min(min), max(max) {}
  944. ggml_tensor * build_graph(ggml_context * ctx) override {
  945. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  946. ggml_tensor * out = ggml_clamp(ctx, a, min, max);
  947. return out;
  948. }
  949. };
  950. // GGML_OP_DIAG_MASK_INF
  951. struct test_diag_mask_inf : public test_case {
  952. const ggml_type type;
  953. const std::array<int64_t, 4> ne;
  954. const int n_past;
  955. std::string vars() override {
  956. return VARS_TO_STR3(type, ne, n_past);
  957. }
  958. test_diag_mask_inf(ggml_type type = GGML_TYPE_F32,
  959. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  960. int n_past = 5)
  961. : type(type), ne(ne), n_past(n_past) {}
  962. ggml_tensor * build_graph(ggml_context * ctx) override {
  963. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  964. ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past);
  965. return out;
  966. }
  967. };
  968. // GGML_OP_SOFT_MAX
  969. struct test_soft_max : public test_case {
  970. const ggml_type type;
  971. const std::array<int64_t, 4> ne;
  972. const bool mask;
  973. const float scale;
  974. const float max_bias;
  975. std::string vars() override {
  976. return VARS_TO_STR5(type, ne, mask, scale, max_bias);
  977. }
  978. // the 1024 test with bias occasionally fails:
  979. // SOFT_MAX(type=f32,ne=[1024,16,1,1],mask=1,scale=1.000000,max_bias=8.000000): [SOFT_MAX] NMSE = 0.000000103 > 0.000000100 FAIL
  980. virtual double max_nmse_err() override {
  981. return 1e-6;
  982. }
  983. test_soft_max(ggml_type type = GGML_TYPE_F32,
  984. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  985. bool mask = false,
  986. float scale = 1.0f,
  987. float max_bias = 0.0f)
  988. : type(type), ne(ne), mask(mask), scale(scale), max_bias(max_bias) {}
  989. ggml_tensor * build_graph(ggml_context * ctx) override {
  990. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  991. ggml_tensor * mask = nullptr;
  992. if (this->mask) {
  993. mask = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ne[0], ne[1]);
  994. }
  995. ggml_tensor * out = ggml_soft_max_ext(ctx, a, mask, scale, max_bias);
  996. return out;
  997. }
  998. };
  999. // GGML_OP_ROPE
  1000. struct test_rope : public test_case {
  1001. const ggml_type type;
  1002. const std::array<int64_t, 4> ne_a;
  1003. int n_dims;
  1004. int mode;
  1005. int n_ctx; // used to generate positions
  1006. float fs; // freq_scale
  1007. float ef; // ext_factor
  1008. float af; // attn_factor
  1009. bool ff;
  1010. int v; // view (1 : non-contiguous a)
  1011. std::string vars() override {
  1012. return VARS_TO_STR10(type, ne_a, n_dims, mode, n_ctx, fs, ef, af, ff, v);
  1013. }
  1014. test_rope(ggml_type type = GGML_TYPE_F32,
  1015. std::array<int64_t, 4> ne_a = {10, 10, 10, 1},
  1016. int n_dims = 10, int mode = 0, int n_ctx = 512, float fs = 1.0f, float ef = 0.0f, float af = 0.0f, bool ff = false, int v = 0)
  1017. : type(type), ne_a(ne_a), n_dims(n_dims), mode(mode), n_ctx(n_ctx), fs(fs), ef(ef), af(af), ff(ff), v(v) {}
  1018. ggml_tensor * build_graph(ggml_context * ctx) override {
  1019. ggml_tensor * a;
  1020. if (v & 1) {
  1021. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1022. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1023. a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
  1024. } else {
  1025. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1026. }
  1027. ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2]);
  1028. ggml_tensor * freq = ff ? ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_dims/2) : nullptr;
  1029. ggml_tensor * out = ggml_rope_ext(ctx, a, pos, freq, n_dims, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f);
  1030. return out;
  1031. }
  1032. void initialize_tensors(ggml_context * ctx) override {
  1033. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1034. if (t->type == GGML_TYPE_I32) {
  1035. // pos
  1036. std::vector<int> data(ne_a[2]);
  1037. for (int i = 0; i < ne_a[2]; i++) {
  1038. data[i] = rand() % n_ctx;
  1039. }
  1040. ggml_backend_tensor_set(t, data.data(), 0, ne_a[2] * sizeof(int));
  1041. } else {
  1042. if (t->ne[0] == n_dims/2) {
  1043. // frequency factors in the range [0.9f, 1.1f]
  1044. init_tensor_uniform(t, 0.9f, 1.1f);
  1045. } else {
  1046. init_tensor_uniform(t);
  1047. }
  1048. }
  1049. }
  1050. }
  1051. };
  1052. // GGML_OP_POOL2D
  1053. struct test_pool2d : public test_case {
  1054. enum ggml_op_pool pool_type;
  1055. const ggml_type type_input;
  1056. const std::array<int64_t, 4> ne_input;
  1057. // kernel size
  1058. const int k0;
  1059. const int k1;
  1060. // stride
  1061. const int s0;
  1062. const int s1;
  1063. // padding
  1064. const int p0;
  1065. const int p1;
  1066. std::string vars() override {
  1067. return VARS_TO_STR9(pool_type, type_input, ne_input, k0, k1, s0, s1, p0, p1);
  1068. }
  1069. test_pool2d(ggml_op_pool pool_type = GGML_OP_POOL_AVG,
  1070. ggml_type type_input = GGML_TYPE_F32,
  1071. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1072. int k0 = 3, int k1 = 3,
  1073. int s0 = 1, int s1 = 1,
  1074. int p0 = 1, int p1 = 1)
  1075. : pool_type(pool_type), type_input(type_input), ne_input(ne_input), k0(k0), k1(k1), s0(s0), s1(s1), p0(p0), p1(p1) {}
  1076. ggml_tensor * build_graph(ggml_context * ctx) override {
  1077. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1078. ggml_tensor * out = ggml_pool_2d(ctx, input, pool_type, k0, k1, s0, s1, p0, p1);
  1079. return out;
  1080. }
  1081. };
  1082. // GGML_OP_CONV_TRANSPOSE_1D
  1083. struct test_conv_transpose_1d : public test_case {
  1084. const std::array<int64_t, 4> ne_input;
  1085. const std::array<int64_t, 4> ne_kernel;
  1086. const int s0; // stride
  1087. const int p0; // padding
  1088. const int d0; // dilation
  1089. std::string vars() override {
  1090. return VARS_TO_STR5(ne_input, ne_kernel, s0, p0, d0);
  1091. }
  1092. test_conv_transpose_1d(std::array<int64_t, 4> ne_input = {197, 32, 1, 1}, // [input_width, input_height, input_channels, 1]
  1093. std::array<int64_t, 4> ne_kernel = {16, 32, 32, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1094. int s0 = 1, int p0 = 0, int d0 = 1)
  1095. : ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), p0(p0), d0(d0) {}
  1096. ggml_tensor * build_graph(ggml_context * ctx) override {
  1097. ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data());
  1098. ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data());
  1099. ggml_tensor * out = ggml_conv_transpose_1d(ctx, kernel, input, s0, p0, d0);
  1100. return out;
  1101. }
  1102. };
  1103. // GGML_OP_IM2COL
  1104. struct test_im2col : public test_case {
  1105. const ggml_type type_input;
  1106. const ggml_type type_kernel;
  1107. const ggml_type dst_type;
  1108. const std::array<int64_t, 4> ne_input;
  1109. const std::array<int64_t, 4> ne_kernel;
  1110. // stride
  1111. const int s0;
  1112. const int s1;
  1113. // padding
  1114. const int p0;
  1115. const int p1;
  1116. // dilation
  1117. const int d0;
  1118. const int d1;
  1119. // mode
  1120. const bool is_2D;
  1121. std::string vars() override {
  1122. return VARS_TO_STR12(type_input, type_kernel, dst_type, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  1123. }
  1124. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32,
  1125. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1126. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1127. int s0 = 1, int s1 = 1,
  1128. int p0 = 1, int p1 = 1,
  1129. int d0 = 1, int d1 = 1,
  1130. bool is_2D = true)
  1131. : 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) {}
  1132. ggml_tensor * build_graph(ggml_context * ctx) override {
  1133. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1134. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  1135. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D, dst_type);
  1136. return out;
  1137. }
  1138. };
  1139. // GGML_OP_CONCAT
  1140. struct test_concat : public test_case {
  1141. const ggml_type type;
  1142. const std::array<int64_t, 4> ne_a;
  1143. const int64_t ne_b_d;
  1144. const int dim;
  1145. const int v; // view (1 << 0: non-cont a, 1 << 1: non-cont b)
  1146. std::string vars() override {
  1147. return VARS_TO_STR5(type, ne_a, ne_b_d, dim, v);
  1148. }
  1149. test_concat(ggml_type type = GGML_TYPE_F32,
  1150. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1151. int64_t ne_b_d = 10,
  1152. int dim = 2, int v = 0)
  1153. : type(type), ne_a(ne_a), ne_b_d(ne_b_d), dim(dim), v(v) {}
  1154. ggml_tensor * build_graph(ggml_context * ctx) override {
  1155. auto ne_b = ne_a;
  1156. ne_b[dim] = ne_b_d;
  1157. ggml_tensor * a;
  1158. if (v & 1) {
  1159. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1160. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1161. a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
  1162. } else {
  1163. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1164. }
  1165. ggml_tensor * b;
  1166. if (v & 2) {
  1167. auto ne = ne_b; ne[0] *= 3; ne[1] *= 2; ne[2] *= 4;
  1168. b = ggml_new_tensor(ctx, type, 4, ne.data());
  1169. b = ggml_view_4d(ctx, b, ne_b[0], ne_b[1], ne_b[2], ne_b[3], b->nb[1], b->nb[2], b->nb[3], 0);
  1170. } else {
  1171. b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1172. }
  1173. ggml_tensor * out = ggml_concat(ctx, a, b, dim);
  1174. return out;
  1175. }
  1176. };
  1177. // GGML_OP_ARGSORT
  1178. struct test_argsort : public test_case {
  1179. const ggml_type type;
  1180. const std::array<int64_t, 4> ne;
  1181. ggml_sort_order order;
  1182. std::string vars() override {
  1183. return VARS_TO_STR3(type, ne, order);
  1184. }
  1185. test_argsort(ggml_type type = GGML_TYPE_F32,
  1186. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  1187. ggml_sort_order order = GGML_SORT_ORDER_ASC)
  1188. : type(type), ne(ne), order(order) {}
  1189. ggml_tensor * build_graph(ggml_context * ctx) override {
  1190. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1191. ggml_tensor * out = ggml_argsort(ctx, a, order);
  1192. return out;
  1193. }
  1194. void initialize_tensors(ggml_context * ctx) override {
  1195. std::random_device rd;
  1196. std::default_random_engine rng(rd());
  1197. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1198. if (t->type == GGML_TYPE_I32) {
  1199. // indices
  1200. std::vector<int> data(ggml_nelements(t));
  1201. for (int i = 0; i < ggml_nelements(t); i++) {
  1202. data[i] = rand();
  1203. }
  1204. std::shuffle(data.begin(), data.end(), rng);
  1205. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  1206. } else if (t->type == GGML_TYPE_F32) {
  1207. // initialize with unique values to avoid ties
  1208. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1209. std::vector<float> data(t->ne[0]);
  1210. for (int i = 0; i < t->ne[0]; i++) {
  1211. data[i] = i;
  1212. }
  1213. std::shuffle(data.begin(), data.end(), rng);
  1214. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  1215. }
  1216. } else {
  1217. GGML_ABORT("fatal error");
  1218. }
  1219. }
  1220. }
  1221. };
  1222. // GGML_OP_SUM_ROWS
  1223. struct test_sum_rows : public test_case {
  1224. const ggml_type type;
  1225. const std::array<int64_t, 4> ne;
  1226. std::string vars() override {
  1227. return VARS_TO_STR2(type, ne);
  1228. }
  1229. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1230. std::array<int64_t, 4> ne = {10, 10, 10, 10})
  1231. : type(type), ne(ne) {}
  1232. ggml_tensor * build_graph(ggml_context * ctx) override {
  1233. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1234. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1235. return out;
  1236. }
  1237. };
  1238. // GGML_OP_UPSCALE
  1239. struct test_upscale : public test_case {
  1240. const ggml_type type;
  1241. const std::array<int64_t, 4> ne;
  1242. const int32_t scale_factor;
  1243. const bool transpose;
  1244. std::string vars() override {
  1245. return VARS_TO_STR4(type, ne, scale_factor, transpose);
  1246. }
  1247. test_upscale(ggml_type type = GGML_TYPE_F32,
  1248. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1249. int32_t scale_factor = 2, bool transpose = false)
  1250. : type(type), ne(ne), scale_factor(scale_factor), transpose(transpose) {}
  1251. ggml_tensor * build_graph(ggml_context * ctx) override {
  1252. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1253. if (transpose) a = ggml_transpose(ctx, a);
  1254. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1255. return out;
  1256. }
  1257. };
  1258. // GGML_OP_UPSCALE (ext)
  1259. struct test_upscale_ext : public test_case {
  1260. const ggml_type type;
  1261. const std::array<int64_t, 4> ne;
  1262. const std::array<int64_t, 4> ne_tgt;
  1263. std::string vars() override {
  1264. return VARS_TO_STR3(type, ne, ne_tgt);
  1265. }
  1266. test_upscale_ext(ggml_type type = GGML_TYPE_F32,
  1267. std::array<int64_t, 4> ne = {2, 5, 7, 11},
  1268. std::array<int64_t, 4> ne_tgt = {5, 7, 11, 13})
  1269. : type(type), ne(ne), ne_tgt(ne_tgt) {}
  1270. ggml_tensor * build_graph(ggml_context * ctx) override {
  1271. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1272. ggml_tensor * out = ggml_upscale_ext(ctx, a, ne_tgt[0], ne_tgt[1],ne_tgt[2], ne_tgt[3]);
  1273. return out;
  1274. }
  1275. };
  1276. // GGML_OP_GROUP_NORM
  1277. struct test_group_norm : public test_case {
  1278. const ggml_type type;
  1279. const std::array<int64_t, 4> ne;
  1280. const int32_t num_groups;
  1281. const float eps;
  1282. std::string vars() override {
  1283. return VARS_TO_STR3(type, ne, num_groups);
  1284. }
  1285. test_group_norm(ggml_type type = GGML_TYPE_F32,
  1286. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  1287. int32_t num_groups = 32,
  1288. float eps = 1e-6f)
  1289. : type(type), ne(ne), num_groups(num_groups), eps(eps) {}
  1290. ggml_tensor * build_graph(ggml_context * ctx) override {
  1291. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1292. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups, eps);
  1293. return out;
  1294. }
  1295. };
  1296. // GGML_OP_ACC
  1297. struct test_acc : public test_case {
  1298. const ggml_type type;
  1299. const std::array<int64_t, 4> ne_a;
  1300. const std::array<int64_t, 4> ne_b;
  1301. std::string vars() override {
  1302. return VARS_TO_STR3(type, ne_a, ne_b);
  1303. }
  1304. test_acc(ggml_type type = GGML_TYPE_F32,
  1305. std::array<int64_t, 4> ne_a = {1024, 577, 1, 1},
  1306. std::array<int64_t, 4> ne_b = {1024, 576, 1, 1})
  1307. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1308. ggml_tensor * build_graph(ggml_context * ctx) override {
  1309. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1310. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1311. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  1312. return out;
  1313. }
  1314. };
  1315. // GGML_OP_PAD
  1316. struct test_pad : public test_case {
  1317. const ggml_type type;
  1318. const std::array<int64_t, 4> ne_a;
  1319. const int pad_0;
  1320. const int pad_1;
  1321. std::string vars() override {
  1322. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  1323. }
  1324. test_pad(ggml_type type = GGML_TYPE_F32,
  1325. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  1326. int pad_0 = 1, int pad_1 = 1)
  1327. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  1328. ggml_tensor * build_graph(ggml_context * ctx) override {
  1329. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1330. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  1331. return out;
  1332. }
  1333. };
  1334. // GGML_OP_ARANGE
  1335. struct test_arange : public test_case {
  1336. const ggml_type type;
  1337. const float start;
  1338. const float stop;
  1339. const float step;
  1340. std::string vars() override {
  1341. return VARS_TO_STR4(type, start, stop, step);
  1342. }
  1343. test_arange(ggml_type type = GGML_TYPE_F32,
  1344. float start = 0.f, float stop = 10.f, float step = 1.f)
  1345. : type(type), start(start), stop(stop), step(step) {}
  1346. ggml_tensor * build_graph(ggml_context * ctx) override {
  1347. ggml_tensor * out = ggml_arange(ctx, start, stop, step);
  1348. return out;
  1349. }
  1350. };
  1351. // GGML_OP_TIMESTEP_EMBEDDING
  1352. struct test_timestep_embedding : public test_case {
  1353. const ggml_type type;
  1354. const std::array<int64_t, 4> ne_a;
  1355. const int dim;
  1356. const int max_period;
  1357. std::string vars() override {
  1358. return VARS_TO_STR4(type, ne_a, dim, max_period);
  1359. }
  1360. test_timestep_embedding(ggml_type type = GGML_TYPE_F32,
  1361. std::array<int64_t, 4> ne_a = {2, 1, 1, 1},
  1362. int dim = 320, int max_period=10000)
  1363. : type(type), ne_a(ne_a), dim(dim), max_period(max_period) {}
  1364. ggml_tensor * build_graph(ggml_context * ctx) override {
  1365. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1366. ggml_tensor * out = ggml_timestep_embedding(ctx, a, dim, max_period);
  1367. return out;
  1368. }
  1369. };
  1370. // GGML_OP_LEAKY_RELU
  1371. struct test_leaky_relu : public test_case {
  1372. const ggml_type type;
  1373. const std::array<int64_t, 4> ne_a;
  1374. const float negative_slope;
  1375. std::string vars() override {
  1376. return VARS_TO_STR3(type, ne_a, negative_slope);
  1377. }
  1378. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  1379. std::array<int64_t, 4> ne_a = {10, 10, 10, 10},
  1380. float negative_slope = 0.1f)
  1381. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  1382. ggml_tensor * build_graph(ggml_context * ctx) override {
  1383. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1384. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  1385. return out;
  1386. }
  1387. };
  1388. // GGML_OP_FLASH_ATTN_EXT
  1389. struct test_flash_attn_ext : public test_case {
  1390. const int64_t hs; // head size
  1391. const int64_t nh; // num heads
  1392. const int64_t kv; // kv size
  1393. const int64_t nb; // batch size
  1394. const bool mask; // use mask
  1395. const float max_bias; // ALiBi
  1396. const ggml_type type_KV;
  1397. std::string vars() override {
  1398. return VARS_TO_STR7(hs, nh, kv, nb, mask, max_bias, type_KV);
  1399. }
  1400. double max_nmse_err() override {
  1401. return 5e-4;
  1402. }
  1403. test_flash_attn_ext(int64_t hs = 128, int64_t nh = 32, int64_t kv = 96, int64_t nb = 8, bool mask = true, float max_bias = 0.0f, ggml_type type_KV = GGML_TYPE_F16)
  1404. : hs(hs), nh(nh), kv(kv), nb(nb), mask(mask), max_bias(max_bias), type_KV(type_KV) {}
  1405. ggml_tensor * build_graph(ggml_context * ctx) override {
  1406. const int64_t hs_padded = GGML_PAD(hs, ggml_blck_size(type_KV));
  1407. ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs_padded, nb, nh, 1);
  1408. ggml_tensor * k = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  1409. ggml_tensor * v = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  1410. ggml_tensor * m = mask ? ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, GGML_PAD(nb, GGML_KQ_MASK_PAD), 1, 1) : nullptr;
  1411. ggml_tensor * out = ggml_flash_attn_ext(ctx, q, k, v, m, 1.0f/sqrtf(hs), max_bias);
  1412. return out;
  1413. }
  1414. };
  1415. enum llm_norm_type {
  1416. LLM_NORM,
  1417. LLM_NORM_RMS,
  1418. };
  1419. struct llama_hparams {
  1420. uint32_t n_vocab;
  1421. uint32_t n_embd;
  1422. uint32_t n_head;
  1423. uint32_t n_head_kv;
  1424. static constexpr uint32_t n_layer = 1;
  1425. uint32_t n_rot;
  1426. uint32_t n_embd_head; // dimension of values (d_v)
  1427. uint32_t n_ff;
  1428. float f_norm_eps;
  1429. float f_norm_rms_eps;
  1430. // cparams
  1431. static constexpr uint32_t n_ctx = 512; // user-specified context size
  1432. static constexpr uint32_t n_ctx_orig = n_ctx;
  1433. // batch
  1434. int32_t n_tokens;
  1435. // llm_build_context
  1436. static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx
  1437. static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache
  1438. uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads
  1439. return n_embd_head * n_head_kv;
  1440. }
  1441. };
  1442. // LLM base class
  1443. struct test_llm : public test_case {
  1444. llama_hparams hp;
  1445. protected:
  1446. test_llm(llama_hparams hp)
  1447. : hp(std::move(hp)) {
  1448. }
  1449. public:
  1450. struct ggml_tensor * llm_build_norm(
  1451. struct ggml_context * ctx,
  1452. struct ggml_tensor * cur,
  1453. struct ggml_tensor * mw,
  1454. struct ggml_tensor * mb,
  1455. llm_norm_type type) {
  1456. switch (type) {
  1457. case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break;
  1458. case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break;
  1459. }
  1460. cur = ggml_mul(ctx, cur, mw);
  1461. if (mb) {
  1462. cur = ggml_add(ctx, cur, mb);
  1463. }
  1464. return cur;
  1465. }
  1466. void llm_build_kv_store(
  1467. struct ggml_context * ctx,
  1468. struct ggml_tensor * k_l,
  1469. struct ggml_tensor * v_l,
  1470. struct ggml_tensor * k_cur,
  1471. struct ggml_tensor * v_cur) {
  1472. // compute the transposed [n_tokens, n_embd] V matrix
  1473. struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens));
  1474. struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(),
  1475. (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head);
  1476. struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(),
  1477. ( hp.n_ctx)*ggml_element_size(v_l),
  1478. (hp.kv_head)*ggml_element_size(v_l));
  1479. // important: storing RoPE-ed version of K in the KV cache!
  1480. ggml_cpy(ctx, k_cur, k_cache_view);
  1481. ggml_cpy(ctx, v_cur_t, v_cache_view);
  1482. }
  1483. struct ggml_tensor * llm_build_kqv(
  1484. struct ggml_context * ctx,
  1485. struct ggml_tensor * k_l,
  1486. struct ggml_tensor * v_l,
  1487. struct ggml_tensor * q_cur,
  1488. struct ggml_tensor * kq_mask,
  1489. float kq_scale) {
  1490. struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3);
  1491. struct ggml_tensor * k =
  1492. ggml_view_3d(ctx, k_l,
  1493. hp.n_embd_head, hp.n_kv, hp.n_head_kv,
  1494. ggml_row_size(k_l->type, hp.n_embd_gqa()),
  1495. ggml_row_size(k_l->type, hp.n_embd_head),
  1496. 0);
  1497. struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q);
  1498. kq = ggml_soft_max_ext(ctx, kq, kq_mask, kq_scale, 0.0f);
  1499. // split cached v into n_head heads
  1500. struct ggml_tensor * v =
  1501. ggml_view_3d(ctx, v_l,
  1502. hp.n_kv, hp.n_embd_head, hp.n_head_kv,
  1503. ggml_element_size(v_l)*hp.n_ctx,
  1504. ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head,
  1505. 0);
  1506. struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq);
  1507. struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3);
  1508. struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens);
  1509. struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1510. cur = ggml_mul_mat(ctx, wo, cur);
  1511. return cur;
  1512. }
  1513. void initialize_tensors(ggml_context * ctx) override {
  1514. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1515. if (t->type == GGML_TYPE_I32) {
  1516. // pos
  1517. std::vector<int> data(hp.n_tokens);
  1518. for (int i = 0; i < hp.n_tokens; i++) {
  1519. data[i] = rand() % hp.n_ctx;
  1520. }
  1521. ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int));
  1522. } else {
  1523. init_tensor_uniform(t);
  1524. }
  1525. }
  1526. }
  1527. };
  1528. // Llama
  1529. struct test_llama : public test_llm {
  1530. static constexpr float freq_base = 10000.0f;
  1531. static constexpr float freq_scale = 1.0f;
  1532. static constexpr float ext_factor = 0.0f;
  1533. static constexpr float attn_factor = 1.0f;
  1534. static constexpr float beta_fast = 32.0f;
  1535. static constexpr float beta_slow = 1.0f;
  1536. std::string op_desc(ggml_tensor * t) override {
  1537. GGML_UNUSED(t);
  1538. return "LLAMA";
  1539. }
  1540. std::string vars() override {
  1541. auto n_tokens = hp.n_tokens;
  1542. return VARS_TO_STR1(n_tokens);
  1543. }
  1544. double max_nmse_err() override {
  1545. return 2e-3;
  1546. }
  1547. test_llama(int n_tokens = 1)
  1548. : test_llm({
  1549. /*n_vocab =*/ 32000,
  1550. /*n_embd =*/ 3200,
  1551. /*n_head =*/ 32,
  1552. /*n_head_kv =*/ 32,
  1553. /*n_rot =*/ 100,
  1554. /*n_embd_head =*/ 100,
  1555. /*n_ff =*/ 8640,
  1556. /*f_norm_eps =*/ 0.f,
  1557. /*f_norm_rms_eps =*/ 1e-5f,
  1558. /*n_tokens =*/ n_tokens,
  1559. }) {
  1560. }
  1561. ggml_tensor * build_graph(ggml_context * ctx) override {
  1562. struct ggml_tensor * cur;
  1563. struct ggml_tensor * inpL;
  1564. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1565. // inp_pos - contains the positions
  1566. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1567. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1568. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  1569. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1570. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1571. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1572. struct ggml_tensor * inpSA = inpL;
  1573. // norm
  1574. ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1575. cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS);
  1576. // self-attention
  1577. {
  1578. ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  1579. ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1580. ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  1581. // compute Q and K and RoPE them
  1582. struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur);
  1583. struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur);
  1584. struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur);
  1585. Qcur = ggml_rope_ext(
  1586. ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos, nullptr,
  1587. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  1588. ext_factor, attn_factor, beta_fast, beta_slow
  1589. );
  1590. Kcur = ggml_rope_ext(
  1591. ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos, nullptr,
  1592. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  1593. ext_factor, attn_factor, beta_fast, beta_slow
  1594. );
  1595. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1596. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1597. }
  1598. struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA);
  1599. // feed-forward network
  1600. ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1601. cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS);
  1602. ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1603. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1604. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1605. struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur);
  1606. cur = ggml_mul_mat(ctx, ffn_gate, cur);
  1607. cur = ggml_silu(ctx, cur);
  1608. cur = ggml_mul(ctx, cur, tmp);
  1609. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1610. cur = ggml_add(ctx, cur, ffn_inp);
  1611. // input for next layer
  1612. inpL = cur;
  1613. }
  1614. cur = inpL;
  1615. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1616. cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS);
  1617. // lm_head
  1618. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab);
  1619. cur = ggml_mul_mat(ctx, output, cur);
  1620. return cur;
  1621. }
  1622. };
  1623. // Falcon
  1624. struct test_falcon : public test_llm {
  1625. static constexpr float freq_base = 10000.0f;
  1626. static constexpr float freq_scale = 1.0f;
  1627. static constexpr float ext_factor = 0.0f;
  1628. static constexpr float attn_factor = 1.0f;
  1629. static constexpr float beta_fast = 32.0f;
  1630. static constexpr float beta_slow = 1.0f;
  1631. std::string op_desc(ggml_tensor * t) override {
  1632. GGML_UNUSED(t);
  1633. return "FALCON";
  1634. }
  1635. std::string vars() override {
  1636. auto n_tokens = hp.n_tokens;
  1637. return VARS_TO_STR1(n_tokens);
  1638. }
  1639. double max_nmse_err() override {
  1640. return 2e-3;
  1641. }
  1642. test_falcon(int n_tokens = 1)
  1643. : test_llm({
  1644. /*n_vocab =*/ 32000,
  1645. /*n_embd =*/ 3200,
  1646. /*n_head =*/ 50,
  1647. /*n_head_kv =*/ 1,
  1648. /*n_rot =*/ 64,
  1649. /*n_embd_head =*/ 64,
  1650. /*n_ff =*/ 8640,
  1651. /*f_norm_eps =*/ 1e-5f,
  1652. /*f_norm_rms_eps =*/ 0.f,
  1653. /*n_tokens =*/ n_tokens,
  1654. }) {
  1655. }
  1656. ggml_tensor * build_graph(ggml_context * ctx) override {
  1657. struct ggml_tensor * cur;
  1658. struct ggml_tensor * inpL;
  1659. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  1660. // inp_pos - contains the positions
  1661. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  1662. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  1663. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  1664. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1665. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  1666. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  1667. // norm
  1668. ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1669. ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1670. ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM);
  1671. // self-attention
  1672. {
  1673. cur = attn_norm;
  1674. ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa());
  1675. cur = ggml_mul_mat(ctx, wqkv, cur);
  1676. 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)));
  1677. 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)));
  1678. 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())));
  1679. Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens);
  1680. Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens);
  1681. // using mode = 2 for neox mode
  1682. Qcur = ggml_rope_ext(
  1683. ctx, Qcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  1684. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1685. );
  1686. Kcur = ggml_rope_ext(
  1687. ctx, Kcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  1688. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  1689. );
  1690. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  1691. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  1692. }
  1693. struct ggml_tensor * ffn_inp = cur;
  1694. // feed forward
  1695. {
  1696. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  1697. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  1698. cur = attn_norm;
  1699. cur = ggml_mul_mat(ctx, ffn_up, cur);
  1700. cur = ggml_gelu(ctx, cur);
  1701. cur = ggml_mul_mat(ctx, ffn_down, cur);
  1702. }
  1703. cur = ggml_add(ctx, cur, ffn_inp);
  1704. cur = ggml_add(ctx, cur, inpL);
  1705. // input for next layer
  1706. inpL = cur;
  1707. }
  1708. cur = inpL;
  1709. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1710. ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  1711. cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM);
  1712. // lm_head
  1713. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab);
  1714. cur = ggml_mul_mat(ctx, output, cur);
  1715. return cur;
  1716. }
  1717. };
  1718. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  1719. std::vector<std::unique_ptr<test_case>> test_cases;
  1720. std::default_random_engine rng(0);
  1721. const ggml_type all_types[] = {
  1722. GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16,
  1723. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  1724. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1725. GGML_TYPE_Q8_0,
  1726. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1727. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  1728. GGML_TYPE_Q6_K,
  1729. GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  1730. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  1731. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  1732. };
  1733. const ggml_type base_types[] = {
  1734. GGML_TYPE_F32, GGML_TYPE_F16,
  1735. GGML_TYPE_Q4_0,
  1736. GGML_TYPE_Q4_K,
  1737. GGML_TYPE_IQ2_XXS
  1738. };
  1739. const ggml_type other_types[] = {
  1740. GGML_TYPE_Q4_1,
  1741. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  1742. GGML_TYPE_Q8_0,
  1743. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  1744. GGML_TYPE_Q5_K,
  1745. GGML_TYPE_Q6_K,
  1746. GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  1747. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  1748. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  1749. GGML_TYPE_BF16,
  1750. };
  1751. // unary ops
  1752. for (int v : {0, 1}) {
  1753. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  1754. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 128, 10, 10, 10 }, v));
  1755. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 7, 13, 19, 23 }, v));
  1756. }
  1757. }
  1758. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  1759. for (ggml_type type : all_types) {
  1760. for (int b : {1, 7}) {
  1761. for (bool v : {false, true}) {
  1762. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  1763. }
  1764. }
  1765. }
  1766. for (int b : {1, 7}) {
  1767. for (bool v : {false, true}) {
  1768. test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, v));
  1769. }
  1770. }
  1771. for (ggml_type type_input : {GGML_TYPE_F32}) {
  1772. for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) {
  1773. for (int k0 : {1, 3}) {
  1774. for (int k1 : {1, 3}) {
  1775. for (int s0 : {1, 2}) {
  1776. for (int s1 : {1, 2}) {
  1777. for (int p0 : {0, 1}) {
  1778. for (int p1 : {0, 1}) {
  1779. test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1));
  1780. }
  1781. }
  1782. }
  1783. }
  1784. }
  1785. }
  1786. }
  1787. }
  1788. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32));
  1789. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16));
  1790. // test cases for 1D im2col
  1791. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {3000, 128, 1, 1}, {3, 128, 1280, 1}, 1, 0, 1, 0, 1, 0, false));
  1792. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {3000, 128, 1, 1}, {3, 128, 1280, 1}, 1, 0, 1, 0, 1, 0, false));
  1793. test_cases.emplace_back(new test_conv_transpose_1d());
  1794. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 3, 0, 1));
  1795. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 2, 0, 1));
  1796. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 1, 0, 1));
  1797. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 2, 0, 1));
  1798. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 1, 0, 1));
  1799. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,1,2,1}, 1, 0, 1));
  1800. test_cases.emplace_back(new test_conv_transpose_1d({2,1,1,1}, {3,1,1,1}, 1, 0, 1));
  1801. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 1}));
  1802. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1803. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 2, 1, 1}));
  1804. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 2, 1}));
  1805. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1806. test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 10, 10, 10}, {2, 1, 1, 1}));
  1807. test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 10, 10, 10}, {1, 1, 1, 2}));
  1808. test_cases.emplace_back(new test_dup(GGML_TYPE_F32));
  1809. test_cases.emplace_back(new test_dup(GGML_TYPE_F16));
  1810. test_cases.emplace_back(new test_dup(GGML_TYPE_I32));
  1811. test_cases.emplace_back(new test_dup(GGML_TYPE_I16));
  1812. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {0, 2, 1, 3}));
  1813. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {0, 2, 1, 3})); // dup by rows
  1814. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {1, 0, 2, 3}));
  1815. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {1, 0, 2, 3})); // dup dst not-contiguous
  1816. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3}));
  1817. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3}));
  1818. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1819. for (ggml_type type_dst : all_types) {
  1820. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4}));
  1821. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {0, 2, 1, 3})); // cpy by rows
  1822. }
  1823. }
  1824. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1825. for (ggml_type type_dst : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  1826. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {1, 0, 2, 3})); // cpy not-contiguous
  1827. }
  1828. }
  1829. test_cases.emplace_back(new test_cont());
  1830. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  1831. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  1832. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  1833. }
  1834. };
  1835. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  1836. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  1837. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  1838. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 1, 1}, {1, 1, 1, 1});
  1839. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 1}, {1, 1, 1, 1});
  1840. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 1});
  1841. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 1, 1, 1});
  1842. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 1, 1});
  1843. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 1});
  1844. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 1, 2});
  1845. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 1, 2, 2});
  1846. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {1, 2, 2, 2});
  1847. add_test_bin_bcast(GGML_TYPE_F32, {16, 10, 10, 10}, {2, 2, 2, 2});
  1848. // stable diffusion
  1849. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  1850. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  1851. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  1852. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  1853. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  1854. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  1855. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  1856. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  1857. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  1858. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  1859. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  1860. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  1861. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  1862. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  1863. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  1864. test_cases.emplace_back(new test_scale());
  1865. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  1866. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1867. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
  1868. }
  1869. #if 1
  1870. for (ggml_type type_a : base_types) {
  1871. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1872. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  1873. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  1874. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  1875. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  1876. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  1877. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  1878. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  1879. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  1880. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  1881. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  1882. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  1883. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  1884. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  1885. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  1886. }
  1887. }
  1888. #else
  1889. // m = a rows
  1890. // n = b rows
  1891. // k = cols
  1892. std::uniform_int_distribution<> dist_m(1, 128);
  1893. std::uniform_int_distribution<> dist_n(16, 128);
  1894. std::uniform_int_distribution<> dist_k(1, 16);
  1895. for (int i = 0; i < 1000; i++) {
  1896. for (ggml_type type_a : all_types) {
  1897. for (ggml_type type_b : {GGML_TYPE_F32}) {
  1898. int m = dist_m(rng);
  1899. int n = dist_n(rng);
  1900. int k = dist_k(rng) * ggml_blck_size(type_a);
  1901. test_cases.emplace_back(new test_mul_mat(type_a, type_b, m, n, k, { 1, 1}, {1, 1}));
  1902. }
  1903. }
  1904. }
  1905. #endif
  1906. for (ggml_type type_a : other_types) {
  1907. for (ggml_type type_b : {GGML_TYPE_F32}) {
  1908. if (ggml_blck_size(type_a) != 256) {
  1909. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, ggml_blck_size(type_a), {1, 1}, {1, 1}));
  1910. }
  1911. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {1, 1}, {1, 1}));
  1912. }
  1913. }
  1914. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 128, { 8, 1}, {1, 1}));
  1915. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 128, { 8, 1}, {4, 1}));
  1916. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 64, { 8, 1}, {4, 1}));
  1917. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 64, { 8, 1}, {4, 1}));
  1918. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 45, 128, { 8, 1}, {4, 1}));
  1919. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 128, 45, 64, { 8, 1}, {4, 1}));
  1920. for (ggml_type type_a : base_types) {
  1921. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1922. for (int n_mats : {4, 8}) {
  1923. for (int n_used : {1, 2, 4}) {
  1924. for (bool b : {false, true}) {
  1925. for (int n : {1, 32}) {
  1926. int m = 512;
  1927. int k = 256;
  1928. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  1929. }
  1930. }
  1931. }
  1932. }
  1933. }
  1934. }
  1935. for (ggml_type type_a : other_types) {
  1936. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  1937. for (int n_mats : {4}) {
  1938. for (int n_used : {2}) {
  1939. for (bool b : {false}) {
  1940. for (int n : {1}) {
  1941. int m = 512;
  1942. int k = 256;
  1943. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  1944. }
  1945. }
  1946. }
  1947. }
  1948. }
  1949. }
  1950. test_cases.emplace_back(new test_sqr());
  1951. test_cases.emplace_back(new test_sqrt());
  1952. test_cases.emplace_back(new test_clamp());
  1953. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  1954. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 1}, 5));
  1955. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 10, 10}, 5));
  1956. #if 0
  1957. std::uniform_int_distribution<> dist_ne1(1, 50);
  1958. int exponent = 1;
  1959. while (exponent < (1 << 17)) {
  1960. std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent);
  1961. for (int n = 0; n < 10; ++n) {
  1962. int64_t ne0 = dist_ne0(rng);
  1963. int64_t ne1 = dist_ne1(rng);
  1964. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, GGML_TYPE_F32, {ne0, ne1, 1, 1}, n/2 == 0, 0.1f, ne0 < 1000 ? 4.0f : 0.0f));
  1965. }
  1966. exponent <<= 1;
  1967. }
  1968. #endif
  1969. for (bool mask : {false, true}) {
  1970. for (float max_bias : {0.0f, 8.0f}) {
  1971. if (!mask && max_bias > 0.0f) continue;
  1972. for (float scale : {1.0f, 0.1f}) {
  1973. for (int64_t ne0 : {16, 1024}) {
  1974. for (int64_t ne1 : {16, 1024}) {
  1975. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, scale, max_bias));
  1976. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, scale, max_bias));
  1977. }
  1978. }
  1979. }
  1980. }
  1981. }
  1982. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, true, 0.1f, 0.0f));
  1983. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 0.0f));
  1984. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 0.0f));
  1985. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 8.0f));
  1986. {
  1987. bool all = true;
  1988. for (float v : { 0, 1 }) {
  1989. for (float fs : { 1.0f, 1.4245f }) {
  1990. for (float ef : { 0.0f, 0.7465f }) {
  1991. for (float af : { 1.0f, 1.4245f }) {
  1992. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  1993. for (bool ff : {false, true}) { // freq_factors
  1994. test_cases.emplace_back(new test_rope(type, {128, 32, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 7B
  1995. if (all) {
  1996. test_cases.emplace_back(new test_rope(type, {128, 40, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 13B
  1997. test_cases.emplace_back(new test_rope(type, {128, 52, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 30B
  1998. test_cases.emplace_back(new test_rope(type, {128, 64, 10, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 65B
  1999. }
  2000. if (all) {
  2001. test_cases.emplace_back(new test_rope(type, { 64, 1, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  2002. test_cases.emplace_back(new test_rope(type, { 64, 71, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  2003. test_cases.emplace_back(new test_rope(type, { 64, 8, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2004. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 20, 2, 512, fs, ef, af, ff, v)); // neox (stablelm)
  2005. test_cases.emplace_back(new test_rope(type, { 80, 32, 10, 1}, 32, 2, 512, fs, ef, af, ff, v)); // neox (phi-2)
  2006. }
  2007. test_cases.emplace_back(new test_rope(type, { 64, 128, 10, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2008. }
  2009. }
  2010. all = false;
  2011. }
  2012. }
  2013. }
  2014. }
  2015. }
  2016. for (int v : { 0, 1, 2, 3 }) {
  2017. for (int dim : { 0, 1, 2, 3, }) {
  2018. test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {11, 12, 13, 14}, 7, dim, v));
  2019. test_cases.emplace_back(new test_concat(GGML_TYPE_I32, {11, 12, 13, 14}, 7, dim, v));
  2020. }
  2021. }
  2022. for (ggml_sort_order order : {GGML_SORT_ORDER_ASC, GGML_SORT_ORDER_DESC}) {
  2023. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  2024. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  2025. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {60, 10, 10, 10}, order)); // qwen
  2026. }
  2027. test_cases.emplace_back(new test_sum_rows());
  2028. test_cases.emplace_back(new test_upscale());
  2029. test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, { 512, 512, 3, 1 }, 2, true));
  2030. test_cases.emplace_back(new test_upscale_ext());
  2031. test_cases.emplace_back(new test_group_norm());
  2032. test_cases.emplace_back(new test_acc());
  2033. test_cases.emplace_back(new test_pad());
  2034. test_cases.emplace_back(new test_arange());
  2035. test_cases.emplace_back(new test_timestep_embedding());
  2036. test_cases.emplace_back(new test_leaky_relu());
  2037. for (int hs : { 64, 80, 128, 256, }) {
  2038. for (bool mask : { true, false } ) {
  2039. for (float max_bias : { 0.0f, 8.0f }) {
  2040. if (!mask && max_bias > 0.0f) continue;
  2041. for (int nh : { 32, }) {
  2042. for (int kv : { 512, 1024, }) {
  2043. for (int nb : { 1, 2, 4, 8, }) {
  2044. for (ggml_type type_KV : {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
  2045. test_cases.emplace_back(new test_flash_attn_ext(hs, nh, kv, nb, mask, max_bias, type_KV));
  2046. }
  2047. }
  2048. }
  2049. }
  2050. }
  2051. }
  2052. }
  2053. // these tests are disabled to save execution time, but they can be handy for debugging
  2054. #if 0
  2055. test_cases.emplace_back(new test_llama(1));
  2056. test_cases.emplace_back(new test_llama(2));
  2057. test_cases.emplace_back(new test_falcon(1));
  2058. test_cases.emplace_back(new test_falcon(2));
  2059. #endif
  2060. // run tests
  2061. if (mode == MODE_TEST) {
  2062. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  2063. size_t n_ok = 0;
  2064. for (auto & test : test_cases) {
  2065. if (test->eval(backend, backend_cpu, op_name)) {
  2066. n_ok++;
  2067. }
  2068. }
  2069. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  2070. ggml_backend_free(backend_cpu);
  2071. return n_ok == test_cases.size();
  2072. }
  2073. if (mode == MODE_PERF) {
  2074. for (auto & test : test_cases) {
  2075. test->eval_perf(backend, op_name);
  2076. }
  2077. return true;
  2078. }
  2079. GGML_ABORT("fatal error");
  2080. return false;
  2081. }
  2082. static void usage(char ** argv) {
  2083. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  2084. printf(" valid modes are: test (compare with CPU backend for correctness) or perf (performance evaluation)\n");
  2085. printf(" op names are as given by ggml_op_desc()\n");
  2086. }
  2087. int main(int argc, char ** argv) {
  2088. test_mode mode = MODE_TEST;
  2089. const char * op_name_filter = NULL;
  2090. const char * backend_filter = NULL;
  2091. for (int i = 1; i < argc; i++) {
  2092. if (strcmp(argv[i], "test") == 0) {
  2093. mode = MODE_TEST;
  2094. } else if (strcmp(argv[i], "perf") == 0) {
  2095. mode = MODE_PERF;
  2096. } else if (strcmp(argv[i], "-o") == 0) {
  2097. if (i + 1 < argc) {
  2098. op_name_filter = argv[++i];
  2099. } else {
  2100. usage(argv);
  2101. return 1;
  2102. }
  2103. } else if (strcmp(argv[i], "-b") == 0) {
  2104. if (i + 1 < argc) {
  2105. backend_filter = argv[++i];
  2106. } else {
  2107. usage(argv);
  2108. return 1;
  2109. }
  2110. } else {
  2111. usage(argv);
  2112. return 1;
  2113. }
  2114. }
  2115. // enumerate backends
  2116. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  2117. size_t n_ok = 0;
  2118. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  2119. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  2120. if (backend_filter != NULL && strcmp(backend_filter, ggml_backend_reg_get_name(i)) != 0) {
  2121. printf(" Skipping\n");
  2122. n_ok++;
  2123. continue;
  2124. }
  2125. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  2126. GGML_ASSERT(backend != NULL);
  2127. if (backend_filter == NULL && ggml_backend_is_cpu(backend)) {
  2128. printf(" Skipping CPU backend\n");
  2129. ggml_backend_free(backend);
  2130. n_ok++;
  2131. continue;
  2132. }
  2133. printf(" Backend name: %s\n", ggml_backend_name(backend));
  2134. bool ok = test_backend(backend, mode, op_name_filter);
  2135. printf(" Backend %s: ", ggml_backend_name(backend));
  2136. if (ok) {
  2137. printf("\033[1;32mOK\033[0m\n");
  2138. n_ok++;
  2139. } else {
  2140. printf("\033[1;31mFAIL\033[0m\n");
  2141. }
  2142. printf("\n");
  2143. ggml_backend_free(backend);
  2144. }
  2145. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  2146. if (n_ok != ggml_backend_reg_get_count()) {
  2147. printf("\033[1;31mFAIL\033[0m\n");
  2148. return 1;
  2149. }
  2150. ggml_quantize_free();
  2151. printf("\033[1;32mOK\033[0m\n");
  2152. return 0;
  2153. }