ggml-sycl.cpp 251 KB

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