test-backend-ops.cpp 129 KB

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