test-backend-ops.cpp 141 KB

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