test-backend-ops.cpp 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620
  1. // This file defines tests for various GGML ops and backends.
  2. // For the forward pass it asserts that the results of multiple backends computing the same GGML ops are consistent.
  3. // For the backwards pass it asserts that the gradients from backpropagation are consistent
  4. // with the gradients obtained via the method of finite differences ("grad" mode, this is optional).
  5. // It is also possible to check the performance ("perf" mode).
  6. //
  7. // this file has three sections: Section 1 does general setup, section 2 defines the GGML ops to be tested,
  8. // and section 3 defines which tests to run.
  9. // Quick start for adding a new GGML op: Go to section 2 and create a struct that inherits from test_case,
  10. // then go to section 3 and add an instantiation of your struct.
  11. // ##############################
  12. // ## Section 1: General Setup ##
  13. // ##############################
  14. #include <ggml.h>
  15. #include <ggml-alloc.h>
  16. #include <ggml-backend.h>
  17. #include <algorithm>
  18. #include <array>
  19. #include <cfloat>
  20. #include <cstdint>
  21. #include <cstring>
  22. #include <cinttypes>
  23. #include <functional>
  24. #include <memory>
  25. #include <random>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string>
  29. #include <thread>
  30. #include <vector>
  31. static void init_tensor_uniform(ggml_tensor * tensor, float min = -1.0f, float max = 1.0f) {
  32. // static RNG initialization (revisit if n_threads stops being constant)
  33. static const size_t n_threads = std::thread::hardware_concurrency();
  34. static std::vector<std::default_random_engine> generators = []() {
  35. std::random_device rd;
  36. std::vector<std::default_random_engine> vec;
  37. vec.reserve(n_threads);
  38. //for (size_t i = 0; i < n_threads; i++) { vec.emplace_back(1234 + i); } // fixed seed
  39. for (size_t i = 0; i < n_threads; i++) { vec.emplace_back(rd()); }
  40. return vec;
  41. }();
  42. size_t size = ggml_nelements(tensor);
  43. std::vector<float> data(size);
  44. auto init_thread = [&](size_t ith, size_t start, size_t end) {
  45. std::uniform_real_distribution<float> distribution(min, max);
  46. for (size_t i = start; i < end; i++) {
  47. data[i] = distribution(generators[ith]);
  48. }
  49. };
  50. std::vector<std::thread> threads;
  51. threads.reserve(n_threads);
  52. for (size_t i = 0; i < n_threads; i++) {
  53. size_t start = i*size/n_threads;
  54. size_t end = (i+1)*size/n_threads;
  55. threads.emplace_back(init_thread, i, start, end);
  56. }
  57. for (auto & t : threads) {
  58. t.join();
  59. }
  60. #if 0
  61. const char * val_str = getenv("GGML_TEST_EPS");
  62. float val = 1e-9f;
  63. if (val_str != nullptr) {
  64. val = std::stof(val_str);
  65. printf("GGML_TEST_EPS=%e\n", val);
  66. }
  67. // test quantization with very small values that may result in nan scales due to division by zero
  68. if (ggml_is_quantized(tensor->type)) {
  69. for (int i = 0; i < 256; i++) {
  70. data[i] = val;
  71. }
  72. }
  73. #endif
  74. if (tensor->type == GGML_TYPE_F32 || tensor->type == GGML_TYPE_I32) {
  75. ggml_backend_tensor_set(tensor, data.data(), 0, size * sizeof(float));
  76. } else if (ggml_is_quantized(tensor->type) || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_BF16) {
  77. GGML_ASSERT(size % ggml_blck_size(tensor->type) == 0);
  78. std::vector<uint8_t> dataq(ggml_row_size(tensor->type, size));
  79. std::vector<float> imatrix(tensor->ne[0], 1.0f); // dummy importance matrix
  80. const float * im = imatrix.data();
  81. if (!ggml_quantize_requires_imatrix(tensor->type)) {
  82. // when the imatrix is optional, we want to test both quantization with and without imatrix
  83. // use one of the random numbers to decide
  84. if (data[0] > 0.5f*(min + max)) {
  85. im = nullptr;
  86. }
  87. }
  88. ggml_quantize_chunk(tensor->type, data.data(), dataq.data(), 0, size/tensor->ne[0], tensor->ne[0], im);
  89. GGML_ASSERT(ggml_validate_row_data(tensor->type, dataq.data(), dataq.size()));
  90. // TODO: other cases
  91. //#pragma omp parallel for
  92. //for (int i = 0; i < tensor->ne[1]; i++) {
  93. // ggml_quantize_chunk(tensor->type, data.data(), dataq.data(),
  94. // i * tensor->ne[0], 1, tensor->ne[0], im);
  95. //}
  96. ggml_backend_tensor_set(tensor, dataq.data(), 0, dataq.size());
  97. } else if (tensor->type == GGML_TYPE_I8 || tensor->type == GGML_TYPE_I16 || tensor->type == GGML_TYPE_I32) {
  98. // This is going to create some weird integers though.
  99. ggml_backend_tensor_set(tensor, data.data(), 0, ggml_nbytes(tensor));
  100. } else {
  101. GGML_ABORT("fatal error");
  102. }
  103. }
  104. static std::vector<float> tensor_to_float(const ggml_tensor * t) {
  105. std::vector<float> tv;
  106. tv.reserve(ggml_nelements(t));
  107. std::vector<uint8_t> buf(ggml_nbytes(t));
  108. ggml_backend_tensor_get(t, buf.data(), 0, ggml_nbytes(t));
  109. ggml_type_traits_t tt = ggml_internal_get_type_traits(t->type);
  110. size_t bs = ggml_blck_size(t->type);
  111. std::vector<float> vq(ggml_blck_size(t->type));
  112. bool quantized = ggml_is_quantized(t->type);
  113. // access elements by index to avoid gaps in views
  114. for (int64_t i3 = 0; i3 < t->ne[3]; i3++) {
  115. for (int64_t i2 = 0; i2 < t->ne[2]; i2++) {
  116. for (int64_t i1 = 0; i1 < t->ne[1]; i1++) {
  117. for (int64_t i0 = 0; i0 < t->ne[0]; i0 += bs) {
  118. size_t i = i3*t->nb[3] + i2*t->nb[2] + i1*t->nb[1] + i0/bs*t->nb[0];
  119. if (t->type == GGML_TYPE_F16) {
  120. tv.push_back(ggml_fp16_to_fp32(*(ggml_fp16_t*)&buf[i]));
  121. } else if (t->type == GGML_TYPE_BF16) {
  122. tv.push_back(ggml_bf16_to_fp32(*(ggml_bf16_t*)&buf[i]));
  123. } else if (t->type == GGML_TYPE_F32) {
  124. tv.push_back(*(float *) &buf[i]);
  125. } else if (t->type == GGML_TYPE_I32) {
  126. tv.push_back((float)*(int32_t *) &buf[i]);
  127. } else if (t->type == GGML_TYPE_I16) {
  128. tv.push_back((float)*(int16_t *) &buf[i]);
  129. } else if (t->type == GGML_TYPE_I8) {
  130. tv.push_back((float)*(int8_t *) &buf[i]);
  131. } else if (quantized) {
  132. tt.to_float(&buf[i], vq.data(), bs);
  133. tv.insert(tv.end(), vq.begin(), vq.end());
  134. } else {
  135. GGML_ABORT("fatal error");
  136. }
  137. }
  138. }
  139. }
  140. }
  141. return tv;
  142. }
  143. /*
  144. static double cosine_similarity(const float * v1, const float * v2, size_t n) {
  145. double dot = 0.0;
  146. double mag1 = 0.0;
  147. double mag2 = 0.0;
  148. for (size_t i = 0; i < n; i++) {
  149. if (std::isnan(v1[i]) || std::isnan(v2[i])) {
  150. return -1.0f;
  151. }
  152. if (std::isinf(v1[i]) && std::isinf(v2[i])) {
  153. continue;
  154. }
  155. dot += v1[i]*v2[i];
  156. mag1 += v1[i]*v1[i];
  157. mag2 += v2[i]*v2[i];
  158. }
  159. return dot/sqrt(mag1*mag2);
  160. }
  161. static float distance(const float * v1, const float * v2, size_t n) {
  162. double d = 0.0;
  163. for (size_t i = 0; i < n; i++) {
  164. if (std::isnan(v1[i]) || std::isnan(v2[i])) {
  165. return INFINITY;
  166. }
  167. if (std::isinf(v1[i]) && std::isinf(v2[i])) {
  168. continue;
  169. }
  170. d += (v1[i] - v2[i])*(v1[i] - v2[i]);
  171. }
  172. return sqrt(d);
  173. }
  174. static float vec_len(const float * v, size_t n) {
  175. double d = 0.0;
  176. for (size_t i = 0; i < n; i++) {
  177. if (std::isnan(v[i])) {
  178. return INFINITY;
  179. }
  180. if (std::isinf(v[i])) {
  181. continue;
  182. }
  183. d += v[i]*v[i];
  184. }
  185. return sqrt(d);
  186. }
  187. */
  188. // normalized mean squared error = mse(a, b) / mse(a, 0)
  189. static double nmse(const float * a, const float * b, size_t n) {
  190. double mse_a_b = 0.0;
  191. double mse_a_0 = 0.0;
  192. for (size_t i = 0; i < n; i++) {
  193. float a_i = a[i];
  194. float b_i = b[i];
  195. mse_a_b += (a_i - b_i) * (a_i - b_i);
  196. mse_a_0 += a_i * a_i;
  197. }
  198. return mse_a_b / mse_a_0;
  199. }
  200. // maximum absolute asymmetry between a and b
  201. // asymmetry: (a - b) / (a + b)
  202. // This is more stable than relative error if one of the values fluctuates towards zero.
  203. // n: number of values to compare.
  204. // expected_vals: optional vector of expected values for a. If expected_vals is not empty, filter out all comparisons where
  205. // a does not match any of the expected values. Needed for noncontinuous gradients where the numerical calculation can fail.
  206. static double mean_abs_asymm(const float * a, const float * b, const size_t n, const std::vector<float> & expected_vals) {
  207. double sum = 0.0f;
  208. size_t nvalid = 0;
  209. for (size_t i = 0; i < n; i++) {
  210. if (!expected_vals.empty()) {
  211. bool matches_any = false;
  212. for (const float & ev : expected_vals) {
  213. if (fabsf(a[i] - ev) < 1e-3f) {
  214. matches_any = true;
  215. break;
  216. }
  217. }
  218. if (!matches_any) {
  219. continue;
  220. }
  221. }
  222. const float asymm = (a[i] - b[i]) / (a[i] + b[i]);
  223. sum += fabsf(asymm);
  224. nvalid++;
  225. }
  226. return sum/nvalid;
  227. }
  228. // utils for printing the variables of the test cases
  229. #define VAR_TO_STR(x) (#x "=" + var_to_str(x))
  230. template<typename T>
  231. static std::string var_to_str(const T & x) {
  232. return std::to_string(x);
  233. }
  234. template<typename T, size_t N>
  235. static std::string var_to_str(const T (&x)[N]) {
  236. std::string s = "[";
  237. for (size_t i = 0; i < N; i++) {
  238. if (i > 0) {
  239. s += ",";
  240. }
  241. s += var_to_str(x[i]);
  242. }
  243. s += "]";
  244. return s;
  245. }
  246. template<typename T, size_t N>
  247. static std::string var_to_str(const std::array<T, N> & x) {
  248. std::string s = "[";
  249. for (size_t i = 0; i < N; i++) {
  250. if (i > 0) {
  251. s += ",";
  252. }
  253. s += var_to_str(x[i]);
  254. }
  255. s += "]";
  256. return s;
  257. }
  258. //static std::string var_to_str(ggml_unary_op unary_op) {
  259. // return ggml_unary_op_name(unary_op);
  260. //}
  261. static std::string var_to_str(ggml_type type) {
  262. return ggml_type_name(type);
  263. }
  264. static std::string var_to_str(ggml_op_pool pool) {
  265. switch (pool) {
  266. case GGML_OP_POOL_AVG: return "avg";
  267. case GGML_OP_POOL_MAX: return "max";
  268. default: return std::to_string(pool);
  269. }
  270. }
  271. #define VARS_TO_STR1(a) VAR_TO_STR(a)
  272. #define VARS_TO_STR2(a, b) VAR_TO_STR(a) + "," + VAR_TO_STR(b)
  273. #define VARS_TO_STR3(a, b, c) VAR_TO_STR(a) + "," + VARS_TO_STR2(b, c)
  274. #define VARS_TO_STR4(a, b, c, d) VAR_TO_STR(a) + "," + VARS_TO_STR3(b, c, d)
  275. #define VARS_TO_STR5(a, b, c, d, e) VAR_TO_STR(a) + "," + VARS_TO_STR4(b, c, d, e)
  276. #define VARS_TO_STR6(a, b, c, d, e, f) VAR_TO_STR(a) + "," + VARS_TO_STR5(b, c, d, e, f)
  277. #define VARS_TO_STR7(a, b, c, d, e, f, g) VAR_TO_STR(a) + "," + VARS_TO_STR6(b, c, d, e, f, g)
  278. #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)
  279. #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)
  280. #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)
  281. #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)
  282. #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)
  283. #ifdef GGML_USE_SYCL
  284. static bool inline _isinf(float f) {
  285. return (*(uint32_t *)&f & 0x7fffffff) == 0x7f800000;
  286. }
  287. #else
  288. static bool inline _isinf(float f) { return std::isinf(f); }
  289. #endif
  290. // accept FLT_MAX as infinity
  291. static bool isinf_or_max(float f) {
  292. return _isinf(f) || f == FLT_MAX || f == -FLT_MAX;
  293. }
  294. static bool ggml_is_view_op(enum ggml_op op) {
  295. return op == GGML_OP_VIEW || op == GGML_OP_RESHAPE || op == GGML_OP_PERMUTE || op == GGML_OP_TRANSPOSE;
  296. }
  297. enum test_mode {
  298. MODE_TEST,
  299. MODE_PERF,
  300. MODE_GRAD,
  301. };
  302. struct test_case {
  303. virtual ~test_case() {}
  304. virtual std::string op_desc(ggml_tensor * t) {
  305. return ggml_op_desc(t);
  306. }
  307. virtual std::string vars() {
  308. return "";
  309. }
  310. virtual ggml_tensor * build_graph(ggml_context * ctx) = 0;
  311. virtual double max_nmse_err() {
  312. return 1e-7;
  313. }
  314. virtual double max_maa_err() {
  315. return 1e-4;
  316. }
  317. virtual float grad_eps(){
  318. return 1e-1f;
  319. }
  320. // If false, estimate gradient with 2 points, neglects 3rd order derivative and higher.
  321. // If true, estimate gradient with 4 points, neglects 5th order derivative and higher.
  322. virtual bool grad_precise(){
  323. return false;
  324. }
  325. // Skip gradient checks if total number of gradients to be checked is larger than this (to speed up the tests).
  326. virtual int64_t grad_nmax() {
  327. return 10000;
  328. }
  329. // No effect if empty.
  330. // If not empty, skip all gradient checks where the numerical result does not match any of the values.
  331. // Needed for dealing with noncontinuous gradients (e.g. ReLU) where estimation using finite differences is unreliable.
  332. virtual std::vector<float> grad_expect() {
  333. return {};
  334. }
  335. virtual void initialize_tensors(ggml_context * ctx) {
  336. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
  337. init_tensor_uniform(t);
  338. }
  339. }
  340. virtual size_t op_size(ggml_tensor * t) {
  341. size_t size = ggml_nbytes(t);
  342. // add source tensors
  343. for (int i = 0; i < GGML_MAX_SRC; i++) {
  344. if (t->src[i] != NULL) {
  345. size += ggml_nbytes(t->src[i]);
  346. }
  347. }
  348. return size;
  349. }
  350. ggml_cgraph * gf = nullptr;
  351. ggml_cgraph * gb = nullptr;
  352. static const int sentinel_size = 1024;
  353. test_mode mode;
  354. std::vector<ggml_tensor *> sentinels;
  355. void add_sentinel(ggml_context * ctx) {
  356. if (mode == MODE_PERF || mode == MODE_GRAD) {
  357. return;
  358. }
  359. ggml_tensor * sentinel = ::ggml_new_tensor_1d(ctx, GGML_TYPE_F32, sentinel_size);
  360. ggml_format_name(sentinel, "sent_%zu", sentinels.size());
  361. sentinels.push_back(sentinel);
  362. }
  363. // hijack ggml_new_tensor to add sentinels after each tensor to check for overflows in the backend
  364. ggml_tensor * ggml_new_tensor(ggml_context * ctx, ggml_type type, int n_dims, const int64_t * ne) {
  365. ggml_tensor * t = ::ggml_new_tensor(ctx, type, n_dims, ne);
  366. add_sentinel(ctx);
  367. return t;
  368. }
  369. ggml_tensor * ggml_new_tensor_1d(ggml_context * ctx, ggml_type type, int64_t ne0) {
  370. ggml_tensor * t = ::ggml_new_tensor_1d(ctx, type, ne0);
  371. add_sentinel(ctx);
  372. return t;
  373. }
  374. ggml_tensor * ggml_new_tensor_2d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1) {
  375. ggml_tensor * t = ::ggml_new_tensor_2d(ctx, type, ne0, ne1);
  376. add_sentinel(ctx);
  377. return t;
  378. }
  379. ggml_tensor * ggml_new_tensor_3d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2) {
  380. ggml_tensor * t = ::ggml_new_tensor_3d(ctx, type, ne0, ne1, ne2);
  381. add_sentinel(ctx);
  382. return t;
  383. }
  384. ggml_tensor * ggml_new_tensor_4d(ggml_context * ctx, ggml_type type, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3) {
  385. ggml_tensor * t = ::ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3);
  386. add_sentinel(ctx);
  387. return t;
  388. }
  389. bool eval(ggml_backend_t backend1, ggml_backend_t backend2, const char * op_name) {
  390. mode = MODE_TEST;
  391. ggml_init_params params = {
  392. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead(),
  393. /* .mem_base = */ NULL,
  394. /* .no_alloc = */ true,
  395. };
  396. ggml_context * ctx = ggml_init(params);
  397. GGML_ASSERT(ctx);
  398. gf = ggml_new_graph(ctx);
  399. // pre-graph sentinel
  400. add_sentinel(ctx);
  401. ggml_tensor * out = build_graph(ctx);
  402. if (op_name != nullptr && op_desc(out) != op_name) {
  403. //printf(" %s: skipping\n", op_desc(out).c_str());
  404. ggml_free(ctx);
  405. return true;
  406. }
  407. printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  408. fflush(stdout);
  409. // check if the backends support the ops
  410. bool supported = true;
  411. for (ggml_backend_t backend : {backend1, backend2}) {
  412. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  413. if (!ggml_backend_supports_op(backend, t)) {
  414. printf("not supported [%s] ", ggml_backend_name(backend));
  415. supported = false;
  416. break;
  417. }
  418. }
  419. }
  420. if (!supported) {
  421. printf("\n");
  422. ggml_free(ctx);
  423. return true;
  424. }
  425. // post-graph sentinel
  426. add_sentinel(ctx);
  427. // allocate
  428. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend1);
  429. if (buf == NULL) {
  430. printf("failed to allocate tensors [%s] ", ggml_backend_name(backend1));
  431. ggml_free(ctx);
  432. return false;
  433. }
  434. // build graph
  435. ggml_build_forward_expand(gf, out);
  436. // add sentinels as graph nodes so that they are checked in the callback
  437. for (ggml_tensor * sentinel : sentinels) {
  438. gf->nodes[gf->n_nodes++] = sentinel;
  439. }
  440. // randomize tensors
  441. initialize_tensors(ctx);
  442. // compare
  443. struct callback_userdata {
  444. bool ok;
  445. double max_err;
  446. ggml_backend_t backend1;
  447. ggml_backend_t backend2;
  448. };
  449. callback_userdata ud {
  450. true,
  451. max_nmse_err(),
  452. backend1,
  453. backend2
  454. };
  455. auto callback = [](int index, ggml_tensor * t1, ggml_tensor * t2, void * user_data) -> bool {
  456. callback_userdata * ud = (callback_userdata *) user_data;
  457. const char * bn1 = ggml_backend_name(ud->backend1);
  458. const char * bn2 = ggml_backend_name(ud->backend2);
  459. if (t1->op == GGML_OP_NONE) {
  460. // sentinels must be unchanged
  461. std::vector<uint8_t> t1_data(ggml_nbytes(t1));
  462. std::vector<uint8_t> t2_data(ggml_nbytes(t2));
  463. ggml_backend_tensor_get(t1, t1_data.data(), 0, ggml_nbytes(t1));
  464. ggml_backend_tensor_get(t2, t2_data.data(), 0, ggml_nbytes(t2));
  465. if (memcmp(t1_data.data(), t2_data.data(), ggml_nbytes(t1)) != 0) {
  466. printf("sentinel mismatch: %s ", t1->name);
  467. ud->ok = false;
  468. return true;
  469. }
  470. }
  471. std::vector<float> f1 = tensor_to_float(t1);
  472. std::vector<float> f2 = tensor_to_float(t2);
  473. for (size_t i = 0; i < f1.size(); i++) {
  474. // check for nans
  475. if (std::isnan(f1[i]) || std::isnan(f2[i])) {
  476. printf("[%s] NaN at index %zu (%s=%f %s=%f) ", ggml_op_desc(t1), i, bn1, f1[i], bn2, f2[i]);
  477. ud->ok = false;
  478. return true;
  479. }
  480. // check for infs: both must be inf of the same sign, or both must be finite
  481. if (isinf_or_max(f1[i]) || isinf_or_max(f2[i])) {
  482. if (isinf_or_max(f1[i]) && isinf_or_max(f2[i])) {
  483. if (std::signbit(f1[i]) != std::signbit(f2[i])) {
  484. printf("[%s] inf sign mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]);
  485. ud->ok = false;
  486. return true;
  487. }
  488. } else {
  489. printf("[%s] inf mismatch: %s=%f %s=%f ", ggml_op_desc(t1), bn1, f1[i], bn2, f2[i]);
  490. ud->ok = false;
  491. return true;
  492. }
  493. }
  494. }
  495. double err = nmse(f1.data(), f2.data(), f1.size());
  496. if (err > ud->max_err) {
  497. printf("[%s] NMSE = %.9f > %.9f ", ggml_op_desc(t1), err, ud->max_err);
  498. //for (int i = 0; i < (int) f1.size(); i++) {
  499. // printf("%5d %9.6f %9.6f, diff = %9.6f\n", i, f1[i], f2[i], f1[i] - f2[i]);
  500. //}
  501. //printf("\n");
  502. //exit(1);
  503. ud->ok = false;
  504. }
  505. return true;
  506. GGML_UNUSED(index);
  507. };
  508. const bool cmp_ok = ggml_backend_compare_graph_backend(backend1, backend2, gf, callback, &ud);
  509. if (!cmp_ok) {
  510. printf("compare failed ");
  511. }
  512. ggml_backend_buffer_free(buf);
  513. ggml_free(ctx);
  514. if (ud.ok && cmp_ok) {
  515. printf("\033[1;32mOK\033[0m\n");
  516. return true;
  517. }
  518. printf("\033[1;31mFAIL\033[0m\n");
  519. return false;
  520. }
  521. bool eval_perf(ggml_backend_t backend, const char * op_name) {
  522. mode = MODE_PERF;
  523. static const size_t graph_nodes = 8192;
  524. ggml_init_params params = {
  525. /* .mem_size = */ ggml_tensor_overhead()*128 + ggml_graph_overhead_custom(graph_nodes, false),
  526. /* .mem_base = */ NULL,
  527. /* .no_alloc = */ true,
  528. };
  529. ggml_context * ctx = ggml_init(params);
  530. GGML_ASSERT(ctx);
  531. ggml_tensor * out = build_graph(ctx);
  532. if (op_name != nullptr && op_desc(out) != op_name) {
  533. //printf(" %s: skipping\n", op_desc(out).c_str());
  534. ggml_free(ctx);
  535. return true;
  536. }
  537. int len = printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  538. fflush(stdout);
  539. // check if backends support op
  540. if (!ggml_backend_supports_op(backend, out)) {
  541. printf("not supported\n");
  542. ggml_free(ctx);
  543. return true;
  544. }
  545. // align while also leaving some margin for variations in parameters
  546. int align = 20;
  547. int last = (len + align - 1) / align * align;
  548. if (last - len < 5) {
  549. last += align;
  550. }
  551. last = std::max(last, 60);
  552. printf("%*s", last - len, "");
  553. // allocate
  554. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend);
  555. if (buf == NULL) {
  556. printf("failed to allocate tensors\n");
  557. ggml_free(ctx);
  558. return false;
  559. }
  560. // randomize tensors
  561. initialize_tensors(ctx);
  562. // build graph
  563. ggml_cgraph * gf = ggml_new_graph_custom(ctx, graph_nodes, false);
  564. ggml_build_forward_expand(gf, out);
  565. // warmup run
  566. ggml_backend_graph_compute(backend, gf);
  567. // duplicate the op
  568. size_t target_size = ggml_backend_is_cpu(backend) ? 1ULL << 33 : 1ULL << 35; // 8 GB CPU, 32 GB GPU
  569. int n_runs = std::min((size_t)gf->size - gf->n_nodes, target_size / op_size(out)) + 1;
  570. for (int i = 1; i < n_runs; i++) {
  571. gf->nodes[gf->n_nodes++] = out;
  572. }
  573. // calculate memory
  574. size_t mem = n_runs * op_size(out);
  575. auto tensor_op_size = [](ggml_tensor * t) {
  576. size_t size = ggml_nbytes(t);
  577. // add source tensors
  578. for (int i = 0; i < GGML_MAX_SRC; i++) {
  579. if (t->src[i] != NULL) {
  580. size += ggml_nbytes(t->src[i]);
  581. }
  582. }
  583. return size;
  584. };
  585. for (int i = 0; i < gf->n_nodes; i++) {
  586. if (ggml_is_view_op(gf->nodes[i]->op) || gf->nodes[i] == out) {
  587. continue;
  588. }
  589. mem += tensor_op_size(gf->nodes[i]);
  590. }
  591. // run
  592. ggml_backend_synchronize(backend);
  593. int64_t start_time = ggml_time_us();
  594. ggml_backend_graph_compute(backend, gf);
  595. ggml_backend_synchronize(backend);
  596. int64_t end_time = ggml_time_us();
  597. double time_us = end_time - start_time;
  598. printf(" %5d runs - %8.2f us/run - %8zu kB/run - \033[1;34m%7.2f GB/s\033[0m\n",
  599. n_runs,
  600. time_us / n_runs,
  601. op_size(out) / 1024,
  602. mem / (time_us/1e6) / 1024.0 / 1024.0 / 1024.0);
  603. ggml_backend_buffer_free(buf);
  604. ggml_free(ctx);
  605. return true;
  606. }
  607. bool eval_grad(ggml_backend_t backend, const char * op_name) {
  608. mode = MODE_GRAD;
  609. const std::vector<float> expect = grad_expect();
  610. ggml_init_params params = {
  611. /* .mem_size = */ ggml_tensor_overhead()*128 + 2*ggml_graph_overhead_custom(GGML_DEFAULT_GRAPH_SIZE, true),
  612. /* .mem_base = */ NULL,
  613. /* .no_alloc = */ true,
  614. };
  615. ggml_context * ctx = ggml_init(params);
  616. GGML_ASSERT(ctx);
  617. gf = ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, true);
  618. gb = ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, true);
  619. ggml_tensor * out = build_graph(ctx);
  620. if (op_name != nullptr && op_desc(out) != op_name) {
  621. //printf(" %s: skipping\n", op_desc(out).c_str());
  622. ggml_free(ctx);
  623. return true;
  624. }
  625. printf(" %s(%s): ", op_desc(out).c_str(), vars().c_str());
  626. fflush(stdout);
  627. if (out->grad == nullptr) {
  628. printf("backwards pass not supported \n");
  629. ggml_free(ctx);
  630. return true;
  631. }
  632. if (out->type != GGML_TYPE_F32) {
  633. ggml_free(ctx);
  634. printf("not supported [%s->type != FP32]\n", out->name);
  635. return true;
  636. }
  637. // check if the backend supports the ops
  638. bool supported = true;
  639. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  640. if (!ggml_backend_supports_op(backend, t)) {
  641. printf("not supported [%s] ", ggml_backend_name(backend));
  642. supported = false;
  643. break;
  644. }
  645. if ((t->flags & GGML_TENSOR_FLAG_PARAM) && t->type != GGML_TYPE_F32) {
  646. printf("not supported [%s->type != FP32] ", t->name);
  647. supported = false;
  648. break;
  649. }
  650. }
  651. if (!supported) {
  652. printf("\n");
  653. ggml_free(ctx);
  654. return true;
  655. }
  656. int64_t ngrads = 0;
  657. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  658. if (t->flags & GGML_TENSOR_FLAG_PARAM) {
  659. ngrads += ggml_nelements(t);
  660. }
  661. }
  662. if (ngrads > grad_nmax()) {
  663. printf("skipping large tensors for speed \n");
  664. ggml_free(ctx);
  665. return true;
  666. }
  667. if (!ggml_is_scalar(out)) {
  668. out = ggml_sum(ctx, out);
  669. ggml_set_name(out, "sum_of_out");
  670. }
  671. ggml_build_forward_expand(gf, out);
  672. ggml_graph_cpy(gf, gb);
  673. ggml_build_backward_expand(ctx, gf, gb, false);
  674. if (expect.size() != 1 || expect[0] != 0.0f) {
  675. GGML_ASSERT(gb->n_nodes > gf->n_nodes);
  676. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  677. GGML_ASSERT(!(t->flags & GGML_TENSOR_FLAG_PARAM) || t->grad->op != GGML_OP_NONE);
  678. }
  679. }
  680. // TODO: refactor so that this check is only needed once
  681. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  682. if (!ggml_backend_supports_op(backend, t)) {
  683. printf("not supported [%s] ", ggml_backend_name(backend));
  684. supported = false;
  685. break;
  686. }
  687. if ((t->flags & GGML_TENSOR_FLAG_PARAM) && t->type != GGML_TYPE_F32) {
  688. printf("not supported [%s->type != FP32] ", t->name);
  689. supported = false;
  690. break;
  691. }
  692. }
  693. if (!supported) {
  694. printf("\n");
  695. ggml_free(ctx);
  696. return true;
  697. }
  698. // allocate
  699. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend);
  700. if (buf == NULL) {
  701. printf("failed to allocate tensors [%s] ", ggml_backend_name(backend));
  702. ggml_free(ctx);
  703. return false;
  704. }
  705. // randomize tensors
  706. initialize_tensors(ctx);
  707. for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
  708. if (!t->grad) {
  709. continue;
  710. }
  711. std::vector<float> tmp(ggml_nelements(t->grad));
  712. ggml_backend_tensor_set(t->grad, tmp.data(), 0, ggml_nbytes(t->grad));
  713. }
  714. // build graphs
  715. const float onef = 1.0f;
  716. ggml_backend_graph_compute(backend, gf);
  717. ggml_backend_tensor_set(out->grad, &onef, 0, ggml_nbytes(out->grad));
  718. ggml_backend_graph_compute(backend, gb);
  719. bool ok = true;
  720. for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
  721. if (!(t->flags & GGML_TENSOR_FLAG_PARAM)) {
  722. continue;
  723. }
  724. const char * bn = ggml_backend_name(backend);
  725. const int64_t ne = ggml_nelements(t);
  726. std::vector<float> ga = tensor_to_float(t->grad);
  727. for (int64_t i = 0; i < ne; ++i) { // gradient algebraic
  728. // check for nans
  729. if (!std::isfinite(ga[i])) {
  730. printf("[%s] nonfinite gradient at index %" PRId64 " (%s=%f) ", ggml_op_desc(t), i, bn, ga[i]);
  731. ok = false;
  732. break;
  733. }
  734. }
  735. if (!ok) {
  736. break;
  737. }
  738. std::vector<float> gn(ne); // gradient numeric
  739. GGML_ASSERT(ga.size() == gn.size());
  740. std::vector<float> x0 = tensor_to_float(t); // original t data
  741. GGML_ASSERT(ggml_is_scalar(out));
  742. GGML_ASSERT(out->type == GGML_TYPE_F32);
  743. const float eps = grad_eps();
  744. for (int64_t i = 0; i < ne; ++i) {
  745. const float xiu = x0[i] + 1.0f*eps; // x, index i, up
  746. const float xiuh = x0[i] + 0.5f*eps; // x, index i, up half
  747. const float xidh = x0[i] - 0.5f*eps; // x, index i, down half
  748. const float xid = x0[i] - 1.0f*eps; // x, index i, down
  749. float fu, fuh, fdh, fd; // output values for xiu, xiuh, xid, xidh
  750. ggml_backend_tensor_set(t, &xiu, i*sizeof(float), sizeof(float));
  751. ggml_backend_graph_compute(backend, gf);
  752. ggml_backend_tensor_get(out, &fu, 0, ggml_nbytes(out));
  753. ggml_backend_tensor_set(t, &xid, i*sizeof(float), sizeof(float));
  754. ggml_backend_graph_compute(backend, gf);
  755. ggml_backend_tensor_get(out, &fd, 0, ggml_nbytes(out));
  756. if (grad_precise()) {
  757. ggml_backend_tensor_set(t, &xiuh, i*sizeof(float), sizeof(float));
  758. ggml_backend_graph_compute(backend, gf);
  759. ggml_backend_tensor_get(out, &fuh, 0, ggml_nbytes(out));
  760. ggml_backend_tensor_set(t, &xidh, i*sizeof(float), sizeof(float));
  761. ggml_backend_graph_compute(backend, gf);
  762. ggml_backend_tensor_get(out, &fdh, 0, ggml_nbytes(out));
  763. gn[i] = (8.0*(double)fuh + (double)fd - (8.0*(double)fdh + (double)fu)) / (6.0*(double)eps);
  764. } else {
  765. gn[i] = (fu - fd) / (2.0f*eps);
  766. }
  767. ggml_backend_tensor_set(t, x0.data(), 0, ggml_nbytes(t));
  768. }
  769. const double err = mean_abs_asymm(gn.data(), ga.data(), gn.size(), expect);
  770. if (err > max_maa_err()) {
  771. printf("[%s] MAA = %.9f > %.9f ", ggml_op_desc(t), err, max_maa_err());
  772. ok = false;
  773. break;
  774. }
  775. if (!ok) {
  776. break;
  777. }
  778. }
  779. if (!ok) {
  780. printf("compare failed ");
  781. }
  782. ggml_backend_buffer_free(buf);
  783. ggml_free(ctx);
  784. if (ok) {
  785. printf("\033[1;32mOK\033[0m\n");
  786. return true;
  787. }
  788. printf("\033[1;31mFAIL\033[0m\n");
  789. return false;
  790. }
  791. };
  792. // ###################################
  793. // ## Section 2: GGML Op Defintions ##
  794. // ###################################
  795. // The following is an example showing the bare minimum for creating a test for a GGML op.
  796. // GGML_OP_EXAMPLE
  797. struct test_example : public test_case {
  798. // Always define these 2 or variants thereof:
  799. const ggml_type type; // The type of the input tensors.
  800. const std::array<int64_t, 4> ne; // The shape of the input tensors.
  801. // For some ops it's necessary to define multiple types or shapes for the inputs.
  802. // Or they may need additional parameters.
  803. // Put all parameters needed to fully define the test into one of the VARS_TO_STR macros.
  804. // In most cases these are just the properties of the struct that you defined above.
  805. // This is needed for info prints.
  806. std::string vars() override {
  807. return VARS_TO_STR2(type, ne);
  808. }
  809. // Define a constructor for the struct.
  810. // In most cases it will be sufficient to have the same arguments as the struct has properties
  811. // and just use initializer lists.
  812. test_example(ggml_type type = GGML_TYPE_F32,
  813. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  814. : type(type), ne(ne) {}
  815. // Define how a simple GGML compute graph can be constructed for the new GGML op.
  816. ggml_tensor * build_graph(ggml_context * ctx) override {
  817. // Step 1: create input tensors that don't depend on any other tensors:
  818. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  819. ggml_set_name(a, "a"); // Setting names is optional but it's useful for debugging.
  820. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data());
  821. ggml_set_name(b, "b");
  822. // Step 2: use the op that you want to test in the GGML compute graph.
  823. ggml_tensor * out = ggml_add(ctx, a, b); // For this example we're just doing a simple addition.
  824. ggml_set_name(out, "out");
  825. // Step 3: return the output tensor.
  826. return out;
  827. }
  828. // In order to also check the gradients for your op, add calls like ggml_set_param(ctx, a)
  829. // immediately after you create the tensors.
  830. // This is optional and only makes sense if a backwards pass has actually been implemented for the new op.
  831. };
  832. // GGML_OP_UNARY
  833. struct test_unary : public test_case {
  834. const ggml_unary_op op;
  835. const ggml_type type;
  836. const std::array<int64_t, 4> ne_a;
  837. int v; // view (1 : non-contiguous a)
  838. std::string vars() override {
  839. return VARS_TO_STR3(type, ne_a, v);
  840. }
  841. test_unary(ggml_unary_op op,
  842. ggml_type type = GGML_TYPE_F32,
  843. std::array<int64_t, 4> ne_a = {128, 2, 2, 2},
  844. int v = 0)
  845. : op(op), type(type), ne_a(ne_a), v(v) {}
  846. ggml_tensor * build_graph(ggml_context * ctx) override {
  847. const bool grad_supported = op == GGML_UNARY_OP_ABS || op == GGML_UNARY_OP_SGN || op == GGML_UNARY_OP_NEG ||
  848. op == GGML_UNARY_OP_STEP || op == GGML_UNARY_OP_RELU || op == GGML_UNARY_OP_SILU;
  849. ggml_tensor * a;
  850. if (v & 1) {
  851. auto ne = ne_a; ne[0] *= 3;
  852. a = ggml_new_tensor(ctx, type, 4, ne.data());
  853. if (grad_supported) {
  854. ggml_set_param(ctx, a);
  855. }
  856. ggml_set_name(a, "a");
  857. 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);
  858. ggml_set_name(a, "view_of_a");
  859. } else {
  860. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  861. if (grad_supported) {
  862. ggml_set_param(ctx, a);
  863. }
  864. ggml_set_name(a, "a");
  865. }
  866. ggml_tensor * out = ggml_unary(ctx, a, op);
  867. ggml_set_name(out, "out");
  868. return out;
  869. }
  870. void initialize_tensors(ggml_context * ctx) override {
  871. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  872. // test extended range of values to check for NaNs in GELU
  873. init_tensor_uniform(t, -150.f, 150.f);
  874. }
  875. }
  876. float grad_eps() override {
  877. return 15.0f;
  878. }
  879. std::vector<float> grad_expect() override {
  880. if (op == GGML_UNARY_OP_ABS) {
  881. return {-1.0f, 1.0f};
  882. }
  883. if (op == GGML_UNARY_OP_SGN || op == GGML_UNARY_OP_STEP) {
  884. return {0.0f};
  885. }
  886. if (op == GGML_UNARY_OP_RELU) {
  887. return {0.0f, 1.0f};
  888. }
  889. return {};
  890. }
  891. };
  892. // GGML_OP_GET_ROWS
  893. struct test_get_rows : public test_case {
  894. const ggml_type type;
  895. const int n; // cols
  896. const int m; // rows
  897. const int r; // rows to get
  898. const int b; // batch size
  899. const bool v; // view (non-contiguous src1)
  900. std::string vars() override {
  901. return VARS_TO_STR6(type, n, m, r, b, v);
  902. }
  903. test_get_rows(ggml_type type = GGML_TYPE_F32, int n = 10, int m = 5, int r = 3, int b = 1, bool v = false)
  904. : type(type), n(n), m(m), r(r), b(b), v(v) {}
  905. ggml_tensor * build_graph(ggml_context * ctx) override {
  906. ggml_tensor * in = ggml_new_tensor_3d(ctx, type, n, m, b);
  907. ggml_set_name(in, "in");
  908. ggml_tensor * rows = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, r, b);
  909. ggml_set_name(rows, "rows");
  910. if (v) {
  911. rows = ggml_view_2d(ctx, rows, r/2, b, rows->nb[1], 0);
  912. ggml_set_name(rows, "view_of_rows");
  913. }
  914. const bool grad_supported = ggml_is_matrix(in) && ggml_is_vector(rows);
  915. if (grad_supported) {
  916. ggml_set_param(ctx, in);
  917. // rows is a constant input -> no gradients
  918. }
  919. ggml_tensor * out = ggml_get_rows(ctx, in, rows);
  920. ggml_set_name(out, "out");
  921. return out;
  922. }
  923. void initialize_tensors(ggml_context * ctx) override {
  924. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  925. if (t->type == GGML_TYPE_I32) {
  926. if (ggml_is_view_op(t->op)) { continue; }
  927. // rows
  928. std::vector<int> data(r*b);
  929. for (int i = 0; i < r*b; i++) {
  930. data[i] = rand() % m;
  931. }
  932. ggml_backend_tensor_set(t, data.data(), 0, r * b * sizeof(int));
  933. } else {
  934. init_tensor_uniform(t);
  935. }
  936. }
  937. }
  938. };
  939. // GGML_OP_REPEAT
  940. struct test_repeat : public test_case {
  941. const ggml_type type;
  942. const std::array<int64_t, 4> ne;
  943. const std::array<int, 4> nr;
  944. std::string vars() override {
  945. return VARS_TO_STR3(type, ne, nr);
  946. }
  947. size_t op_size(ggml_tensor * t) override {
  948. return ggml_nbytes(t) * 2;
  949. }
  950. test_repeat(ggml_type type = GGML_TYPE_F32,
  951. std::array<int64_t, 4> ne = {10, 5, 4, 3},
  952. std::array<int, 4> nr = {2, 2, 2, 2})
  953. : type(type), ne(ne), nr(nr) {}
  954. ggml_tensor * build_graph(ggml_context * ctx) override {
  955. 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]);
  956. ggml_set_name(target, "target");
  957. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  958. ggml_set_param(ctx, src);
  959. ggml_set_name(src, "src");
  960. ggml_tensor * out = ggml_repeat(ctx, src, target);
  961. ggml_set_name(out, "out");
  962. return out;
  963. }
  964. };
  965. // GGML_OP_DUP
  966. struct test_dup : public test_case {
  967. const ggml_type type;
  968. const std::array<int64_t, 4> ne;
  969. const std::array<int64_t, 4> permute;
  970. bool _use_permute;
  971. std::string vars() override {
  972. std::string v = VARS_TO_STR2(type, ne);
  973. if (_use_permute) v += "," + VAR_TO_STR(permute);
  974. return v;
  975. }
  976. test_dup(ggml_type type = GGML_TYPE_F32,
  977. std::array<int64_t, 4> ne = {10, 10, 20, 1},
  978. std::array<int64_t, 4> permute = {0, 0, 0, 0})
  979. : type(type), ne(ne), permute(permute),
  980. _use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {}
  981. ggml_tensor * build_graph(ggml_context * ctx) override {
  982. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  983. ggml_set_param(ctx, src);
  984. ggml_set_name(src, "src");
  985. if (_use_permute) {
  986. src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]);
  987. ggml_set_name(src, "src_permuted");
  988. }
  989. ggml_tensor * out = ggml_dup(ctx, src);
  990. ggml_set_name(out, "out");
  991. return out;
  992. }
  993. };
  994. // GGML_OP_SET
  995. struct test_set : public test_case {
  996. const ggml_type type_src;
  997. const ggml_type type_dst;
  998. const std::array<int64_t, 4> ne;
  999. const int dim;
  1000. std::string vars() override {
  1001. return VARS_TO_STR4(type_src, type_dst, ne, dim);
  1002. }
  1003. size_t op_size(ggml_tensor * t) override {
  1004. return ggml_nbytes(t) + ggml_nbytes(t->src[0]);
  1005. }
  1006. test_set(ggml_type type_src = GGML_TYPE_F32, ggml_type type_dst = GGML_TYPE_F32,
  1007. std::array<int64_t, 4> ne = {6, 5, 4, 3}, int dim = 1)
  1008. : type_src(type_src), type_dst(type_dst), ne(ne), dim(dim) {}
  1009. ggml_tensor * build_graph(ggml_context * ctx) override {
  1010. ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne.data());
  1011. ggml_set_param(ctx, src);
  1012. ggml_set_name(src, "src");
  1013. auto ne_dst = ne;
  1014. for (int i = 0; i < dim; ++i) {
  1015. ne_dst[i] *= 2;
  1016. }
  1017. ggml_tensor* dst = ggml_new_tensor(ctx, type_dst, 4, ne_dst.data());
  1018. ggml_set_param(ctx, dst);
  1019. ggml_set_name(dst, "dst");
  1020. size_t offset = 0;
  1021. for (int i = 0; i < dim; ++i) {
  1022. offset += ((ne_dst[i] - ne[i])/2)*dst->nb[i];
  1023. }
  1024. ggml_tensor * out = ggml_set(ctx, dst, src,
  1025. // The backwards pass requires setting a contiguous region:
  1026. src->nb[1], src->nb[2], src->nb[3], offset);
  1027. ggml_set_name(out, "out");
  1028. return out;
  1029. }
  1030. };
  1031. // GGML_OP_CPY
  1032. struct test_cpy : public test_case {
  1033. const ggml_type type_src;
  1034. const ggml_type type_dst;
  1035. const std::array<int64_t, 4> ne;
  1036. const std::array<int64_t, 4> permute;
  1037. bool _src_use_permute;
  1038. std::string vars() override {
  1039. return VARS_TO_STR4(type_src, type_dst, ne, permute);
  1040. }
  1041. double max_nmse_err() override {
  1042. return 1e-6;
  1043. }
  1044. size_t op_size(ggml_tensor * t) override {
  1045. return ggml_nbytes(t) + ggml_nbytes(t->src[0]);
  1046. }
  1047. test_cpy(ggml_type type_src = GGML_TYPE_F32, ggml_type type_dst = GGML_TYPE_F32,
  1048. std::array<int64_t, 4> ne = {10, 10, 10, 1},
  1049. std::array<int64_t, 4> permute = {0, 0, 0, 0})
  1050. : type_src(type_src), type_dst(type_dst), ne(ne), permute(permute),
  1051. _src_use_permute(permute[0] + permute[1] + permute[2] + permute[3] > 0) {}
  1052. ggml_tensor * build_graph(ggml_context * ctx) override {
  1053. ggml_tensor * src = ggml_new_tensor(ctx, type_src, 4, ne.data());
  1054. ggml_set_param(ctx, src);
  1055. ggml_set_name(src, "src");
  1056. if (_src_use_permute) {
  1057. src = ggml_permute(ctx, src, permute[0], permute[1], permute[2], permute[3]);
  1058. ggml_set_name(src, "src_permuted");
  1059. }
  1060. ggml_tensor* dst = ggml_new_tensor(ctx, type_dst, 4, src->ne);
  1061. ggml_set_name(dst, "dst");
  1062. ggml_tensor * out = ggml_cpy(ctx, src, dst);
  1063. ggml_set_name(out, "out");
  1064. return out;
  1065. }
  1066. };
  1067. // GGML_OP_CONT
  1068. struct test_cont : public test_case {
  1069. const ggml_type type;
  1070. const std::array<int64_t, 4> ne;
  1071. std::string vars() override {
  1072. return VARS_TO_STR2(type, ne);
  1073. }
  1074. test_cont(ggml_type type = GGML_TYPE_F32,
  1075. std::array<int64_t, 4> ne = {10, 10, 10, 1})
  1076. : type(type), ne(ne) {}
  1077. ggml_tensor * build_graph(ggml_context * ctx) override {
  1078. ggml_tensor * src = ggml_new_tensor(ctx, type, 4, ne.data());
  1079. ggml_set_param(ctx, src);
  1080. ggml_set_name(src, "src");
  1081. src = ggml_transpose(ctx, src);
  1082. ggml_set_name(src, "src_transposed");
  1083. ggml_tensor * out = ggml_cont(ctx, src);
  1084. ggml_set_name(out, "out");
  1085. return out;
  1086. }
  1087. };
  1088. // GGML_OP_ADD
  1089. // GGML_OP_MUL
  1090. // GGML_OP_DIV
  1091. struct test_bin_bcast : public test_case {
  1092. using op_t = ggml_tensor * (*) (ggml_context *, ggml_tensor *, ggml_tensor *);
  1093. op_t op;
  1094. const ggml_type type;
  1095. const std::array<int64_t, 4> ne;
  1096. const std::array<int, 4> nr;
  1097. std::string vars() override {
  1098. return VARS_TO_STR3(type, ne, nr);
  1099. }
  1100. size_t op_size(ggml_tensor * t) override {
  1101. return ggml_nbytes(t) * 3;
  1102. }
  1103. test_bin_bcast(op_t op, ggml_type type = GGML_TYPE_F32,
  1104. std::array<int64_t, 4> ne = {10, 10, 1, 1},
  1105. std::array<int, 4> nr = {1, 2, 1, 1})
  1106. : op(op), type(type), ne(ne), nr(nr) {}
  1107. ggml_tensor * build_graph(ggml_context * ctx) override {
  1108. 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]);
  1109. ggml_set_name(a, "a");
  1110. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne.data());
  1111. ggml_set_name(b, "b");
  1112. // The backwards pass supports broadcasting only for GGML_ADD:
  1113. const bool grad_supported = op == ggml_add || ggml_are_same_shape(a, b);
  1114. if (grad_supported) {
  1115. ggml_set_param(ctx, a);
  1116. ggml_set_param(ctx, b);
  1117. }
  1118. ggml_tensor * out = op(ctx, a, b);
  1119. ggml_set_name(out, "out");
  1120. return out;
  1121. }
  1122. void initialize_tensors(ggml_context * ctx) override {
  1123. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1124. if (op == ggml_mul || op == ggml_div) {
  1125. // MUL and DIV have numerical issues around zero:
  1126. init_tensor_uniform(t, 0.9f, 1.1f);
  1127. } else {
  1128. init_tensor_uniform(t);
  1129. }
  1130. }
  1131. }
  1132. float grad_eps() override {
  1133. return 0.1f * (op == ggml_mul ? ne[0]*ne[1]*ne[2]*ne[3] : 1);
  1134. }
  1135. bool grad_precise() override {
  1136. return op == ggml_div;
  1137. }
  1138. double max_maa_err() override {
  1139. return op == ggml_add ? 1e-4 : 1e-3;
  1140. }
  1141. };
  1142. // GGML_OP_ADD1
  1143. struct test_add1 : public test_case {
  1144. const ggml_type type;
  1145. const std::array<int64_t, 4> ne;
  1146. std::string vars() override {
  1147. return VARS_TO_STR2(type, ne);
  1148. }
  1149. test_add1(ggml_type type = GGML_TYPE_F32,
  1150. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1151. : type(type), ne(ne) {}
  1152. ggml_tensor * build_graph(ggml_context * ctx) override {
  1153. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1154. ggml_set_param(ctx, a);
  1155. ggml_set_name(a, "a");
  1156. ggml_tensor * b = ggml_new_tensor_1d(ctx, type, 1);
  1157. // ggml_set_param(ctx, b); // TODO: implement
  1158. ggml_set_name(b, "b");
  1159. ggml_tensor * out = ggml_add1(ctx, a, b);
  1160. ggml_set_name(out, "out");
  1161. return out;
  1162. }
  1163. float grad_eps() override {
  1164. return 0.1f * ne[0]*ne[1]*ne[2]*ne[3];
  1165. }
  1166. };
  1167. // GGML_OP_SCALE
  1168. struct test_scale : public test_case {
  1169. const ggml_type type;
  1170. const std::array<int64_t, 4> ne;
  1171. float scale;
  1172. std::string vars() override {
  1173. return VARS_TO_STR3(type, ne, scale);
  1174. }
  1175. test_scale(ggml_type type = GGML_TYPE_F32,
  1176. std::array<int64_t, 4> ne = {10, 10, 10, 10},
  1177. float scale = 2.0f)
  1178. : type(type), ne(ne), scale(scale) {}
  1179. ggml_tensor * build_graph(ggml_context * ctx) override {
  1180. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1181. ggml_set_param(ctx, a);
  1182. ggml_set_name(a, "a");
  1183. ggml_tensor * out = ggml_scale(ctx, a, scale);
  1184. ggml_set_name(out, "out");
  1185. return out;
  1186. }
  1187. };
  1188. // GGML_OP_NORM
  1189. struct test_norm : public test_case {
  1190. const ggml_type type;
  1191. const std::array<int64_t, 4> ne;
  1192. float eps;
  1193. std::string vars() override {
  1194. return VARS_TO_STR3(type, ne, eps);
  1195. }
  1196. test_norm(ggml_type type = GGML_TYPE_F32,
  1197. std::array<int64_t, 4> ne = {64, 5, 4, 3},
  1198. float eps = 1e-6f)
  1199. : type(type), ne(ne), eps(eps) {}
  1200. ggml_tensor * build_graph(ggml_context * ctx) override {
  1201. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1202. ggml_set_name(a, "a");
  1203. ggml_tensor * out = ggml_norm(ctx, a, eps);
  1204. ggml_set_name(out, "out");
  1205. return out;
  1206. }
  1207. };
  1208. // GGML_OP_RMS_NORM
  1209. struct test_rms_norm : public test_case {
  1210. const ggml_type type;
  1211. const std::array<int64_t, 4> ne;
  1212. float eps;
  1213. std::string vars() override {
  1214. return VARS_TO_STR3(type, ne, eps);
  1215. }
  1216. test_rms_norm(ggml_type type = GGML_TYPE_F32,
  1217. std::array<int64_t, 4> ne = {64, 5, 4, 3},
  1218. float eps = 1e-6f)
  1219. : type(type), ne(ne), eps(eps) {}
  1220. ggml_tensor * build_graph(ggml_context * ctx) override {
  1221. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1222. ggml_set_param(ctx, a);
  1223. ggml_set_name(a, "a");
  1224. ggml_tensor * out = ggml_rms_norm(ctx, a, eps);
  1225. ggml_set_name(out, "out");
  1226. return out;
  1227. }
  1228. bool grad_precise() override {
  1229. return true;
  1230. }
  1231. };
  1232. // GGML_OP_SSM_CONV
  1233. struct test_ssm_conv : public test_case {
  1234. const ggml_type type;
  1235. const std::array<int64_t, 4> ne_a;
  1236. const std::array<int64_t, 4> ne_b;
  1237. std::string vars() override {
  1238. return VARS_TO_STR3(type, ne_a, ne_b);
  1239. }
  1240. test_ssm_conv(ggml_type type = GGML_TYPE_F32,
  1241. std::array<int64_t, 4> ne_a = {10, 10, 10, 1},
  1242. std::array<int64_t, 4> ne_b = {3, 3, 1, 1})
  1243. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1244. ggml_tensor * build_graph(ggml_context * ctx) override {
  1245. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1246. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1247. ggml_tensor * out = ggml_ssm_conv(ctx, a, b);
  1248. return out;
  1249. }
  1250. };
  1251. // GGML_OP_SSM_SCAN
  1252. struct test_ssm_scan : public test_case {
  1253. const ggml_type type;
  1254. const int64_t d_state;
  1255. const int64_t d_inner;
  1256. const int64_t n_seq_tokens;
  1257. const int64_t n_seqs;
  1258. std::string vars() override {
  1259. return VARS_TO_STR5(type, d_state, d_inner, n_seq_tokens, n_seqs);
  1260. }
  1261. test_ssm_scan(ggml_type type = GGML_TYPE_F32,
  1262. int64_t d_state = 32, int64_t d_inner = 32, int64_t n_seq_tokens = 32, int64_t n_seqs = 32)
  1263. : type(type), d_state(d_state), d_inner(d_inner), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs) {}
  1264. ggml_tensor * build_graph(ggml_context * ctx) override {
  1265. ggml_tensor * s = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_state, d_inner, n_seqs, 1 }.data());
  1266. ggml_tensor * x = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_inner, n_seq_tokens, n_seqs, 1 }.data());
  1267. ggml_tensor * dt = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_inner, n_seq_tokens, n_seqs, 1 }.data());
  1268. ggml_tensor * A = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_state, d_inner, 1 , 1 }.data());
  1269. ggml_tensor * B = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_state, n_seq_tokens, n_seqs, 1 }.data());
  1270. ggml_tensor * C = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ d_state, n_seq_tokens, n_seqs, 1 }.data());
  1271. ggml_tensor * out = ggml_ssm_scan(ctx, s, x, dt, A, B, C);
  1272. return out;
  1273. }
  1274. };
  1275. // GGML_OP_MUL_MAT
  1276. struct test_mul_mat : public test_case {
  1277. const ggml_type type_a;
  1278. const ggml_type type_b;
  1279. const int64_t m;
  1280. const int64_t n;
  1281. const int64_t k;
  1282. const std::array<int64_t, 2> bs; // dims 3 and 4
  1283. const std::array<int64_t, 2> nr; // repeat in dims 3 and 4
  1284. std::string vars() override {
  1285. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, nr);
  1286. }
  1287. double max_nmse_err() override {
  1288. return 5e-4;
  1289. }
  1290. size_t op_size(ggml_tensor * t) override {
  1291. size_t a = ggml_nbytes(t->src[0]) * n * nr[0] * nr[1];
  1292. size_t b = ggml_nbytes(t->src[1]) * m;
  1293. size_t c = ggml_nbytes(t);
  1294. return a + b + c;
  1295. GGML_UNUSED(t);
  1296. }
  1297. test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  1298. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  1299. std::array<int64_t, 2> bs = {10, 10},
  1300. std::array<int64_t, 2> nr = {2, 2})
  1301. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr) {}
  1302. ggml_tensor * build_graph(ggml_context * ctx) override {
  1303. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  1304. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, k, m, bs[0] , bs[1]);
  1305. ggml_tensor * b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]);
  1306. ggml_set_param(ctx, a);
  1307. ggml_set_param(ctx, b);
  1308. ggml_set_name(a, "a");
  1309. ggml_set_name(b, "b");
  1310. ggml_tensor * out = ggml_mul_mat(ctx, a, b);
  1311. ggml_set_name(out, "out");
  1312. return out;
  1313. }
  1314. };
  1315. // GGML_OP_MUL_MAT_ID
  1316. struct test_mul_mat_id : public test_case {
  1317. const ggml_type type_a;
  1318. const ggml_type type_b;
  1319. const int n_mats;
  1320. const int n_used;
  1321. const bool b; // brodcast b matrix
  1322. const int64_t m;
  1323. const int64_t n;
  1324. const int64_t k;
  1325. std::string vars() override {
  1326. return VARS_TO_STR8(type_a, type_b, n_mats, n_used, b, m, n, k);
  1327. }
  1328. double max_nmse_err() override {
  1329. return 5e-4;
  1330. }
  1331. size_t op_size(ggml_tensor * t) override {
  1332. size_t a = ggml_nbytes(t->src[2]) * n;
  1333. size_t b = ggml_nbytes(t->src[1]) * m;
  1334. size_t c = ggml_nbytes(t);
  1335. return a + b + c;
  1336. GGML_UNUSED(t);
  1337. }
  1338. test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  1339. int n_mats = 8, int n_used = 2, bool b = false,
  1340. int64_t m = 32, int64_t n = 32, int64_t k = 32)
  1341. : type_a(type_a), type_b(type_b), n_mats(n_mats), n_used(n_used), b(b),
  1342. m(m), n(n), k(k) {
  1343. GGML_ASSERT(n_used <= n_mats);
  1344. }
  1345. ggml_tensor * build_graph(ggml_context * ctx) override {
  1346. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  1347. ggml_tensor * as = ggml_new_tensor_3d(ctx, type_a, k, m, n_mats);
  1348. ggml_set_name(as, "as");
  1349. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n);
  1350. ggml_set_name(ids, "ids");
  1351. if (n_used != n_mats) {
  1352. ids = ggml_view_2d(ctx, ids, n_used, n, ids->nb[1], 0);
  1353. ggml_set_name(ids, "view_of_ids");
  1354. }
  1355. ggml_tensor * b = ggml_new_tensor_3d(ctx, type_b, k, this->b ? 1 : n_used, n);
  1356. ggml_set_name(b, "b");
  1357. ggml_tensor * out = ggml_mul_mat_id(ctx, as, b, ids);
  1358. ggml_set_name(out, "out");
  1359. return out;
  1360. }
  1361. void initialize_tensors(ggml_context * ctx) override {
  1362. std::random_device rd;
  1363. std::default_random_engine rng(rd());
  1364. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1365. if (t->type == GGML_TYPE_I32) {
  1366. if (ggml_is_view_op(t->op)) { continue; }
  1367. // ids
  1368. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1369. std::vector<int32_t> data(t->ne[0]);
  1370. for (int i = 0; i < t->ne[0]; i++) {
  1371. data[i] = i % n_mats;
  1372. }
  1373. std::shuffle(data.begin(), data.end(), rng);
  1374. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t));
  1375. }
  1376. } else {
  1377. init_tensor_uniform(t);
  1378. }
  1379. }
  1380. }
  1381. };
  1382. // GGML_OP_SQR
  1383. struct test_sqr : public test_case {
  1384. const ggml_type type;
  1385. const std::array<int64_t, 4> ne;
  1386. std::string vars() override {
  1387. return VARS_TO_STR2(type, ne);
  1388. }
  1389. test_sqr(ggml_type type = GGML_TYPE_F32,
  1390. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1391. : type(type), ne(ne) {}
  1392. ggml_tensor * build_graph(ggml_context * ctx) override {
  1393. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1394. ggml_set_param(ctx, a);
  1395. ggml_set_name(a, "a");
  1396. ggml_tensor * out = ggml_sqr(ctx, a);
  1397. ggml_set_name(out, "out");
  1398. return out;
  1399. }
  1400. float grad_eps() override {
  1401. return 0.1f * 0.25f*ne[0]*ne[1]*ne[2]*ne[3]; // 10% of expected value of sum.
  1402. }
  1403. };
  1404. // GGML_OP_SQRT
  1405. struct test_sqrt : public test_case {
  1406. const ggml_type type;
  1407. const std::array<int64_t, 4> ne;
  1408. std::string vars() override {
  1409. return VARS_TO_STR2(type, ne);
  1410. }
  1411. test_sqrt(ggml_type type = GGML_TYPE_F32,
  1412. std::array<int64_t, 4> ne = {10, 3, 3, 2})
  1413. : type(type), ne(ne) {}
  1414. ggml_tensor * build_graph(ggml_context * ctx) override {
  1415. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1416. ggml_set_param(ctx, a);
  1417. ggml_set_name(a, "a");
  1418. ggml_tensor * out = ggml_sqrt(ctx, a);
  1419. ggml_set_name(out, "out");
  1420. return out;
  1421. }
  1422. void initialize_tensors(ggml_context * ctx) override {
  1423. // fill with positive values
  1424. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1425. init_tensor_uniform(t, 50.0f, 100.0f);
  1426. }
  1427. }
  1428. float grad_eps() override {
  1429. return 20.0f;
  1430. }
  1431. bool grad_precise() override {
  1432. return true;
  1433. }
  1434. };
  1435. // GGML_OP_LOG
  1436. struct test_log : public test_case {
  1437. const ggml_type type;
  1438. const std::array<int64_t, 4> ne;
  1439. std::string vars() override {
  1440. return VARS_TO_STR2(type, ne);
  1441. }
  1442. test_log(ggml_type type = GGML_TYPE_F32,
  1443. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1444. : type(type), ne(ne) {}
  1445. ggml_tensor * build_graph(ggml_context * ctx) override {
  1446. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1447. ggml_set_param(ctx, a);
  1448. ggml_set_name(a, "a");
  1449. ggml_tensor * out = ggml_log(ctx, a);
  1450. ggml_set_name(out, "out");
  1451. return out;
  1452. }
  1453. void initialize_tensors(ggml_context * ctx) override {
  1454. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1455. // log(1) == 0, cluster values there to keep the sum low for better precision in the backwards pass:
  1456. init_tensor_uniform(t, 0.9f, 1.1f);
  1457. }
  1458. }
  1459. bool grad_precise() override {
  1460. return true;
  1461. }
  1462. };
  1463. // GGML_OP_SIN
  1464. struct test_sin : public test_case {
  1465. const ggml_type type;
  1466. const std::array<int64_t, 4> ne;
  1467. std::string vars() override {
  1468. return VARS_TO_STR2(type, ne);
  1469. }
  1470. test_sin(ggml_type type = GGML_TYPE_F32,
  1471. std::array<int64_t, 4> ne = {10, 2, 2, 2})
  1472. : type(type), ne(ne) {}
  1473. ggml_tensor * build_graph(ggml_context * ctx) override {
  1474. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1475. ggml_set_param(ctx, a);
  1476. ggml_set_name(a, "a");
  1477. ggml_tensor * out = ggml_sin(ctx, a);
  1478. ggml_set_name(out, "out");
  1479. return out;
  1480. }
  1481. void initialize_tensors(ggml_context * ctx) override {
  1482. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1483. init_tensor_uniform(t, -6.5f, 6.5f); // Covers interval [-2*pi, 2*pi].
  1484. }
  1485. }
  1486. double max_maa_err() override {
  1487. return 1e-3;
  1488. }
  1489. float grad_eps() override {
  1490. return 0.2f;
  1491. }
  1492. bool grad_precise() override {
  1493. return true;
  1494. }
  1495. };
  1496. // GGML_OP_COS
  1497. struct test_cos : public test_case {
  1498. const ggml_type type;
  1499. const std::array<int64_t, 4> ne;
  1500. std::string vars() override {
  1501. return VARS_TO_STR2(type, ne);
  1502. }
  1503. test_cos(ggml_type type = GGML_TYPE_F32,
  1504. std::array<int64_t, 4> ne = {10, 2, 2, 2})
  1505. : type(type), ne(ne) {}
  1506. ggml_tensor * build_graph(ggml_context * ctx) override {
  1507. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1508. ggml_set_param(ctx, a);
  1509. ggml_set_name(a, "a");
  1510. ggml_tensor * out = ggml_cos(ctx, a);
  1511. ggml_set_name(out, "out");
  1512. return out;
  1513. }
  1514. void initialize_tensors(ggml_context * ctx) override {
  1515. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1516. init_tensor_uniform(t, -6.5f, 6.5f); // Covers interval [-2*pi, 2*pi].
  1517. }
  1518. }
  1519. double max_maa_err() override {
  1520. return 1e-3;
  1521. }
  1522. float grad_eps() override {
  1523. return 0.2f;
  1524. }
  1525. bool grad_precise() override {
  1526. return true;
  1527. }
  1528. };
  1529. // GGML_OP_CLAMP
  1530. struct test_clamp : public test_case {
  1531. const ggml_type type;
  1532. const std::array<int64_t, 4> ne;
  1533. float min;
  1534. float max;
  1535. std::string vars() override {
  1536. return VARS_TO_STR4(type, ne, min, max);
  1537. }
  1538. test_clamp(ggml_type type = GGML_TYPE_F32,
  1539. std::array<int64_t, 4> ne = {10, 5, 4, 3},
  1540. float min = -0.5f, float max = 0.5f)
  1541. : type(type), ne(ne), min(min), max(max) {}
  1542. ggml_tensor * build_graph(ggml_context * ctx) override {
  1543. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1544. ggml_set_name(a, "a");
  1545. ggml_tensor * out = ggml_clamp(ctx, a, min, max);
  1546. ggml_set_name(out, "out");
  1547. return out;
  1548. }
  1549. float grad_eps() override {
  1550. return 1e-2f;
  1551. }
  1552. std::vector<float> grad_expect() override {
  1553. return {0.0f, 1.0f};
  1554. }
  1555. };
  1556. // GGML_OP_DIAG_MASK_INF
  1557. struct test_diag_mask_inf : public test_case {
  1558. const ggml_type type;
  1559. const std::array<int64_t, 4> ne;
  1560. const int n_past;
  1561. std::string vars() override {
  1562. return VARS_TO_STR3(type, ne, n_past);
  1563. }
  1564. test_diag_mask_inf(ggml_type type = GGML_TYPE_F32,
  1565. std::array<int64_t, 4> ne = {10, 10, 3, 2},
  1566. int n_past = 5)
  1567. : type(type), ne(ne), n_past(n_past) {}
  1568. ggml_tensor * build_graph(ggml_context * ctx) override {
  1569. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1570. ggml_set_param(ctx, a);
  1571. ggml_set_name(a, "a");
  1572. ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past);
  1573. ggml_set_name(out, "out");
  1574. return out;
  1575. }
  1576. };
  1577. // GGML_OP_SOFT_MAX
  1578. struct test_soft_max : public test_case {
  1579. const ggml_type type;
  1580. const std::array<int64_t, 4> ne;
  1581. const bool mask;
  1582. const float scale;
  1583. const float max_bias;
  1584. std::string vars() override {
  1585. return VARS_TO_STR5(type, ne, mask, scale, max_bias);
  1586. }
  1587. // the 1024 test with bias occasionally fails:
  1588. // 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
  1589. virtual double max_nmse_err() override {
  1590. return 1e-6;
  1591. }
  1592. test_soft_max(ggml_type type = GGML_TYPE_F32,
  1593. std::array<int64_t, 4> ne = {10, 5, 4, 3},
  1594. bool mask = false,
  1595. float scale = 1.0f,
  1596. float max_bias = 0.0f)
  1597. : type(type), ne(ne), mask(mask), scale(scale), max_bias(max_bias) {}
  1598. ggml_tensor * build_graph(ggml_context * ctx) override {
  1599. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1600. ggml_set_param(ctx, a);
  1601. ggml_set_name(a, "a");
  1602. ggml_tensor * mask = nullptr;
  1603. if (this->mask) {
  1604. mask = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ne[0], ne[1]);
  1605. ggml_set_name(mask, "mask");
  1606. }
  1607. ggml_tensor * out = ggml_soft_max_ext(ctx, a, mask, scale, max_bias);
  1608. ggml_set_name(out, "out");
  1609. return out;
  1610. }
  1611. bool grad_precise() override {
  1612. return true;
  1613. }
  1614. };
  1615. // GGML_OP_ROPE
  1616. struct test_rope : public test_case {
  1617. const ggml_type type;
  1618. const std::array<int64_t, 4> ne_a;
  1619. int n_dims;
  1620. int mode;
  1621. int n_ctx; // used to generate positions
  1622. float fs; // freq_scale
  1623. float ef; // ext_factor
  1624. float af; // attn_factor
  1625. bool ff;
  1626. int v; // view (1 : non-contiguous a)
  1627. std::string vars() override {
  1628. return VARS_TO_STR10(type, ne_a, n_dims, mode, n_ctx, fs, ef, af, ff, v);
  1629. }
  1630. test_rope(ggml_type type = GGML_TYPE_F32,
  1631. std::array<int64_t, 4> ne_a = {10, 5, 3, 1},
  1632. 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)
  1633. : 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) {}
  1634. ggml_tensor * build_graph(ggml_context * ctx) override {
  1635. ggml_tensor * a;
  1636. if (v & 1) {
  1637. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1638. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1639. ggml_set_param(ctx, a);
  1640. ggml_set_name(a, "a");
  1641. 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);
  1642. ggml_set_name(a, "view_of_a");
  1643. } else {
  1644. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1645. ggml_set_param(ctx, a);
  1646. ggml_set_name(a, "a");
  1647. }
  1648. ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2]);
  1649. ggml_set_name(pos, "pos");
  1650. ggml_tensor * freq = nullptr;
  1651. if (ff) {
  1652. freq = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_dims/2);
  1653. ggml_set_name(freq, "freq");
  1654. }
  1655. ggml_tensor * out = ggml_rope_ext(ctx, a, pos, freq, n_dims, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f);
  1656. ggml_set_name(out, "out");
  1657. return out;
  1658. }
  1659. void initialize_tensors(ggml_context * ctx) override {
  1660. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1661. if (t->type == GGML_TYPE_I32) {
  1662. // pos
  1663. std::vector<int> data(ne_a[2]);
  1664. for (int i = 0; i < ne_a[2]; i++) {
  1665. data[i] = rand() % n_ctx;
  1666. }
  1667. ggml_backend_tensor_set(t, data.data(), 0, ne_a[2] * sizeof(int));
  1668. } else {
  1669. if (t->ne[0] == n_dims/2) {
  1670. // frequency factors in the range [0.9f, 1.1f]
  1671. init_tensor_uniform(t, 0.9f, 1.1f);
  1672. } else {
  1673. init_tensor_uniform(t);
  1674. }
  1675. }
  1676. }
  1677. }
  1678. double max_maa_err() override {
  1679. return 1e-3;
  1680. }
  1681. bool grad_precise() override {
  1682. return true;
  1683. }
  1684. };
  1685. // GGML_OP_POOL2D
  1686. struct test_pool2d : public test_case {
  1687. enum ggml_op_pool pool_type;
  1688. const ggml_type type_input;
  1689. const std::array<int64_t, 4> ne_input;
  1690. // kernel size
  1691. const int k0;
  1692. const int k1;
  1693. // stride
  1694. const int s0;
  1695. const int s1;
  1696. // padding
  1697. const int p0;
  1698. const int p1;
  1699. std::string vars() override {
  1700. return VARS_TO_STR9(pool_type, type_input, ne_input, k0, k1, s0, s1, p0, p1);
  1701. }
  1702. test_pool2d(ggml_op_pool pool_type = GGML_OP_POOL_AVG,
  1703. ggml_type type_input = GGML_TYPE_F32,
  1704. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1705. int k0 = 3, int k1 = 3,
  1706. int s0 = 1, int s1 = 1,
  1707. int p0 = 1, int p1 = 1)
  1708. : pool_type(pool_type), type_input(type_input), ne_input(ne_input), k0(k0), k1(k1), s0(s0), s1(s1), p0(p0), p1(p1) {}
  1709. ggml_tensor * build_graph(ggml_context * ctx) override {
  1710. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1711. ggml_set_param(ctx, input);
  1712. ggml_set_name(input, "input");
  1713. ggml_tensor * out = ggml_pool_2d(ctx, input, pool_type, k0, k1, s0, s1, p0, p1);
  1714. ggml_set_name(out, "out");
  1715. return out;
  1716. }
  1717. };
  1718. // GGML_OP_CONV_TRANSPOSE_1D
  1719. struct test_conv_transpose_1d : public test_case {
  1720. const std::array<int64_t, 4> ne_input;
  1721. const std::array<int64_t, 4> ne_kernel;
  1722. const int s0; // stride
  1723. const int p0; // padding
  1724. const int d0; // dilation
  1725. std::string vars() override {
  1726. return VARS_TO_STR5(ne_input, ne_kernel, s0, p0, d0);
  1727. }
  1728. test_conv_transpose_1d(std::array<int64_t, 4> ne_input = {197, 32, 1, 1}, // [input_width, input_height, input_channels, 1]
  1729. std::array<int64_t, 4> ne_kernel = {16, 32, 32, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1730. int s0 = 1, int p0 = 0, int d0 = 1)
  1731. : ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), p0(p0), d0(d0) {}
  1732. ggml_tensor * build_graph(ggml_context * ctx) override {
  1733. ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data());
  1734. ggml_set_name(input, "input");
  1735. ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data());
  1736. ggml_set_name(kernel, "kernel");
  1737. ggml_tensor * out = ggml_conv_transpose_1d(ctx, kernel, input, s0, p0, d0);
  1738. ggml_set_name(out, "out");
  1739. return out;
  1740. }
  1741. };
  1742. // GGML_OP_IM2COL
  1743. struct test_im2col : public test_case {
  1744. const ggml_type type_input;
  1745. const ggml_type type_kernel;
  1746. const ggml_type dst_type;
  1747. const std::array<int64_t, 4> ne_input;
  1748. const std::array<int64_t, 4> ne_kernel;
  1749. // stride
  1750. const int s0;
  1751. const int s1;
  1752. // padding
  1753. const int p0;
  1754. const int p1;
  1755. // dilation
  1756. const int d0;
  1757. const int d1;
  1758. // mode
  1759. const bool is_2D;
  1760. std::string vars() override {
  1761. return VARS_TO_STR12(type_input, type_kernel, dst_type, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  1762. }
  1763. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32,
  1764. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1765. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1766. int s0 = 1, int s1 = 1,
  1767. int p0 = 1, int p1 = 1,
  1768. int d0 = 1, int d1 = 1,
  1769. bool is_2D = true)
  1770. : 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) {}
  1771. ggml_tensor * build_graph(ggml_context * ctx) override {
  1772. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1773. ggml_set_param(ctx, input);
  1774. ggml_set_name(input, "input");
  1775. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  1776. ggml_set_name(kernel, "kernel");
  1777. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D, dst_type);
  1778. ggml_set_name(out, "out");
  1779. return out;
  1780. }
  1781. };
  1782. // GGML_OP_CONCAT
  1783. struct test_concat : public test_case {
  1784. const ggml_type type;
  1785. const std::array<int64_t, 4> ne_a;
  1786. const int64_t ne_b_d;
  1787. const int dim;
  1788. const int v; // view (1 << 0: non-cont a, 1 << 1: non-cont b)
  1789. std::string vars() override {
  1790. return VARS_TO_STR5(type, ne_a, ne_b_d, dim, v);
  1791. }
  1792. test_concat(ggml_type type = GGML_TYPE_F32,
  1793. std::array<int64_t, 4> ne_a = {10, 5, 5, 5},
  1794. int64_t ne_b_d = 5,
  1795. int dim = 2, int v = 0)
  1796. : type(type), ne_a(ne_a), ne_b_d(ne_b_d), dim(dim), v(v) {}
  1797. ggml_tensor * build_graph(ggml_context * ctx) override {
  1798. auto ne_b = ne_a;
  1799. ne_b[dim] = ne_b_d;
  1800. ggml_tensor * a;
  1801. if (v & 1) {
  1802. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1803. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1804. ggml_set_name(a, "a");
  1805. 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);
  1806. ggml_set_name(a, "view_of_a");
  1807. } else {
  1808. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1809. ggml_set_name(a, "a");
  1810. }
  1811. ggml_tensor * b;
  1812. if (v & 2) {
  1813. auto ne = ne_b; ne[0] *= 3; ne[1] *= 2; ne[2] *= 4;
  1814. b = ggml_new_tensor(ctx, type, 4, ne.data());
  1815. ggml_set_name(b, "b");
  1816. 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);
  1817. ggml_set_name(b, "view_of_b");
  1818. } else {
  1819. b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1820. ggml_set_name(b, "b");
  1821. }
  1822. ggml_tensor * out = ggml_concat(ctx, a, b, dim);
  1823. ggml_set_name(out, "out");
  1824. return out;
  1825. }
  1826. };
  1827. // GGML_OP_ARGSORT
  1828. struct test_argsort : public test_case {
  1829. const ggml_type type;
  1830. const std::array<int64_t, 4> ne;
  1831. ggml_sort_order order;
  1832. std::string vars() override {
  1833. return VARS_TO_STR3(type, ne, order);
  1834. }
  1835. test_argsort(ggml_type type = GGML_TYPE_F32,
  1836. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  1837. ggml_sort_order order = GGML_SORT_ORDER_ASC)
  1838. : type(type), ne(ne), order(order) {}
  1839. ggml_tensor * build_graph(ggml_context * ctx) override {
  1840. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1841. ggml_set_name(a, "a");
  1842. ggml_tensor * out = ggml_argsort(ctx, a, order);
  1843. ggml_set_name(out, "out");
  1844. return out;
  1845. }
  1846. void initialize_tensors(ggml_context * ctx) override {
  1847. std::random_device rd;
  1848. std::default_random_engine rng(rd());
  1849. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1850. if (t->type == GGML_TYPE_I32) {
  1851. // indices
  1852. std::vector<int> data(ggml_nelements(t));
  1853. for (int i = 0; i < ggml_nelements(t); i++) {
  1854. data[i] = rand();
  1855. }
  1856. std::shuffle(data.begin(), data.end(), rng);
  1857. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  1858. } else if (t->type == GGML_TYPE_F32) {
  1859. // initialize with unique values to avoid ties
  1860. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1861. std::vector<float> data(t->ne[0]);
  1862. for (int i = 0; i < t->ne[0]; i++) {
  1863. data[i] = i;
  1864. }
  1865. std::shuffle(data.begin(), data.end(), rng);
  1866. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  1867. }
  1868. } else {
  1869. GGML_ABORT("fatal error");
  1870. }
  1871. }
  1872. }
  1873. };
  1874. // GGML_OP_SUM
  1875. struct test_sum : public test_case {
  1876. const ggml_type type;
  1877. const std::array<int64_t, 4> ne;
  1878. std::string vars() override {
  1879. return VARS_TO_STR2(type, ne);
  1880. }
  1881. test_sum(ggml_type type = GGML_TYPE_F32,
  1882. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1883. : type(type), ne(ne) {}
  1884. ggml_tensor * build_graph(ggml_context * ctx) override {
  1885. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1886. ggml_set_param(ctx, a);
  1887. ggml_set_name(a, "a");
  1888. ggml_tensor * out = ggml_sum(ctx, a);
  1889. ggml_set_name(out, "out");
  1890. return out;
  1891. }
  1892. float grad_eps() override {
  1893. return 0.1f * sqrtf(ne[0]*ne[1]*ne[2]*ne[3]);
  1894. }
  1895. };
  1896. // GGML_OP_SUM_ROWS
  1897. struct test_sum_rows : public test_case {
  1898. const ggml_type type;
  1899. const std::array<int64_t, 4> ne;
  1900. std::string vars() override {
  1901. return VARS_TO_STR2(type, ne);
  1902. }
  1903. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1904. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1905. : type(type), ne(ne) {}
  1906. ggml_tensor * build_graph(ggml_context * ctx) override {
  1907. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1908. ggml_set_param(ctx, a);
  1909. ggml_set_name(a, "a");
  1910. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1911. ggml_set_name(out, "out");
  1912. return out;
  1913. }
  1914. };
  1915. // GGML_OP_UPSCALE
  1916. struct test_upscale : public test_case {
  1917. const ggml_type type;
  1918. const std::array<int64_t, 4> ne;
  1919. const int32_t scale_factor;
  1920. const bool transpose;
  1921. std::string vars() override {
  1922. return VARS_TO_STR4(type, ne, scale_factor, transpose);
  1923. }
  1924. test_upscale(ggml_type type = GGML_TYPE_F32,
  1925. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1926. int32_t scale_factor = 2, bool transpose = false)
  1927. : type(type), ne(ne), scale_factor(scale_factor), transpose(transpose) {}
  1928. ggml_tensor * build_graph(ggml_context * ctx) override {
  1929. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1930. ggml_set_name(a, "a");
  1931. if (transpose) {
  1932. a = ggml_transpose(ctx, a);
  1933. ggml_set_name(a, "a_transposed");
  1934. }
  1935. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1936. ggml_set_name(out, "out");
  1937. return out;
  1938. }
  1939. };
  1940. // GGML_OP_UPSCALE (ext)
  1941. struct test_upscale_ext : public test_case {
  1942. const ggml_type type;
  1943. const std::array<int64_t, 4> ne;
  1944. const std::array<int64_t, 4> ne_tgt;
  1945. std::string vars() override {
  1946. return VARS_TO_STR3(type, ne, ne_tgt);
  1947. }
  1948. test_upscale_ext(ggml_type type = GGML_TYPE_F32,
  1949. std::array<int64_t, 4> ne = {2, 5, 7, 11},
  1950. std::array<int64_t, 4> ne_tgt = {5, 7, 11, 13})
  1951. : type(type), ne(ne), ne_tgt(ne_tgt) {}
  1952. ggml_tensor * build_graph(ggml_context * ctx) override {
  1953. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1954. ggml_set_name(a, "a");
  1955. ggml_tensor * out = ggml_upscale_ext(ctx, a, ne_tgt[0], ne_tgt[1],ne_tgt[2], ne_tgt[3]);
  1956. ggml_set_name(out, "out");
  1957. return out;
  1958. }
  1959. };
  1960. // GGML_OP_GROUP_NORM
  1961. struct test_group_norm : public test_case {
  1962. const ggml_type type;
  1963. const std::array<int64_t, 4> ne;
  1964. const int32_t num_groups;
  1965. const float eps;
  1966. std::string vars() override {
  1967. return VARS_TO_STR3(type, ne, num_groups);
  1968. }
  1969. test_group_norm(ggml_type type = GGML_TYPE_F32,
  1970. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  1971. int32_t num_groups = 32,
  1972. float eps = 1e-6f)
  1973. : type(type), ne(ne), num_groups(num_groups), eps(eps) {}
  1974. ggml_tensor * build_graph(ggml_context * ctx) override {
  1975. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1976. ggml_set_name(a, "a");
  1977. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups, eps);
  1978. ggml_set_name(out, "out");
  1979. return out;
  1980. }
  1981. };
  1982. // GGML_OP_ACC
  1983. struct test_acc : public test_case {
  1984. const ggml_type type;
  1985. const std::array<int64_t, 4> ne_a;
  1986. const std::array<int64_t, 4> ne_b;
  1987. std::string vars() override {
  1988. return VARS_TO_STR3(type, ne_a, ne_b);
  1989. }
  1990. test_acc(ggml_type type = GGML_TYPE_F32,
  1991. std::array<int64_t, 4> ne_a = {256, 17, 1, 1},
  1992. std::array<int64_t, 4> ne_b = {256, 16, 1, 1})
  1993. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  1994. ggml_tensor * build_graph(ggml_context * ctx) override {
  1995. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1996. ggml_set_param(ctx, a);
  1997. ggml_set_name(a, "a");
  1998. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1999. ggml_set_param(ctx, b);
  2000. ggml_set_name(b, "b");
  2001. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  2002. ggml_set_name(out, "out");
  2003. return out;
  2004. }
  2005. };
  2006. // GGML_OP_PAD
  2007. struct test_pad : public test_case {
  2008. const ggml_type type;
  2009. const std::array<int64_t, 4> ne_a;
  2010. const int pad_0;
  2011. const int pad_1;
  2012. std::string vars() override {
  2013. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  2014. }
  2015. test_pad(ggml_type type = GGML_TYPE_F32,
  2016. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  2017. int pad_0 = 1, int pad_1 = 1)
  2018. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  2019. ggml_tensor * build_graph(ggml_context * ctx) override {
  2020. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  2021. ggml_set_name(a, "a");
  2022. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  2023. ggml_set_name(out, "out");
  2024. return out;
  2025. }
  2026. };
  2027. // GGML_OP_ARANGE
  2028. struct test_arange : public test_case {
  2029. const ggml_type type;
  2030. const float start;
  2031. const float stop;
  2032. const float step;
  2033. std::string vars() override {
  2034. return VARS_TO_STR4(type, start, stop, step);
  2035. }
  2036. test_arange(ggml_type type = GGML_TYPE_F32,
  2037. float start = 0.f, float stop = 10.f, float step = 1.f)
  2038. : type(type), start(start), stop(stop), step(step) {}
  2039. ggml_tensor * build_graph(ggml_context * ctx) override {
  2040. ggml_tensor * out = ggml_arange(ctx, start, stop, step);
  2041. ggml_set_name(out, "out");
  2042. return out;
  2043. }
  2044. };
  2045. // GGML_OP_TIMESTEP_EMBEDDING
  2046. struct test_timestep_embedding : public test_case {
  2047. const ggml_type type;
  2048. const std::array<int64_t, 4> ne_a;
  2049. const int dim;
  2050. const int max_period;
  2051. std::string vars() override {
  2052. return VARS_TO_STR4(type, ne_a, dim, max_period);
  2053. }
  2054. test_timestep_embedding(ggml_type type = GGML_TYPE_F32,
  2055. std::array<int64_t, 4> ne_a = {2, 1, 1, 1},
  2056. int dim = 320, int max_period=10000)
  2057. : type(type), ne_a(ne_a), dim(dim), max_period(max_period) {}
  2058. ggml_tensor * build_graph(ggml_context * ctx) override {
  2059. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  2060. ggml_set_name(a, "a");
  2061. ggml_tensor * out = ggml_timestep_embedding(ctx, a, dim, max_period);
  2062. ggml_set_name(out, "out");
  2063. return out;
  2064. }
  2065. };
  2066. // GGML_OP_LEAKY_RELU
  2067. struct test_leaky_relu : public test_case {
  2068. const ggml_type type;
  2069. const std::array<int64_t, 4> ne_a;
  2070. const float negative_slope;
  2071. std::string vars() override {
  2072. return VARS_TO_STR3(type, ne_a, negative_slope);
  2073. }
  2074. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  2075. std::array<int64_t, 4> ne_a = {10, 5, 4, 3},
  2076. float negative_slope = 0.1f)
  2077. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  2078. ggml_tensor * build_graph(ggml_context * ctx) override {
  2079. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  2080. ggml_set_name(a, "a");
  2081. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  2082. ggml_set_name(out, "out");
  2083. return out;
  2084. }
  2085. };
  2086. // GGML_OP_FLASH_ATTN_EXT
  2087. struct test_flash_attn_ext : public test_case {
  2088. const int64_t hs; // head size
  2089. const int64_t nh; // num heads
  2090. const int64_t kv; // kv size
  2091. const int64_t nb; // batch size
  2092. const bool mask; // use mask
  2093. const float max_bias; // ALiBi
  2094. const float logit_softcap; // Gemma 2
  2095. const ggml_type type_KV;
  2096. std::string vars() override {
  2097. return VARS_TO_STR8(hs, nh, kv, nb, mask, max_bias, logit_softcap, type_KV);
  2098. }
  2099. double max_nmse_err() override {
  2100. return 5e-4;
  2101. }
  2102. test_flash_attn_ext(int64_t hs = 128, int64_t nh = 32, int64_t kv = 96, int64_t nb = 8,
  2103. bool mask = true, float max_bias = 0.0f, float logit_softcap = 0.0f, ggml_type type_KV = GGML_TYPE_F16)
  2104. : hs(hs), nh(nh), kv(kv), nb(nb), mask(mask), max_bias(max_bias), logit_softcap(logit_softcap), type_KV(type_KV) {}
  2105. ggml_tensor * build_graph(ggml_context * ctx) override {
  2106. const int64_t hs_padded = GGML_PAD(hs, ggml_blck_size(type_KV));
  2107. ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs_padded, nb, nh, 1);
  2108. ggml_set_name(q, "q");
  2109. ggml_tensor * k = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  2110. ggml_set_name(k, "k");
  2111. ggml_tensor * v = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  2112. ggml_set_name(v, "v");
  2113. ggml_tensor * m = nullptr;
  2114. if (mask) {
  2115. m = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, GGML_PAD(nb, GGML_KQ_MASK_PAD), 1, 1);
  2116. ggml_set_name(m, "m");
  2117. }
  2118. ggml_tensor * out = ggml_flash_attn_ext(ctx, q, k, v, m, 1.0f/sqrtf(hs), max_bias, logit_softcap);
  2119. ggml_set_name(out, "out");
  2120. return out;
  2121. }
  2122. bool grad_precise() override {
  2123. return true;
  2124. }
  2125. };
  2126. // GGML_OP_CROSS_ENTROPY_LOSS
  2127. struct test_cross_entropy_loss : public test_case {
  2128. const ggml_type type;
  2129. const std::array<int64_t, 4> ne;
  2130. std::string vars() override {
  2131. return VARS_TO_STR2(type, ne);
  2132. }
  2133. test_cross_entropy_loss(ggml_type type = GGML_TYPE_F32,
  2134. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  2135. : type(type), ne(ne) {}
  2136. ggml_tensor * build_graph(ggml_context * ctx) override {
  2137. ggml_tensor * logits = ggml_new_tensor(ctx, type, 4, ne.data());
  2138. ggml_set_param(ctx, logits);
  2139. ggml_set_name(logits, "logits");
  2140. ggml_tensor * labels = ggml_new_tensor(ctx, type, 4, ne.data());
  2141. // The labels are assumed to be constant -> no gradients.
  2142. ggml_set_name(labels, "labels");
  2143. // Ensure labels add up to 1:
  2144. labels = ggml_soft_max(ctx, labels);
  2145. ggml_set_name(labels, "labels_normalized");
  2146. ggml_tensor * out = ggml_cross_entropy_loss(ctx, logits, labels);
  2147. ggml_set_name(out, "out");
  2148. return out;
  2149. }
  2150. void initialize_tensors(ggml_context * ctx) override {
  2151. // For larger abs. diffs between logits softmax is more linear, therefore more precise num. gradients.
  2152. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  2153. init_tensor_uniform(t, -100.0f, 100.0f);
  2154. }
  2155. }
  2156. float grad_eps() override {
  2157. return 1.0f;
  2158. }
  2159. bool grad_precise() override {
  2160. return true;
  2161. }
  2162. };
  2163. enum llm_norm_type {
  2164. LLM_NORM,
  2165. LLM_NORM_RMS,
  2166. };
  2167. struct llama_hparams {
  2168. uint32_t n_vocab;
  2169. uint32_t n_embd;
  2170. uint32_t n_head;
  2171. uint32_t n_head_kv;
  2172. static constexpr uint32_t n_layer = 1;
  2173. uint32_t n_rot;
  2174. uint32_t n_embd_head; // dimension of values (d_v)
  2175. uint32_t n_ff;
  2176. float f_norm_eps;
  2177. float f_norm_rms_eps;
  2178. // cparams
  2179. static constexpr uint32_t n_ctx = 512; // user-specified context size
  2180. static constexpr uint32_t n_ctx_orig = n_ctx;
  2181. // batch
  2182. int32_t n_tokens;
  2183. // llm_build_context
  2184. static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx
  2185. static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache
  2186. uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads
  2187. return n_embd_head * n_head_kv;
  2188. }
  2189. };
  2190. // LLM base class
  2191. struct test_llm : public test_case {
  2192. llama_hparams hp;
  2193. protected:
  2194. test_llm(llama_hparams hp)
  2195. : hp(std::move(hp)) {
  2196. }
  2197. public:
  2198. struct ggml_tensor * llm_build_norm(
  2199. struct ggml_context * ctx,
  2200. struct ggml_tensor * cur,
  2201. struct ggml_tensor * mw,
  2202. struct ggml_tensor * mb,
  2203. llm_norm_type type) {
  2204. switch (type) {
  2205. case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break;
  2206. case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break;
  2207. }
  2208. cur = ggml_mul(ctx, cur, mw);
  2209. if (mb) {
  2210. cur = ggml_add(ctx, cur, mb);
  2211. }
  2212. return cur;
  2213. }
  2214. void llm_build_kv_store(
  2215. struct ggml_context * ctx,
  2216. struct ggml_tensor * k_l,
  2217. struct ggml_tensor * v_l,
  2218. struct ggml_tensor * k_cur,
  2219. struct ggml_tensor * v_cur) {
  2220. // compute the transposed [n_tokens, n_embd] V matrix
  2221. struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens));
  2222. struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(),
  2223. (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head);
  2224. struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(),
  2225. ( hp.n_ctx)*ggml_element_size(v_l),
  2226. (hp.kv_head)*ggml_element_size(v_l));
  2227. // important: storing RoPE-ed version of K in the KV cache!
  2228. ggml_cpy(ctx, k_cur, k_cache_view);
  2229. ggml_cpy(ctx, v_cur_t, v_cache_view);
  2230. }
  2231. struct ggml_tensor * llm_build_kqv(
  2232. struct ggml_context * ctx,
  2233. struct ggml_tensor * k_l,
  2234. struct ggml_tensor * v_l,
  2235. struct ggml_tensor * q_cur,
  2236. struct ggml_tensor * kq_mask,
  2237. float kq_scale) {
  2238. struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3);
  2239. struct ggml_tensor * k =
  2240. ggml_view_3d(ctx, k_l,
  2241. hp.n_embd_head, hp.n_kv, hp.n_head_kv,
  2242. ggml_row_size(k_l->type, hp.n_embd_gqa()),
  2243. ggml_row_size(k_l->type, hp.n_embd_head),
  2244. 0);
  2245. struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q);
  2246. kq = ggml_soft_max_ext(ctx, kq, kq_mask, kq_scale, 0.0f);
  2247. // split cached v into n_head heads
  2248. struct ggml_tensor * v =
  2249. ggml_view_3d(ctx, v_l,
  2250. hp.n_kv, hp.n_embd_head, hp.n_head_kv,
  2251. ggml_element_size(v_l)*hp.n_ctx,
  2252. ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head,
  2253. 0);
  2254. struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq);
  2255. struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3);
  2256. struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens);
  2257. struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  2258. cur = ggml_mul_mat(ctx, wo, cur);
  2259. return cur;
  2260. }
  2261. void initialize_tensors(ggml_context * ctx) override {
  2262. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  2263. if (t->type == GGML_TYPE_I32) {
  2264. // pos
  2265. std::vector<int> data(hp.n_tokens);
  2266. for (int i = 0; i < hp.n_tokens; i++) {
  2267. data[i] = rand() % hp.n_ctx;
  2268. }
  2269. ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int));
  2270. } else {
  2271. init_tensor_uniform(t);
  2272. }
  2273. }
  2274. }
  2275. };
  2276. // Llama
  2277. struct test_llama : public test_llm {
  2278. static constexpr float freq_base = 10000.0f;
  2279. static constexpr float freq_scale = 1.0f;
  2280. static constexpr float ext_factor = 0.0f;
  2281. static constexpr float attn_factor = 1.0f;
  2282. static constexpr float beta_fast = 32.0f;
  2283. static constexpr float beta_slow = 1.0f;
  2284. std::string op_desc(ggml_tensor * t) override {
  2285. GGML_UNUSED(t);
  2286. return "LLAMA";
  2287. }
  2288. std::string vars() override {
  2289. auto n_tokens = hp.n_tokens;
  2290. return VARS_TO_STR1(n_tokens);
  2291. }
  2292. double max_nmse_err() override {
  2293. return 2e-3;
  2294. }
  2295. test_llama(int n_tokens = 1)
  2296. : test_llm({
  2297. /*n_vocab =*/ 32000,
  2298. /*n_embd =*/ 3200,
  2299. /*n_head =*/ 32,
  2300. /*n_head_kv =*/ 32,
  2301. /*n_rot =*/ 100,
  2302. /*n_embd_head =*/ 100,
  2303. /*n_ff =*/ 8640,
  2304. /*f_norm_eps =*/ 0.f,
  2305. /*f_norm_rms_eps =*/ 1e-5f,
  2306. /*n_tokens =*/ n_tokens,
  2307. }) {
  2308. }
  2309. ggml_tensor * build_graph(ggml_context * ctx) override {
  2310. struct ggml_tensor * cur;
  2311. struct ggml_tensor * inpL;
  2312. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  2313. // inp_pos - contains the positions
  2314. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  2315. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  2316. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  2317. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  2318. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  2319. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  2320. struct ggml_tensor * inpSA = inpL;
  2321. // norm
  2322. ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2323. cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS);
  2324. // self-attention
  2325. {
  2326. ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  2327. ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  2328. ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  2329. // compute Q and K and RoPE them
  2330. struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur);
  2331. struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur);
  2332. struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur);
  2333. Qcur = ggml_rope_ext(
  2334. ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos, nullptr,
  2335. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  2336. ext_factor, attn_factor, beta_fast, beta_slow
  2337. );
  2338. Kcur = ggml_rope_ext(
  2339. ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos, nullptr,
  2340. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  2341. ext_factor, attn_factor, beta_fast, beta_slow
  2342. );
  2343. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  2344. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  2345. }
  2346. struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA);
  2347. // feed-forward network
  2348. ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2349. cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS);
  2350. ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  2351. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  2352. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  2353. struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur);
  2354. cur = ggml_mul_mat(ctx, ffn_gate, cur);
  2355. cur = ggml_silu(ctx, cur);
  2356. cur = ggml_mul(ctx, cur, tmp);
  2357. cur = ggml_mul_mat(ctx, ffn_down, cur);
  2358. cur = ggml_add(ctx, cur, ffn_inp);
  2359. // input for next layer
  2360. inpL = cur;
  2361. }
  2362. cur = inpL;
  2363. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2364. cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS);
  2365. // lm_head
  2366. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab);
  2367. cur = ggml_mul_mat(ctx, output, cur);
  2368. return cur;
  2369. }
  2370. };
  2371. // Falcon
  2372. struct test_falcon : public test_llm {
  2373. static constexpr float freq_base = 10000.0f;
  2374. static constexpr float freq_scale = 1.0f;
  2375. static constexpr float ext_factor = 0.0f;
  2376. static constexpr float attn_factor = 1.0f;
  2377. static constexpr float beta_fast = 32.0f;
  2378. static constexpr float beta_slow = 1.0f;
  2379. std::string op_desc(ggml_tensor * t) override {
  2380. GGML_UNUSED(t);
  2381. return "FALCON";
  2382. }
  2383. std::string vars() override {
  2384. auto n_tokens = hp.n_tokens;
  2385. return VARS_TO_STR1(n_tokens);
  2386. }
  2387. double max_nmse_err() override {
  2388. return 2e-3;
  2389. }
  2390. test_falcon(int n_tokens = 1)
  2391. : test_llm({
  2392. /*n_vocab =*/ 32000,
  2393. /*n_embd =*/ 3200,
  2394. /*n_head =*/ 50,
  2395. /*n_head_kv =*/ 1,
  2396. /*n_rot =*/ 64,
  2397. /*n_embd_head =*/ 64,
  2398. /*n_ff =*/ 8640,
  2399. /*f_norm_eps =*/ 1e-5f,
  2400. /*f_norm_rms_eps =*/ 0.f,
  2401. /*n_tokens =*/ n_tokens,
  2402. }) {
  2403. }
  2404. ggml_tensor * build_graph(ggml_context * ctx) override {
  2405. struct ggml_tensor * cur;
  2406. struct ggml_tensor * inpL;
  2407. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  2408. // inp_pos - contains the positions
  2409. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  2410. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  2411. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  2412. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  2413. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  2414. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  2415. // norm
  2416. ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2417. ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2418. ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM);
  2419. // self-attention
  2420. {
  2421. cur = attn_norm;
  2422. ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa());
  2423. cur = ggml_mul_mat(ctx, wqkv, cur);
  2424. 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)));
  2425. 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)));
  2426. 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())));
  2427. Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens);
  2428. Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens);
  2429. // using mode = 2 for neox mode
  2430. Qcur = ggml_rope_ext(
  2431. ctx, Qcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  2432. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  2433. );
  2434. Kcur = ggml_rope_ext(
  2435. ctx, Kcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  2436. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  2437. );
  2438. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  2439. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  2440. }
  2441. struct ggml_tensor * ffn_inp = cur;
  2442. // feed forward
  2443. {
  2444. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  2445. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  2446. cur = attn_norm;
  2447. cur = ggml_mul_mat(ctx, ffn_up, cur);
  2448. cur = ggml_gelu(ctx, cur);
  2449. cur = ggml_mul_mat(ctx, ffn_down, cur);
  2450. }
  2451. cur = ggml_add(ctx, cur, ffn_inp);
  2452. cur = ggml_add(ctx, cur, inpL);
  2453. // input for next layer
  2454. inpL = cur;
  2455. }
  2456. cur = inpL;
  2457. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2458. ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2459. cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM);
  2460. // lm_head
  2461. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab);
  2462. cur = ggml_mul_mat(ctx, output, cur);
  2463. return cur;
  2464. }
  2465. };
  2466. // ###########################################
  2467. // ## Section 3: GGML Op Test Instantiation ##
  2468. // ###########################################
  2469. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  2470. std::vector<std::unique_ptr<test_case>> test_cases;
  2471. std::default_random_engine rng(0);
  2472. const ggml_type all_types[] = {
  2473. GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16,
  2474. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  2475. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  2476. GGML_TYPE_Q8_0,
  2477. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  2478. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  2479. GGML_TYPE_Q6_K,
  2480. // GGML_TYPE_TQ1_0, GGML_TYPE_TQ2_0, // TODO: implement for all backends
  2481. GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  2482. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  2483. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  2484. };
  2485. const ggml_type base_types[] = {
  2486. GGML_TYPE_F32, GGML_TYPE_F16,
  2487. GGML_TYPE_Q4_0,
  2488. GGML_TYPE_Q4_K,
  2489. GGML_TYPE_IQ2_XXS
  2490. };
  2491. const ggml_type other_types[] = {
  2492. GGML_TYPE_Q4_1,
  2493. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  2494. GGML_TYPE_Q8_0,
  2495. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  2496. GGML_TYPE_Q5_K,
  2497. GGML_TYPE_Q6_K,
  2498. // GGML_TYPE_TQ1_0, GGML_TYPE_TQ2_0, // TODO: implement for all backends
  2499. GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  2500. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  2501. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  2502. GGML_TYPE_BF16,
  2503. };
  2504. // unary ops
  2505. for (int v : {0, 1}) {
  2506. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  2507. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 128, 2, 2, 2 }, v));
  2508. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 5, 7, 11, 13 }, v));
  2509. }
  2510. }
  2511. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  2512. for (ggml_type type : all_types) {
  2513. for (int b : {1, 7}) {
  2514. for (bool v : {false, true}) {
  2515. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  2516. }
  2517. }
  2518. }
  2519. for (int b : {1, 7}) {
  2520. for (bool v : {false, true}) {
  2521. test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, v));
  2522. }
  2523. }
  2524. for (ggml_type type_input : {GGML_TYPE_F32}) {
  2525. for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) {
  2526. for (int k0 : {1, 3}) {
  2527. for (int k1 : {1, 3}) {
  2528. for (int s0 : {1, 2}) {
  2529. for (int s1 : {1, 2}) {
  2530. for (int p0 : {0, 1}) {
  2531. for (int p1 : {0, 1}) {
  2532. test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1));
  2533. }
  2534. }
  2535. }
  2536. }
  2537. }
  2538. }
  2539. }
  2540. }
  2541. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32));
  2542. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32));
  2543. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16));
  2544. // test cases for 1D im2col
  2545. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32, {3000, 128, 1, 1}, {3, 128, 1280, 1}, 1, 0, 1, 0, 1, 0, false));
  2546. 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));
  2547. 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));
  2548. // sycl backend will limit task global_range < MAX_INT
  2549. // test cases for 2D im2col with large input W and H (occurs in stable-diffusion)
  2550. // however these cases need to alloc more memory which may fail in some devices (Intel Arc770, etc.)
  2551. // these cases are verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend)
  2552. // test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16, {1024, 1024, 256, 1}, {3, 3, 256, 1}, 1, 1, 1, 1, 1, 1, true));
  2553. // test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {1024, 1024, 256, 1}, {3, 3, 256, 1}, 1, 1, 1, 1, 1, 1, true));
  2554. test_cases.emplace_back(new test_conv_transpose_1d());
  2555. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 3, 0, 1));
  2556. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 2, 0, 1));
  2557. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 1, 0, 1));
  2558. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 2, 0, 1));
  2559. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 1, 0, 1));
  2560. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,1,2,1}, 1, 0, 1));
  2561. test_cases.emplace_back(new test_conv_transpose_1d({2,1,1,1}, {3,1,1,1}, 1, 0, 1));
  2562. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 1, 1}));
  2563. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, 3}, {2, 1, 1, 1}));
  2564. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 2, 1, 1}));
  2565. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 2, 1}));
  2566. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 1, 2}));
  2567. test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 5, 4, 3}, {2, 1, 1, 1}));
  2568. test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 5, 4, 3}, {1, 1, 1, 2}));
  2569. test_cases.emplace_back(new test_dup(GGML_TYPE_F32));
  2570. test_cases.emplace_back(new test_dup(GGML_TYPE_F16));
  2571. test_cases.emplace_back(new test_dup(GGML_TYPE_I32));
  2572. test_cases.emplace_back(new test_dup(GGML_TYPE_I16));
  2573. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {0, 2, 1, 3}));
  2574. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {0, 2, 1, 3})); // dup by rows
  2575. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {1, 0, 2, 3}));
  2576. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {1, 0, 2, 3})); // dup dst not-contiguous
  2577. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3}));
  2578. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3}));
  2579. for (int dim = 1; dim < GGML_MAX_DIMS; ++dim) {
  2580. test_cases.emplace_back(new test_set(GGML_TYPE_F32, GGML_TYPE_F32, {6, 5, 4, 3}, dim));
  2581. }
  2582. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  2583. for (ggml_type type_dst : all_types) {
  2584. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4}));
  2585. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {0, 2, 1, 3})); // cpy by rows
  2586. }
  2587. }
  2588. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  2589. for (ggml_type type_dst : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  2590. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {1, 0, 2, 3})); // cpy not-contiguous
  2591. }
  2592. }
  2593. test_cases.emplace_back(new test_cont());
  2594. test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {2, 1, 1 ,1}));
  2595. test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {2, 1, 3 ,5}));
  2596. test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {2, 3, 5 ,7}));
  2597. test_cases.emplace_back(new test_cont(GGML_TYPE_F16, {2, 1, 1 ,1}));
  2598. test_cases.emplace_back(new test_cont(GGML_TYPE_F16, {2, 1, 3 ,5}));
  2599. test_cases.emplace_back(new test_cont(GGML_TYPE_F16, {2, 3, 5 ,7}));
  2600. test_cases.emplace_back(new test_cont(GGML_TYPE_BF16, {2, 1, 1 ,1}));
  2601. test_cases.emplace_back(new test_cont(GGML_TYPE_BF16, {2, 1, 3 ,5}));
  2602. test_cases.emplace_back(new test_cont(GGML_TYPE_BF16, {2, 3, 5 ,7}));
  2603. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  2604. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  2605. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  2606. }
  2607. };
  2608. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  2609. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  2610. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  2611. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 1, 1}, {1, 1, 1, 1});
  2612. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 1}, {1, 1, 1, 1});
  2613. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 1, 1});
  2614. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {2, 1, 1, 1});
  2615. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 2, 1, 1});
  2616. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 2, 1});
  2617. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 1, 2});
  2618. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 2, 2});
  2619. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 2, 2, 2});
  2620. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {2, 2, 2, 2});
  2621. // stable diffusion
  2622. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  2623. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  2624. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  2625. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  2626. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  2627. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  2628. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  2629. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  2630. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  2631. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  2632. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  2633. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  2634. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  2635. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  2636. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  2637. test_cases.emplace_back(new test_add1());
  2638. test_cases.emplace_back(new test_scale());
  2639. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  2640. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 5, 4, 3}, eps));
  2641. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 5, 4, 3}, eps));
  2642. }
  2643. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 1536, 1, 1}, {4, 1536, 1, 1}));
  2644. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {8, 1536, 1, 1}, {4, 1536, 1, 1}));
  2645. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 1536, 4, 1}, {4, 1536, 1, 1}));
  2646. test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 16, 1024, 32, 4));
  2647. #if 1
  2648. for (ggml_type type_a : base_types) {
  2649. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  2650. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  2651. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  2652. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  2653. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  2654. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  2655. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  2656. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  2657. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  2658. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  2659. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  2660. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  2661. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  2662. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  2663. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  2664. }
  2665. }
  2666. #else
  2667. // m = a rows
  2668. // n = b rows
  2669. // k = cols
  2670. std::uniform_int_distribution<> dist_m(1, 128);
  2671. std::uniform_int_distribution<> dist_n(16, 128);
  2672. std::uniform_int_distribution<> dist_k(1, 16);
  2673. for (int i = 0; i < 1000; i++) {
  2674. for (ggml_type type_a : all_types) {
  2675. for (ggml_type type_b : {GGML_TYPE_F32}) {
  2676. int m = dist_m(rng);
  2677. int n = dist_n(rng);
  2678. int k = dist_k(rng) * ggml_blck_size(type_a);
  2679. test_cases.emplace_back(new test_mul_mat(type_a, type_b, m, n, k, { 1, 1}, {1, 1}));
  2680. }
  2681. }
  2682. }
  2683. #endif
  2684. for (ggml_type type_a : other_types) {
  2685. for (ggml_type type_b : {GGML_TYPE_F32}) {
  2686. if (ggml_blck_size(type_a) != 256) {
  2687. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, ggml_blck_size(type_a), {1, 1}, {1, 1}));
  2688. }
  2689. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {1, 1}, {1, 1}));
  2690. }
  2691. }
  2692. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 128, { 8, 1}, {1, 1}));
  2693. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 128, { 8, 1}, {4, 1}));
  2694. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 64, { 8, 1}, {4, 1}));
  2695. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 64, { 8, 1}, {4, 1}));
  2696. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 45, 128, { 8, 1}, {4, 1}));
  2697. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 128, 45, 64, { 8, 1}, {4, 1}));
  2698. // sycl backend will limit task global_range < MAX_INT
  2699. // test case for f16-type-convert-to-fp32 kernel with large k under fp32 compute dtype (occurs in stable-diffusion)
  2700. // however this case needs to alloc more memory which may fail in some devices (Intel Arc770, etc.)
  2701. // this case is verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend)
  2702. // test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 512, 262144, 9216, {1, 1}, {1, 1}));
  2703. for (ggml_type type_a : base_types) {
  2704. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  2705. for (int n_mats : {4, 8}) {
  2706. for (int n_used : {1, 2, 4}) {
  2707. for (bool b : {false, true}) {
  2708. for (int n : {1, 32}) {
  2709. int m = 512;
  2710. int k = 256;
  2711. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  2712. }
  2713. }
  2714. }
  2715. }
  2716. }
  2717. }
  2718. for (ggml_type type_a : other_types) {
  2719. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  2720. for (int n_mats : {4}) {
  2721. for (int n_used : {2}) {
  2722. for (bool b : {false}) {
  2723. for (int n : {1}) {
  2724. int m = 512;
  2725. int k = 256;
  2726. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  2727. }
  2728. }
  2729. }
  2730. }
  2731. }
  2732. }
  2733. test_cases.emplace_back(new test_sqr());
  2734. test_cases.emplace_back(new test_sqrt());
  2735. test_cases.emplace_back(new test_log());
  2736. test_cases.emplace_back(new test_sin());
  2737. test_cases.emplace_back(new test_cos());
  2738. test_cases.emplace_back(new test_clamp());
  2739. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  2740. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 3, 1}, 5));
  2741. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 3, 2}, 5));
  2742. #if 0
  2743. std::uniform_int_distribution<> dist_ne1(1, 50);
  2744. int exponent = 1;
  2745. while (exponent < (1 << 17)) {
  2746. std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent);
  2747. for (int n = 0; n < 10; ++n) {
  2748. int64_t ne0 = dist_ne0(rng);
  2749. int64_t ne1 = dist_ne1(rng);
  2750. 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));
  2751. }
  2752. exponent <<= 1;
  2753. }
  2754. #endif
  2755. for (bool mask : {false, true}) {
  2756. for (float max_bias : {0.0f, 8.0f}) {
  2757. if (!mask && max_bias > 0.0f) continue;
  2758. for (float scale : {1.0f, 0.1f}) {
  2759. for (int64_t ne0 : {16, 1024}) {
  2760. for (int64_t ne1 : {16, 1024}) {
  2761. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, scale, max_bias));
  2762. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, scale, max_bias));
  2763. }
  2764. }
  2765. }
  2766. }
  2767. }
  2768. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, true, 0.1f, 0.0f));
  2769. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 0.0f));
  2770. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 0.0f));
  2771. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 8.0f));
  2772. {
  2773. bool all = true;
  2774. for (float v : { 0, 1 }) {
  2775. for (float fs : { 1.0f, 1.4245f }) {
  2776. for (float ef : { 0.0f, 0.7465f }) {
  2777. for (float af : { 1.0f, 1.4245f }) {
  2778. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  2779. for (bool ff : {false, true}) { // freq_factors
  2780. test_cases.emplace_back(new test_rope(type, {128, 32, 2, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 7B
  2781. if (all) {
  2782. test_cases.emplace_back(new test_rope(type, {128, 40, 2, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 13B
  2783. test_cases.emplace_back(new test_rope(type, {128, 52, 2, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 30B
  2784. test_cases.emplace_back(new test_rope(type, {128, 64, 2, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 65B
  2785. }
  2786. if (all) {
  2787. test_cases.emplace_back(new test_rope(type, { 64, 1, 2, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  2788. test_cases.emplace_back(new test_rope(type, { 64, 71, 2, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  2789. test_cases.emplace_back(new test_rope(type, { 64, 8, 2, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2790. test_cases.emplace_back(new test_rope(type, { 80, 32, 2, 1}, 20, 2, 512, fs, ef, af, ff, v)); // neox (stablelm)
  2791. test_cases.emplace_back(new test_rope(type, { 80, 32, 2, 1}, 32, 2, 512, fs, ef, af, ff, v)); // neox (phi-2)
  2792. }
  2793. test_cases.emplace_back(new test_rope(type, { 64, 128, 2, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2794. }
  2795. }
  2796. all = false;
  2797. }
  2798. }
  2799. }
  2800. }
  2801. }
  2802. for (int v : { 0, 1, 2, 3 }) {
  2803. for (int dim : { 0, 1, 2, 3, }) {
  2804. test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {11, 12, 13, 14}, 7, dim, v));
  2805. test_cases.emplace_back(new test_concat(GGML_TYPE_I32, {11, 12, 13, 14}, 7, dim, v));
  2806. }
  2807. }
  2808. for (ggml_sort_order order : {GGML_SORT_ORDER_ASC, GGML_SORT_ORDER_DESC}) {
  2809. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  2810. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  2811. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {60, 10, 10, 10}, order)); // qwen
  2812. }
  2813. test_cases.emplace_back(new test_sum());
  2814. test_cases.emplace_back(new test_sum_rows());
  2815. test_cases.emplace_back(new test_upscale());
  2816. test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, { 512, 512, 3, 1 }, 2, true));
  2817. test_cases.emplace_back(new test_upscale_ext());
  2818. test_cases.emplace_back(new test_group_norm());
  2819. test_cases.emplace_back(new test_acc());
  2820. test_cases.emplace_back(new test_pad());
  2821. test_cases.emplace_back(new test_arange());
  2822. test_cases.emplace_back(new test_timestep_embedding());
  2823. test_cases.emplace_back(new test_leaky_relu());
  2824. for (int hs : { 64, 80, 128, 256, }) {
  2825. for (bool mask : { true, false } ) {
  2826. for (float max_bias : { 0.0f, 8.0f }) {
  2827. if (!mask && max_bias > 0.0f) continue;
  2828. for (float logit_softcap : {0.0f, 10.0f}) {
  2829. if (hs != 128 && logit_softcap != 0.0f) continue;
  2830. for (int nh : { 32, }) {
  2831. for (int kv : { 512, 1024, }) {
  2832. for (int nb : { 1, 2, 4, 8, }) {
  2833. for (ggml_type type_KV : {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
  2834. test_cases.emplace_back(new test_flash_attn_ext(hs, nh, kv, nb, mask, max_bias, logit_softcap, type_KV));
  2835. }
  2836. }
  2837. }
  2838. }
  2839. }
  2840. }
  2841. }
  2842. }
  2843. test_cases.emplace_back(new test_cross_entropy_loss());
  2844. // these tests are disabled to save execution time, but they can be handy for debugging
  2845. #if 0
  2846. test_cases.emplace_back(new test_llama(1));
  2847. test_cases.emplace_back(new test_llama(2));
  2848. test_cases.emplace_back(new test_falcon(1));
  2849. test_cases.emplace_back(new test_falcon(2));
  2850. #endif
  2851. // run tests
  2852. if (mode == MODE_GRAD) {
  2853. size_t n_ok = 0;
  2854. for (auto & test : test_cases) {
  2855. if (test->eval_grad(backend, op_name)) {
  2856. n_ok++;
  2857. }
  2858. }
  2859. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  2860. return n_ok == test_cases.size();
  2861. }
  2862. if (mode == MODE_TEST) {
  2863. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  2864. size_t n_ok = 0;
  2865. for (auto & test : test_cases) {
  2866. if (test->eval(backend, backend_cpu, op_name)) {
  2867. n_ok++;
  2868. }
  2869. }
  2870. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  2871. ggml_backend_free(backend_cpu);
  2872. return n_ok == test_cases.size();
  2873. }
  2874. if (mode == MODE_PERF) {
  2875. for (auto & test : test_cases) {
  2876. test->eval_perf(backend, op_name);
  2877. }
  2878. return true;
  2879. }
  2880. GGML_ABORT("fatal error");
  2881. }
  2882. static void usage(char ** argv) {
  2883. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  2884. printf(" valid modes:\n");
  2885. printf(" - test (default, compare with CPU backend for correctness)\n");
  2886. printf(" - perf (performance evaluation)\n");
  2887. printf(" - grad (compare gradients from backpropagation with method of finite differences)\n");
  2888. printf(" op names are as given by ggml_op_desc() (e.g. GGML_ADD)\n");
  2889. }
  2890. int main(int argc, char ** argv) {
  2891. test_mode mode = MODE_TEST;
  2892. const char * op_name_filter = NULL;
  2893. const char * backend_filter = NULL;
  2894. for (int i = 1; i < argc; i++) {
  2895. if (strcmp(argv[i], "test") == 0) {
  2896. mode = MODE_TEST;
  2897. } else if (strcmp(argv[i], "perf") == 0) {
  2898. mode = MODE_PERF;
  2899. } else if (strcmp(argv[i], "grad") == 0) {
  2900. mode = MODE_GRAD;
  2901. } else if (strcmp(argv[i], "-o") == 0) {
  2902. if (i + 1 < argc) {
  2903. op_name_filter = argv[++i];
  2904. } else {
  2905. usage(argv);
  2906. return 1;
  2907. }
  2908. } else if (strcmp(argv[i], "-b") == 0) {
  2909. if (i + 1 < argc) {
  2910. backend_filter = argv[++i];
  2911. } else {
  2912. usage(argv);
  2913. return 1;
  2914. }
  2915. } else {
  2916. usage(argv);
  2917. return 1;
  2918. }
  2919. }
  2920. // enumerate backends
  2921. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  2922. size_t n_ok = 0;
  2923. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  2924. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  2925. if (backend_filter != NULL && strcmp(backend_filter, ggml_backend_reg_get_name(i)) != 0) {
  2926. printf(" Skipping\n");
  2927. n_ok++;
  2928. continue;
  2929. }
  2930. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  2931. GGML_ASSERT(backend != NULL);
  2932. if (backend_filter == NULL && ggml_backend_is_cpu(backend) && mode != MODE_GRAD) {
  2933. printf(" Skipping CPU backend\n");
  2934. ggml_backend_free(backend);
  2935. n_ok++;
  2936. continue;
  2937. }
  2938. printf(" Backend name: %s\n", ggml_backend_name(backend));
  2939. bool ok = test_backend(backend, mode, op_name_filter);
  2940. printf(" Backend %s: ", ggml_backend_name(backend));
  2941. if (ok) {
  2942. printf("\033[1;32mOK\033[0m\n");
  2943. n_ok++;
  2944. } else {
  2945. printf("\033[1;31mFAIL\033[0m\n");
  2946. }
  2947. printf("\n");
  2948. ggml_backend_free(backend);
  2949. }
  2950. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  2951. if (n_ok != ggml_backend_reg_get_count()) {
  2952. printf("\033[1;31mFAIL\033[0m\n");
  2953. return 1;
  2954. }
  2955. ggml_quantize_free();
  2956. printf("\033[1;32mOK\033[0m\n");
  2957. return 0;
  2958. }