test-backend-ops.cpp 142 KB

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