ggml-sycl.cpp 238 KB

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