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