ggml-sycl.cpp 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451
  1. //
  2. // MIT license
  3. // Copyright (C) 2024 Intel Corporation
  4. // SPDX-License-Identifier: MIT
  5. //
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. #include <algorithm>
  12. #include <assert.h>
  13. #include <atomic>
  14. #include <cinttypes>
  15. #include <cstddef>
  16. #include <cstdint>
  17. #include <cstdlib>
  18. #include <float.h>
  19. #include <limits>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <vector>
  23. #include <cmath>
  24. #include <iostream>
  25. #include <fstream>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <regex>
  29. #include <sycl/sycl.hpp>
  30. #include <sycl/half_type.hpp>
  31. #include "ggml-sycl.h"
  32. #include "ggml-impl.h"
  33. #include "ggml-backend-impl.h"
  34. #include "ggml-sycl/backend.hpp"
  35. #include "ggml-sycl/presets.hpp"
  36. #include "ggml-sycl/gemm.hpp"
  37. static bool g_sycl_loaded = false;
  38. static ggml_sycl_device_info ggml_sycl_init() {
  39. ggml_sycl_device_info info = {};
  40. info.device_count = dpct::dev_mgr::instance().device_count();
  41. if (info.device_count == 0) {
  42. fprintf(stderr, "%s: failed to initialize " GGML_SYCL_NAME ": %s\n", __func__);
  43. return info;
  44. }
  45. GGML_ASSERT(info.device_count <= GGML_SYCL_MAX_DEVICES);
  46. int64_t total_vram = 0;
  47. #if defined(GGML_SYCL_FORCE_MMQ)
  48. fprintf(stderr, "%s: GGML_SYCL_FORCE_MMQ: yes\n", __func__);
  49. #else
  50. fprintf(stderr, "%s: GGML_SYCL_FORCE_MMQ: no\n", __func__);
  51. #endif
  52. #if defined(SYCL_USE_XMX)
  53. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  54. #else
  55. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  56. #endif
  57. fprintf(stderr, "%s: found %d " GGML_SYCL_NAME " devices:\n", __func__, info.device_count);
  58. for (int i = 0; i < info.device_count; ++i) {
  59. info.devices[i].vmm = 0;
  60. dpct::device_info prop;
  61. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  62. prop, dpct::dev_mgr::instance().get_device(i))));
  63. info.default_tensor_split[i] = total_vram;
  64. total_vram += prop.get_global_mem_size();
  65. info.devices[i].cc =
  66. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  67. info.max_work_group_sizes[i] = prop.get_max_work_group_size();
  68. }
  69. for (int id = 0; id < info.device_count; ++id) {
  70. info.default_tensor_split[id] /= total_vram;
  71. }
  72. return info;
  73. }
  74. const ggml_sycl_device_info & ggml_sycl_info() {
  75. static ggml_sycl_device_info info = ggml_sycl_init();
  76. return info;
  77. }
  78. void print_device_detail(int id, sycl::device &device, std::string device_type) {
  79. dpct::device_info prop;
  80. SYCL_CHECK(CHECK_TRY_ERROR(
  81. dpct::get_device_info(prop, device)));
  82. std::string version;
  83. version += std::to_string(prop.get_major_version());
  84. version += ".";
  85. version += std::to_string(prop.get_minor_version());
  86. device_type = std::regex_replace(device_type, std::regex("ext_oneapi_"), "");
  87. std::string name = std::string(prop.get_name());
  88. name = std::regex_replace(name, std::regex("\\(R\\)"), "");
  89. name = std::regex_replace(name, std::regex("\\(TM\\)"), "");
  90. auto global_mem_size = prop.get_global_mem_size()/1000000;
  91. fprintf(stderr, "|%2d|%19s|%39s|%7s|%7d|%8d|%5d|%6luM|%21s|\n", id, device_type.c_str(),
  92. name.c_str(), version.c_str(), prop.get_max_compute_units(),
  93. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  94. global_mem_size, device.get_info<sycl::info::device::driver_version>().c_str());
  95. }
  96. void ggml_backend_sycl_print_sycl_devices() {
  97. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_print_sycl_devices\n");
  98. int device_count = dpct::dev_mgr::instance().device_count();
  99. std::map<std::string, size_t> DeviceNums;
  100. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  101. fprintf(stderr, "| | | | |Max | |Max |Global | |\n");
  102. fprintf(stderr, "| | | | |compute|Max work|sub |mem | |\n");
  103. fprintf(stderr, "|ID| Device Type| Name|Version|units |group |group|size | Driver version|\n");
  104. fprintf(stderr, "|--|-------------------|---------------------------------------|-------|-------|--------|-----|-------|---------------------|\n");
  105. for (int id = 0; id < device_count; ++id) {
  106. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  107. sycl::backend backend = device.get_backend();
  108. std::string backend_type = get_device_backend_and_type(device);
  109. int type_id=DeviceNums[backend_type]++;
  110. std::stringstream device_type;
  111. device_type << "[" << backend_type << ":" << std::to_string(type_id) << "]";
  112. print_device_detail(id, device, device_type.str());
  113. }
  114. }
  115. static inline int get_sycl_env(const char *env_name, int default_val) {
  116. char *user_device_string = getenv(env_name);
  117. int user_number = default_val;
  118. unsigned n;
  119. if (user_device_string != NULL &&
  120. sscanf(user_device_string, " %u", &n) == 1) {
  121. user_number = (int)n;
  122. } else {
  123. user_number = default_val;
  124. }
  125. return user_number;
  126. }
  127. static void ggml_check_sycl() try {
  128. static bool initialized = false;
  129. if (!initialized) {
  130. fprintf(stderr, "[SYCL] call ggml_check_sycl\n");
  131. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  132. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  133. #if defined(GGML_SYCL_F16)
  134. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  135. #else
  136. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  137. #endif
  138. /* NOT REMOVE, keep it for next optimize for XMX.
  139. #if defined(SYCL_USE_XMX)
  140. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  141. #else
  142. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  143. #endif
  144. */
  145. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  146. dpct::dev_mgr::instance().device_count()) != 0) {
  147. initialized = true;
  148. g_sycl_loaded = false;
  149. return;
  150. }
  151. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  152. ggml_backend_sycl_print_sycl_devices();
  153. initialized = true;
  154. g_sycl_loaded = true;
  155. }
  156. }
  157. catch (sycl::exception const &exc) {
  158. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  159. << ", line:" << __LINE__ << std::endl;
  160. std::exit(1);
  161. }
  162. /*
  163. device_index: device index from 0 to n (continue numbers).
  164. It is used for device select/set in SYCL backend internal data structure.
  165. */
  166. inline void check_allow_gpu_index(const int device_index) {
  167. if (device_index >= ggml_sycl_info().device_count) {
  168. char error_buf[256];
  169. snprintf(
  170. error_buf,
  171. sizeof(error_buf),
  172. "%s error: device_index:%d is out of range: [0-%d]",
  173. __func__,
  174. device_index,
  175. ggml_sycl_info().device_count - 1);
  176. fprintf(stderr, "%s\n", error_buf);
  177. assert(false);
  178. }
  179. }
  180. GGML_API void ggml_backend_sycl_get_gpu_list(int *id_list, int max_len) try {
  181. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_gpu_list\n");
  182. for(int i=0;i<max_len;i++) id_list[i] = -1;
  183. for (int i=0;i< ggml_sycl_info().device_count;i++){
  184. if (i>=max_len) break;
  185. id_list[i] = i;
  186. }
  187. return;
  188. }
  189. catch (sycl::exception const &exc) {
  190. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  191. << ", line:" << __LINE__ << std::endl;
  192. std::exit(1);
  193. }
  194. // sycl buffer
  195. struct ggml_backend_sycl_buffer_context {
  196. int device;
  197. void * dev_ptr = nullptr;
  198. queue_ptr stream;
  199. std::string name;
  200. ggml_backend_sycl_buffer_context(int device, void * dev_ptr, queue_ptr stream) :
  201. device(device), dev_ptr(dev_ptr), stream(stream) {
  202. check_allow_gpu_index(device);
  203. name = (GGML_SYCL_NAME + std::to_string(device));
  204. }
  205. ~ggml_backend_sycl_buffer_context() {
  206. if (dev_ptr != nullptr) {
  207. ggml_sycl_set_device(device);
  208. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(dev_ptr, *stream)));
  209. }
  210. }
  211. };
  212. static const char * ggml_backend_sycl_buffer_type_get_name(ggml_backend_buffer_type_t buft);
  213. static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  214. return buffer->buft->iface.get_name == ggml_backend_sycl_buffer_type_get_name;
  215. }
  216. static void
  217. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  218. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  219. ggml_sycl_set_device(ctx->device);
  220. delete ctx;
  221. }
  222. catch (sycl::exception const &exc) {
  223. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  224. << ", line:" << __LINE__ << std::endl;
  225. std::exit(1);
  226. }
  227. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  228. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  229. return ctx->dev_ptr;
  230. }
  231. static void
  232. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  233. ggml_tensor *tensor) try {
  234. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  235. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  236. assert(tensor->view_src->buffer->buft == buffer->buft);
  237. tensor->backend = tensor->view_src->backend;
  238. tensor->extra = tensor->view_src->extra;
  239. return;
  240. }
  241. if (ggml_is_quantized(tensor->type)) {
  242. // initialize padding to 0 to avoid possible NaN values
  243. size_t original_size = ggml_nbytes(tensor);
  244. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  245. if (padded_size > original_size && tensor->view_src == nullptr) {
  246. SYCL_CHECK(CHECK_TRY_ERROR(ctx->stream->memset(
  247. (char *)tensor->data + original_size, 0,
  248. padded_size - original_size).wait()));
  249. }
  250. }
  251. }
  252. catch (sycl::exception const &exc) {
  253. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  254. << ", line:" << __LINE__ << std::endl;
  255. std::exit(1);
  256. }
  257. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  258. ggml_tensor *tensor,
  259. const void *data, size_t offset,
  260. size_t size) try {
  261. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  262. ggml_sycl_set_device(ctx->device);
  263. auto stream = &(dpct::dev_mgr::instance().get_device(ctx->device).default_queue());
  264. SYCL_CHECK(
  265. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  266. char* host_buf = (char*)malloc(size);
  267. memcpy(host_buf, data, size);
  268. SYCL_CHECK(
  269. CHECK_TRY_ERROR((*stream).memcpy((char *)tensor->data + offset, host_buf, size)
  270. .wait()));
  271. free(host_buf);
  272. }
  273. catch (sycl::exception const &exc) {
  274. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  275. << ", line:" << __LINE__ << std::endl;
  276. std::exit(1);
  277. }
  278. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  279. const ggml_tensor *tensor,
  280. void *data, size_t offset,
  281. size_t size) try {
  282. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  283. ggml_sycl_set_device(ctx->device);
  284. auto stream = dpct::dev_mgr::instance().get_device(ctx->device).default_queue();
  285. SYCL_CHECK(CHECK_TRY_ERROR(
  286. stream.memcpy(data, (const char *)tensor->data + offset, size)
  287. .wait()));
  288. }
  289. catch (sycl::exception const &exc) {
  290. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  291. << ", line:" << __LINE__ << std::endl;
  292. std::exit(1);
  293. }
  294. void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
  295. const void *ptr_src, size_t size) {
  296. char *host_buf = (char *)malloc(size);
  297. q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
  298. q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
  299. free(host_buf);
  300. }
  301. static bool
  302. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  303. const ggml_tensor *src,
  304. ggml_tensor *dst) try {
  305. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  306. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  307. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)dst->buffer->context;
  308. ggml_sycl_set_device(src_ctx->device);
  309. /*
  310. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  311. error codes. The original code was commented out and a warning string
  312. was inserted. You need to rewrite this code.
  313. */
  314. SYCL_CHECK(CHECK_TRY_ERROR(
  315. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  316. ggml_sycl_set_device(dst_ctx->device);
  317. /*
  318. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  319. error codes. The original code was commented out and a warning string
  320. was inserted. You need to rewrite this code.
  321. */
  322. SYCL_CHECK(CHECK_TRY_ERROR(
  323. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  324. /*
  325. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  326. error codes. The original code was commented out and a warning string
  327. was inserted. You need to rewrite this code.
  328. */
  329. queue_ptr stream_dst = dst_ctx->stream;
  330. queue_ptr stream_src = src_ctx->stream;
  331. size_t size = ggml_nbytes(src);
  332. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  333. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  334. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  335. #if 0
  336. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  337. (char *)dst->data, (const char *)src->data, size).wait()));
  338. /*
  339. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  340. error codes. The original code was commented out and a warning string
  341. was inserted. You need to rewrite this code.
  342. */
  343. SYCL_CHECK(CHECK_TRY_ERROR(
  344. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  345. #endif
  346. return true;
  347. }
  348. return false;
  349. }
  350. catch (sycl::exception const &exc) {
  351. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  352. << ", line:" << __LINE__ << std::endl;
  353. std::exit(1);
  354. }
  355. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  356. uint8_t value) try {
  357. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  358. ggml_sycl_set_device(ctx->device);
  359. queue_ptr stream = ctx->stream;
  360. SYCL_CHECK(
  361. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  362. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  363. .memset(ctx->dev_ptr, value, buffer->size)
  364. .wait()));
  365. }
  366. catch (sycl::exception const &exc) {
  367. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  368. << ", line:" << __LINE__ << std::endl;
  369. std::exit(1);
  370. }
  371. static const ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  372. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  373. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  374. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  375. /* .memset_tensor = */ NULL,
  376. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  377. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  378. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  379. /* .clear = */ ggml_backend_sycl_buffer_clear,
  380. /* .reset = */ NULL,
  381. };
  382. // sycl buffer type
  383. struct ggml_backend_sycl_buffer_type_context {
  384. int device;
  385. std::string name;
  386. // each buffer type has its own stream
  387. queue_ptr stream = nullptr;
  388. };
  389. static const char * ggml_backend_sycl_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  390. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  391. return ctx->name.c_str();
  392. }
  393. static ggml_backend_buffer_t
  394. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  395. size_t size) try {
  396. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  397. ggml_sycl_set_device(buft_ctx->device);
  398. const queue_ptr stream = buft_ctx->stream;
  399. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  400. void * dev_ptr;
  401. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  402. size, *stream)));
  403. if (!dev_ptr) {
  404. fprintf(stderr, "%s: can't malloc %lu Bytes memory on device", __func__, size);
  405. return nullptr;
  406. }
  407. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr, buft_ctx->stream);
  408. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  409. }
  410. catch (sycl::exception const &exc) {
  411. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  412. << ", line:" << __LINE__ << std::endl;
  413. std::exit(1);
  414. }
  415. static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  416. return 128;
  417. GGML_UNUSED(buft);
  418. }
  419. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  420. return dpct::get_current_device().get_max_mem_alloc_size();
  421. GGML_UNUSED(buft);
  422. }
  423. static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  424. size_t size = ggml_nbytes(tensor);
  425. int64_t ne0 = tensor->ne[0];
  426. if (ggml_is_quantized(tensor->type)) {
  427. if (ne0 % MATRIX_ROW_PADDING != 0) {
  428. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  429. }
  430. }
  431. return size;
  432. GGML_UNUSED(buft);
  433. }
  434. static const ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  435. /* .get_name = */ ggml_backend_sycl_buffer_type_get_name,
  436. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  437. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  438. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  439. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  440. /* .is_host = */ NULL,
  441. };
  442. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device) {
  443. static std::mutex mutex;
  444. std::lock_guard<std::mutex> lock(mutex);
  445. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  446. auto dev_count = ggml_backend_sycl_get_device_count();
  447. if (device>=dev_count or device<0) {
  448. printf("ggml_backend_sycl_buffer_type error: device_index:%d is out of range [0, %d], miss to call ggml_backend_sycl_set_single_device()\n",
  449. device, dev_count-1);
  450. GGML_ASSERT(device<dev_count);
  451. }
  452. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  453. static bool ggml_backend_sycl_buffer_type_initialized = false;
  454. if (!ggml_backend_sycl_buffer_type_initialized) {
  455. for (int i = 0; i < dev_count; i++) {
  456. auto & device_i = dpct::dev_mgr::instance().get_device(i);
  457. queue_ptr stream = &(device_i.default_queue());
  458. ggml_backend_sycl_buffer_types[i] = {
  459. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  460. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_sycl_reg(), i),
  461. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(i), stream},
  462. };
  463. }
  464. ggml_backend_sycl_buffer_type_initialized = true;
  465. }
  466. return &ggml_backend_sycl_buffer_types[device];
  467. }
  468. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(ggml_backend_sycl_context * ctx) {
  469. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  470. int device = ctx->device;
  471. if (device>=ggml_sycl_info().device_count or device<0) {
  472. printf("ggml_backend_sycl_buffer_type error: device_index:%d is out of range [0, %d], miss to call ggml_backend_sycl_set_single_device()\n",
  473. device, ggml_sycl_info().device_count-1);
  474. GGML_ASSERT(device<ggml_sycl_info().device_count);
  475. }
  476. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  477. static bool ggml_backend_sycl_buffer_type_initialized = false;
  478. if (!ggml_backend_sycl_buffer_type_initialized) {
  479. for (int i = 0; i < ggml_sycl_info().device_count; i++) {
  480. ggml_backend_sycl_buffer_types[i] = {
  481. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  482. /* .device = */ nullptr,
  483. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(i), ctx->stream(i, 0)},
  484. };
  485. }
  486. ggml_backend_sycl_buffer_type_initialized = true;
  487. }
  488. return &ggml_backend_sycl_buffer_types[device];
  489. }
  490. // sycl split buffer
  491. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  492. int64_t min_compute_capability = INT_MAX;
  493. int64_t max_compute_capability = INT_MIN;
  494. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  495. if (tensor_split[i] < (i + 1 < ggml_sycl_info().device_count ? tensor_split[i + 1] : 1.0f)) {
  496. if (min_compute_capability > ggml_sycl_info().devices[i].cc) {
  497. min_compute_capability = ggml_sycl_info().devices[i].cc;
  498. }
  499. if (max_compute_capability < ggml_sycl_info().devices[i].cc) {
  500. max_compute_capability = ggml_sycl_info().devices[i].cc;
  501. }
  502. }
  503. }
  504. switch(type) {
  505. case GGML_TYPE_Q4_0:
  506. case GGML_TYPE_Q4_1:
  507. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  508. case GGML_TYPE_Q5_0:
  509. case GGML_TYPE_Q5_1:
  510. case GGML_TYPE_Q8_0:
  511. return 64;
  512. case GGML_TYPE_F16:
  513. case GGML_TYPE_F32:
  514. return 1;
  515. case GGML_TYPE_Q2_K:
  516. case GGML_TYPE_Q3_K:
  517. case GGML_TYPE_Q4_K:
  518. case GGML_TYPE_Q5_K:
  519. case GGML_TYPE_IQ2_XXS:
  520. case GGML_TYPE_IQ2_XS:
  521. case GGML_TYPE_IQ2_S:
  522. case GGML_TYPE_IQ1_S:
  523. case GGML_TYPE_IQ1_M:
  524. case GGML_TYPE_IQ3_XXS:
  525. case GGML_TYPE_IQ4_XS:
  526. case GGML_TYPE_IQ4_NL:
  527. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  528. case GGML_TYPE_IQ3_S:
  529. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  530. case GGML_TYPE_Q6_K:
  531. return 64;
  532. default:
  533. GGML_ABORT("fatal error");
  534. }
  535. }
  536. static void get_row_split(int64_t * row_low, int64_t * row_high, const ggml_tensor * tensor, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split, int id) {
  537. const int64_t nrows = ggml_nrows(tensor);
  538. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  539. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  540. *row_low -= *row_low % rounding;
  541. if (id == ggml_sycl_info().device_count - 1) {
  542. *row_high = nrows;
  543. } else {
  544. *row_high = nrows*tensor_split[id + 1];
  545. *row_high -= *row_high % rounding;
  546. }
  547. }
  548. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  549. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  550. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  551. }
  552. struct ggml_backend_sycl_split_buffer_type_context {
  553. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  554. };
  555. struct ggml_backend_sycl_split_buffer_context {
  556. ~ggml_backend_sycl_split_buffer_context() try {
  557. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  558. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  559. for (int64_t is = 0; is < GGML_SYCL_MAX_STREAMS; ++is) {
  560. if (extra->events[i][is] != nullptr) {
  561. /*
  562. DPCT1009:206: SYCL uses exceptions to report errors and
  563. does not use the error codes. The original code was
  564. commented out and a warning string was inserted. You
  565. need to rewrite this code.
  566. */
  567. SYCL_CHECK(CHECK_TRY_ERROR(
  568. dpct::destroy_event(extra->events[i][is])));
  569. }
  570. }
  571. if (extra->data_device[i] != nullptr) {
  572. /*
  573. DPCT1009:207: SYCL uses exceptions to report errors and does
  574. not use the error codes. The original code was commented out
  575. and a warning string was inserted. You need to rewrite this
  576. code.
  577. */
  578. ggml_sycl_set_device(i);
  579. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  580. extra->data_device[i], *(streams[i]))));
  581. }
  582. }
  583. delete extra;
  584. }
  585. }
  586. catch (sycl::exception const &exc) {
  587. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  588. << ", line:" << __LINE__ << std::endl;
  589. std::exit(1);
  590. }
  591. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  592. std::vector<queue_ptr> streams;
  593. };
  594. static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  595. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  596. delete ctx;
  597. }
  598. static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  599. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  600. return (void *)0x1000;
  601. GGML_UNUSED(buffer);
  602. }
  603. static void
  604. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  605. ggml_tensor *tensor) try {
  606. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  607. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  608. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  609. const int64_t ne0 = tensor->ne[0];
  610. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  611. ctx->tensor_extras.push_back(extra);
  612. ctx->streams.push_back(&(dpct::get_current_device().default_queue()));
  613. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  614. int64_t row_low, row_high;
  615. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  616. int64_t nrows_split = row_high - row_low;
  617. if (nrows_split == 0) {
  618. continue;
  619. }
  620. size_t size = ggml_nbytes_split(tensor, nrows_split);
  621. const size_t original_size = size;
  622. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  623. if (ne0 % MATRIX_ROW_PADDING != 0) {
  624. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  625. }
  626. // FIXME: do not crash if cudaMalloc fails
  627. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  628. ggml_sycl_set_device(i);
  629. const queue_ptr stream = ctx->streams[i];
  630. char * buf;
  631. /*
  632. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  633. error codes. The original code was commented out and a warning string
  634. was inserted. You need to rewrite this code.
  635. */
  636. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  637. size, *stream)));
  638. if (!buf) {
  639. char err_buf[1024];
  640. snprintf(err_buf, 1023, "%s: can't malloc %lu Bytes memory on device", __func__, size);
  641. throw std::runtime_error(err_buf);
  642. }
  643. // set padding to 0 to avoid possible NaN values
  644. if (size > original_size) {
  645. /*
  646. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  647. the error codes. The original code was commented out and a warning
  648. string was inserted. You need to rewrite this code.
  649. */
  650. SYCL_CHECK(CHECK_TRY_ERROR(
  651. (*stream)
  652. .memset(buf + original_size, 0, size - original_size)
  653. .wait()));
  654. }
  655. extra->data_device[i] = buf;
  656. for (int64_t is = 0; is < GGML_SYCL_MAX_STREAMS; ++is) {
  657. /*
  658. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  659. the error codes. The original code was commented out and a warning
  660. string was inserted. You need to rewrite this code.
  661. */
  662. SYCL_CHECK(
  663. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  664. }
  665. }
  666. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  667. tensor->extra = extra;
  668. }
  669. catch (sycl::exception const &exc) {
  670. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  671. << ", line:" << __LINE__ << std::endl;
  672. std::exit(1);
  673. }
  674. static void
  675. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  676. ggml_tensor *tensor, const void *data,
  677. size_t offset, size_t size) try {
  678. // split tensors must always be set in their entirety at once
  679. GGML_ASSERT(offset == 0);
  680. GGML_ASSERT(size == ggml_nbytes(tensor));
  681. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  682. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  683. const int64_t ne0 = tensor->ne[0];
  684. const size_t nb1 = tensor->nb[1];
  685. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  686. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  687. int64_t row_low, row_high;
  688. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  689. int64_t nrows_split = row_high - row_low;
  690. if (nrows_split == 0) {
  691. continue;
  692. }
  693. const size_t offset_split = row_low*nb1;
  694. size_t size = ggml_nbytes_split(tensor, nrows_split);
  695. const size_t original_size = size;
  696. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  697. if (ne0 % MATRIX_ROW_PADDING != 0) {
  698. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  699. }
  700. const char * buf_host = (const char *)data + offset_split;
  701. /*
  702. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  703. error codes. The original code was commented out and a warning string
  704. was inserted. You need to rewrite this code.
  705. */
  706. ggml_sycl_set_device(i);
  707. const queue_ptr stream = ctx->streams[i];
  708. SYCL_CHECK(CHECK_TRY_ERROR(
  709. (*stream)
  710. .memcpy(extra->data_device[i], buf_host, original_size)
  711. .wait()));
  712. }
  713. }
  714. catch (sycl::exception const &exc) {
  715. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  716. << ", line:" << __LINE__ << std::endl;
  717. std::exit(1);
  718. }
  719. static void
  720. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  721. const ggml_tensor *tensor, void *data,
  722. size_t offset, size_t size) try {
  723. // split tensors must always be set in their entirety at once
  724. GGML_ASSERT(offset == 0);
  725. GGML_ASSERT(size == ggml_nbytes(tensor));
  726. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  727. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  728. const int64_t ne0 = tensor->ne[0];
  729. const size_t nb1 = tensor->nb[1];
  730. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  731. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  732. int64_t row_low, row_high;
  733. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  734. int64_t nrows_split = row_high - row_low;
  735. if (nrows_split == 0) {
  736. continue;
  737. }
  738. const size_t offset_split = row_low*nb1;
  739. size_t size = ggml_nbytes_split(tensor, nrows_split);
  740. const size_t original_size = size;
  741. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  742. if (ne0 % MATRIX_ROW_PADDING != 0) {
  743. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  744. }
  745. char * buf_host = (char *)data + offset_split;
  746. /*
  747. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  748. error codes. The original code was commented out and a warning string
  749. was inserted. You need to rewrite this code.
  750. */
  751. ggml_sycl_set_device(i);
  752. const queue_ptr stream = ctx->streams[i];
  753. SYCL_CHECK(CHECK_TRY_ERROR(
  754. (*stream)
  755. .memcpy(buf_host, extra->data_device[i], original_size)
  756. .wait()));
  757. }
  758. }
  759. catch (sycl::exception const &exc) {
  760. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  761. << ", line:" << __LINE__ << std::endl;
  762. std::exit(1);
  763. }
  764. static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  765. GGML_UNUSED(buffer);
  766. GGML_UNUSED(value);
  767. }
  768. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  769. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  770. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  771. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  772. /* .memset_tensor = */ NULL,
  773. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  774. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  775. /* .cpy_tensor = */ NULL,
  776. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  777. /* .reset = */ NULL,
  778. };
  779. // sycl split buffer type
  780. static const char * ggml_backend_sycl_split_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  781. return GGML_SYCL_NAME "_Split";
  782. GGML_UNUSED(buft);
  783. }
  784. static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  785. return buffer->buft->iface.get_name == ggml_backend_sycl_split_buffer_type_get_name;
  786. }
  787. static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  788. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  789. // instead, we allocate them for each tensor separately in init_tensor
  790. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  791. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  792. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  793. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  794. }
  795. static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  796. return 128;
  797. GGML_UNUSED(buft);
  798. }
  799. static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  800. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  801. size_t total_size = 0;
  802. const int64_t ne0 = tensor->ne[0];
  803. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  804. int64_t row_low, row_high;
  805. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  806. int64_t nrows_split = row_high - row_low;
  807. if (nrows_split == 0) {
  808. continue;
  809. }
  810. total_size += ggml_nbytes_split(tensor, nrows_split);
  811. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  812. if (ne0 % MATRIX_ROW_PADDING != 0) {
  813. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  814. }
  815. }
  816. return total_size;
  817. }
  818. static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  819. return false;
  820. GGML_UNUSED(buft);
  821. }
  822. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  823. /* .get_name = */ ggml_backend_sycl_split_buffer_type_get_name,
  824. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  825. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  826. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  827. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  828. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  829. };
  830. ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  831. static std::mutex mutex;
  832. std::lock_guard<std::mutex> lock(mutex);
  833. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_split_buffer_type\n");
  834. ggml_check_sycl();
  835. // FIXME: this is not thread safe
  836. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  837. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  838. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  839. if (all_zero) {
  840. tensor_split_arr = ggml_sycl_info().default_tensor_split;
  841. } else {
  842. float split_sum = 0.0f;
  843. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  844. tensor_split_arr[i] = split_sum;
  845. split_sum += tensor_split[i];
  846. }
  847. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  848. tensor_split_arr[i] /= split_sum;
  849. }
  850. }
  851. auto it = buft_map.find(tensor_split_arr);
  852. if (it != buft_map.end()) {
  853. return &it->second;
  854. }
  855. struct ggml_backend_buffer_type buft {
  856. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  857. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_sycl_reg(), 0),
  858. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  859. };
  860. auto result = buft_map.emplace(tensor_split_arr, buft);
  861. return &result.first->second;
  862. }
  863. // host buffer type
  864. static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  865. return GGML_SYCL_NAME "_Host";
  866. GGML_UNUSED(buft);
  867. }
  868. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  869. ggml_sycl_host_free(buffer->context);
  870. }
  871. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  872. void * ptr = ggml_sycl_host_malloc(size);
  873. if (ptr == nullptr) {
  874. // fallback to cpu buffer
  875. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  876. }
  877. // FIXME: this is a hack to avoid having to implement a new buffer type
  878. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  879. buffer->buft = buft;
  880. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  881. return buffer;
  882. }
  883. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  884. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_host_buffer_type\n");
  885. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  886. /* .iface = */ {
  887. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  888. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  889. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  890. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  891. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  892. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  893. },
  894. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_sycl_reg(), 0),
  895. /* .context = */ nullptr,
  896. };
  897. return &ggml_backend_sycl_buffer_type_host;
  898. }
  899. // buffer pool for sycl (legacy)
  900. struct ggml_sycl_pool_leg : public ggml_sycl_pool {
  901. static const int MAX_SYCL_BUFFERS = 256;
  902. int device;
  903. queue_ptr qptr;
  904. struct ggml_sycl_buffer {
  905. void * ptr = nullptr;
  906. size_t size = 0;
  907. };
  908. ggml_sycl_buffer buffer_pool[MAX_SYCL_BUFFERS] = {};
  909. size_t pool_size = 0;
  910. explicit ggml_sycl_pool_leg(queue_ptr qptr_, int device_) :
  911. qptr(qptr_),
  912. device(device_) {
  913. }
  914. ~ggml_sycl_pool_leg() {
  915. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  916. ggml_sycl_buffer & b = buffer_pool[i];
  917. if (b.ptr != nullptr) {
  918. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(b.ptr, *qptr)));
  919. pool_size -= b.size;
  920. }
  921. }
  922. GGML_ASSERT(pool_size == 0);
  923. }
  924. void * alloc(size_t size, size_t * actual_size) override {
  925. #ifdef DEBUG_sycl_MALLOC
  926. int nnz = 0;
  927. size_t max_size = 0;
  928. #endif
  929. size_t best_diff = 1ull << 36;
  930. int ibest = -1;
  931. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  932. ggml_sycl_buffer& b = buffer_pool[i];
  933. if (b.ptr != nullptr) {
  934. #ifdef DEBUG_sycl_MALLOC
  935. ++nnz;
  936. if (b.size > max_size) max_size = b.size;
  937. #endif
  938. if (b.size >= size) {
  939. size_t diff = b.size - size;
  940. if (diff < best_diff) {
  941. best_diff = diff;
  942. ibest = i;
  943. if (!best_diff) {
  944. void * ptr = b.ptr;
  945. *actual_size = b.size;
  946. b.ptr = nullptr;
  947. b.size = 0;
  948. return ptr;
  949. }
  950. }
  951. }
  952. }
  953. }
  954. if (ibest >= 0) {
  955. ggml_sycl_buffer& b = buffer_pool[ibest];
  956. void * ptr = b.ptr;
  957. *actual_size = b.size;
  958. b.ptr = nullptr;
  959. b.size = 0;
  960. return ptr;
  961. }
  962. void * ptr;
  963. size_t look_ahead_size = (size_t) (1.05 * size);
  964. SYCL_CHECK(
  965. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  966. look_ahead_size, *qptr)));
  967. if (!ptr) {
  968. fprintf(stderr, "%s: can't malloc %lu Bytes memory on device", __func__, look_ahead_size);
  969. return nullptr;
  970. }
  971. *actual_size = look_ahead_size;
  972. pool_size += look_ahead_size;
  973. #ifdef DEBUG_SYCL_MALLOC
  974. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  975. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  976. #endif
  977. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  978. return ptr;
  979. }
  980. void free(void * ptr, size_t size) override {
  981. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  982. ggml_sycl_buffer& b = buffer_pool[i];
  983. if (b.ptr == nullptr) {
  984. b.ptr = ptr;
  985. b.size = size;
  986. return;
  987. }
  988. }
  989. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_sycl_BUFFERS\n");
  990. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *qptr)));
  991. pool_size -= size;
  992. }
  993. };
  994. std::unique_ptr<ggml_sycl_pool> ggml_backend_sycl_context::new_pool_for_device(queue_ptr qptr, int device) {
  995. // TBD: NO VMM support
  996. // if (ggml_sycl_info().devices[device].vmm) {
  997. // return std::unique_ptr<ggml_sycl_pool>(new ggml_sycl_pool_vmm(device));
  998. // }
  999. return std::unique_ptr<ggml_sycl_pool>(new ggml_sycl_pool_leg(qptr, device));
  1000. }
  1001. // TBD pool with virtual memory management
  1002. // struct ggml_sycl_pool_vmm : public ggml_sycl_pool
  1003. /// kernels
  1004. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  1005. typedef void (*ggml_sycl_func_t)(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  1006. typedef void (*ggml_sycl_op_mul_mat_t)(
  1007. ggml_backend_sycl_context & ctx,
  1008. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  1009. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  1010. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  1011. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  1012. const queue_ptr &stream);
  1013. typedef void (*ggml_sycl_op_flatten_t)(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1014. const ggml_tensor *src1,
  1015. ggml_tensor *dst, const float *src0_dd,
  1016. const float *src1_dd, float *dst_dd,
  1017. const queue_ptr &main_stream);
  1018. static __dpct_inline__ float op_repeat(const float a, const float b) {
  1019. return b;
  1020. GGML_UNUSED(a);
  1021. }
  1022. static __dpct_inline__ float op_add(const float a, const float b) {
  1023. return a + b;
  1024. }
  1025. static __dpct_inline__ float op_mul(const float a, const float b) {
  1026. return a * b;
  1027. }
  1028. static __dpct_inline__ float op_div(const float a, const float b) {
  1029. return a / b;
  1030. }
  1031. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  1032. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  1033. int ne0, int ne1, int ne2, int ne3,
  1034. int ne10, int ne11, int ne12, int ne13,
  1035. /*int s0, */ int s1, int s2, int s3,
  1036. /*int s10,*/ int s11, int s12, int s13,
  1037. const sycl::nd_item<3> &item_ct1) {
  1038. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1039. item_ct1.get_local_id(2);
  1040. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  1041. item_ct1.get_local_id(1));
  1042. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  1043. item_ct1.get_local_id(0)) /
  1044. ne3;
  1045. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  1046. item_ct1.get_local_id(0)) %
  1047. ne3;
  1048. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  1049. return;
  1050. }
  1051. const int i11 = i1 % ne11;
  1052. const int i12 = i2 % ne12;
  1053. const int i13 = i3 % ne13;
  1054. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  1055. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  1056. const size_t i_dst = i_src0;
  1057. const src0_t * src0_row = src0 + i_src0;
  1058. const src1_t * src1_row = src1 + i_src1;
  1059. dst_t * dst_row = dst + i_dst;
  1060. for (int i0 = i0s; i0 < ne0;
  1061. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  1062. const int i10 = i0 % ne10;
  1063. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  1064. }
  1065. }
  1066. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  1067. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  1068. int ne0, int ne1, int ne2, int ne3,
  1069. int ne10, int ne11, int ne12, int ne13,
  1070. /*int s0, */ int s1, int s2, int s3,
  1071. /*int s10,*/ int s11, int s12, int s13,
  1072. const sycl::nd_item<3> &item_ct1) {
  1073. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1074. item_ct1.get_local_id(2);
  1075. const int i3 = i/(ne2*ne1*ne0);
  1076. const int i2 = (i/(ne1*ne0)) % ne2;
  1077. const int i1 = (i/ne0) % ne1;
  1078. const int i0 = i % ne0;
  1079. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  1080. return;
  1081. }
  1082. const int i11 = i1 % ne11;
  1083. const int i12 = i2 % ne12;
  1084. const int i13 = i3 % ne13;
  1085. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  1086. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  1087. const size_t i_dst = i_src0;
  1088. const src0_t * src0_row = src0 + i_src0;
  1089. const src1_t * src1_row = src1 + i_src1;
  1090. dst_t * dst_row = dst + i_dst;
  1091. const int i10 = i0 % ne10;
  1092. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  1093. }
  1094. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  1095. const int ne10, const int ne11, const int ne12,
  1096. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  1097. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1098. item_ct1.get_local_id(2);
  1099. if (i >= ne) {
  1100. return;
  1101. }
  1102. int src1_idx = i - offset;
  1103. int oz = src1_idx / nb2;
  1104. int oy = (src1_idx - (oz * nb2)) / nb1;
  1105. int ox = src1_idx % nb1;
  1106. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  1107. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  1108. } else {
  1109. dst[i] = x[i];
  1110. }
  1111. }
  1112. static void gelu_f32(const float * x, float * dst, const int k,
  1113. const sycl::nd_item<3> &item_ct1) {
  1114. const float GELU_COEF_A = 0.044715f;
  1115. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  1116. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1117. item_ct1.get_local_id(2);
  1118. if (i >= k) {
  1119. return;
  1120. }
  1121. float xi = x[i];
  1122. dst[i] = 0.5f * xi *
  1123. (1.0f +
  1124. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  1125. }
  1126. static void silu_f32(const float * x, float * dst, const int k,
  1127. const sycl::nd_item<3> &item_ct1) {
  1128. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1129. item_ct1.get_local_id(2);
  1130. if (i >= k) {
  1131. return;
  1132. }
  1133. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  1134. }
  1135. static void gelu_quick_f32(const float *x, float *dst, int k,
  1136. const sycl::nd_item<3> &item_ct1) {
  1137. const float GELU_QUICK_COEF = -1.702f;
  1138. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1139. item_ct1.get_local_id(2);
  1140. if (i >= k) {
  1141. return;
  1142. }
  1143. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  1144. }
  1145. static void tanh_f32(const float *x, float *dst, int k,
  1146. const sycl::nd_item<3> &item_ct1) {
  1147. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1148. item_ct1.get_local_id(2);
  1149. if (i >= k) {
  1150. return;
  1151. }
  1152. dst[i] = sycl::tanh((float)(x[i]));
  1153. }
  1154. static void relu_f32(const float * x, float * dst, const int k,
  1155. const sycl::nd_item<3> &item_ct1) {
  1156. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1157. item_ct1.get_local_id(2);
  1158. if (i >= k) {
  1159. return;
  1160. }
  1161. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  1162. }
  1163. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  1164. const sycl::nd_item<3> &item_ct1) {
  1165. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1166. item_ct1.get_local_id(2);
  1167. if (i >= k) {
  1168. return;
  1169. }
  1170. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  1171. }
  1172. static void hardswish_f32(const float * x, float * dst, const int k,
  1173. const sycl::nd_item<3> &item_ct1) {
  1174. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1175. item_ct1.get_local_id(2);
  1176. if (i >= k) {
  1177. return;
  1178. }
  1179. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  1180. }
  1181. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  1182. const sycl::nd_item<3> &item_ct1) {
  1183. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1184. item_ct1.get_local_id(2);
  1185. if (i >= k) {
  1186. return;
  1187. }
  1188. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  1189. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  1190. }
  1191. static void sqr_f32(const float * x, float * dst, const int k,
  1192. const sycl::nd_item<3> &item_ct1) {
  1193. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1194. item_ct1.get_local_id(2);
  1195. if (i >= k) {
  1196. return;
  1197. }
  1198. dst[i] = x[i] * x[i];
  1199. }
  1200. static void upscale_f32(const float *x, float *dst, const int nb00, const int nb01,
  1201. const int nb02, const int nb03, const int ne10, const int ne11,
  1202. const int ne12, const int ne13, const float sf0, const float sf1,
  1203. const float sf2, const float sf3, const sycl::nd_item<1> &item_ct1) {
  1204. int index = item_ct1.get_local_id(0) +
  1205. item_ct1.get_group(0) * item_ct1.get_local_range(0);
  1206. if (index >= ne10 * ne11 * ne12 * ne13) {
  1207. return;
  1208. }
  1209. // operation
  1210. int i10 = index % ne10;
  1211. int i11 = (index / ne10) % ne11;
  1212. int i12 = (index / (ne10 * ne11)) % ne12;
  1213. int i13 = (index / (ne10 * ne11 * ne12)) % ne13;
  1214. int i00 = i10 / sf0;
  1215. int i01 = i11 / sf1;
  1216. int i02 = i12 / sf2;
  1217. int i03 = i13 / sf3;
  1218. dst[index] = *(float *)((char *)x + i03 * nb03 + i02 * nb02 + i01 * nb01 + i00 * nb00);
  1219. }
  1220. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  1221. const sycl::nd_item<3> &item_ct1) {
  1222. int nidx = item_ct1.get_local_id(2) +
  1223. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  1224. if (nidx >= ne0) {
  1225. return;
  1226. }
  1227. // operation
  1228. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  1229. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  1230. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  1231. item_ct1.get_group(0) < ne02) {
  1232. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  1233. item_ct1.get_group(0) * ne00 * ne01;
  1234. dst[offset_dst] = x[offset_src];
  1235. } else {
  1236. dst[offset_dst] = 0.0f;
  1237. }
  1238. }
  1239. template<int QUANT_BLOCK_TILE>
  1240. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  1241. const sycl::nd_item<3> &item_ct1) {
  1242. const int ix = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1243. item_ct1.get_local_id(2)) * QUANT_BLOCK_TILE;
  1244. if (ix >= kx_padded) {
  1245. return;
  1246. }
  1247. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  1248. item_ct1.get_local_id(1);
  1249. const int i_padded = iy*kx_padded + ix;
  1250. block_q8_1 * y = (block_q8_1 *) vy;
  1251. const int ib = i_padded / QK8_1; // block index
  1252. const int iqs = i_padded % QK8_1; // quant index
  1253. typedef sycl::vec<float, QUANT_BLOCK_TILE> TC;
  1254. typedef sycl::vec<int8_t, QUANT_BLOCK_TILE> TQ;
  1255. TC zeros;
  1256. TQ qzeros;
  1257. #pragma unroll
  1258. for (int i = 0; i < QUANT_BLOCK_TILE; i++)
  1259. {
  1260. zeros[i] = 0.f;
  1261. qzeros[i] = 0;
  1262. }
  1263. const TC xi = ix < kx ? *(TC *)&x[iy * kx + ix] : zeros;
  1264. float sum = xi[0];
  1265. float amax = sycl::fabs(xi[0]);
  1266. #pragma unroll
  1267. for (int i = 1; i < QUANT_BLOCK_TILE; i++)
  1268. {
  1269. sum += xi[i];
  1270. amax = sycl::fmax(sycl::fabs(xi[i]), amax);
  1271. }
  1272. sum = warp_reduce_sum(sum, item_ct1);
  1273. amax = warp_reduce_max(amax, item_ct1);
  1274. const float d = amax / 127;
  1275. TQ q = qzeros;
  1276. if (amax != 0.0f)
  1277. {
  1278. #pragma unroll
  1279. for (int i = 0; i < QUANT_BLOCK_TILE; i++) {
  1280. q[i] = sycl::round(xi[i] / d);
  1281. }
  1282. }
  1283. *(TQ *)&y[ib].qs[iqs] = q;
  1284. if (iqs > 0) {
  1285. return;
  1286. }
  1287. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  1288. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  1289. }
  1290. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  1291. static void k_get_rows(
  1292. const void * src0, const int32_t * src1, dst_t * dst,
  1293. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1294. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1295. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1296. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1297. size_t s10, size_t s11, size_t s12,
  1298. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  1299. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  1300. item_ct1.get_local_id(2)) *
  1301. 2;
  1302. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  1303. item_ct1.get_local_id(1);
  1304. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  1305. item_ct1.get_local_id(0)) /
  1306. ne12;
  1307. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  1308. item_ct1.get_local_id(0)) %
  1309. ne12;
  1310. if (i00 >= ne00) {
  1311. return;
  1312. }
  1313. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1314. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1315. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  1316. const int ib = i00/qk; // block index
  1317. const int iqs = (i00%qk)/qr; // quant index
  1318. const int iybs = i00 - i00%qk; // dst block start index
  1319. const int y_offset = qr == 1 ? 1 : qk/2;
  1320. // dequantize
  1321. dfloat2 v;
  1322. dequantize_kernel(src0_row, ib, iqs, v);
  1323. dst_row[iybs + iqs + 0] = v.x();
  1324. dst_row[iybs + iqs + y_offset] = v.y();
  1325. }
  1326. template<typename src0_t, typename dst_t>
  1327. static void k_get_rows_float(
  1328. const src0_t * src0, const int32_t * src1, dst_t * dst,
  1329. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  1330. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  1331. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  1332. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  1333. size_t s10, size_t s11, size_t s12,
  1334. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  1335. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  1336. item_ct1.get_local_id(2);
  1337. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  1338. item_ct1.get_local_id(1);
  1339. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  1340. item_ct1.get_local_id(0)) /
  1341. ne12;
  1342. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  1343. item_ct1.get_local_id(0)) %
  1344. ne12;
  1345. if (i00 >= ne00) {
  1346. return;
  1347. }
  1348. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  1349. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  1350. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  1351. dst_row[i00] = src0_row[i00];
  1352. }
  1353. static void mul_mat_p021_f16_f32(
  1354. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  1355. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  1356. const sycl::nd_item<3> &item_ct1) {
  1357. const sycl::half *x = (const sycl::half *)vx;
  1358. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  1359. item_ct1.get_local_id(1);
  1360. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  1361. item_ct1.get_local_id(0);
  1362. const int channel_x = channel / (nchannels_y / nchannels_x);
  1363. const int nrows_y = ncols_x;
  1364. const int nrows_dst = nrows_x;
  1365. const int row_dst = row_x;
  1366. float tmp = 0.0f;
  1367. for (int col_x0 = 0; col_x0 < ncols_x;
  1368. col_x0 += item_ct1.get_local_range(2)) {
  1369. const int col_x = col_x0 + item_ct1.get_local_id(2);
  1370. if (col_x >= ncols_x) {
  1371. break;
  1372. }
  1373. // x is transposed and permuted
  1374. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  1375. const float xi =
  1376. sycl::vec<sycl::half, 1>(x[ix])
  1377. .convert<float, sycl::rounding_mode::automatic>()[0];
  1378. const int row_y = col_x;
  1379. // y is not transposed but permuted
  1380. const int iy = channel*nrows_y + row_y;
  1381. tmp += xi * y[iy];
  1382. }
  1383. // dst is not transposed and not permuted
  1384. const int idst = channel*nrows_dst + row_dst;
  1385. // sum up partial sums and write back result
  1386. #pragma unroll
  1387. for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
  1388. tmp +=
  1389. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  1390. }
  1391. if (item_ct1.get_local_id(2) == 0) {
  1392. dst[idst] = tmp;
  1393. }
  1394. }
  1395. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  1396. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  1397. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  1398. const sycl::nd_item<3> &item_ct1) {
  1399. const sycl::half *x = (const sycl::half *)vx;
  1400. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  1401. item_ct1.get_local_id(1);
  1402. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  1403. item_ct1.get_local_id(0);
  1404. const int channel_x = channel / channel_x_divisor;
  1405. const int nrows_y = ncols_x;
  1406. const int nrows_dst = nrows_x;
  1407. const int row_dst = row_x;
  1408. const int idst = channel*nrows_dst + row_dst;
  1409. float tmp = 0.0f;
  1410. for (int col_x0 = 0; col_x0 < ncols_x;
  1411. col_x0 += item_ct1.get_local_range(2)) {
  1412. const int col_x = col_x0 + item_ct1.get_local_id(2);
  1413. if (col_x >= ncols_x) {
  1414. break;
  1415. }
  1416. const int row_y = col_x;
  1417. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  1418. const int iy = channel*nrows_y + row_y;
  1419. const float xi =
  1420. sycl::vec<sycl::half, 1>(x[ix])
  1421. .convert<float, sycl::rounding_mode::automatic>()[0];
  1422. tmp += xi * y[iy];
  1423. }
  1424. // sum up partial sums and write back result
  1425. #pragma unroll
  1426. for (int mask = WARP_SIZE / 2; mask > 0; mask >>= 1) {
  1427. tmp +=
  1428. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  1429. }
  1430. if (item_ct1.get_local_id(2) == 0) {
  1431. dst[idst] = tmp;
  1432. }
  1433. }
  1434. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  1435. const float * xi = (const float *) cxi;
  1436. float * dsti = (float *) cdsti;
  1437. *dsti = *xi;
  1438. }
  1439. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  1440. const float * xi = (const float *) cxi;
  1441. sycl::half *dsti = (sycl::half *)cdsti;
  1442. *dsti = sycl::vec<float, 1>(*xi)
  1443. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  1444. }
  1445. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  1446. const sycl::half *xi = (const sycl::half *)cxi;
  1447. sycl::half *dsti = (sycl::half *)cdsti;
  1448. *dsti = *xi;
  1449. }
  1450. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  1451. const sycl::half *xi = (const sycl::half *)cxi;
  1452. float * dsti = (float *) cdsti;
  1453. *dsti = *xi;
  1454. }
  1455. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  1456. const int16_t *xi = (const int16_t *)cxi;
  1457. int16_t *dsti = (int16_t *)cdsti;
  1458. *dsti = *xi;
  1459. }
  1460. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  1461. const int32_t *xi = (const int32_t *)cxi;
  1462. int32_t *dsti = (int32_t *)cdsti;
  1463. *dsti = *xi;
  1464. }
  1465. template <cpy_kernel_t cpy_1>
  1466. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  1467. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  1468. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  1469. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  1470. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1471. item_ct1.get_local_id(2);
  1472. if (i >= ne) {
  1473. return;
  1474. }
  1475. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  1476. // then combine those indices with the corresponding byte offsets to get the total offsets
  1477. const int i03 = i/(ne00 * ne01 * ne02);
  1478. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  1479. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  1480. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  1481. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  1482. const int i13 = i/(ne10 * ne11 * ne12);
  1483. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  1484. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  1485. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  1486. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  1487. cpy_1(cx + x_offset, cdst + dst_offset);
  1488. }
  1489. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  1490. const float * xi = (const float *) cxi;
  1491. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  1492. float amax = 0.0f; // absolute max
  1493. for (int j = 0; j < QK8_0; j++) {
  1494. const float v = xi[j];
  1495. amax = sycl::fmax(amax, sycl::fabs((float)v));
  1496. }
  1497. const float d = amax / ((1 << 7) - 1);
  1498. const float id = d ? 1.0f/d : 0.0f;
  1499. dsti->d = d;
  1500. for (int j = 0; j < QK8_0; ++j) {
  1501. const float x0 = xi[j]*id;
  1502. dsti->qs[j] = sycl::round((float)x0);
  1503. }
  1504. }
  1505. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  1506. const float * xi = (const float *) cxi;
  1507. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  1508. float amax = 0.0f;
  1509. float vmax = 0.0f;
  1510. for (int j = 0; j < QK4_0; ++j) {
  1511. const float v = xi[j];
  1512. if (amax < sycl::fabs((float)v)) {
  1513. amax = sycl::fabs((float)v);
  1514. vmax = v;
  1515. }
  1516. }
  1517. const float d = vmax / -8;
  1518. const float id = d ? 1.0f/d : 0.0f;
  1519. dsti->d = d;
  1520. for (int j = 0; j < QK4_0/2; ++j) {
  1521. const float x0 = xi[0 + j]*id;
  1522. const float x1 = xi[QK4_0/2 + j]*id;
  1523. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  1524. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  1525. dsti->qs[j] = xi0;
  1526. dsti->qs[j] |= xi1 << 4;
  1527. }
  1528. }
  1529. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  1530. const float * xi = (const float *) cxi;
  1531. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  1532. float vmin = FLT_MAX;
  1533. float vmax = -FLT_MAX;
  1534. for (int j = 0; j < QK4_1; ++j) {
  1535. const float v = xi[j];
  1536. if (v < vmin) vmin = v;
  1537. if (v > vmax) vmax = v;
  1538. }
  1539. const float d = (vmax - vmin) / ((1 << 4) - 1);
  1540. const float id = d ? 1.0f/d : 0.0f;
  1541. dsti->dm.x() = d;
  1542. dsti->dm.y() = vmin;
  1543. for (int j = 0; j < QK4_1/2; ++j) {
  1544. const float x0 = (xi[0 + j] - vmin)*id;
  1545. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  1546. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  1547. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  1548. dsti->qs[j] = xi0;
  1549. dsti->qs[j] |= xi1 << 4;
  1550. }
  1551. }
  1552. template <cpy_kernel_t cpy_blck, int qk>
  1553. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  1554. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  1555. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  1556. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  1557. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1558. item_ct1.get_local_id(2)) *
  1559. qk;
  1560. if (i >= ne) {
  1561. return;
  1562. }
  1563. const int i03 = i/(ne00 * ne01 * ne02);
  1564. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  1565. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  1566. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  1567. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  1568. const int i13 = i/(ne10 * ne11 * ne12);
  1569. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  1570. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  1571. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  1572. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  1573. cpy_blck(cx + x_offset, cdst + dst_offset);
  1574. }
  1575. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  1576. const sycl::nd_item<3> &item_ct1) {
  1577. const int row = item_ct1.get_group(1);
  1578. const int col = item_ct1.get_local_id(2);
  1579. float sum = 0.0f;
  1580. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  1581. sum += x[row * ncols + i];
  1582. }
  1583. sum = warp_reduce_sum(sum, item_ct1);
  1584. if (col == 0) {
  1585. dst[row] = sum;
  1586. }
  1587. }
  1588. template<typename T>
  1589. static inline void ggml_sycl_swap(T & a, T & b) {
  1590. T tmp = a;
  1591. a = b;
  1592. b = tmp;
  1593. }
  1594. template <ggml_sort_order order>
  1595. __dpct_inline__ static void
  1596. k_argsort_f32_i32(const float *x, int *dst, const int ncols, int ncols_pad,
  1597. const sycl::nd_item<3> &item_ct1, uint8_t *dpct_local) {
  1598. // bitonic sort
  1599. int col = item_ct1.get_local_id(2);
  1600. int row = item_ct1.get_group(1);
  1601. if (col >= ncols_pad) {
  1602. return;
  1603. }
  1604. const float * x_row = x + row * ncols;
  1605. auto dst_row = (int *)dpct_local;
  1606. // initialize indices
  1607. dst_row[col] = col;
  1608. item_ct1.barrier(sycl::access::fence_space::local_space);
  1609. for (int k = 2; k <= ncols_pad; k *= 2) {
  1610. for (int j = k / 2; j > 0; j /= 2) {
  1611. int ixj = col ^ j;
  1612. if (ixj > col) {
  1613. if ((col & k) == 0) {
  1614. if (dst_row[col] >= ncols ||
  1615. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  1616. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  1617. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  1618. ) {
  1619. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  1620. }
  1621. } else {
  1622. if (dst_row[ixj] >= ncols ||
  1623. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  1624. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  1625. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  1626. ) {
  1627. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  1628. }
  1629. }
  1630. }
  1631. /*
  1632. DPCT1118:1: SYCL group functions and algorithms must be encountered
  1633. in converged control flow. You may need to adjust the code.
  1634. */
  1635. item_ct1.barrier(sycl::access::fence_space::local_space);
  1636. }
  1637. }
  1638. // copy the result to dst without the padding
  1639. if (col < ncols) {
  1640. dst[row * ncols + col] = dst_row[col];
  1641. }
  1642. }
  1643. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  1644. const sycl::nd_item<3> &item_ct1) {
  1645. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  1646. item_ct1.get_local_id(1);
  1647. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1648. item_ct1.get_local_id(2);
  1649. if (col >= ncols) {
  1650. return;
  1651. }
  1652. const int i = row*ncols + col;
  1653. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  1654. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  1655. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  1656. }
  1657. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  1658. const sycl::nd_item<3> &item_ct1) {
  1659. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1660. item_ct1.get_local_id(2);
  1661. if (i >= k) {
  1662. return;
  1663. }
  1664. dst[i] = scale * x[i];
  1665. }
  1666. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  1667. const sycl::nd_item<3> &item_ct1) {
  1668. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  1669. item_ct1.get_local_id(2);
  1670. if (i >= k) {
  1671. return;
  1672. }
  1673. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  1674. }
  1675. template <typename Ti, typename To>
  1676. static void pool2d_nchw_kernel(
  1677. const int ih, const int iw, const int oh, const int ow,
  1678. const int kh, const int kw, const int sh, const int sw,
  1679. const int ph, const int pw, const int parallel_elements,
  1680. const Ti* src, To* dst, const enum ggml_op_pool op,
  1681. const sycl::nd_item<3> &item_ct1) {
  1682. int idx = item_ct1.get_local_id(2) +
  1683. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  1684. if (idx >= parallel_elements) {
  1685. return;
  1686. }
  1687. const int I_HW = ih * iw;
  1688. const int O_HW = oh * ow;
  1689. const int nc = idx / O_HW;
  1690. const int cur_oh = idx % O_HW / ow;
  1691. const int cur_ow = idx % O_HW % ow;
  1692. const Ti* i_ptr = src + nc * I_HW;
  1693. To* o_ptr = dst + nc * O_HW;
  1694. const int start_h = cur_oh * sh - ph;
  1695. const int bh = sycl::max(0, start_h);
  1696. const int eh = sycl::min(ih, start_h + kh);
  1697. const int start_w = cur_ow * sw - pw;
  1698. const int bw = sycl::max(0, start_w);
  1699. const int ew = sycl::min(iw, start_w + kw);
  1700. To res = 0;
  1701. switch (op) {
  1702. case GGML_OP_POOL_AVG: res = 0; break;
  1703. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  1704. }
  1705. for (int i = bh; i < eh; i += 1) {
  1706. for (int j = bw; j < ew; j += 1) {
  1707. #if DPCT_COMPATIBILITY_TEMP >= 350
  1708. /*
  1709. DPCT1098:106: The '*' expression is used instead of the __ldg
  1710. call. These two expressions do not provide the exact same
  1711. functionality. Check the generated code for potential precision
  1712. and/or performance issues.
  1713. */
  1714. Ti cur = *(i_ptr + i * iw + j);
  1715. #else
  1716. Ti cur = i_ptr[i * iw + j];
  1717. #endif
  1718. switch (op) {
  1719. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  1720. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  1721. }
  1722. }
  1723. }
  1724. o_ptr[cur_oh * ow + cur_ow] = res;
  1725. }
  1726. template <int qk, int qr, dequantize_kernel_t dq>
  1727. static void get_rows_sycl(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  1728. ggml_tensor *dst, const void *src0_dd,
  1729. const int32_t *src1_dd, float *dst_dd,
  1730. queue_ptr stream) {
  1731. GGML_TENSOR_BINARY_OP_LOCALS
  1732. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  1733. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  1734. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  1735. // strides in elements
  1736. //const size_t s0 = nb0 / ggml_element_size(dst);
  1737. const size_t s1 = nb1 / ggml_element_size(dst);
  1738. const size_t s2 = nb2 / ggml_element_size(dst);
  1739. const size_t s3 = nb3 / ggml_element_size(dst);
  1740. const size_t s10 = nb10 / ggml_element_size(src1);
  1741. const size_t s11 = nb11 / ggml_element_size(src1);
  1742. const size_t s12 = nb12 / ggml_element_size(src1);
  1743. //const size_t s13 = nb13 / ggml_element_size(src1);
  1744. GGML_ASSERT(ne00 % 2 == 0);
  1745. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1746. [=](sycl::nd_item<3> item_ct1) {
  1747. k_get_rows<qk, qr, dq>(
  1748. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  1749. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  1750. });
  1751. (void) dst;
  1752. }
  1753. template <typename src0_t>
  1754. static void get_rows_sycl_float(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  1755. const ggml_tensor *src1, ggml_tensor *dst,
  1756. const src0_t *src0_dd, const int32_t *src1_dd,
  1757. float *dst_dd, queue_ptr stream) {
  1758. GGML_TENSOR_BINARY_OP_LOCALS
  1759. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  1760. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  1761. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  1762. // strides in elements
  1763. //const size_t s0 = nb0 / ggml_element_size(dst);
  1764. const size_t s1 = nb1 / ggml_element_size(dst);
  1765. const size_t s2 = nb2 / ggml_element_size(dst);
  1766. const size_t s3 = nb3 / ggml_element_size(dst);
  1767. const size_t s10 = nb10 / ggml_element_size(src1);
  1768. const size_t s11 = nb11 / ggml_element_size(src1);
  1769. const size_t s12 = nb12 / ggml_element_size(src1);
  1770. //const size_t s13 = nb13 / ggml_element_size(src1);
  1771. {
  1772. dpct::has_capability_or_fail(stream->get_device(),
  1773. {sycl::aspect::fp16});
  1774. stream->parallel_for(
  1775. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1776. [=](sycl::nd_item<3> item_ct1) {
  1777. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  1778. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  1779. });
  1780. }
  1781. (void) dst;
  1782. }
  1783. template<float (*bin_op)(const float, const float)>
  1784. struct bin_bcast_sycl {
  1785. template <typename src0_t, typename src1_t, typename dst_t>
  1786. void operator()(ggml_backend_sycl_context & ctx,
  1787. const struct ggml_tensor *src0,
  1788. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  1789. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  1790. queue_ptr stream) {
  1791. GGML_TENSOR_BINARY_OP_LOCALS
  1792. int nr0 = ne10/ne0;
  1793. int nr1 = ne11/ne1;
  1794. int nr2 = ne12/ne2;
  1795. int nr3 = ne13/ne3;
  1796. int nr[4] = { nr0, nr1, nr2, nr3 };
  1797. // collapse dimensions until first broadcast dimension
  1798. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  1799. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  1800. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  1801. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  1802. auto collapse = [](int64_t cne[]) {
  1803. cne[0] *= cne[1];
  1804. cne[1] = cne[2];
  1805. cne[2] = cne[3];
  1806. cne[3] = 1;
  1807. };
  1808. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  1809. cnb[1] *= cne[1];
  1810. cnb[2] *= cne[2];
  1811. cnb[3] *= cne[3];
  1812. };
  1813. for (int i = 0; i < 4; i++) {
  1814. if (nr[i] != 1) {
  1815. break;
  1816. }
  1817. if (i > 0) {
  1818. collapse_nb(cnb0, cne0);
  1819. collapse_nb(cnb1, cne1);
  1820. collapse(cne0);
  1821. collapse(cne1);
  1822. }
  1823. }
  1824. {
  1825. int64_t ne0 = cne0[0];
  1826. int64_t ne1 = cne0[1];
  1827. int64_t ne2 = cne0[2];
  1828. int64_t ne3 = cne0[3];
  1829. int64_t ne10 = cne1[0];
  1830. int64_t ne11 = cne1[1];
  1831. int64_t ne12 = cne1[2];
  1832. int64_t ne13 = cne1[3];
  1833. size_t nb0 = cnb0[0];
  1834. size_t nb1 = cnb0[1];
  1835. size_t nb2 = cnb0[2];
  1836. size_t nb3 = cnb0[3];
  1837. size_t nb10 = cnb1[0];
  1838. size_t nb11 = cnb1[1];
  1839. size_t nb12 = cnb1[2];
  1840. size_t nb13 = cnb1[3];
  1841. size_t s0 = nb0 / sizeof(dst_t);
  1842. size_t s1 = nb1 / sizeof(dst_t);
  1843. size_t s2 = nb2 / sizeof(dst_t);
  1844. size_t s3 = nb3 / sizeof(dst_t);
  1845. size_t s10 = nb10 / sizeof(src1_t);
  1846. size_t s11 = nb11 / sizeof(src1_t);
  1847. size_t s12 = nb12 / sizeof(src1_t);
  1848. size_t s13 = nb13 / sizeof(src1_t);
  1849. GGML_ASSERT(s0 == 1);
  1850. GGML_ASSERT(s10 == 1);
  1851. const int block_size = 128;
  1852. int64_t hne0 = std::max(ne0/2LL, 1LL);
  1853. sycl::range<3> block_dims(1, 1, 1);
  1854. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  1855. block_dims[1] = std::min<unsigned int>(
  1856. ne1, block_size / (unsigned int)block_dims[2]);
  1857. block_dims[0] = std::min(
  1858. std::min<unsigned int>(
  1859. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  1860. (unsigned int)block_dims[1]),
  1861. 64U);
  1862. sycl::range<3> block_nums(
  1863. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  1864. (ne1 + block_dims[1] - 1) / block_dims[1],
  1865. (hne0 + block_dims[2] - 1) / block_dims[2]);
  1866. if (block_nums[0] > 65535) {
  1867. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  1868. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  1869. {
  1870. dpct::has_capability_or_fail(stream->get_device(),
  1871. {sycl::aspect::fp16});
  1872. stream->parallel_for(
  1873. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  1874. sycl::range<3>(1, 1, block_size),
  1875. sycl::range<3>(1, 1, block_size)),
  1876. [=](sycl::nd_item<3> item_ct1) {
  1877. k_bin_bcast_unravel<bin_op>(
  1878. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  1879. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  1880. s13, item_ct1);
  1881. });
  1882. }
  1883. } else {
  1884. /*
  1885. DPCT1049:16: The work-group size passed to the SYCL kernel may
  1886. exceed the limit. To get the device limit, query
  1887. info::device::max_work_group_size. Adjust the work-group size if
  1888. needed.
  1889. */
  1890. dpct::has_capability_or_fail(stream->get_device(),
  1891. {sycl::aspect::fp16});
  1892. stream->parallel_for(
  1893. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  1894. [=](sycl::nd_item<3> item_ct1) {
  1895. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  1896. ne2, ne3, ne10, ne11, ne12, ne13,
  1897. s1, s2, s3, s11, s12, s13,
  1898. item_ct1);
  1899. });
  1900. }
  1901. }
  1902. }
  1903. };
  1904. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  1905. const int n_elements, const int ne10, const int ne11,
  1906. const int ne12, const int nb1, const int nb2,
  1907. const int offset, queue_ptr stream) {
  1908. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  1909. stream->parallel_for(
  1910. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1911. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  1912. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  1913. [=](sycl::nd_item<3> item_ct1) {
  1914. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  1915. item_ct1);
  1916. });
  1917. }
  1918. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  1919. queue_ptr stream) {
  1920. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  1921. stream->parallel_for(
  1922. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1923. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  1924. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  1925. [=](sycl::nd_item<3> item_ct1) {
  1926. gelu_f32(x, dst, k, item_ct1);
  1927. });
  1928. }
  1929. static void silu_f32_sycl(const float *x, float *dst, const int k,
  1930. queue_ptr stream) {
  1931. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  1932. stream->parallel_for(
  1933. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1934. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  1935. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  1936. [=](sycl::nd_item<3> item_ct1) {
  1937. silu_f32(x, dst, k, item_ct1);
  1938. });
  1939. }
  1940. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  1941. queue_ptr stream) {
  1942. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  1943. stream->parallel_for(
  1944. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1945. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  1946. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  1947. [=](sycl::nd_item<3> item_ct1) {
  1948. gelu_quick_f32(x, dst, k, item_ct1);
  1949. });
  1950. }
  1951. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  1952. queue_ptr stream) {
  1953. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  1954. stream->parallel_for(
  1955. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1956. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  1957. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  1958. [=](sycl::nd_item<3> item_ct1) {
  1959. tanh_f32(x, dst, k, item_ct1);
  1960. });
  1961. }
  1962. static void relu_f32_sycl(const float *x, float *dst, const int k,
  1963. queue_ptr stream) {
  1964. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  1965. stream->parallel_for(
  1966. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1967. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  1968. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  1969. [=](sycl::nd_item<3> item_ct1) {
  1970. relu_f32(x, dst, k, item_ct1);
  1971. });
  1972. }
  1973. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  1974. queue_ptr stream) {
  1975. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  1976. stream->parallel_for(
  1977. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1978. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  1979. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  1980. [=](sycl::nd_item<3> item_ct1) {
  1981. hardsigmoid_f32(x, dst, k, item_ct1);
  1982. });
  1983. }
  1984. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  1985. queue_ptr stream) {
  1986. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  1987. stream->parallel_for(
  1988. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  1989. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  1990. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  1991. [=](sycl::nd_item<3> item_ct1) {
  1992. hardswish_f32(x, dst, k, item_ct1);
  1993. });
  1994. }
  1995. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  1996. const float negative_slope,
  1997. queue_ptr stream) {
  1998. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  1999. stream->parallel_for(
  2000. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2001. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  2002. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  2003. [=](sycl::nd_item<3> item_ct1) {
  2004. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  2005. });
  2006. }
  2007. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  2008. queue_ptr stream) {
  2009. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  2010. stream->parallel_for(
  2011. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2012. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  2013. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  2014. [=](sycl::nd_item<3> item_ct1) {
  2015. sqr_f32(x, dst, k, item_ct1);
  2016. });
  2017. }
  2018. static void upscale_f32_sycl(const float *x, float *dst, const int nb00, const int nb01,
  2019. const int nb02, const int nb03, const int ne10, const int ne11,
  2020. const int ne12, const int ne13, const float sf0, const float sf1,
  2021. const float sf2, const float sf3, queue_ptr stream) {
  2022. int dst_size = ne10 * ne11 * ne12 * ne13;
  2023. int num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  2024. sycl::range<1> gridDim(num_blocks * SYCL_UPSCALE_BLOCK_SIZE);
  2025. stream->parallel_for(
  2026. sycl::nd_range<1>(gridDim, sycl::range<1>(SYCL_UPSCALE_BLOCK_SIZE)),
  2027. [=](sycl::nd_item<1> item_ct1) {
  2028. upscale_f32(x, dst, nb00, nb01, nb02, nb03, ne10, ne11, ne12, ne13, sf0, sf1, sf2, sf3, item_ct1);
  2029. });
  2030. }
  2031. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  2032. const int ne01, const int ne02, const int ne0,
  2033. const int ne1, const int ne2, queue_ptr stream) {
  2034. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  2035. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  2036. stream->parallel_for(
  2037. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  2038. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  2039. [=](sycl::nd_item<3> item_ct1) {
  2040. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  2041. });
  2042. }
  2043. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  2044. const int ky, const int kx_padded,
  2045. queue_ptr stream) {
  2046. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  2047. const sycl::range<3> num_blocks(1, ky, block_num_x);
  2048. int constexpr QUANT_BLOCK_TILE = QK8_1 / WARP_SIZE;
  2049. static_assert(QK8_1 % WARP_SIZE == 0);
  2050. const sycl::range<3> block_size(1, 1, SYCL_QUANTIZE_BLOCK_SIZE / QUANT_BLOCK_TILE);
  2051. {
  2052. dpct::has_capability_or_fail(stream->get_device(),
  2053. {sycl::aspect::fp16});
  2054. stream->parallel_for(
  2055. sycl::nd_range<3>(num_blocks * block_size, block_size),
  2056. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(WARP_SIZE)]] {
  2057. quantize_q8_1<QUANT_BLOCK_TILE>(x, vy, kx, kx_padded, item_ct1);
  2058. });
  2059. }
  2060. }
  2061. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  2062. float *dst, const int ncols_x,
  2063. const int nrows_x,
  2064. const int nchannels_x,
  2065. const int nchannels_y,
  2066. queue_ptr stream) {
  2067. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  2068. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  2069. {
  2070. dpct::has_capability_or_fail(stream->get_device(),
  2071. {sycl::aspect::fp16});
  2072. stream->parallel_for(
  2073. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  2074. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(WARP_SIZE)]] {
  2075. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  2076. nchannels_y, item_ct1);
  2077. });
  2078. }
  2079. }
  2080. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  2081. const void *vx, const float *y, float *dst, const int ncols_x,
  2082. const int nrows_x, const int row_stride_x, const int nchannels_x,
  2083. const int nchannels_y, const int channel_stride_x, queue_ptr stream) {
  2084. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  2085. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  2086. {
  2087. dpct::has_capability_or_fail(stream->get_device(),
  2088. {sycl::aspect::fp16});
  2089. stream->parallel_for(
  2090. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  2091. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(WARP_SIZE)]] {
  2092. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  2093. row_stride_x, channel_stride_x,
  2094. nchannels_y / nchannels_x, item_ct1);
  2095. });
  2096. }
  2097. }
  2098. static void
  2099. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  2100. const int ne01, const int ne02, const int nb00,
  2101. const int nb01, const int nb02, const int nb03,
  2102. const int ne10, const int ne11, const int ne12,
  2103. const int nb10, const int nb11, const int nb12,
  2104. const int nb13, queue_ptr stream) {
  2105. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  2106. {
  2107. dpct::has_capability_or_fail(stream->get_device(),
  2108. {sycl::aspect::fp16});
  2109. stream->parallel_for(
  2110. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2111. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  2112. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  2113. [=](sycl::nd_item<3> item_ct1) {
  2114. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  2115. nb01, nb02, nb03, ne10, ne11, ne12,
  2116. nb10, nb11, nb12, nb13, item_ct1);
  2117. });
  2118. }
  2119. }
  2120. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  2121. const int ne00, const int ne01,
  2122. const int ne02, const int nb00,
  2123. const int nb01, const int nb02,
  2124. const int nb03, const int ne10,
  2125. const int ne11, const int ne12,
  2126. const int nb10, const int nb11,
  2127. const int nb12, const int nb13,
  2128. queue_ptr stream) {
  2129. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  2130. {
  2131. dpct::has_capability_or_fail(stream->get_device(),
  2132. {sycl::aspect::fp16});
  2133. stream->parallel_for(
  2134. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2135. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  2136. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  2137. [=](sycl::nd_item<3> item_ct1) {
  2138. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  2139. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  2140. item_ct1);
  2141. });
  2142. }
  2143. }
  2144. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  2145. const int ne00, const int ne01,
  2146. const int ne02, const int nb00,
  2147. const int nb01, const int nb02,
  2148. const int nb03, const int ne10,
  2149. const int ne11, const int ne12,
  2150. const int nb10, const int nb11,
  2151. const int nb12, const int nb13,
  2152. queue_ptr stream) {
  2153. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  2154. {
  2155. dpct::has_capability_or_fail(stream->get_device(),
  2156. {sycl::aspect::fp16});
  2157. stream->parallel_for(
  2158. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2159. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  2160. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  2161. [=](sycl::nd_item<3> item_ct1) {
  2162. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  2163. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  2164. item_ct1);
  2165. });
  2166. }
  2167. }
  2168. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  2169. const int ne00, const int ne01,
  2170. const int ne02, const int nb00,
  2171. const int nb01, const int nb02,
  2172. const int nb03, const int ne10,
  2173. const int ne11, const int ne12,
  2174. const int nb10, const int nb11,
  2175. const int nb12, const int nb13,
  2176. queue_ptr stream) {
  2177. GGML_ASSERT(ne % QK8_0 == 0);
  2178. const int num_blocks = ne / QK8_0;
  2179. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  2180. sycl::range<3>(1, 1, 1)),
  2181. [=](sycl::nd_item<3> item_ct1) {
  2182. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  2183. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  2184. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  2185. item_ct1);
  2186. });
  2187. }
  2188. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  2189. const int ne00, const int ne01,
  2190. const int ne02, const int nb00,
  2191. const int nb01, const int nb02,
  2192. const int nb03, const int ne10,
  2193. const int ne11, const int ne12,
  2194. const int nb10, const int nb11,
  2195. const int nb12, const int nb13,
  2196. queue_ptr stream) {
  2197. GGML_ASSERT(ne % QK4_0 == 0);
  2198. const int num_blocks = ne / QK4_0;
  2199. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  2200. sycl::range<3>(1, 1, 1)),
  2201. [=](sycl::nd_item<3> item_ct1) {
  2202. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  2203. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  2204. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  2205. item_ct1);
  2206. });
  2207. }
  2208. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  2209. const int ne00, const int ne01,
  2210. const int ne02, const int nb00,
  2211. const int nb01, const int nb02,
  2212. const int nb03, const int ne10,
  2213. const int ne11, const int ne12,
  2214. const int nb10, const int nb11,
  2215. const int nb12, const int nb13,
  2216. queue_ptr stream) {
  2217. GGML_ASSERT(ne % QK4_1 == 0);
  2218. const int num_blocks = ne / QK4_1;
  2219. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  2220. sycl::range<3>(1, 1, 1)),
  2221. [=](sycl::nd_item<3> item_ct1) {
  2222. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  2223. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  2224. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  2225. item_ct1);
  2226. });
  2227. }
  2228. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  2229. const int ne00, const int ne01,
  2230. const int ne02, const int nb00,
  2231. const int nb01, const int nb02,
  2232. const int nb03, const int ne10,
  2233. const int ne11, const int ne12,
  2234. const int nb10, const int nb11,
  2235. const int nb12, const int nb13,
  2236. queue_ptr stream) {
  2237. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  2238. {
  2239. dpct::has_capability_or_fail(stream->get_device(),
  2240. {sycl::aspect::fp16});
  2241. stream->parallel_for(
  2242. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2243. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  2244. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  2245. [=](sycl::nd_item<3> item_ct1) {
  2246. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  2247. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  2248. item_ct1);
  2249. });
  2250. }
  2251. }
  2252. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  2253. const int ne00, const int ne01,
  2254. const int ne02, const int nb00,
  2255. const int nb01, const int nb02,
  2256. const int nb03, const int ne10,
  2257. const int ne11, const int ne12,
  2258. const int nb10, const int nb11,
  2259. const int nb12, const int nb13,
  2260. queue_ptr stream) {
  2261. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  2262. {
  2263. // dpct::has_capability_or_fail(stream->get_device(),
  2264. // {sycl::aspect::fp16});
  2265. stream->parallel_for(
  2266. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2267. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  2268. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  2269. [=](sycl::nd_item<3> item_ct1) {
  2270. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  2271. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  2272. item_ct1);
  2273. });
  2274. }
  2275. }
  2276. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  2277. const int ne00, const int ne01,
  2278. const int ne02, const int nb00,
  2279. const int nb01, const int nb02,
  2280. const int nb03, const int ne10,
  2281. const int ne11, const int ne12,
  2282. const int nb10, const int nb11,
  2283. const int nb12, const int nb13,
  2284. queue_ptr stream) {
  2285. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  2286. {
  2287. // dpct::has_capability_or_fail(stream->get_device(),
  2288. // {sycl::aspect::fp16});
  2289. stream->parallel_for(
  2290. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2291. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  2292. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  2293. [=](sycl::nd_item<3> item_ct1) {
  2294. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  2295. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  2296. item_ct1);
  2297. });
  2298. }
  2299. }
  2300. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  2301. const int k, queue_ptr stream) {
  2302. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  2303. stream->parallel_for(
  2304. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2305. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  2306. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  2307. [=](sycl::nd_item<3> item_ct1) {
  2308. scale_f32(x, dst, scale, k, item_ct1);
  2309. });
  2310. }
  2311. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  2312. const float max, const int k,
  2313. queue_ptr stream) {
  2314. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  2315. stream->parallel_for(
  2316. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  2317. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  2318. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  2319. [=](sycl::nd_item<3> item_ct1) {
  2320. clamp_f32(x, dst, min, max, k, item_ct1);
  2321. });
  2322. }
  2323. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  2324. const int nrows, queue_ptr stream) {
  2325. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  2326. const sycl::range<3> block_nums(1, nrows, 1);
  2327. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  2328. [=](sycl::nd_item<3> item_ct1)
  2329. [[intel::reqd_sub_group_size(WARP_SIZE)]] {
  2330. k_sum_rows_f32(x, dst, ncols, item_ct1);
  2331. });
  2332. }
  2333. static int next_power_of_2(int x) {
  2334. int n = 1;
  2335. while (n < x) {
  2336. n *= 2;
  2337. }
  2338. return n;
  2339. }
  2340. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  2341. const int nrows, ggml_sort_order order,
  2342. queue_ptr stream) {
  2343. // bitonic sort requires ncols to be power of 2
  2344. const int ncols_pad = next_power_of_2(ncols);
  2345. const sycl::range<3> block_dims(1, 1, ncols_pad);
  2346. const sycl::range<3> block_nums(1, nrows, 1);
  2347. const size_t shared_mem = ncols_pad * sizeof(int);
  2348. if (order == GGML_SORT_ORDER_ASC) {
  2349. stream->submit([&](sycl::handler &cgh) {
  2350. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  2351. sycl::range<1>(shared_mem), cgh);
  2352. cgh.parallel_for(
  2353. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  2354. [=](sycl::nd_item<3> item_ct1) {
  2355. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(
  2356. x, dst, ncols, ncols_pad, item_ct1,
  2357. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  2358. .get());
  2359. });
  2360. });
  2361. } else if (order == GGML_SORT_ORDER_DESC) {
  2362. stream->submit([&](sycl::handler &cgh) {
  2363. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  2364. sycl::range<1>(shared_mem), cgh);
  2365. cgh.parallel_for(
  2366. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  2367. [=](sycl::nd_item<3> item_ct1) {
  2368. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(
  2369. x, dst, ncols, ncols_pad, item_ct1,
  2370. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  2371. .get());
  2372. });
  2373. });
  2374. } else {
  2375. GGML_ABORT("fatal error");
  2376. }
  2377. }
  2378. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  2379. const int ncols_x, const int nrows_x,
  2380. const int rows_per_channel, const int n_past,
  2381. queue_ptr stream) {
  2382. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  2383. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  2384. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  2385. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  2386. [=](sycl::nd_item<3> item_ct1) {
  2387. diag_mask_inf_f32(x, dst, ncols_x,
  2388. rows_per_channel, n_past,
  2389. item_ct1);
  2390. });
  2391. }
  2392. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  2393. const struct ggml_tensor *src,
  2394. int64_t i3, int64_t i2,
  2395. int64_t i1_low, int64_t i1_high,
  2396. queue_ptr stream) try {
  2397. dpct::memcpy_direction kind;
  2398. char * src_ptr;
  2399. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  2400. kind = dpct::host_to_device;
  2401. src_ptr = (char *) src->data;
  2402. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  2403. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  2404. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  2405. kind = dpct::device_to_device;
  2406. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  2407. int id;
  2408. SYCL_CHECK(CHECK_TRY_ERROR(
  2409. id = get_current_device_id()));
  2410. // GGML_SYCL_DEBUG("current device index %d\n", id);
  2411. src_ptr = (char *) extra->data_device[id];
  2412. } else {
  2413. // GGML_SYCL_DEBUG("GGML_ABORT("fatal error")\n");
  2414. GGML_ABORT("fatal error");
  2415. }
  2416. char * dst_ptr = (char *) dst;
  2417. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  2418. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  2419. const enum ggml_type type = src->type;
  2420. const int64_t ts = ggml_type_size(type);
  2421. const int64_t bs = ggml_blck_size(type);
  2422. int64_t i1_diff = i1_high - i1_low;
  2423. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  2424. if (nb0 == ts && nb1 == ts*ne0/bs) {
  2425. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  2426. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  2427. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  2428. kind, *stream));
  2429. } else if (nb0 == ts) {
  2430. return CHECK_TRY_ERROR(
  2431. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  2432. ts * ne0 / bs, i1_diff, kind, *stream));
  2433. } else {
  2434. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  2435. const void * rx = (const void *) ((const char *) x + i1*nb1);
  2436. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  2437. // pretend the row is a matrix with cols=1
  2438. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  2439. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  2440. /*
  2441. DPCT1001:85: The statement could not be removed.
  2442. */
  2443. /*
  2444. DPCT1000:86: Error handling if-stmt was detected but could not be
  2445. rewritten.
  2446. */
  2447. if (r != 0) return r;
  2448. }
  2449. return 0;
  2450. }
  2451. }
  2452. catch (sycl::exception const &exc) {
  2453. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2454. << ", line:" << __LINE__ << std::endl;
  2455. std::exit(1);
  2456. }
  2457. static void ggml_sycl_op_get_rows(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2458. const ggml_tensor *src1, ggml_tensor *dst,
  2459. const float *src0_d, const float *src1_d,
  2460. float *dst_d, const queue_ptr &stream) {
  2461. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  2462. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  2463. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  2464. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  2465. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  2466. const int32_t * src1_i32 = (const int32_t *) src1_d;
  2467. switch (src0->type) {
  2468. case GGML_TYPE_F16:
  2469. get_rows_sycl_float(ctx, src0, src1, dst, (const sycl::half *)src0_d,
  2470. src1_i32, dst_d, stream);
  2471. break;
  2472. case GGML_TYPE_F32:
  2473. get_rows_sycl_float(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  2474. break;
  2475. case GGML_TYPE_Q4_0:
  2476. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  2477. break;
  2478. case GGML_TYPE_Q4_1:
  2479. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  2480. break;
  2481. case GGML_TYPE_Q5_0:
  2482. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  2483. break;
  2484. case GGML_TYPE_Q5_1:
  2485. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  2486. break;
  2487. case GGML_TYPE_Q8_0:
  2488. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(ctx, src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  2489. break;
  2490. default:
  2491. // TODO: k-quants
  2492. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  2493. GGML_ABORT("fatal error");
  2494. break;
  2495. }
  2496. }
  2497. template <class op>
  2498. inline void ggml_sycl_op_bin_bcast(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2499. const ggml_tensor *src1, ggml_tensor *dst,
  2500. const float *src0_dd, const float *src1_dd,
  2501. float *dst_dd,
  2502. const queue_ptr &main_stream) {
  2503. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  2504. op()(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  2505. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  2506. op()(ctx, src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  2507. (sycl::half *)dst_dd, main_stream);
  2508. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  2509. op()(ctx, src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  2510. main_stream);
  2511. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  2512. op()(ctx, src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  2513. main_stream);
  2514. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  2515. op()(ctx, src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  2516. main_stream);
  2517. } else {
  2518. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  2519. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  2520. GGML_ABORT("fatal error");
  2521. }
  2522. }
  2523. static void ggml_sycl_op_repeat(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2524. const ggml_tensor *src1, ggml_tensor *dst,
  2525. const float *src0_d, const float *src1_d,
  2526. float *dst_d,
  2527. const queue_ptr &main_stream) {
  2528. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(ctx, dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  2529. (void) src1;
  2530. (void) src1_d;
  2531. }
  2532. inline void ggml_sycl_op_add(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2533. ggml_tensor *dst, const float *src0_dd,
  2534. const float *src1_dd, float *dst_dd,
  2535. const queue_ptr &main_stream) {
  2536. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  2537. }
  2538. inline void ggml_sycl_op_acc(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2539. ggml_tensor *dst, const float *src0_dd,
  2540. const float *src1_dd, float *dst_dd,
  2541. const queue_ptr &main_stream) {
  2542. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2543. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  2544. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2545. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  2546. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  2547. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  2548. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  2549. int offset = dst->op_params[3] / 4; // offset in bytes
  2550. acc_f32_sycl(src0_dd, src1_dd, dst_dd, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, main_stream);
  2551. (void) dst;
  2552. }
  2553. inline void ggml_sycl_op_mul(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2554. ggml_tensor *dst, const float *src0_dd,
  2555. const float *src1_dd, float *dst_dd,
  2556. const queue_ptr &main_stream) {
  2557. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  2558. }
  2559. inline void ggml_sycl_op_div(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2560. ggml_tensor *dst, const float *src0_dd,
  2561. const float *src1_dd, float *dst_dd,
  2562. const queue_ptr &main_stream) {
  2563. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(ctx, src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  2564. }
  2565. inline void ggml_sycl_op_gelu(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2566. ggml_tensor *dst, const float *src0_dd,
  2567. const float *src1_dd, float *dst_dd,
  2568. const queue_ptr &main_stream) {
  2569. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2570. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2571. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  2572. (void) src1;
  2573. (void) dst;
  2574. (void) src1_dd;
  2575. }
  2576. inline void ggml_sycl_op_silu(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2577. ggml_tensor *dst, const float *src0_dd,
  2578. const float *src1_dd, float *dst_dd,
  2579. const queue_ptr &main_stream) {
  2580. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2581. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2582. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  2583. (void) src1;
  2584. (void) dst;
  2585. (void) src1_dd;
  2586. }
  2587. inline void ggml_sycl_op_gelu_quick(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2588. const ggml_tensor *src1, ggml_tensor *dst,
  2589. const float *src0_dd, const float *src1_dd,
  2590. float *dst_dd,
  2591. const queue_ptr &main_stream) {
  2592. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2593. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2594. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  2595. (void) src1;
  2596. (void) dst;
  2597. (void) src1_dd;
  2598. }
  2599. inline void ggml_sycl_op_tanh(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2600. ggml_tensor *dst, const float *src0_dd,
  2601. const float *src1_dd, float *dst_dd,
  2602. const queue_ptr &main_stream) {
  2603. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2604. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2605. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  2606. (void) src1;
  2607. (void) dst;
  2608. (void) src1_dd;
  2609. }
  2610. inline void ggml_sycl_op_relu(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2611. ggml_tensor *dst, const float *src0_dd,
  2612. const float *src1_dd, float *dst_dd,
  2613. const queue_ptr &main_stream) {
  2614. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2615. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2616. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  2617. (void) src1;
  2618. (void) dst;
  2619. (void) src1_dd;
  2620. }
  2621. static void ggml_sycl_op_hardsigmoid(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2622. const ggml_tensor *src1, ggml_tensor *dst,
  2623. const float *src0_dd, const float *src1_dd,
  2624. float *dst_dd,
  2625. const queue_ptr &main_stream) {
  2626. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2627. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2628. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  2629. (void) src1;
  2630. (void) dst;
  2631. (void) src1_dd;
  2632. }
  2633. static void ggml_sycl_op_hardswish(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2634. const ggml_tensor *src1, ggml_tensor *dst,
  2635. const float *src0_dd, const float *src1_dd,
  2636. float *dst_dd, const queue_ptr &main_stream) {
  2637. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2638. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2639. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  2640. (void) src1;
  2641. (void) dst;
  2642. (void) src1_dd;
  2643. }
  2644. inline void ggml_sycl_op_leaky_relu(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2645. const ggml_tensor *src1, ggml_tensor *dst,
  2646. const float *src0_dd, const float *src1_dd,
  2647. float *dst_dd,
  2648. const queue_ptr &main_stream) {
  2649. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2650. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2651. float negative_slope;
  2652. memcpy(&negative_slope, dst->op_params, sizeof(float));
  2653. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  2654. (void) src1;
  2655. (void) dst;
  2656. (void) src1_dd;
  2657. }
  2658. inline void ggml_sycl_op_sqr(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2659. ggml_tensor *dst, const float *src0_dd,
  2660. const float *src1_dd, float *dst_dd,
  2661. const queue_ptr &main_stream) {
  2662. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2663. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2664. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  2665. (void) src1;
  2666. (void) dst;
  2667. (void) src1_dd;
  2668. }
  2669. inline void ggml_sycl_op_upscale(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2670. const ggml_tensor *src1, ggml_tensor *dst,
  2671. const float *src0_dd, const float *src1_dd,
  2672. float *dst_dd,
  2673. const queue_ptr &main_stream) {
  2674. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2675. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  2676. const float sf0 = (float)dst->ne[0]/src0->ne[0];
  2677. const float sf1 = (float)dst->ne[1]/src0->ne[1];
  2678. const float sf2 = (float)dst->ne[2]/src0->ne[2];
  2679. const float sf3 = (float)dst->ne[3]/src0->ne[3];
  2680. upscale_f32_sycl(src0_dd, dst_dd, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
  2681. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], sf0, sf1, sf2, sf3,
  2682. main_stream);
  2683. (void) src1;
  2684. (void) dst;
  2685. (void) src1_dd;
  2686. }
  2687. inline void ggml_sycl_op_pad(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2688. ggml_tensor *dst, const float *src0_dd,
  2689. const float *src1_dd, float *dst_dd,
  2690. const queue_ptr &main_stream) {
  2691. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2692. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  2693. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  2694. pad_f32_sycl(src0_dd, dst_dd,
  2695. src0->ne[0], src0->ne[1], src0->ne[2],
  2696. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  2697. (void) src1;
  2698. (void) dst;
  2699. (void) src1_dd;
  2700. }
  2701. inline void ggml_sycl_op_mul_mat_sycl(
  2702. ggml_backend_sycl_context & ctx,
  2703. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2704. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2705. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2706. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2707. const queue_ptr &stream) try {
  2708. GGML_ASSERT(src0_dd_i != nullptr);
  2709. GGML_ASSERT(src1_ddf_i != nullptr);
  2710. GGML_ASSERT(dst_dd_i != nullptr);
  2711. const int64_t ne00 = src0->ne[0];
  2712. const int64_t ne10 = src1->ne[0];
  2713. const int64_t ne0 = dst->ne[0];
  2714. const int64_t row_diff = row_high - row_low;
  2715. int id;
  2716. SYCL_CHECK(
  2717. CHECK_TRY_ERROR(id = get_current_device_id()));
  2718. // the main device has a larger memory buffer to hold the results from all GPUs
  2719. // ldc == nrows of the matrix that cuBLAS writes into
  2720. int ldc = id == ctx.device ? ne0 : row_diff;
  2721. #ifdef GGML_SYCL_F16
  2722. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  2723. #else
  2724. bool use_fp16 = false;
  2725. #endif
  2726. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  2727. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  2728. dst->op_params[0] == GGML_PREC_DEFAULT) {
  2729. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  2730. ggml_sycl_pool_alloc<sycl::half> src0_as_f16(ctx.pool());
  2731. if (src0->type != GGML_TYPE_F16) {
  2732. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  2733. GGML_ASSERT(to_fp16_sycl != nullptr);
  2734. size_t ne = row_diff*ne00;
  2735. src0_as_f16.alloc(ne);
  2736. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  2737. }
  2738. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  2739. ? (const sycl::half *)src0_dd_i
  2740. : src0_as_f16.get();
  2741. ggml_sycl_pool_alloc<sycl::half> src1_as_f16(ctx.pool());
  2742. if (src1->type != GGML_TYPE_F16) {
  2743. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  2744. GGML_ASSERT(to_fp16_sycl != nullptr);
  2745. size_t ne = src1_ncols*ne10;
  2746. src1_as_f16.alloc(ne);
  2747. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  2748. }
  2749. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  2750. ? (const sycl::half *)src1->data + src1_padded_row_size
  2751. : src1_as_f16.get();
  2752. ggml_sycl_pool_alloc<sycl::half> dst_f16(ctx.pool(), row_diff * src1_ncols);
  2753. const sycl::half alpha_f16 = 1.0f;
  2754. const sycl::half beta_f16 = 0.0f;
  2755. #if !GGML_SYCL_DNNL
  2756. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  2757. *stream, oneapi::mkl::transpose::trans,
  2758. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  2759. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  2760. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  2761. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  2762. dpct::library_data_t::real_half)));
  2763. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  2764. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  2765. #else
  2766. auto dnnl_stream = ctx.stream_dnnl(stream);
  2767. DnnlGemmWrapper::row_gemm(dnnl_stream, false, true, src1_ncols, row_diff, ne10, src1_ptr, DnnlGemmWrapper::to_dt<sycl::half>(),
  2768. src0_ptr, DnnlGemmWrapper::to_dt<sycl::half>(), dst_f16.get(), DnnlGemmWrapper::to_dt<sycl::half>());
  2769. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  2770. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff* src1_ncols, stream);
  2771. #endif
  2772. }
  2773. else {
  2774. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  2775. ggml_sycl_pool_alloc<float> src0_ddq_as_f32(ctx.pool());
  2776. ggml_sycl_pool_alloc<float> src1_ddq_as_f32(ctx.pool());
  2777. if (src0->type != GGML_TYPE_F32) {
  2778. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  2779. GGML_ASSERT(to_fp32_sycl != nullptr);
  2780. src0_ddq_as_f32.alloc(row_diff*ne00);
  2781. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  2782. }
  2783. if (src1->type != GGML_TYPE_F32) {
  2784. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  2785. GGML_ASSERT(to_fp32_sycl != nullptr);
  2786. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  2787. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  2788. }
  2789. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  2790. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  2791. const float alpha = 1.0f;
  2792. const float beta = 0.0f;
  2793. #if !GGML_SYCL_DNNL
  2794. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  2795. *stream, oneapi::mkl::transpose::trans,
  2796. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  2797. dpct::get_value(&alpha, *stream), src0_ddf_i, ne00,
  2798. src1_ddf1_i, ne10, dpct::get_value(&beta, *stream),
  2799. dst_dd_i, ldc)));
  2800. #else
  2801. auto dnnl_stream = ctx.stream_dnnl(stream);
  2802. DnnlGemmWrapper::row_gemm(dnnl_stream, false, true, src1_ncols, row_diff, ne10, src1_ddf1_i, DnnlGemmWrapper::to_dt<float>(),
  2803. src0_ddf_i, DnnlGemmWrapper::to_dt<float>(), dst_dd_i, DnnlGemmWrapper::to_dt<float>());
  2804. #endif
  2805. }
  2806. (void) dst;
  2807. (void) src1_ddq_i;
  2808. (void) src1_padded_row_size;
  2809. }
  2810. catch (sycl::exception const &exc) {
  2811. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2812. << ", line:" << __LINE__ << std::endl;
  2813. std::exit(1);
  2814. }
  2815. static void ggml_sycl_op_pool2d(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2816. const ggml_tensor *src1, ggml_tensor *dst,
  2817. const float *src0_dd, const float *src1_dd,
  2818. float *dst_dd, const queue_ptr &main_stream) {
  2819. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2820. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2821. const int32_t * opts = (const int32_t *)dst->op_params;
  2822. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  2823. const int k0 = opts[1];
  2824. const int k1 = opts[2];
  2825. const int s0 = opts[3];
  2826. const int s1 = opts[4];
  2827. const int p0 = opts[5];
  2828. const int p1 = opts[6];
  2829. const int64_t IH = src0->ne[1];
  2830. const int64_t IW = src0->ne[0];
  2831. const int64_t N = dst->ne[3];
  2832. const int64_t OC = dst->ne[2];
  2833. const int64_t OH = dst->ne[1];
  2834. const int64_t OW = dst->ne[0];
  2835. const int parallel_elements = N * OC * OH * OW;
  2836. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  2837. sycl::range<3> block_nums(1, 1, num_blocks);
  2838. main_stream->parallel_for(
  2839. sycl::nd_range<3>(block_nums *
  2840. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  2841. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  2842. [=](sycl::nd_item<3> item_ct1) {
  2843. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  2844. parallel_elements, src0_dd, dst_dd, op,
  2845. item_ct1);
  2846. });
  2847. (void) src1;
  2848. (void) src1_dd;
  2849. }
  2850. inline void ggml_sycl_op_sum_rows(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2851. const ggml_tensor *src1, ggml_tensor *dst,
  2852. const float *src0_dd, const float *src1_dd,
  2853. float *dst_dd,
  2854. const queue_ptr &main_stream) {
  2855. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2856. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2857. const int64_t ncols = src0->ne[0];
  2858. const int64_t nrows = ggml_nrows(src0);
  2859. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  2860. (void) src1;
  2861. (void) dst;
  2862. (void) src1_dd;
  2863. }
  2864. inline void ggml_sycl_op_argsort(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2865. const ggml_tensor *src1, ggml_tensor *dst,
  2866. const float *src0_dd, const float *src1_dd,
  2867. float *dst_dd,
  2868. const queue_ptr &main_stream) {
  2869. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2870. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  2871. const int64_t ncols = src0->ne[0];
  2872. const int64_t nrows = ggml_nrows(src0);
  2873. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  2874. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  2875. (void) src1;
  2876. (void) dst;
  2877. (void) src1_dd;
  2878. }
  2879. inline void ggml_sycl_op_diag_mask_inf(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2880. const ggml_tensor *src1,
  2881. ggml_tensor *dst, const float *src0_dd,
  2882. const float *src1_dd, float *dst_dd,
  2883. const queue_ptr &main_stream) {
  2884. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2885. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2886. const int64_t ne00 = src0->ne[0];
  2887. const int64_t ne01 = src0->ne[1];
  2888. const int nrows0 = ggml_nrows(src0);
  2889. const int n_past = ((int32_t *) dst->op_params)[0];
  2890. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  2891. (void) src1;
  2892. (void) dst;
  2893. (void) src1_dd;
  2894. }
  2895. inline void ggml_sycl_op_scale(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2896. ggml_tensor *dst, const float *src0_dd,
  2897. const float *src1_dd, float *dst_dd,
  2898. const queue_ptr &main_stream) {
  2899. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2900. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2901. float scale;
  2902. memcpy(&scale, dst->op_params, sizeof(float));
  2903. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  2904. /*
  2905. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  2906. error codes. The call was replaced with 0. You need to rewrite this code.
  2907. */
  2908. SYCL_CHECK(0);
  2909. (void) src1;
  2910. (void) dst;
  2911. (void) src1_dd;
  2912. }
  2913. inline void ggml_sycl_op_clamp(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  2914. ggml_tensor *dst, const float *src0_dd,
  2915. const float *src1_dd, float *dst_dd,
  2916. const queue_ptr &main_stream) {
  2917. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  2918. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  2919. float min;
  2920. float max;
  2921. memcpy(&min, dst->op_params, sizeof(float));
  2922. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  2923. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  2924. /*
  2925. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  2926. error codes. The call was replaced with 0. You need to rewrite this code.
  2927. */
  2928. SYCL_CHECK(0);
  2929. (void) src1;
  2930. (void) dst;
  2931. (void) src1_dd;
  2932. }
  2933. static void ggml_sycl_op_flatten(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2934. const ggml_tensor *src1, ggml_tensor *dst,
  2935. const ggml_sycl_op_flatten_t op) try {
  2936. const int64_t nrows0 = ggml_nrows(src0);
  2937. const bool use_src1 = src1 != nullptr;
  2938. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  2939. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2940. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  2941. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  2942. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  2943. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  2944. // dd = data device
  2945. float * src0_ddf = (float *) src0->data;
  2946. float * src1_ddf = use_src1 ? (float *) src1->data : nullptr;
  2947. float * dst_ddf = (float *) dst->data;
  2948. ggml_sycl_pool_alloc<float> src0_f(ctx.pool());
  2949. ggml_sycl_pool_alloc<float> src1_f(ctx.pool());
  2950. ggml_sycl_pool_alloc<float> dst_f(ctx.pool());
  2951. ggml_sycl_set_device(ctx.device);
  2952. queue_ptr main_stream = ctx.stream();
  2953. // GGML_SYCL_DEBUG("ctx.device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  2954. // ctx.device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  2955. // do the computation
  2956. op(ctx, src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  2957. // print_ggml_tensor("tensor", dst);
  2958. }
  2959. catch (sycl::exception const &exc) {
  2960. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2961. << ", line:" << __LINE__ << std::endl;
  2962. std::exit(1);
  2963. }
  2964. static void ggml_sycl_set_peer_access(const int n_tokens, int main_device) {
  2965. static bool peer_access_enabled = false;
  2966. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  2967. if (peer_access_enabled == enable_peer_access) {
  2968. return;
  2969. }
  2970. #ifdef NDEBUG
  2971. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2972. SYCL_CHECK(ggml_sycl_set_device(i));
  2973. }
  2974. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  2975. SYCL_CHECK(ggml_sycl_set_device(i));
  2976. for (int id_other = 0; id_other < ggml_sycl_info().device_count; ++id_other) {
  2977. if (i == id_other) {
  2978. continue;
  2979. }
  2980. if (i != main_device && id_other != main_device) {
  2981. continue;
  2982. }
  2983. // int can_access_peer;
  2984. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  2985. // if (can_access_peer) {
  2986. // if (enable_peer_access) {
  2987. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  2988. // } else {
  2989. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  2990. // }
  2991. // }
  2992. }
  2993. }
  2994. #endif // NDEBUG
  2995. peer_access_enabled = enable_peer_access;
  2996. }
  2997. static void ggml_sycl_op_mul_mat(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  2998. const ggml_tensor *src1, ggml_tensor *dst,
  2999. ggml_sycl_op_mul_mat_t op,
  3000. const bool convert_src1_to_q8_1) try {
  3001. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  3002. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  3003. const int64_t nrows1 = ggml_nrows(src1);
  3004. GGML_ASSERT(ne03 == ne13);
  3005. const int64_t ne0 = dst->ne[0];
  3006. const int64_t ne1 = dst->ne[1];
  3007. const int nb2 = dst->nb[2];
  3008. const int nb3 = dst->nb[3];
  3009. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  3010. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  3011. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  3012. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  3013. const int64_t i02_divisor = ne12 / ne02;
  3014. const size_t src0_ts = ggml_type_size(src0->type);
  3015. const size_t src0_bs = ggml_blck_size(src0->type);
  3016. const size_t q8_1_ts = sizeof(block_q8_1);
  3017. const size_t q8_1_bs = QK8_1;
  3018. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  3019. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  3020. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  3021. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  3022. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  3023. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  3024. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  3025. GGML_ASSERT(!(split && ne02 > 1));
  3026. GGML_ASSERT(!(split && ne03 > 1));
  3027. GGML_ASSERT(!(split && ne02 < ne12));
  3028. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  3029. if (split) {
  3030. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  3031. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  3032. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  3033. tensor_split = buft_ctx->tensor_split;
  3034. }
  3035. struct dev_data {
  3036. ggml_sycl_pool_alloc<char> src0_dd_alloc;
  3037. ggml_sycl_pool_alloc<float> src1_ddf_alloc;
  3038. ggml_sycl_pool_alloc<char> src1_ddq_alloc;
  3039. ggml_sycl_pool_alloc<float> dst_dd_alloc;
  3040. char *src0_dd = nullptr;
  3041. float *src1_ddf = nullptr; // float
  3042. char *src1_ddq = nullptr; // q8_1
  3043. float *dst_dd = nullptr;
  3044. int64_t row_low;
  3045. int64_t row_high;
  3046. };
  3047. dev_data dev[GGML_SYCL_MAX_DEVICES];
  3048. int used_devices = 0;
  3049. queue_ptr main_stream = ctx.stream();
  3050. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  3051. // by default, use all rows
  3052. dev[i].row_low = 0;
  3053. dev[i].row_high = ne01;
  3054. // for multi GPU, get the row boundaries from tensor split
  3055. // and round to mul_mat_q tile sizes
  3056. if (split) {
  3057. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  3058. if (i != 0) {
  3059. dev[i].row_low = ne01*tensor_split[i];
  3060. if (dev[i].row_low < ne01) {
  3061. dev[i].row_low -= dev[i].row_low % rounding;
  3062. }
  3063. }
  3064. if (i != ggml_sycl_info().device_count - 1) {
  3065. dev[i].row_high = ne01*tensor_split[i + 1];
  3066. if (dev[i].row_high < ne01) {
  3067. dev[i].row_high -= dev[i].row_high % rounding;
  3068. }
  3069. }
  3070. }
  3071. }
  3072. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  3073. if ((!split && i != ctx.device) || dev[i].row_low == dev[i].row_high) {
  3074. continue;
  3075. }
  3076. used_devices++;
  3077. const bool src1_on_device = i == ctx.device;
  3078. const bool dst_on_device = i == ctx.device;
  3079. ggml_sycl_set_device(i);
  3080. queue_ptr stream = ctx.stream(i, 0);
  3081. if (src0_is_contiguous) {
  3082. dev[i].src0_dd = (char *) src0->data;
  3083. } else {
  3084. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ctx.pool(i), ggml_nbytes(src0));
  3085. }
  3086. if (src1_on_device && src1_is_contiguous) {
  3087. dev[i].src1_ddf = (float *) src1->data;
  3088. } else {
  3089. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ctx.pool(i), ggml_nelements(src1));
  3090. }
  3091. if (convert_src1_to_q8_1) {
  3092. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(ctx.pool(i), nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  3093. if (src1_on_device && src1_is_contiguous) {
  3094. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  3095. /*
  3096. DPCT1010:90: SYCL uses exceptions to report errors and does not
  3097. use the error codes. The call was replaced with 0. You need to
  3098. rewrite this code.
  3099. */
  3100. SYCL_CHECK(0);
  3101. }
  3102. }
  3103. if (dst_on_device) {
  3104. dev[i].dst_dd = (float *) dst->data;
  3105. } else {
  3106. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  3107. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(ctx.pool(i), size_dst_ddf);
  3108. }
  3109. }
  3110. // if multiple devices are used they need to wait for the main device
  3111. // here an event is recorded that signals that the main device has finished calculating the input data
  3112. if (split && used_devices > 1) {
  3113. ggml_sycl_set_device(ctx.device);
  3114. /*
  3115. DPCT1024:91: The original code returned the error code that was further
  3116. consumed by the program logic. This original code was replaced with 0.
  3117. You may need to rewrite the program logic consuming the error code.
  3118. */
  3119. SYCL_CHECK(CHECK_TRY_ERROR(
  3120. *src0_extra->events[ctx.device][0] =
  3121. ctx.stream()->ext_oneapi_submit_barrier()));
  3122. }
  3123. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  3124. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  3125. const int64_t is = split ? (src1_col_0/src1_col_stride) % GGML_SYCL_MAX_STREAMS : 0;
  3126. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  3127. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  3128. if ((!split && i != ctx.device) || dev[i].row_low == dev[i].row_high) {
  3129. continue;
  3130. }
  3131. const bool src1_on_device = i == ctx.device;
  3132. const bool dst_on_device = i == ctx.device;
  3133. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  3134. ggml_sycl_set_device(i);
  3135. queue_ptr stream = ctx.stream(i, is);
  3136. // wait for main GPU data if necessary
  3137. if (split && (i != ctx.device || is != 0)) {
  3138. /*
  3139. DPCT1009:163: SYCL uses exceptions to report errors and does not
  3140. use the error codes. The original code was commented out and a
  3141. warning string was inserted. You need to rewrite this code.
  3142. */
  3143. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  3144. {*src0_extra->events[ctx.device][0]})));
  3145. }
  3146. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  3147. const int64_t i03 = i0 / ne12;
  3148. const int64_t i02 = i0 % ne12;
  3149. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  3150. // for split tensors the data begins at i0 == i0_offset_low
  3151. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  3152. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  3153. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  3154. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  3155. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  3156. // in that case an offset on dst_ddf_i is needed
  3157. if (i == ctx.device) {
  3158. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  3159. }
  3160. // copy src0, src1 to device if necessary
  3161. if (src1_is_contiguous) {
  3162. if (i != ctx.device) {
  3163. if (convert_src1_to_q8_1) {
  3164. char * src1_ddq_i_source = dev[ctx.device].src1_ddq + src1_ddq_i_offset;
  3165. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  3166. src1_ddq_i, src1_ddq_i_source,
  3167. src1_ncols * src1_padded_col_size * q8_1_ts /
  3168. q8_1_bs).wait()));
  3169. } else {
  3170. float * src1_ddf_i_source = (float *) src1_extra->data_device[ctx.device];
  3171. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  3172. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  3173. src1_ddf_i, src1_ddf_i_source,
  3174. src1_ncols * ne10 * sizeof(float))));
  3175. }
  3176. }
  3177. } else if (src1_on_device && !src1_is_contiguous) {
  3178. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  3179. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  3180. } else {
  3181. GGML_ABORT("fatal error");
  3182. }
  3183. if (convert_src1_to_q8_1 && !src1_is_contiguous) {
  3184. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  3185. /*
  3186. DPCT1010:92: SYCL uses exceptions to report errors and does
  3187. not use the error codes. The call was replaced with 0. You
  3188. need to rewrite this code.
  3189. */
  3190. SYCL_CHECK(0);
  3191. }
  3192. if (src1_col_0 == 0 && !src0_is_contiguous && i02 % i02_divisor == 0) {
  3193. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  3194. }
  3195. if (src1->type == GGML_TYPE_F16) {
  3196. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  3197. }
  3198. // do the computation
  3199. SYCL_CHECK(CHECK_TRY_ERROR(op(ctx, src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  3200. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream)));
  3201. /*
  3202. DPCT1010:93: SYCL uses exceptions to report errors and does not
  3203. use the error codes. The call was replaced with 0. You need to
  3204. rewrite this code.
  3205. */
  3206. SYCL_CHECK(0);
  3207. // copy dst to host or other device if necessary
  3208. if (!dst_on_device) {
  3209. void * dst_off_device = dst->data;
  3210. if (split) {
  3211. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  3212. // dst is NOT transposed.
  3213. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  3214. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  3215. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  3216. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  3217. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  3218. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  3219. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  3220. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  3221. row_diff * sizeof(float), row_diff * sizeof(float),
  3222. src1_ncols, dpct::device_to_device, *stream)));
  3223. } else {
  3224. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  3225. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  3226. dhf_dst_i += src1_col_0*ne0;
  3227. SYCL_CHECK(CHECK_TRY_ERROR(
  3228. stream->memcpy(dhf_dst_i, dst_dd_i,
  3229. src1_ncols * ne0 * sizeof(float)).wait()));
  3230. }
  3231. }
  3232. // add event for the main device to wait on until other device is done
  3233. if (split && (i != ctx.device || is != 0)) {
  3234. /*
  3235. DPCT1024:94: The original code returned the error code that
  3236. was further consumed by the program logic. This original
  3237. code was replaced with 0. You may need to rewrite the
  3238. program logic consuming the error code.
  3239. */
  3240. SYCL_CHECK(CHECK_TRY_ERROR(
  3241. *src0_extra->events[i][is] =
  3242. stream->ext_oneapi_submit_barrier()));
  3243. }
  3244. }
  3245. }
  3246. }
  3247. // main device waits for all other devices to be finished
  3248. if (split && ggml_sycl_info().device_count > 1) {
  3249. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  3250. is_max = is_max <= GGML_SYCL_MAX_STREAMS ? is_max : GGML_SYCL_MAX_STREAMS;
  3251. ggml_sycl_set_device(ctx.device);
  3252. for (int i = 0; i < ggml_sycl_info().device_count; ++i) {
  3253. if (dev[i].row_low == dev[i].row_high) {
  3254. continue;
  3255. }
  3256. for (int64_t is = 0; is < is_max; ++is) {
  3257. SYCL_CHECK(CHECK_TRY_ERROR(
  3258. ctx.stream()->ext_oneapi_submit_barrier(
  3259. {*src0_extra->events[i][is]})));
  3260. }
  3261. }
  3262. }
  3263. }
  3264. catch (sycl::exception const &exc) {
  3265. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3266. << ", line:" << __LINE__ << std::endl;
  3267. std::exit(1);
  3268. }
  3269. static void ggml_sycl_repeat(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3270. GGML_SYCL_DEBUG("call %s\n", __func__);
  3271. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_repeat);
  3272. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3273. }
  3274. static void ggml_sycl_get_rows(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3275. GGML_SYCL_DEBUG("call %s\n", __func__);
  3276. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_get_rows);
  3277. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3278. }
  3279. static void ggml_sycl_add(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3280. GGML_SYCL_DEBUG("call %s\n", __func__);
  3281. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_add);
  3282. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3283. }
  3284. static void ggml_sycl_acc(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3285. GGML_SYCL_DEBUG("call %s\n", __func__);
  3286. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_acc);
  3287. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3288. }
  3289. static void ggml_sycl_mul(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3290. GGML_SYCL_DEBUG("call %s\n", __func__);
  3291. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_mul);
  3292. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3293. }
  3294. static void ggml_sycl_div(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3295. GGML_SYCL_DEBUG("call %s\n", __func__);
  3296. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_div);
  3297. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3298. }
  3299. static void ggml_sycl_gelu(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3300. GGML_SYCL_DEBUG("call %s\n", __func__);
  3301. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_gelu);
  3302. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3303. }
  3304. static void ggml_sycl_silu(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3305. GGML_SYCL_DEBUG("call %s\n", __func__);
  3306. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_silu);
  3307. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3308. }
  3309. static void ggml_sycl_gelu_quick(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3310. GGML_SYCL_DEBUG("call %s\n", __func__);
  3311. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_gelu_quick);
  3312. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3313. }
  3314. static void ggml_sycl_tanh(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3315. GGML_SYCL_DEBUG("call %s\n", __func__);
  3316. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_tanh);
  3317. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3318. }
  3319. static void ggml_sycl_relu(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3320. GGML_SYCL_DEBUG("call %s\n", __func__);
  3321. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_relu);
  3322. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3323. }
  3324. static void ggml_sycl_hardsigmoid(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3325. GGML_SYCL_DEBUG("call %s\n", __func__);
  3326. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_hardsigmoid);
  3327. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3328. }
  3329. static void ggml_sycl_hardswish(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3330. GGML_SYCL_DEBUG("call %s\n", __func__);
  3331. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_hardswish);
  3332. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3333. }
  3334. static void ggml_sycl_leaky_relu(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3335. GGML_SYCL_DEBUG("call %s\n", __func__);
  3336. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_leaky_relu);
  3337. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3338. }
  3339. static void ggml_sycl_sqr(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3340. GGML_SYCL_DEBUG("call %s\n", __func__);
  3341. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_sqr);
  3342. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3343. }
  3344. static void ggml_sycl_norm(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3345. GGML_SYCL_DEBUG("call %s\n", __func__);
  3346. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_norm);
  3347. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3348. }
  3349. static void ggml_sycl_group_norm(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3350. GGML_SYCL_DEBUG("call %s\n", __func__);
  3351. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_group_norm);
  3352. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3353. }
  3354. static void ggml_sycl_upscale(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3355. GGML_SYCL_DEBUG("call %s\n", __func__);
  3356. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_upscale);
  3357. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3358. }
  3359. static void ggml_sycl_pad(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3360. GGML_SYCL_DEBUG("call %s\n", __func__);
  3361. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_pad);
  3362. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3363. }
  3364. static void ggml_sycl_rms_norm(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3365. GGML_SYCL_DEBUG("call %s\n", __func__);
  3366. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_rms_norm);
  3367. GGML_SYCL_DEBUG("call %s done\n", __func__);
  3368. }
  3369. static void ggml_sycl_mul_mat_vec_p021(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  3370. const ggml_tensor *src1,
  3371. ggml_tensor *dst) try {
  3372. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  3373. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  3374. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  3375. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  3376. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3377. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3378. const int64_t ne00 = src0->ne[0];
  3379. const int64_t ne01 = src0->ne[1];
  3380. const int64_t ne02 = src0->ne[2];
  3381. const int64_t ne12 = src1->ne[2];
  3382. SYCL_CHECK(ggml_sycl_set_device(ctx.device));
  3383. queue_ptr main_stream = ctx.stream();
  3384. void * src0_ddq = src0->data;
  3385. float * src1_ddf = (float *) src1->data;
  3386. float * dst_ddf = (float *) dst->data;
  3387. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  3388. }
  3389. catch (sycl::exception const &exc) {
  3390. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3391. << ", line:" << __LINE__ << std::endl;
  3392. std::exit(1);
  3393. }
  3394. static void ggml_sycl_mul_mat_vec_nc(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  3395. const ggml_tensor *src1,
  3396. ggml_tensor *dst) try {
  3397. GGML_ASSERT(!ggml_is_transposed(src0));
  3398. GGML_ASSERT(!ggml_is_transposed(src1));
  3399. GGML_ASSERT(!ggml_is_permuted(src0));
  3400. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  3401. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3402. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3403. const int64_t ne00 = src0->ne[0];
  3404. const int64_t ne01 = src0->ne[1];
  3405. const int64_t ne02 = src0->ne[2];
  3406. const int64_t nb01 = src0->nb[1];
  3407. const int64_t nb02 = src0->nb[2];
  3408. const int64_t ne12 = src1->ne[2];
  3409. SYCL_CHECK(ggml_sycl_set_device(ctx.device));
  3410. queue_ptr main_stream = ctx.stream();
  3411. void * src0_ddq = src0->data;
  3412. float * src1_ddf = (float *) src1->data;
  3413. float * dst_ddf = (float *) dst->data;
  3414. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  3415. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  3416. ggml_mul_mat_vec_nc_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, row_stride_x, ne02, ne12, channel_stride_x, main_stream);
  3417. }
  3418. catch (sycl::exception const &exc) {
  3419. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3420. << ", line:" << __LINE__ << std::endl;
  3421. std::exit(1);
  3422. }
  3423. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  3424. const sycl::half *src1_as_f16, char *dst,
  3425. const void **ptrs_src, void **ptrs_dst,
  3426. int64_t ne12, int64_t ne13, int64_t ne23,
  3427. size_t nb02, size_t nb03, size_t nb12,
  3428. size_t nb13, size_t nbd2, size_t nbd3,
  3429. int64_t r2, int64_t r3,
  3430. const sycl::nd_item<3> &item_ct1) {
  3431. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  3432. item_ct1.get_local_id(2);
  3433. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  3434. item_ct1.get_local_id(1);
  3435. if (i13 >= ne13 || i12 >= ne12) {
  3436. return;
  3437. }
  3438. int64_t i03 = i13 / r3;
  3439. int64_t i02 = i12 / r2;
  3440. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  3441. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  3442. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  3443. }
  3444. static void ggml_sycl_mul_mat_batched_sycl(ggml_backend_sycl_context & ctx,
  3445. const ggml_tensor *src0,
  3446. const ggml_tensor *src1,
  3447. ggml_tensor *dst) try {
  3448. GGML_ASSERT(!ggml_is_transposed(src0));
  3449. GGML_ASSERT(!ggml_is_transposed(src1));
  3450. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  3451. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3452. GGML_TENSOR_BINARY_OP_LOCALS
  3453. const int64_t ne_dst = ggml_nelements(dst);
  3454. SYCL_CHECK(ggml_sycl_set_device(ctx.device));
  3455. queue_ptr main_stream = ctx.stream();;
  3456. void * src0_ddq = src0->data;
  3457. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  3458. float * src1_ddf = (float *) src1->data;
  3459. float * dst_ddf = (float *) dst->data;
  3460. // convert src1 to fp16
  3461. ggml_sycl_pool_alloc<sycl::half> src1_f16_alloc(ctx.pool());
  3462. if (src1->type != GGML_TYPE_F16) {
  3463. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  3464. const int64_t ne_src1 = ggml_nelements(src1);
  3465. src1_f16_alloc.alloc(ne_src1);
  3466. GGML_ASSERT(to_fp16_sycl != nullptr);
  3467. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  3468. }
  3469. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  3470. : src1_f16_alloc.get();
  3471. char * dst_t;
  3472. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_float;
  3473. dpct::library_data_t cu_data_type = dpct::library_data_t::real_float;
  3474. // dst strides
  3475. size_t nbd2 = dst->nb[2];
  3476. size_t nbd3 = dst->nb[3];
  3477. const float alpha_f32 = 1.0f;
  3478. const float beta_f32 = 0.0f;
  3479. const void * alpha = &alpha_f32;
  3480. const void * beta = &beta_f32;
  3481. dst_t = (char *) dst_ddf;
  3482. GGML_ASSERT(ne12 % ne02 == 0);
  3483. GGML_ASSERT(ne13 % ne03 == 0);
  3484. // broadcast factors
  3485. const int64_t r2 = ne12/ne02;
  3486. const int64_t r3 = ne13/ne03;
  3487. if (r2 == 1 && r3 == 1 && ggml_is_contiguous_2(src0) && ggml_is_contiguous_2(src1)) {
  3488. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  3489. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  3490. *main_stream, oneapi::mkl::transpose::trans,
  3491. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  3492. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  3493. nb01 / nb00, nb02 / nb00,
  3494. (const char *)src1_f16, dpct::library_data_t::real_half,
  3495. nb11 / nb10, nb12 / nb10, beta,
  3496. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  3497. ne12 * ne13, cu_compute_type)));
  3498. } else {
  3499. const int ne23 = ne12*ne13;
  3500. ggml_sycl_pool_alloc<const void *> ptrs_src(ctx.pool(), 2*ne23);
  3501. ggml_sycl_pool_alloc< void *> ptrs_dst(ctx.pool(), 1*ne23);
  3502. sycl::range<3> block_dims(1, ne12, ne13);
  3503. /*
  3504. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  3505. the limit. To get the device limit, query
  3506. info::device::max_work_group_size. Adjust the work-group size if needed.
  3507. */
  3508. {
  3509. dpct::has_capability_or_fail(main_stream->get_device(),
  3510. {sycl::aspect::fp16});
  3511. main_stream->submit([&](sycl::handler &cgh) {
  3512. const void **ptrs_src_get = ptrs_src.get();
  3513. void **ptrs_dst_get = ptrs_dst.get();
  3514. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  3515. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  3516. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  3517. [=](sycl::nd_item<3> item_ct1) {
  3518. k_compute_batched_ptrs(
  3519. src0_as_f16, src1_f16,
  3520. dst_t, ptrs_src_get,
  3521. ptrs_dst_get, ne12, ne13, ne23,
  3522. nb02, nb03, nb12_scaled, nb13_scaled,
  3523. nbd2, nbd3, r2, r3, item_ct1);
  3524. });
  3525. });
  3526. }
  3527. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  3528. *main_stream, oneapi::mkl::transpose::trans,
  3529. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  3530. (const void **)(ptrs_src.get() + 0 * ne23),
  3531. dpct::library_data_t::real_half, nb01 / nb00,
  3532. (const void **)(ptrs_src.get() + 1 * ne23),
  3533. dpct::library_data_t::real_half, nb11 / nb10, beta,
  3534. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  3535. cu_compute_type)));
  3536. }
  3537. }
  3538. catch (sycl::exception const &exc) {
  3539. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3540. << ", line:" << __LINE__ << std::endl;
  3541. std::exit(1);
  3542. }
  3543. inline bool ggml_sycl_supports_mmq(enum ggml_type type) {
  3544. // TODO: accuracy issues in MMQ
  3545. return false;
  3546. }
  3547. bool ggml_sycl_supports_dmmv(enum ggml_type type) {
  3548. switch (type) {
  3549. case GGML_TYPE_Q4_0:
  3550. case GGML_TYPE_Q4_1:
  3551. case GGML_TYPE_Q5_0:
  3552. case GGML_TYPE_Q5_1:
  3553. case GGML_TYPE_Q8_0:
  3554. case GGML_TYPE_Q2_K:
  3555. case GGML_TYPE_Q3_K:
  3556. case GGML_TYPE_Q4_K:
  3557. case GGML_TYPE_Q5_K:
  3558. case GGML_TYPE_Q6_K:
  3559. case GGML_TYPE_F16:
  3560. return true;
  3561. default:
  3562. return false;
  3563. }
  3564. }
  3565. static void ggml_sycl_mul_mat(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3566. const bool split = ggml_backend_buffer_is_sycl_split(src0->buffer);
  3567. int64_t min_compute_capability = INT_MAX;
  3568. if (split) {
  3569. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  3570. auto & tensor_split = buft_ctx->tensor_split;
  3571. for (int id = 0; id < ggml_sycl_info().device_count; ++id) {
  3572. // skip devices that are not going to do any work:
  3573. if (tensor_split[id] >= (id + 1 < ggml_sycl_info().device_count ? tensor_split[id + 1] : 1.0f)) {
  3574. continue;
  3575. }
  3576. if (min_compute_capability > ggml_sycl_info().devices[id].cc) {
  3577. min_compute_capability = ggml_sycl_info().devices[id].cc;
  3578. }
  3579. }
  3580. } else {
  3581. min_compute_capability = ggml_sycl_info().devices[ctx.device].cc;
  3582. }
  3583. // check data types and tensor shapes for custom matrix multiplication kernels:
  3584. bool use_dequantize_mul_mat_vec = ggml_sycl_supports_dmmv(src0->type)
  3585. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  3586. && src0->ne[0] % GGML_SYCL_DMMV_X == 0 && src1->ne[1] == 1;
  3587. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  3588. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  3589. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  3590. bool use_mul_mat_q = ggml_sycl_supports_mmq(src0->type)
  3591. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  3592. // mmvq and mmq need the __dp4a instruction which is available for gen12+
  3593. // Workaround in https://github.com/ggerganov/llama.cpp/commit/95f84d5ce8b449a9b16009434aca800df504a02e
  3594. use_mul_mat_q = use_mul_mat_q && (src0->type != GGML_TYPE_IQ2_XXS);
  3595. #ifdef SYCL_USE_XMX
  3596. use_mul_mat_q = use_mul_mat_q && (src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  3597. #endif // SYCL_USE_XMX
  3598. // mmvq path is faster in the CUDA backend.
  3599. if (ctx.stream()->get_backend() == sycl::backend::ext_oneapi_cuda)
  3600. use_dequantize_mul_mat_vec = use_dequantize_mul_mat_vec && !use_mul_mat_vec_q;
  3601. if (!split && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  3602. // KQ single-batch
  3603. ggml_sycl_mul_mat_vec_p021(ctx, src0, src1, dst);
  3604. } else if (!split && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  3605. // KQV single-batch
  3606. ggml_sycl_mul_mat_vec_nc(ctx, src0, src1, dst);
  3607. } else if (!split && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
  3608. // KQ + KQV multi-batch
  3609. ggml_sycl_mul_mat_batched_sycl(ctx, src0, src1, dst);
  3610. } else if (use_dequantize_mul_mat_vec) {
  3611. ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  3612. } else if (use_mul_mat_vec_q) {
  3613. ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  3614. } else if (use_mul_mat_q) {
  3615. ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  3616. } else {
  3617. ggml_sycl_op_mul_mat(ctx, src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  3618. }
  3619. }
  3620. struct mmid_row_mapping {
  3621. int32_t i1;
  3622. int32_t i2;
  3623. };
  3624. __dpct_inline__ static void k_copy_src1_to_contiguous(
  3625. const char *__restrict__ src1_original, char *__restrict__ src1_contiguous,
  3626. int *__restrict__ cur_src1_row, mmid_row_mapping *__restrict__ row_mapping,
  3627. const char *__restrict ids, int64_t i02, size_t ids_nb1, size_t ids_nb0,
  3628. int64_t ne11, int64_t ne10, size_t nb11, size_t nb12,
  3629. const sycl::nd_item<3> &item_ct1, int &src1_row) {
  3630. int32_t iid1 = item_ct1.get_group(2);
  3631. int32_t id = item_ct1.get_group(1);
  3632. const int32_t row_id_i = *(const int32_t *) (ids + iid1*ids_nb1 + id*ids_nb0);
  3633. if (row_id_i != i02) {
  3634. return;
  3635. }
  3636. const int64_t i11 = id % ne11;
  3637. const int64_t i12 = iid1;
  3638. if (item_ct1.get_local_id(2) == 0) {
  3639. src1_row =
  3640. dpct::atomic_fetch_add<sycl::access::address_space::generic_space>(
  3641. cur_src1_row, 1);
  3642. row_mapping[src1_row] = {id, iid1};
  3643. }
  3644. /*
  3645. DPCT1065:194: Consider replacing sycl::nd_item::barrier() with
  3646. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
  3647. performance if there is no access to global memory.
  3648. */
  3649. item_ct1.barrier();
  3650. const float * src1_row_original = (const float *)(src1_original + i11*nb11 + i12*nb12);
  3651. float * src1_row_contiguous = (float *)(src1_contiguous + src1_row*nb11);
  3652. #pragma unroll
  3653. for (int i = item_ct1.get_local_id(2); i < ne10;
  3654. i += item_ct1.get_local_range(2)) {
  3655. src1_row_contiguous[i] = src1_row_original[i];
  3656. }
  3657. }
  3658. __dpct_inline__ static void k_copy_dst_from_contiguous(
  3659. char *__restrict__ dst_original, const char *__restrict__ dst_contiguous,
  3660. const mmid_row_mapping *__restrict__ row_mapping, int64_t ne0, size_t nb1,
  3661. size_t nb2, const sycl::nd_item<3> &item_ct1) {
  3662. int32_t i = item_ct1.get_group(2);
  3663. const int32_t i1 = row_mapping[i].i1;
  3664. const int32_t i2 = row_mapping[i].i2;
  3665. const float * dst_row_contiguous = (const float *)(dst_contiguous + i*nb1);
  3666. float * dst_row_original = (float *)(dst_original + i1*nb1 + i2*nb2);
  3667. #pragma unroll
  3668. for (int j = item_ct1.get_local_id(2); j < ne0;
  3669. j += item_ct1.get_local_range(2)) {
  3670. dst_row_original[j] = dst_row_contiguous[j];
  3671. }
  3672. }
  3673. static void ggml_sycl_mul_mat_id(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  3674. const ggml_tensor *src1,
  3675. ggml_tensor *dst) try {
  3676. GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(src0->buffer) && "mul_mat_id does not support split buffers");
  3677. const ggml_tensor *ids = dst->src[2];
  3678. GGML_TENSOR_BINARY_OP_LOCALS
  3679. const queue_ptr stream = ctx.stream();
  3680. const int64_t n_as = ne02;
  3681. const int64_t n_ids = ids->ne[0];
  3682. std::vector<char> ids_host(ggml_nbytes(ids));
  3683. const char * ids_dev = (const char *) ids->data;
  3684. SYCL_CHECK(CHECK_TRY_ERROR(
  3685. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
  3686. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  3687. ggml_tensor src0_row = *src0;
  3688. ggml_tensor src1_row = *src1;
  3689. ggml_tensor dst_row = *dst;
  3690. char *src0_original = (char *)src0->data;
  3691. char *src1_original = (char *)src1->data;
  3692. char *dst_original = (char *)dst->data;
  3693. src0_row.ne[2] = 1;
  3694. src0_row.ne[3] = 1;
  3695. src0_row.nb[3] = nb02;
  3696. src1_row.ne[1] = 1;
  3697. src1_row.ne[2] = 1;
  3698. src1_row.ne[3] = 1;
  3699. src1_row.nb[2] = nb11;
  3700. src1_row.nb[3] = nb11;
  3701. dst_row.ne[1] = 1;
  3702. dst_row.ne[2] = 1;
  3703. dst_row.ne[3] = 1;
  3704. dst_row.nb[2] = nb1;
  3705. dst_row.nb[3] = nb1;
  3706. if (ne12 == 1) {
  3707. for (int64_t iid1 = 0; iid1 < ids->ne[1]; iid1++) {
  3708. for (int64_t id = 0; id < n_ids; id++) {
  3709. const int32_t i02 = *(const int32_t *) (ids_host.data() + iid1*ids->nb[1] + id*ids->nb[0]);
  3710. GGML_ASSERT(i02 >= 0 && i02 < n_as);
  3711. const int64_t i11 = id % ne11;
  3712. const int64_t i12 = iid1;
  3713. const int64_t i1 = id;
  3714. const int64_t i2 = i12;
  3715. src0_row.data = src0_original + i02*nb02;
  3716. src1_row.data = src1_original + + i11*nb11 + i12*nb12;
  3717. dst_row.data = dst_original + i1*nb1 + i2*nb2;
  3718. ggml_sycl_mul_mat(ctx, &src0_row, &src1_row, &dst_row);
  3719. }
  3720. }
  3721. } else {
  3722. ggml_sycl_pool_alloc<char> src1_contiguous(ctx.pool(), sizeof(float)*ggml_nelements(src1));
  3723. ggml_sycl_pool_alloc<char> dst_contiguous(ctx.pool(), sizeof(float)*ggml_nelements(dst));
  3724. src1_row.data = src1_contiguous.get();
  3725. dst_row.data = dst_contiguous.get();
  3726. for (int64_t i02 = 0; i02 < n_as; i02++) {
  3727. int64_t num_src1_rows = 0;
  3728. for (int64_t iid1 = 0; iid1 < ids->ne[1]; iid1++) {
  3729. for (int64_t id = 0; id < n_ids; id++) {
  3730. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + iid1*ids->nb[1] + id*ids->nb[0]);
  3731. GGML_ASSERT(row_id_i >= 0 && row_id_i < n_as);
  3732. if (row_id_i != i02) {
  3733. continue;
  3734. }
  3735. num_src1_rows++;
  3736. }
  3737. }
  3738. if (num_src1_rows == 0) {
  3739. continue;
  3740. }
  3741. ggml_sycl_pool_alloc<int> dev_cur_src1_row(ctx.pool(), 1);
  3742. ggml_sycl_pool_alloc<mmid_row_mapping> dev_row_mapping(ctx.pool(), num_src1_rows);
  3743. SYCL_CHECK(CHECK_TRY_ERROR(
  3744. stream->memset(dev_cur_src1_row.get(), 0, sizeof(int))));
  3745. {
  3746. sycl::range<3> block_dims(1, 1, std::min((unsigned int)ne10, 768u));
  3747. sycl::range<3> grid_dims(1, n_ids, ids->ne[1]);
  3748. stream->submit([&](sycl::handler &cgh) {
  3749. sycl::local_accessor<int, 0> src1_row_acc(cgh);
  3750. char *__restrict src1_contiguous_get =
  3751. src1_contiguous.get();
  3752. int *__restrict dev_cur_src1_row_get =
  3753. dev_cur_src1_row.get();
  3754. mmid_row_mapping *__restrict dev_row_mapping_get =
  3755. dev_row_mapping.get();
  3756. size_t ids_nb_ct6 = ids->nb[1];
  3757. size_t ids_nb_ct7 = ids->nb[0];
  3758. cgh.parallel_for(
  3759. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  3760. [=](sycl::nd_item<3> item_ct1) {
  3761. k_copy_src1_to_contiguous(
  3762. src1_original, src1_contiguous_get,
  3763. dev_cur_src1_row_get,
  3764. dev_row_mapping_get, ids_dev, i02,
  3765. ids_nb_ct6, ids_nb_ct7, ne11, ne10, nb11, nb12,
  3766. item_ct1, src1_row_acc);
  3767. });
  3768. });
  3769. }
  3770. src0_row.data = src0_original + i02*nb02;
  3771. GGML_ASSERT(nb11 == sizeof(float)*ne10);
  3772. GGML_ASSERT(nb1 == sizeof(float)*ne0);
  3773. src1_row.ne[1] = num_src1_rows;
  3774. src1_row.nb[1] = nb11;
  3775. src1_row.nb[2] = num_src1_rows*nb11;
  3776. src1_row.nb[3] = num_src1_rows*nb11;
  3777. dst_row.ne[1] = num_src1_rows;
  3778. dst_row.nb[1] = nb1;
  3779. dst_row.nb[2] = num_src1_rows*nb1;
  3780. dst_row.nb[3] = num_src1_rows*nb1;
  3781. ggml_sycl_mul_mat(ctx, &src0_row, &src1_row, &dst_row);
  3782. {
  3783. sycl::range<3> block_dims(1, 1, std::min((unsigned int)ne0, 768u));
  3784. sycl::range<3> grid_dims(1, 1, num_src1_rows);
  3785. stream->submit([&](sycl::handler &cgh) {
  3786. const char *__restrict dst_contiguous_get =
  3787. dst_contiguous.get();
  3788. const mmid_row_mapping *__restrict dev_row_mapping_get =
  3789. dev_row_mapping.get();
  3790. cgh.parallel_for(
  3791. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  3792. [=](sycl::nd_item<3> item_ct1) {
  3793. k_copy_dst_from_contiguous(dst_original,
  3794. dst_contiguous_get,
  3795. dev_row_mapping_get,
  3796. ne0, nb1, nb2, item_ct1);
  3797. });
  3798. });
  3799. }
  3800. }
  3801. }
  3802. }
  3803. catch (sycl::exception const &exc) {
  3804. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3805. << ", line:" << __LINE__ << std::endl;
  3806. std::exit(1);
  3807. }
  3808. static void ggml_sycl_scale(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3809. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_scale);
  3810. }
  3811. static void ggml_sycl_clamp(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3812. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_clamp);
  3813. }
  3814. static void ggml_sycl_cpy(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
  3815. ggml_tensor *dst) try {
  3816. const int64_t ne = ggml_nelements(src0);
  3817. GGML_ASSERT(ne == ggml_nelements(src1));
  3818. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  3819. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  3820. GGML_TENSOR_BINARY_OP_LOCALS01;
  3821. SYCL_CHECK(ggml_sycl_set_device(ctx.device));
  3822. queue_ptr main_stream = ctx.stream();
  3823. char * src0_ddc = (char *) src0->data;
  3824. char * src1_ddc = (char *) src1->data;
  3825. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  3826. ggml_cpy_f32_f32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3827. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  3828. ggml_cpy_f32_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3829. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  3830. ggml_cpy_f32_q8_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3831. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  3832. ggml_cpy_f32_q4_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3833. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  3834. ggml_cpy_f32_q4_1_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3835. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  3836. ggml_cpy_f16_f32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3837. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  3838. ggml_cpy_f16_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3839. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  3840. ggml_cpy_i16_i16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3841. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  3842. ggml_cpy_i32_i32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  3843. } else {
  3844. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  3845. ggml_type_name(src0->type), ggml_type_name(src1->type));
  3846. GGML_ABORT("fatal error");
  3847. }
  3848. (void) dst;
  3849. }
  3850. catch (sycl::exception const &exc) {
  3851. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3852. << ", line:" << __LINE__ << std::endl;
  3853. std::exit(1);
  3854. }
  3855. static void ggml_sycl_dup(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3856. // TODO: why do we pass dst as src1 here?
  3857. ggml_sycl_cpy(ctx, src0, dst, nullptr);
  3858. (void) src1;
  3859. }
  3860. static void ggml_sycl_diag_mask_inf(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3861. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  3862. }
  3863. static void ggml_sycl_soft_max(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3864. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_soft_max);
  3865. }
  3866. static void ggml_sycl_rope(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3867. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  3868. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_rope);
  3869. }
  3870. static void ggml_sycl_pool2d(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3871. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_pool2d);
  3872. }
  3873. static void ggml_sycl_im2col(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3874. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_im2col);
  3875. }
  3876. static void ggml_sycl_sum_rows(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3877. GGML_ASSERT(ggml_is_contiguous(src0));
  3878. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_sum_rows);
  3879. }
  3880. static void ggml_sycl_argsort(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3881. GGML_ASSERT(ggml_is_contiguous(src0));
  3882. ggml_sycl_op_flatten(ctx, src0, src1, dst, ggml_sycl_op_argsort);
  3883. }
  3884. static void ggml_sycl_nop(ggml_backend_sycl_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  3885. (void) src0;
  3886. (void) src1;
  3887. (void) dst;
  3888. }
  3889. void ggml_sycl_set_main_device(const int main_device) try {
  3890. if (dpct::get_current_device_id() == main_device) return;
  3891. check_allow_gpu_index(main_device);
  3892. dpct::select_device(main_device);
  3893. if (g_ggml_sycl_debug) {
  3894. dpct::device_info prop;
  3895. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  3896. prop, dpct::dev_mgr::instance().get_device(main_device))));
  3897. fprintf(stderr, "Using device %d (%s) as main device\n",
  3898. main_device, prop.get_name());
  3899. }
  3900. }
  3901. catch (sycl::exception const &exc) {
  3902. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3903. << ", line:" << __LINE__ << std::endl;
  3904. std::exit(1);
  3905. }
  3906. bool ggml_sycl_compute_forward(ggml_backend_sycl_context & ctx, struct ggml_tensor * tensor) {
  3907. if (!g_sycl_loaded) return false;
  3908. ggml_sycl_func_t func;
  3909. switch (tensor->op) {
  3910. case GGML_OP_CONV_TRANSPOSE_1D:
  3911. func = ggml_sycl_op_conv_transpose_1d;
  3912. break;
  3913. case GGML_OP_REPEAT:
  3914. func = ggml_sycl_repeat;
  3915. break;
  3916. case GGML_OP_GET_ROWS:
  3917. func = ggml_sycl_get_rows;
  3918. break;
  3919. case GGML_OP_DUP:
  3920. func = ggml_sycl_dup;
  3921. break;
  3922. case GGML_OP_ADD:
  3923. func = ggml_sycl_add;
  3924. break;
  3925. case GGML_OP_ACC:
  3926. func = ggml_sycl_acc;
  3927. break;
  3928. case GGML_OP_MUL:
  3929. func = ggml_sycl_mul;
  3930. break;
  3931. case GGML_OP_DIV:
  3932. func = ggml_sycl_div;
  3933. break;
  3934. case GGML_OP_UNARY:
  3935. switch (ggml_get_unary_op(tensor)) {
  3936. case GGML_UNARY_OP_GELU:
  3937. func = ggml_sycl_gelu;
  3938. break;
  3939. case GGML_UNARY_OP_SILU:
  3940. func = ggml_sycl_silu;
  3941. break;
  3942. case GGML_UNARY_OP_GELU_QUICK:
  3943. func = ggml_sycl_gelu_quick;
  3944. break;
  3945. case GGML_UNARY_OP_TANH:
  3946. func = ggml_sycl_tanh;
  3947. break;
  3948. case GGML_UNARY_OP_RELU:
  3949. func = ggml_sycl_relu;
  3950. break;
  3951. case GGML_UNARY_OP_HARDSIGMOID:
  3952. func = ggml_sycl_hardsigmoid;
  3953. break;
  3954. case GGML_UNARY_OP_HARDSWISH:
  3955. func = ggml_sycl_hardswish;
  3956. break;
  3957. default:
  3958. return false;
  3959. }
  3960. break;
  3961. case GGML_OP_NORM:
  3962. func = ggml_sycl_norm;
  3963. break;
  3964. case GGML_OP_GROUP_NORM:
  3965. func = ggml_sycl_group_norm;
  3966. break;
  3967. case GGML_OP_CONCAT:
  3968. func = ggml_sycl_op_concat;
  3969. break;
  3970. case GGML_OP_UPSCALE:
  3971. func = ggml_sycl_upscale;
  3972. break;
  3973. case GGML_OP_PAD:
  3974. func = ggml_sycl_pad;
  3975. break;
  3976. case GGML_OP_LEAKY_RELU:
  3977. func = ggml_sycl_leaky_relu;
  3978. break;
  3979. case GGML_OP_RMS_NORM:
  3980. func = ggml_sycl_rms_norm;
  3981. break;
  3982. case GGML_OP_MUL_MAT:
  3983. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  3984. return false;
  3985. }
  3986. func = ggml_sycl_mul_mat;
  3987. break;
  3988. case GGML_OP_MUL_MAT_ID:
  3989. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  3990. return false;
  3991. }
  3992. func = ggml_sycl_mul_mat_id;
  3993. break;
  3994. case GGML_OP_SCALE:
  3995. func = ggml_sycl_scale;
  3996. break;
  3997. case GGML_OP_SQR:
  3998. func = ggml_sycl_sqr;
  3999. break;
  4000. case GGML_OP_CLAMP:
  4001. func = ggml_sycl_clamp;
  4002. break;
  4003. case GGML_OP_CPY:
  4004. func = ggml_sycl_cpy;
  4005. break;
  4006. case GGML_OP_CONT:
  4007. func = ggml_sycl_dup;
  4008. break;
  4009. case GGML_OP_NONE:
  4010. case GGML_OP_RESHAPE:
  4011. case GGML_OP_VIEW:
  4012. case GGML_OP_PERMUTE:
  4013. case GGML_OP_TRANSPOSE:
  4014. func = ggml_sycl_nop;
  4015. break;
  4016. case GGML_OP_DIAG_MASK_INF:
  4017. func = ggml_sycl_diag_mask_inf;
  4018. break;
  4019. case GGML_OP_SOFT_MAX:
  4020. func = ggml_sycl_soft_max;
  4021. break;
  4022. case GGML_OP_ROPE:
  4023. func = ggml_sycl_rope;
  4024. break;
  4025. case GGML_OP_IM2COL:
  4026. func = ggml_sycl_im2col;
  4027. break;
  4028. case GGML_OP_POOL_2D:
  4029. func = ggml_sycl_pool2d;
  4030. break;
  4031. case GGML_OP_SUM_ROWS:
  4032. func = ggml_sycl_sum_rows;
  4033. break;
  4034. case GGML_OP_ARGSORT:
  4035. func = ggml_sycl_argsort;
  4036. break;
  4037. case GGML_OP_TIMESTEP_EMBEDDING:
  4038. func = ggml_sycl_op_timestep_embedding;
  4039. break;
  4040. default:
  4041. return false;
  4042. }
  4043. if (tensor->src[0] != nullptr && ggml_backend_buffer_is_sycl_split(tensor->src[0]->buffer)) {
  4044. ggml_sycl_set_peer_access(tensor->src[1]->ne[1], ctx.device);
  4045. }
  4046. func(ctx, tensor->src[0], tensor->src[1], tensor);
  4047. return true;
  4048. }
  4049. GGML_API void ggml_backend_sycl_get_device_description(int device, char *description,
  4050. size_t description_size) try {
  4051. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_description\n");
  4052. dpct::device_info prop;
  4053. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  4054. prop, dpct::dev_mgr::instance().get_device(device))));
  4055. snprintf(description, description_size, "%s", prop.get_name());
  4056. }
  4057. catch (sycl::exception const &exc) {
  4058. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4059. << ", line:" << __LINE__ << std::endl;
  4060. std::exit(1);
  4061. }
  4062. void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  4063. size_t *total) try {
  4064. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_memory\n");
  4065. ggml_sycl_set_device(device);
  4066. /*
  4067. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  4068. error codes. The original code was commented out and a warning string was
  4069. inserted. You need to rewrite this code.
  4070. */
  4071. /*
  4072. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  4073. device information which may not be supported by all compilers or runtimes.
  4074. You may need to adjust the code.
  4075. */
  4076. SYCL_CHECK(CHECK_TRY_ERROR(
  4077. dpct::dev_mgr::instance().get_device(device).get_memory_info(*free, *total)));
  4078. }
  4079. catch (sycl::exception const &exc) {
  4080. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4081. << ", line:" << __LINE__ << std::endl;
  4082. std::exit(1);
  4083. }
  4084. ////////////////////////////////////////////////////////////////////////////////
  4085. // backend
  4086. static const char * ggml_backend_sycl_get_name(ggml_backend_t backend) {
  4087. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4088. return sycl_ctx->name.c_str();
  4089. }
  4090. static void ggml_backend_sycl_free(ggml_backend_t backend) {
  4091. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4092. delete sycl_ctx;
  4093. delete backend;
  4094. }
  4095. static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  4096. ggml_tensor *tensor,
  4097. const void *data, size_t offset,
  4098. size_t size) try {
  4099. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4100. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  4101. GGML_ASSERT(buf->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  4102. const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4103. SYCL_CHECK(CHECK_TRY_ERROR(
  4104. (stream)->memcpy((char *)tensor->data + offset, data, size)));
  4105. }
  4106. catch (sycl::exception const &exc) {
  4107. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4108. << ", line:" << __LINE__ << std::endl;
  4109. std::exit(1);
  4110. }
  4111. static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  4112. const ggml_tensor *tensor,
  4113. void *data, size_t offset,
  4114. size_t size) try {
  4115. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4116. ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
  4117. GGML_ASSERT(buf->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  4118. const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4119. SYCL_CHECK(CHECK_TRY_ERROR((stream)->memcpy(
  4120. data, (const char *)tensor->data + offset, size).wait()));
  4121. }
  4122. catch (sycl::exception const &exc) {
  4123. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4124. << ", line:" << __LINE__ << std::endl;
  4125. std::exit(1);
  4126. }
  4127. static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  4128. const ggml_tensor *src,
  4129. ggml_tensor *dst) try {
  4130. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4131. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  4132. /*
  4133. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  4134. error codes. The original code was commented out and a warning string
  4135. was inserted. You need to rewrite this code.
  4136. */
  4137. const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4138. SYCL_CHECK(CHECK_TRY_ERROR((stream)->memcpy(
  4139. dst->data, src->data, ggml_nbytes(dst)).wait()));
  4140. return true;
  4141. }
  4142. return false;
  4143. }
  4144. catch (sycl::exception const &exc) {
  4145. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4146. << ", line:" << __LINE__ << std::endl;
  4147. std::exit(1);
  4148. }
  4149. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  4150. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4151. const queue_ptr stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4152. SYCL_CHECK(CHECK_TRY_ERROR((stream)->wait()));
  4153. GGML_UNUSED(backend);
  4154. }
  4155. catch (sycl::exception const &exc) {
  4156. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4157. << ", line:" << __LINE__ << std::endl;
  4158. std::exit(1);
  4159. }
  4160. static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  4161. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  4162. ggml_sycl_set_main_device(sycl_ctx->device);
  4163. for (int i = 0; i < cgraph->n_nodes; i++) {
  4164. ggml_tensor * node = cgraph->nodes[i];
  4165. if (ggml_is_empty(node) || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
  4166. continue;
  4167. }
  4168. #ifndef NDEBUG
  4169. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  4170. for (int j = 0; j < GGML_MAX_SRC; j++) {
  4171. if (node->src[j] != nullptr) {
  4172. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  4173. }
  4174. }
  4175. #endif
  4176. bool ok = ggml_sycl_compute_forward(*sycl_ctx, node);
  4177. if (!ok) {
  4178. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  4179. }
  4180. GGML_ASSERT(ok);
  4181. }
  4182. return GGML_STATUS_SUCCESS;
  4183. }
  4184. static void ggml_backend_sycl_event_record(ggml_backend_t backend, ggml_backend_event_t event)
  4185. try
  4186. {
  4187. ggml_backend_sycl_context *sycl_ctx =
  4188. (ggml_backend_sycl_context *)backend->context;
  4189. sycl::event *sycl_event = static_cast<sycl::event *>(event->context);
  4190. const queue_ptr &stream = sycl_ctx->stream(sycl_ctx->device, 0);
  4191. // Record the current state of the queue
  4192. SYCL_CHECK(CHECK_TRY_ERROR(*sycl_event = stream->ext_oneapi_submit_barrier()));
  4193. }
  4194. catch (sycl::exception const &exc)
  4195. {
  4196. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4197. << ", line:" << __LINE__ << std::endl;
  4198. std::exit(1);
  4199. }
  4200. static void ggml_backend_sycl_event_wait(ggml_backend_t backend, ggml_backend_event_t event) try {
  4201. ggml_backend_sycl_context* sycl_ctx = static_cast<ggml_backend_sycl_context*>(backend->context);
  4202. sycl::event* sycl_event = static_cast<sycl::event*>(event->context);
  4203. if (ggml_backend_is_sycl(backend)) {
  4204. SYCL_CHECK(CHECK_TRY_ERROR(sycl_event->wait()));
  4205. } else
  4206. GGML_ABORT("fatal error");
  4207. } catch (sycl::exception const& exc) {
  4208. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4209. << ", line:" << __LINE__ << std::endl;
  4210. std::exit(1);
  4211. }
  4212. static ggml_backend_i ggml_backend_sycl_interface = {
  4213. /* .get_name = */ ggml_backend_sycl_get_name,
  4214. /* .free = */ ggml_backend_sycl_free,
  4215. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  4216. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  4217. /* .cpy_tensor_async = */ NULL, // ggml_backend_sycl_cpy_tensor_async,
  4218. // // TODO: update for the new
  4219. // interface
  4220. /* .synchronize = */ ggml_backend_sycl_synchronize,
  4221. /* .graph_plan_create = */ NULL,
  4222. /* .graph_plan_free = */ NULL,
  4223. /* .graph_plan_update = */ NULL,
  4224. /* .graph_plan_compute = */ NULL,
  4225. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  4226. /* .event_record = */ ggml_backend_sycl_event_record,
  4227. /* .event_wait = */ ggml_backend_sycl_event_wait,
  4228. };
  4229. static ggml_guid_t ggml_backend_sycl_guid() {
  4230. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  4231. return &guid;
  4232. }
  4233. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  4234. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  4235. }
  4236. int ggml_backend_sycl_get_device_count() {
  4237. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_count\n");
  4238. return ggml_sycl_info().device_count;
  4239. }
  4240. // backend device
  4241. struct ggml_backend_sycl_device_context {
  4242. int device;
  4243. std::string name;
  4244. std::string description;
  4245. };
  4246. static const char * ggml_backend_sycl_device_get_name(ggml_backend_dev_t dev) {
  4247. ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
  4248. return ctx->name.c_str();
  4249. }
  4250. static const char * ggml_backend_sycl_device_get_description(ggml_backend_dev_t dev) {
  4251. ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
  4252. return ctx->description.c_str();
  4253. }
  4254. static void ggml_backend_sycl_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
  4255. ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
  4256. ggml_sycl_set_device(ctx->device);
  4257. SYCL_CHECK(CHECK_TRY_ERROR(
  4258. dpct::dev_mgr::instance().get_device(ctx->device).get_memory_info(*free, *total)));
  4259. }
  4260. static enum ggml_backend_dev_type ggml_backend_sycl_device_get_type(ggml_backend_dev_t dev) {
  4261. GGML_UNUSED(dev);
  4262. return GGML_BACKEND_DEVICE_TYPE_GPU;
  4263. }
  4264. static void ggml_backend_sycl_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) {
  4265. props->name = ggml_backend_sycl_device_get_name(dev);
  4266. props->description = ggml_backend_sycl_device_get_description(dev);
  4267. props->type = ggml_backend_sycl_device_get_type(dev);
  4268. ggml_backend_sycl_device_get_memory(dev, &props->memory_free, &props->memory_total);
  4269. bool host_buffer = getenv("GGML_SYCL_NO_PINNED") == nullptr;
  4270. #ifdef GGML_SYCL_NO_PEER_COPY
  4271. bool events = false;
  4272. #else
  4273. bool events = true;
  4274. #endif
  4275. props->caps = {
  4276. /* .async = */ true,
  4277. /* .host_buffer = */ host_buffer,
  4278. /* .buffer_from_host_ptr = */ false,
  4279. /* .events = */ events,
  4280. };
  4281. }
  4282. static ggml_backend_t ggml_backend_sycl_device_init(ggml_backend_dev_t dev, const char * params) {
  4283. GGML_UNUSED(params);
  4284. ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
  4285. return ggml_backend_sycl_init(ctx->device);
  4286. }
  4287. static ggml_backend_buffer_type_t ggml_backend_sycl_device_get_buffer_type(ggml_backend_dev_t dev) {
  4288. ggml_backend_sycl_device_context * ctx = (ggml_backend_sycl_device_context *)dev->context;
  4289. return ggml_backend_sycl_buffer_type(ctx->device);
  4290. }
  4291. static ggml_backend_buffer_type_t ggml_backend_sycl_device_get_host_buffer_type(ggml_backend_dev_t dev) {
  4292. GGML_UNUSED(dev);
  4293. return ggml_backend_sycl_host_buffer_type();
  4294. }
  4295. static ggml_backend_buffer_t ggml_backend_sycl_device_buffer_from_host_ptr(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) {
  4296. GGML_UNUSED(dev);
  4297. GGML_UNUSED(ptr);
  4298. GGML_UNUSED(size);
  4299. GGML_UNUSED(max_tensor_size);
  4300. return nullptr;
  4301. }
  4302. static bool ggml_backend_sycl_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  4303. switch (op->op) {
  4304. case GGML_OP_CONV_TRANSPOSE_1D:
  4305. {
  4306. ggml_type src0_type = op->src[0]->type;
  4307. ggml_type src1_type = op->src[1]->type;
  4308. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4309. return true;
  4310. }
  4311. return false;
  4312. } break;
  4313. case GGML_OP_UNARY:
  4314. switch (ggml_get_unary_op(op)) {
  4315. case GGML_UNARY_OP_GELU:
  4316. case GGML_UNARY_OP_SILU:
  4317. case GGML_UNARY_OP_RELU:
  4318. case GGML_UNARY_OP_HARDSIGMOID:
  4319. case GGML_UNARY_OP_HARDSWISH:
  4320. case GGML_UNARY_OP_GELU_QUICK:
  4321. case GGML_UNARY_OP_TANH:
  4322. return ggml_is_contiguous(op->src[0]);
  4323. default:
  4324. return false;
  4325. }
  4326. break;
  4327. case GGML_OP_MUL_MAT:
  4328. case GGML_OP_MUL_MAT_ID:
  4329. {
  4330. struct ggml_tensor * a;
  4331. struct ggml_tensor * b;
  4332. if (op->op == GGML_OP_MUL_MAT) {
  4333. a = op->src[0];
  4334. b = op->src[1];
  4335. } else {
  4336. a = op->src[2];
  4337. b = op->src[1];
  4338. }
  4339. if (a->ne[3] != b->ne[3]) {
  4340. return false;
  4341. }
  4342. ggml_type a_type = a->type;
  4343. if (a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ4_XS ||
  4344. a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ3_S ||
  4345. a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ2_S ||
  4346. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ1_M
  4347. ) {
  4348. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  4349. return false;
  4350. }
  4351. }
  4352. ggml_type src0_type = op->src[0]->type;
  4353. if (src0_type == GGML_TYPE_BF16) {
  4354. return false;
  4355. }
  4356. return true;
  4357. } break;
  4358. case GGML_OP_GET_ROWS:
  4359. {
  4360. switch (op->src[0]->type) {
  4361. case GGML_TYPE_F16:
  4362. case GGML_TYPE_F32:
  4363. case GGML_TYPE_Q4_0:
  4364. case GGML_TYPE_Q4_1:
  4365. case GGML_TYPE_Q5_0:
  4366. case GGML_TYPE_Q5_1:
  4367. case GGML_TYPE_Q8_0:
  4368. return true;
  4369. default:
  4370. return false;
  4371. }
  4372. } break;
  4373. case GGML_OP_CPY:
  4374. {
  4375. ggml_type src0_type = op->src[0]->type;
  4376. ggml_type src1_type = op->src[1]->type;
  4377. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  4378. return true;
  4379. }
  4380. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  4381. return true;
  4382. }
  4383. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  4384. return true;
  4385. }
  4386. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  4387. return true;
  4388. }
  4389. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  4390. return true;
  4391. }
  4392. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  4393. return true;
  4394. }
  4395. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  4396. return true;
  4397. }
  4398. return false;
  4399. } break;
  4400. case GGML_OP_CONCAT:
  4401. {
  4402. ggml_type src0_type = op->src[0]->type;
  4403. int dim = op->op_params[0];
  4404. return ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]) && src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16 && dim == 2;
  4405. } break;
  4406. case GGML_OP_DUP:
  4407. case GGML_OP_NONE:
  4408. case GGML_OP_RESHAPE:
  4409. case GGML_OP_REPEAT:
  4410. case GGML_OP_VIEW:
  4411. case GGML_OP_PERMUTE:
  4412. case GGML_OP_TRANSPOSE:
  4413. case GGML_OP_NORM:
  4414. case GGML_OP_ADD:
  4415. case GGML_OP_MUL:
  4416. case GGML_OP_DIV:
  4417. case GGML_OP_RMS_NORM:
  4418. case GGML_OP_SCALE:
  4419. case GGML_OP_SQR:
  4420. case GGML_OP_CLAMP:
  4421. return true;
  4422. case GGML_OP_CONT:
  4423. return op->src[0]->type != GGML_TYPE_BF16;
  4424. case GGML_OP_DIAG_MASK_INF:
  4425. case GGML_OP_SOFT_MAX:
  4426. return true;
  4427. case GGML_OP_ROPE:
  4428. return ggml_is_contiguous(op->src[0]);
  4429. case GGML_OP_IM2COL:
  4430. // TODO: add support for the new F32 operations
  4431. return op->src[0]->type == GGML_TYPE_F16;
  4432. case GGML_OP_POOL_2D:
  4433. case GGML_OP_SUM_ROWS:
  4434. case GGML_OP_ARGSORT:
  4435. case GGML_OP_ACC:
  4436. case GGML_OP_GROUP_NORM:
  4437. case GGML_OP_UPSCALE:
  4438. case GGML_OP_PAD:
  4439. case GGML_OP_LEAKY_RELU:
  4440. case GGML_OP_TIMESTEP_EMBEDDING:
  4441. return true;
  4442. default:
  4443. return false;
  4444. }
  4445. GGML_UNUSED(dev);
  4446. }
  4447. static bool ggml_backend_sycl_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  4448. if (buft->iface.get_name != ggml_backend_sycl_buffer_type_get_name) {
  4449. return false;
  4450. }
  4451. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  4452. ggml_backend_sycl_device_context * sycl_ctx = (ggml_backend_sycl_device_context *)dev->context;
  4453. return buft_ctx->device == sycl_ctx->device;
  4454. }
  4455. static bool ggml_backend_sycl_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
  4456. const int min_batch_size = 32;
  4457. return op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS && op->op != GGML_OP_MUL_MAT_ID;
  4458. GGML_UNUSED(dev);
  4459. }
  4460. static ggml_backend_event_t
  4461. ggml_backend_sycl_device_event_new(ggml_backend_dev_t dev) {
  4462. #ifdef GGML_SYCL_NO_PEER_COPY
  4463. return nullptr;
  4464. #else
  4465. sycl::event *event_ptr = new sycl::event();
  4466. return new ggml_backend_event{
  4467. /* .device = */ dev,
  4468. /* .context = */ event_ptr,
  4469. };
  4470. #endif
  4471. }
  4472. static void ggml_backend_sycl_device_event_free(ggml_backend_dev_t dev, ggml_backend_event_t event) try {
  4473. GGML_UNUSED(dev);
  4474. if (event == nullptr) {
  4475. return;
  4476. }
  4477. if (event->context != nullptr) {
  4478. sycl::event *sycl_event = static_cast<sycl::event *>(event->context);
  4479. delete sycl_event;
  4480. event->context = nullptr;
  4481. }
  4482. delete event;
  4483. } catch (sycl::exception const &exc) {
  4484. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4485. << ", line:" << __LINE__ << std::endl;
  4486. std::exit(1);
  4487. }
  4488. static void ggml_backend_sycl_device_event_synchronize(ggml_backend_dev_t dev, ggml_backend_event_t event) try {
  4489. GGML_UNUSED(dev);
  4490. sycl::event *sycl_event = static_cast<sycl::event *>(event->context);
  4491. SYCL_CHECK(CHECK_TRY_ERROR(sycl_event->wait()));
  4492. } catch (sycl::exception const &exc) {
  4493. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  4494. << ", line:" << __LINE__ << std::endl;
  4495. std::exit(1);
  4496. }
  4497. static const ggml_backend_device_i ggml_backend_sycl_device_interface = {
  4498. /* .get_name = */ ggml_backend_sycl_device_get_name,
  4499. /* .get_description = */ ggml_backend_sycl_device_get_description,
  4500. /* .get_memory = */ ggml_backend_sycl_device_get_memory,
  4501. /* .get_type = */ ggml_backend_sycl_device_get_type,
  4502. /* .get_props = */ ggml_backend_sycl_device_get_props,
  4503. /* .init_backend = */ ggml_backend_sycl_device_init,
  4504. /* .get_buffer_type = */ ggml_backend_sycl_device_get_buffer_type,
  4505. /* .get_host_buffer_type = */ ggml_backend_sycl_device_get_host_buffer_type,
  4506. /* .buffer_from_host_ptr = */ ggml_backend_sycl_device_buffer_from_host_ptr,
  4507. /* .supports_op = */ ggml_backend_sycl_device_supports_op,
  4508. /* .supports_buft = */ ggml_backend_sycl_device_supports_buft,
  4509. /* .offload_op = */ ggml_backend_sycl_device_offload_op,
  4510. /* .event_new = */ ggml_backend_sycl_device_event_new,
  4511. /* .event_free = */ ggml_backend_sycl_device_event_free,
  4512. /* .event_synchronize = */ ggml_backend_sycl_device_event_synchronize,
  4513. };
  4514. // backend reg
  4515. struct ggml_backend_sycl_reg_context {
  4516. std::vector<ggml_backend_dev_t> devices;
  4517. };
  4518. static const char * ggml_backend_sycl_reg_get_name(ggml_backend_reg_t reg) {
  4519. GGML_UNUSED(reg);
  4520. return GGML_SYCL_NAME;
  4521. }
  4522. static size_t ggml_backend_sycl_reg_get_device_count(ggml_backend_reg_t reg) {
  4523. ggml_backend_sycl_reg_context * ctx = (ggml_backend_sycl_reg_context *)reg->context;
  4524. return ctx->devices.size();
  4525. }
  4526. static ggml_backend_dev_t ggml_backend_sycl_reg_get_device(ggml_backend_reg_t reg, size_t index) {
  4527. ggml_backend_sycl_reg_context * ctx = (ggml_backend_sycl_reg_context *)reg->context;
  4528. GGML_ASSERT(index < ctx->devices.size());
  4529. return ctx->devices[index];
  4530. }
  4531. static void *ggml_backend_sycl_reg_get_proc_address(ggml_backend_reg_t reg, const char *name) {
  4532. GGML_UNUSED(reg);
  4533. // TODO: update to the current function signature
  4534. //if (strcmp(name, "ggml_backend_split_buffer_type") == 0) {
  4535. // return (void *)ggml_backend_sycl_split_buffer_type;
  4536. //}
  4537. // SYCL doesn't support registering host memory, left here for reference
  4538. // "ggml_backend_register_host_buffer"
  4539. // "ggml_backend_unregister_host_buffer"
  4540. return nullptr;
  4541. }
  4542. static const ggml_backend_reg_i ggml_backend_sycl_reg_interface = {
  4543. /* .get_name = */ ggml_backend_sycl_reg_get_name,
  4544. /* .get_device_count = */ ggml_backend_sycl_reg_get_device_count,
  4545. /* .get_device_get = */ ggml_backend_sycl_reg_get_device,
  4546. /* .get_proc_address = */ ggml_backend_sycl_reg_get_proc_address,
  4547. };
  4548. // backend registry
  4549. ggml_backend_reg_t ggml_backend_sycl_reg() {
  4550. static ggml_backend_reg reg;
  4551. static bool initialized = false;
  4552. {
  4553. static std::mutex mutex;
  4554. std::lock_guard<std::mutex> lock(mutex);
  4555. if (!initialized) {
  4556. ggml_backend_sycl_reg_context * ctx = new ggml_backend_sycl_reg_context;
  4557. for (int i = 0; i < ggml_sycl_info().device_count; i++) {
  4558. ggml_backend_sycl_device_context * dev_ctx = new ggml_backend_sycl_device_context;
  4559. dev_ctx->device = i;
  4560. dev_ctx->name = GGML_SYCL_NAME + std::to_string(i);
  4561. ggml_sycl_set_device(i);
  4562. dpct::device_info prop;
  4563. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  4564. prop, dpct::dev_mgr::instance().get_device(i))));
  4565. dev_ctx->description = prop.get_name();
  4566. ggml_backend_dev_t dev = new ggml_backend_device {
  4567. /* .interface = */ ggml_backend_sycl_device_interface,
  4568. /* .reg = */ &reg,
  4569. /* .context = */ dev_ctx
  4570. };
  4571. ctx->devices.push_back(dev);
  4572. }
  4573. reg = ggml_backend_reg {
  4574. /* .interface = */ ggml_backend_sycl_reg_interface,
  4575. /* .context = */ ctx
  4576. };
  4577. }
  4578. initialized = true;
  4579. }
  4580. return &reg;
  4581. }
  4582. ggml_backend_t ggml_backend_sycl_init(int device) {
  4583. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_init\n");
  4584. ggml_check_sycl();
  4585. check_allow_gpu_index(device);
  4586. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context(device);
  4587. if (ctx == nullptr) {
  4588. fprintf(stderr, "%s: error: failed to allocate context\n", __func__);
  4589. return nullptr;
  4590. };
  4591. ggml_backend_t sycl_backend = new ggml_backend {
  4592. /* .guid = */ ggml_backend_sycl_guid(),
  4593. /* .interface = */ ggml_backend_sycl_interface,
  4594. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_sycl_reg(), device),
  4595. /* .context = */ ctx
  4596. };
  4597. return sycl_backend;
  4598. }