test-backend-ops.cpp 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759
  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_RWKV_WKV
  1267. struct test_rwkv_wkv : public test_case {
  1268. const ggml_type type;
  1269. const int64_t head_count;
  1270. const int64_t head_size;
  1271. const int64_t n_seq_tokens;
  1272. const int64_t n_seqs;
  1273. std::string vars() override {
  1274. return VARS_TO_STR5(type, head_count, head_size, n_seq_tokens, n_seqs);
  1275. }
  1276. test_rwkv_wkv(ggml_type type = GGML_TYPE_F32,
  1277. int64_t head_count = 32, int64_t head_size = 64, int64_t n_seq_tokens = 32, int64_t n_seqs = 32)
  1278. : type(type), head_count(head_count), head_size(head_size), n_seq_tokens(n_seq_tokens), n_seqs(n_seqs) {}
  1279. ggml_tensor * build_graph(ggml_context * ctx) override {
  1280. const int64_t n_tokens = n_seq_tokens * n_seqs;
  1281. ggml_tensor * r = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ 1, head_size, head_count, n_tokens }.data());
  1282. ggml_tensor * k = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ head_size, 1, head_count, n_tokens }.data());
  1283. ggml_tensor * v = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ 1, head_size, head_count, n_tokens }.data());
  1284. ggml_tensor * tf = ggml_new_tensor(ctx, type, 2, std::vector<int64_t>{ head_size, head_count }.data());
  1285. ggml_tensor * td = ggml_new_tensor(ctx, type, 4, std::vector<int64_t>{ 1, head_size, head_count, n_tokens }.data());
  1286. ggml_tensor * s = ggml_new_tensor(ctx, type, 2, std::vector<int64_t>{ head_size * head_size * head_count, n_seqs }.data());
  1287. ggml_tensor * out = ggml_rwkv_wkv(ctx, k, v, r, tf, td, s);
  1288. return out;
  1289. }
  1290. };
  1291. // GGML_OP_MUL_MAT
  1292. struct test_mul_mat : public test_case {
  1293. const ggml_type type_a;
  1294. const ggml_type type_b;
  1295. const int64_t m;
  1296. const int64_t n;
  1297. const int64_t k;
  1298. const std::array<int64_t, 2> bs; // dims 3 and 4
  1299. const std::array<int64_t, 2> nr; // repeat in dims 3 and 4
  1300. std::string vars() override {
  1301. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, nr);
  1302. }
  1303. double max_nmse_err() override {
  1304. return 5e-4;
  1305. }
  1306. size_t op_size(ggml_tensor * t) override {
  1307. size_t a = ggml_nbytes(t->src[0]) * n * nr[0] * nr[1];
  1308. size_t b = ggml_nbytes(t->src[1]) * m;
  1309. size_t c = ggml_nbytes(t);
  1310. return a + b + c;
  1311. GGML_UNUSED(t);
  1312. }
  1313. test_mul_mat(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  1314. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  1315. std::array<int64_t, 2> bs = {10, 10},
  1316. std::array<int64_t, 2> nr = {2, 2})
  1317. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), nr(nr) {}
  1318. ggml_tensor * build_graph(ggml_context * ctx) override {
  1319. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  1320. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, k, m, bs[0] , bs[1]);
  1321. ggml_tensor * b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0]*nr[0], bs[1]*nr[1]);
  1322. ggml_set_param(ctx, a);
  1323. ggml_set_param(ctx, b);
  1324. ggml_set_name(a, "a");
  1325. ggml_set_name(b, "b");
  1326. ggml_tensor * out = ggml_mul_mat(ctx, a, b);
  1327. ggml_set_name(out, "out");
  1328. return out;
  1329. }
  1330. };
  1331. // GGML_OP_MUL_MAT_ID
  1332. struct test_mul_mat_id : public test_case {
  1333. const ggml_type type_a;
  1334. const ggml_type type_b;
  1335. const int n_mats;
  1336. const int n_used;
  1337. const bool b; // brodcast b matrix
  1338. const int64_t m;
  1339. const int64_t n;
  1340. const int64_t k;
  1341. std::string vars() override {
  1342. return VARS_TO_STR8(type_a, type_b, n_mats, n_used, b, m, n, k);
  1343. }
  1344. double max_nmse_err() override {
  1345. return 5e-4;
  1346. }
  1347. size_t op_size(ggml_tensor * t) override {
  1348. size_t a = ggml_nbytes(t->src[2]) * n;
  1349. size_t b = ggml_nbytes(t->src[1]) * m;
  1350. size_t c = ggml_nbytes(t);
  1351. return a + b + c;
  1352. GGML_UNUSED(t);
  1353. }
  1354. test_mul_mat_id(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  1355. int n_mats = 8, int n_used = 2, bool b = false,
  1356. int64_t m = 32, int64_t n = 32, int64_t k = 32)
  1357. : type_a(type_a), type_b(type_b), n_mats(n_mats), n_used(n_used), b(b),
  1358. m(m), n(n), k(k) {
  1359. GGML_ASSERT(n_used <= n_mats);
  1360. }
  1361. ggml_tensor * build_graph(ggml_context * ctx) override {
  1362. // C^T = A * B^T: (k, m) * (k, n) => (m, n)
  1363. ggml_tensor * as = ggml_new_tensor_3d(ctx, type_a, k, m, n_mats);
  1364. ggml_set_name(as, "as");
  1365. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_mats, n);
  1366. ggml_set_name(ids, "ids");
  1367. if (n_used != n_mats) {
  1368. ids = ggml_view_2d(ctx, ids, n_used, n, ids->nb[1], 0);
  1369. ggml_set_name(ids, "view_of_ids");
  1370. }
  1371. ggml_tensor * b = ggml_new_tensor_3d(ctx, type_b, k, this->b ? 1 : n_used, n);
  1372. ggml_set_name(b, "b");
  1373. ggml_tensor * out = ggml_mul_mat_id(ctx, as, b, ids);
  1374. ggml_set_name(out, "out");
  1375. return out;
  1376. }
  1377. void initialize_tensors(ggml_context * ctx) override {
  1378. std::random_device rd;
  1379. std::default_random_engine rng(rd());
  1380. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1381. if (t->type == GGML_TYPE_I32) {
  1382. if (ggml_is_view_op(t->op)) { continue; }
  1383. // ids
  1384. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1385. std::vector<int32_t> data(t->ne[0]);
  1386. for (int i = 0; i < t->ne[0]; i++) {
  1387. data[i] = i % n_mats;
  1388. }
  1389. std::shuffle(data.begin(), data.end(), rng);
  1390. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(int32_t));
  1391. }
  1392. } else {
  1393. init_tensor_uniform(t);
  1394. }
  1395. }
  1396. }
  1397. };
  1398. // GGML_OP_OUT_PROD
  1399. struct test_out_prod : public test_case {
  1400. const ggml_type type_a;
  1401. const ggml_type type_b;
  1402. const int64_t m;
  1403. const int64_t n;
  1404. const int64_t k;
  1405. const std::array<int64_t, 2> bs; // dims 3 and 4
  1406. const bool trans_b;
  1407. std::string vars() override {
  1408. return VARS_TO_STR7(type_a, type_b, m, n, k, bs, trans_b);
  1409. }
  1410. double max_nmse_err() override {
  1411. return 5e-4;
  1412. }
  1413. test_out_prod(ggml_type type_a = GGML_TYPE_F32, ggml_type type_b = GGML_TYPE_F32,
  1414. int64_t m = 32, int64_t n = 32, int64_t k = 32,
  1415. std::array<int64_t, 2> bs = {10, 10},
  1416. bool trans_b = false)
  1417. : type_a(type_a), type_b(type_b), m(m), n(n), k(k), bs(bs), trans_b(trans_b) {}
  1418. ggml_tensor * build_graph(ggml_context * ctx) override {
  1419. ggml_tensor * a = ggml_new_tensor_4d(ctx, type_a, m, k, bs[0], bs[1]);
  1420. ggml_set_name(a, "a");
  1421. ggml_tensor * b;
  1422. if (trans_b) {
  1423. b = ggml_new_tensor_4d(ctx, type_b, k, n, bs[0], bs[1]);
  1424. b = ggml_transpose(ctx, b);
  1425. } else {
  1426. b = ggml_new_tensor_4d(ctx, type_b, n, k, bs[0], bs[1]);
  1427. }
  1428. ggml_set_name(b, "b");
  1429. ggml_tensor * out = ggml_out_prod(ctx, a, b);
  1430. ggml_set_name(out, "out");
  1431. return out;
  1432. }
  1433. };
  1434. // GGML_OP_SQR
  1435. struct test_sqr : public test_case {
  1436. const ggml_type type;
  1437. const std::array<int64_t, 4> ne;
  1438. std::string vars() override {
  1439. return VARS_TO_STR2(type, ne);
  1440. }
  1441. test_sqr(ggml_type type = GGML_TYPE_F32,
  1442. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1443. : type(type), ne(ne) {}
  1444. ggml_tensor * build_graph(ggml_context * ctx) override {
  1445. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1446. ggml_set_param(ctx, a);
  1447. ggml_set_name(a, "a");
  1448. ggml_tensor * out = ggml_sqr(ctx, a);
  1449. ggml_set_name(out, "out");
  1450. return out;
  1451. }
  1452. float grad_eps() override {
  1453. return 0.1f * 0.25f*ne[0]*ne[1]*ne[2]*ne[3]; // 10% of expected value of sum.
  1454. }
  1455. };
  1456. // GGML_OP_SQRT
  1457. struct test_sqrt : public test_case {
  1458. const ggml_type type;
  1459. const std::array<int64_t, 4> ne;
  1460. std::string vars() override {
  1461. return VARS_TO_STR2(type, ne);
  1462. }
  1463. test_sqrt(ggml_type type = GGML_TYPE_F32,
  1464. std::array<int64_t, 4> ne = {10, 3, 3, 2})
  1465. : type(type), ne(ne) {}
  1466. ggml_tensor * build_graph(ggml_context * ctx) override {
  1467. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1468. ggml_set_param(ctx, a);
  1469. ggml_set_name(a, "a");
  1470. ggml_tensor * out = ggml_sqrt(ctx, a);
  1471. ggml_set_name(out, "out");
  1472. return out;
  1473. }
  1474. void initialize_tensors(ggml_context * ctx) override {
  1475. // fill with positive values
  1476. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1477. init_tensor_uniform(t, 50.0f, 100.0f);
  1478. }
  1479. }
  1480. float grad_eps() override {
  1481. return 20.0f;
  1482. }
  1483. bool grad_precise() override {
  1484. return true;
  1485. }
  1486. };
  1487. // GGML_OP_LOG
  1488. struct test_log : public test_case {
  1489. const ggml_type type;
  1490. const std::array<int64_t, 4> ne;
  1491. std::string vars() override {
  1492. return VARS_TO_STR2(type, ne);
  1493. }
  1494. test_log(ggml_type type = GGML_TYPE_F32,
  1495. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1496. : type(type), ne(ne) {}
  1497. ggml_tensor * build_graph(ggml_context * ctx) override {
  1498. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1499. ggml_set_param(ctx, a);
  1500. ggml_set_name(a, "a");
  1501. ggml_tensor * out = ggml_log(ctx, a);
  1502. ggml_set_name(out, "out");
  1503. return out;
  1504. }
  1505. void initialize_tensors(ggml_context * ctx) override {
  1506. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1507. // log(1) == 0, cluster values there to keep the sum low for better precision in the backwards pass:
  1508. init_tensor_uniform(t, 0.9f, 1.1f);
  1509. }
  1510. }
  1511. bool grad_precise() override {
  1512. return true;
  1513. }
  1514. };
  1515. // GGML_OP_SIN
  1516. struct test_sin : public test_case {
  1517. const ggml_type type;
  1518. const std::array<int64_t, 4> ne;
  1519. std::string vars() override {
  1520. return VARS_TO_STR2(type, ne);
  1521. }
  1522. test_sin(ggml_type type = GGML_TYPE_F32,
  1523. std::array<int64_t, 4> ne = {10, 2, 2, 2})
  1524. : type(type), ne(ne) {}
  1525. ggml_tensor * build_graph(ggml_context * ctx) override {
  1526. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1527. ggml_set_param(ctx, a);
  1528. ggml_set_name(a, "a");
  1529. ggml_tensor * out = ggml_sin(ctx, a);
  1530. ggml_set_name(out, "out");
  1531. return out;
  1532. }
  1533. void initialize_tensors(ggml_context * ctx) override {
  1534. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1535. init_tensor_uniform(t, -6.5f, 6.5f); // Covers interval [-2*pi, 2*pi].
  1536. }
  1537. }
  1538. double max_maa_err() override {
  1539. return 1e-3;
  1540. }
  1541. float grad_eps() override {
  1542. return 0.2f;
  1543. }
  1544. bool grad_precise() override {
  1545. return true;
  1546. }
  1547. };
  1548. // GGML_OP_COS
  1549. struct test_cos : public test_case {
  1550. const ggml_type type;
  1551. const std::array<int64_t, 4> ne;
  1552. std::string vars() override {
  1553. return VARS_TO_STR2(type, ne);
  1554. }
  1555. test_cos(ggml_type type = GGML_TYPE_F32,
  1556. std::array<int64_t, 4> ne = {10, 2, 2, 2})
  1557. : type(type), ne(ne) {}
  1558. ggml_tensor * build_graph(ggml_context * ctx) override {
  1559. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1560. ggml_set_param(ctx, a);
  1561. ggml_set_name(a, "a");
  1562. ggml_tensor * out = ggml_cos(ctx, a);
  1563. ggml_set_name(out, "out");
  1564. return out;
  1565. }
  1566. void initialize_tensors(ggml_context * ctx) override {
  1567. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1568. init_tensor_uniform(t, -6.5f, 6.5f); // Covers interval [-2*pi, 2*pi].
  1569. }
  1570. }
  1571. double max_maa_err() override {
  1572. return 1e-3;
  1573. }
  1574. float grad_eps() override {
  1575. return 0.2f;
  1576. }
  1577. bool grad_precise() override {
  1578. return true;
  1579. }
  1580. };
  1581. // GGML_OP_CLAMP
  1582. struct test_clamp : public test_case {
  1583. const ggml_type type;
  1584. const std::array<int64_t, 4> ne;
  1585. float min;
  1586. float max;
  1587. std::string vars() override {
  1588. return VARS_TO_STR4(type, ne, min, max);
  1589. }
  1590. test_clamp(ggml_type type = GGML_TYPE_F32,
  1591. std::array<int64_t, 4> ne = {10, 5, 4, 3},
  1592. float min = -0.5f, float max = 0.5f)
  1593. : type(type), ne(ne), min(min), max(max) {}
  1594. ggml_tensor * build_graph(ggml_context * ctx) override {
  1595. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1596. ggml_set_name(a, "a");
  1597. ggml_tensor * out = ggml_clamp(ctx, a, min, max);
  1598. ggml_set_name(out, "out");
  1599. return out;
  1600. }
  1601. float grad_eps() override {
  1602. return 1e-2f;
  1603. }
  1604. std::vector<float> grad_expect() override {
  1605. return {0.0f, 1.0f};
  1606. }
  1607. };
  1608. // GGML_OP_DIAG_MASK_INF
  1609. struct test_diag_mask_inf : public test_case {
  1610. const ggml_type type;
  1611. const std::array<int64_t, 4> ne;
  1612. const int n_past;
  1613. std::string vars() override {
  1614. return VARS_TO_STR3(type, ne, n_past);
  1615. }
  1616. test_diag_mask_inf(ggml_type type = GGML_TYPE_F32,
  1617. std::array<int64_t, 4> ne = {10, 10, 3, 2},
  1618. int n_past = 5)
  1619. : type(type), ne(ne), n_past(n_past) {}
  1620. ggml_tensor * build_graph(ggml_context * ctx) override {
  1621. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1622. ggml_set_param(ctx, a);
  1623. ggml_set_name(a, "a");
  1624. ggml_tensor * out = ggml_diag_mask_inf(ctx, a, n_past);
  1625. ggml_set_name(out, "out");
  1626. return out;
  1627. }
  1628. };
  1629. // GGML_OP_SOFT_MAX
  1630. struct test_soft_max : public test_case {
  1631. const ggml_type type;
  1632. const std::array<int64_t, 4> ne;
  1633. const bool mask;
  1634. const float scale;
  1635. const float max_bias;
  1636. std::string vars() override {
  1637. return VARS_TO_STR5(type, ne, mask, scale, max_bias);
  1638. }
  1639. // the 1024 test with bias occasionally fails:
  1640. // SOFT_MAX(type=f32,ne=[1024,16,1,1],mask=1,scale=1.000000,max_bias=8.000000): [SOFT_MAX] NMSE = 0.000000103 > 0.000000100 FAIL
  1641. virtual double max_nmse_err() override {
  1642. return 1e-6;
  1643. }
  1644. test_soft_max(ggml_type type = GGML_TYPE_F32,
  1645. std::array<int64_t, 4> ne = {10, 5, 4, 3},
  1646. bool mask = false,
  1647. float scale = 1.0f,
  1648. float max_bias = 0.0f)
  1649. : type(type), ne(ne), mask(mask), scale(scale), max_bias(max_bias) {}
  1650. ggml_tensor * build_graph(ggml_context * ctx) override {
  1651. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1652. ggml_set_param(ctx, a);
  1653. ggml_set_name(a, "a");
  1654. ggml_tensor * mask = nullptr;
  1655. if (this->mask) {
  1656. mask = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, ne[0], ne[1]);
  1657. ggml_set_name(mask, "mask");
  1658. }
  1659. ggml_tensor * out = ggml_soft_max_ext(ctx, a, mask, scale, max_bias);
  1660. ggml_set_name(out, "out");
  1661. return out;
  1662. }
  1663. bool grad_precise() override {
  1664. return true;
  1665. }
  1666. };
  1667. // GGML_OP_ROPE
  1668. struct test_rope : public test_case {
  1669. const ggml_type type;
  1670. const std::array<int64_t, 4> ne_a;
  1671. int n_dims;
  1672. int mode;
  1673. int n_ctx; // used to generate positions
  1674. float fs; // freq_scale
  1675. float ef; // ext_factor
  1676. float af; // attn_factor
  1677. bool ff;
  1678. int v; // view (1 : non-contiguous a)
  1679. std::string vars() override {
  1680. return VARS_TO_STR10(type, ne_a, n_dims, mode, n_ctx, fs, ef, af, ff, v);
  1681. }
  1682. test_rope(ggml_type type = GGML_TYPE_F32,
  1683. std::array<int64_t, 4> ne_a = {10, 5, 3, 1},
  1684. int n_dims = 10, int mode = 0, int n_ctx = 512, float fs = 1.0f, float ef = 0.0f, float af = 0.0f, bool ff = false, int v = 0)
  1685. : type(type), ne_a(ne_a), n_dims(n_dims), mode(mode), n_ctx(n_ctx), fs(fs), ef(ef), af(af), ff(ff), v(v) {}
  1686. ggml_tensor * build_graph(ggml_context * ctx) override {
  1687. ggml_tensor * a;
  1688. if (v & 1) {
  1689. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1690. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1691. ggml_set_param(ctx, a);
  1692. ggml_set_name(a, "a");
  1693. a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
  1694. ggml_set_name(a, "view_of_a");
  1695. } else {
  1696. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1697. ggml_set_param(ctx, a);
  1698. ggml_set_name(a, "a");
  1699. }
  1700. ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, ne_a[2]);
  1701. ggml_set_name(pos, "pos");
  1702. ggml_tensor * freq = nullptr;
  1703. if (ff) {
  1704. freq = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_dims/2);
  1705. ggml_set_name(freq, "freq");
  1706. }
  1707. ggml_tensor * out = ggml_rope_ext(ctx, a, pos, freq, n_dims, mode, 0, 10000.0f, fs, ef, af, 1.0f, 1.0f);
  1708. ggml_set_name(out, "out");
  1709. return out;
  1710. }
  1711. void initialize_tensors(ggml_context * ctx) override {
  1712. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1713. if (t->type == GGML_TYPE_I32) {
  1714. // pos
  1715. std::vector<int> data(ne_a[2]);
  1716. for (int i = 0; i < ne_a[2]; i++) {
  1717. data[i] = rand() % n_ctx;
  1718. }
  1719. ggml_backend_tensor_set(t, data.data(), 0, ne_a[2] * sizeof(int));
  1720. } else {
  1721. if (t->ne[0] == n_dims/2) {
  1722. // frequency factors in the range [0.9f, 1.1f]
  1723. init_tensor_uniform(t, 0.9f, 1.1f);
  1724. } else {
  1725. init_tensor_uniform(t);
  1726. }
  1727. }
  1728. }
  1729. }
  1730. double max_maa_err() override {
  1731. return 1e-3;
  1732. }
  1733. bool grad_precise() override {
  1734. return true;
  1735. }
  1736. };
  1737. // GGML_OP_POOL2D
  1738. struct test_pool2d : public test_case {
  1739. enum ggml_op_pool pool_type;
  1740. const ggml_type type_input;
  1741. const std::array<int64_t, 4> ne_input;
  1742. // kernel size
  1743. const int k0;
  1744. const int k1;
  1745. // stride
  1746. const int s0;
  1747. const int s1;
  1748. // padding
  1749. const int p0;
  1750. const int p1;
  1751. std::string vars() override {
  1752. return VARS_TO_STR9(pool_type, type_input, ne_input, k0, k1, s0, s1, p0, p1);
  1753. }
  1754. test_pool2d(ggml_op_pool pool_type = GGML_OP_POOL_AVG,
  1755. ggml_type type_input = GGML_TYPE_F32,
  1756. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1757. int k0 = 3, int k1 = 3,
  1758. int s0 = 1, int s1 = 1,
  1759. int p0 = 1, int p1 = 1)
  1760. : pool_type(pool_type), type_input(type_input), ne_input(ne_input), k0(k0), k1(k1), s0(s0), s1(s1), p0(p0), p1(p1) {}
  1761. ggml_tensor * build_graph(ggml_context * ctx) override {
  1762. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1763. ggml_set_param(ctx, input);
  1764. ggml_set_name(input, "input");
  1765. ggml_tensor * out = ggml_pool_2d(ctx, input, pool_type, k0, k1, s0, s1, p0, p1);
  1766. ggml_set_name(out, "out");
  1767. return out;
  1768. }
  1769. };
  1770. // GGML_OP_CONV_TRANSPOSE_1D
  1771. struct test_conv_transpose_1d : public test_case {
  1772. const std::array<int64_t, 4> ne_input;
  1773. const std::array<int64_t, 4> ne_kernel;
  1774. const int s0; // stride
  1775. const int p0; // padding
  1776. const int d0; // dilation
  1777. std::string vars() override {
  1778. return VARS_TO_STR5(ne_input, ne_kernel, s0, p0, d0);
  1779. }
  1780. test_conv_transpose_1d(std::array<int64_t, 4> ne_input = {197, 32, 1, 1}, // [input_width, input_height, input_channels, 1]
  1781. std::array<int64_t, 4> ne_kernel = {16, 32, 32, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1782. int s0 = 1, int p0 = 0, int d0 = 1)
  1783. : ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), p0(p0), d0(d0) {}
  1784. ggml_tensor * build_graph(ggml_context * ctx) override {
  1785. ggml_tensor * input = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_input.data());
  1786. ggml_set_name(input, "input");
  1787. ggml_tensor * kernel = ggml_new_tensor(ctx, GGML_TYPE_F32, 4, ne_kernel.data());
  1788. ggml_set_name(kernel, "kernel");
  1789. ggml_tensor * out = ggml_conv_transpose_1d(ctx, kernel, input, s0, p0, d0);
  1790. ggml_set_name(out, "out");
  1791. return out;
  1792. }
  1793. };
  1794. // GGML_OP_IM2COL
  1795. struct test_im2col : public test_case {
  1796. const ggml_type type_input;
  1797. const ggml_type type_kernel;
  1798. const ggml_type dst_type;
  1799. const std::array<int64_t, 4> ne_input;
  1800. const std::array<int64_t, 4> ne_kernel;
  1801. // stride
  1802. const int s0;
  1803. const int s1;
  1804. // padding
  1805. const int p0;
  1806. const int p1;
  1807. // dilation
  1808. const int d0;
  1809. const int d1;
  1810. // mode
  1811. const bool is_2D;
  1812. std::string vars() override {
  1813. return VARS_TO_STR12(type_input, type_kernel, dst_type, ne_input, ne_kernel, s0, s1, p0, p1, d0, d1, is_2D);
  1814. }
  1815. test_im2col(ggml_type type_input = GGML_TYPE_F32, ggml_type type_kernel = GGML_TYPE_F16, ggml_type dst_type = GGML_TYPE_F32,
  1816. std::array<int64_t, 4> ne_input = {10, 10, 3, 1}, // [input_width, input_height, input_channels, 1]
  1817. std::array<int64_t, 4> ne_kernel = {3, 3, 3, 1}, // [kernel_width, kernel_height, input_channels, 1]
  1818. int s0 = 1, int s1 = 1,
  1819. int p0 = 1, int p1 = 1,
  1820. int d0 = 1, int d1 = 1,
  1821. bool is_2D = true)
  1822. : type_input(type_input), type_kernel(type_kernel), dst_type(dst_type), ne_input(ne_input), ne_kernel(ne_kernel), s0(s0), s1(s1), p0(p0), p1(p1), d0(d0), d1(d1), is_2D(is_2D) {}
  1823. ggml_tensor * build_graph(ggml_context * ctx) override {
  1824. ggml_tensor * input = ggml_new_tensor(ctx, type_input, 4, ne_input.data());
  1825. ggml_set_param(ctx, input);
  1826. ggml_set_name(input, "input");
  1827. ggml_tensor * kernel = ggml_new_tensor(ctx, type_kernel, 4, ne_kernel.data());
  1828. ggml_set_name(kernel, "kernel");
  1829. ggml_tensor * out = ggml_im2col(ctx, kernel, input, s0, s1, p0, p1, d0, d1, is_2D, dst_type);
  1830. ggml_set_name(out, "out");
  1831. return out;
  1832. }
  1833. };
  1834. // GGML_OP_CONCAT
  1835. struct test_concat : public test_case {
  1836. const ggml_type type;
  1837. const std::array<int64_t, 4> ne_a;
  1838. const int64_t ne_b_d;
  1839. const int dim;
  1840. const int v; // view (1 << 0: non-cont a, 1 << 1: non-cont b)
  1841. std::string vars() override {
  1842. return VARS_TO_STR5(type, ne_a, ne_b_d, dim, v);
  1843. }
  1844. test_concat(ggml_type type = GGML_TYPE_F32,
  1845. std::array<int64_t, 4> ne_a = {10, 5, 5, 5},
  1846. int64_t ne_b_d = 5,
  1847. int dim = 2, int v = 0)
  1848. : type(type), ne_a(ne_a), ne_b_d(ne_b_d), dim(dim), v(v) {}
  1849. ggml_tensor * build_graph(ggml_context * ctx) override {
  1850. auto ne_b = ne_a;
  1851. ne_b[dim] = ne_b_d;
  1852. ggml_tensor * a;
  1853. if (v & 1) {
  1854. auto ne = ne_a; ne[0] *= 2; ne[1] *= 4; ne[2] *= 3;
  1855. a = ggml_new_tensor(ctx, type, 4, ne.data());
  1856. ggml_set_name(a, "a");
  1857. a = ggml_view_4d(ctx, a, ne_a[0], ne_a[1], ne_a[2], ne_a[3], a->nb[1], a->nb[2], a->nb[3], 0);
  1858. ggml_set_name(a, "view_of_a");
  1859. } else {
  1860. a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  1861. ggml_set_name(a, "a");
  1862. }
  1863. ggml_tensor * b;
  1864. if (v & 2) {
  1865. auto ne = ne_b; ne[0] *= 3; ne[1] *= 2; ne[2] *= 4;
  1866. b = ggml_new_tensor(ctx, type, 4, ne.data());
  1867. ggml_set_name(b, "b");
  1868. b = ggml_view_4d(ctx, b, ne_b[0], ne_b[1], ne_b[2], ne_b[3], b->nb[1], b->nb[2], b->nb[3], 0);
  1869. ggml_set_name(b, "view_of_b");
  1870. } else {
  1871. b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  1872. ggml_set_name(b, "b");
  1873. }
  1874. ggml_tensor * out = ggml_concat(ctx, a, b, dim);
  1875. ggml_set_name(out, "out");
  1876. return out;
  1877. }
  1878. };
  1879. // GGML_OP_ARGSORT
  1880. struct test_argsort : public test_case {
  1881. const ggml_type type;
  1882. const std::array<int64_t, 4> ne;
  1883. ggml_sort_order order;
  1884. std::string vars() override {
  1885. return VARS_TO_STR3(type, ne, order);
  1886. }
  1887. test_argsort(ggml_type type = GGML_TYPE_F32,
  1888. std::array<int64_t, 4> ne = {16, 10, 10, 10},
  1889. ggml_sort_order order = GGML_SORT_ORDER_ASC)
  1890. : type(type), ne(ne), order(order) {}
  1891. ggml_tensor * build_graph(ggml_context * ctx) override {
  1892. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1893. ggml_set_name(a, "a");
  1894. ggml_tensor * out = ggml_argsort(ctx, a, order);
  1895. ggml_set_name(out, "out");
  1896. return out;
  1897. }
  1898. void initialize_tensors(ggml_context * ctx) override {
  1899. std::random_device rd;
  1900. std::default_random_engine rng(rd());
  1901. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  1902. if (t->type == GGML_TYPE_I32) {
  1903. // indices
  1904. std::vector<int> data(ggml_nelements(t));
  1905. for (int i = 0; i < ggml_nelements(t); i++) {
  1906. data[i] = rand();
  1907. }
  1908. std::shuffle(data.begin(), data.end(), rng);
  1909. ggml_backend_tensor_set(t, data.data(), 0, ne[0]*ne[1]*ne[2]*ne[3] * sizeof(int));
  1910. } else if (t->type == GGML_TYPE_F32) {
  1911. // initialize with unique values to avoid ties
  1912. for (int64_t r = 0; r < ggml_nrows(t); r++) {
  1913. std::vector<float> data(t->ne[0]);
  1914. for (int i = 0; i < t->ne[0]; i++) {
  1915. data[i] = i;
  1916. }
  1917. std::shuffle(data.begin(), data.end(), rng);
  1918. ggml_backend_tensor_set(t, data.data(), r * t->nb[1], t->ne[0] * sizeof(float));
  1919. }
  1920. } else {
  1921. GGML_ABORT("fatal error");
  1922. }
  1923. }
  1924. }
  1925. };
  1926. // GGML_OP_SUM
  1927. struct test_sum : public test_case {
  1928. const ggml_type type;
  1929. const std::array<int64_t, 4> ne;
  1930. std::string vars() override {
  1931. return VARS_TO_STR2(type, ne);
  1932. }
  1933. test_sum(ggml_type type = GGML_TYPE_F32,
  1934. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1935. : type(type), ne(ne) {}
  1936. ggml_tensor * build_graph(ggml_context * ctx) override {
  1937. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1938. ggml_set_param(ctx, a);
  1939. ggml_set_name(a, "a");
  1940. ggml_tensor * out = ggml_sum(ctx, a);
  1941. ggml_set_name(out, "out");
  1942. return out;
  1943. }
  1944. float grad_eps() override {
  1945. return 0.1f * sqrtf(ne[0]*ne[1]*ne[2]*ne[3]);
  1946. }
  1947. };
  1948. // GGML_OP_SUM_ROWS
  1949. struct test_sum_rows : public test_case {
  1950. const ggml_type type;
  1951. const std::array<int64_t, 4> ne;
  1952. std::string vars() override {
  1953. return VARS_TO_STR2(type, ne);
  1954. }
  1955. test_sum_rows(ggml_type type = GGML_TYPE_F32,
  1956. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  1957. : type(type), ne(ne) {}
  1958. ggml_tensor * build_graph(ggml_context * ctx) override {
  1959. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1960. ggml_set_param(ctx, a);
  1961. ggml_set_name(a, "a");
  1962. ggml_tensor * out = ggml_sum_rows(ctx, a);
  1963. ggml_set_name(out, "out");
  1964. return out;
  1965. }
  1966. };
  1967. // GGML_OP_UPSCALE
  1968. struct test_upscale : public test_case {
  1969. const ggml_type type;
  1970. const std::array<int64_t, 4> ne;
  1971. const int32_t scale_factor;
  1972. const bool transpose;
  1973. std::string vars() override {
  1974. return VARS_TO_STR4(type, ne, scale_factor, transpose);
  1975. }
  1976. test_upscale(ggml_type type = GGML_TYPE_F32,
  1977. std::array<int64_t, 4> ne = {512, 512, 3, 1},
  1978. int32_t scale_factor = 2, bool transpose = false)
  1979. : type(type), ne(ne), scale_factor(scale_factor), transpose(transpose) {}
  1980. ggml_tensor * build_graph(ggml_context * ctx) override {
  1981. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  1982. ggml_set_name(a, "a");
  1983. if (transpose) {
  1984. a = ggml_transpose(ctx, a);
  1985. ggml_set_name(a, "a_transposed");
  1986. }
  1987. ggml_tensor * out = ggml_upscale(ctx, a, scale_factor);
  1988. ggml_set_name(out, "out");
  1989. return out;
  1990. }
  1991. };
  1992. // GGML_OP_UPSCALE (ext)
  1993. struct test_upscale_ext : public test_case {
  1994. const ggml_type type;
  1995. const std::array<int64_t, 4> ne;
  1996. const std::array<int64_t, 4> ne_tgt;
  1997. std::string vars() override {
  1998. return VARS_TO_STR3(type, ne, ne_tgt);
  1999. }
  2000. test_upscale_ext(ggml_type type = GGML_TYPE_F32,
  2001. std::array<int64_t, 4> ne = {2, 5, 7, 11},
  2002. std::array<int64_t, 4> ne_tgt = {5, 7, 11, 13})
  2003. : type(type), ne(ne), ne_tgt(ne_tgt) {}
  2004. ggml_tensor * build_graph(ggml_context * ctx) override {
  2005. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  2006. ggml_set_name(a, "a");
  2007. ggml_tensor * out = ggml_upscale_ext(ctx, a, ne_tgt[0], ne_tgt[1],ne_tgt[2], ne_tgt[3]);
  2008. ggml_set_name(out, "out");
  2009. return out;
  2010. }
  2011. };
  2012. // GGML_OP_GROUP_NORM
  2013. struct test_group_norm : public test_case {
  2014. const ggml_type type;
  2015. const std::array<int64_t, 4> ne;
  2016. const int32_t num_groups;
  2017. const float eps;
  2018. std::string vars() override {
  2019. return VARS_TO_STR3(type, ne, num_groups);
  2020. }
  2021. test_group_norm(ggml_type type = GGML_TYPE_F32,
  2022. std::array<int64_t, 4> ne = {64, 64, 320, 1},
  2023. int32_t num_groups = 32,
  2024. float eps = 1e-6f)
  2025. : type(type), ne(ne), num_groups(num_groups), eps(eps) {}
  2026. ggml_tensor * build_graph(ggml_context * ctx) override {
  2027. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
  2028. ggml_set_name(a, "a");
  2029. ggml_tensor * out = ggml_group_norm(ctx, a, num_groups, eps);
  2030. ggml_set_name(out, "out");
  2031. return out;
  2032. }
  2033. };
  2034. // GGML_OP_ACC
  2035. struct test_acc : public test_case {
  2036. const ggml_type type;
  2037. const std::array<int64_t, 4> ne_a;
  2038. const std::array<int64_t, 4> ne_b;
  2039. std::string vars() override {
  2040. return VARS_TO_STR3(type, ne_a, ne_b);
  2041. }
  2042. test_acc(ggml_type type = GGML_TYPE_F32,
  2043. std::array<int64_t, 4> ne_a = {256, 17, 1, 1},
  2044. std::array<int64_t, 4> ne_b = {256, 16, 1, 1})
  2045. : type(type), ne_a(ne_a), ne_b(ne_b) {}
  2046. ggml_tensor * build_graph(ggml_context * ctx) override {
  2047. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  2048. ggml_set_param(ctx, a);
  2049. ggml_set_name(a, "a");
  2050. ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
  2051. ggml_set_param(ctx, b);
  2052. ggml_set_name(b, "b");
  2053. ggml_tensor * out = ggml_acc(ctx, a, b, a->nb[1], a->nb[2], a->nb[3], b->nb[1]);
  2054. ggml_set_name(out, "out");
  2055. return out;
  2056. }
  2057. };
  2058. // GGML_OP_PAD
  2059. struct test_pad : public test_case {
  2060. const ggml_type type;
  2061. const std::array<int64_t, 4> ne_a;
  2062. const int pad_0;
  2063. const int pad_1;
  2064. std::string vars() override {
  2065. return VARS_TO_STR4(type, ne_a, pad_0, pad_1);
  2066. }
  2067. test_pad(ggml_type type = GGML_TYPE_F32,
  2068. std::array<int64_t, 4> ne_a = {512, 512, 1, 1},
  2069. int pad_0 = 1, int pad_1 = 1)
  2070. : type(type), ne_a(ne_a), pad_0(pad_0), pad_1(pad_1) {}
  2071. ggml_tensor * build_graph(ggml_context * ctx) override {
  2072. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  2073. ggml_set_name(a, "a");
  2074. ggml_tensor * out = ggml_pad(ctx, a, pad_0, pad_1, 0, 0);
  2075. ggml_set_name(out, "out");
  2076. return out;
  2077. }
  2078. };
  2079. // GGML_OP_ARANGE
  2080. struct test_arange : public test_case {
  2081. const ggml_type type;
  2082. const float start;
  2083. const float stop;
  2084. const float step;
  2085. std::string vars() override {
  2086. return VARS_TO_STR4(type, start, stop, step);
  2087. }
  2088. test_arange(ggml_type type = GGML_TYPE_F32,
  2089. float start = 0.f, float stop = 10.f, float step = 1.f)
  2090. : type(type), start(start), stop(stop), step(step) {}
  2091. ggml_tensor * build_graph(ggml_context * ctx) override {
  2092. ggml_tensor * out = ggml_arange(ctx, start, stop, step);
  2093. ggml_set_name(out, "out");
  2094. return out;
  2095. }
  2096. };
  2097. // GGML_OP_TIMESTEP_EMBEDDING
  2098. struct test_timestep_embedding : public test_case {
  2099. const ggml_type type;
  2100. const std::array<int64_t, 4> ne_a;
  2101. const int dim;
  2102. const int max_period;
  2103. std::string vars() override {
  2104. return VARS_TO_STR4(type, ne_a, dim, max_period);
  2105. }
  2106. test_timestep_embedding(ggml_type type = GGML_TYPE_F32,
  2107. std::array<int64_t, 4> ne_a = {2, 1, 1, 1},
  2108. int dim = 320, int max_period=10000)
  2109. : type(type), ne_a(ne_a), dim(dim), max_period(max_period) {}
  2110. ggml_tensor * build_graph(ggml_context * ctx) override {
  2111. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  2112. ggml_set_name(a, "a");
  2113. ggml_tensor * out = ggml_timestep_embedding(ctx, a, dim, max_period);
  2114. ggml_set_name(out, "out");
  2115. return out;
  2116. }
  2117. };
  2118. // GGML_OP_LEAKY_RELU
  2119. struct test_leaky_relu : public test_case {
  2120. const ggml_type type;
  2121. const std::array<int64_t, 4> ne_a;
  2122. const float negative_slope;
  2123. std::string vars() override {
  2124. return VARS_TO_STR3(type, ne_a, negative_slope);
  2125. }
  2126. test_leaky_relu(ggml_type type = GGML_TYPE_F32,
  2127. std::array<int64_t, 4> ne_a = {10, 5, 4, 3},
  2128. float negative_slope = 0.1f)
  2129. : type(type), ne_a(ne_a), negative_slope(negative_slope) {}
  2130. ggml_tensor * build_graph(ggml_context * ctx) override {
  2131. ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
  2132. ggml_set_name(a, "a");
  2133. ggml_tensor * out = ggml_leaky_relu(ctx, a, negative_slope, true);
  2134. ggml_set_name(out, "out");
  2135. return out;
  2136. }
  2137. };
  2138. // GGML_OP_FLASH_ATTN_EXT
  2139. struct test_flash_attn_ext : public test_case {
  2140. const int64_t hs; // head size
  2141. const int64_t nh; // num heads
  2142. const int64_t kv; // kv size
  2143. const int64_t nb; // batch size
  2144. const bool mask; // use mask
  2145. const float max_bias; // ALiBi
  2146. const float logit_softcap; // Gemma 2
  2147. const ggml_type type_KV;
  2148. std::string vars() override {
  2149. return VARS_TO_STR8(hs, nh, kv, nb, mask, max_bias, logit_softcap, type_KV);
  2150. }
  2151. double max_nmse_err() override {
  2152. return 5e-4;
  2153. }
  2154. test_flash_attn_ext(int64_t hs = 128, int64_t nh = 32, int64_t kv = 96, int64_t nb = 8,
  2155. bool mask = true, float max_bias = 0.0f, float logit_softcap = 0.0f, ggml_type type_KV = GGML_TYPE_F16)
  2156. : hs(hs), nh(nh), kv(kv), nb(nb), mask(mask), max_bias(max_bias), logit_softcap(logit_softcap), type_KV(type_KV) {}
  2157. ggml_tensor * build_graph(ggml_context * ctx) override {
  2158. const int64_t hs_padded = GGML_PAD(hs, ggml_blck_size(type_KV));
  2159. ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs_padded, nb, nh, 1);
  2160. ggml_set_name(q, "q");
  2161. ggml_tensor * k = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  2162. ggml_set_name(k, "k");
  2163. ggml_tensor * v = ggml_new_tensor_4d(ctx, type_KV, hs_padded, kv, nh, 1);
  2164. ggml_set_name(v, "v");
  2165. ggml_tensor * m = nullptr;
  2166. if (mask) {
  2167. m = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, GGML_PAD(nb, GGML_KQ_MASK_PAD), 1, 1);
  2168. ggml_set_name(m, "m");
  2169. }
  2170. ggml_tensor * out = ggml_flash_attn_ext(ctx, q, k, v, m, 1.0f/sqrtf(hs), max_bias, logit_softcap);
  2171. ggml_set_name(out, "out");
  2172. return out;
  2173. }
  2174. bool grad_precise() override {
  2175. return true;
  2176. }
  2177. };
  2178. // GGML_OP_CROSS_ENTROPY_LOSS
  2179. struct test_cross_entropy_loss : public test_case {
  2180. const ggml_type type;
  2181. const std::array<int64_t, 4> ne;
  2182. std::string vars() override {
  2183. return VARS_TO_STR2(type, ne);
  2184. }
  2185. test_cross_entropy_loss(ggml_type type = GGML_TYPE_F32,
  2186. std::array<int64_t, 4> ne = {10, 5, 4, 3})
  2187. : type(type), ne(ne) {}
  2188. ggml_tensor * build_graph(ggml_context * ctx) override {
  2189. ggml_tensor * logits = ggml_new_tensor(ctx, type, 4, ne.data());
  2190. ggml_set_param(ctx, logits);
  2191. ggml_set_name(logits, "logits");
  2192. ggml_tensor * labels = ggml_new_tensor(ctx, type, 4, ne.data());
  2193. // The labels are assumed to be constant -> no gradients.
  2194. ggml_set_name(labels, "labels");
  2195. // Ensure labels add up to 1:
  2196. labels = ggml_soft_max(ctx, labels);
  2197. ggml_set_name(labels, "labels_normalized");
  2198. ggml_tensor * out = ggml_cross_entropy_loss(ctx, logits, labels);
  2199. ggml_set_name(out, "out");
  2200. return out;
  2201. }
  2202. void initialize_tensors(ggml_context * ctx) override {
  2203. // For larger abs. diffs between logits softmax is more linear, therefore more precise num. gradients.
  2204. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  2205. init_tensor_uniform(t, -100.0f, 100.0f);
  2206. }
  2207. }
  2208. float grad_eps() override {
  2209. return 1.0f;
  2210. }
  2211. bool grad_precise() override {
  2212. return true;
  2213. }
  2214. };
  2215. // GGML_OP_OPT_STEP_ADAMW
  2216. struct test_opt_step_adamw : public test_case {
  2217. const ggml_type type;
  2218. const std::array<int64_t, 4> ne;
  2219. const float alpha;
  2220. const float beta1;
  2221. const float beta2;
  2222. const float eps;
  2223. const float wd;
  2224. std::string vars() override {
  2225. return VARS_TO_STR7(type, ne, alpha, beta1, beta2, eps, wd);
  2226. }
  2227. test_opt_step_adamw(ggml_type type = GGML_TYPE_F32,
  2228. std::array<int64_t, 4> ne = {10, 5, 4, 3},
  2229. float alpha = 1e-3f,
  2230. float beta1 = 0.9f,
  2231. float beta2 = 0.999f,
  2232. float eps = 1e-8f,
  2233. float wd = 0.0f)
  2234. : type(type), ne(ne), alpha(alpha), beta1(beta1), beta2(beta2), eps(eps), wd(wd) {}
  2235. ggml_tensor * build_graph(ggml_context * ctx) override {
  2236. ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]);
  2237. ggml_set_param(ctx, a); // Despite tensor a having gradients the output tensor will not.
  2238. ggml_set_name(a, "a");
  2239. ggml_tensor * out = ggml_opt_step_adamw(ctx, a, alpha, beta1, beta2, eps, wd);
  2240. ggml_set_name(out, "out");
  2241. return out;
  2242. }
  2243. void initialize_tensors(ggml_context * ctx) override {
  2244. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  2245. init_tensor_uniform(t, 0.0f, 1.0f); // grad_v needs non-negative values.
  2246. }
  2247. }
  2248. bool grad_precise() override {
  2249. return true;
  2250. }
  2251. };
  2252. enum llm_norm_type {
  2253. LLM_NORM,
  2254. LLM_NORM_RMS,
  2255. };
  2256. struct llama_hparams {
  2257. uint32_t n_vocab;
  2258. uint32_t n_embd;
  2259. uint32_t n_head;
  2260. uint32_t n_head_kv;
  2261. static constexpr uint32_t n_layer = 1;
  2262. uint32_t n_rot;
  2263. uint32_t n_embd_head; // dimension of values (d_v)
  2264. uint32_t n_ff;
  2265. float f_norm_eps;
  2266. float f_norm_rms_eps;
  2267. // cparams
  2268. static constexpr uint32_t n_ctx = 512; // user-specified context size
  2269. static constexpr uint32_t n_ctx_orig = n_ctx;
  2270. // batch
  2271. int32_t n_tokens;
  2272. // llm_build_context
  2273. static constexpr int32_t n_kv = 32; // size of KV cache to consider (n_kv <= n_ctx
  2274. static constexpr int32_t kv_head = 1; // index of where we store new KV data in the cache
  2275. uint32_t n_embd_gqa() const { // dimension of key embeddings across all k-v heads
  2276. return n_embd_head * n_head_kv;
  2277. }
  2278. };
  2279. // LLM base class
  2280. struct test_llm : public test_case {
  2281. llama_hparams hp;
  2282. protected:
  2283. test_llm(llama_hparams hp)
  2284. : hp(std::move(hp)) {
  2285. }
  2286. public:
  2287. struct ggml_tensor * llm_build_norm(
  2288. struct ggml_context * ctx,
  2289. struct ggml_tensor * cur,
  2290. struct ggml_tensor * mw,
  2291. struct ggml_tensor * mb,
  2292. llm_norm_type type) {
  2293. switch (type) {
  2294. case LLM_NORM: cur = ggml_norm (ctx, cur, hp.f_norm_eps); break;
  2295. case LLM_NORM_RMS: cur = ggml_rms_norm(ctx, cur, hp.f_norm_rms_eps); break;
  2296. }
  2297. cur = ggml_mul(ctx, cur, mw);
  2298. if (mb) {
  2299. cur = ggml_add(ctx, cur, mb);
  2300. }
  2301. return cur;
  2302. }
  2303. void llm_build_kv_store(
  2304. struct ggml_context * ctx,
  2305. struct ggml_tensor * k_l,
  2306. struct ggml_tensor * v_l,
  2307. struct ggml_tensor * k_cur,
  2308. struct ggml_tensor * v_cur) {
  2309. // compute the transposed [n_tokens, n_embd] V matrix
  2310. struct ggml_tensor * v_cur_t = ggml_transpose(ctx, ggml_reshape_2d(ctx, v_cur, hp.n_embd_gqa(), hp.n_tokens));
  2311. struct ggml_tensor * k_cache_view = ggml_view_1d(ctx, k_l, hp.n_tokens*hp.n_embd_gqa(),
  2312. (ggml_row_size(k_l->type, hp.n_embd_gqa()))*hp.kv_head);
  2313. struct ggml_tensor * v_cache_view = ggml_view_2d(ctx, v_l, hp.n_tokens, hp.n_embd_gqa(),
  2314. ( hp.n_ctx)*ggml_element_size(v_l),
  2315. (hp.kv_head)*ggml_element_size(v_l));
  2316. // important: storing RoPE-ed version of K in the KV cache!
  2317. ggml_cpy(ctx, k_cur, k_cache_view);
  2318. ggml_cpy(ctx, v_cur_t, v_cache_view);
  2319. }
  2320. struct ggml_tensor * llm_build_kqv(
  2321. struct ggml_context * ctx,
  2322. struct ggml_tensor * k_l,
  2323. struct ggml_tensor * v_l,
  2324. struct ggml_tensor * q_cur,
  2325. struct ggml_tensor * kq_mask,
  2326. float kq_scale) {
  2327. struct ggml_tensor * q = ggml_permute(ctx, q_cur, 0, 2, 1, 3);
  2328. struct ggml_tensor * k =
  2329. ggml_view_3d(ctx, k_l,
  2330. hp.n_embd_head, hp.n_kv, hp.n_head_kv,
  2331. ggml_row_size(k_l->type, hp.n_embd_gqa()),
  2332. ggml_row_size(k_l->type, hp.n_embd_head),
  2333. 0);
  2334. struct ggml_tensor * kq = ggml_mul_mat(ctx, k, q);
  2335. kq = ggml_soft_max_ext(ctx, kq, kq_mask, kq_scale, 0.0f);
  2336. // split cached v into n_head heads
  2337. struct ggml_tensor * v =
  2338. ggml_view_3d(ctx, v_l,
  2339. hp.n_kv, hp.n_embd_head, hp.n_head_kv,
  2340. ggml_element_size(v_l)*hp.n_ctx,
  2341. ggml_element_size(v_l)*hp.n_ctx*hp.n_embd_head,
  2342. 0);
  2343. struct ggml_tensor * kqv = ggml_mul_mat(ctx, v, kq);
  2344. struct ggml_tensor * kqv_merged = ggml_permute(ctx, kqv, 0, 2, 1, 3);
  2345. struct ggml_tensor * cur = ggml_cont_2d(ctx, kqv_merged, hp.n_embd_head*hp.n_head, hp.n_tokens);
  2346. struct ggml_tensor * wo = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  2347. cur = ggml_mul_mat(ctx, wo, cur);
  2348. return cur;
  2349. }
  2350. void initialize_tensors(ggml_context * ctx) override {
  2351. for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
  2352. if (t->type == GGML_TYPE_I32) {
  2353. // pos
  2354. std::vector<int> data(hp.n_tokens);
  2355. for (int i = 0; i < hp.n_tokens; i++) {
  2356. data[i] = rand() % hp.n_ctx;
  2357. }
  2358. ggml_backend_tensor_set(t, data.data(), 0, hp.n_tokens * sizeof(int));
  2359. } else {
  2360. init_tensor_uniform(t);
  2361. }
  2362. }
  2363. }
  2364. };
  2365. // Llama
  2366. struct test_llama : public test_llm {
  2367. static constexpr float freq_base = 10000.0f;
  2368. static constexpr float freq_scale = 1.0f;
  2369. static constexpr float ext_factor = 0.0f;
  2370. static constexpr float attn_factor = 1.0f;
  2371. static constexpr float beta_fast = 32.0f;
  2372. static constexpr float beta_slow = 1.0f;
  2373. std::string op_desc(ggml_tensor * t) override {
  2374. GGML_UNUSED(t);
  2375. return "LLAMA";
  2376. }
  2377. std::string vars() override {
  2378. auto n_tokens = hp.n_tokens;
  2379. return VARS_TO_STR1(n_tokens);
  2380. }
  2381. double max_nmse_err() override {
  2382. return 2e-3;
  2383. }
  2384. test_llama(int n_tokens = 1)
  2385. : test_llm({
  2386. /*n_vocab =*/ 32000,
  2387. /*n_embd =*/ 3200,
  2388. /*n_head =*/ 32,
  2389. /*n_head_kv =*/ 32,
  2390. /*n_rot =*/ 100,
  2391. /*n_embd_head =*/ 100,
  2392. /*n_ff =*/ 8640,
  2393. /*f_norm_eps =*/ 0.f,
  2394. /*f_norm_rms_eps =*/ 1e-5f,
  2395. /*n_tokens =*/ n_tokens,
  2396. }) {
  2397. }
  2398. ggml_tensor * build_graph(ggml_context * ctx) override {
  2399. struct ggml_tensor * cur;
  2400. struct ggml_tensor * inpL;
  2401. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  2402. // inp_pos - contains the positions
  2403. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  2404. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  2405. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  2406. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  2407. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  2408. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  2409. struct ggml_tensor * inpSA = inpL;
  2410. // norm
  2411. ggml_tensor * attn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2412. cur = llm_build_norm(ctx, inpL, attn_norm, nullptr, LLM_NORM_RMS);
  2413. // self-attention
  2414. {
  2415. ggml_tensor * wq = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd);
  2416. ggml_tensor * wk = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  2417. ggml_tensor * wv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd_gqa());
  2418. // compute Q and K and RoPE them
  2419. struct ggml_tensor * Qcur = ggml_mul_mat(ctx, wq, cur);
  2420. struct ggml_tensor * Kcur = ggml_mul_mat(ctx, wk, cur);
  2421. struct ggml_tensor * Vcur = ggml_mul_mat(ctx, wv, cur);
  2422. Qcur = ggml_rope_ext(
  2423. ctx, ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens), inp_pos, nullptr,
  2424. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  2425. ext_factor, attn_factor, beta_fast, beta_slow
  2426. );
  2427. Kcur = ggml_rope_ext(
  2428. ctx, ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens), inp_pos, nullptr,
  2429. hp.n_rot, 0, hp.n_ctx_orig, freq_base, freq_scale,
  2430. ext_factor, attn_factor, beta_fast, beta_slow
  2431. );
  2432. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  2433. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  2434. }
  2435. struct ggml_tensor * ffn_inp = ggml_add(ctx, cur, inpSA);
  2436. // feed-forward network
  2437. ggml_tensor * ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2438. cur = llm_build_norm(ctx, ffn_inp, ffn_norm, nullptr, LLM_NORM_RMS);
  2439. ggml_tensor * ffn_gate = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  2440. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  2441. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  2442. struct ggml_tensor * tmp = ggml_mul_mat(ctx, ffn_up, cur);
  2443. cur = ggml_mul_mat(ctx, ffn_gate, cur);
  2444. cur = ggml_silu(ctx, cur);
  2445. cur = ggml_mul(ctx, cur, tmp);
  2446. cur = ggml_mul_mat(ctx, ffn_down, cur);
  2447. cur = ggml_add(ctx, cur, ffn_inp);
  2448. // input for next layer
  2449. inpL = cur;
  2450. }
  2451. cur = inpL;
  2452. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2453. cur = llm_build_norm(ctx, cur, output_norm, nullptr, LLM_NORM_RMS);
  2454. // lm_head
  2455. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_vocab);
  2456. cur = ggml_mul_mat(ctx, output, cur);
  2457. return cur;
  2458. }
  2459. };
  2460. // Falcon
  2461. struct test_falcon : public test_llm {
  2462. static constexpr float freq_base = 10000.0f;
  2463. static constexpr float freq_scale = 1.0f;
  2464. static constexpr float ext_factor = 0.0f;
  2465. static constexpr float attn_factor = 1.0f;
  2466. static constexpr float beta_fast = 32.0f;
  2467. static constexpr float beta_slow = 1.0f;
  2468. std::string op_desc(ggml_tensor * t) override {
  2469. GGML_UNUSED(t);
  2470. return "FALCON";
  2471. }
  2472. std::string vars() override {
  2473. auto n_tokens = hp.n_tokens;
  2474. return VARS_TO_STR1(n_tokens);
  2475. }
  2476. double max_nmse_err() override {
  2477. return 2e-3;
  2478. }
  2479. test_falcon(int n_tokens = 1)
  2480. : test_llm({
  2481. /*n_vocab =*/ 32000,
  2482. /*n_embd =*/ 3200,
  2483. /*n_head =*/ 50,
  2484. /*n_head_kv =*/ 1,
  2485. /*n_rot =*/ 64,
  2486. /*n_embd_head =*/ 64,
  2487. /*n_ff =*/ 8640,
  2488. /*f_norm_eps =*/ 1e-5f,
  2489. /*f_norm_rms_eps =*/ 0.f,
  2490. /*n_tokens =*/ n_tokens,
  2491. }) {
  2492. }
  2493. ggml_tensor * build_graph(ggml_context * ctx) override {
  2494. struct ggml_tensor * cur;
  2495. struct ggml_tensor * inpL;
  2496. inpL = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, hp.n_embd, hp.n_tokens);
  2497. // inp_pos - contains the positions
  2498. struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, hp.n_tokens);
  2499. // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
  2500. struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx, GGML_TYPE_F16, hp.n_kv, hp.n_tokens, 1);
  2501. ggml_tensor * k_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  2502. ggml_tensor * v_l = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 1638400);
  2503. for (uint32_t il = 0; il < hp.n_layer; ++il) {
  2504. // norm
  2505. ggml_tensor * attn_norm_w = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2506. ggml_tensor * attn_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2507. ggml_tensor * attn_norm = llm_build_norm(ctx, inpL, attn_norm_w, attn_norm_b, LLM_NORM);
  2508. // self-attention
  2509. {
  2510. cur = attn_norm;
  2511. ggml_tensor * wqkv = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_embd + 2*hp.n_embd_gqa());
  2512. cur = ggml_mul_mat(ctx, wqkv, cur);
  2513. struct ggml_tensor * Qcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd, hp.n_tokens, cur->nb[1], 0*sizeof(float)*(hp.n_embd)));
  2514. struct ggml_tensor * Kcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd_gqa(), hp.n_tokens, cur->nb[1], 1*sizeof(float)*(hp.n_embd)));
  2515. struct ggml_tensor * Vcur = ggml_cont(ctx, ggml_view_2d(ctx, cur, hp.n_embd_gqa(), hp.n_tokens, cur->nb[1], 1*sizeof(float)*(hp.n_embd + hp.n_embd_gqa())));
  2516. Qcur = ggml_reshape_3d(ctx, Qcur, hp.n_embd_head, hp.n_head, hp.n_tokens);
  2517. Kcur = ggml_reshape_3d(ctx, Kcur, hp.n_embd_head, hp.n_head_kv, hp.n_tokens);
  2518. // using mode = 2 for neox mode
  2519. Qcur = ggml_rope_ext(
  2520. ctx, Qcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  2521. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  2522. );
  2523. Kcur = ggml_rope_ext(
  2524. ctx, Kcur, inp_pos, nullptr, hp.n_rot, 2, hp.n_ctx_orig,
  2525. freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
  2526. );
  2527. llm_build_kv_store(ctx, k_l, v_l, Kcur, Vcur);
  2528. cur = llm_build_kqv(ctx, k_l, v_l, Qcur, KQ_mask, 1.0f/sqrtf(float(hp.n_embd_head)));
  2529. }
  2530. struct ggml_tensor * ffn_inp = cur;
  2531. // feed forward
  2532. {
  2533. ggml_tensor * ffn_up = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_embd, hp.n_ff);
  2534. ggml_tensor * ffn_down = ggml_new_tensor_2d(ctx, GGML_TYPE_Q4_0, hp.n_ff, hp.n_embd);
  2535. cur = attn_norm;
  2536. cur = ggml_mul_mat(ctx, ffn_up, cur);
  2537. cur = ggml_gelu(ctx, cur);
  2538. cur = ggml_mul_mat(ctx, ffn_down, cur);
  2539. }
  2540. cur = ggml_add(ctx, cur, ffn_inp);
  2541. cur = ggml_add(ctx, cur, inpL);
  2542. // input for next layer
  2543. inpL = cur;
  2544. }
  2545. cur = inpL;
  2546. ggml_tensor * output_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2547. ggml_tensor * output_norm_b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hp.n_embd);
  2548. cur = llm_build_norm(ctx, cur, output_norm, output_norm_b, LLM_NORM);
  2549. // lm_head
  2550. ggml_tensor * output = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, hp.n_embd, hp.n_vocab);
  2551. cur = ggml_mul_mat(ctx, output, cur);
  2552. return cur;
  2553. }
  2554. };
  2555. // ###########################################
  2556. // ## Section 3: GGML Op Test Instantiation ##
  2557. // ###########################################
  2558. static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op_name) {
  2559. std::vector<std::unique_ptr<test_case>> test_cases;
  2560. std::default_random_engine rng(0);
  2561. const ggml_type all_types[] = {
  2562. GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16,
  2563. GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
  2564. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  2565. GGML_TYPE_Q8_0,
  2566. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  2567. GGML_TYPE_Q4_K, GGML_TYPE_Q5_K,
  2568. GGML_TYPE_Q6_K,
  2569. // GGML_TYPE_TQ1_0, GGML_TYPE_TQ2_0, // TODO: implement for all backends
  2570. GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  2571. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  2572. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  2573. };
  2574. const ggml_type base_types[] = {
  2575. GGML_TYPE_F32, GGML_TYPE_F16,
  2576. GGML_TYPE_Q4_0,
  2577. GGML_TYPE_Q4_K,
  2578. GGML_TYPE_IQ2_XXS
  2579. };
  2580. const ggml_type other_types[] = {
  2581. GGML_TYPE_Q4_1,
  2582. GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
  2583. GGML_TYPE_Q8_0,
  2584. GGML_TYPE_Q2_K, GGML_TYPE_Q3_K,
  2585. GGML_TYPE_Q5_K,
  2586. GGML_TYPE_Q6_K,
  2587. // GGML_TYPE_TQ1_0, GGML_TYPE_TQ2_0, // TODO: implement for all backends
  2588. GGML_TYPE_IQ2_XS, GGML_TYPE_IQ2_S,
  2589. GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M,
  2590. GGML_TYPE_IQ4_NL, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS,
  2591. GGML_TYPE_BF16,
  2592. };
  2593. // unary ops
  2594. for (int v : {0, 1}) {
  2595. for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
  2596. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 128, 2, 2, 2 }, v));
  2597. test_cases.emplace_back(new test_unary((ggml_unary_op) op, GGML_TYPE_F32, { 5, 7, 11, 13 }, v));
  2598. }
  2599. }
  2600. test_cases.emplace_back(new test_get_rows(GGML_TYPE_F32, 1, 8, 2, 1, false));
  2601. for (ggml_type type : all_types) {
  2602. for (int b : {1, 7}) {
  2603. for (bool v : {false, true}) {
  2604. test_cases.emplace_back(new test_get_rows(type, 256, 5, 4, b, v));
  2605. }
  2606. }
  2607. }
  2608. for (int b : {1, 7}) {
  2609. for (bool v : {false, true}) {
  2610. test_cases.emplace_back(new test_get_rows(GGML_TYPE_I32, 256, 5, 4, b, v));
  2611. }
  2612. }
  2613. for (ggml_type type_input : {GGML_TYPE_F32}) {
  2614. for (ggml_op_pool pool_type : {GGML_OP_POOL_AVG, GGML_OP_POOL_MAX}) {
  2615. for (int k0 : {1, 3}) {
  2616. for (int k1 : {1, 3}) {
  2617. for (int s0 : {1, 2}) {
  2618. for (int s1 : {1, 2}) {
  2619. for (int p0 : {0, 1}) {
  2620. for (int p1 : {0, 1}) {
  2621. test_cases.emplace_back(new test_pool2d(pool_type, type_input, {10, 10, 3, 1}, k0, k1, s0, s1, p0, p1));
  2622. }
  2623. }
  2624. }
  2625. }
  2626. }
  2627. }
  2628. }
  2629. }
  2630. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32));
  2631. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32));
  2632. test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F16));
  2633. // test cases for 1D im2col
  2634. 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));
  2635. 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));
  2636. 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));
  2637. // sycl backend will limit task global_range < MAX_INT
  2638. // test cases for 2D im2col with large input W and H (occurs in stable-diffusion)
  2639. // however these cases need to alloc more memory which may fail in some devices (Intel Arc770, etc.)
  2640. // these cases are verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend)
  2641. // 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));
  2642. // 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));
  2643. test_cases.emplace_back(new test_conv_transpose_1d());
  2644. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 3, 0, 1));
  2645. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 2, 0, 1));
  2646. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {2,3,2,1}, 1, 0, 1));
  2647. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 2, 0, 1));
  2648. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,2,2,1}, 1, 0, 1));
  2649. test_cases.emplace_back(new test_conv_transpose_1d({3,2,1,1}, {3,1,2,1}, 1, 0, 1));
  2650. test_cases.emplace_back(new test_conv_transpose_1d({2,1,1,1}, {3,1,1,1}, 1, 0, 1));
  2651. for (int ne3 : {1, 3}) { // CUDA backwards pass only supports ne3 == 1
  2652. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {1, 1, 1, 1}));
  2653. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {2, 1, 1, 1}));
  2654. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {1, 2, 1, 1}));
  2655. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {1, 1, 2, 1}));
  2656. test_cases.emplace_back(new test_repeat(GGML_TYPE_F32, {10, 5, 4, ne3}, {1, 1, 1, 2}));
  2657. test_cases.emplace_back(new test_repeat(GGML_TYPE_I32, {10, 5, 4, ne3}, {2, 1, 1, 1}));
  2658. test_cases.emplace_back(new test_repeat(GGML_TYPE_I16, {10, 5, 4, ne3}, {1, 1, 1, 2}));
  2659. }
  2660. test_cases.emplace_back(new test_dup(GGML_TYPE_F32));
  2661. test_cases.emplace_back(new test_dup(GGML_TYPE_F16));
  2662. test_cases.emplace_back(new test_dup(GGML_TYPE_I32));
  2663. test_cases.emplace_back(new test_dup(GGML_TYPE_I16));
  2664. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {0, 2, 1, 3}));
  2665. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {0, 2, 1, 3})); // dup by rows
  2666. test_cases.emplace_back(new test_dup(GGML_TYPE_F32, {10, 10, 5, 1}, {1, 0, 2, 3}));
  2667. test_cases.emplace_back(new test_dup(GGML_TYPE_F16, {10, 10, 5, 1}, {1, 0, 2, 3})); // dup dst not-contiguous
  2668. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {0, 2, 1, 3}));
  2669. test_cases.emplace_back(new test_dup(GGML_TYPE_I16, {10, 8, 3, 1}, {1, 2, 0, 3}));
  2670. for (int dim = 1; dim < GGML_MAX_DIMS; ++dim) {
  2671. test_cases.emplace_back(new test_set(GGML_TYPE_F32, GGML_TYPE_F32, {6, 5, 4, 3}, dim));
  2672. }
  2673. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  2674. for (ggml_type type_dst : all_types) {
  2675. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 4, 4, 4}));
  2676. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {0, 2, 1, 3})); // cpy by rows
  2677. }
  2678. }
  2679. for (ggml_type type_src : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  2680. for (ggml_type type_dst : {GGML_TYPE_F16, GGML_TYPE_F32}) {
  2681. test_cases.emplace_back(new test_cpy(type_src, type_dst, {256, 2, 3, 4}, {1, 0, 2, 3})); // cpy not-contiguous
  2682. }
  2683. }
  2684. test_cases.emplace_back(new test_cont());
  2685. test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {2, 1, 1 ,1}));
  2686. test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {2, 1, 3 ,5}));
  2687. test_cases.emplace_back(new test_cont(GGML_TYPE_F32, {2, 3, 5 ,7}));
  2688. test_cases.emplace_back(new test_cont(GGML_TYPE_F16, {2, 1, 1 ,1}));
  2689. test_cases.emplace_back(new test_cont(GGML_TYPE_F16, {2, 1, 3 ,5}));
  2690. test_cases.emplace_back(new test_cont(GGML_TYPE_F16, {2, 3, 5 ,7}));
  2691. test_cases.emplace_back(new test_cont(GGML_TYPE_BF16, {2, 1, 1 ,1}));
  2692. test_cases.emplace_back(new test_cont(GGML_TYPE_BF16, {2, 1, 3 ,5}));
  2693. test_cases.emplace_back(new test_cont(GGML_TYPE_BF16, {2, 3, 5 ,7}));
  2694. auto add_test_bin_bcast = [&](ggml_type type, std::array<int64_t, 4> ne, std::array<int, 4> nr) {
  2695. for (auto op : {ggml_add, ggml_mul, ggml_div}) {
  2696. test_cases.emplace_back(new test_bin_bcast(op, type, ne, nr));
  2697. }
  2698. };
  2699. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 8, 1}, {1, 1, 1, 1});
  2700. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1, 1}, {32, 1, 1, 1});
  2701. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 320, 320}, {1, 1, 1, 1});
  2702. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 1, 1}, {1, 1, 1, 1});
  2703. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 1}, {1, 1, 1, 1});
  2704. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 1, 1});
  2705. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {2, 1, 1, 1});
  2706. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 2, 1, 1});
  2707. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 2, 1});
  2708. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 1, 2});
  2709. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 1, 2, 2});
  2710. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {1, 2, 2, 2});
  2711. add_test_bin_bcast(GGML_TYPE_F32, {10, 5, 4, 3}, {2, 2, 2, 2});
  2712. // stable diffusion
  2713. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 1, 1, 1});
  2714. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 16, 16, 1});
  2715. add_test_bin_bcast(GGML_TYPE_F32, {1280, 16, 16, 1}, {1, 1, 1, 1});
  2716. add_test_bin_bcast(GGML_TYPE_F32, {1280, 1, 1, 1}, {1, 256, 1, 1});
  2717. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {16, 16, 1, 1});
  2718. add_test_bin_bcast(GGML_TYPE_F32, {16, 16, 1280, 1}, {1, 1, 1, 1});
  2719. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {16, 16, 1, 1});
  2720. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 2560, 1}, {16, 16, 1, 1});
  2721. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1280, 1}, {32, 32, 1, 1});
  2722. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 1920, 1}, {32, 32, 1, 1});
  2723. add_test_bin_bcast(GGML_TYPE_F32, {1, 1, 640, 1}, {32, 32, 1, 1});
  2724. add_test_bin_bcast(GGML_TYPE_F32, {5120, 1, 1, 1}, {1, 256, 1, 1});
  2725. add_test_bin_bcast(GGML_TYPE_F32, {640, 1, 1, 1}, {1, 1, 1, 1});
  2726. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {1, 1, 1, 1});
  2727. //add_test_bin_bcast(GGML_TYPE_F32, {3, 3, 2560, 1280}, {2, 1, 1, 1});
  2728. test_cases.emplace_back(new test_add1());
  2729. test_cases.emplace_back(new test_scale());
  2730. for (float eps : {1e-6f, 1e-5f, 1e-3f, 1e-1f}) {
  2731. test_cases.emplace_back(new test_norm(GGML_TYPE_F32, {64, 5, 4, 3}, eps));
  2732. test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 5, 4, 3}, eps));
  2733. }
  2734. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 1536, 1, 1}, {4, 1536, 1, 1}));
  2735. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {8, 1536, 1, 1}, {4, 1536, 1, 1}));
  2736. test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 1536, 4, 1}, {4, 1536, 1, 1}));
  2737. test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 16, 1024, 32, 4));
  2738. test_cases.emplace_back(new test_rwkv_wkv(GGML_TYPE_F32, 32, 64, 1, 1));
  2739. test_cases.emplace_back(new test_rwkv_wkv(GGML_TYPE_F32, 32, 64, 32, 1));
  2740. test_cases.emplace_back(new test_rwkv_wkv(GGML_TYPE_F32, 32, 64, 32, 4));
  2741. test_cases.emplace_back(new test_rwkv_wkv(GGML_TYPE_F32, 32, 64, 128, 4));
  2742. #if 1
  2743. for (ggml_type type_a : base_types) {
  2744. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  2745. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, { 1, 1}, {1, 1}));
  2746. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {1, 1}));
  2747. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 1}, {2, 1}));
  2748. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 1}));
  2749. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 1}));
  2750. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {1, 2}));
  2751. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {10, 10}, {2, 2}));
  2752. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, { 1, 1}, {1, 1}));
  2753. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {1, 1}));
  2754. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 1}, {2, 1}));
  2755. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 1}));
  2756. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 1}));
  2757. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {1, 2}));
  2758. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 16, 256, {10, 10}, {2, 2}));
  2759. }
  2760. }
  2761. #else
  2762. // m = a rows
  2763. // n = b rows
  2764. // k = cols
  2765. std::uniform_int_distribution<> dist_m(1, 128);
  2766. std::uniform_int_distribution<> dist_n(16, 128);
  2767. std::uniform_int_distribution<> dist_k(1, 16);
  2768. for (int i = 0; i < 1000; i++) {
  2769. for (ggml_type type_a : all_types) {
  2770. for (ggml_type type_b : {GGML_TYPE_F32}) {
  2771. int m = dist_m(rng);
  2772. int n = dist_n(rng);
  2773. int k = dist_k(rng) * ggml_blck_size(type_a);
  2774. test_cases.emplace_back(new test_mul_mat(type_a, type_b, m, n, k, { 1, 1}, {1, 1}));
  2775. }
  2776. }
  2777. }
  2778. #endif
  2779. for (ggml_type type_a : other_types) {
  2780. for (ggml_type type_b : {GGML_TYPE_F32}) {
  2781. if (ggml_blck_size(type_a) != 256) {
  2782. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, ggml_blck_size(type_a), {1, 1}, {1, 1}));
  2783. }
  2784. test_cases.emplace_back(new test_mul_mat(type_a, type_b, 16, 1, 256, {1, 1}, {1, 1}));
  2785. }
  2786. }
  2787. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 128, { 8, 1}, {1, 1}));
  2788. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 128, { 8, 1}, {4, 1}));
  2789. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 2, 64, { 8, 1}, {4, 1}));
  2790. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 83, 2, 64, { 8, 1}, {4, 1}));
  2791. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 45, 128, { 8, 1}, {4, 1}));
  2792. test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 128, 45, 64, { 8, 1}, {4, 1}));
  2793. // sycl backend will limit task global_range < MAX_INT
  2794. // test case for f16-type-convert-to-fp32 kernel with large k under fp32 compute dtype (occurs in stable-diffusion)
  2795. // however this case needs to alloc more memory which may fail in some devices (Intel Arc770, etc.)
  2796. // this case is verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend)
  2797. // test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 512, 262144, 9216, {1, 1}, {1, 1}));
  2798. for (ggml_type type_a : base_types) {
  2799. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  2800. for (int n_mats : {4, 8}) {
  2801. for (int n_used : {1, 2, 4}) {
  2802. for (bool b : {false, true}) {
  2803. for (int n : {1, 32}) {
  2804. int m = 512;
  2805. int k = 256;
  2806. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  2807. }
  2808. }
  2809. }
  2810. }
  2811. }
  2812. }
  2813. for (ggml_type type_a : other_types) {
  2814. for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
  2815. for (int n_mats : {4}) {
  2816. for (int n_used : {2}) {
  2817. for (bool b : {false}) {
  2818. for (int n : {1}) {
  2819. int m = 512;
  2820. int k = 256;
  2821. test_cases.emplace_back(new test_mul_mat_id(type_a, type_b, n_mats, n_used, b, m, n, k));
  2822. }
  2823. }
  2824. }
  2825. }
  2826. }
  2827. }
  2828. for (ggml_type type_a : base_types) {
  2829. for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  2830. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 1, 16, { 1, 1}));
  2831. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 1, 16, {10, 1}));
  2832. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 1, 16, {10, 1}));
  2833. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 1, 16, {10, 10}));
  2834. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 1, 16, {10, 10}));
  2835. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 1, 16, {10, 10}));
  2836. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 1, 16, {10, 10}));
  2837. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 16, 16, { 1, 1}));
  2838. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 16, 16, { 1, 1}, true));
  2839. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 16, 16, {10, 1}));
  2840. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 16, 16, {10, 1}));
  2841. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 16, 16, {10, 10}));
  2842. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 16, 16, {10, 10}));
  2843. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 16, 16, {10, 10}));
  2844. test_cases.emplace_back(new test_out_prod(type_a, type_b, 256, 16, 16, {10, 10}));
  2845. }
  2846. }
  2847. test_cases.emplace_back(new test_sqr());
  2848. test_cases.emplace_back(new test_sqrt());
  2849. test_cases.emplace_back(new test_log());
  2850. test_cases.emplace_back(new test_sin());
  2851. test_cases.emplace_back(new test_cos());
  2852. test_cases.emplace_back(new test_clamp());
  2853. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 1, 1}, 5));
  2854. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 3, 1}, 5));
  2855. test_cases.emplace_back(new test_diag_mask_inf(GGML_TYPE_F32, {10, 10, 3, 2}, 5));
  2856. #if 0
  2857. std::uniform_int_distribution<> dist_ne1(1, 50);
  2858. int exponent = 1;
  2859. while (exponent < (1 << 17)) {
  2860. std::uniform_int_distribution<> dist_ne0(exponent, 2*exponent);
  2861. for (int n = 0; n < 10; ++n) {
  2862. int64_t ne0 = dist_ne0(rng);
  2863. int64_t ne1 = dist_ne1(rng);
  2864. 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));
  2865. }
  2866. exponent <<= 1;
  2867. }
  2868. #endif
  2869. for (bool mask : {false, true}) {
  2870. for (float max_bias : {0.0f, 8.0f}) {
  2871. if (!mask && max_bias > 0.0f) continue;
  2872. for (float scale : {1.0f, 0.1f}) {
  2873. for (int64_t ne0 : {16, 1024}) {
  2874. for (int64_t ne1 : {16, 1024}) {
  2875. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, ne1, 1, 1}, mask, scale, max_bias));
  2876. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0-1, ne1-1, 1, 1}, mask, scale, max_bias));
  2877. }
  2878. }
  2879. }
  2880. }
  2881. }
  2882. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, true, 0.1f, 0.0f));
  2883. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {16, 2, 32, 1}, false, 0.1f, 0.0f));
  2884. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 0.0f));
  2885. test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {32, 2, 32, 1}, true, 0.1f, 8.0f));
  2886. {
  2887. bool all = true;
  2888. for (float v : { 0, 1 }) {
  2889. for (float fs : { 1.0f, 1.4245f }) {
  2890. for (float ef : { 0.0f, 0.7465f }) {
  2891. for (float af : { 1.0f, 1.4245f }) {
  2892. for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
  2893. for (bool ff : {false, true}) { // freq_factors
  2894. test_cases.emplace_back(new test_rope(type, {128, 32, 2, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 7B
  2895. if (all) {
  2896. test_cases.emplace_back(new test_rope(type, {128, 40, 2, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 13B
  2897. test_cases.emplace_back(new test_rope(type, {128, 52, 2, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 30B
  2898. test_cases.emplace_back(new test_rope(type, {128, 64, 2, 1}, 128, 0, 512, fs, ef, af, ff, v)); // llama 65B
  2899. }
  2900. if (all) {
  2901. test_cases.emplace_back(new test_rope(type, { 64, 1, 2, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  2902. test_cases.emplace_back(new test_rope(type, { 64, 71, 2, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 7B)
  2903. test_cases.emplace_back(new test_rope(type, { 64, 8, 2, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2904. test_cases.emplace_back(new test_rope(type, { 80, 32, 2, 1}, 20, 2, 512, fs, ef, af, ff, v)); // neox (stablelm)
  2905. test_cases.emplace_back(new test_rope(type, { 80, 32, 2, 1}, 32, 2, 512, fs, ef, af, ff, v)); // neox (phi-2)
  2906. }
  2907. test_cases.emplace_back(new test_rope(type, { 64, 128, 2, 1}, 64, 2, 512, fs, ef, af, ff, v)); // neox (falcon 40B)
  2908. }
  2909. }
  2910. all = false;
  2911. }
  2912. }
  2913. }
  2914. }
  2915. }
  2916. for (int v : { 0, 1, 2, 3 }) {
  2917. for (int dim : { 0, 1, 2, 3, }) {
  2918. test_cases.emplace_back(new test_concat(GGML_TYPE_F32, {11, 12, 13, 14}, 7, dim, v));
  2919. test_cases.emplace_back(new test_concat(GGML_TYPE_I32, {11, 12, 13, 14}, 7, dim, v));
  2920. }
  2921. }
  2922. for (ggml_sort_order order : {GGML_SORT_ORDER_ASC, GGML_SORT_ORDER_DESC}) {
  2923. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {8, 1, 1, 1}, order));
  2924. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {16, 10, 10, 10}, order));
  2925. test_cases.emplace_back(new test_argsort(GGML_TYPE_F32, {60, 10, 10, 10}, order)); // qwen
  2926. }
  2927. test_cases.emplace_back(new test_sum());
  2928. test_cases.emplace_back(new test_sum_rows());
  2929. test_cases.emplace_back(new test_upscale());
  2930. test_cases.emplace_back(new test_upscale(GGML_TYPE_F32, { 512, 512, 3, 1 }, 2, true));
  2931. test_cases.emplace_back(new test_upscale_ext());
  2932. test_cases.emplace_back(new test_group_norm());
  2933. test_cases.emplace_back(new test_acc());
  2934. test_cases.emplace_back(new test_pad());
  2935. test_cases.emplace_back(new test_arange());
  2936. test_cases.emplace_back(new test_timestep_embedding());
  2937. test_cases.emplace_back(new test_leaky_relu());
  2938. for (int hs : { 64, 80, 128, 256, }) {
  2939. for (bool mask : { true, false } ) {
  2940. for (float max_bias : { 0.0f, 8.0f }) {
  2941. if (!mask && max_bias > 0.0f) continue;
  2942. for (float logit_softcap : {0.0f, 10.0f}) {
  2943. if (hs != 128 && logit_softcap != 0.0f) continue;
  2944. for (int nh : { 32, }) {
  2945. for (int kv : { 512, 1024, }) {
  2946. for (int nb : { 1, 2, 4, 8, }) {
  2947. for (ggml_type type_KV : {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
  2948. test_cases.emplace_back(new test_flash_attn_ext(hs, nh, kv, nb, mask, max_bias, logit_softcap, type_KV));
  2949. }
  2950. }
  2951. }
  2952. }
  2953. }
  2954. }
  2955. }
  2956. }
  2957. test_cases.emplace_back(new test_cross_entropy_loss());
  2958. for (float wd : {0.0f, 1e-2f}) {
  2959. 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));
  2960. }
  2961. // these tests are disabled to save execution time, but they can be handy for debugging
  2962. #if 0
  2963. test_cases.emplace_back(new test_llama(1));
  2964. test_cases.emplace_back(new test_llama(2));
  2965. test_cases.emplace_back(new test_falcon(1));
  2966. test_cases.emplace_back(new test_falcon(2));
  2967. #endif
  2968. // run tests
  2969. if (mode == MODE_GRAD) {
  2970. size_t n_ok = 0;
  2971. for (auto & test : test_cases) {
  2972. if (test->eval_grad(backend, op_name)) {
  2973. n_ok++;
  2974. }
  2975. }
  2976. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  2977. return n_ok == test_cases.size();
  2978. }
  2979. if (mode == MODE_TEST) {
  2980. ggml_backend_t backend_cpu = ggml_backend_cpu_init();
  2981. size_t n_ok = 0;
  2982. for (auto & test : test_cases) {
  2983. if (test->eval(backend, backend_cpu, op_name)) {
  2984. n_ok++;
  2985. }
  2986. }
  2987. printf(" %zu/%zu tests passed\n", n_ok, test_cases.size());
  2988. ggml_backend_free(backend_cpu);
  2989. return n_ok == test_cases.size();
  2990. }
  2991. if (mode == MODE_PERF) {
  2992. for (auto & test : test_cases) {
  2993. test->eval_perf(backend, op_name);
  2994. }
  2995. return true;
  2996. }
  2997. GGML_ABORT("fatal error");
  2998. }
  2999. static void usage(char ** argv) {
  3000. printf("Usage: %s [mode] [-o op] [-b backend]\n", argv[0]);
  3001. printf(" valid modes:\n");
  3002. printf(" - test (default, compare with CPU backend for correctness)\n");
  3003. printf(" - perf (performance evaluation)\n");
  3004. printf(" - grad (compare gradients from backpropagation with method of finite differences)\n");
  3005. printf(" op names are as given by ggml_op_desc() (e.g. GGML_ADD)\n");
  3006. }
  3007. int main(int argc, char ** argv) {
  3008. test_mode mode = MODE_TEST;
  3009. const char * op_name_filter = NULL;
  3010. const char * backend_filter = NULL;
  3011. for (int i = 1; i < argc; i++) {
  3012. if (strcmp(argv[i], "test") == 0) {
  3013. mode = MODE_TEST;
  3014. } else if (strcmp(argv[i], "perf") == 0) {
  3015. mode = MODE_PERF;
  3016. } else if (strcmp(argv[i], "grad") == 0) {
  3017. mode = MODE_GRAD;
  3018. } else if (strcmp(argv[i], "-o") == 0) {
  3019. if (i + 1 < argc) {
  3020. op_name_filter = argv[++i];
  3021. } else {
  3022. usage(argv);
  3023. return 1;
  3024. }
  3025. } else if (strcmp(argv[i], "-b") == 0) {
  3026. if (i + 1 < argc) {
  3027. backend_filter = argv[++i];
  3028. } else {
  3029. usage(argv);
  3030. return 1;
  3031. }
  3032. } else {
  3033. usage(argv);
  3034. return 1;
  3035. }
  3036. }
  3037. // enumerate backends
  3038. printf("Testing %zu backends\n\n", ggml_backend_reg_get_count());
  3039. size_t n_ok = 0;
  3040. for (size_t i = 0; i < ggml_backend_reg_get_count(); i++) {
  3041. printf("Backend %zu/%zu (%s)\n", i + 1, ggml_backend_reg_get_count(), ggml_backend_reg_get_name(i));
  3042. if (backend_filter != NULL && strcmp(backend_filter, ggml_backend_reg_get_name(i)) != 0) {
  3043. printf(" Skipping\n");
  3044. n_ok++;
  3045. continue;
  3046. }
  3047. ggml_backend_t backend = ggml_backend_reg_init_backend(i, NULL);
  3048. GGML_ASSERT(backend != NULL);
  3049. if (backend_filter == NULL && ggml_backend_is_cpu(backend) && mode != MODE_GRAD) {
  3050. printf(" Skipping CPU backend\n");
  3051. ggml_backend_free(backend);
  3052. n_ok++;
  3053. continue;
  3054. }
  3055. printf(" Backend name: %s\n", ggml_backend_name(backend));
  3056. bool ok = test_backend(backend, mode, op_name_filter);
  3057. printf(" Backend %s: ", ggml_backend_name(backend));
  3058. if (ok) {
  3059. printf("\033[1;32mOK\033[0m\n");
  3060. n_ok++;
  3061. } else {
  3062. printf("\033[1;31mFAIL\033[0m\n");
  3063. }
  3064. printf("\n");
  3065. ggml_backend_free(backend);
  3066. }
  3067. printf("%zu/%zu backends passed\n", n_ok, ggml_backend_reg_get_count());
  3068. if (n_ok != ggml_backend_reg_get_count()) {
  3069. printf("\033[1;31mFAIL\033[0m\n");
  3070. return 1;
  3071. }
  3072. ggml_quantize_free();
  3073. printf("\033[1;32mOK\033[0m\n");
  3074. return 0;
  3075. }