test-backend-ops.cpp 133 KB

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