ggml-sycl.cpp 669 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315
  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. /*
  35. Following definition copied from DPCT head files, which are used by ggml-sycl.cpp
  36. */
  37. // COPY from DPCT head files
  38. #include <sycl/sycl.hpp>
  39. #include <oneapi/mkl.hpp>
  40. #include <map>
  41. #if defined(__linux__)
  42. #include <sys/mman.h>
  43. #elif defined(_WIN64)
  44. #ifndef NOMINMAX
  45. #define NOMINMAX
  46. #endif
  47. #include <windows.h>
  48. #else
  49. #error "Only support Windows and Linux."
  50. #endif
  51. #if defined(__linux__)
  52. #include <unistd.h>
  53. #include <sys/syscall.h>
  54. #endif
  55. #if defined(_WIN64)
  56. #ifndef NOMINMAX
  57. #define NOMINMAX
  58. #endif
  59. #include <windows.h>
  60. #endif
  61. #define DPCT_COMPATIBILITY_TEMP (900)
  62. #if defined(_MSC_VER)
  63. #define __dpct_align__(n) __declspec(align(n))
  64. #define __dpct_inline__ __forceinline
  65. #else
  66. #define __dpct_align__(n) __attribute__((aligned(n)))
  67. #define __dpct_inline__ __inline__ __attribute__((always_inline))
  68. #endif
  69. #if defined(_MSC_VER)
  70. #define __dpct_noinline__ __declspec(noinline)
  71. #else
  72. #define __dpct_noinline__ __attribute__((noinline))
  73. #endif
  74. std::string get_device_type_name(const sycl::device &Device) {
  75. auto DeviceType = Device.get_info<sycl::info::device::device_type>();
  76. switch (DeviceType) {
  77. case sycl::info::device_type::cpu:
  78. return "cpu";
  79. case sycl::info::device_type::gpu:
  80. return "gpu";
  81. case sycl::info::device_type::host:
  82. return "host";
  83. case sycl::info::device_type::accelerator:
  84. return "acc";
  85. default:
  86. return "unknown";
  87. }
  88. }
  89. std::string get_device_backend_and_type(const sycl::device &device) {
  90. std::stringstream device_type;
  91. sycl::backend backend = device.get_backend();
  92. device_type << backend << ":" << get_device_type_name(device);
  93. return device_type.str();
  94. }
  95. namespace dpct
  96. {
  97. typedef sycl::queue *queue_ptr;
  98. typedef sycl::event *event_ptr;
  99. typedef char *device_ptr;
  100. typedef uint8_t byte_t;
  101. typedef sycl::buffer<byte_t> buffer_t;
  102. /// SYCL default exception handler
  103. inline auto exception_handler = [](sycl::exception_list exceptions)
  104. {
  105. for (std::exception_ptr const &e : exceptions)
  106. {
  107. try
  108. {
  109. std::rethrow_exception(e);
  110. }
  111. catch (sycl::exception const &e)
  112. {
  113. std::cerr << "Caught asynchronous SYCL exception:" << std::endl
  114. << e.what() << std::endl
  115. << "Exception caught at file:" << __FILE__
  116. << ", line:" << __LINE__ << std::endl;
  117. }
  118. }
  119. };
  120. enum error_code
  121. {
  122. success = 0,
  123. default_error = 999
  124. };
  125. enum memcpy_direction
  126. {
  127. host_to_host,
  128. host_to_device,
  129. device_to_host,
  130. device_to_device,
  131. automatic
  132. };
  133. enum memory_region
  134. {
  135. global = 0, // device global memory
  136. constant, // device constant memory
  137. local, // device local memory
  138. shared, // memory which can be accessed by host and device
  139. };
  140. enum class library_data_t : unsigned char
  141. {
  142. real_float = 0,
  143. complex_float,
  144. real_double,
  145. complex_double,
  146. real_half,
  147. complex_half,
  148. real_bfloat16,
  149. complex_bfloat16,
  150. real_int4,
  151. complex_int4,
  152. real_uint4,
  153. complex_uint4,
  154. real_int8,
  155. complex_int8,
  156. real_uint8,
  157. complex_uint8,
  158. real_int16,
  159. complex_int16,
  160. real_uint16,
  161. complex_uint16,
  162. real_int32,
  163. complex_int32,
  164. real_uint32,
  165. complex_uint32,
  166. real_int64,
  167. complex_int64,
  168. real_uint64,
  169. complex_uint64,
  170. real_int8_4,
  171. real_int8_32,
  172. real_uint8_4,
  173. library_data_t_size
  174. };
  175. template <typename T>
  176. struct DataType
  177. {
  178. using T2 = T;
  179. };
  180. template <typename T>
  181. struct DataType<sycl::vec<T, 2>>
  182. {
  183. using T2 = std::complex<T>;
  184. };
  185. static void destroy_event(event_ptr event)
  186. {
  187. delete event;
  188. }
  189. static inline unsigned int get_tid()
  190. {
  191. #if defined(__linux__)
  192. return syscall(SYS_gettid);
  193. #elif defined(_WIN64)
  194. return GetCurrentThreadId();
  195. #else
  196. #error "Only support Windows and Linux."
  197. #endif
  198. }
  199. namespace detail
  200. {
  201. static void get_version(const sycl::device &dev, int &major, int &minor)
  202. {
  203. // Version string has the following format:
  204. // a. OpenCL<space><major.minor><space><vendor-specific-information>
  205. // b. <major.minor>
  206. // c. <AmdGcnArchName> e.g gfx1030
  207. std::string ver;
  208. ver = dev.get_info<sycl::info::device::version>();
  209. std::string::size_type i = 0;
  210. while (i < ver.size()) {
  211. if (isdigit(ver[i]))
  212. break;
  213. i++;
  214. }
  215. major = std::stoi(&(ver[i]));
  216. while (i < ver.size()) {
  217. if (ver[i] == '.')
  218. break;
  219. i++;
  220. }
  221. if (i < ver.size()) {
  222. // a. and b.
  223. i++;
  224. minor = std::stoi(&(ver[i]));
  225. } else {
  226. // c.
  227. minor = 0;
  228. }
  229. }
  230. template <typename tag, typename T>
  231. class generic_error_type
  232. {
  233. public:
  234. generic_error_type() = default;
  235. generic_error_type(T value) : value{value} {}
  236. operator T() const { return value; }
  237. private:
  238. T value;
  239. };
  240. } // namespace detail
  241. /// Pitched 2D/3D memory data.
  242. class pitched_data
  243. {
  244. public:
  245. pitched_data() : pitched_data(nullptr, 0, 0, 0) {}
  246. pitched_data(void *data, size_t pitch, size_t x, size_t y)
  247. : _data(data), _pitch(pitch), _x(x), _y(y) {}
  248. void *get_data_ptr() { return _data; }
  249. void set_data_ptr(void *data) { _data = data; }
  250. size_t get_pitch() { return _pitch; }
  251. void set_pitch(size_t pitch) { _pitch = pitch; }
  252. size_t get_x() { return _x; }
  253. void set_x(size_t x) { _x = x; };
  254. size_t get_y() { return _y; }
  255. void set_y(size_t y) { _y = y; }
  256. private:
  257. void *_data;
  258. size_t _pitch, _x, _y;
  259. };
  260. class device_info
  261. {
  262. public:
  263. // get interface
  264. const char *get_name() const { return _name; }
  265. char *get_name() { return _name; }
  266. template <typename WorkItemSizesTy = sycl::range<3>,
  267. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  268. std::is_same_v<WorkItemSizesTy, int *>,
  269. int> = 0>
  270. auto get_max_work_item_sizes() const
  271. {
  272. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  273. return sycl::range<3>(_max_work_item_sizes_i[0],
  274. _max_work_item_sizes_i[1],
  275. _max_work_item_sizes_i[2]);
  276. else
  277. {
  278. return _max_work_item_sizes_i;
  279. }
  280. }
  281. template <typename WorkItemSizesTy = sycl::range<3>,
  282. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  283. std::is_same_v<WorkItemSizesTy, int *>,
  284. int> = 0>
  285. auto get_max_work_item_sizes()
  286. {
  287. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  288. return sycl::range<3>(_max_work_item_sizes_i[0],
  289. _max_work_item_sizes_i[1],
  290. _max_work_item_sizes_i[2]);
  291. else
  292. {
  293. return _max_work_item_sizes_i;
  294. }
  295. }
  296. bool get_host_unified_memory() const { return _host_unified_memory; }
  297. int get_major_version() const { return _major; }
  298. int get_minor_version() const { return _minor; }
  299. int get_integrated() const { return _integrated; }
  300. int get_max_clock_frequency() const { return _frequency; }
  301. int get_max_compute_units() const { return _max_compute_units; }
  302. int get_max_work_group_size() const { return _max_work_group_size; }
  303. int get_max_sub_group_size() const { return _max_sub_group_size; }
  304. int get_max_work_items_per_compute_unit() const
  305. {
  306. return _max_work_items_per_compute_unit;
  307. }
  308. int get_max_register_size_per_work_group() const
  309. {
  310. return _max_register_size_per_work_group;
  311. }
  312. template <typename NDRangeSizeTy = size_t *,
  313. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  314. std::is_same_v<NDRangeSizeTy, int *>,
  315. int> = 0>
  316. auto get_max_nd_range_size() const
  317. {
  318. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  319. return _max_nd_range_size;
  320. else
  321. return _max_nd_range_size_i;
  322. }
  323. template <typename NDRangeSizeTy = size_t *,
  324. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  325. std::is_same_v<NDRangeSizeTy, int *>,
  326. int> = 0>
  327. auto get_max_nd_range_size()
  328. {
  329. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  330. return _max_nd_range_size;
  331. else
  332. return _max_nd_range_size_i;
  333. }
  334. size_t get_global_mem_size() const { return _global_mem_size; }
  335. size_t get_local_mem_size() const { return _local_mem_size; }
  336. size_t get_max_mem_alloc_size() const { return _max_mem_alloc_size; }
  337. /// Returns the maximum clock rate of device's global memory in kHz. If
  338. /// compiler does not support this API then returns default value 3200000 kHz.
  339. unsigned int get_memory_clock_rate() const { return _memory_clock_rate; }
  340. /// Returns the maximum bus width between device and memory in bits. If
  341. /// compiler does not support this API then returns default value 64 bits.
  342. unsigned int get_memory_bus_width() const { return _memory_bus_width; }
  343. uint32_t get_device_id() const { return _device_id; }
  344. std::array<unsigned char, 16> get_uuid() const { return _uuid; }
  345. /// Returns global memory cache size in bytes.
  346. unsigned int get_global_mem_cache_size() const
  347. {
  348. return _global_mem_cache_size;
  349. }
  350. // set interface
  351. void set_name(const char *name)
  352. {
  353. size_t length = strlen(name);
  354. if (length < 256)
  355. {
  356. std::memcpy(_name, name, length + 1);
  357. }
  358. else
  359. {
  360. std::memcpy(_name, name, 255);
  361. _name[255] = '\0';
  362. }
  363. }
  364. void set_max_work_item_sizes(const sycl::range<3> max_work_item_sizes)
  365. {
  366. for (int i = 0; i < 3; ++i)
  367. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  368. }
  369. [[deprecated]] void
  370. set_max_work_item_sizes(const sycl::id<3> max_work_item_sizes)
  371. {
  372. for (int i = 0; i < 3; ++i)
  373. {
  374. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  375. }
  376. }
  377. void set_host_unified_memory(bool host_unified_memory)
  378. {
  379. _host_unified_memory = host_unified_memory;
  380. }
  381. void set_major_version(int major) { _major = major; }
  382. void set_minor_version(int minor) { _minor = minor; }
  383. void set_integrated(int integrated) { _integrated = integrated; }
  384. void set_max_clock_frequency(int frequency) { _frequency = frequency; }
  385. void set_max_compute_units(int max_compute_units)
  386. {
  387. _max_compute_units = max_compute_units;
  388. }
  389. void set_global_mem_size(size_t global_mem_size)
  390. {
  391. _global_mem_size = global_mem_size;
  392. }
  393. void set_local_mem_size(size_t local_mem_size)
  394. {
  395. _local_mem_size = local_mem_size;
  396. }
  397. void set_max_mem_alloc_size(size_t max_mem_alloc_size)
  398. {
  399. _max_mem_alloc_size = max_mem_alloc_size;
  400. }
  401. void set_max_work_group_size(int max_work_group_size)
  402. {
  403. _max_work_group_size = max_work_group_size;
  404. }
  405. void set_max_sub_group_size(int max_sub_group_size)
  406. {
  407. _max_sub_group_size = max_sub_group_size;
  408. }
  409. void
  410. set_max_work_items_per_compute_unit(int max_work_items_per_compute_unit)
  411. {
  412. _max_work_items_per_compute_unit = max_work_items_per_compute_unit;
  413. }
  414. void set_max_nd_range_size(int max_nd_range_size[])
  415. {
  416. for (int i = 0; i < 3; i++)
  417. {
  418. _max_nd_range_size[i] = max_nd_range_size[i];
  419. _max_nd_range_size_i[i] = max_nd_range_size[i];
  420. }
  421. }
  422. void set_memory_clock_rate(unsigned int memory_clock_rate)
  423. {
  424. _memory_clock_rate = memory_clock_rate;
  425. }
  426. void set_memory_bus_width(unsigned int memory_bus_width)
  427. {
  428. _memory_bus_width = memory_bus_width;
  429. }
  430. void
  431. set_max_register_size_per_work_group(int max_register_size_per_work_group)
  432. {
  433. _max_register_size_per_work_group = max_register_size_per_work_group;
  434. }
  435. void set_device_id(uint32_t device_id)
  436. {
  437. _device_id = device_id;
  438. }
  439. void set_uuid(std::array<unsigned char, 16> uuid)
  440. {
  441. _uuid = std::move(uuid);
  442. }
  443. void set_global_mem_cache_size(unsigned int global_mem_cache_size)
  444. {
  445. _global_mem_cache_size = global_mem_cache_size;
  446. }
  447. private:
  448. char _name[256];
  449. int _max_work_item_sizes_i[3];
  450. bool _host_unified_memory = false;
  451. int _major;
  452. int _minor;
  453. int _integrated = 0;
  454. int _frequency;
  455. // Set estimated value 3200000 kHz as default value.
  456. unsigned int _memory_clock_rate = 3200000;
  457. // Set estimated value 64 bits as default value.
  458. unsigned int _memory_bus_width = 64;
  459. unsigned int _global_mem_cache_size;
  460. int _max_compute_units;
  461. int _max_work_group_size;
  462. int _max_sub_group_size;
  463. int _max_work_items_per_compute_unit;
  464. int _max_register_size_per_work_group;
  465. size_t _global_mem_size;
  466. size_t _local_mem_size;
  467. size_t _max_mem_alloc_size;
  468. size_t _max_nd_range_size[3];
  469. int _max_nd_range_size_i[3];
  470. uint32_t _device_id;
  471. std::array<unsigned char, 16> _uuid;
  472. };
  473. static int get_major_version(const sycl::device &dev)
  474. {
  475. int major, minor;
  476. detail::get_version(dev, major, minor);
  477. return major;
  478. }
  479. static int get_minor_version(const sycl::device &dev)
  480. {
  481. int major, minor;
  482. detail::get_version(dev, major, minor);
  483. return minor;
  484. }
  485. static void get_device_info(device_info &out, const sycl::device &dev)
  486. {
  487. device_info prop;
  488. prop.set_name(dev.get_info<sycl::info::device::name>().c_str());
  489. int major, minor;
  490. detail::get_version(dev, major, minor);
  491. prop.set_major_version(major);
  492. prop.set_minor_version(minor);
  493. prop.set_max_work_item_sizes(
  494. #if (__SYCL_COMPILER_VERSION && __SYCL_COMPILER_VERSION < 20220902)
  495. // oneAPI DPC++ compiler older than 2022/09/02, where max_work_item_sizes
  496. // is an enum class element
  497. dev.get_info<sycl::info::device::max_work_item_sizes>());
  498. #else
  499. // SYCL 2020-conformant code, max_work_item_sizes is a struct templated by
  500. // an int
  501. dev.get_info<sycl::info::device::max_work_item_sizes<3>>());
  502. #endif
  503. prop.set_host_unified_memory(dev.has(sycl::aspect::usm_host_allocations));
  504. prop.set_max_clock_frequency(
  505. dev.get_info<sycl::info::device::max_clock_frequency>() * 1000);
  506. prop.set_max_compute_units(
  507. dev.get_info<sycl::info::device::max_compute_units>());
  508. prop.set_max_work_group_size(
  509. dev.get_info<sycl::info::device::max_work_group_size>());
  510. prop.set_global_mem_size(dev.get_info<sycl::info::device::global_mem_size>());
  511. prop.set_local_mem_size(dev.get_info<sycl::info::device::local_mem_size>());
  512. prop.set_max_mem_alloc_size(dev.get_info<sycl::info::device::max_mem_alloc_size>());
  513. #if (defined(SYCL_EXT_INTEL_DEVICE_INFO) && SYCL_EXT_INTEL_DEVICE_INFO >= 6)
  514. if (dev.has(sycl::aspect::ext_intel_memory_clock_rate))
  515. {
  516. unsigned int tmp =
  517. dev.get_info<sycl::ext::intel::info::device::memory_clock_rate>();
  518. if (tmp != 0)
  519. prop.set_memory_clock_rate(1000 * tmp);
  520. }
  521. if (dev.has(sycl::aspect::ext_intel_memory_bus_width))
  522. {
  523. prop.set_memory_bus_width(
  524. dev.get_info<sycl::ext::intel::info::device::memory_bus_width>());
  525. }
  526. if (dev.has(sycl::aspect::ext_intel_device_id))
  527. {
  528. prop.set_device_id(
  529. dev.get_info<sycl::ext::intel::info::device::device_id>());
  530. }
  531. if (dev.has(sycl::aspect::ext_intel_device_info_uuid))
  532. {
  533. prop.set_uuid(dev.get_info<sycl::ext::intel::info::device::uuid>());
  534. }
  535. #elif defined(_MSC_VER) && !defined(__clang__)
  536. #pragma message("get_device_info: querying memory_clock_rate and \
  537. memory_bus_width are not supported by the compiler used. \
  538. Use 3200000 kHz as memory_clock_rate default value. \
  539. Use 64 bits as memory_bus_width default value.")
  540. #else
  541. #warning "get_device_info: querying memory_clock_rate and \
  542. memory_bus_width are not supported by the compiler used. \
  543. Use 3200000 kHz as memory_clock_rate default value. \
  544. Use 64 bits as memory_bus_width default value."
  545. #endif
  546. size_t max_sub_group_size = 1;
  547. std::vector<size_t> sub_group_sizes =
  548. dev.get_info<sycl::info::device::sub_group_sizes>();
  549. for (const auto &sub_group_size : sub_group_sizes)
  550. {
  551. if (max_sub_group_size < sub_group_size)
  552. max_sub_group_size = sub_group_size;
  553. }
  554. prop.set_max_sub_group_size(max_sub_group_size);
  555. prop.set_max_work_items_per_compute_unit(
  556. dev.get_info<sycl::info::device::max_work_group_size>());
  557. int max_nd_range_size[] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
  558. prop.set_max_nd_range_size(max_nd_range_size);
  559. // Estimates max register size per work group, feel free to update the value
  560. // according to device properties.
  561. prop.set_max_register_size_per_work_group(65536);
  562. prop.set_global_mem_cache_size(
  563. dev.get_info<sycl::info::device::global_mem_cache_size>());
  564. out = prop;
  565. }
  566. /// dpct device extension
  567. class device_ext : public sycl::device
  568. {
  569. typedef std::mutex mutex_type;
  570. public:
  571. device_ext() : sycl::device(), _ctx(*this) {}
  572. ~device_ext()
  573. {
  574. std::lock_guard<mutex_type> lock(m_mutex);
  575. clear_queues();
  576. }
  577. device_ext(const sycl::device &base) : sycl::device(base), _ctx(*this)
  578. {
  579. std::lock_guard<mutex_type> lock(m_mutex);
  580. init_queues();
  581. }
  582. int is_native_atomic_supported() { return 0; }
  583. int get_major_version() const
  584. {
  585. return dpct::get_major_version(*this);
  586. }
  587. int get_minor_version() const
  588. {
  589. return dpct::get_minor_version(*this);
  590. }
  591. int get_max_compute_units() const
  592. {
  593. return get_device_info().get_max_compute_units();
  594. }
  595. /// Return the maximum clock frequency of this device in KHz.
  596. int get_max_clock_frequency() const
  597. {
  598. return get_device_info().get_max_clock_frequency();
  599. }
  600. int get_integrated() const { return get_device_info().get_integrated(); }
  601. int get_max_sub_group_size() const
  602. {
  603. return get_device_info().get_max_sub_group_size();
  604. }
  605. int get_max_register_size_per_work_group() const
  606. {
  607. return get_device_info().get_max_register_size_per_work_group();
  608. }
  609. int get_max_work_group_size() const
  610. {
  611. return get_device_info().get_max_work_group_size();
  612. }
  613. int get_mem_base_addr_align() const
  614. {
  615. return get_info<sycl::info::device::mem_base_addr_align>();
  616. }
  617. size_t get_global_mem_size() const
  618. {
  619. return get_device_info().get_global_mem_size();
  620. }
  621. size_t get_max_mem_alloc_size() const
  622. {
  623. return get_device_info().get_max_mem_alloc_size();
  624. }
  625. /// Get the number of bytes of free and total memory on the SYCL device.
  626. /// \param [out] free_memory The number of bytes of free memory on the SYCL device.
  627. /// \param [out] total_memory The number of bytes of total memory on the SYCL device.
  628. void get_memory_info(size_t &free_memory, size_t &total_memory)
  629. {
  630. total_memory = get_device_info().get_global_mem_size();
  631. const char *warning_info = "get_memory_info: [warning] ext_intel_free_memory is not "
  632. "supported (export/set ZES_ENABLE_SYSMAN=1 to support), "
  633. "use total memory as free memory";
  634. #if (defined(__SYCL_COMPILER_VERSION) && __SYCL_COMPILER_VERSION >= 20221105)
  635. if (!has(sycl::aspect::ext_intel_free_memory))
  636. {
  637. std::cerr << warning_info << std::endl;
  638. free_memory = total_memory;
  639. }
  640. else
  641. {
  642. free_memory = get_info<sycl::ext::intel::info::device::free_memory>();
  643. }
  644. #else
  645. std::cerr << warning_info << std::endl;
  646. free_memory = total_memory;
  647. #if defined(_MSC_VER) && !defined(__clang__)
  648. #pragma message("Querying the number of bytes of free memory is not supported")
  649. #else
  650. #warning "Querying the number of bytes of free memory is not supported"
  651. #endif
  652. #endif
  653. }
  654. void get_device_info(device_info &out) const
  655. {
  656. dpct::get_device_info(out, *this);
  657. }
  658. device_info get_device_info() const
  659. {
  660. device_info prop;
  661. dpct::get_device_info(prop, *this);
  662. return prop;
  663. }
  664. void reset()
  665. {
  666. std::lock_guard<mutex_type> lock(m_mutex);
  667. clear_queues();
  668. init_queues();
  669. }
  670. sycl::queue &in_order_queue() { return *_q_in_order; }
  671. sycl::queue &out_of_order_queue() { return *_q_out_of_order; }
  672. sycl::queue &default_queue()
  673. {
  674. return in_order_queue();
  675. }
  676. void queues_wait_and_throw()
  677. {
  678. std::unique_lock<mutex_type> lock(m_mutex);
  679. std::vector<std::shared_ptr<sycl::queue>> current_queues(
  680. _queues);
  681. lock.unlock();
  682. for (const auto &q : current_queues)
  683. {
  684. q->wait_and_throw();
  685. }
  686. // Guard the destruct of current_queues to make sure the ref count is safe.
  687. lock.lock();
  688. }
  689. sycl::queue *create_queue(bool enable_exception_handler = false)
  690. {
  691. return create_in_order_queue(enable_exception_handler);
  692. }
  693. sycl::queue *create_queue(sycl::context context, sycl::device device,
  694. bool enable_exception_handler = false) {
  695. return create_in_order_queue(context, device, enable_exception_handler);
  696. }
  697. sycl::queue *create_in_order_queue(bool enable_exception_handler = false) {
  698. std::lock_guard<mutex_type> lock(m_mutex);
  699. return create_queue_impl(enable_exception_handler,
  700. sycl::property::queue::in_order());
  701. }
  702. sycl::queue *create_in_order_queue(sycl::context context, sycl::device device,
  703. bool enable_exception_handler = false) {
  704. std::lock_guard<mutex_type> lock(m_mutex);
  705. return create_queue_impl(context, device, enable_exception_handler,
  706. sycl::property::queue::in_order());
  707. }
  708. sycl::queue *create_out_of_order_queue(bool enable_exception_handler = false) {
  709. std::lock_guard<mutex_type> lock(m_mutex);
  710. return create_queue_impl(enable_exception_handler);
  711. }
  712. void destroy_queue(sycl::queue *&queue)
  713. {
  714. std::lock_guard<mutex_type> lock(m_mutex);
  715. _queues.erase(std::remove_if(_queues.begin(), _queues.end(),
  716. [=](const std::shared_ptr<sycl::queue> &q) -> bool
  717. {
  718. return q.get() == queue;
  719. }),
  720. _queues.end());
  721. queue = nullptr;
  722. }
  723. void set_saved_queue(sycl::queue *q)
  724. {
  725. std::lock_guard<mutex_type> lock(m_mutex);
  726. _saved_queue = q;
  727. }
  728. sycl::queue *get_saved_queue() const
  729. {
  730. std::lock_guard<mutex_type> lock(m_mutex);
  731. return _saved_queue;
  732. }
  733. sycl::context get_context() const { return _ctx; }
  734. private:
  735. void clear_queues()
  736. {
  737. _queues.clear();
  738. _q_in_order = _q_out_of_order = _saved_queue = nullptr;
  739. }
  740. void init_queues()
  741. {
  742. _q_in_order = create_queue_impl(true, sycl::property::queue::in_order());
  743. _q_out_of_order = create_queue_impl(true);
  744. _saved_queue = &default_queue();
  745. }
  746. /// Caller should acquire resource \p m_mutex before calling this function.
  747. template <class... Properties>
  748. sycl::queue *create_queue_impl(bool enable_exception_handler,
  749. Properties... properties)
  750. {
  751. sycl::async_handler eh = {};
  752. if (enable_exception_handler)
  753. {
  754. eh = exception_handler;
  755. }
  756. _queues.push_back(std::make_shared<sycl::queue>(
  757. _ctx, *this, eh,
  758. sycl::property_list(
  759. #ifdef DPCT_PROFILING_ENABLED
  760. sycl::property::queue::enable_profiling(),
  761. #endif
  762. properties...)));
  763. return _queues.back().get();
  764. }
  765. template <class... Properties>
  766. sycl::queue *create_queue_impl(sycl::context context, sycl::device device,
  767. bool enable_exception_handler,
  768. Properties... properties) {
  769. sycl::async_handler eh = {};
  770. if (enable_exception_handler) {
  771. eh = exception_handler;
  772. }
  773. _queues.push_back(std::make_shared<sycl::queue>(
  774. context, device, eh,
  775. sycl::property_list(
  776. #ifdef DPCT_PROFILING_ENABLED
  777. sycl::property::queue::enable_profiling(),
  778. #endif
  779. properties...)));
  780. return _queues.back().get();
  781. }
  782. void get_version(int &major, int &minor) const
  783. {
  784. detail::get_version(*this, major, minor);
  785. }
  786. sycl::queue *_q_in_order, *_q_out_of_order;
  787. sycl::queue *_saved_queue;
  788. sycl::context _ctx;
  789. std::vector<std::shared_ptr<sycl::queue>> _queues;
  790. mutable mutex_type m_mutex;
  791. };
  792. /// device manager
  793. class dev_mgr
  794. {
  795. public:
  796. device_ext &current_device()
  797. {
  798. unsigned int dev_id = current_device_id();
  799. check_id(dev_id);
  800. return *_devs[dev_id];
  801. }
  802. device_ext &cpu_device() const
  803. {
  804. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  805. if (_cpu_device == -1)
  806. {
  807. throw std::runtime_error("no valid cpu device");
  808. }
  809. else
  810. {
  811. return *_devs[_cpu_device];
  812. }
  813. }
  814. device_ext &get_device(unsigned int id) const
  815. {
  816. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  817. check_id(id);
  818. return *_devs[id];
  819. }
  820. unsigned int current_device_id() const
  821. {
  822. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  823. auto it = _thread2dev_map.find(get_tid());
  824. if (it != _thread2dev_map.end())
  825. return it->second;
  826. return DEFAULT_DEVICE_ID;
  827. }
  828. /// Select device with a device ID.
  829. /// \param [in] id The id of the device which can
  830. /// be obtained through get_device_id(const sycl::device).
  831. void select_device(unsigned int id)
  832. {
  833. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  834. check_id(id);
  835. _thread2dev_map[get_tid()] = id;
  836. }
  837. unsigned int device_count() { return _devs.size(); }
  838. unsigned int get_device_id(const sycl::device &dev)
  839. {
  840. unsigned int id = 0;
  841. for (auto dev_item : _devs)
  842. {
  843. if (*dev_item == dev)
  844. {
  845. break;
  846. }
  847. id++;
  848. }
  849. return id;
  850. }
  851. template <class DeviceSelector>
  852. std::enable_if_t<
  853. std::is_invocable_r_v<int, DeviceSelector, const sycl::device &>>
  854. select_device(const DeviceSelector &selector = sycl::gpu_selector_v)
  855. {
  856. sycl::device selected_device = sycl::device(selector);
  857. unsigned int selected_device_id = get_device_id(selected_device);
  858. select_device(selected_device_id);
  859. }
  860. /// Returns the instance of device manager singleton.
  861. static dev_mgr &instance()
  862. {
  863. static dev_mgr d_m;
  864. return d_m;
  865. }
  866. dev_mgr(const dev_mgr &) = delete;
  867. dev_mgr &operator=(const dev_mgr &) = delete;
  868. dev_mgr(dev_mgr &&) = delete;
  869. dev_mgr &operator=(dev_mgr &&) = delete;
  870. private:
  871. mutable std::recursive_mutex m_mutex;
  872. static bool compare_dev(sycl::device &device1, sycl::device &device2)
  873. {
  874. dpct::device_info prop1;
  875. dpct::get_device_info(prop1, device1);
  876. dpct::device_info prop2;
  877. dpct::get_device_info(prop2, device2);
  878. return prop1.get_max_compute_units() > prop2.get_max_compute_units();
  879. }
  880. static int convert_backend_index(std::string & backend) {
  881. if (backend == "ext_oneapi_level_zero:gpu") return 0;
  882. if (backend == "opencl:gpu") return 1;
  883. if (backend == "ext_oneapi_cuda:gpu") return 2;
  884. if (backend == "ext_oneapi_hip:gpu") return 3;
  885. if (backend == "opencl:cpu") return 4;
  886. if (backend == "opencl:acc") return 5;
  887. printf("convert_backend_index: can't handle backend=%s\n", backend.c_str());
  888. GGML_ASSERT(false);
  889. }
  890. static bool compare_backend(std::string &backend1, std::string &backend2) {
  891. return convert_backend_index(backend1) < convert_backend_index(backend2);
  892. }
  893. dev_mgr()
  894. {
  895. sycl::device default_device =
  896. sycl::device(sycl::default_selector_v);
  897. _devs.push_back(std::make_shared<device_ext>(default_device));
  898. std::vector<sycl::device> sycl_all_devs;
  899. // Collect other devices except for the default device.
  900. if (default_device.is_cpu())
  901. _cpu_device = 0;
  902. auto Platforms = sycl::platform::get_platforms();
  903. // Keep track of the number of devices per backend
  904. std::map<sycl::backend, size_t> DeviceNums;
  905. std::map<std::string, std::vector<sycl::device>> backend_devices;
  906. while (!Platforms.empty()) {
  907. auto Platform = Platforms.back();
  908. Platforms.pop_back();
  909. auto devices = Platform.get_devices();
  910. std::string backend_type = get_device_backend_and_type(devices[0]);
  911. for (const auto &device : devices) {
  912. backend_devices[backend_type].push_back(device);
  913. }
  914. }
  915. std::vector<std::string> keys;
  916. for(auto it = backend_devices.begin(); it != backend_devices.end(); ++it) {
  917. keys.push_back(it->first);
  918. }
  919. std::sort(keys.begin(), keys.end(), compare_backend);
  920. for (auto &key : keys) {
  921. std::vector<sycl::device> devs = backend_devices[key];
  922. std::sort(devs.begin(), devs.end(), compare_dev);
  923. for (const auto &dev : devs) {
  924. sycl_all_devs.push_back(dev);
  925. }
  926. }
  927. for (auto &dev : sycl_all_devs)
  928. {
  929. if (dev == default_device)
  930. {
  931. continue;
  932. }
  933. _devs.push_back(std::make_shared<device_ext>(dev));
  934. if (_cpu_device == -1 && dev.is_cpu())
  935. {
  936. _cpu_device = _devs.size() - 1;
  937. }
  938. }
  939. }
  940. void check_id(unsigned int id) const
  941. {
  942. if (id >= _devs.size())
  943. {
  944. throw std::runtime_error("invalid device id");
  945. }
  946. }
  947. std::vector<std::shared_ptr<device_ext>> _devs;
  948. /// DEFAULT_DEVICE_ID is used, if current_device_id() can not find current
  949. /// thread id in _thread2dev_map, which means default device should be used
  950. /// for the current thread.
  951. const unsigned int DEFAULT_DEVICE_ID = 0;
  952. /// thread-id to device-id map.
  953. std::map<unsigned int, unsigned int> _thread2dev_map;
  954. int _cpu_device = -1;
  955. };
  956. static inline sycl::queue &get_default_queue()
  957. {
  958. return dev_mgr::instance().current_device().default_queue();
  959. }
  960. namespace detail
  961. {
  962. enum class pointer_access_attribute
  963. {
  964. host_only = 0,
  965. device_only,
  966. host_device,
  967. end
  968. };
  969. static pointer_access_attribute get_pointer_attribute(sycl::queue &q,
  970. const void *ptr)
  971. {
  972. switch (sycl::get_pointer_type(ptr, q.get_context()))
  973. {
  974. case sycl::usm::alloc::unknown:
  975. return pointer_access_attribute::host_only;
  976. case sycl::usm::alloc::device:
  977. return pointer_access_attribute::device_only;
  978. case sycl::usm::alloc::shared:
  979. case sycl::usm::alloc::host:
  980. return pointer_access_attribute::host_device;
  981. }
  982. }
  983. template <typename ArgT>
  984. inline constexpr std::uint64_t get_type_combination_id(ArgT Val)
  985. {
  986. static_assert((unsigned char)library_data_t::library_data_t_size <=
  987. std::numeric_limits<unsigned char>::max() &&
  988. "library_data_t size exceeds limit.");
  989. static_assert(std::is_same_v<ArgT, library_data_t>, "Unsupported ArgT");
  990. return (std::uint64_t)Val;
  991. }
  992. template <typename FirstT, typename... RestT>
  993. inline constexpr std::uint64_t get_type_combination_id(FirstT FirstVal,
  994. RestT... RestVal)
  995. {
  996. static_assert((std::uint8_t)library_data_t::library_data_t_size <=
  997. std::numeric_limits<unsigned char>::max() &&
  998. "library_data_t size exceeds limit.");
  999. static_assert(sizeof...(RestT) <= 8 && "Too many parameters");
  1000. static_assert(std::is_same_v<FirstT, library_data_t>, "Unsupported FirstT");
  1001. return get_type_combination_id(RestVal...) << 8 | ((std::uint64_t)FirstVal);
  1002. }
  1003. class mem_mgr
  1004. {
  1005. mem_mgr()
  1006. {
  1007. // Reserved address space, no real memory allocation happens here.
  1008. #if defined(__linux__)
  1009. mapped_address_space =
  1010. (byte_t *)mmap(nullptr, mapped_region_size, PROT_NONE,
  1011. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  1012. #elif defined(_WIN64)
  1013. mapped_address_space = (byte_t *)VirtualAlloc(
  1014. NULL, // NULL specified as the base address parameter
  1015. mapped_region_size, // Size of allocation
  1016. MEM_RESERVE, // Allocate reserved pages
  1017. PAGE_NOACCESS); // Protection = no access
  1018. #else
  1019. #error "Only support Windows and Linux."
  1020. #endif
  1021. next_free = mapped_address_space;
  1022. };
  1023. public:
  1024. using buffer_id_t = int;
  1025. struct allocation
  1026. {
  1027. buffer_t buffer;
  1028. byte_t *alloc_ptr;
  1029. size_t size;
  1030. };
  1031. ~mem_mgr()
  1032. {
  1033. #if defined(__linux__)
  1034. munmap(mapped_address_space, mapped_region_size);
  1035. #elif defined(_WIN64)
  1036. VirtualFree(mapped_address_space, 0, MEM_RELEASE);
  1037. #else
  1038. #error "Only support Windows and Linux."
  1039. #endif
  1040. };
  1041. mem_mgr(const mem_mgr &) = delete;
  1042. mem_mgr &operator=(const mem_mgr &) = delete;
  1043. mem_mgr(mem_mgr &&) = delete;
  1044. mem_mgr &operator=(mem_mgr &&) = delete;
  1045. /// Allocate
  1046. void *mem_alloc(size_t size)
  1047. {
  1048. if (!size)
  1049. return nullptr;
  1050. std::lock_guard<std::mutex> lock(m_mutex);
  1051. if (next_free + size > mapped_address_space + mapped_region_size)
  1052. {
  1053. throw std::runtime_error("dpct_malloc: out of memory for virtual memory pool");
  1054. }
  1055. // Allocation
  1056. sycl::range<1> r(size);
  1057. buffer_t buf(r);
  1058. allocation A{buf, next_free, size};
  1059. // Map allocation to device pointer
  1060. void *result = next_free;
  1061. m_map.emplace(next_free + size, A);
  1062. // Update pointer to the next free space.
  1063. next_free += (size + extra_padding + alignment - 1) & ~(alignment - 1);
  1064. return result;
  1065. }
  1066. /// Deallocate
  1067. void mem_free(const void *ptr)
  1068. {
  1069. if (!ptr)
  1070. return;
  1071. std::lock_guard<std::mutex> lock(m_mutex);
  1072. auto it = get_map_iterator(ptr);
  1073. m_map.erase(it);
  1074. }
  1075. /// map: device pointer -> allocation(buffer, alloc_ptr, size)
  1076. allocation translate_ptr(const void *ptr)
  1077. {
  1078. std::lock_guard<std::mutex> lock(m_mutex);
  1079. auto it = get_map_iterator(ptr);
  1080. return it->second;
  1081. }
  1082. /// Check if the pointer represents device pointer or not.
  1083. bool is_device_ptr(const void *ptr) const
  1084. {
  1085. std::lock_guard<std::mutex> lock(m_mutex);
  1086. return (mapped_address_space <= ptr) &&
  1087. (ptr < mapped_address_space + mapped_region_size);
  1088. }
  1089. /// Returns the instance of memory manager singleton.
  1090. static mem_mgr &instance()
  1091. {
  1092. static mem_mgr m;
  1093. return m;
  1094. }
  1095. private:
  1096. std::map<byte_t *, allocation> m_map;
  1097. mutable std::mutex m_mutex;
  1098. byte_t *mapped_address_space;
  1099. byte_t *next_free;
  1100. const size_t mapped_region_size = 128ull * 1024 * 1024 * 1024;
  1101. const size_t alignment = 256;
  1102. /// This padding may be defined to some positive value to debug
  1103. /// out of bound accesses.
  1104. const size_t extra_padding = 0;
  1105. std::map<byte_t *, allocation>::iterator get_map_iterator(const void *ptr)
  1106. {
  1107. auto it = m_map.upper_bound((byte_t *)ptr);
  1108. if (it == m_map.end())
  1109. {
  1110. // Not a virtual pointer.
  1111. throw std::runtime_error("can not get buffer from non-virtual pointer");
  1112. }
  1113. const allocation &alloc = it->second;
  1114. if (ptr < alloc.alloc_ptr)
  1115. {
  1116. // Out of bound.
  1117. // This may happen if there's a gap between allocations due to alignment
  1118. // or extra padding and pointer points to this gap.
  1119. throw std::runtime_error("invalid virtual pointer");
  1120. }
  1121. return it;
  1122. }
  1123. };
  1124. template <class T, memory_region Memory, size_t Dimension>
  1125. class accessor;
  1126. template <memory_region Memory, class T = byte_t>
  1127. class memory_traits
  1128. {
  1129. public:
  1130. static constexpr sycl::access::target target =
  1131. sycl::access::target::device;
  1132. static constexpr sycl::access_mode mode =
  1133. (Memory == constant) ? sycl::access_mode::read
  1134. : sycl::access_mode::read_write;
  1135. static constexpr size_t type_size = sizeof(T);
  1136. using element_t =
  1137. typename std::conditional<Memory == constant, const T, T>::type;
  1138. using value_t = typename std::remove_cv<T>::type;
  1139. template <size_t Dimension = 1>
  1140. using accessor_t = typename std::conditional<
  1141. Memory == local, sycl::local_accessor<value_t, Dimension>,
  1142. sycl::accessor<T, Dimension, mode, target>>::type;
  1143. using pointer_t = T *;
  1144. };
  1145. static inline void *dpct_malloc(size_t size, sycl::queue &q)
  1146. {
  1147. return sycl::malloc_device(size, q.get_device(), q.get_context());
  1148. }
  1149. #define PITCH_DEFAULT_ALIGN(x) (((x) + 31) & ~(0x1F))
  1150. static inline void *dpct_malloc(size_t &pitch, size_t x, size_t y, size_t z,
  1151. sycl::queue &q)
  1152. {
  1153. pitch = PITCH_DEFAULT_ALIGN(x);
  1154. return dpct_malloc(pitch * y * z, q);
  1155. }
  1156. /**
  1157. * @brief Sets \p value to the first \p size elements starting from \p dev_ptr in \p q.
  1158. * @tparam valueT The type of the element to be set.
  1159. * @param [in] q The queue in which the operation is done.
  1160. * @param [in] dev_ptr Pointer to the virtual device memory address.
  1161. * @param [in] value The value to be set.
  1162. * @param [in] size Number of elements to be set to the value.
  1163. * @return An event representing the memset operation.
  1164. */
  1165. template <typename valueT>
  1166. static inline sycl::event dpct_memset(sycl::queue &q, void *dev_ptr,
  1167. valueT value, size_t size)
  1168. {
  1169. return q.fill(dev_ptr, value, size);
  1170. }
  1171. /**
  1172. * @brief Sets \p value to the 3D memory region pointed by \p data in \p q.
  1173. * @tparam valueT The type of the element to be set.
  1174. * @param [in] q The queue in which the operation is done.
  1175. * @param [in] data Pointer to the pitched device memory region.
  1176. * @param [in] value The value to be set.
  1177. * @param [in] size 3D memory region by number of elements.
  1178. * @return An event list representing the memset operations.
  1179. */
  1180. template <typename valueT>
  1181. static inline std::vector<sycl::event>
  1182. dpct_memset(sycl::queue &q, pitched_data data, valueT value,
  1183. sycl::range<3> size)
  1184. {
  1185. std::vector<sycl::event> event_list;
  1186. size_t slice = data.get_pitch() * data.get_y();
  1187. unsigned char *data_surface = (unsigned char *)data.get_data_ptr();
  1188. for (size_t z = 0; z < size.get(2); ++z)
  1189. {
  1190. unsigned char *data_ptr = data_surface;
  1191. for (size_t y = 0; y < size.get(1); ++y)
  1192. {
  1193. event_list.push_back(dpct_memset(q, data_ptr, value, size.get(0)));
  1194. data_ptr += data.get_pitch();
  1195. }
  1196. data_surface += slice;
  1197. }
  1198. return event_list;
  1199. }
  1200. /**
  1201. * @brief Sets \p val to the pitched 2D memory region pointed by \p ptr in \p q.
  1202. * @tparam valueT The type of the element to be set.
  1203. * @param [in] q The queue in which the operation is done.
  1204. * @param [in] ptr Pointer to the virtual device memory.
  1205. * @param [in] pitch The pitch size by number of elements, including padding.
  1206. * @param [in] val The value to be set.
  1207. * @param [in] x The width of memory region by number of elements.
  1208. * @param [in] y The height of memory region by number of elements.
  1209. * @return An event list representing the memset operations.
  1210. */
  1211. template <typename valueT>
  1212. static inline std::vector<sycl::event>
  1213. dpct_memset(sycl::queue &q, void *ptr, size_t pitch, valueT val, size_t x,
  1214. size_t y)
  1215. {
  1216. return dpct_memset(q, pitched_data(ptr, pitch, x, 1), val,
  1217. sycl::range<3>(x, y, 1));
  1218. }
  1219. static memcpy_direction deduce_memcpy_direction(sycl::queue &q, void *to_ptr,
  1220. const void *from_ptr,
  1221. memcpy_direction dir)
  1222. {
  1223. switch (dir)
  1224. {
  1225. case memcpy_direction::host_to_host:
  1226. case memcpy_direction::host_to_device:
  1227. case memcpy_direction::device_to_host:
  1228. case memcpy_direction::device_to_device:
  1229. return dir;
  1230. case memcpy_direction::automatic:
  1231. {
  1232. // table[to_attribute][from_attribute]
  1233. static const memcpy_direction
  1234. direction_table[static_cast<unsigned>(pointer_access_attribute::end)]
  1235. [static_cast<unsigned>(pointer_access_attribute::end)] =
  1236. {{memcpy_direction::host_to_host,
  1237. memcpy_direction::device_to_host,
  1238. memcpy_direction::host_to_host},
  1239. {memcpy_direction::host_to_device,
  1240. memcpy_direction::device_to_device,
  1241. memcpy_direction::device_to_device},
  1242. {memcpy_direction::host_to_host,
  1243. memcpy_direction::device_to_device,
  1244. memcpy_direction::device_to_device}};
  1245. return direction_table[static_cast<unsigned>(get_pointer_attribute(
  1246. q, to_ptr))][static_cast<unsigned>(get_pointer_attribute(q, from_ptr))];
  1247. }
  1248. default:
  1249. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1250. }
  1251. }
  1252. static sycl::event
  1253. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1254. memcpy_direction direction,
  1255. const std::vector<sycl::event> &dep_events = {})
  1256. {
  1257. if (!size)
  1258. return sycl::event{};
  1259. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1260. GGML_UNUSED(direction);
  1261. }
  1262. // Get actual copy range and make sure it will not exceed range.
  1263. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1264. size_t pitch)
  1265. {
  1266. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1267. }
  1268. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1269. size_t pitch)
  1270. {
  1271. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1272. }
  1273. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1274. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1275. static inline std::vector<sycl::event>
  1276. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1277. sycl::range<3> to_range, sycl::range<3> from_range,
  1278. sycl::id<3> to_id, sycl::id<3> from_id,
  1279. sycl::range<3> size, memcpy_direction direction,
  1280. const std::vector<sycl::event> &dep_events = {})
  1281. {
  1282. // RAII for host pointer
  1283. class host_buffer
  1284. {
  1285. void *_buf;
  1286. size_t _size;
  1287. sycl::queue &_q;
  1288. const std::vector<sycl::event> &_deps; // free operation depends
  1289. public:
  1290. host_buffer(size_t size, sycl::queue &q,
  1291. const std::vector<sycl::event> &deps)
  1292. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1293. void *get_ptr() const { return _buf; }
  1294. size_t get_size() const { return _size; }
  1295. ~host_buffer()
  1296. {
  1297. if (_buf)
  1298. {
  1299. _q.submit([&](sycl::handler &cgh)
  1300. {
  1301. cgh.depends_on(_deps);
  1302. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1303. }
  1304. }
  1305. };
  1306. std::vector<sycl::event> event_list;
  1307. size_t to_slice = to_range.get(1) * to_range.get(0),
  1308. from_slice = from_range.get(1) * from_range.get(0);
  1309. unsigned char *to_surface =
  1310. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1311. const unsigned char *from_surface =
  1312. (const unsigned char *)from_ptr +
  1313. get_offset(from_id, from_slice, from_range.get(0));
  1314. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1315. {
  1316. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1317. direction, dep_events)};
  1318. }
  1319. direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1320. size_t size_slice = size.get(1) * size.get(0);
  1321. switch (direction)
  1322. {
  1323. case host_to_host:
  1324. for (size_t z = 0; z < size.get(2); ++z)
  1325. {
  1326. unsigned char *to_ptr = to_surface;
  1327. const unsigned char *from_ptr = from_surface;
  1328. if (to_range.get(0) == from_range.get(0) &&
  1329. to_range.get(0) == size.get(0))
  1330. {
  1331. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1332. direction, dep_events));
  1333. }
  1334. else
  1335. {
  1336. for (size_t y = 0; y < size.get(1); ++y)
  1337. {
  1338. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1339. direction, dep_events));
  1340. to_ptr += to_range.get(0);
  1341. from_ptr += from_range.get(0);
  1342. }
  1343. }
  1344. to_surface += to_slice;
  1345. from_surface += from_slice;
  1346. }
  1347. break;
  1348. case host_to_device:
  1349. {
  1350. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1351. event_list);
  1352. std::vector<sycl::event> host_events;
  1353. if (to_slice == size_slice)
  1354. {
  1355. // Copy host data to a temp host buffer with the shape of target.
  1356. host_events =
  1357. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  1358. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  1359. host_to_host, dep_events);
  1360. }
  1361. else
  1362. {
  1363. // Copy host data to a temp host buffer with the shape of target.
  1364. host_events = dpct_memcpy(
  1365. q, buf.get_ptr(), from_surface, to_range, from_range,
  1366. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  1367. // If has padding data, not sure whether it is useless. So fill temp
  1368. // buffer with it.
  1369. std::vector<sycl::event>{
  1370. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  1371. device_to_host, dep_events)});
  1372. }
  1373. // Copy from temp host buffer to device with only one submit.
  1374. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  1375. buf.get_size(), host_to_device,
  1376. host_events));
  1377. break;
  1378. }
  1379. case device_to_host:
  1380. {
  1381. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  1382. event_list);
  1383. // Copy from host temp buffer to host target with reshaping.
  1384. event_list = dpct_memcpy(
  1385. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  1386. sycl::id<3>(0, 0, 0), size, host_to_host,
  1387. // Copy from device to temp host buffer with only one submit.
  1388. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  1389. buf.get_size(),
  1390. device_to_host, dep_events)});
  1391. break;
  1392. }
  1393. case device_to_device:
  1394. event_list.push_back(q.submit([&](sycl::handler &cgh){
  1395. cgh.depends_on(dep_events);
  1396. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  1397. size,
  1398. [=](sycl::id<3> id) {
  1399. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  1400. from_surface[get_offset(id, from_slice, from_range.get(0))];
  1401. }); }));
  1402. break;
  1403. default:
  1404. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1405. }
  1406. return event_list;
  1407. }
  1408. /// memcpy 2D/3D matrix specified by pitched_data.
  1409. static inline std::vector<sycl::event>
  1410. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  1411. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  1412. memcpy_direction direction = automatic)
  1413. {
  1414. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  1415. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  1416. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  1417. size, direction);
  1418. }
  1419. /// memcpy 2D matrix with pitch.
  1420. static inline std::vector<sycl::event>
  1421. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1422. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  1423. memcpy_direction direction = automatic)
  1424. {
  1425. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  1426. sycl::range<3>(from_pitch, y, 1),
  1427. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  1428. sycl::range<3>(x, y, 1), direction);
  1429. }
  1430. namespace deprecated
  1431. {
  1432. template <typename T, sycl::usm::alloc AllocKind>
  1433. class usm_allocator
  1434. {
  1435. private:
  1436. using Alloc = sycl::usm_allocator<T, AllocKind>;
  1437. Alloc _impl;
  1438. public:
  1439. using value_type = typename std::allocator_traits<Alloc>::value_type;
  1440. using pointer = typename std::allocator_traits<Alloc>::pointer;
  1441. using const_pointer = typename std::allocator_traits<Alloc>::const_pointer;
  1442. using void_pointer = typename std::allocator_traits<Alloc>::void_pointer;
  1443. using const_void_pointer =
  1444. typename std::allocator_traits<Alloc>::const_void_pointer;
  1445. using reference = typename std::allocator_traits<Alloc>::value_type &;
  1446. using const_reference =
  1447. const typename std::allocator_traits<Alloc>::value_type &;
  1448. using difference_type =
  1449. typename std::allocator_traits<Alloc>::difference_type;
  1450. using size_type = typename std::allocator_traits<Alloc>::size_type;
  1451. using propagate_on_container_copy_assignment = typename std::allocator_traits<
  1452. Alloc>::propagate_on_container_copy_assignment;
  1453. using propagate_on_container_move_assignment = typename std::allocator_traits<
  1454. Alloc>::propagate_on_container_move_assignment;
  1455. using propagate_on_container_swap =
  1456. typename std::allocator_traits<Alloc>::propagate_on_container_swap;
  1457. using is_always_equal =
  1458. typename std::allocator_traits<Alloc>::is_always_equal;
  1459. template <typename U>
  1460. struct rebind
  1461. {
  1462. typedef usm_allocator<U, AllocKind> other;
  1463. };
  1464. usm_allocator() : _impl(dpct::get_default_queue()) {}
  1465. ~usm_allocator() {}
  1466. usm_allocator(const usm_allocator &other) : _impl(other._impl) {}
  1467. usm_allocator(usm_allocator &&other) : _impl(std::move(other._impl)) {}
  1468. pointer address(reference r) { return &r; }
  1469. const_pointer address(const_reference r) { return &r; }
  1470. pointer allocate(size_type cnt, const_void_pointer hint = nullptr)
  1471. {
  1472. return std::allocator_traits<Alloc>::allocate(_impl, cnt, hint);
  1473. }
  1474. void deallocate(pointer p, size_type cnt)
  1475. {
  1476. std::allocator_traits<Alloc>::deallocate(_impl, p, cnt);
  1477. }
  1478. size_type max_size() const
  1479. {
  1480. return std::allocator_traits<Alloc>::max_size(_impl);
  1481. }
  1482. bool operator==(const usm_allocator &other) const { return _impl == other._impl; }
  1483. bool operator!=(const usm_allocator &other) const { return _impl != other._impl; }
  1484. };
  1485. } // namespace deprecated
  1486. inline void dpct_free(void *ptr,
  1487. const sycl::queue &q)
  1488. {
  1489. if (ptr)
  1490. {
  1491. sycl::free(ptr, q.get_context());
  1492. }
  1493. }
  1494. template <typename T>
  1495. inline auto get_memory(const void *x)
  1496. {
  1497. T *new_x = reinterpret_cast<T *>(const_cast<void *>(x));
  1498. return new_x;
  1499. }
  1500. template <typename T>
  1501. inline typename DataType<T>::T2 get_value(const T *s, sycl::queue &q)
  1502. {
  1503. using Ty = typename DataType<T>::T2;
  1504. Ty s_h;
  1505. if (get_pointer_attribute(q, s) == pointer_access_attribute::device_only)
  1506. detail::dpct_memcpy(q, (void *)&s_h, (const void *)s, sizeof(T), device_to_host)
  1507. .wait();
  1508. else
  1509. s_h = *reinterpret_cast<const Ty *>(s);
  1510. return s_h;
  1511. }
  1512. } // namespace detail
  1513. template <typename T>
  1514. inline auto get_value(const T *s, sycl::queue &q)
  1515. {
  1516. return detail::get_value(s, q);
  1517. }
  1518. namespace detail
  1519. {
  1520. template <class Ta, class Tb, class Tc, class Ts>
  1521. inline void gemm_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1522. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1523. const void *alpha, const void *a, int lda, const void *b,
  1524. int ldb, const void *beta, void *c, int ldc)
  1525. {
  1526. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1527. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1528. auto data_a = get_memory<const Ta>(a);
  1529. auto data_b = get_memory<const Tb>(b);
  1530. auto data_c = get_memory<Tc>(c);
  1531. oneapi::mkl::blas::column_major::gemm(
  1532. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1533. data_b, ldb, beta_value, data_c, ldc);
  1534. }
  1535. template <typename VecT, class BinaryOperation, class = void>
  1536. class vectorized_binary
  1537. {
  1538. public:
  1539. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1540. {
  1541. VecT v4;
  1542. for (size_t i = 0; i < v4.size(); ++i)
  1543. {
  1544. v4[i] = binary_op(a[i], b[i]);
  1545. }
  1546. return v4;
  1547. }
  1548. };
  1549. template <typename VecT, class BinaryOperation>
  1550. class vectorized_binary<
  1551. VecT, BinaryOperation,
  1552. std::void_t<std::invoke_result_t<BinaryOperation, VecT, VecT>>>
  1553. {
  1554. public:
  1555. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1556. {
  1557. return binary_op(a, b).template as<VecT>();
  1558. }
  1559. };
  1560. template <class Ta, class Tb, class Tc, class Ts>
  1561. inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1562. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1563. const void *alpha, const void **a, int lda,
  1564. const void **b, int ldb, const void *beta, void **c,
  1565. int ldc, int batch_size)
  1566. {
  1567. struct matrix_info_t
  1568. {
  1569. oneapi::mkl::transpose transpose_info[2];
  1570. Ts value_info[2];
  1571. std::int64_t size_info[3];
  1572. std::int64_t ld_info[3];
  1573. std::int64_t groupsize_info;
  1574. };
  1575. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1576. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1577. matrix_info_t *matrix_info =
  1578. (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
  1579. matrix_info->transpose_info[0] = a_trans;
  1580. matrix_info->transpose_info[1] = b_trans;
  1581. matrix_info->value_info[0] = alpha_value;
  1582. matrix_info->value_info[1] = beta_value;
  1583. matrix_info->size_info[0] = m;
  1584. matrix_info->size_info[1] = n;
  1585. matrix_info->size_info[2] = k;
  1586. matrix_info->ld_info[0] = lda;
  1587. matrix_info->ld_info[1] = ldb;
  1588. matrix_info->ld_info[2] = ldc;
  1589. matrix_info->groupsize_info = batch_size;
  1590. sycl::event e = oneapi::mkl::blas::column_major::gemm_batch(
  1591. q, matrix_info->transpose_info, matrix_info->transpose_info + 1,
  1592. matrix_info->size_info, matrix_info->size_info + 1,
  1593. matrix_info->size_info + 2, matrix_info->value_info,
  1594. reinterpret_cast<const Ta **>(a), matrix_info->ld_info,
  1595. reinterpret_cast<const Tb **>(b), matrix_info->ld_info + 1,
  1596. matrix_info->value_info + 1, reinterpret_cast<Tc **>(c),
  1597. matrix_info->ld_info + 2, 1, &(matrix_info->groupsize_info));
  1598. q.submit([&](sycl::handler &cgh)
  1599. {
  1600. cgh.depends_on(e);
  1601. cgh.host_task([=] { std::free(matrix_info); }); });
  1602. }
  1603. template <class Ta, class Tb, class Tc, class Ts>
  1604. inline void
  1605. gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1606. oneapi::mkl::transpose b_trans, int m, int n,
  1607. int k, const void *alpha, const void *a, int lda,
  1608. long long int stride_a, const void *b, int ldb,
  1609. long long int stride_b, const void *beta, void *c,
  1610. int ldc, long long int stride_c, int batch_size)
  1611. {
  1612. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1613. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1614. auto data_a = get_memory<const Ta>(a);
  1615. auto data_b = get_memory<const Tb>(b);
  1616. auto data_c = get_memory<Tc>(c);
  1617. oneapi::mkl::blas::column_major::gemm_batch(
  1618. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1619. stride_a, data_b, ldb, stride_b, beta_value,
  1620. data_c, ldc, stride_c, batch_size);
  1621. }
  1622. } // namespace detail
  1623. template <typename VecT, class BinaryOperation>
  1624. inline unsigned vectorized_binary(unsigned a, unsigned b,
  1625. const BinaryOperation binary_op)
  1626. {
  1627. sycl::vec<unsigned, 1> v0{a}, v1{b};
  1628. auto v2 = v0.as<VecT>();
  1629. auto v3 = v1.as<VecT>();
  1630. auto v4 =
  1631. detail::vectorized_binary<VecT, BinaryOperation>()(v2, v3, binary_op);
  1632. v0 = v4.template as<sycl::vec<unsigned, 1>>();
  1633. return v0;
  1634. }
  1635. static void async_dpct_memcpy(void *to_ptr, const void *from_ptr, size_t size,
  1636. memcpy_direction direction = automatic,
  1637. sycl::queue &q = dpct::get_default_queue())
  1638. {
  1639. detail::dpct_memcpy(q, to_ptr, from_ptr, size, direction);
  1640. }
  1641. static inline unsigned int select_device(unsigned int id)
  1642. {
  1643. dev_mgr::instance().select_device(id);
  1644. return id;
  1645. }
  1646. template <typename T>
  1647. T permute_sub_group_by_xor(sycl::sub_group g, T x, unsigned int mask,
  1648. unsigned int logical_sub_group_size = 32)
  1649. {
  1650. unsigned int id = g.get_local_linear_id();
  1651. unsigned int start_index =
  1652. id / logical_sub_group_size * logical_sub_group_size;
  1653. unsigned int target_offset = (id % logical_sub_group_size) ^ mask;
  1654. return sycl::select_from_group(g, x,
  1655. target_offset < logical_sub_group_size
  1656. ? start_index + target_offset
  1657. : id);
  1658. }
  1659. template <typename T>
  1660. sycl::vec<T, 4> extract_and_sign_or_zero_extend4(T val)
  1661. {
  1662. return sycl::vec<T, 1>(val)
  1663. .template as<sycl::vec<
  1664. std::conditional_t<std::is_signed_v<T>, int8_t, uint8_t>, 4>>()
  1665. .template convert<T>();
  1666. }
  1667. template <typename T1, typename T2>
  1668. using dot_product_acc_t =
  1669. std::conditional_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>,
  1670. uint32_t, int32_t>;
  1671. template <typename T1, typename T2, typename T3>
  1672. inline auto dp4a(T1 a, T2 b, T3 c)
  1673. {
  1674. dot_product_acc_t<T1, T2> res = c;
  1675. auto va = extract_and_sign_or_zero_extend4(a);
  1676. auto vb = extract_and_sign_or_zero_extend4(b);
  1677. res += va[0] * vb[0];
  1678. res += va[1] * vb[1];
  1679. res += va[2] * vb[2];
  1680. res += va[3] * vb[3];
  1681. return res;
  1682. }
  1683. struct sub_sat
  1684. {
  1685. template <typename T>
  1686. auto operator()(const T x, const T y) const
  1687. {
  1688. return sycl::sub_sat(x, y);
  1689. }
  1690. };
  1691. template <typename S, typename T>
  1692. inline T vectorized_min(T a, T b)
  1693. {
  1694. sycl::vec<T, 1> v0{a}, v1{b};
  1695. auto v2 = v0.template as<S>();
  1696. auto v3 = v1.template as<S>();
  1697. auto v4 = sycl::min(v2, v3);
  1698. v0 = v4.template as<sycl::vec<T, 1>>();
  1699. return v0;
  1700. }
  1701. inline float pow(const float a, const int b) { return sycl::pown(a, b); }
  1702. inline double pow(const double a, const int b) { return sycl::pown(a, b); }
  1703. inline float pow(const float a, const float b) { return sycl::pow(a, b); }
  1704. inline double pow(const double a, const double b) { return sycl::pow(a, b); }
  1705. template <typename T, typename U>
  1706. inline typename std::enable_if_t<std::is_floating_point_v<T>, T>
  1707. pow(const T a, const U b)
  1708. {
  1709. return sycl::pow(a, static_cast<T>(b));
  1710. }
  1711. template <typename T, typename U>
  1712. inline typename std::enable_if_t<!std::is_floating_point_v<T>, double>
  1713. pow(const T a, const U b)
  1714. {
  1715. return sycl::pow(static_cast<double>(a), static_cast<double>(b));
  1716. }
  1717. inline double min(const double a, const float b)
  1718. {
  1719. return sycl::fmin(a, static_cast<double>(b));
  1720. }
  1721. inline double min(const float a, const double b)
  1722. {
  1723. return sycl::fmin(static_cast<double>(a), b);
  1724. }
  1725. inline float min(const float a, const float b) { return sycl::fmin(a, b); }
  1726. inline double min(const double a, const double b) { return sycl::fmin(a, b); }
  1727. inline std::uint32_t min(const std::uint32_t a, const std::int32_t b)
  1728. {
  1729. return sycl::min(a, static_cast<std::uint32_t>(b));
  1730. }
  1731. inline std::uint32_t min(const std::int32_t a, const std::uint32_t b)
  1732. {
  1733. return sycl::min(static_cast<std::uint32_t>(a), b);
  1734. }
  1735. inline std::int32_t min(const std::int32_t a, const std::int32_t b)
  1736. {
  1737. return sycl::min(a, b);
  1738. }
  1739. inline std::uint32_t min(const std::uint32_t a, const std::uint32_t b)
  1740. {
  1741. return sycl::min(a, b);
  1742. }
  1743. inline std::uint64_t min(const std::uint64_t a, const std::int64_t b)
  1744. {
  1745. return sycl::min(a, static_cast<std::uint64_t>(b));
  1746. }
  1747. inline std::uint64_t min(const std::int64_t a, const std::uint64_t b)
  1748. {
  1749. return sycl::min(static_cast<std::uint64_t>(a), b);
  1750. }
  1751. inline std::int64_t min(const std::int64_t a, const std::int64_t b)
  1752. {
  1753. return sycl::min(a, b);
  1754. }
  1755. inline std::uint64_t min(const std::uint64_t a, const std::uint64_t b)
  1756. {
  1757. return sycl::min(a, b);
  1758. }
  1759. inline std::uint64_t min(const std::uint64_t a, const std::int32_t b)
  1760. {
  1761. return sycl::min(a, static_cast<std::uint64_t>(b));
  1762. }
  1763. inline std::uint64_t min(const std::int32_t a, const std::uint64_t b)
  1764. {
  1765. return sycl::min(static_cast<std::uint64_t>(a), b);
  1766. }
  1767. inline std::uint64_t min(const std::uint64_t a, const std::uint32_t b)
  1768. {
  1769. return sycl::min(a, static_cast<std::uint64_t>(b));
  1770. }
  1771. inline std::uint64_t min(const std::uint32_t a, const std::uint64_t b)
  1772. {
  1773. return sycl::min(static_cast<std::uint64_t>(a), b);
  1774. }
  1775. // max function overloads.
  1776. // For floating-point types, `float` or `double` arguments are acceptable.
  1777. // For integer types, `std::uint32_t`, `std::int32_t`, `std::uint64_t` or
  1778. // `std::int64_t` type arguments are acceptable.
  1779. inline double max(const double a, const float b)
  1780. {
  1781. return sycl::fmax(a, static_cast<double>(b));
  1782. }
  1783. inline double max(const float a, const double b)
  1784. {
  1785. return sycl::fmax(static_cast<double>(a), b);
  1786. }
  1787. inline float max(const float a, const float b) { return sycl::fmax(a, b); }
  1788. inline double max(const double a, const double b) { return sycl::fmax(a, b); }
  1789. inline std::uint32_t max(const std::uint32_t a, const std::int32_t b)
  1790. {
  1791. return sycl::max(a, static_cast<std::uint32_t>(b));
  1792. }
  1793. inline std::uint32_t max(const std::int32_t a, const std::uint32_t b)
  1794. {
  1795. return sycl::max(static_cast<std::uint32_t>(a), b);
  1796. }
  1797. inline std::int32_t max(const std::int32_t a, const std::int32_t b)
  1798. {
  1799. return sycl::max(a, b);
  1800. }
  1801. inline std::uint32_t max(const std::uint32_t a, const std::uint32_t b)
  1802. {
  1803. return sycl::max(a, b);
  1804. }
  1805. inline std::uint64_t max(const std::uint64_t a, const std::int64_t b)
  1806. {
  1807. return sycl::max(a, static_cast<std::uint64_t>(b));
  1808. }
  1809. inline std::uint64_t max(const std::int64_t a, const std::uint64_t b)
  1810. {
  1811. return sycl::max(static_cast<std::uint64_t>(a), b);
  1812. }
  1813. inline std::int64_t max(const std::int64_t a, const std::int64_t b)
  1814. {
  1815. return sycl::max(a, b);
  1816. }
  1817. inline std::uint64_t max(const std::uint64_t a, const std::uint64_t b)
  1818. {
  1819. return sycl::max(a, b);
  1820. }
  1821. inline std::uint64_t max(const std::uint64_t a, const std::int32_t b)
  1822. {
  1823. return sycl::max(a, static_cast<std::uint64_t>(b));
  1824. }
  1825. inline std::uint64_t max(const std::int32_t a, const std::uint64_t b)
  1826. {
  1827. return sycl::max(static_cast<std::uint64_t>(a), b);
  1828. }
  1829. inline std::uint64_t max(const std::uint64_t a, const std::uint32_t b)
  1830. {
  1831. return sycl::max(a, static_cast<std::uint64_t>(b));
  1832. }
  1833. inline std::uint64_t max(const std::uint32_t a, const std::uint64_t b)
  1834. {
  1835. return sycl::max(static_cast<std::uint64_t>(a), b);
  1836. }
  1837. inline void
  1838. has_capability_or_fail(const sycl::device &dev,
  1839. const std::initializer_list<sycl::aspect> &props)
  1840. {
  1841. for (const auto &it : props)
  1842. {
  1843. if (dev.has(it))
  1844. continue;
  1845. switch (it)
  1846. {
  1847. case sycl::aspect::fp64:
  1848. throw std::runtime_error("'double' is not supported in '" +
  1849. dev.get_info<sycl::info::device::name>() +
  1850. "' device");
  1851. break;
  1852. case sycl::aspect::fp16:
  1853. throw std::runtime_error("'half' is not supported in '" +
  1854. dev.get_info<sycl::info::device::name>() +
  1855. "' device");
  1856. break;
  1857. default:
  1858. #define __SYCL_ASPECT(ASPECT, ID) \
  1859. case sycl::aspect::ASPECT: \
  1860. return #ASPECT;
  1861. #define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
  1862. #define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
  1863. auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string
  1864. {
  1865. switch (AspectNum)
  1866. {
  1867. #include <sycl/info/aspects.def>
  1868. #include <sycl/info/aspects_deprecated.def>
  1869. default:
  1870. return "unknown aspect";
  1871. }
  1872. };
  1873. #undef __SYCL_ASPECT_DEPRECATED_ALIAS
  1874. #undef __SYCL_ASPECT_DEPRECATED
  1875. #undef __SYCL_ASPECT
  1876. throw std::runtime_error(
  1877. "'" + getAspectNameStr(it) + "' is not supported in '" +
  1878. dev.get_info<sycl::info::device::name>() + "' device");
  1879. }
  1880. break;
  1881. }
  1882. }
  1883. static inline unsigned int get_current_device_id()
  1884. {
  1885. return dev_mgr::instance().current_device_id();
  1886. }
  1887. static inline device_ext &get_current_device()
  1888. {
  1889. return dev_mgr::instance().current_device();
  1890. }
  1891. static inline sycl::queue &get_in_order_queue()
  1892. {
  1893. return dev_mgr::instance().current_device().in_order_queue();
  1894. }
  1895. static sycl::event
  1896. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1897. memcpy_direction direction,
  1898. const std::vector<sycl::event> &dep_events = {})
  1899. {
  1900. if (!size)
  1901. return sycl::event{};
  1902. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1903. GGML_UNUSED(direction);
  1904. }
  1905. // Get actual copy range and make sure it will not exceed range.
  1906. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1907. size_t pitch)
  1908. {
  1909. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1910. }
  1911. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1912. size_t pitch)
  1913. {
  1914. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1915. }
  1916. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1917. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1918. static inline std::vector<sycl::event>
  1919. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1920. sycl::range<3> to_range, sycl::range<3> from_range,
  1921. sycl::id<3> to_id, sycl::id<3> from_id,
  1922. sycl::range<3> size, memcpy_direction direction,
  1923. const std::vector<sycl::event> &dep_events = {})
  1924. {
  1925. // RAII for host pointer
  1926. class host_buffer
  1927. {
  1928. void *_buf;
  1929. size_t _size;
  1930. sycl::queue &_q;
  1931. const std::vector<sycl::event> &_deps; // free operation depends
  1932. public:
  1933. host_buffer(size_t size, sycl::queue &q,
  1934. const std::vector<sycl::event> &deps)
  1935. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1936. void *get_ptr() const { return _buf; }
  1937. size_t get_size() const { return _size; }
  1938. ~host_buffer()
  1939. {
  1940. if (_buf)
  1941. {
  1942. _q.submit([&](sycl::handler &cgh)
  1943. {
  1944. cgh.depends_on(_deps);
  1945. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1946. }
  1947. }
  1948. };
  1949. std::vector<sycl::event> event_list;
  1950. size_t to_slice = to_range.get(1) * to_range.get(0),
  1951. from_slice = from_range.get(1) * from_range.get(0);
  1952. unsigned char *to_surface =
  1953. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1954. const unsigned char *from_surface =
  1955. (const unsigned char *)from_ptr +
  1956. get_offset(from_id, from_slice, from_range.get(0));
  1957. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1958. {
  1959. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1960. direction, dep_events)};
  1961. }
  1962. direction = detail::deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1963. size_t size_slice = size.get(1) * size.get(0);
  1964. switch (direction)
  1965. {
  1966. case host_to_host:
  1967. for (size_t z = 0; z < size.get(2); ++z)
  1968. {
  1969. unsigned char *to_ptr = to_surface;
  1970. const unsigned char *from_ptr = from_surface;
  1971. if (to_range.get(0) == from_range.get(0) &&
  1972. to_range.get(0) == size.get(0))
  1973. {
  1974. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1975. direction, dep_events));
  1976. }
  1977. else
  1978. {
  1979. for (size_t y = 0; y < size.get(1); ++y)
  1980. {
  1981. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1982. direction, dep_events));
  1983. to_ptr += to_range.get(0);
  1984. from_ptr += from_range.get(0);
  1985. }
  1986. }
  1987. to_surface += to_slice;
  1988. from_surface += from_slice;
  1989. }
  1990. break;
  1991. case host_to_device:
  1992. {
  1993. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1994. event_list);
  1995. std::vector<sycl::event> host_events;
  1996. if (to_slice == size_slice)
  1997. {
  1998. // Copy host data to a temp host buffer with the shape of target.
  1999. host_events =
  2000. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  2001. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  2002. host_to_host, dep_events);
  2003. }
  2004. else
  2005. {
  2006. // Copy host data to a temp host buffer with the shape of target.
  2007. host_events = dpct_memcpy(
  2008. q, buf.get_ptr(), from_surface, to_range, from_range,
  2009. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  2010. // If has padding data, not sure whether it is useless. So fill temp
  2011. // buffer with it.
  2012. std::vector<sycl::event>{
  2013. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  2014. device_to_host, dep_events)});
  2015. }
  2016. // Copy from temp host buffer to device with only one submit.
  2017. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  2018. buf.get_size(), host_to_device,
  2019. host_events));
  2020. break;
  2021. }
  2022. case device_to_host:
  2023. {
  2024. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  2025. event_list);
  2026. // Copy from host temp buffer to host target with reshaping.
  2027. event_list = dpct_memcpy(
  2028. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  2029. sycl::id<3>(0, 0, 0), size, host_to_host,
  2030. // Copy from device to temp host buffer with only one submit.
  2031. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  2032. buf.get_size(),
  2033. device_to_host, dep_events)});
  2034. break;
  2035. }
  2036. case device_to_device:
  2037. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2038. {
  2039. cgh.depends_on(dep_events);
  2040. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  2041. size,
  2042. [=](sycl::id<3> id) {
  2043. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  2044. from_surface[get_offset(id, from_slice, from_range.get(0))];
  2045. }); }));
  2046. break;
  2047. default:
  2048. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2049. }
  2050. return event_list;
  2051. }
  2052. /// memcpy 2D/3D matrix specified by pitched_data.
  2053. static inline std::vector<sycl::event>
  2054. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  2055. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  2056. memcpy_direction direction = automatic)
  2057. {
  2058. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  2059. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  2060. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  2061. size, direction);
  2062. }
  2063. /// memcpy 2D matrix with pitch.
  2064. static inline std::vector<sycl::event>
  2065. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2066. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  2067. memcpy_direction direction = automatic)
  2068. {
  2069. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  2070. sycl::range<3>(from_pitch, y, 1),
  2071. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  2072. sycl::range<3>(x, y, 1), direction);
  2073. }
  2074. inline void gemm(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2075. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2076. const void *alpha, const void *a, library_data_t a_type,
  2077. int lda, const void *b, library_data_t b_type, int ldb,
  2078. const void *beta, void *c, library_data_t c_type, int ldc,
  2079. library_data_t scaling_type)
  2080. {
  2081. if (scaling_type == library_data_t::real_float &&
  2082. c_type == library_data_t::complex_float)
  2083. {
  2084. scaling_type = library_data_t::complex_float;
  2085. }
  2086. else if (scaling_type == library_data_t::real_double &&
  2087. c_type == library_data_t::complex_double)
  2088. {
  2089. scaling_type = library_data_t::complex_double;
  2090. }
  2091. std::uint64_t key =
  2092. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2093. switch (key)
  2094. {
  2095. case detail::get_type_combination_id(
  2096. library_data_t::real_float, library_data_t::real_float,
  2097. library_data_t::real_float, library_data_t::real_float):
  2098. {
  2099. detail::gemm_impl<float, float, float, float>(
  2100. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2101. break;
  2102. }
  2103. case detail::get_type_combination_id(
  2104. library_data_t::real_double, library_data_t::real_double,
  2105. library_data_t::real_double, library_data_t::real_double):
  2106. {
  2107. detail::gemm_impl<double, double, double, double>(
  2108. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2109. break;
  2110. }
  2111. case detail::get_type_combination_id(
  2112. library_data_t::complex_float, library_data_t::complex_float,
  2113. library_data_t::complex_float, library_data_t::complex_float):
  2114. {
  2115. detail::gemm_impl<std::complex<float>, std::complex<float>,
  2116. std::complex<float>, std::complex<float>>(
  2117. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2118. break;
  2119. }
  2120. case detail::get_type_combination_id(
  2121. library_data_t::complex_double, library_data_t::complex_double,
  2122. library_data_t::complex_double, library_data_t::complex_double):
  2123. {
  2124. detail::gemm_impl<std::complex<double>, std::complex<double>,
  2125. std::complex<double>, std::complex<double>>(
  2126. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2127. break;
  2128. }
  2129. case detail::get_type_combination_id(
  2130. library_data_t::real_half, library_data_t::real_half,
  2131. library_data_t::real_half, library_data_t::real_half):
  2132. {
  2133. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2134. sycl::half>(q, a_trans, b_trans, m, n, k, alpha, a,
  2135. lda, b, ldb, beta, c, ldc);
  2136. break;
  2137. }
  2138. #ifdef __INTEL_MKL__
  2139. case detail::get_type_combination_id(
  2140. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2141. library_data_t::real_float, library_data_t::real_float):
  2142. {
  2143. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2144. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda, b,
  2145. ldb, beta, c, ldc);
  2146. break;
  2147. }
  2148. case detail::get_type_combination_id(
  2149. library_data_t::real_half, library_data_t::real_half,
  2150. library_data_t::real_float, library_data_t::real_float):
  2151. {
  2152. detail::gemm_impl<sycl::half, sycl::half, float, float>(
  2153. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2154. break;
  2155. }
  2156. case detail::get_type_combination_id(
  2157. library_data_t::real_half, library_data_t::real_half,
  2158. library_data_t::real_half, library_data_t::real_float):
  2159. {
  2160. float alpha_value =
  2161. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2162. float beta_value =
  2163. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2164. sycl::half alpha_half(alpha_value);
  2165. sycl::half beta_half(beta_value);
  2166. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2167. sycl::half>(q, a_trans, b_trans, m, n, k, &alpha_half,
  2168. a, lda, b, ldb, &beta_half, c, ldc);
  2169. break;
  2170. }
  2171. case detail::get_type_combination_id(
  2172. library_data_t::real_int8, library_data_t::real_int8,
  2173. library_data_t::real_float, library_data_t::real_float):
  2174. {
  2175. detail::gemm_impl<std::int8_t, std::int8_t, float, float>(
  2176. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2177. break;
  2178. }
  2179. case detail::get_type_combination_id(
  2180. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2181. library_data_t::real_bfloat16, library_data_t::real_float):
  2182. {
  2183. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2184. oneapi::mkl::bfloat16, float>(
  2185. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2186. break;
  2187. }
  2188. case detail::get_type_combination_id(
  2189. library_data_t::real_int8, library_data_t::real_int8,
  2190. library_data_t::real_int32, library_data_t::real_int32):
  2191. {
  2192. float alpha_float =
  2193. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2194. float beta_float =
  2195. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2196. detail::gemm_impl<std::int8_t, std::int8_t, std::int32_t, float>(
  2197. q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float, c, ldc);
  2198. break;
  2199. }
  2200. #endif // __INTEL_MKL__
  2201. default:
  2202. throw std::runtime_error("the combination of data type is unsupported");
  2203. }
  2204. } // gemm()
  2205. /// Computes a batch of matrix-matrix product with general matrices.
  2206. /// \param [in] q The queue where the routine should be executed.
  2207. /// \param [in] a_trans Specifies the operation applied to A.
  2208. /// \param [in] b_trans Specifies the operation applied to B.
  2209. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2210. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2211. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2212. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2213. /// \param [in] a Input matrix A.
  2214. /// \param [in] a_type Data type of the matrix A.
  2215. /// \param [in] lda Leading dimension of A.
  2216. /// \param [in] b Input matrix B.
  2217. /// \param [in] b_type Data type of the matrix B.
  2218. /// \param [in] ldb Leading dimension of B.
  2219. /// \param [in] beta Scaling factor for matrix C.
  2220. /// \param [in, out] c Input/Output matrix C.
  2221. /// \param [in] c_type Data type of the matrix C.
  2222. /// \param [in] ldc Leading dimension of C.
  2223. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2224. /// \param [in] scaling_type Data type of the scaling factors.
  2225. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2226. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2227. const void *alpha, const void *a[],
  2228. library_data_t a_type, int lda, const void *b[],
  2229. library_data_t b_type, int ldb, const void *beta,
  2230. void *c[], library_data_t c_type, int ldc,
  2231. int batch_size, library_data_t scaling_type)
  2232. {
  2233. if (scaling_type == library_data_t::real_float &&
  2234. c_type == library_data_t::complex_float)
  2235. {
  2236. scaling_type = library_data_t::complex_float;
  2237. }
  2238. else if (scaling_type == library_data_t::real_double &&
  2239. c_type == library_data_t::complex_double)
  2240. {
  2241. scaling_type = library_data_t::complex_double;
  2242. }
  2243. std::uint64_t key =
  2244. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2245. switch (key)
  2246. {
  2247. case detail::get_type_combination_id(
  2248. library_data_t::real_float, library_data_t::real_float,
  2249. library_data_t::real_float, library_data_t::real_float):
  2250. {
  2251. detail::gemm_batch_impl<float, float, float, float>(
  2252. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2253. batch_size);
  2254. break;
  2255. }
  2256. case detail::get_type_combination_id(
  2257. library_data_t::real_double, library_data_t::real_double,
  2258. library_data_t::real_double, library_data_t::real_double):
  2259. {
  2260. detail::gemm_batch_impl<double, double, double, double>(
  2261. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2262. batch_size);
  2263. break;
  2264. }
  2265. case detail::get_type_combination_id(
  2266. library_data_t::complex_float, library_data_t::complex_float,
  2267. library_data_t::complex_float, library_data_t::complex_float):
  2268. {
  2269. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2270. std::complex<float>, std::complex<float>>(
  2271. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2272. batch_size);
  2273. break;
  2274. }
  2275. case detail::get_type_combination_id(
  2276. library_data_t::complex_double, library_data_t::complex_double,
  2277. library_data_t::complex_double, library_data_t::complex_double):
  2278. {
  2279. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2280. std::complex<double>, std::complex<double>>(
  2281. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2282. batch_size);
  2283. break;
  2284. }
  2285. case detail::get_type_combination_id(
  2286. library_data_t::real_half, library_data_t::real_half,
  2287. library_data_t::real_half, library_data_t::real_half):
  2288. {
  2289. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2290. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2291. a, lda, b, ldb, beta, c, ldc,
  2292. batch_size);
  2293. break;
  2294. }
  2295. #ifdef __INTEL_MKL__
  2296. case detail::get_type_combination_id(
  2297. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2298. library_data_t::real_bfloat16, library_data_t::real_float):
  2299. {
  2300. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2301. oneapi::mkl::bfloat16, float>(
  2302. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2303. batch_size);
  2304. break;
  2305. }
  2306. case detail::get_type_combination_id(
  2307. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2308. library_data_t::real_float, library_data_t::real_float):
  2309. {
  2310. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2311. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2312. b, ldb, beta, c, ldc, batch_size);
  2313. break;
  2314. }
  2315. case detail::get_type_combination_id(
  2316. library_data_t::real_int8, library_data_t::real_int8,
  2317. library_data_t::real_int32, library_data_t::real_int32):
  2318. {
  2319. float alpha_float =
  2320. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2321. float beta_float =
  2322. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2323. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2324. float>(q, a_trans, b_trans, m, n, k, &alpha_float,
  2325. a, lda, b, ldb, &beta_float, c, ldc,
  2326. batch_size);
  2327. break;
  2328. }
  2329. case detail::get_type_combination_id(
  2330. library_data_t::real_int8, library_data_t::real_int8,
  2331. library_data_t::real_float, library_data_t::real_float):
  2332. {
  2333. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2334. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2335. batch_size);
  2336. break;
  2337. }
  2338. case detail::get_type_combination_id(
  2339. library_data_t::real_half, library_data_t::real_half,
  2340. library_data_t::real_float, library_data_t::real_float):
  2341. {
  2342. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2343. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2344. batch_size);
  2345. break;
  2346. }
  2347. #endif
  2348. case detail::get_type_combination_id(
  2349. library_data_t::real_half, library_data_t::real_half,
  2350. library_data_t::real_half, library_data_t::real_float):
  2351. {
  2352. float alpha_value =
  2353. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2354. float beta_value =
  2355. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2356. sycl::half alpha_half(alpha_value);
  2357. sycl::half beta_half(beta_value);
  2358. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2359. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half, c, ldc,
  2360. batch_size);
  2361. break;
  2362. }
  2363. default:
  2364. throw std::runtime_error("the combination of data type is unsupported");
  2365. }
  2366. }
  2367. /// Computes a batch of matrix-matrix product with general matrices.
  2368. /// \param [in] q The queue where the routine should be executed.
  2369. /// \param [in] a_trans Specifies the operation applied to A.
  2370. /// \param [in] b_trans Specifies the operation applied to B.
  2371. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2372. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2373. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2374. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2375. /// \param [in] a Input matrix A.
  2376. /// \param [in] a_type Data type of the matrix A.
  2377. /// \param [in] lda Leading dimension of A.
  2378. /// \param [in] stride_a Stride between the different A matrices.
  2379. /// \param [in] b Input matrix B.
  2380. /// \param [in] b_type Data type of the matrix B.
  2381. /// \param [in] ldb Leading dimension of B.
  2382. /// \param [in] stride_b Stride between the different B matrices.
  2383. /// \param [in] beta Scaling factor for matrix C.
  2384. /// \param [in, out] c Input/Output matrix C.
  2385. /// \param [in] c_type Data type of the matrix C.
  2386. /// \param [in] ldc Leading dimension of C.
  2387. /// \param [in] stride_c Stride between the different C matrices.
  2388. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2389. /// \param [in] scaling_type Data type of the scaling factors.
  2390. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2391. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2392. const void *alpha, const void *a, library_data_t a_type,
  2393. int lda, long long int stride_a, const void *b,
  2394. library_data_t b_type, int ldb, long long int stride_b,
  2395. const void *beta, void *c, library_data_t c_type,
  2396. int ldc, long long int stride_c, int batch_size,
  2397. library_data_t scaling_type)
  2398. {
  2399. if (scaling_type == library_data_t::real_float &&
  2400. c_type == library_data_t::complex_float)
  2401. {
  2402. scaling_type = library_data_t::complex_float;
  2403. }
  2404. else if (scaling_type == library_data_t::real_double &&
  2405. c_type == library_data_t::complex_double)
  2406. {
  2407. scaling_type = library_data_t::complex_double;
  2408. }
  2409. std::uint64_t key =
  2410. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2411. switch (key)
  2412. {
  2413. case detail::get_type_combination_id(
  2414. library_data_t::real_float, library_data_t::real_float,
  2415. library_data_t::real_float, library_data_t::real_float):
  2416. {
  2417. detail::gemm_batch_impl<float, float, float, float>(
  2418. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2419. beta, c, ldc, stride_c, batch_size);
  2420. break;
  2421. }
  2422. case detail::get_type_combination_id(
  2423. library_data_t::real_double, library_data_t::real_double,
  2424. library_data_t::real_double, library_data_t::real_double):
  2425. {
  2426. detail::gemm_batch_impl<double, double, double, double>(
  2427. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2428. beta, c, ldc, stride_c, batch_size);
  2429. break;
  2430. }
  2431. case detail::get_type_combination_id(
  2432. library_data_t::complex_float, library_data_t::complex_float,
  2433. library_data_t::complex_float, library_data_t::complex_float):
  2434. {
  2435. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2436. std::complex<float>, std::complex<float>>(
  2437. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2438. beta, c, ldc, stride_c, batch_size);
  2439. break;
  2440. }
  2441. case detail::get_type_combination_id(
  2442. library_data_t::complex_double, library_data_t::complex_double,
  2443. library_data_t::complex_double, library_data_t::complex_double):
  2444. {
  2445. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2446. std::complex<double>, std::complex<double>>(
  2447. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2448. beta, c, ldc, stride_c, batch_size);
  2449. break;
  2450. }
  2451. case detail::get_type_combination_id(
  2452. library_data_t::real_half, library_data_t::real_half,
  2453. library_data_t::real_half, library_data_t::real_half):
  2454. {
  2455. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2456. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2457. a, lda, stride_a, b, ldb, stride_b,
  2458. beta, c, ldc, stride_c, batch_size);
  2459. break;
  2460. }
  2461. #ifdef __INTEL_MKL__
  2462. case detail::get_type_combination_id(
  2463. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2464. library_data_t::real_bfloat16, library_data_t::real_float):
  2465. {
  2466. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2467. oneapi::mkl::bfloat16, float>(
  2468. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2469. beta, c, ldc, stride_c, batch_size);
  2470. break;
  2471. }
  2472. case detail::get_type_combination_id(
  2473. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2474. library_data_t::real_float, library_data_t::real_float):
  2475. {
  2476. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2477. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2478. stride_a, b, ldb, stride_b, beta, c, ldc,
  2479. stride_c, batch_size);
  2480. break;
  2481. }
  2482. case detail::get_type_combination_id(
  2483. library_data_t::real_int8, library_data_t::real_int8,
  2484. library_data_t::real_int32, library_data_t::real_int32):
  2485. {
  2486. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2487. std::int32_t>(q, a_trans, b_trans, m, n, k, alpha,
  2488. a, lda, stride_a, b, ldb, stride_b,
  2489. beta, c, ldc, stride_c, batch_size);
  2490. break;
  2491. }
  2492. case detail::get_type_combination_id(
  2493. library_data_t::real_int8, library_data_t::real_int8,
  2494. library_data_t::real_float, library_data_t::real_float):
  2495. {
  2496. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2497. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2498. beta, c, ldc, stride_c, batch_size);
  2499. break;
  2500. }
  2501. case detail::get_type_combination_id(
  2502. library_data_t::real_half, library_data_t::real_half,
  2503. library_data_t::real_float, library_data_t::real_float):
  2504. {
  2505. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2506. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2507. beta, c, ldc, stride_c, batch_size);
  2508. break;
  2509. }
  2510. #endif
  2511. case detail::get_type_combination_id(
  2512. library_data_t::real_half, library_data_t::real_half,
  2513. library_data_t::real_half, library_data_t::real_float):
  2514. {
  2515. float alpha_value =
  2516. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2517. float beta_value =
  2518. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2519. sycl::half alpha_half(alpha_value);
  2520. sycl::half beta_half(beta_value);
  2521. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2522. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, stride_a, b, ldb, stride_b,
  2523. &beta_half, c, ldc, stride_c, batch_size);
  2524. break;
  2525. }
  2526. default:
  2527. throw std::runtime_error("the combination of data type is unsupported");
  2528. }
  2529. }
  2530. static inline void
  2531. async_dpct_memcpy(void *to_ptr, size_t to_pitch, const void *from_ptr,
  2532. size_t from_pitch, size_t x, size_t y,
  2533. memcpy_direction direction = automatic,
  2534. sycl::queue &q = get_default_queue())
  2535. {
  2536. detail::dpct_memcpy(q, to_ptr, from_ptr, to_pitch, from_pitch, x, y,
  2537. direction);
  2538. }
  2539. using err0 = detail::generic_error_type<struct err0_tag, int>;
  2540. using err1 = detail::generic_error_type<struct err1_tag, int>;
  2541. static inline void dpct_free(void *ptr, sycl::queue &q = get_default_queue()) {
  2542. detail::dpct_free(ptr, q);
  2543. }
  2544. /// dpct accessor used as device function parameter.
  2545. template <class T, memory_region Memory, size_t Dimension> class accessor;
  2546. template <class T, memory_region Memory> class accessor<T, Memory, 3> {
  2547. public:
  2548. using memory_t = detail::memory_traits<Memory, T>;
  2549. using element_t = typename memory_t::element_t;
  2550. using pointer_t = typename memory_t::pointer_t;
  2551. using accessor_t = typename memory_t::template accessor_t<3>;
  2552. accessor(pointer_t data, const sycl::range<3> &in_range)
  2553. : _data(data), _range(in_range) {}
  2554. template <memory_region M = Memory>
  2555. accessor(typename std::enable_if<M != local, const accessor_t>::type &acc)
  2556. : accessor(acc, acc.get_range()) {}
  2557. accessor(const accessor_t &acc, const sycl::range<3> &in_range)
  2558. : accessor(acc.get_pointer(), in_range) {}
  2559. accessor<T, Memory, 2> operator[](size_t index) const {
  2560. sycl::range<2> sub(_range.get(1), _range.get(2));
  2561. return accessor<T, Memory, 2>(_data + index * sub.size(), sub);
  2562. }
  2563. pointer_t get_ptr() const { return _data; }
  2564. private:
  2565. pointer_t _data;
  2566. sycl::range<3> _range;
  2567. };
  2568. template <class T, memory_region Memory> class accessor<T, Memory, 2> {
  2569. public:
  2570. using memory_t = detail::memory_traits<Memory, T>;
  2571. using element_t = typename memory_t::element_t;
  2572. using pointer_t = typename memory_t::pointer_t;
  2573. using accessor_t = typename memory_t::template accessor_t<2>;
  2574. accessor(pointer_t data, const sycl::range<2> &in_range)
  2575. : _data(data), _range(in_range) {}
  2576. template <memory_region M = Memory>
  2577. accessor(typename std::enable_if<M != local, const accessor_t>::type &acc)
  2578. : accessor(acc, acc.get_range()) {}
  2579. accessor(const accessor_t &acc, const sycl::range<2> &in_range)
  2580. : accessor(acc.get_pointer(), in_range) {}
  2581. pointer_t operator[](size_t index) const {
  2582. return _data + _range.get(1) * index;
  2583. }
  2584. pointer_t get_ptr() const { return _data; }
  2585. private:
  2586. pointer_t _data;
  2587. sycl::range<2> _range;
  2588. };
  2589. namespace detail {
  2590. /// Device variable with address space of shared, global or constant.
  2591. template <class T, memory_region Memory, size_t Dimension> class device_memory {
  2592. public:
  2593. using accessor_t =
  2594. typename detail::memory_traits<Memory,
  2595. T>::template accessor_t<Dimension>;
  2596. using value_t = typename detail::memory_traits<Memory, T>::value_t;
  2597. using dpct_accessor_t = dpct::accessor<T, Memory, Dimension>;
  2598. device_memory() : device_memory(sycl::range<Dimension>(1)) {}
  2599. /// Constructor of 1-D array with initializer list
  2600. device_memory(const sycl::range<Dimension> &in_range,
  2601. std::initializer_list<value_t> &&init_list)
  2602. : device_memory(in_range) {
  2603. assert(init_list.size() <= in_range.size());
  2604. _host_ptr = (value_t *)std::malloc(_size);
  2605. std::memset(_host_ptr, 0, _size);
  2606. std::memcpy(_host_ptr, init_list.begin(), init_list.size() * sizeof(T));
  2607. }
  2608. /// Constructor of 2-D array with initializer list
  2609. template <size_t D = Dimension>
  2610. device_memory(
  2611. const typename std::enable_if<D == 2, sycl::range<2>>::type &in_range,
  2612. std::initializer_list<std::initializer_list<value_t>> &&init_list)
  2613. : device_memory(in_range) {
  2614. assert(init_list.size() <= in_range[0]);
  2615. _host_ptr = (value_t *)std::malloc(_size);
  2616. std::memset(_host_ptr, 0, _size);
  2617. auto tmp_data = _host_ptr;
  2618. for (auto sub_list : init_list) {
  2619. assert(sub_list.size() <= in_range[1]);
  2620. std::memcpy(tmp_data, sub_list.begin(),
  2621. sub_list.size() * sizeof(T));
  2622. tmp_data += in_range[1];
  2623. }
  2624. }
  2625. /// Constructor with range
  2626. device_memory(const sycl::range<Dimension> &range_in)
  2627. : _size(range_in.size() * sizeof(T)), _range(range_in),
  2628. _reference(false), _host_ptr(nullptr), _device_ptr(nullptr) {
  2629. static_assert(
  2630. (Memory == global) || (Memory == constant) || (Memory == shared),
  2631. "device memory region should be global, constant or shared");
  2632. // Make sure that singleton class mem_mgr and dev_mgr will destruct
  2633. // later than this.
  2634. detail::mem_mgr::instance();
  2635. dev_mgr::instance();
  2636. }
  2637. /// Constructor with range
  2638. template <class... Args>
  2639. device_memory(Args... Arguments)
  2640. : device_memory(sycl::range<Dimension>(Arguments...)) {}
  2641. ~device_memory() {
  2642. if (_device_ptr && !_reference)
  2643. dpct::dpct_free(_device_ptr);
  2644. if (_host_ptr)
  2645. std::free(_host_ptr);
  2646. }
  2647. /// Allocate memory with default queue, and init memory if has initial
  2648. /// value.
  2649. void init() { init(dpct::get_default_queue()); }
  2650. /// Allocate memory with specified queue, and init memory if has initial
  2651. /// value.
  2652. void init(sycl::queue &q) {
  2653. if (_device_ptr)
  2654. return;
  2655. if (!_size)
  2656. return;
  2657. allocate_device(q);
  2658. if (_host_ptr)
  2659. detail::dpct_memcpy(q, _device_ptr, _host_ptr, _size,
  2660. host_to_device);
  2661. }
  2662. /// The variable is assigned to a device pointer.
  2663. void assign(value_t *src, size_t size) {
  2664. this->~device_memory();
  2665. new (this) device_memory(src, size);
  2666. }
  2667. /// Get memory pointer of the memory object, which is virtual pointer when
  2668. /// usm is not used, and device pointer when usm is used.
  2669. value_t *get_ptr() { return get_ptr(get_default_queue()); }
  2670. /// Get memory pointer of the memory object, which is virtual pointer when
  2671. /// usm is not used, and device pointer when usm is used.
  2672. value_t *get_ptr(sycl::queue &q) {
  2673. init(q);
  2674. return _device_ptr;
  2675. }
  2676. /// Get the device memory object size in bytes.
  2677. size_t get_size() { return _size; }
  2678. template <size_t D = Dimension>
  2679. typename std::enable_if<D == 1, T>::type &operator[](size_t index) {
  2680. init();
  2681. return _device_ptr[index];
  2682. }
  2683. /// Get dpct::accessor with dimension info for the device memory object
  2684. /// when usm is used and dimension is greater than 1.
  2685. template <size_t D = Dimension>
  2686. typename std::enable_if<D != 1, dpct_accessor_t>::type
  2687. get_access(sycl::handler &cgh) {
  2688. return dpct_accessor_t((T *)_device_ptr, _range);
  2689. }
  2690. private:
  2691. device_memory(value_t *memory_ptr, size_t size)
  2692. : _size(size), _range(size / sizeof(T)), _reference(true),
  2693. _device_ptr(memory_ptr) {}
  2694. void allocate_device(sycl::queue &q) {
  2695. #ifndef DPCT_USM_LEVEL_NONE
  2696. if (Memory == shared) {
  2697. _device_ptr = (value_t *)sycl::malloc_shared(_size, q.get_device(),
  2698. q.get_context());
  2699. return;
  2700. }
  2701. #ifdef SYCL_EXT_ONEAPI_USM_DEVICE_READ_ONLY
  2702. if (Memory == constant) {
  2703. _device_ptr = (value_t *)sycl::malloc_device(
  2704. _size, q.get_device(), q.get_context(),
  2705. sycl::ext::oneapi::property::usm::device_read_only());
  2706. return;
  2707. }
  2708. #endif
  2709. #endif
  2710. _device_ptr = (value_t *)detail::dpct_malloc(_size, q);
  2711. }
  2712. size_t _size;
  2713. sycl::range<Dimension> _range;
  2714. bool _reference;
  2715. value_t *_host_ptr;
  2716. value_t *_device_ptr;
  2717. };
  2718. template <class T, memory_region Memory>
  2719. class device_memory<T, Memory, 0> : public device_memory<T, Memory, 1> {
  2720. public:
  2721. using base = device_memory<T, Memory, 1>;
  2722. using value_t = typename base::value_t;
  2723. using accessor_t =
  2724. typename detail::memory_traits<Memory, T>::template accessor_t<0>;
  2725. /// Constructor with initial value.
  2726. device_memory(const value_t &val) : base(sycl::range<1>(1), {val}) {}
  2727. /// Default constructor
  2728. device_memory() : base(1) {}
  2729. };
  2730. } // namespace detail
  2731. template <class T, size_t Dimension>
  2732. using global_memory = detail::device_memory<T, global, Dimension>;
  2733. template <class T, size_t Dimension>
  2734. using constant_memory = detail::device_memory<T, constant, Dimension>;
  2735. template <class T, size_t Dimension>
  2736. using shared_memory = detail::device_memory<T, shared, Dimension>;
  2737. } // COPY from DPCT head files
  2738. #define GGML_COMMON_DECL_SYCL
  2739. #define GGML_COMMON_IMPL_SYCL
  2740. #include "ggml-common.h"
  2741. static int g_ggml_sycl_debug=0;
  2742. #define GGML_SYCL_DEBUG(...) do{if(g_ggml_sycl_debug) fprintf(stderr, __VA_ARGS__);}while(0)
  2743. #define CHECK_TRY_ERROR(expr) \
  2744. [&]() { \
  2745. try { \
  2746. expr; \
  2747. return dpct::success; \
  2748. } catch (std::exception const &e) { \
  2749. std::cerr << e.what()<< "\nException caught at file:" << __FILE__ \
  2750. << ", line:" << __LINE__ <<", func:"<<__func__<< std::endl; \
  2751. return dpct::default_error; \
  2752. } \
  2753. }()
  2754. // #define DEBUG_SYCL_MALLOC
  2755. static int g_work_group_size = 0;
  2756. // typedef sycl::half ggml_fp16_t;
  2757. #define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
  2758. #define VER_4VEC 610 //todo for hardward optimize.
  2759. #define VER_GEN9 700 //todo for hardward optimize.
  2760. #define VER_GEN12 1000000 //todo for hardward optimize.
  2761. #define VER_GEN13 (VER_GEN12 + 1030) //todo for hardward optimize.
  2762. #define GGML_SYCL_MAX_NODES 8192 //TODO: adapt to hardwares
  2763. //define for XMX in Intel GPU
  2764. //TODO: currently, it's not used for XMX really.
  2765. #define SYCL_USE_XMX
  2766. // max batch size to use MMQ kernels when tensor cores are available
  2767. #define XMX_MAX_BATCH_SIZE 32
  2768. #if defined(_MSC_VER)
  2769. #pragma warning(disable: 4244 4267) // possible loss of data
  2770. #endif
  2771. // dmmv = dequantize_mul_mat_vec
  2772. #ifndef GGML_SYCL_DMMV_X
  2773. #define GGML_SYCL_DMMV_X 32
  2774. #endif
  2775. #ifndef GGML_SYCL_MMV_Y
  2776. #define GGML_SYCL_MMV_Y 1
  2777. #endif
  2778. enum ggml_sycl_backend_gpu_mode {
  2779. SYCL_UNSET_GPU_MODE = -1,
  2780. SYCL_SINGLE_GPU_MODE = 0,
  2781. SYCL_MUL_GPU_MODE
  2782. };
  2783. static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  2784. static void crash(){
  2785. int *ptr = NULL;
  2786. *ptr = 0;
  2787. }
  2788. static void ggml_sycl_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  2789. fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
  2790. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  2791. GGML_ASSERT(!"SYCL error");
  2792. }
  2793. #define SYCL_CHECK(err) do { \
  2794. auto err_ = (err); if (err_ != 0) ggml_sycl_error( \
  2795. #err, __func__, __FILE__, __LINE__, \
  2796. "Meet error in this line code!"); \
  2797. } while (0)
  2798. #if DPCT_COMPAT_RT_VERSION >= 11100
  2799. #define GGML_SYCL_ASSUME(x) __builtin_assume(x)
  2800. #else
  2801. #define GGML_SYCL_ASSUME(x)
  2802. #endif // DPCT_COMPAT_RT_VERSION >= 11100
  2803. #ifdef GGML_SYCL_F16
  2804. typedef sycl::half dfloat; // dequantize float
  2805. typedef sycl::half2 dfloat2;
  2806. #else
  2807. typedef float dfloat; // dequantize float
  2808. typedef sycl::float2 dfloat2;
  2809. #endif //GGML_SYCL_F16
  2810. #define MMVQ_MAX_BATCH_SIZE 8
  2811. static const int8_t kvalues_iq4nl[16]={-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2812. bool ggml_sycl_loaded(void);
  2813. void * ggml_sycl_host_malloc(size_t size);
  2814. void ggml_sycl_host_free(void * ptr);
  2815. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  2816. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  2817. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor);
  2818. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  2819. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  2820. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  2821. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  2822. void ggml_sycl_set_main_device(int main_device);
  2823. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  2824. void ggml_sycl_set_scratch_size(size_t scratch_size);
  2825. void ggml_sycl_free_scratch(void);
  2826. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  2827. bool ggml_backend_is_sycl(ggml_backend_t backend);
  2828. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  2829. int get_main_device();
  2830. void print_ggml_tensor(const char*name, struct ggml_tensor *src);
  2831. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt);
  2832. void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
  2833. const void *ptr_src, size_t size) {
  2834. char *host_buf = (char *)malloc(size);
  2835. q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
  2836. q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
  2837. free(host_buf);
  2838. }
  2839. static __dpct_inline__ int get_int_from_int8(const int8_t *x8, const int &i32) {
  2840. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2841. int x32 = 0;
  2842. x32 |= x16[0] << 0;
  2843. x32 |= x16[1] << 16;
  2844. return x32;
  2845. }
  2846. static __dpct_inline__ int get_int_from_uint8(const uint8_t *x8,
  2847. const int &i32) {
  2848. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2849. int x32 = 0;
  2850. x32 |= x16[0] << 0;
  2851. x32 |= x16[1] << 16;
  2852. return x32;
  2853. }
  2854. static __dpct_inline__ int get_int_from_int8_aligned(const int8_t *x8,
  2855. const int &i32) {
  2856. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2857. }
  2858. static __dpct_inline__ int get_int_from_uint8_aligned(const uint8_t *x8,
  2859. const int &i32) {
  2860. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2861. }
  2862. template <typename T>
  2863. using to_t_sycl_t = void (*)(const void *__restrict__ x, T *__restrict__ y,
  2864. int k, dpct::queue_ptr stream);
  2865. typedef to_t_sycl_t<float> to_fp32_sycl_t;
  2866. typedef to_t_sycl_t<sycl::half> to_fp16_sycl_t;
  2867. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  2868. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  2869. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  2870. typedef void (*ggml_sycl_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  2871. typedef void (*ggml_sycl_op_mul_mat_t)(
  2872. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2873. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2874. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2875. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2876. const dpct::queue_ptr &stream);
  2877. typedef void (*ggml_sycl_op_flatten_t)(const ggml_tensor *src0,
  2878. const ggml_tensor *src1,
  2879. ggml_tensor *dst, const float *src0_dd,
  2880. const float *src1_dd, float *dst_dd,
  2881. const dpct::queue_ptr &main_stream);
  2882. typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  2883. typedef void (*allocate_tiles_sycl_t)(int **x_ql, sycl::half2 **x_dm,
  2884. int **x_qh, int **x_sc);
  2885. typedef void (*load_tiles_sycl_t)(const void *__restrict__ vx,
  2886. int *__restrict__ x_ql,
  2887. sycl::half2 *__restrict__ x_dm,
  2888. int *__restrict__ x_qh,
  2889. int *__restrict__ x_sc, const int &i_offset,
  2890. const int &i_max, const int &k,
  2891. const int &blocks_per_row);
  2892. typedef float (*vec_dot_q_mul_mat_sycl_t)(
  2893. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  2894. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  2895. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ms,
  2896. const int &i, const int &j, const int &k);
  2897. #define WARP_SIZE 32
  2898. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  2899. #define SYCL_GELU_BLOCK_SIZE 256
  2900. #define SYCL_SILU_BLOCK_SIZE 256
  2901. #define SYCL_TANH_BLOCK_SIZE 256
  2902. #define SYCL_RELU_BLOCK_SIZE 256
  2903. #define SYCL_HARDSIGMOID_BLOCK_SIZE 256
  2904. #define SYCL_HARDSWISH_BLOCK_SIZE 256
  2905. #define SYCL_SQR_BLOCK_SIZE 256
  2906. #define SYCL_CPY_BLOCK_SIZE 32
  2907. #define SYCL_SCALE_BLOCK_SIZE 256
  2908. #define SYCL_CLAMP_BLOCK_SIZE 256
  2909. #define SYCL_ROPE_BLOCK_SIZE 256
  2910. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  2911. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  2912. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  2913. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  2914. #define SYCL_UPSCALE_BLOCK_SIZE 256
  2915. #define SYCL_CONCAT_BLOCK_SIZE 256
  2916. #define SYCL_PAD_BLOCK_SIZE 256
  2917. #define SYCL_ACC_BLOCK_SIZE 256
  2918. #define SYCL_IM2COL_BLOCK_SIZE 256
  2919. #define SYCL_POOL2D_BLOCK_SIZE 256
  2920. // dmmv = dequantize_mul_mat_vec
  2921. #ifndef GGML_SYCL_DMMV_X
  2922. #define GGML_SYCL_DMMV_X 32
  2923. #endif
  2924. #ifndef GGML_SYCL_MMV_Y
  2925. #define GGML_SYCL_MMV_Y 1
  2926. #endif
  2927. #ifndef K_QUANTS_PER_ITERATION
  2928. #define K_QUANTS_PER_ITERATION 2
  2929. #else
  2930. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  2931. #endif
  2932. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  2933. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  2934. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  2935. #define MUL_MAT_SRC1_COL_STRIDE 128
  2936. #define MAX_STREAMS 8
  2937. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {{0}};
  2938. struct ggml_tensor_extra_gpu {
  2939. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  2940. dpct::event_ptr
  2941. events[GGML_SYCL_MAX_DEVICES]
  2942. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  2943. };
  2944. class sycl_gpu_mgr {
  2945. public:
  2946. std::vector<int> gpus;
  2947. std::vector<sycl::device> devices;
  2948. sycl::queue *first_queue;
  2949. sycl::context co_ctx;
  2950. int max_compute_units = 0;
  2951. int work_group_size = 0;
  2952. std::string gpus_list = "";
  2953. /*
  2954. Use all GPUs with same top max compute units
  2955. */
  2956. sycl_gpu_mgr() {
  2957. detect_sycl_gpu_list_with_max_cu();
  2958. get_allow_gpus();
  2959. create_context_with_gpus();
  2960. }
  2961. /*
  2962. Only use the assigned GPU
  2963. */
  2964. sycl_gpu_mgr(int main_gpu_id) {
  2965. sycl::device device = dpct::dev_mgr::instance().get_device(main_gpu_id);
  2966. dpct::device_info prop;
  2967. dpct::get_device_info(prop, device);
  2968. gpus.push_back(main_gpu_id);
  2969. devices.push_back(device);
  2970. work_group_size = prop.get_max_work_group_size();
  2971. max_compute_units = prop.get_max_compute_units();
  2972. get_allow_gpus();
  2973. create_context_with_gpus();
  2974. }
  2975. void create_context_with_gpus() {
  2976. sycl::context ctx = sycl::context(devices);
  2977. assert(gpus.size() > 0);
  2978. first_queue = dpct::get_current_device().create_queue(ctx, devices[0]);
  2979. co_ctx = first_queue->get_context();
  2980. }
  2981. sycl::context &get_co_ctx() { return co_ctx; }
  2982. void get_allow_gpus() {
  2983. gpus_list = "";
  2984. for (size_t i = 0; i < gpus.size(); ++i) {
  2985. gpus_list += std::to_string(gpus[i]);
  2986. gpus_list += ",";
  2987. }
  2988. if (gpus_list.length() > 1) {
  2989. gpus_list.pop_back();
  2990. }
  2991. }
  2992. bool is_allowed_gpu(int device_id) {
  2993. return std::find(gpus.begin(), gpus.end(), device_id) != gpus.end();
  2994. }
  2995. void detect_sycl_gpu_list_with_max_cu() try {
  2996. int device_count = dpct::dev_mgr::instance().device_count();
  2997. for (int id = 0; id < device_count; id++) {
  2998. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  2999. if (!device.is_gpu())
  3000. continue;
  3001. dpct::device_info prop;
  3002. dpct::get_device_info(prop, device);
  3003. if (max_compute_units < prop.get_max_compute_units())
  3004. max_compute_units = prop.get_max_compute_units();
  3005. }
  3006. for (int id = 0; id < device_count; id++) {
  3007. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3008. if (!device.is_gpu())
  3009. continue;
  3010. dpct::device_info prop;
  3011. dpct::get_device_info(prop, device);
  3012. if (max_compute_units == prop.get_max_compute_units() &&
  3013. is_ext_oneapi_device(device)) {
  3014. gpus.push_back(id);
  3015. devices.push_back(device);
  3016. work_group_size = prop.get_max_work_group_size();
  3017. }
  3018. }
  3019. return;
  3020. } catch (sycl::exception const &exc) {
  3021. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3022. << ", line:" << __LINE__ << std::endl;
  3023. std::exit(1);
  3024. }
  3025. int get_gpu_count() { return (int)gpus.size(); }
  3026. int get_index(int id) {
  3027. for (int i = 0; i < (int)gpus.size(); i++) {
  3028. if (gpus[i] == id)
  3029. return i;
  3030. }
  3031. printf("miss to get device index by id=%d\n", id);
  3032. GGML_ASSERT(false);
  3033. }
  3034. int get_next_index(int id) {
  3035. int cur_index = get_index(id);
  3036. for (int i = cur_index + 1; i < (int)gpus.size(); i++) {
  3037. if (gpus[i] == id)
  3038. return i;
  3039. }
  3040. GGML_ASSERT(false);
  3041. }
  3042. bool is_ext_oneapi_device(const sycl::device &dev) {
  3043. sycl::backend dev_backend = dev.get_backend();
  3044. if (dev_backend == sycl::backend::ext_oneapi_level_zero ||
  3045. dev_backend == sycl::backend::ext_oneapi_cuda ||
  3046. dev_backend == sycl::backend::ext_oneapi_hip)
  3047. return true;
  3048. return false;
  3049. }
  3050. };
  3051. static sycl_gpu_mgr *g_sycl_gpu_mgr = NULL;
  3052. static int g_device_count = -1;
  3053. static int g_all_sycl_device_count = -1;
  3054. static int g_main_device = -1;
  3055. static int g_main_device_id = -1;
  3056. static bool g_ggml_backend_sycl_buffer_type_initialized = false;
  3057. static std::array<float, GGML_SYCL_MAX_DEVICES> g_default_tensor_split = {};
  3058. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  3059. static ggml_sycl_backend_gpu_mode g_ggml_sycl_backend_gpu_mode = SYCL_UNSET_GPU_MODE;
  3060. struct sycl_device_capabilities {
  3061. int cc; // compute capability
  3062. bool vmm; // virtual memory support
  3063. size_t vmm_granularity; // granularity of virtual memory
  3064. int device_id;
  3065. };
  3066. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3067. struct sycl_device_id2index {
  3068. int index;
  3069. };
  3070. static void * g_scratch_buffer = nullptr;
  3071. static size_t g_scratch_size = 0; // disabled by default
  3072. static size_t g_scratch_offset = 0;
  3073. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3074. int get_main_device(){
  3075. return g_main_device;
  3076. }
  3077. [[noreturn]]
  3078. static void bad_arch(const sycl::stream &stream_ct1) {
  3079. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3080. "current GPU architecture.\n";
  3081. // __trap();
  3082. std::exit(1);
  3083. (void) bad_arch; // suppress unused function warning
  3084. }
  3085. /*
  3086. device_index: device index from 0 to n (continue numbers).
  3087. It is used for device select/set in SYCL backend internal data structure.
  3088. */
  3089. void check_allow_gpu_index(const int device_index) {
  3090. if (device_index >= g_device_count) {
  3091. char error_buf[256];
  3092. snprintf(error_buf, sizeof(error_buf),
  3093. "%s error: device_index:%d is out of range: [0-%d]", __func__,
  3094. device_index, g_device_count - 1);
  3095. fprintf(stderr, "%s\n", error_buf);
  3096. assert(false);
  3097. }
  3098. }
  3099. /*
  3100. device_id: device ID is shown by ggml_backend_sycl_print_sycl_devices().
  3101. It is only used to set current working device.
  3102. */
  3103. void check_allow_gpu_id(const int device_id) {
  3104. if (!g_sycl_gpu_mgr->is_allowed_gpu(device_id)) {
  3105. char error_buf[256];
  3106. snprintf(error_buf, sizeof(error_buf),
  3107. "error: cannot set device=%d, which is not allowed. Please "
  3108. "set GPU ID in: [%s]",
  3109. device_id, g_sycl_gpu_mgr->gpus_list.c_str());
  3110. fprintf(stderr, "%s\n", error_buf);
  3111. throw std::invalid_argument(error_buf);
  3112. }
  3113. }
  3114. int get_current_device_id() {
  3115. return dpct::dev_mgr::instance().current_device_id();
  3116. }
  3117. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  3118. int device_id = g_sycl_gpu_mgr->gpus[device];
  3119. check_allow_gpu_id(device_id);
  3120. int current_device_id;
  3121. SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
  3122. // GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
  3123. // current_device_id=%d\n", device, current_device);
  3124. if (device_id == current_device_id) {
  3125. return 0;
  3126. }
  3127. return CHECK_TRY_ERROR(dpct::select_device(device_id));
  3128. } catch (sycl::exception const &exc) {
  3129. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3130. << ", line:" << __LINE__ << std::endl;
  3131. crash();
  3132. std::exit(1);
  3133. }
  3134. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3135. if(!g_ggml_sycl_debug) return;
  3136. if(!src){
  3137. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3138. return;
  3139. }
  3140. char filename[1024];
  3141. sprintf(filename, "%s.txt", name);
  3142. printf("GGML Tensor:%s save to %s\n", name, filename);
  3143. size_t total_size = total_elements*sizeof(float);
  3144. float *local_buf = NULL;
  3145. if(src_on_device) {
  3146. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3147. ggml_sycl_set_device(g_main_device);
  3148. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3149. main_stream->memcpy(local_buf, src, total_size).wait();
  3150. }
  3151. else {
  3152. local_buf = (float *)src;
  3153. }
  3154. std::ofstream logfile;
  3155. logfile.open(filename);
  3156. for(size_t i=0; i<total_elements; i++){
  3157. logfile << local_buf[i] <<" ";
  3158. if((i+1)%20 ==0) logfile <<std::endl;
  3159. }
  3160. logfile <<std::endl;
  3161. logfile.close();
  3162. if(src_on_device) ggml_sycl_host_free(local_buf);
  3163. }
  3164. void log_ggml_var_device_fp16(const char*name, sycl::half *src, size_t total_elements, bool src_on_device){
  3165. if(!g_ggml_sycl_debug) return;
  3166. if(!src){
  3167. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3168. return;
  3169. }
  3170. char filename[1024];
  3171. sprintf(filename, "%s.txt", name);
  3172. printf("GGML Tensor:%s save to %s\n", name, filename);
  3173. size_t total_size = total_elements*sizeof(sycl::half);
  3174. sycl::half *local_buf = NULL;
  3175. if(src_on_device) {
  3176. local_buf = (sycl::half *) ggml_sycl_host_malloc(total_size);
  3177. ggml_sycl_set_device(g_main_device);
  3178. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3179. main_stream->memcpy(local_buf, src, total_size).wait();
  3180. }
  3181. else {
  3182. local_buf = (sycl::half *)src;
  3183. }
  3184. std::ofstream logfile;
  3185. logfile.open(filename);
  3186. for(size_t i=0; i<total_elements; i++){
  3187. logfile << local_buf[i] <<" ";
  3188. if((i+1)%20 ==0) logfile <<std::endl;
  3189. }
  3190. logfile <<std::endl;
  3191. logfile.close();
  3192. if(src_on_device) ggml_sycl_host_free(local_buf);
  3193. }
  3194. //todo: debug for crash in some case
  3195. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3196. if(!g_ggml_sycl_debug) return;
  3197. if(!src){
  3198. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3199. return;
  3200. }
  3201. size_t total_elements = ggml_nelements(src);
  3202. const bool src_on_device = src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  3203. float *src_data =NULL;
  3204. if(src_on_device) {
  3205. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3206. src_data = (float*)src_extra->data_device[g_main_device];
  3207. }
  3208. else {
  3209. src_data = (float *)src->data;
  3210. }
  3211. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3212. }
  3213. static int log_file_name_idx=0;
  3214. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3215. stop_cnt = 4;
  3216. if(log_file_name_idx>=stop_cnt) return;
  3217. char filename[1280];
  3218. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3219. log_file_name_idx++;
  3220. print_ggml_tensor(filename, src);
  3221. }
  3222. static __dpct_inline__ float warp_reduce_sum(float x,
  3223. const sycl::nd_item<3> &item_ct1) {
  3224. #pragma unroll
  3225. for (int mask = 16; mask > 0; mask >>= 1) {
  3226. /*
  3227. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3228. kernel that calls this function may be less than "32". The function
  3229. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3230. CPU device. Modify the size of the work-group to ensure that the value
  3231. of the right-most dimension is a multiple of "32".
  3232. */
  3233. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3234. }
  3235. return x;
  3236. }
  3237. static __dpct_inline__ sycl::float2
  3238. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3239. #pragma unroll
  3240. for (int mask = 16; mask > 0; mask >>= 1) {
  3241. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3242. mask);
  3243. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3244. mask);
  3245. }
  3246. return a;
  3247. }
  3248. static __dpct_inline__ float warp_reduce_max(float x,
  3249. const sycl::nd_item<3> &item_ct1) {
  3250. #pragma unroll
  3251. for (int mask = 16; mask > 0; mask >>= 1) {
  3252. /*
  3253. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3254. kernel that calls this function may be less than "32". The function
  3255. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3256. CPU device. Modify the size of the work-group to ensure that the value
  3257. of the right-most dimension is a multiple of "32".
  3258. */
  3259. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3260. item_ct1.get_sub_group(), x, mask));
  3261. }
  3262. return x;
  3263. }
  3264. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3265. return b;
  3266. GGML_UNUSED(a);
  3267. }
  3268. static __dpct_inline__ float op_add(const float a, const float b) {
  3269. return a + b;
  3270. }
  3271. static __dpct_inline__ float op_mul(const float a, const float b) {
  3272. return a * b;
  3273. }
  3274. static __dpct_inline__ float op_div(const float a, const float b) {
  3275. return a / b;
  3276. }
  3277. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3278. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3279. int ne0, int ne1, int ne2, int ne3,
  3280. int ne10, int ne11, int ne12, int ne13,
  3281. /*int s0, */ int s1, int s2, int s3,
  3282. /*int s10,*/ int s11, int s12, int s13,
  3283. const sycl::nd_item<3> &item_ct1) {
  3284. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3285. item_ct1.get_local_id(2);
  3286. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3287. item_ct1.get_local_id(1));
  3288. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3289. item_ct1.get_local_id(0)) /
  3290. ne3;
  3291. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3292. item_ct1.get_local_id(0)) %
  3293. ne3;
  3294. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3295. return;
  3296. }
  3297. const int i11 = i1 % ne11;
  3298. const int i12 = i2 % ne12;
  3299. const int i13 = i3 % ne13;
  3300. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3301. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3302. const size_t i_dst = i_src0;
  3303. const src0_t * src0_row = src0 + i_src0;
  3304. const src1_t * src1_row = src1 + i_src1;
  3305. dst_t * dst_row = dst + i_dst;
  3306. for (int i0 = i0s; i0 < ne0;
  3307. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3308. const int i10 = i0 % ne10;
  3309. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3310. }
  3311. }
  3312. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3313. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3314. int ne0, int ne1, int ne2, int ne3,
  3315. int ne10, int ne11, int ne12, int ne13,
  3316. /*int s0, */ int s1, int s2, int s3,
  3317. /*int s10,*/ int s11, int s12, int s13,
  3318. const sycl::nd_item<3> &item_ct1) {
  3319. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3320. item_ct1.get_local_id(2);
  3321. const int i3 = i/(ne2*ne1*ne0);
  3322. const int i2 = (i/(ne1*ne0)) % ne2;
  3323. const int i1 = (i/ne0) % ne1;
  3324. const int i0 = i % ne0;
  3325. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3326. return;
  3327. }
  3328. const int i11 = i1 % ne11;
  3329. const int i12 = i2 % ne12;
  3330. const int i13 = i3 % ne13;
  3331. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3332. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3333. const size_t i_dst = i_src0;
  3334. const src0_t * src0_row = src0 + i_src0;
  3335. const src1_t * src1_row = src1 + i_src1;
  3336. dst_t * dst_row = dst + i_dst;
  3337. const int i10 = i0 % ne10;
  3338. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3339. }
  3340. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3341. const int ne10, const int ne11, const int ne12,
  3342. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3343. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3344. item_ct1.get_local_id(2);
  3345. if (i >= ne) {
  3346. return;
  3347. }
  3348. int src1_idx = i - offset;
  3349. int oz = src1_idx / nb2;
  3350. int oy = (src1_idx - (oz * nb2)) / nb1;
  3351. int ox = src1_idx % nb1;
  3352. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3353. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3354. } else {
  3355. dst[i] = x[i];
  3356. }
  3357. }
  3358. static void gelu_f32(const float * x, float * dst, const int k,
  3359. const sycl::nd_item<3> &item_ct1) {
  3360. const float GELU_COEF_A = 0.044715f;
  3361. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  3362. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3363. item_ct1.get_local_id(2);
  3364. if (i >= k) {
  3365. return;
  3366. }
  3367. float xi = x[i];
  3368. dst[i] = 0.5f * xi *
  3369. (1.0f +
  3370. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3371. }
  3372. static void silu_f32(const float * x, float * dst, const int k,
  3373. const sycl::nd_item<3> &item_ct1) {
  3374. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3375. item_ct1.get_local_id(2);
  3376. if (i >= k) {
  3377. return;
  3378. }
  3379. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3380. }
  3381. static void gelu_quick_f32(const float *x, float *dst, int k,
  3382. const sycl::nd_item<3> &item_ct1) {
  3383. const float GELU_QUICK_COEF = -1.702f;
  3384. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3385. item_ct1.get_local_id(2);
  3386. if (i >= k) {
  3387. return;
  3388. }
  3389. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3390. }
  3391. static void tanh_f32(const float *x, float *dst, int k,
  3392. const sycl::nd_item<3> &item_ct1) {
  3393. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3394. item_ct1.get_local_id(2);
  3395. if (i >= k) {
  3396. return;
  3397. }
  3398. dst[i] = sycl::tanh((float)(x[i]));
  3399. }
  3400. static void relu_f32(const float * x, float * dst, const int k,
  3401. const sycl::nd_item<3> &item_ct1) {
  3402. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3403. item_ct1.get_local_id(2);
  3404. if (i >= k) {
  3405. return;
  3406. }
  3407. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3408. }
  3409. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  3410. const sycl::nd_item<3> &item_ct1) {
  3411. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3412. item_ct1.get_local_id(2);
  3413. if (i >= k) {
  3414. return;
  3415. }
  3416. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3417. }
  3418. static void hardswish_f32(const float * x, float * dst, const int k,
  3419. const sycl::nd_item<3> &item_ct1) {
  3420. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3421. item_ct1.get_local_id(2);
  3422. if (i >= k) {
  3423. return;
  3424. }
  3425. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3426. }
  3427. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3428. const sycl::nd_item<3> &item_ct1) {
  3429. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3430. item_ct1.get_local_id(2);
  3431. if (i >= k) {
  3432. return;
  3433. }
  3434. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3435. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3436. }
  3437. static void sqr_f32(const float * x, float * dst, const int k,
  3438. const sycl::nd_item<3> &item_ct1) {
  3439. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3440. item_ct1.get_local_id(2);
  3441. if (i >= k) {
  3442. return;
  3443. }
  3444. dst[i] = x[i] * x[i];
  3445. }
  3446. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3447. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3448. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3449. item_ct1.get_local_id(1);
  3450. const int tid = item_ct1.get_local_id(2);
  3451. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3452. for (int col = tid; col < ncols; col += block_size) {
  3453. const float xi = x[row*ncols + col];
  3454. mean_var.x() += xi;
  3455. mean_var.y() += xi * xi;
  3456. }
  3457. // sum up partial sums
  3458. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3459. if (block_size > WARP_SIZE) {
  3460. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3461. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3462. if (lane_id == 0) {
  3463. s_sum[warp_id] = mean_var;
  3464. }
  3465. /*
  3466. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3467. converged control flow. You may need to adjust the code.
  3468. */
  3469. item_ct1.barrier(sycl::access::fence_space::local_space);
  3470. mean_var = s_sum[lane_id];
  3471. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3472. }
  3473. const float mean = mean_var.x() / ncols;
  3474. const float var = mean_var.y() / ncols - mean * mean;
  3475. const float inv_std = sycl::rsqrt(var + eps);
  3476. for (int col = tid; col < ncols; col += block_size) {
  3477. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3478. }
  3479. }
  3480. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3481. const sycl::nd_item<3> &item_ct1) {
  3482. int nidx = item_ct1.get_local_id(2) +
  3483. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3484. if (nidx >= ne0) {
  3485. return;
  3486. }
  3487. // operation
  3488. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3489. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3490. if (item_ct1.get_group(0) < ne02) { // src0
  3491. int offset_src =
  3492. nidx + item_ct1.get_group(1) * ne0 +
  3493. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3494. dst[offset_dst] = x[offset_src];
  3495. } else {
  3496. int offset_src =
  3497. nidx + item_ct1.get_group(1) * ne0 +
  3498. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3499. dst[offset_dst] = y[offset_src];
  3500. }
  3501. }
  3502. static void upscale_f32(const float *x, float *dst, const int nb00, const int nb01,
  3503. const int nb02, const int nb03, const int ne10, const int ne11,
  3504. const int ne12, const int ne13, const float sf0, const float sf1,
  3505. const float sf2, const float sf3, const sycl::nd_item<1> &item_ct1) {
  3506. int index = item_ct1.get_local_id(0) +
  3507. item_ct1.get_group(0) * item_ct1.get_local_range(0);
  3508. if (index >= ne10 * ne11 * ne12 * ne13) {
  3509. return;
  3510. }
  3511. // operation
  3512. int i10 = index % ne10;
  3513. int i11 = (index / ne10) % ne11;
  3514. int i12 = (index / (ne10 * ne11)) % ne12;
  3515. int i13 = (index / (ne10 * ne11 * ne12)) % ne13;
  3516. int i00 = i10 / sf0;
  3517. int i01 = i11 / sf1;
  3518. int i02 = i12 / sf2;
  3519. int i03 = i13 / sf3;
  3520. dst[index] = *(float *)((char *)x + i03 * nb03 + i02 * nb02 + i01 * nb01 + i00 * nb00);
  3521. }
  3522. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3523. const sycl::nd_item<3> &item_ct1) {
  3524. int nidx = item_ct1.get_local_id(2) +
  3525. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3526. if (nidx >= ne0) {
  3527. return;
  3528. }
  3529. // operation
  3530. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3531. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3532. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3533. item_ct1.get_group(0) < ne02) {
  3534. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3535. item_ct1.get_group(0) * ne00 * ne01;
  3536. dst[offset_dst] = x[offset_src];
  3537. } else {
  3538. dst[offset_dst] = 0.0f;
  3539. }
  3540. }
  3541. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3542. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3543. int start = item_ct1.get_group(2) * group_size;
  3544. int end = start + group_size;
  3545. start += item_ct1.get_local_id(2);
  3546. if (end >= ne_elements) {
  3547. end = ne_elements;
  3548. }
  3549. float tmp = 0.0f; // partial sum for thread in warp
  3550. for (int j = start; j < end; j += block_size) {
  3551. tmp += x[j];
  3552. }
  3553. tmp = warp_reduce_sum(tmp, item_ct1);
  3554. if (block_size > WARP_SIZE) {
  3555. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3556. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3557. if (lane_id == 0) {
  3558. s_sum[warp_id] = tmp;
  3559. }
  3560. /*
  3561. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3562. converged control flow. You may need to adjust the code.
  3563. */
  3564. /*
  3565. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3566. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3567. better performance if there is no access to global memory.
  3568. */
  3569. item_ct1.barrier();
  3570. tmp = s_sum[lane_id];
  3571. tmp = warp_reduce_sum(tmp, item_ct1);
  3572. }
  3573. float mean = tmp / group_size;
  3574. tmp = 0.0f;
  3575. for (int j = start; j < end; j += block_size) {
  3576. float xi = x[j] - mean;
  3577. dst[j] = xi;
  3578. tmp += xi * xi;
  3579. }
  3580. tmp = warp_reduce_sum(tmp, item_ct1);
  3581. if (block_size > WARP_SIZE) {
  3582. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3583. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3584. if (lane_id == 0) {
  3585. s_sum[warp_id] = tmp;
  3586. }
  3587. /*
  3588. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3589. converged control flow. You may need to adjust the code.
  3590. */
  3591. /*
  3592. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3593. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3594. better performance if there is no access to global memory.
  3595. */
  3596. item_ct1.barrier();
  3597. tmp = s_sum[lane_id];
  3598. tmp = warp_reduce_sum(tmp, item_ct1);
  3599. }
  3600. float variance = tmp / group_size;
  3601. float scale = sycl::rsqrt(variance + eps);
  3602. for (int j = start; j < end; j += block_size) {
  3603. dst[j] *= scale;
  3604. }
  3605. }
  3606. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3607. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3608. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3609. item_ct1.get_local_id(1);
  3610. const int tid = item_ct1.get_local_id(2);
  3611. float tmp = 0.0f; // partial sum for thread in warp
  3612. for (int col = tid; col < ncols; col += block_size) {
  3613. const float xi = x[row*ncols + col];
  3614. tmp += xi * xi;
  3615. }
  3616. // sum up partial sums
  3617. tmp = warp_reduce_sum(tmp, item_ct1);
  3618. if (block_size > WARP_SIZE) {
  3619. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3620. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3621. if (lane_id == 0) {
  3622. s_sum[warp_id] = tmp;
  3623. }
  3624. /*
  3625. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3626. converged control flow. You may need to adjust the code.
  3627. */
  3628. item_ct1.barrier(sycl::access::fence_space::local_space);
  3629. tmp = s_sum[lane_id];
  3630. tmp = warp_reduce_sum(tmp, item_ct1);
  3631. }
  3632. const float mean = tmp / ncols;
  3633. const float scale = sycl::rsqrt(mean + eps);
  3634. for (int col = tid; col < ncols; col += block_size) {
  3635. dst[row*ncols + col] = scale * x[row*ncols + col];
  3636. }
  3637. }
  3638. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3639. const int iqs, dfloat2 &v) {
  3640. const block_q4_0 * x = (const block_q4_0 *) vx;
  3641. const dfloat d = x[ib].d;
  3642. const int vui = x[ib].qs[iqs];
  3643. v.x() = vui & 0xF;
  3644. v.y() = vui >> 4;
  3645. #ifdef GGML_SYCL_F16
  3646. // v = v - {8.0f, 8.0f};
  3647. // v = v * {d, d};
  3648. v.s0() = (v.s0() - 8.0f) * d;
  3649. v.s1() = (v.s1() - 8.0f) * d;
  3650. #else
  3651. v.x() = (v.x() - 8.0f) * d;
  3652. v.y() = (v.y() - 8.0f) * d;
  3653. #endif // GGML_SYCL_F16
  3654. }
  3655. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3656. const int iqs, dfloat2 &v) {
  3657. const block_q4_1 * x = (const block_q4_1 *) vx;
  3658. const dfloat d = x[ib].dm[0];
  3659. const dfloat m = x[ib].dm[1];
  3660. const int vui = x[ib].qs[iqs];
  3661. v.x() = vui & 0xF;
  3662. v.y() = vui >> 4;
  3663. #ifdef GGML_SYCL_F16
  3664. // v = v * {d, d};
  3665. // v = v + {m, m};
  3666. v.s0() = (v.s0() * d) + m;
  3667. v.s1() = (v.s1() * d) + m;
  3668. #else
  3669. v.x() = (v.x() * d) + m;
  3670. v.y() = (v.y() * d) + m;
  3671. #endif // GGML_SYCL_F16
  3672. }
  3673. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3674. const int iqs, dfloat2 &v) {
  3675. const block_q5_0 * x = (const block_q5_0 *) vx;
  3676. const dfloat d = x[ib].d;
  3677. uint32_t qh;
  3678. memcpy(&qh, x[ib].qh, sizeof(qh));
  3679. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3680. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3681. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3682. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3683. #ifdef GGML_SYCL_F16
  3684. // v = v - {16.0f, 16.0f};
  3685. // v = v * {d, d};
  3686. v.s0() = (v.s0() - 16.0f) * d;
  3687. v.s1() = (v.s1() - 16.0f) * d;
  3688. #else
  3689. v.x() = (v.x() - 16.0f) * d;
  3690. v.y() = (v.y() - 16.0f) * d;
  3691. #endif // GGML_SYCL_F16
  3692. }
  3693. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  3694. const int iqs, dfloat2 &v) {
  3695. const block_q5_1 * x = (const block_q5_1 *) vx;
  3696. const dfloat d = x[ib].dm[0];
  3697. const dfloat m = x[ib].dm[1];
  3698. uint32_t qh;
  3699. memcpy(&qh, x[ib].qh, sizeof(qh));
  3700. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3701. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3702. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3703. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3704. #ifdef GGML_SYCL_F16
  3705. // v = v * {d, d};
  3706. // v = v + {m, m};
  3707. v.s0() = (v.s0() * d) + m;
  3708. v.s1() = (v.s1() * d) + m;
  3709. #else
  3710. v.x() = (v.x() * d) + m;
  3711. v.y() = (v.y() * d) + m;
  3712. #endif // GGML_SYCL_F16
  3713. }
  3714. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  3715. const int iqs, dfloat2 &v) {
  3716. const block_q8_0 * x = (const block_q8_0 *) vx;
  3717. const dfloat d = x[ib].d;
  3718. v.x() = x[ib].qs[iqs + 0];
  3719. v.y() = x[ib].qs[iqs + 1];
  3720. #ifdef GGML_SYCL_F16
  3721. // v = v * {d, d};
  3722. v.s0() *= d;
  3723. v.s1() *= d;
  3724. #else
  3725. v.x() *= d;
  3726. v.y() *= d;
  3727. #endif // GGML_SYCL_F16
  3728. }
  3729. template<typename dst_t>
  3730. static void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3731. const sycl::nd_item<3> &item_ct1) {
  3732. const int i = item_ct1.get_group(2);
  3733. // assume 32 threads
  3734. const int tid = item_ct1.get_local_id(2);
  3735. const int il = tid/8;
  3736. const int ir = tid%8;
  3737. const int ib = 8*i + ir;
  3738. if (ib >= nb32) {
  3739. return;
  3740. }
  3741. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3742. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  3743. const float d = sycl::vec<sycl::half, 1>(x->d)
  3744. .convert<float, sycl::rounding_mode::automatic>()[0];
  3745. const float dm = -8*d;
  3746. const uint8_t * q = x->qs + 4*il;
  3747. for (int l = 0; l < 4; ++l) {
  3748. y[l+ 0] = d * (q[l] & 0xF) + dm;
  3749. y[l+16] = d * (q[l] >> 4) + dm;
  3750. }
  3751. }
  3752. template<typename dst_t>
  3753. static void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3754. const sycl::nd_item<3> &item_ct1) {
  3755. const int i = item_ct1.get_group(2);
  3756. // assume 32 threads
  3757. const int tid = item_ct1.get_local_id(2);
  3758. const int il = tid/8;
  3759. const int ir = tid%8;
  3760. const int ib = 8*i + ir;
  3761. if (ib >= nb32) {
  3762. return;
  3763. }
  3764. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3765. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  3766. const sycl::float2 d =
  3767. x->dm.convert<float, sycl::rounding_mode::automatic>();
  3768. const uint8_t * q = x->qs + 4*il;
  3769. for (int l = 0; l < 4; ++l) {
  3770. y[l + 0] = d.x() * (q[l] & 0xF) + d.y();
  3771. y[l + 16] = d.x() * (q[l] >> 4) + d.y();
  3772. }
  3773. }
  3774. //================================== k-quants
  3775. template<typename dst_t>
  3776. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3777. const sycl::nd_item<3> &item_ct1) {
  3778. const int i = item_ct1.get_group(2);
  3779. const block_q2_K * x = (const block_q2_K *) vx;
  3780. const int tid = item_ct1.get_local_id(2);
  3781. const int n = tid/32;
  3782. const int l = tid - 32*n;
  3783. const int is = 8*n + l/16;
  3784. const uint8_t q = x[i].qs[32*n + l];
  3785. dst_t * y = yy + i*QK_K + 128*n;
  3786. float dall = x[i].dm[0];
  3787. float dmin = x[i].dm[1];
  3788. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3789. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3790. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  3791. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  3792. }
  3793. template<typename dst_t>
  3794. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3795. const sycl::nd_item<3> &item_ct1) {
  3796. const int i = item_ct1.get_group(2);
  3797. const block_q3_K * x = (const block_q3_K *) vx;
  3798. const int r = item_ct1.get_local_id(2) / 4;
  3799. const int tid = r/2;
  3800. const int is0 = r%2;
  3801. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  3802. const int n = tid / 4;
  3803. const int j = tid - 4*n;
  3804. uint8_t m = 1 << (4*n + j);
  3805. int is = 8*n + 2*j + is0;
  3806. int shift = 2*j;
  3807. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  3808. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  3809. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  3810. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  3811. float d_all = x[i].d;
  3812. float dl = d_all * (us - 32);
  3813. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  3814. const uint8_t * q = x[i].qs + 32*n;
  3815. const uint8_t * hm = x[i].hmask;
  3816. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  3817. }
  3818. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  3819. if (j < 4) {
  3820. d = q[j] & 63; m = q[j + 4] & 63;
  3821. } else {
  3822. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  3823. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  3824. }
  3825. }
  3826. template<typename dst_t>
  3827. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3828. const sycl::nd_item<3> &item_ct1) {
  3829. const block_q4_K * x = (const block_q4_K *) vx;
  3830. const int i = item_ct1.get_group(2);
  3831. // assume 32 threads
  3832. const int tid = item_ct1.get_local_id(2);
  3833. const int il = tid/8;
  3834. const int ir = tid%8;
  3835. const int is = 2*il;
  3836. const int n = 4;
  3837. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  3838. const float dall = x[i].dm[0];
  3839. const float dmin = x[i].dm[1];
  3840. const uint8_t * q = x[i].qs + 32*il + n*ir;
  3841. uint8_t sc, m;
  3842. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3843. const float d1 = dall * sc; const float m1 = dmin * m;
  3844. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3845. const float d2 = dall * sc; const float m2 = dmin * m;
  3846. for (int l = 0; l < n; ++l) {
  3847. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  3848. y[l +32] = d2 * (q[l] >> 4) - m2;
  3849. }
  3850. }
  3851. template<typename dst_t>
  3852. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3853. const sycl::nd_item<3> &item_ct1) {
  3854. const block_q5_K * x = (const block_q5_K *) vx;
  3855. const int i = item_ct1.get_group(2);
  3856. // assume 64 threads - this is very slightly better than the one below
  3857. const int tid = item_ct1.get_local_id(2);
  3858. const int il = tid/16; // il is in 0...3
  3859. const int ir = tid%16; // ir is in 0...15
  3860. const int is = 2*il; // is is in 0...6
  3861. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  3862. const float dall = x[i].dm[0];
  3863. const float dmin = x[i].dm[1];
  3864. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  3865. const uint8_t * qh = x[i].qh + 2*ir;
  3866. uint8_t sc, m;
  3867. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3868. const float d1 = dall * sc; const float m1 = dmin * m;
  3869. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3870. const float d2 = dall * sc; const float m2 = dmin * m;
  3871. uint8_t hm = 1 << (2*il);
  3872. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  3873. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  3874. hm <<= 1;
  3875. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  3876. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  3877. }
  3878. template<typename dst_t>
  3879. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3880. const sycl::nd_item<3> &item_ct1) {
  3881. const block_q6_K * x = (const block_q6_K *) vx;
  3882. const int i = item_ct1.get_group(2);
  3883. // assume 64 threads - this is very slightly better than the one below
  3884. const int tid = item_ct1.get_local_id(2);
  3885. const int ip = tid/32; // ip is 0 or 1
  3886. const int il = tid - 32*ip; // 0...32
  3887. const int is = 8*ip + il/16;
  3888. dst_t * y = yy + i*QK_K + 128*ip + il;
  3889. const float d = x[i].d;
  3890. const uint8_t * ql = x[i].ql + 64*ip + il;
  3891. const uint8_t qh = x[i].qh[32*ip + il];
  3892. const int8_t * sc = x[i].scales + is;
  3893. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3894. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  3895. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3896. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  3897. }
  3898. template<typename dst_t>
  3899. static void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3900. const sycl::nd_item<3> &item_ct1,
  3901. const uint64_t *iq2xxs_grid_ptr,
  3902. const uint8_t *ksigns_iq2xs_ptr,
  3903. const uint8_t *kmask_iq2xs_ptr) {
  3904. const int i = item_ct1.get_group(2);
  3905. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  3906. const int tid = item_ct1.get_local_id(2);
  3907. const int il = tid/8; // 0...3
  3908. const int ib = tid%8; // 0...7
  3909. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3910. const uint16_t * q2 = x[i].qs + 4*ib;
  3911. const uint8_t * aux8 = (const uint8_t *)q2;
  3912. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid_ptr + aux8[il]);
  3913. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3914. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  3915. const uint8_t signs = ksigns_iq2xs_ptr[(aux32 >> 7*il) & 127];
  3916. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs_ptr[j] ? -1.f : 1.f);
  3917. }
  3918. template<typename dst_t>
  3919. static void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3920. const sycl::nd_item<3> &item_ct1,
  3921. const uint64_t *iq2xs_grid,
  3922. const uint8_t *ksigns_iq2xs,
  3923. const uint8_t *kmask_iq2xs) {
  3924. const int i = item_ct1.get_group(2);
  3925. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  3926. const int tid = item_ct1.get_local_id(2);
  3927. const int il = tid/8; // 0...3
  3928. const int ib = tid%8; // 0...7
  3929. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3930. const uint16_t * q2 = x[i].qs + 4*ib;
  3931. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  3932. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  3933. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  3934. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3935. }
  3936. template <typename dst_t>
  3937. __dpct_inline__ static void
  3938. dequantize_block_iq2_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  3939. const sycl::nd_item<3> &item_ct1) {
  3940. const int i = item_ct1.get_group(2);
  3941. const block_iq2_s * x = (const block_iq2_s *) vx;
  3942. const int tid = item_ct1.get_local_id(2);
  3943. const int il = tid/8; // 0...3
  3944. const int ib = tid%8; // 0...7
  3945. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3946. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  3947. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  3948. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  3949. #pragma unroll
  3950. for (int j = 0; j < 8; ++j) {
  3951. y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3952. }
  3953. }
  3954. template<typename dst_t>
  3955. static void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3956. const sycl::nd_item<3> &item_ct1,
  3957. const uint32_t *iq3xxs_grid,
  3958. const uint8_t *ksigns_iq2xs,
  3959. const uint8_t *kmask_iq2xs) {
  3960. const int i = item_ct1.get_group(2);
  3961. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  3962. const int tid = item_ct1.get_local_id(2);
  3963. const int il = tid/8; // 0...3
  3964. const int ib = tid%8; // 0...7
  3965. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3966. const uint8_t * q3 = x[i].qs + 8*ib;
  3967. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  3968. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  3969. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  3970. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  3971. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  3972. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  3973. for (int j = 0; j < 4; ++j) {
  3974. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  3975. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  3976. }
  3977. }
  3978. template <typename dst_t>
  3979. __dpct_inline__ static void
  3980. dequantize_block_iq3_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  3981. const sycl::nd_item<3> &item_ct1,
  3982. const uint8_t *kmask_iq2xs, const uint32_t *iq3s_grid) {
  3983. const int i = item_ct1.get_group(2);
  3984. const block_iq3_s * x = (const block_iq3_s *) vx;
  3985. const int tid = item_ct1.get_local_id(2);
  3986. const int il = tid/8; // 0...3
  3987. const int ib = tid%8; // 0...7
  3988. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3989. const uint8_t * qs = x[i].qs + 8*ib;
  3990. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  3991. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  3992. const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
  3993. const uint8_t signs = x[i].signs[4*ib + il];
  3994. #pragma unroll
  3995. for (int j = 0; j < 4; ++j) {
  3996. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  3997. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  3998. }
  3999. }
  4000. template <typename dst_t>
  4001. __dpct_inline__ static void
  4002. dequantize_block_iq1_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4003. const sycl::nd_item<3> &item_ct1,
  4004. const uint32_t *iq1s_grid_gpu) {
  4005. const int i = item_ct1.get_group(2);
  4006. const block_iq1_s * x = (const block_iq1_s *) vx;
  4007. const int tid = item_ct1.get_local_id(2);
  4008. const int il = tid/8; // 0...3
  4009. const int ib = tid%8; // 0...7
  4010. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4011. const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
  4012. const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
  4013. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  4014. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
  4015. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  4016. grid32[0] &= 0x0f0f0f0f;
  4017. #pragma unroll
  4018. for (int j = 0; j < 8; ++j) {
  4019. y[j] = d * (q[j] + delta);
  4020. }
  4021. }
  4022. template <typename dst_t>
  4023. __dpct_inline__ static void
  4024. dequantize_block_iq1_m(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4025. const sycl::nd_item<3> &item_ct1,
  4026. const uint32_t *iq1s_grid_gpu) {
  4027. const int i = item_ct1.get_group(2);
  4028. const block_iq1_m * x = (const block_iq1_m *) vx;
  4029. const int tid = item_ct1.get_local_id(2);
  4030. const int il = tid/8; // 0...3
  4031. const int ib = tid%8; // 0...7
  4032. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4033. const uint16_t * sc = (const uint16_t *)x[i].scales;
  4034. iq1m_scale_t scale;
  4035. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  4036. const int ib16 = 2*ib + il/2; // sc[ib16/4] >> 3*(ib16%4) -> sc[ib/2] >> 3*((2*ib+il/2)%4);
  4037. const float d = (float)scale.f16 * (2*((sc[ib16/4] >> 3*(ib16%4)) & 0x7) + 1);
  4038. const float delta = x[i].qh[2*ib+il/2] & (0x08 << 4*(il%2)) ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA;
  4039. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  4040. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[2*ib+il/2] >> 4*(il%2)) & 7) << 8)];
  4041. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  4042. grid32[0] &= 0x0f0f0f0f;
  4043. #pragma unroll
  4044. for (int j = 0; j < 8; ++j) {
  4045. y[j] = d * (q[j] + delta);
  4046. }
  4047. }
  4048. template <typename dst_t>
  4049. __dpct_inline__ static void
  4050. dequantize_block_iq4_nl(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4051. const sycl::nd_item<3> &item_ct1) {
  4052. const int i = item_ct1.get_group(2);
  4053. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  4054. const int tid = item_ct1.get_local_id(2);
  4055. const int il = tid/8; // 0...3
  4056. const int ib = tid%8; // 0...7
  4057. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  4058. const uint8_t * q4 = x[ib].qs + 4*il;
  4059. const float d = (float)x[ib].d;
  4060. #pragma unroll
  4061. for (int j = 0; j < 4; ++j) {
  4062. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  4063. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  4064. }
  4065. }
  4066. template <typename dst_t>
  4067. __dpct_inline__ static void
  4068. dequantize_block_iq4_xs(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4069. const sycl::nd_item<3> &item_ct1) {
  4070. const int i = item_ct1.get_group(2);
  4071. const block_iq4_xs * x = (const block_iq4_xs *)vx;
  4072. const int tid = item_ct1.get_local_id(2);
  4073. const int il = tid/8; // 0...3
  4074. const int ib = tid%8; // 0...7
  4075. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  4076. const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
  4077. const float d = (float)x[i].d * ((((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4)) - 32);
  4078. #pragma unroll
  4079. for (int j = 0; j < 4; ++j) {
  4080. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  4081. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  4082. }
  4083. }
  4084. /*
  4085. DPCT1110:4: The total declared local variable size in device function
  4086. dequantize_mul_mat_vec_q2_k exceeds 128 bytes and may cause high register
  4087. pressure. Consult with your hardware vendor to find the total register size
  4088. available and adjust the code, or use smaller sub-group size to avoid high
  4089. register pressure.
  4090. */
  4091. static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
  4092. const float *__restrict__ yy,
  4093. float *__restrict__ dst,
  4094. const int ncols, int nrows,
  4095. const sycl::nd_item<3> &item_ct1) {
  4096. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4097. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4098. item_ct1.get_local_id(1);
  4099. if (row > nrows) return;
  4100. const int num_blocks_per_row = ncols / QK_K;
  4101. const int ib0 = row*num_blocks_per_row;
  4102. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  4103. float tmp = 0; // partial sum for thread in warp
  4104. const int tid =
  4105. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  4106. const int ix =
  4107. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4108. const int step = 16/K_QUANTS_PER_ITERATION;
  4109. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4110. const int in = tid - step*im; // 0...15 or 0...7
  4111. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  4112. const int q_offset = 32*im + l0;
  4113. const int s_offset = 8*im;
  4114. const int y_offset = 128*im + l0;
  4115. uint32_t aux[4];
  4116. const uint8_t * d = (const uint8_t *)aux;
  4117. const uint8_t * m = (const uint8_t *)(aux + 2);
  4118. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4119. const float * y = yy + i * QK_K + y_offset;
  4120. const uint8_t * q = x[i].qs + q_offset;
  4121. const float dall = x[i].dm[0];
  4122. const float dmin = x[i].dm[1];
  4123. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  4124. aux[0] = a[0] & 0x0f0f0f0f;
  4125. aux[1] = a[1] & 0x0f0f0f0f;
  4126. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  4127. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  4128. float sum1 = 0, sum2 = 0;
  4129. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4130. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  4131. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  4132. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  4133. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  4134. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  4135. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  4136. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  4137. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  4138. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  4139. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  4140. }
  4141. tmp += dall * sum1 - dmin * sum2;
  4142. }
  4143. // sum up partial sums and write back result
  4144. #pragma unroll
  4145. for (int mask = 16; mask > 0; mask >>= 1) {
  4146. tmp +=
  4147. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4148. }
  4149. if (item_ct1.get_local_id(2) == 0) {
  4150. dst[row] = tmp;
  4151. }
  4152. }
  4153. /*
  4154. DPCT1110:5: The total declared local variable size in device function
  4155. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  4156. pressure. Consult with your hardware vendor to find the total register size
  4157. available and adjust the code, or use smaller sub-group size to avoid high
  4158. register pressure.
  4159. */
  4160. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  4161. const float *__restrict__ yy,
  4162. float *__restrict__ dst,
  4163. const int ncols, int nrows,
  4164. const sycl::nd_item<3> &item_ct1) {
  4165. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4166. item_ct1.get_local_id(1);
  4167. if (row > nrows) return;
  4168. const int num_blocks_per_row = ncols / QK_K;
  4169. const int ib0 = row*num_blocks_per_row;
  4170. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  4171. float tmp = 0; // partial sum for thread in warp
  4172. const uint16_t kmask1 = 0x0303;
  4173. const uint16_t kmask2 = 0x0f0f;
  4174. const int tid =
  4175. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4176. const int ix =
  4177. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4178. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  4179. const int step = 16/K_QUANTS_PER_ITERATION;
  4180. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4181. const int in = tid - step*im; // 0....15 or 0...7
  4182. const uint8_t m = 1 << (4*im);
  4183. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  4184. const int q_offset = 32*im + l0;
  4185. const int y_offset = 128*im + l0;
  4186. uint16_t utmp[4];
  4187. const int8_t * s = (const int8_t *)utmp;
  4188. const uint16_t s_shift = 4*im;
  4189. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4190. const float * y = yy + i * QK_K + y_offset;
  4191. const uint8_t * q = x[i].qs + q_offset;
  4192. const uint8_t * h = x[i].hmask + l0;
  4193. const uint16_t * a = (const uint16_t *)x[i].scales;
  4194. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  4195. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  4196. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  4197. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  4198. const float d = x[i].d;
  4199. float sum = 0;
  4200. for (int l = 0; l < n; ++l) {
  4201. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  4202. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  4203. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  4204. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  4205. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  4206. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  4207. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  4208. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  4209. }
  4210. tmp += d * sum;
  4211. }
  4212. // sum up partial sums and write back result
  4213. #pragma unroll
  4214. for (int mask = 16; mask > 0; mask >>= 1) {
  4215. tmp +=
  4216. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4217. }
  4218. if (item_ct1.get_local_id(2) == 0) {
  4219. dst[row] = tmp;
  4220. }
  4221. }
  4222. /*
  4223. DPCT1110:6: The total declared local variable size in device function
  4224. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  4225. pressure. Consult with your hardware vendor to find the total register size
  4226. available and adjust the code, or use smaller sub-group size to avoid high
  4227. register pressure.
  4228. */
  4229. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  4230. const float *__restrict__ yy,
  4231. float *__restrict__ dst,
  4232. const int ncols, int nrows,
  4233. const sycl::nd_item<3> &item_ct1) {
  4234. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4235. item_ct1.get_local_id(1);
  4236. if (row > nrows) return;
  4237. const int num_blocks_per_row = ncols / QK_K;
  4238. const int ib0 = row*num_blocks_per_row;
  4239. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  4240. const uint16_t kmask1 = 0x3f3f;
  4241. const uint16_t kmask2 = 0x0f0f;
  4242. const uint16_t kmask3 = 0xc0c0;
  4243. const int tid =
  4244. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4245. const int ix =
  4246. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4247. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  4248. const int il = tid/step; // 0...3
  4249. const int ir = tid - step*il; // 0...7 or 0...3
  4250. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  4251. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4252. const int in = il%2;
  4253. const int l0 = n*(2*ir + in);
  4254. const int q_offset = 32*im + l0;
  4255. const int y_offset = 64*im + l0;
  4256. uint16_t aux[4];
  4257. const uint8_t * sc = (const uint8_t *)aux;
  4258. #if K_QUANTS_PER_ITERATION == 2
  4259. uint32_t q32[4];
  4260. const uint8_t * q4 = (const uint8_t *)q32;
  4261. #else
  4262. uint16_t q16[4];
  4263. const uint8_t * q4 = (const uint8_t *)q16;
  4264. #endif
  4265. float tmp = 0; // partial sum for thread in warp
  4266. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4267. const float * y1 = yy + i*QK_K + y_offset;
  4268. const float * y2 = y1 + 128;
  4269. const float dall = x[i].dm[0];
  4270. const float dmin = x[i].dm[1];
  4271. const uint16_t * a = (const uint16_t *)x[i].scales;
  4272. aux[0] = a[im+0] & kmask1;
  4273. aux[1] = a[im+2] & kmask1;
  4274. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4275. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4276. #if K_QUANTS_PER_ITERATION == 2
  4277. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4278. const uint32_t * q2 = q1 + 16;
  4279. q32[0] = q1[0] & 0x0f0f0f0f;
  4280. q32[1] = q1[0] & 0xf0f0f0f0;
  4281. q32[2] = q2[0] & 0x0f0f0f0f;
  4282. q32[3] = q2[0] & 0xf0f0f0f0;
  4283. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4284. float smin = 0;
  4285. for (int l = 0; l < 4; ++l) {
  4286. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4287. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4288. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4289. }
  4290. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4291. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4292. dmin * smin;
  4293. #else
  4294. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4295. const uint16_t * q2 = q1 + 32;
  4296. q16[0] = q1[0] & 0x0f0f;
  4297. q16[1] = q1[0] & 0xf0f0;
  4298. q16[2] = q2[0] & 0x0f0f;
  4299. q16[3] = q2[0] & 0xf0f0;
  4300. float4 s = {0.f, 0.f, 0.f, 0.f};
  4301. float smin = 0;
  4302. for (int l = 0; l < 2; ++l) {
  4303. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  4304. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  4305. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4306. }
  4307. tmp += dall * (s.x * sc[0] + s.y * sc[1] * 1.f/16.f + s.z * sc[4] + s.w * sc[5] * 1.f/16.f) - dmin * smin;
  4308. #endif
  4309. }
  4310. // sum up partial sums and write back result
  4311. #pragma unroll
  4312. for (int mask = 16; mask > 0; mask >>= 1) {
  4313. tmp +=
  4314. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4315. }
  4316. if (tid == 0) {
  4317. dst[row] = tmp;
  4318. }
  4319. }
  4320. /*
  4321. DPCT1110:7: The total declared local variable size in device function
  4322. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  4323. pressure. Consult with your hardware vendor to find the total register size
  4324. available and adjust the code, or use smaller sub-group size to avoid high
  4325. register pressure.
  4326. */
  4327. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  4328. const float *__restrict__ yy,
  4329. float *__restrict__ dst,
  4330. const int ncols,
  4331. const sycl::nd_item<3> &item_ct1) {
  4332. const int row = item_ct1.get_group(2);
  4333. const int num_blocks_per_row = ncols / QK_K;
  4334. const int ib0 = row*num_blocks_per_row;
  4335. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  4336. float tmp = 0; // partial sum for thread in warp
  4337. const uint16_t kmask1 = 0x3f3f;
  4338. const uint16_t kmask2 = 0x0f0f;
  4339. const uint16_t kmask3 = 0xc0c0;
  4340. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  4341. const int ix = item_ct1.get_local_id(2) % 2;
  4342. const int il = tid/4; // 0...3
  4343. const int ir = tid - 4*il;// 0...3
  4344. const int n = 2;
  4345. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4346. const int in = il%2;
  4347. const int l0 = n*(2*ir + in);
  4348. const int q_offset = 32*im + l0;
  4349. const int y_offset = 64*im + l0;
  4350. const uint8_t hm1 = 1 << (2*im);
  4351. const uint8_t hm2 = hm1 << 4;
  4352. uint16_t aux[4];
  4353. const uint8_t * sc = (const uint8_t *)aux;
  4354. uint16_t q16[8];
  4355. const uint8_t * q4 = (const uint8_t *)q16;
  4356. for (int i = ix; i < num_blocks_per_row; i += 2) {
  4357. const uint8_t * ql1 = x[i].qs + q_offset;
  4358. const uint8_t * qh = x[i].qh + l0;
  4359. const float * y1 = yy + i*QK_K + y_offset;
  4360. const float * y2 = y1 + 128;
  4361. const float dall = x[i].dm[0];
  4362. const float dmin = x[i].dm[1];
  4363. const uint16_t * a = (const uint16_t *)x[i].scales;
  4364. aux[0] = a[im+0] & kmask1;
  4365. aux[1] = a[im+2] & kmask1;
  4366. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4367. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4368. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  4369. float smin = 0;
  4370. const uint16_t * q1 = (const uint16_t *)ql1;
  4371. const uint16_t * q2 = q1 + 32;
  4372. q16[0] = q1[0] & 0x0f0f;
  4373. q16[1] = q1[8] & 0x0f0f;
  4374. q16[2] = (q1[0] >> 4) & 0x0f0f;
  4375. q16[3] = (q1[8] >> 4) & 0x0f0f;
  4376. q16[4] = q2[0] & 0x0f0f;
  4377. q16[5] = q2[8] & 0x0f0f;
  4378. q16[6] = (q2[0] >> 4) & 0x0f0f;
  4379. q16[7] = (q2[8] >> 4) & 0x0f0f;
  4380. for (int l = 0; l < n; ++l) {
  4381. sum.x() +=
  4382. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  4383. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  4384. sum.y() +=
  4385. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  4386. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  4387. sum.z() +=
  4388. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  4389. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  4390. sum.w() +=
  4391. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  4392. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  4393. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  4394. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  4395. }
  4396. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  4397. sum.w() * sc[5]) -
  4398. dmin * smin;
  4399. }
  4400. // sum up partial sums and write back result
  4401. #pragma unroll
  4402. for (int mask = 16; mask > 0; mask >>= 1) {
  4403. tmp +=
  4404. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4405. }
  4406. if (item_ct1.get_local_id(2) == 0) {
  4407. dst[row] = tmp;
  4408. }
  4409. }
  4410. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  4411. const sycl::nd_item<3> &item_ct1) {
  4412. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4413. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4414. item_ct1.get_local_id(1);
  4415. if (row > nrows) return;
  4416. const int num_blocks_per_row = ncols / QK_K;
  4417. const int ib0 = row*num_blocks_per_row;
  4418. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  4419. const int tid =
  4420. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4421. const int ix =
  4422. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  4423. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  4424. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4425. const int in = tid - step*im; // 0...15 or 0...7
  4426. #if K_QUANTS_PER_ITERATION == 1
  4427. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  4428. const int is = 0;
  4429. #else
  4430. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  4431. const int is = in / 4;
  4432. #endif
  4433. const int ql_offset = 64*im + l0;
  4434. const int qh_offset = 32*im + l0;
  4435. const int s_offset = 8*im + is;
  4436. const int y_offset = 128*im + l0;
  4437. float tmp = 0; // partial sum for thread in warp
  4438. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4439. const float * y = yy + i * QK_K + y_offset;
  4440. const uint8_t * ql = x[i].ql + ql_offset;
  4441. const uint8_t * qh = x[i].qh + qh_offset;
  4442. const int8_t * s = x[i].scales + s_offset;
  4443. const float d = x[i].d;
  4444. #if K_QUANTS_PER_ITERATION == 1
  4445. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  4446. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  4447. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  4448. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  4449. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  4450. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  4451. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  4452. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  4453. tmp += sum;
  4454. #else
  4455. float sum = 0;
  4456. for (int l = 0; l < 4; ++l) {
  4457. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  4458. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  4459. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  4460. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  4461. }
  4462. tmp += sum;
  4463. #endif
  4464. }
  4465. // sum up partial sums and write back result
  4466. #pragma unroll
  4467. for (int mask = 16; mask > 0; mask >>= 1) {
  4468. tmp +=
  4469. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4470. }
  4471. if (tid == 0) {
  4472. dst[row] = tmp;
  4473. }
  4474. }
  4475. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4476. const sycl::half *x = (const sycl::half *)vx;
  4477. // automatic half -> float type cast if dfloat == float
  4478. v.x() = x[ib + iqs + 0];
  4479. v.y() = x[ib + iqs + 1];
  4480. }
  4481. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4482. const float * x = (const float *) vx;
  4483. // automatic half -> float type cast if dfloat == float
  4484. v.x() = x[ib + iqs + 0];
  4485. v.y() = x[ib + iqs + 1];
  4486. }
  4487. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  4488. const sycl::nd_item<3> &item_ct1) {
  4489. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4490. item_ct1.get_local_id(2);
  4491. if (ix >= kx_padded) {
  4492. return;
  4493. }
  4494. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4495. item_ct1.get_local_id(1);
  4496. const int i_padded = iy*kx_padded + ix;
  4497. block_q8_1 * y = (block_q8_1 *) vy;
  4498. const int ib = i_padded / QK8_1; // block index
  4499. const int iqs = i_padded % QK8_1; // quant index
  4500. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  4501. float amax = sycl::fabs((float)xi);
  4502. float sum = xi;
  4503. #pragma unroll
  4504. for (int mask = 16; mask > 0; mask >>= 1) {
  4505. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  4506. item_ct1.get_sub_group(), amax, mask));
  4507. sum +=
  4508. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  4509. }
  4510. const float d = amax / 127;
  4511. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  4512. y[ib].qs[iqs] = q;
  4513. if (iqs > 0) {
  4514. return;
  4515. }
  4516. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  4517. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  4518. }
  4519. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4520. static void k_get_rows(
  4521. const void * src0, const int32_t * src1, dst_t * dst,
  4522. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4523. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4524. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4525. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4526. size_t s10, size_t s11, size_t s12,
  4527. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4528. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4529. item_ct1.get_local_id(2)) *
  4530. 2;
  4531. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4532. item_ct1.get_local_id(1);
  4533. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4534. item_ct1.get_local_id(0)) /
  4535. ne12;
  4536. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4537. item_ct1.get_local_id(0)) %
  4538. ne12;
  4539. if (i00 >= ne00) {
  4540. return;
  4541. }
  4542. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4543. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4544. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  4545. const int ib = i00/qk; // block index
  4546. const int iqs = (i00%qk)/qr; // quant index
  4547. const int iybs = i00 - i00%qk; // dst block start index
  4548. const int y_offset = qr == 1 ? 1 : qk/2;
  4549. // dequantize
  4550. dfloat2 v;
  4551. dequantize_kernel(src0_row, ib, iqs, v);
  4552. dst_row[iybs + iqs + 0] = v.x();
  4553. dst_row[iybs + iqs + y_offset] = v.y();
  4554. }
  4555. template<typename src0_t, typename dst_t>
  4556. static void k_get_rows_float(
  4557. const src0_t * src0, const int32_t * src1, dst_t * dst,
  4558. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4559. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4560. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4561. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4562. size_t s10, size_t s11, size_t s12,
  4563. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4564. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4565. item_ct1.get_local_id(2);
  4566. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4567. item_ct1.get_local_id(1);
  4568. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4569. item_ct1.get_local_id(0)) /
  4570. ne12;
  4571. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4572. item_ct1.get_local_id(0)) %
  4573. ne12;
  4574. if (i00 >= ne00) {
  4575. return;
  4576. }
  4577. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4578. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4579. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  4580. dst_row[i00] = src0_row[i00];
  4581. }
  4582. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4583. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4584. const sycl::nd_item<3> &item_ct1) {
  4585. const int i = 2 * (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4586. item_ct1.get_local_id(2));
  4587. if (i >= k) {
  4588. return;
  4589. }
  4590. const int ib = i/qk; // block index
  4591. const int iqs = (i%qk)/qr; // quant index
  4592. const int iybs = i - i%qk; // y block start index
  4593. const int y_offset = qr == 1 ? 1 : qk/2;
  4594. // dequantize
  4595. dfloat2 v;
  4596. dequantize_kernel(vx, ib, iqs, v);
  4597. y[iybs + iqs + 0] = v.x();
  4598. y[iybs + iqs + y_offset] = v.y();
  4599. }
  4600. template <typename src_t, typename dst_t>
  4601. static void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4602. const sycl::nd_item<3> &item_ct1) {
  4603. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4604. item_ct1.get_local_id(2);
  4605. if (i >= k) {
  4606. return;
  4607. }
  4608. const src_t * x = (src_t *) vx;
  4609. y[i] = x[i];
  4610. }
  4611. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  4612. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  4613. #define VDR_Q4_0_Q8_1_MMVQ 2
  4614. #define VDR_Q4_0_Q8_1_MMQ 4
  4615. template <int vdr>
  4616. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  4617. const float &d4,
  4618. const sycl::half2 &ds8) {
  4619. int sumi = 0;
  4620. #pragma unroll
  4621. for (int i = 0; i < vdr; ++i) {
  4622. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4623. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4624. // SIMD dot product of quantized values
  4625. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4626. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4627. }
  4628. const sycl::float2 ds8f =
  4629. ds8.convert<float, sycl::rounding_mode::automatic>();
  4630. // second part effectively subtracts 8 from each quant value
  4631. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  4632. }
  4633. #define VDR_Q4_1_Q8_1_MMVQ 2
  4634. #define VDR_Q4_1_Q8_1_MMQ 4
  4635. template <int vdr>
  4636. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  4637. const sycl::half2 &dm4,
  4638. const sycl::half2 &ds8) {
  4639. int sumi = 0;
  4640. #pragma unroll
  4641. for (int i = 0; i < vdr; ++i) {
  4642. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4643. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4644. // SIMD dot product of quantized values
  4645. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4646. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4647. }
  4648. #ifdef GGML_SYCL_F16
  4649. const sycl::float2 tmp =
  4650. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4651. const float d4d8 = tmp.x();
  4652. const float m4s8 = tmp.y();
  4653. #else
  4654. const sycl::float2 dm4f =
  4655. dm4.convert<float, sycl::rounding_mode::automatic>();
  4656. const sycl::float2 ds8f =
  4657. ds8.convert<float, sycl::rounding_mode::automatic>();
  4658. const float d4d8 = dm4f.x() * ds8f.x();
  4659. const float m4s8 = dm4f.y() * ds8f.y();
  4660. #endif // GGML_SYCL_F16
  4661. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  4662. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  4663. }
  4664. #define VDR_Q5_0_Q8_1_MMVQ 2
  4665. #define VDR_Q5_0_Q8_1_MMQ 4
  4666. template <int vdr>
  4667. static __dpct_inline__ float
  4668. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  4669. const float &d5, const sycl::half2 &ds8) {
  4670. int sumi = 0;
  4671. #pragma unroll
  4672. for (int i = 0; i < vdr; ++i) {
  4673. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4674. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4675. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4676. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4677. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4678. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4679. sumi); // SIMD dot product of quantized values
  4680. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4681. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4682. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4683. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4684. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4685. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4686. sumi); // SIMD dot product of quantized values
  4687. }
  4688. const sycl::float2 ds8f =
  4689. ds8.convert<float, sycl::rounding_mode::automatic>();
  4690. // second part effectively subtracts 16 from each quant value
  4691. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  4692. }
  4693. #define VDR_Q5_1_Q8_1_MMVQ 2
  4694. #define VDR_Q5_1_Q8_1_MMQ 4
  4695. template <int vdr>
  4696. static __dpct_inline__ float
  4697. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  4698. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  4699. int sumi = 0;
  4700. #pragma unroll
  4701. for (int i = 0; i < vdr; ++i) {
  4702. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4703. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4704. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4705. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4706. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4707. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4708. sumi); // SIMD dot product of quantized values
  4709. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4710. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4711. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4712. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4713. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4714. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4715. sumi); // SIMD dot product of quantized values
  4716. }
  4717. #ifdef GGML_SYCL_F16
  4718. const sycl::float2 tmp =
  4719. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4720. const float d5d8 = tmp.x();
  4721. const float m5s8 = tmp.y();
  4722. #else
  4723. const sycl::float2 dm5f =
  4724. dm5.convert<float, sycl::rounding_mode::automatic>();
  4725. const sycl::float2 ds8f =
  4726. ds8.convert<float, sycl::rounding_mode::automatic>();
  4727. const float d5d8 = dm5f.x() * ds8f.x();
  4728. const float m5s8 = dm5f.y() * ds8f.y();
  4729. #endif // GGML_SYCL_F16
  4730. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  4731. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  4732. }
  4733. #define VDR_Q8_0_Q8_1_MMVQ 2
  4734. #define VDR_Q8_0_Q8_1_MMQ 8
  4735. template <int vdr>
  4736. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  4737. const float &d8_0,
  4738. const float &d8_1) {
  4739. int sumi = 0;
  4740. #pragma unroll
  4741. for (int i = 0; i < vdr; ++i) {
  4742. // SIMD dot product of quantized values
  4743. sumi = dpct::dp4a(v[i], u[i], sumi);
  4744. }
  4745. return d8_0*d8_1 * sumi;
  4746. }
  4747. template <int vdr>
  4748. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  4749. const sycl::half2 &dm8,
  4750. const sycl::half2 &ds8) {
  4751. int sumi = 0;
  4752. #pragma unroll
  4753. for (int i = 0; i < vdr; ++i) {
  4754. // SIMD dot product of quantized values
  4755. sumi = dpct::dp4a(v[i], u[i], sumi);
  4756. }
  4757. #ifdef GGML_SYCL_F16
  4758. const sycl::float2 tmp =
  4759. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4760. const float d8d8 = tmp.x();
  4761. const float m8s8 = tmp.y();
  4762. #else
  4763. const sycl::float2 dm8f =
  4764. dm8.convert<float, sycl::rounding_mode::automatic>();
  4765. const sycl::float2 ds8f =
  4766. ds8.convert<float, sycl::rounding_mode::automatic>();
  4767. const float d8d8 = dm8f.x() * ds8f.x();
  4768. const float m8s8 = dm8f.y() * ds8f.y();
  4769. #endif // GGML_SYCL_F16
  4770. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  4771. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  4772. }
  4773. #define VDR_Q2_K_Q8_1_MMVQ 1
  4774. #define VDR_Q2_K_Q8_1_MMQ 2
  4775. // contiguous v/x values
  4776. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  4777. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  4778. const sycl::half2 &dm2, const float *__restrict__ d8) {
  4779. float sumf_d = 0.0f;
  4780. float sumf_m = 0.0f;
  4781. #pragma unroll
  4782. for (int i = 0; i < QR2_K; ++i) {
  4783. const int sc = scales[2*i];
  4784. const int vi = (v >> (2*i)) & 0x03030303;
  4785. sumf_d +=
  4786. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  4787. // fill int with 4x m
  4788. int m = sc >> 4;
  4789. m |= m << 8;
  4790. m |= m << 16;
  4791. sumf_m += d8[i] *
  4792. dpct::dp4a(
  4793. m, u[i],
  4794. 0); // multiply constant q2_K part with sum of q8_1 values
  4795. }
  4796. const sycl::float2 dm2f =
  4797. dm2.convert<float, sycl::rounding_mode::automatic>();
  4798. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  4799. }
  4800. // contiguous u/y values
  4801. static __dpct_inline__ float
  4802. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4803. const uint8_t *__restrict__ scales,
  4804. const sycl::half2 &dm2, const float &d8) {
  4805. int sumi_d = 0;
  4806. int sumi_m = 0;
  4807. #pragma unroll
  4808. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  4809. int sumi_d_sc = 0;
  4810. const int sc = scales[i0 / (QI8_1/2)];
  4811. // fill int with 4x m
  4812. int m = sc >> 4;
  4813. m |= m << 8;
  4814. m |= m << 16;
  4815. #pragma unroll
  4816. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4817. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  4818. sumi_m = dpct::dp4a(m, u[i],
  4819. sumi_m); // multiply sum of q8_1 values with m
  4820. }
  4821. sumi_d += sumi_d_sc * (sc & 0xF);
  4822. }
  4823. const sycl::float2 dm2f =
  4824. dm2.convert<float, sycl::rounding_mode::automatic>();
  4825. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  4826. }
  4827. #define VDR_Q3_K_Q8_1_MMVQ 1
  4828. #define VDR_Q3_K_Q8_1_MMQ 2
  4829. // contiguous v/x values
  4830. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  4831. const int &vl, const int &vh, const int *__restrict__ u,
  4832. const uint8_t *__restrict__ scales, const int &scale_offset,
  4833. const float &d3, const float *__restrict__ d8) {
  4834. float sumf = 0.0f;
  4835. #pragma unroll
  4836. for (int i = 0; i < QR3_K; ++i) {
  4837. const int isc = scale_offset + 2*i;
  4838. const int isc_low = isc % (QK_K/32);
  4839. const int sc_shift_low = 4 * (isc / (QK_K/32));
  4840. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  4841. const int isc_high = isc % (QK_K/64);
  4842. const int sc_shift_high = 2 * (isc / (QK_K/64));
  4843. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  4844. const int sc = (sc_low | sc_high) - 32;
  4845. const int vil = (vl >> (2*i)) & 0x03030303;
  4846. const int vih = ((vh >> i) << 2) & 0x04040404;
  4847. const int vi =
  4848. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  4849. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4850. }
  4851. return d3 * sumf;
  4852. }
  4853. // contiguous u/y values
  4854. static __dpct_inline__ float
  4855. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4856. const int8_t *__restrict__ scales, const float &d3,
  4857. const float &d8) {
  4858. int sumi = 0;
  4859. #pragma unroll
  4860. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  4861. int sumi_sc = 0;
  4862. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4863. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  4864. }
  4865. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  4866. }
  4867. return d3*d8 * sumi;
  4868. }
  4869. #define VDR_Q4_K_Q8_1_MMVQ 2
  4870. #define VDR_Q4_K_Q8_1_MMQ 8
  4871. // contiguous v/x values
  4872. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  4873. const int *__restrict__ v, const int *__restrict__ u,
  4874. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4875. const sycl::half2 &dm4, const float *__restrict__ d8) {
  4876. float sumf_d = 0.0f;
  4877. float sumf_m = 0.0f;
  4878. #pragma unroll
  4879. for (int i = 0; i < QR4_K; ++i) {
  4880. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  4881. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  4882. const int dot1 =
  4883. dpct::dp4a(v1i, u[2 * i + 1],
  4884. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  4885. const int dot2 =
  4886. dpct::dp4a(0x01010101, u[2 * i + 1],
  4887. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  4888. sumf_d += d8[i] * (dot1 * sc[i]);
  4889. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  4890. }
  4891. const sycl::float2 dm4f =
  4892. dm4.convert<float, sycl::rounding_mode::automatic>();
  4893. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4894. }
  4895. // contiguous u/y values
  4896. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  4897. const int *__restrict__ v, const int *__restrict__ u,
  4898. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4899. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4900. float sumf_d = 0.0f;
  4901. float sumf_m = 0.0f;
  4902. #pragma unroll
  4903. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  4904. int sumi_d = 0;
  4905. #pragma unroll
  4906. for (int j = 0; j < QI8_1; ++j) {
  4907. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  4908. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  4909. }
  4910. const sycl::float2 ds8f =
  4911. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4912. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4913. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4914. }
  4915. const sycl::float2 dm4f =
  4916. dm4.convert<float, sycl::rounding_mode::automatic>();
  4917. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4918. }
  4919. #define VDR_Q5_K_Q8_1_MMVQ 2
  4920. #define VDR_Q5_K_Q8_1_MMQ 8
  4921. // contiguous v/x values
  4922. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  4923. const int *__restrict__ vl, const int *__restrict__ vh,
  4924. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  4925. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  4926. const float *__restrict__ d8) {
  4927. float sumf_d = 0.0f;
  4928. float sumf_m = 0.0f;
  4929. #pragma unroll
  4930. for (int i = 0; i < QR5_K; ++i) {
  4931. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  4932. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  4933. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  4934. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  4935. const int v0i = vl0i | vh0i;
  4936. const int v1i = vl1i | vh1i;
  4937. const int dot1 =
  4938. dpct::dp4a(v0i, u[2 * i + 0],
  4939. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  4940. const int dot2 =
  4941. dpct::dp4a(0x01010101, u[2 * i + 0],
  4942. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  4943. sumf_d += d8[i] * (dot1 * sc[i]);
  4944. sumf_m += d8[i] * (dot2 * m[i]);
  4945. }
  4946. const sycl::float2 dm5f =
  4947. dm5.convert<float, sycl::rounding_mode::automatic>();
  4948. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  4949. }
  4950. // contiguous u/y values
  4951. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  4952. const int *__restrict__ v, const int *__restrict__ u,
  4953. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4954. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4955. float sumf_d = 0.0f;
  4956. float sumf_m = 0.0f;
  4957. #pragma unroll
  4958. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  4959. int sumi_d = 0;
  4960. #pragma unroll
  4961. for (int j = 0; j < QI8_1; ++j) {
  4962. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  4963. sumi_d); // SIMD dot product
  4964. }
  4965. const sycl::float2 ds8f =
  4966. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4967. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4968. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4969. }
  4970. const sycl::float2 dm4f =
  4971. dm4.convert<float, sycl::rounding_mode::automatic>();
  4972. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4973. }
  4974. #define VDR_Q6_K_Q8_1_MMVQ 1
  4975. #define VDR_Q6_K_Q8_1_MMQ 8
  4976. // contiguous v/x values
  4977. static __dpct_inline__ float
  4978. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  4979. const int *__restrict__ u,
  4980. const int8_t *__restrict__ scales, const float &d,
  4981. const float *__restrict__ d8) {
  4982. float sumf = 0.0f;
  4983. #pragma unroll
  4984. for (int i = 0; i < QR6_K; ++i) {
  4985. const int sc = scales[4*i];
  4986. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  4987. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  4988. const int vi = dpct::vectorized_binary<sycl::char4>(
  4989. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  4990. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4991. }
  4992. return d*sumf;
  4993. }
  4994. // contiguous u/y values
  4995. static __dpct_inline__ float
  4996. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4997. const int8_t *__restrict__ sc, const float &d6,
  4998. const float *__restrict__ d8) {
  4999. float sumf_d = 0.0f;
  5000. #pragma unroll
  5001. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  5002. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  5003. #pragma unroll
  5004. for (int i = i0; i < i0 + 2; ++i) {
  5005. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  5006. sumi_d.x()); // SIMD dot product
  5007. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  5008. sumi_d.x()); // SIMD dot product
  5009. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  5010. sumi_d.y()); // SIMD dot product
  5011. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  5012. sumi_d.y()); // SIMD dot product
  5013. }
  5014. sumf_d += d8[i0 / 4] *
  5015. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  5016. }
  5017. return d6 * sumf_d;
  5018. }
  5019. static __dpct_inline__ float
  5020. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  5021. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5022. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  5023. int v[VDR_Q4_0_Q8_1_MMVQ];
  5024. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  5025. #pragma unroll
  5026. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  5027. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  5028. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5029. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  5030. }
  5031. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  5032. }
  5033. template <int mmq_y>
  5034. static __dpct_inline__ void
  5035. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5036. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  5037. (void)x_qh; (void)x_sc;
  5038. *x_ql = tile_x_qs_q4_0;
  5039. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  5040. }
  5041. template <int mmq_y, int nwarps, bool need_check>
  5042. static __dpct_inline__ void
  5043. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5044. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5045. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5046. const int &k, const int &blocks_per_row) {
  5047. (void)x_qh; (void)x_sc;
  5048. GGML_SYCL_ASSUME(i_offset >= 0);
  5049. GGML_SYCL_ASSUME(i_offset < nwarps);
  5050. GGML_SYCL_ASSUME(k >= 0);
  5051. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5052. const int kbx = k / QI4_0;
  5053. const int kqsx = k % QI4_0;
  5054. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  5055. float * x_dmf = (float *) x_dm;
  5056. #pragma unroll
  5057. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5058. int i = i0 + i_offset;
  5059. if (need_check) {
  5060. i = sycl::min(i, i_max);
  5061. }
  5062. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5063. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5064. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  5065. }
  5066. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  5067. const int kbxd = k % blocks_per_tile_x_row;
  5068. #pragma unroll
  5069. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  5070. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  5071. if (need_check) {
  5072. i = sycl::min(i, i_max);
  5073. }
  5074. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5075. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  5076. }
  5077. }
  5078. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  5079. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5080. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5081. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5082. const int &i, const int &j, const int &k) {
  5083. (void)x_qh; (void)x_sc;
  5084. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5085. const float * x_dmf = (const float *) x_dm;
  5086. int u[2*VDR_Q4_0_Q8_1_MMQ];
  5087. #pragma unroll
  5088. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  5089. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5090. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  5091. }
  5092. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  5093. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  5094. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5095. }
  5096. static __dpct_inline__ float
  5097. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  5098. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5099. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  5100. int v[VDR_Q4_1_Q8_1_MMVQ];
  5101. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  5102. #pragma unroll
  5103. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  5104. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  5105. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5106. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  5107. }
  5108. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  5109. }
  5110. template <int mmq_y>
  5111. static __dpct_inline__ void
  5112. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5113. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  5114. (void)x_qh; (void)x_sc;
  5115. *x_ql = tile_x_qs_q4_1;
  5116. *x_dm = tile_x_dm_q4_1;
  5117. }
  5118. template <int mmq_y, int nwarps, bool need_check>
  5119. static __dpct_inline__ void
  5120. load_tiles_q4_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5121. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5122. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5123. const int &k, const int &blocks_per_row) {
  5124. (void)x_qh; (void)x_sc;
  5125. GGML_SYCL_ASSUME(i_offset >= 0);
  5126. GGML_SYCL_ASSUME(i_offset < nwarps);
  5127. GGML_SYCL_ASSUME(k >= 0);
  5128. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5129. const int kbx = k / QI4_1;
  5130. const int kqsx = k % QI4_1;
  5131. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  5132. #pragma unroll
  5133. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5134. int i = i0 + i_offset;
  5135. if (need_check) {
  5136. i = sycl::min(i, i_max);
  5137. }
  5138. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5139. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5140. }
  5141. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  5142. const int kbxd = k % blocks_per_tile_x_row;
  5143. #pragma unroll
  5144. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  5145. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  5146. if (need_check) {
  5147. i = sycl::min(i, i_max);
  5148. }
  5149. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5150. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  5151. }
  5152. }
  5153. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  5154. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5155. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5156. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5157. const int &i, const int &j, const int &k) {
  5158. (void)x_qh; (void)x_sc;
  5159. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5160. int u[2*VDR_Q4_1_Q8_1_MMQ];
  5161. #pragma unroll
  5162. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  5163. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5164. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  5165. }
  5166. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  5167. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  5168. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5169. }
  5170. static __dpct_inline__ float
  5171. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  5172. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5173. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  5174. int vl[VDR_Q5_0_Q8_1_MMVQ];
  5175. int vh[VDR_Q5_0_Q8_1_MMVQ];
  5176. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  5177. #pragma unroll
  5178. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  5179. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  5180. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  5181. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5182. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  5183. }
  5184. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  5185. }
  5186. template <int mmq_y>
  5187. static __dpct_inline__ void
  5188. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5189. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  5190. (void)x_qh; (void)x_sc;
  5191. *x_ql = tile_x_ql_q5_0;
  5192. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  5193. }
  5194. template <int mmq_y, int nwarps, bool need_check>
  5195. static __dpct_inline__ void
  5196. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5197. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5198. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5199. const int &k, const int &blocks_per_row) {
  5200. (void)x_qh; (void)x_sc;
  5201. GGML_SYCL_ASSUME(i_offset >= 0);
  5202. GGML_SYCL_ASSUME(i_offset < nwarps);
  5203. GGML_SYCL_ASSUME(k >= 0);
  5204. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5205. const int kbx = k / QI5_0;
  5206. const int kqsx = k % QI5_0;
  5207. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5208. #pragma unroll
  5209. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5210. int i = i0 + i_offset;
  5211. if (need_check) {
  5212. i = sycl::min(i, i_max);
  5213. }
  5214. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5215. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5216. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5217. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5218. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5219. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5220. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5221. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5222. qs0 = dpct::vectorized_binary<sycl::char4>(
  5223. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5224. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5225. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5226. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5227. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5228. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5229. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5230. qs1 = dpct::vectorized_binary<sycl::char4>(
  5231. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  5232. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5233. }
  5234. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  5235. const int kbxd = k % blocks_per_tile_x_row;
  5236. float * x_dmf = (float *) x_dm;
  5237. #pragma unroll
  5238. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  5239. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  5240. if (need_check) {
  5241. i = sycl::min(i, i_max);
  5242. }
  5243. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5244. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  5245. }
  5246. }
  5247. static __dpct_inline__ float vec_dot_q5_0_q8_1_mul_mat(
  5248. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5249. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5250. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5251. const int &i, const int &j, const int &k) {
  5252. (void)x_qh; (void)x_sc;
  5253. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5254. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  5255. const float * x_dmf = (const float *) x_dm;
  5256. const float * y_df = (const float *) y_ds;
  5257. int u[2*VDR_Q5_0_Q8_1_MMQ];
  5258. #pragma unroll
  5259. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  5260. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5261. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  5262. }
  5263. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  5264. (&x_ql[i * (2*WARP_SIZE + 1) + 2 * k], u, x_dmf[index_bx], y_df[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5265. }
  5266. static __dpct_inline__ float
  5267. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  5268. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5269. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  5270. int vl[VDR_Q5_1_Q8_1_MMVQ];
  5271. int vh[VDR_Q5_1_Q8_1_MMVQ];
  5272. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  5273. #pragma unroll
  5274. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  5275. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  5276. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  5277. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5278. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  5279. }
  5280. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  5281. }
  5282. template <int mmq_y>
  5283. static __dpct_inline__ void
  5284. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5285. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  5286. (void)x_qh; (void)x_sc;
  5287. *x_ql = tile_x_ql_q5_1;
  5288. *x_dm = tile_x_dm_q5_1;
  5289. }
  5290. template <int mmq_y, int nwarps, bool need_check>
  5291. static __dpct_inline__ void
  5292. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5293. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5294. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5295. const int &k, const int &blocks_per_row) {
  5296. (void)x_qh; (void)x_sc;
  5297. GGML_SYCL_ASSUME(i_offset >= 0);
  5298. GGML_SYCL_ASSUME(i_offset < nwarps);
  5299. GGML_SYCL_ASSUME(k >= 0);
  5300. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5301. const int kbx = k / QI5_1;
  5302. const int kqsx = k % QI5_1;
  5303. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  5304. #pragma unroll
  5305. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5306. int i = i0 + i_offset;
  5307. if (need_check) {
  5308. i = sycl::min(i, i_max);
  5309. }
  5310. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5311. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5312. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  5313. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5314. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5315. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5316. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5317. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5318. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5319. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5320. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5321. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5322. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5323. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5324. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5325. }
  5326. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  5327. const int kbxd = k % blocks_per_tile_x_row;
  5328. #pragma unroll
  5329. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  5330. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  5331. if (need_check) {
  5332. i = sycl::min(i, i_max);
  5333. }
  5334. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5335. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  5336. }
  5337. }
  5338. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  5339. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5340. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5341. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5342. const int &i, const int &j, const int &k) {
  5343. (void)x_qh; (void)x_sc;
  5344. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5345. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  5346. int u[2*VDR_Q5_1_Q8_1_MMQ];
  5347. #pragma unroll
  5348. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  5349. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5350. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  5351. }
  5352. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  5353. (&x_ql[i * (2*WARP_SIZE + 1) + 2 * k], u, x_dm[index_bx], y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5354. }
  5355. static __dpct_inline__ float
  5356. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  5357. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5358. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  5359. int v[VDR_Q8_0_Q8_1_MMVQ];
  5360. int u[VDR_Q8_0_Q8_1_MMVQ];
  5361. #pragma unroll
  5362. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  5363. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  5364. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5365. }
  5366. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  5367. bq8_1->ds[0]);
  5368. }
  5369. template <int mmq_y>
  5370. static __dpct_inline__ void
  5371. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5372. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  5373. (void)x_qh; (void)x_sc;
  5374. *x_ql = tile_x_qs_q8_0;
  5375. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  5376. }
  5377. template <int mmq_y, int nwarps, bool need_check>
  5378. static __dpct_inline__ void
  5379. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5380. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5381. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5382. const int &k, const int &blocks_per_row) {
  5383. (void)x_qh; (void)x_sc;
  5384. GGML_SYCL_ASSUME(i_offset >= 0);
  5385. GGML_SYCL_ASSUME(i_offset < nwarps);
  5386. GGML_SYCL_ASSUME(k >= 0);
  5387. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5388. const int kbx = k / QI8_0;
  5389. const int kqsx = k % QI8_0;
  5390. float * x_dmf = (float *) x_dm;
  5391. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  5392. #pragma unroll
  5393. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5394. int i = i0 + i_offset;
  5395. if (need_check) {
  5396. i = sycl::min(i, i_max);
  5397. }
  5398. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5399. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  5400. }
  5401. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  5402. const int kbxd = k % blocks_per_tile_x_row;
  5403. #pragma unroll
  5404. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  5405. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  5406. if (need_check) {
  5407. i = sycl::min(i, i_max);
  5408. }
  5409. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5410. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  5411. }
  5412. }
  5413. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  5414. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5415. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5416. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5417. const int &i, const int &j, const int &k) {
  5418. (void)x_qh; (void)x_sc;
  5419. const float * x_dmf = (const float *) x_dm;
  5420. const float * y_df = (const float *) y_ds;
  5421. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  5422. (&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[j * WARP_SIZE + k], x_dmf[i * (WARP_SIZE/QI8_0) + i/QI8_0 + k/QI8_0],
  5423. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  5424. }
  5425. static __dpct_inline__ float
  5426. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  5427. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5428. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  5429. const int bq8_offset = QR2_K * (iqs / QI8_1);
  5430. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5431. const uint8_t * scales = bq2_K->scales + scale_offset;
  5432. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  5433. int u[QR2_K];
  5434. float d8[QR2_K];
  5435. #pragma unroll
  5436. for (int i = 0; i < QR2_K; ++ i) {
  5437. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5438. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5439. }
  5440. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  5441. }
  5442. template <int mmq_y>
  5443. static __dpct_inline__ void
  5444. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5445. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  5446. int *tile_x_sc_q2_K) {
  5447. (void)x_qh;
  5448. *x_ql = tile_x_ql_q2_K;
  5449. *x_dm = tile_x_dm_q2_K;
  5450. *x_sc = tile_x_sc_q2_K;
  5451. }
  5452. template <int mmq_y, int nwarps, bool need_check>
  5453. static __dpct_inline__ void
  5454. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5455. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5456. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5457. const int &k, const int &blocks_per_row) {
  5458. (void)x_qh;
  5459. GGML_SYCL_ASSUME(i_offset >= 0);
  5460. GGML_SYCL_ASSUME(i_offset < nwarps);
  5461. GGML_SYCL_ASSUME(k >= 0);
  5462. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5463. const int kbx = k / QI2_K;
  5464. const int kqsx = k % QI2_K;
  5465. const block_q2_K * bx0 = (const block_q2_K *) vx;
  5466. #pragma unroll
  5467. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5468. int i = i0 + i_offset;
  5469. if (need_check) {
  5470. i = sycl::min(i, i_max);
  5471. }
  5472. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  5473. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5474. }
  5475. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  5476. const int kbxd = k % blocks_per_tile_x_row;
  5477. #pragma unroll
  5478. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  5479. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  5480. if (need_check) {
  5481. i = sycl::min(i, i_max);
  5482. }
  5483. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5484. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  5485. }
  5486. #pragma unroll
  5487. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5488. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5489. if (need_check) {
  5490. i = sycl::min(i, i_max);
  5491. }
  5492. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  5493. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  5494. }
  5495. }
  5496. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  5497. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5498. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5499. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5500. const int &i, const int &j, const int &k) {
  5501. (void)x_qh;
  5502. const int kbx = k / QI2_K;
  5503. const int ky = (k % QI2_K) * QR2_K;
  5504. const float * y_df = (const float *) y_ds;
  5505. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  5506. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  5507. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  5508. #pragma unroll
  5509. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  5510. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5511. }
  5512. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  5513. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  5514. return vec_dot_q2_K_q8_1_impl_mmq(v, &y_qs[index_y], scales, x_dm[i * (WARP_SIZE/QI2_K) + i/QI2_K + kbx], y_df[index_y/QI8_1]);
  5515. }
  5516. static __dpct_inline__ float
  5517. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  5518. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5519. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  5520. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  5521. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5522. const float d = bq3_K->d;
  5523. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  5524. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5525. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  5526. int u[QR3_K];
  5527. float d8[QR3_K];
  5528. #pragma unroll
  5529. for (int i = 0; i < QR3_K; ++i) {
  5530. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5531. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5532. }
  5533. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  5534. }
  5535. template <int mmq_y>
  5536. static __dpct_inline__ void
  5537. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5538. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  5539. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  5540. *x_ql = tile_x_ql_q3_K;
  5541. *x_dm = tile_x_dm_q3_K;
  5542. *x_qh = tile_x_qh_q3_K;
  5543. *x_sc = tile_x_sc_q3_K;
  5544. }
  5545. template <int mmq_y, int nwarps, bool need_check>
  5546. static __dpct_inline__ void
  5547. load_tiles_q3_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5548. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5549. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5550. const int &k, const int &blocks_per_row) {
  5551. GGML_SYCL_ASSUME(i_offset >= 0);
  5552. GGML_SYCL_ASSUME(i_offset < nwarps);
  5553. GGML_SYCL_ASSUME(k >= 0);
  5554. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5555. const int kbx = k / QI3_K;
  5556. const int kqsx = k % QI3_K;
  5557. const block_q3_K * bx0 = (const block_q3_K *) vx;
  5558. #pragma unroll
  5559. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5560. int i = i0 + i_offset;
  5561. if (need_check) {
  5562. i = sycl::min(i, i_max);
  5563. }
  5564. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  5565. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5566. }
  5567. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  5568. const int kbxd = k % blocks_per_tile_x_row;
  5569. float * x_dmf = (float *) x_dm;
  5570. #pragma unroll
  5571. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  5572. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  5573. if (need_check) {
  5574. i = sycl::min(i, i_max);
  5575. }
  5576. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5577. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  5578. }
  5579. #pragma unroll
  5580. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  5581. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  5582. if (need_check) {
  5583. i = sycl::min(i, i_max);
  5584. }
  5585. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  5586. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5587. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  5588. }
  5589. #pragma unroll
  5590. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5591. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5592. if (need_check) {
  5593. i = sycl::min(i, i_max);
  5594. }
  5595. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  5596. const int ksc = k % (QI3_K/4);
  5597. const int ksc_low = ksc % (QI3_K/8);
  5598. const int shift_low = 4 * (ksc / (QI3_K/8));
  5599. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  5600. const int ksc_high = QI3_K/8;
  5601. const int shift_high = 2 * ksc;
  5602. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  5603. const int sc = dpct::vectorized_binary<sycl::char4>(
  5604. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  5605. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  5606. }
  5607. }
  5608. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  5609. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5610. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5611. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5612. const int &i, const int &j, const int &k) {
  5613. const int kbx = k / QI3_K;
  5614. const int ky = (k % QI3_K) * QR3_K;
  5615. const float * x_dmf = (const float *) x_dm;
  5616. const float * y_df = (const float *) y_ds;
  5617. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  5618. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  5619. #pragma unroll
  5620. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  5621. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  5622. const int shift = 2 * ((ky % 32) / 8);
  5623. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5624. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  5625. const int vlh = (vh << 2) & 0x04040404;
  5626. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  5627. }
  5628. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  5629. return vec_dot_q3_K_q8_1_impl_mmq(v, &y_qs[index_y], scales, x_dmf[i * (WARP_SIZE/QI3_K) + i/QI3_K + kbx], y_df[index_y/QI8_1]);
  5630. }
  5631. static __dpct_inline__ float
  5632. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  5633. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5634. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5635. int v[2];
  5636. int u[2*QR4_K];
  5637. float d8[QR4_K];
  5638. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  5639. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  5640. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  5641. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  5642. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  5643. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  5644. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5645. v[0] = q4[0];
  5646. v[1] = q4[4];
  5647. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  5648. uint16_t aux[2];
  5649. const int j = bq8_offset/2;
  5650. if (j < 2) {
  5651. aux[0] = scales[j+0] & 0x3f3f;
  5652. aux[1] = scales[j+2] & 0x3f3f;
  5653. } else {
  5654. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5655. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5656. }
  5657. const uint8_t * sc = (const uint8_t *)aux;
  5658. const uint8_t * m = sc + 2;
  5659. for (int i = 0; i < QR4_K; ++i) {
  5660. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5661. d8[i] = bq8i->ds[0];
  5662. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5663. u[2*i+0] = q8[0];
  5664. u[2*i+1] = q8[4];
  5665. }
  5666. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  5667. }
  5668. template <int mmq_y>
  5669. static __dpct_inline__ void
  5670. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5671. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  5672. int *tile_x_sc_q4_K) {
  5673. (void)x_qh;
  5674. *x_ql = tile_x_ql_q4_K;
  5675. *x_dm = tile_x_dm_q4_K;
  5676. *x_sc = tile_x_sc_q4_K;
  5677. }
  5678. template <int mmq_y, int nwarps, bool need_check>
  5679. static __dpct_inline__ void
  5680. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5681. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5682. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5683. const int &k, const int &blocks_per_row) {
  5684. (void)x_qh;
  5685. GGML_SYCL_ASSUME(i_offset >= 0);
  5686. GGML_SYCL_ASSUME(i_offset < nwarps);
  5687. GGML_SYCL_ASSUME(k >= 0);
  5688. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5689. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  5690. const int kqsx = k % QI4_K; // == k if QK_K == 256
  5691. const block_q4_K * bx0 = (const block_q4_K *) vx;
  5692. #pragma unroll
  5693. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5694. int i = i0 + i_offset;
  5695. if (need_check) {
  5696. i = sycl::min(i, i_max);
  5697. }
  5698. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  5699. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5700. }
  5701. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  5702. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5703. #pragma unroll
  5704. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  5705. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  5706. if (need_check) {
  5707. i = sycl::min(i, i_max);
  5708. }
  5709. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5710. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  5711. }
  5712. #pragma unroll
  5713. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5714. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5715. if (need_check) {
  5716. i = sycl::min(i, i_max);
  5717. }
  5718. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  5719. const int * scales = (const int *) bxi->scales;
  5720. const int ksc = k % (WARP_SIZE/8);
  5721. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5722. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5723. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5724. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5725. }
  5726. }
  5727. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  5728. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5729. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5730. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5731. const int &i, const int &j, const int &k) {
  5732. (void)x_qh;
  5733. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  5734. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  5735. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  5736. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  5737. }
  5738. static __dpct_inline__ float
  5739. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  5740. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5741. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5742. int vl[2];
  5743. int vh[2];
  5744. int u[2*QR5_K];
  5745. float d8[QR5_K];
  5746. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  5747. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5748. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  5749. vl[0] = ql[0];
  5750. vl[1] = ql[4];
  5751. vh[0] = qh[0] >> bq8_offset;
  5752. vh[1] = qh[4] >> bq8_offset;
  5753. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  5754. uint16_t aux[2];
  5755. const int j = bq8_offset/2;
  5756. if (j < 2) {
  5757. aux[0] = scales[j+0] & 0x3f3f;
  5758. aux[1] = scales[j+2] & 0x3f3f;
  5759. } else {
  5760. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5761. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5762. }
  5763. const uint8_t * sc = (const uint8_t *)aux;
  5764. const uint8_t * m = sc + 2;
  5765. #pragma unroll
  5766. for (int i = 0; i < QR5_K; ++i) {
  5767. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5768. d8[i] = bq8i->ds[0];
  5769. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5770. u[2*i+0] = q8[0];
  5771. u[2*i+1] = q8[4];
  5772. }
  5773. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  5774. }
  5775. template <int mmq_y>
  5776. static __dpct_inline__ void
  5777. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5778. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  5779. int *tile_x_sc_q5_K) {
  5780. (void)x_qh;
  5781. *x_ql = tile_x_ql_q5_K;
  5782. *x_dm = tile_x_dm_q5_K;
  5783. *x_sc = tile_x_sc_q5_K;
  5784. }
  5785. template <int mmq_y, int nwarps, bool need_check>
  5786. static __dpct_inline__ void
  5787. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5788. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5789. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5790. const int &k, const int &blocks_per_row) {
  5791. (void)x_qh;
  5792. GGML_SYCL_ASSUME(i_offset >= 0);
  5793. GGML_SYCL_ASSUME(i_offset < nwarps);
  5794. GGML_SYCL_ASSUME(k >= 0);
  5795. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5796. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  5797. const int kqsx = k % QI5_K; // == k if QK_K == 256
  5798. const block_q5_K * bx0 = (const block_q5_K *) vx;
  5799. #pragma unroll
  5800. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5801. int i = i0 + i_offset;
  5802. if (need_check) {
  5803. i = sycl::min(i, i_max);
  5804. }
  5805. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  5806. const int ky = QR5_K*kqsx;
  5807. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5808. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5809. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5810. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  5811. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  5812. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  5813. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  5814. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  5815. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  5816. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  5817. }
  5818. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  5819. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5820. #pragma unroll
  5821. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  5822. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  5823. if (need_check) {
  5824. i = sycl::min(i, i_max);
  5825. }
  5826. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5827. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  5828. }
  5829. #pragma unroll
  5830. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5831. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5832. if (need_check) {
  5833. i = sycl::min(i, i_max);
  5834. }
  5835. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  5836. const int * scales = (const int *) bxi->scales;
  5837. const int ksc = k % (WARP_SIZE/8);
  5838. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5839. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5840. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5841. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5842. }
  5843. }
  5844. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  5845. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5846. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5847. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5848. const int &i, const int &j, const int &k) {
  5849. (void)x_qh;
  5850. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  5851. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  5852. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  5853. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  5854. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  5855. }
  5856. static __dpct_inline__ float
  5857. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  5858. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5859. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  5860. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  5861. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  5862. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  5863. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  5864. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  5865. const int8_t * scales = bq6_K->scales + scale_offset;
  5866. int u[QR6_K];
  5867. float d8[QR6_K];
  5868. #pragma unroll
  5869. for (int i = 0; i < QR6_K; ++i) {
  5870. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  5871. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  5872. }
  5873. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  5874. }
  5875. template <int mmq_y>
  5876. static __dpct_inline__ void
  5877. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5878. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  5879. (void)x_qh;
  5880. *x_ql = tile_x_ql;
  5881. *x_dm = tile_x_dm;
  5882. *x_sc = tile_x_sc;
  5883. }
  5884. template <int mmq_y, int nwarps, bool need_check>
  5885. static __dpct_inline__ void
  5886. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5887. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5888. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5889. const int &k, const int &blocks_per_row) {
  5890. (void)x_qh;
  5891. GGML_SYCL_ASSUME(i_offset >= 0);
  5892. GGML_SYCL_ASSUME(i_offset < nwarps);
  5893. GGML_SYCL_ASSUME(k >= 0);
  5894. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5895. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  5896. const int kqsx = k % QI6_K; // == k if QK_K == 256
  5897. const block_q6_K * bx0 = (const block_q6_K *) vx;
  5898. #pragma unroll
  5899. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5900. int i = i0 + i_offset;
  5901. if (need_check) {
  5902. i = sycl::min(i, i_max);
  5903. }
  5904. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  5905. const int ky = QR6_K*kqsx;
  5906. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  5907. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5908. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5909. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  5910. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  5911. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  5912. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  5913. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  5914. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  5915. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  5916. dpct::sub_sat());
  5917. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  5918. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  5919. dpct::sub_sat());
  5920. }
  5921. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  5922. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5923. float * x_dmf = (float *) x_dm;
  5924. #pragma unroll
  5925. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  5926. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  5927. if (need_check) {
  5928. i = sycl::min(i, i_max);
  5929. }
  5930. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5931. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  5932. }
  5933. #pragma unroll
  5934. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5935. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5936. if (need_check) {
  5937. i = sycl::min(i, i_max);
  5938. }
  5939. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  5940. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  5941. }
  5942. }
  5943. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  5944. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5945. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5946. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5947. const int &i, const int &j, const int &k) {
  5948. (void)x_qh;
  5949. const float * x_dmf = (const float *) x_dm;
  5950. const float * y_df = (const float *) y_ds;
  5951. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  5952. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  5953. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  5954. return vec_dot_q6_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, x_dmf[i * (WARP_SIZE/QI6_K) + i/QI6_K], &y_df[index_y/QI8_1]);
  5955. }
  5956. static __dpct_inline__ float
  5957. vec_dot_iq2_xxs_q8_1(const void *__restrict__ vbq,
  5958. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  5959. const uint64_t *iq2xxs_grid, const uint8_t *ksigns_iq2xs,
  5960. const uint8_t *kmask_iq2xs) {
  5961. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  5962. #if QR2_XXS == 8
  5963. const int ib32 = iqs;
  5964. const uint16_t * q2 = bq2->qs + 4*ib32;
  5965. const uint8_t * aux8 = (const uint8_t *)q2;
  5966. const int8_t * q8 = bq8_1[ib32].qs;
  5967. uint32_t aux32 = q2[2] | (q2[3] << 16);
  5968. int sumi = 0;
  5969. for (int l = 0; l < 4; ++l) {
  5970. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  5971. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  5972. for (int j = 0; j < 8; ++j) {
  5973. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  5974. }
  5975. q8 += 8;
  5976. aux32 >>= 7;
  5977. }
  5978. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.25f;
  5979. return d * sumi;
  5980. #else
  5981. // iqs is 0...15
  5982. const int ib32 = iqs/2;
  5983. const int il = iqs%2;
  5984. const uint16_t * q2 = bq2->qs + 4*ib32;
  5985. const uint8_t * aux8 = (const uint8_t *)q2;
  5986. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  5987. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  5988. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  5989. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * bq8_1[ib32].ds[0] * 0.25f;
  5990. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  5991. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  5992. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  5993. int sumi1 = 0, sumi2 = 0;
  5994. for (int j = 0; j < 8; ++j) {
  5995. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  5996. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  5997. }
  5998. return d * (sumi1 + sumi2);
  5999. #endif
  6000. }
  6001. static __dpct_inline__ float
  6002. vec_dot_iq2_xs_q8_1(const void *__restrict__ vbq,
  6003. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6004. const uint64_t *iq2xs_grid, const uint64_t *ksigns64) {
  6005. #if DPCT_COMPATIBILITY_TEMP >= \
  6006. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6007. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  6008. const int ib32 = iqs;
  6009. const uint16_t * q2 = bq2->qs + 4*ib32;
  6010. const int8_t * q8 = bq8_1[ib32].qs;
  6011. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6012. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6013. int sumi1 = 0;
  6014. for (int l = 0; l < 2; ++l) {
  6015. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6016. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6017. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6018. grid[0] ^ signs[0], signs[0], std::minus<>());
  6019. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6020. grid[1] ^ signs[1], signs[1], std::minus<>());
  6021. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6022. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6023. q8 += 8;
  6024. }
  6025. int sumi2 = 0;
  6026. for (int l = 2; l < 4; ++l) {
  6027. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6028. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6029. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6030. grid[0] ^ signs[0], signs[0], std::minus<>());
  6031. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6032. grid[1] ^ signs[1], signs[1], std::minus<>());
  6033. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6034. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6035. q8 += 8;
  6036. }
  6037. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6038. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6039. #else
  6040. assert(false);
  6041. return 0.f;
  6042. #endif
  6043. }
  6044. static __dpct_inline__ float
  6045. vec_dot_iq2_s_q8_1(const void *__restrict__ vbq,
  6046. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6047. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  6048. const int ib32 = iqs;
  6049. const int8_t * q8 = bq8_1[ib32].qs;
  6050. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  6051. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6052. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6053. int sumi1 = 0;
  6054. for (int l = 0; l < 2; ++l) {
  6055. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  6056. const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6057. ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201,
  6058. std::equal_to<>());
  6059. const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6060. ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201,
  6061. std::equal_to<>());
  6062. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6063. grid[0] ^ signs0, signs0, std::minus<>());
  6064. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6065. grid[1] ^ signs1, signs1, std::minus<>());
  6066. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6067. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6068. q8 += 8;
  6069. }
  6070. int sumi2 = 0;
  6071. for (int l = 2; l < 4; ++l) {
  6072. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  6073. const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6074. ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201,
  6075. std::equal_to<>());
  6076. const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6077. ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201,
  6078. std::equal_to<>());
  6079. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6080. grid[0] ^ signs0, signs0, std::minus<>());
  6081. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6082. grid[1] ^ signs1, signs1, std::minus<>());
  6083. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6084. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6085. q8 += 8;
  6086. }
  6087. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6088. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6089. }
  6090. static __dpct_inline__ float
  6091. vec_dot_iq3_xxs_q8_1(const void *__restrict__ vbq,
  6092. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6093. const uint32_t *iq3xxs_grid, const uint64_t *ksigns64) {
  6094. #if DPCT_COMPATIBILITY_TEMP >= \
  6095. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6096. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  6097. const int ib32 = iqs;
  6098. const uint8_t * q3 = bq2->qs + 8*ib32;
  6099. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  6100. const int8_t * q8 = bq8_1[ib32].qs;
  6101. uint32_t aux32 = gas[0] | (gas[1] << 16);
  6102. int sumi = 0;
  6103. for (int l = 0; l < 4; ++l) {
  6104. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  6105. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  6106. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  6107. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6108. grid1[0] ^ signs[0], signs[0], std::minus<>());
  6109. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6110. grid2[0] ^ signs[1], signs[1], std::minus<>());
  6111. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6112. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6113. q8 += 8;
  6114. aux32 >>= 7;
  6115. }
  6116. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.5f;
  6117. return d * sumi;
  6118. #else
  6119. assert(false);
  6120. return 0.f;
  6121. #endif
  6122. }
  6123. static __dpct_inline__ float
  6124. vec_dot_iq3_s_q8_1(const void *__restrict__ vbq,
  6125. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6126. const uint32_t *iq3s_grid) {
  6127. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  6128. const int ib32 = iqs;
  6129. const uint8_t * qs = bq2->qs + 8*ib32;
  6130. const int8_t * q8 = bq8_1[ib32].qs;
  6131. int sumi = 0;
  6132. for (int l = 0; l < 4; ++l) {
  6133. const uint32_t * grid1 = iq3s_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  6134. const uint32_t * grid2 = iq3s_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  6135. uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6136. ((bq2->signs[4 * ib32 + l] & 0xf) * 0x01010101) & 0x08040201,
  6137. 0x08040201, std::equal_to<>());
  6138. uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6139. ((bq2->signs[4 * ib32 + l] >> 4) * 0x01010101) & 0x08040201,
  6140. 0x08040201, std::equal_to<>());
  6141. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6142. grid1[0] ^ signs0, signs0, std::minus<>());
  6143. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6144. grid2[0] ^ signs1, signs1, std::minus<>());
  6145. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6146. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6147. q8 += 8;
  6148. }
  6149. const float d =
  6150. (float)bq2->d *
  6151. (1 + 2 * ((bq2->scales[ib32 / 2] >> 4 * (ib32 % 2)) & 0xf)) *
  6152. bq8_1[ib32].ds[0];
  6153. return d * sumi;
  6154. }
  6155. static __dpct_inline__ float
  6156. vec_dot_iq1_s_q8_1(const void *__restrict__ vbq,
  6157. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6158. const uint32_t *iq1s_grid_gpu) {
  6159. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  6160. const int ib32 = iqs;
  6161. int sumi = 0;
  6162. const int * q8 = (const int *)bq8_1[ib32].qs;
  6163. for (int l = 0; l < 4; ++l) {
  6164. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  6165. int grid0 = grid[0] & 0x0f0f0f0f;
  6166. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  6167. sumi = dpct::dp4a(q8[2 * l + 1], grid1,
  6168. dpct::dp4a(q8[2 * l + 0], grid0, sumi));
  6169. }
  6170. const float delta = bq1->qh[ib32] & 0x8000 ? -1-IQ1S_DELTA : -1+IQ1S_DELTA;
  6171. const float d1q = (float)bq1->d * (2*((bq1->qh[ib32] >> 12) & 7) + 1);
  6172. const float d = d1q * bq8_1[ib32].ds[0];
  6173. const float m = d1q * bq8_1[ib32].ds[1];
  6174. return d * sumi + m * delta;
  6175. }
  6176. static __dpct_inline__ float
  6177. vec_dot_iq1_m_q8_1(const void *__restrict__ vbq,
  6178. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6179. const block_iq1_m * bq1 = (const block_iq1_m *) vbq;
  6180. const int ib32 = iqs;
  6181. int sumi[2] = {0, 0};
  6182. float sumf[2] = {0.f, 0.f};
  6183. const int * q8 = (const int *)bq8_1[ib32].qs;
  6184. for (int l = 0; l < 4; ++l) {
  6185. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 7) << 8)));
  6186. int grid0 = grid[0] & 0x0f0f0f0f;
  6187. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  6188. sumi[l / 2] = dpct::dp4a(q8[2 * l + 1], grid1,
  6189. dpct::dp4a(q8[2 * l + 0], grid0, sumi[l / 2]));
  6190. const float delta = (bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 0x08 ? -1-IQ1M_DELTA : -1+IQ1M_DELTA;
  6191. const int sumy = dpct::dp4a(q8[2 * l + 1], 0x01010101,
  6192. dpct::dp4a(q8[2 * l + 0], 0x01010101, 0));
  6193. sumf[l/2] += delta*sumy;
  6194. }
  6195. iq1m_scale_t scale;
  6196. const uint16_t * sc = (const uint16_t *)bq1->scales;
  6197. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  6198. const float d = (float)scale.f16 * bq8_1[ib32].ds[0];
  6199. return d * ((sumi[0] + sumf[0]) * (2*((sc[ib32/2] >> 6*(ib32%2)) & 0x7) + 1) + (sumi[1] + sumf[1]) * (2*((sc[ib32/2] >> (6*(ib32%2)+3)) & 0x7) + 1));
  6200. }
  6201. static __dpct_inline__ void get_int_from_table_16(const uint32_t &q4,
  6202. const uint8_t *values,
  6203. int &val1, int &val2) {
  6204. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  6205. aux32 = q4 & 0x0f0f0f0f;
  6206. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  6207. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  6208. val1 = v1 | (v2 << 16);
  6209. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  6210. v1 = values[q8[0]] | (values[q8[1]] << 8);
  6211. v2 = values[q8[2]] | (values[q8[3]] << 8);
  6212. val2 = v1 | (v2 << 16);
  6213. }
  6214. static __dpct_inline__ float
  6215. vec_dot_iq4_nl_q8_1(const void *__restrict__ vbq,
  6216. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6217. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  6218. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  6219. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  6220. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  6221. int v1, v2;
  6222. int sumi1 = 0, sumi2 = 0;
  6223. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  6224. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  6225. get_int_from_table_16(aux, values, v1, v2);
  6226. sumi1 = dpct::dp4a(v1, q8[l + 0], sumi1);
  6227. sumi2 = dpct::dp4a(v2, q8[l + 4], sumi2);
  6228. }
  6229. const float d = (float)bq->d * bq8_1->ds[0];
  6230. return d * (sumi1 + sumi2);
  6231. }
  6232. static __dpct_inline__ float
  6233. vec_dot_iq4_xs_q8_1(const void *__restrict__ vbq,
  6234. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6235. const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq;
  6236. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  6237. // iqs is 0...7
  6238. const int ib32 = iqs;
  6239. const int32_t * q8 = (const int *)bq8_1[ib32].qs;
  6240. const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32;
  6241. const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  6242. const float d = (float)bq4->d * (ls - 32) * bq8_1[ib32].ds[0];
  6243. int v1, v2;
  6244. int sumi1 = 0, sumi2 = 0;
  6245. for (int j = 0; j < 4; ++j) {
  6246. get_int_from_table_16(q4[j], values, v1, v2);
  6247. sumi1 = dpct::dp4a(v1, q8[j + 0], sumi1);
  6248. sumi2 = dpct::dp4a(v2, q8[j + 4], sumi2);
  6249. }
  6250. return d * (sumi1 + sumi2);
  6251. }
  6252. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  6253. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  6254. vec_dot_q_mul_mat_sycl_t vec_dot>
  6255. /*
  6256. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  6257. exceeds 128 bytes and may cause high register pressure. Consult with your
  6258. hardware vendor to find the total register size available and adjust the code,
  6259. or use smaller sub-group size to avoid high register pressure.
  6260. */
  6261. static __dpct_inline__ void
  6262. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  6263. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  6264. const int ncols_y, const int nrows_y, const int nrows_dst,
  6265. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  6266. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  6267. sycl::half2 *tile_y_ds) {
  6268. const block_q_t * x = (const block_q_t *) vx;
  6269. const block_q8_1 * y = (const block_q8_1 *) vy;
  6270. const int blocks_per_row_x = ncols_x / qk;
  6271. const int blocks_per_col_y = nrows_y / QK8_1;
  6272. const int blocks_per_warp = WARP_SIZE / qi;
  6273. const int & ncols_dst = ncols_y;
  6274. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  6275. const int & row_x_0 = row_dst_0;
  6276. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  6277. const int & col_y_0 = col_dst_0;
  6278. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  6279. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  6280. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  6281. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  6282. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  6283. blocks_per_row_x);
  6284. #pragma unroll
  6285. for (int ir = 0; ir < qr; ++ir) {
  6286. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  6287. const int kbxd = kqs / QI8_1;
  6288. #pragma unroll
  6289. for (int i = 0; i < mmq_x; i += nwarps) {
  6290. const int col_y_eff = dpct::min(
  6291. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  6292. ncols_y - 1); // to prevent out-of-bounds memory accesses
  6293. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  6294. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  6295. kqs % WARP_SIZE;
  6296. tile_y_qs[index_y] = get_int_from_int8_aligned(
  6297. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  6298. }
  6299. #pragma unroll
  6300. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  6301. const int ids =
  6302. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  6303. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  6304. mmq_x;
  6305. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  6306. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  6307. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  6308. const sycl::half2 *dsi_src =
  6309. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  6310. ir * (WARP_SIZE / QI8_1) + kby]
  6311. .ds;
  6312. sycl::half2 *dsi_dst =
  6313. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  6314. if (need_sum) {
  6315. *dsi_dst = *dsi_src;
  6316. } else {
  6317. float * dfi_dst = (float *) dsi_dst;
  6318. *dfi_dst = (*dsi_src)[0];
  6319. }
  6320. }
  6321. /*
  6322. DPCT1118:9: SYCL group functions and algorithms must be encountered
  6323. in converged control flow. You may need to adjust the code.
  6324. */
  6325. /*
  6326. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  6327. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6328. better performance if there is no access to global memory.
  6329. */
  6330. item_ct1.barrier();
  6331. // #pragma unroll // unrolling this loop causes too much register pressure
  6332. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  6333. #pragma unroll
  6334. for (int j = 0; j < mmq_x; j += nwarps) {
  6335. #pragma unroll
  6336. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6337. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  6338. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  6339. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  6340. item_ct1.get_local_id(1) + j, k);
  6341. }
  6342. }
  6343. }
  6344. /*
  6345. DPCT1118:10: SYCL group functions and algorithms must be encountered
  6346. in converged control flow. You may need to adjust the code.
  6347. */
  6348. /*
  6349. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  6350. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6351. better performance if there is no access to global memory.
  6352. */
  6353. item_ct1.barrier();
  6354. }
  6355. }
  6356. #pragma unroll
  6357. for (int j = 0; j < mmq_x; j += nwarps) {
  6358. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  6359. if (col_dst >= ncols_dst) {
  6360. return;
  6361. }
  6362. #pragma unroll
  6363. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6364. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  6365. if (row_dst >= nrows_dst) {
  6366. continue;
  6367. }
  6368. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  6369. }
  6370. }
  6371. }
  6372. #define MMQ_X_Q4_0_RDNA2 64
  6373. #define MMQ_Y_Q4_0_RDNA2 128
  6374. #define NWARPS_Q4_0_RDNA2 8
  6375. #define MMQ_X_Q4_0_RDNA1 64
  6376. #define MMQ_Y_Q4_0_RDNA1 64
  6377. #define NWARPS_Q4_0_RDNA1 8
  6378. #if defined(SYCL_USE_XMX)
  6379. #define MMQ_X_Q4_0_AMPERE 4
  6380. #define MMQ_Y_Q4_0_AMPERE 32
  6381. #define NWARPS_Q4_0_AMPERE 4
  6382. #else
  6383. #define MMQ_X_Q4_0_AMPERE 64
  6384. #define MMQ_Y_Q4_0_AMPERE 128
  6385. #define NWARPS_Q4_0_AMPERE 4
  6386. #endif
  6387. #define MMQ_X_Q4_0_PASCAL 64
  6388. #define MMQ_Y_Q4_0_PASCAL 64
  6389. #define NWARPS_Q4_0_PASCAL 8
  6390. template <bool need_check> static void
  6391. mul_mat_q4_0(
  6392. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6393. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6394. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  6395. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6396. int * tile_x_ql = nullptr;
  6397. sycl::half2 *tile_x_dm = nullptr;
  6398. int * tile_x_qh = nullptr;
  6399. int * tile_x_sc = nullptr;
  6400. //sycl_todo: change according to hardware
  6401. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  6402. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  6403. const int nwarps = NWARPS_Q4_0_AMPERE;
  6404. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6405. tile_x_qs_q4_0, tile_x_d_q4_0);
  6406. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  6407. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  6408. vec_dot_q4_0_q8_1_mul_mat>(
  6409. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6410. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6411. }
  6412. #define MMQ_X_Q4_1_RDNA2 64
  6413. #define MMQ_Y_Q4_1_RDNA2 128
  6414. #define NWARPS_Q4_1_RDNA2 8
  6415. #define MMQ_X_Q4_1_RDNA1 64
  6416. #define MMQ_Y_Q4_1_RDNA1 64
  6417. #define NWARPS_Q4_1_RDNA1 8
  6418. #if defined(SYCL_USE_XMX)
  6419. #define MMQ_X_Q4_1_AMPERE 4
  6420. #define MMQ_Y_Q4_1_AMPERE 32
  6421. #define NWARPS_Q4_1_AMPERE 4
  6422. #else
  6423. #define MMQ_X_Q4_1_AMPERE 64
  6424. #define MMQ_Y_Q4_1_AMPERE 128
  6425. #define NWARPS_Q4_1_AMPERE 4
  6426. #endif
  6427. #define MMQ_X_Q4_1_PASCAL 64
  6428. #define MMQ_Y_Q4_1_PASCAL 64
  6429. #define NWARPS_Q4_1_PASCAL 8
  6430. template <bool need_check> static void
  6431. mul_mat_q4_1(
  6432. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6433. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6434. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  6435. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6436. int * tile_x_ql = nullptr;
  6437. sycl::half2 *tile_x_dm = nullptr;
  6438. int * tile_x_qh = nullptr;
  6439. int * tile_x_sc = nullptr;
  6440. //sycl_todo: change according to hardware
  6441. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  6442. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  6443. const int nwarps = NWARPS_Q4_1_AMPERE;
  6444. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6445. tile_x_qs_q4_1, tile_x_dm_q4_1);
  6446. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  6447. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  6448. vec_dot_q4_1_q8_1_mul_mat>(
  6449. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6450. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6451. }
  6452. #define MMQ_X_Q5_0_RDNA2 64
  6453. #define MMQ_Y_Q5_0_RDNA2 128
  6454. #define NWARPS_Q5_0_RDNA2 8
  6455. #define MMQ_X_Q5_0_RDNA1 64
  6456. #define MMQ_Y_Q5_0_RDNA1 64
  6457. #define NWARPS_Q5_0_RDNA1 8
  6458. #if defined(SYCL_USE_XMX)
  6459. #define MMQ_X_Q5_0_AMPERE 4
  6460. #define MMQ_Y_Q5_0_AMPERE 32
  6461. #define NWARPS_Q5_0_AMPERE 4
  6462. #else
  6463. #define MMQ_X_Q5_0_AMPERE 128
  6464. #define MMQ_Y_Q5_0_AMPERE 64
  6465. #define NWARPS_Q5_0_AMPERE 4
  6466. #endif
  6467. #define MMQ_X_Q5_0_PASCAL 64
  6468. #define MMQ_Y_Q5_0_PASCAL 64
  6469. #define NWARPS_Q5_0_PASCAL 8
  6470. template <bool need_check> static void
  6471. mul_mat_q5_0(
  6472. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6473. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6474. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  6475. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6476. int * tile_x_ql = nullptr;
  6477. sycl::half2 *tile_x_dm = nullptr;
  6478. int * tile_x_qh = nullptr;
  6479. int * tile_x_sc = nullptr;
  6480. //sycl_todo: change according to hardware
  6481. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  6482. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  6483. const int nwarps = NWARPS_Q5_0_AMPERE;
  6484. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6485. tile_x_ql_q5_0, tile_x_d_q5_0);
  6486. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  6487. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  6488. vec_dot_q5_0_q8_1_mul_mat>(
  6489. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6490. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6491. }
  6492. #define MMQ_X_Q5_1_RDNA2 64
  6493. #define MMQ_Y_Q5_1_RDNA2 128
  6494. #define NWARPS_Q5_1_RDNA2 8
  6495. #define MMQ_X_Q5_1_RDNA1 64
  6496. #define MMQ_Y_Q5_1_RDNA1 64
  6497. #define NWARPS_Q5_1_RDNA1 8
  6498. #if defined(SYCL_USE_XMX)
  6499. #define MMQ_X_Q5_1_AMPERE 4
  6500. #define MMQ_Y_Q5_1_AMPERE 32
  6501. #define NWARPS_Q5_1_AMPERE 4
  6502. #else
  6503. #define MMQ_X_Q5_1_AMPERE 128
  6504. #define MMQ_Y_Q5_1_AMPERE 64
  6505. #define NWARPS_Q5_1_AMPERE 4
  6506. #endif
  6507. #define MMQ_X_Q5_1_PASCAL 64
  6508. #define MMQ_Y_Q5_1_PASCAL 64
  6509. #define NWARPS_Q5_1_PASCAL 8
  6510. template <bool need_check> static void
  6511. mul_mat_q5_1(
  6512. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6513. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6514. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  6515. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6516. int * tile_x_ql = nullptr;
  6517. sycl::half2 *tile_x_dm = nullptr;
  6518. int * tile_x_qh = nullptr;
  6519. int * tile_x_sc = nullptr;
  6520. //sycl_todo: change according to hardware
  6521. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  6522. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  6523. const int nwarps = NWARPS_Q5_1_AMPERE;
  6524. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6525. tile_x_ql_q5_1, tile_x_dm_q5_1);
  6526. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  6527. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  6528. vec_dot_q5_1_q8_1_mul_mat>(
  6529. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6530. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6531. }
  6532. #define MMQ_X_Q8_0_RDNA2 64
  6533. #define MMQ_Y_Q8_0_RDNA2 128
  6534. #define NWARPS_Q8_0_RDNA2 8
  6535. #define MMQ_X_Q8_0_RDNA1 64
  6536. #define MMQ_Y_Q8_0_RDNA1 64
  6537. #define NWARPS_Q8_0_RDNA1 8
  6538. #if defined(SYCL_USE_XMX)
  6539. #define MMQ_X_Q8_0_AMPERE 4
  6540. #define MMQ_Y_Q8_0_AMPERE 32
  6541. #define NWARPS_Q8_0_AMPERE 4
  6542. #else
  6543. #define MMQ_X_Q8_0_AMPERE 128
  6544. #define MMQ_Y_Q8_0_AMPERE 64
  6545. #define NWARPS_Q8_0_AMPERE 4
  6546. #endif
  6547. #define MMQ_X_Q8_0_PASCAL 64
  6548. #define MMQ_Y_Q8_0_PASCAL 64
  6549. #define NWARPS_Q8_0_PASCAL 8
  6550. template <bool need_check> static void
  6551. mul_mat_q8_0(
  6552. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6553. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6554. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  6555. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6556. int * tile_x_ql = nullptr;
  6557. sycl::half2 *tile_x_dm = nullptr;
  6558. int * tile_x_qh = nullptr;
  6559. int * tile_x_sc = nullptr;
  6560. //sycl_todo: change according to hardware
  6561. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  6562. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  6563. const int nwarps = NWARPS_Q8_0_AMPERE;
  6564. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6565. tile_x_qs_q8_0, tile_x_d_q8_0);
  6566. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  6567. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  6568. vec_dot_q8_0_q8_1_mul_mat>(
  6569. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6570. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6571. }
  6572. #define MMQ_X_Q2_K_RDNA2 64
  6573. #define MMQ_Y_Q2_K_RDNA2 128
  6574. #define NWARPS_Q2_K_RDNA2 8
  6575. #define MMQ_X_Q2_K_RDNA1 128
  6576. #define MMQ_Y_Q2_K_RDNA1 32
  6577. #define NWARPS_Q2_K_RDNA1 8
  6578. #if defined(SYCL_USE_XMX)
  6579. #define MMQ_X_Q2_K_AMPERE 4
  6580. #define MMQ_Y_Q2_K_AMPERE 32
  6581. #define NWARPS_Q2_K_AMPERE 4
  6582. #else
  6583. #define MMQ_X_Q2_K_AMPERE 64
  6584. #define MMQ_Y_Q2_K_AMPERE 128
  6585. #define NWARPS_Q2_K_AMPERE 4
  6586. #endif
  6587. #define MMQ_X_Q2_K_PASCAL 64
  6588. #define MMQ_Y_Q2_K_PASCAL 64
  6589. #define NWARPS_Q2_K_PASCAL 8
  6590. template <bool need_check> static void
  6591. mul_mat_q2_K(
  6592. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6593. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6594. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  6595. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  6596. sycl::half2 *tile_y_ds) {
  6597. int * tile_x_ql = nullptr;
  6598. sycl::half2 *tile_x_dm = nullptr;
  6599. int * tile_x_qh = nullptr;
  6600. int * tile_x_sc = nullptr;
  6601. //sycl_todo: change according to hardware
  6602. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  6603. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  6604. const int nwarps = NWARPS_Q2_K_AMPERE;
  6605. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6606. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  6607. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  6608. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  6609. vec_dot_q2_K_q8_1_mul_mat>(
  6610. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6611. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6612. }
  6613. #define MMQ_X_Q3_K_RDNA2 128
  6614. #define MMQ_Y_Q3_K_RDNA2 64
  6615. #define NWARPS_Q3_K_RDNA2 8
  6616. #define MMQ_X_Q3_K_RDNA1 32
  6617. #define MMQ_Y_Q3_K_RDNA1 128
  6618. #define NWARPS_Q3_K_RDNA1 8
  6619. #if defined(SYCL_USE_XMX)
  6620. #define MMQ_X_Q3_K_AMPERE 4
  6621. #define MMQ_Y_Q3_K_AMPERE 32
  6622. #define NWARPS_Q3_K_AMPERE 4
  6623. #else
  6624. #define MMQ_X_Q3_K_AMPERE 128
  6625. #define MMQ_Y_Q3_K_AMPERE 128
  6626. #define NWARPS_Q3_K_AMPERE 4
  6627. #endif
  6628. #define MMQ_X_Q3_K_PASCAL 64
  6629. #define MMQ_Y_Q3_K_PASCAL 64
  6630. #define NWARPS_Q3_K_PASCAL 8
  6631. template <bool need_check> static void
  6632. mul_mat_q3_K(
  6633. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6634. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6635. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  6636. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  6637. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6638. int * tile_x_ql = nullptr;
  6639. sycl::half2 *tile_x_dm = nullptr;
  6640. int * tile_x_qh = nullptr;
  6641. int * tile_x_sc = nullptr;
  6642. //sycl_todo: change according to hardware
  6643. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  6644. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  6645. const int nwarps = NWARPS_Q3_K_AMPERE;
  6646. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6647. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  6648. tile_x_sc_q3_K);
  6649. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  6650. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  6651. vec_dot_q3_K_q8_1_mul_mat>(
  6652. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6653. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6654. }
  6655. #define MMQ_X_Q4_K_RDNA2 64
  6656. #define MMQ_Y_Q4_K_RDNA2 128
  6657. #define NWARPS_Q4_K_RDNA2 8
  6658. #define MMQ_X_Q4_K_RDNA1 32
  6659. #define MMQ_Y_Q4_K_RDNA1 64
  6660. #define NWARPS_Q4_K_RDNA1 8
  6661. #if defined(SYCL_USE_XMX)
  6662. #define MMQ_X_Q4_K_AMPERE 4
  6663. #define MMQ_Y_Q4_K_AMPERE 32
  6664. #define NWARPS_Q4_K_AMPERE 4
  6665. #else
  6666. #define MMQ_X_Q4_K_AMPERE 64
  6667. #define MMQ_Y_Q4_K_AMPERE 128
  6668. #define NWARPS_Q4_K_AMPERE 4
  6669. #endif
  6670. #define MMQ_X_Q4_K_PASCAL 64
  6671. #define MMQ_Y_Q4_K_PASCAL 64
  6672. #define NWARPS_Q4_K_PASCAL 8
  6673. template <bool need_check> static void
  6674. mul_mat_q4_K(
  6675. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6676. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6677. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  6678. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  6679. sycl::half2 *tile_y_ds) {
  6680. int * tile_x_ql = nullptr;
  6681. sycl::half2 *tile_x_dm = nullptr;
  6682. int * tile_x_qh = nullptr;
  6683. int * tile_x_sc = nullptr;
  6684. //sycl_todo: change according to hardware
  6685. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  6686. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  6687. const int nwarps = NWARPS_Q4_K_AMPERE;
  6688. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6689. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  6690. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  6691. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  6692. vec_dot_q4_K_q8_1_mul_mat>(
  6693. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6694. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6695. }
  6696. #define MMQ_X_Q5_K_RDNA2 64
  6697. #define MMQ_Y_Q5_K_RDNA2 128
  6698. #define NWARPS_Q5_K_RDNA2 8
  6699. #define MMQ_X_Q5_K_RDNA1 32
  6700. #define MMQ_Y_Q5_K_RDNA1 64
  6701. #define NWARPS_Q5_K_RDNA1 8
  6702. #if defined(SYCL_USE_XMX)
  6703. #define MMQ_X_Q5_K_AMPERE 4
  6704. #define MMQ_Y_Q5_K_AMPERE 32
  6705. #define NWARPS_Q5_K_AMPERE 4
  6706. #else
  6707. #define MMQ_X_Q5_K_AMPERE 64
  6708. #define MMQ_Y_Q5_K_AMPERE 128
  6709. #define NWARPS_Q5_K_AMPERE 4
  6710. #endif
  6711. #define MMQ_X_Q5_K_PASCAL 64
  6712. #define MMQ_Y_Q5_K_PASCAL 64
  6713. #define NWARPS_Q5_K_PASCAL 8
  6714. template <bool need_check> static void
  6715. mul_mat_q5_K(
  6716. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6717. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6718. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  6719. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  6720. sycl::half2 *tile_y_ds) {
  6721. int * tile_x_ql = nullptr;
  6722. sycl::half2 *tile_x_dm = nullptr;
  6723. int * tile_x_qh = nullptr;
  6724. int * tile_x_sc = nullptr;
  6725. //sycl_todo: change according to hardware
  6726. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  6727. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  6728. const int nwarps = NWARPS_Q5_K_AMPERE;
  6729. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6730. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  6731. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  6732. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  6733. vec_dot_q5_K_q8_1_mul_mat>(
  6734. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6735. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6736. }
  6737. #define MMQ_X_Q6_K_RDNA2 64
  6738. #define MMQ_Y_Q6_K_RDNA2 128
  6739. #define NWARPS_Q6_K_RDNA2 8
  6740. #define MMQ_X_Q6_K_RDNA1 32
  6741. #define MMQ_Y_Q6_K_RDNA1 64
  6742. #define NWARPS_Q6_K_RDNA1 8
  6743. #if defined(SYCL_USE_XMX)
  6744. #define MMQ_X_Q6_K_AMPERE 4
  6745. #define MMQ_Y_Q6_K_AMPERE 32
  6746. #define NWARPS_Q6_K_AMPERE 4
  6747. #else
  6748. #define MMQ_X_Q6_K_AMPERE 64
  6749. #define MMQ_Y_Q6_K_AMPERE 64
  6750. #define NWARPS_Q6_K_AMPERE 4
  6751. #endif
  6752. #define MMQ_X_Q6_K_PASCAL 64
  6753. #define MMQ_Y_Q6_K_PASCAL 64
  6754. #define NWARPS_Q6_K_PASCAL 8
  6755. template <bool need_check> static void
  6756. mul_mat_q6_K(
  6757. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6758. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6759. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  6760. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6761. // int * tile_x_ql = nullptr;
  6762. // sycl::half2 *tile_x_dm = nullptr;
  6763. int * tile_x_qh = nullptr;
  6764. // int * tile_x_sc = nullptr;
  6765. //sycl_todo: change according to hardware
  6766. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  6767. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  6768. const int nwarps = NWARPS_Q6_K_AMPERE;
  6769. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6770. tile_x_ql, tile_x_dm, tile_x_sc);
  6771. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  6772. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  6773. vec_dot_q6_K_q8_1_mul_mat>(
  6774. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6775. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6776. }
  6777. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  6778. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  6779. const sycl::nd_item<3> &item_ct1) {
  6780. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6781. item_ct1.get_local_id(1);
  6782. if (row >= nrows) {
  6783. return;
  6784. }
  6785. const int blocks_per_row = ncols / qk;
  6786. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6787. const int qi_vdr = (qi / vdr); // N_threads processing 1 qk block
  6788. // partial sum for each thread
  6789. float tmp = 0.0f;
  6790. const block_q_t * x = (const block_q_t *) vx;
  6791. const block_q8_1 * y = (const block_q8_1 *) vy;
  6792. for (int i = item_ct1.get_local_id(2) / qi_vdr; i < blocks_per_row;
  6793. i += blocks_per_warp) {
  6794. const int ibx = row * blocks_per_row + i; // x block index
  6795. const int iby = i * (qk / QK8_1); // y block index that aligns with ibx
  6796. const int iqs =
  6797. vdr *
  6798. (item_ct1.get_local_id(2) -
  6799. i * qi_vdr); // x block quant index when casting the quants to int
  6800. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  6801. }
  6802. // sum up partial sums and write back result
  6803. #pragma unroll
  6804. for (int mask = 16; mask > 0; mask >>= 1) {
  6805. tmp +=
  6806. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6807. }
  6808. if (item_ct1.get_local_id(2) == 0) {
  6809. dst[row] = tmp;
  6810. }
  6811. }
  6812. template <int qk, int qi, typename block_q_t, int vdr>
  6813. static void mul_mat_vec_q_iq2_xxs_q8_1(const void *__restrict__ vx,
  6814. const void *__restrict__ vy,
  6815. float *__restrict__ dst, const int ncols,
  6816. const int nrows,
  6817. const sycl::nd_item<3> &item_ct1) {
  6818. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6819. item_ct1.get_local_id(1);
  6820. if (row >= nrows) {
  6821. return;
  6822. }
  6823. const int blocks_per_row = ncols / qk;
  6824. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6825. // partial sum for each thread
  6826. float tmp = 0.0f;
  6827. const block_q_t * x = (const block_q_t *) vx;
  6828. const block_q8_1 * y = (const block_q8_1 *) vy;
  6829. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6830. i += blocks_per_warp) {
  6831. const int ibx = row*blocks_per_row + i; // x block index
  6832. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6833. const int iqs =
  6834. vdr *
  6835. (item_ct1.get_local_id(2) %
  6836. (qi / vdr)); // x block quant index when casting the quants to int
  6837. tmp += vec_dot_iq2_xxs_q8_1(&x[ibx], &y[iby], iqs, iq2xxs_grid, ksigns_iq2xs, kmask_iq2xs);
  6838. }
  6839. // sum up partial sums and write back result
  6840. #pragma unroll
  6841. for (int mask = 16; mask > 0; mask >>= 1) {
  6842. tmp +=
  6843. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6844. }
  6845. if (item_ct1.get_local_id(2) == 0) {
  6846. dst[row] = tmp;
  6847. }
  6848. }
  6849. template <int qk, int qi, typename block_q_t, int vdr>
  6850. static void mul_mat_vec_q_iq2_xs_q8_1(const void *__restrict__ vx,
  6851. const void *__restrict__ vy,
  6852. float *__restrict__ dst, const int ncols,
  6853. const int nrows,
  6854. const sycl::nd_item<3> &item_ct1) {
  6855. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6856. item_ct1.get_local_id(1);
  6857. if (row >= nrows) {
  6858. return;
  6859. }
  6860. const int blocks_per_row = ncols / qk;
  6861. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6862. // partial sum for each thread
  6863. float tmp = 0.0f;
  6864. const block_q_t * x = (const block_q_t *) vx;
  6865. const block_q8_1 * y = (const block_q8_1 *) vy;
  6866. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6867. i += blocks_per_warp) {
  6868. const int ibx = row*blocks_per_row + i; // x block index
  6869. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6870. const int iqs =
  6871. vdr *
  6872. (item_ct1.get_local_id(2) %
  6873. (qi / vdr)); // x block quant index when casting the quants to int
  6874. tmp += vec_dot_iq2_xs_q8_1(&x[ibx], &y[iby], iqs, iq2xs_grid, ksigns64);
  6875. }
  6876. // sum up partial sums and write back result
  6877. #pragma unroll
  6878. for (int mask = 16; mask > 0; mask >>= 1) {
  6879. tmp +=
  6880. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6881. }
  6882. if (item_ct1.get_local_id(2) == 0) {
  6883. dst[row] = tmp;
  6884. }
  6885. }
  6886. template <int qk, int qi, typename block_q_t, int vdr>
  6887. static void mul_mat_vec_q_iq2_s_q8_1(const void *__restrict__ vx,
  6888. const void *__restrict__ vy,
  6889. float *__restrict__ dst, const int ncols,
  6890. const int nrows,
  6891. const sycl::nd_item<3> &item_ct1) {
  6892. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6893. item_ct1.get_local_id(1);
  6894. if (row >= nrows) {
  6895. return;
  6896. }
  6897. const int blocks_per_row = ncols / qk;
  6898. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6899. // partial sum for each thread
  6900. float tmp = 0.0f;
  6901. const block_q_t * x = (const block_q_t *) vx;
  6902. const block_q8_1 * y = (const block_q8_1 *) vy;
  6903. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6904. i += blocks_per_warp) {
  6905. const int ibx = row*blocks_per_row + i; // x block index
  6906. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6907. const int iqs =
  6908. vdr *
  6909. (item_ct1.get_local_id(2) %
  6910. (qi / vdr)); // x block quant index when casting the quants to int
  6911. tmp += vec_dot_iq2_s_q8_1(&x[ibx], &y[iby], iqs);
  6912. }
  6913. // sum up partial sums and write back result
  6914. #pragma unroll
  6915. for (int mask = 16; mask > 0; mask >>= 1) {
  6916. tmp +=
  6917. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6918. }
  6919. if (item_ct1.get_local_id(2) == 0) {
  6920. dst[row] = tmp;
  6921. }
  6922. }
  6923. template <int qk, int qi, typename block_q_t, int vdr>
  6924. static void mul_mat_vec_q_iq3_xxs_q8_1(const void *__restrict__ vx,
  6925. const void *__restrict__ vy,
  6926. float *__restrict__ dst, const int ncols,
  6927. const int nrows,
  6928. const sycl::nd_item<3> &item_ct1) {
  6929. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6930. item_ct1.get_local_id(1);
  6931. if (row >= nrows) {
  6932. return;
  6933. }
  6934. const int blocks_per_row = ncols / qk;
  6935. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6936. // partial sum for each thread
  6937. float tmp = 0.0f;
  6938. const block_q_t * x = (const block_q_t *) vx;
  6939. const block_q8_1 * y = (const block_q8_1 *) vy;
  6940. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6941. i += blocks_per_warp) {
  6942. const int ibx = row*blocks_per_row + i; // x block index
  6943. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6944. const int iqs =
  6945. vdr *
  6946. (item_ct1.get_local_id(2) %
  6947. (qi / vdr)); // x block quant index when casting the quants to int
  6948. tmp += vec_dot_iq3_xxs_q8_1(&x[ibx], &y[iby], iqs, iq3xxs_grid, ksigns64);
  6949. }
  6950. // sum up partial sums and write back result
  6951. #pragma unroll
  6952. for (int mask = 16; mask > 0; mask >>= 1) {
  6953. tmp +=
  6954. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6955. }
  6956. if (item_ct1.get_local_id(2) == 0) {
  6957. dst[row] = tmp;
  6958. }
  6959. }
  6960. template <int qk, int qi, typename block_q_t, int vdr>
  6961. static void mul_mat_vec_q_iq3_s_q8_1(const void *__restrict__ vx,
  6962. const void *__restrict__ vy,
  6963. float *__restrict__ dst, const int ncols,
  6964. const int nrows,
  6965. const sycl::nd_item<3> &item_ct1) {
  6966. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6967. item_ct1.get_local_id(1);
  6968. if (row >= nrows) {
  6969. return;
  6970. }
  6971. const int blocks_per_row = ncols / qk;
  6972. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6973. // partial sum for each thread
  6974. float tmp = 0.0f;
  6975. const block_q_t * x = (const block_q_t *) vx;
  6976. const block_q8_1 * y = (const block_q8_1 *) vy;
  6977. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6978. i += blocks_per_warp) {
  6979. const int ibx = row*blocks_per_row + i; // x block index
  6980. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6981. const int iqs =
  6982. vdr *
  6983. (item_ct1.get_local_id(2) %
  6984. (qi / vdr)); // x block quant index when casting the quants to int
  6985. tmp += vec_dot_iq3_s_q8_1(&x[ibx], &y[iby], iqs, iq3s_grid);
  6986. }
  6987. // sum up partial sums and write back result
  6988. #pragma unroll
  6989. for (int mask = 16; mask > 0; mask >>= 1) {
  6990. tmp +=
  6991. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6992. }
  6993. if (item_ct1.get_local_id(2) == 0) {
  6994. dst[row] = tmp;
  6995. }
  6996. }
  6997. template <int qk, int qi, typename block_q_t, int vdr>
  6998. static void mul_mat_vec_q_iq1_s_q8_1(const void *__restrict__ vx,
  6999. const void *__restrict__ vy,
  7000. float *__restrict__ dst, const int ncols,
  7001. const int nrows,
  7002. const sycl::nd_item<3> &item_ct1) {
  7003. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7004. item_ct1.get_local_id(1);
  7005. if (row >= nrows) {
  7006. return;
  7007. }
  7008. const int blocks_per_row = ncols / qk;
  7009. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7010. // partial sum for each thread
  7011. float tmp = 0.0f;
  7012. const block_q_t * x = (const block_q_t *) vx;
  7013. const block_q8_1 * y = (const block_q8_1 *) vy;
  7014. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7015. i += blocks_per_warp) {
  7016. const int ibx = row*blocks_per_row + i; // x block index
  7017. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7018. const int iqs =
  7019. vdr *
  7020. (item_ct1.get_local_id(2) %
  7021. (qi / vdr)); // x block quant index when casting the quants to int
  7022. tmp += vec_dot_iq1_s_q8_1(&x[ibx], &y[iby], iqs, iq1s_grid_gpu);
  7023. }
  7024. // sum up partial sums and write back result
  7025. #pragma unroll
  7026. for (int mask = 16; mask > 0; mask >>= 1) {
  7027. tmp +=
  7028. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7029. }
  7030. if (item_ct1.get_local_id(2) == 0) {
  7031. dst[row] = tmp;
  7032. }
  7033. }
  7034. template <int qk, int qi, typename block_q_t, int vdr>
  7035. static void mul_mat_vec_q_iq1_m_q8_1(const void *__restrict__ vx,
  7036. const void *__restrict__ vy,
  7037. float *__restrict__ dst, const int ncols,
  7038. const int nrows,
  7039. const sycl::nd_item<3> &item_ct1) {
  7040. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7041. item_ct1.get_local_id(1);
  7042. if (row >= nrows) {
  7043. return;
  7044. }
  7045. const int blocks_per_row = ncols / qk;
  7046. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7047. // partial sum for each thread
  7048. float tmp = 0.0f;
  7049. const block_q_t * x = (const block_q_t *) vx;
  7050. const block_q8_1 * y = (const block_q8_1 *) vy;
  7051. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7052. i += blocks_per_warp) {
  7053. const int ibx = row*blocks_per_row + i; // x block index
  7054. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7055. const int iqs =
  7056. vdr *
  7057. (item_ct1.get_local_id(2) %
  7058. (qi / vdr)); // x block quant index when casting the quants to int
  7059. tmp += vec_dot_iq1_m_q8_1(&x[ibx], &y[iby], iqs);
  7060. }
  7061. // sum up partial sums and write back result
  7062. #pragma unroll
  7063. for (int mask = 16; mask > 0; mask >>= 1) {
  7064. tmp +=
  7065. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7066. }
  7067. if (item_ct1.get_local_id(2) == 0) {
  7068. dst[row] = tmp;
  7069. }
  7070. }
  7071. template <int qk, int qi, typename block_q_t, int vdr>
  7072. static void mul_mat_vec_q_iq4_nl_q8_1(const void *__restrict__ vx,
  7073. const void *__restrict__ vy,
  7074. float *__restrict__ dst, const int ncols,
  7075. const int nrows,
  7076. const sycl::nd_item<3> &item_ct1) {
  7077. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7078. item_ct1.get_local_id(1);
  7079. if (row >= nrows) {
  7080. return;
  7081. }
  7082. const int blocks_per_row = ncols / qk;
  7083. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7084. // partial sum for each thread
  7085. float tmp = 0.0f;
  7086. const block_q_t * x = (const block_q_t *) vx;
  7087. const block_q8_1 * y = (const block_q8_1 *) vy;
  7088. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7089. i += blocks_per_warp) {
  7090. const int ibx = row*blocks_per_row + i; // x block index
  7091. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7092. const int iqs =
  7093. vdr *
  7094. (item_ct1.get_local_id(2) %
  7095. (qi / vdr)); // x block quant index when casting the quants to int
  7096. tmp += vec_dot_iq4_nl_q8_1(&x[ibx], &y[iby], iqs);
  7097. }
  7098. // sum up partial sums and write back result
  7099. #pragma unroll
  7100. for (int mask = 16; mask > 0; mask >>= 1) {
  7101. tmp +=
  7102. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7103. }
  7104. if (item_ct1.get_local_id(2) == 0) {
  7105. dst[row] = tmp;
  7106. }
  7107. }
  7108. template <int qk, int qi, typename block_q_t, int vdr>
  7109. static void mul_mat_vec_q_iq4_xs_q8_1(const void *__restrict__ vx,
  7110. const void *__restrict__ vy,
  7111. float *__restrict__ dst, const int ncols,
  7112. const int nrows,
  7113. const sycl::nd_item<3> &item_ct1) {
  7114. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7115. item_ct1.get_local_id(1);
  7116. if (row >= nrows) {
  7117. return;
  7118. }
  7119. const int blocks_per_row = ncols / qk;
  7120. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7121. // partial sum for each thread
  7122. float tmp = 0.0f;
  7123. const block_q_t * x = (const block_q_t *) vx;
  7124. const block_q8_1 * y = (const block_q8_1 *) vy;
  7125. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7126. i += blocks_per_warp) {
  7127. const int ibx = row*blocks_per_row + i; // x block index
  7128. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7129. const int iqs =
  7130. vdr *
  7131. (item_ct1.get_local_id(2) %
  7132. (qi / vdr)); // x block quant index when casting the quants to int
  7133. tmp += vec_dot_iq4_xs_q8_1(&x[ibx], &y[iby], iqs);
  7134. }
  7135. // sum up partial sums and write back result
  7136. #pragma unroll
  7137. for (int mask = 16; mask > 0; mask >>= 1) {
  7138. tmp +=
  7139. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7140. }
  7141. if (item_ct1.get_local_id(2) == 0) {
  7142. dst[row] = tmp;
  7143. }
  7144. }
  7145. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  7146. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  7147. const sycl::nd_item<3> &item_ct1) {
  7148. // qk = quantized weights per x block
  7149. // qr = number of quantized weights per data value in x block
  7150. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7151. item_ct1.get_local_id(1);
  7152. if (row >= nrows) {
  7153. return;
  7154. }
  7155. const int tid = item_ct1.get_local_id(2);
  7156. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  7157. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  7158. const int y_offset = qr == 1 ? 1 : qk/2;
  7159. // partial sum for each thread
  7160. #ifdef GGML_SYCL_F16
  7161. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  7162. #else
  7163. float tmp = 0.0f;
  7164. #endif // GGML_SYCL_F16
  7165. for (int i = 0; i < ncols; i += iter_stride) {
  7166. const int col = i + vals_per_iter*tid;
  7167. const int ib = (row*ncols + col)/qk; // x block index
  7168. const int iqs = (col%qk)/qr; // x quant index
  7169. const int iybs = col - col%qk; // y block start index
  7170. // processing >2 values per i iter is faster for fast GPUs
  7171. #pragma unroll
  7172. for (int j = 0; j < vals_per_iter; j += 2) {
  7173. // process 2 vals per j iter
  7174. // dequantize
  7175. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  7176. dfloat2 v;
  7177. dequantize_kernel(vx, ib, iqs + j/qr, v);
  7178. // matrix multiplication
  7179. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  7180. #ifdef GGML_SYCL_F16
  7181. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  7182. y[iybs + iqs + j / qr + y_offset]};
  7183. tmp += v * t1;
  7184. #else
  7185. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  7186. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  7187. #endif // GGML_SYCL_F16
  7188. }
  7189. }
  7190. // sum up partial sums and write back result
  7191. #pragma unroll
  7192. for (int mask = 16; mask > 0; mask >>= 1) {
  7193. tmp +=
  7194. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7195. }
  7196. if (tid == 0) {
  7197. #ifdef GGML_SYCL_F16
  7198. dst[row] = tmp.x() + tmp.y();
  7199. #else
  7200. dst[row] = tmp;
  7201. #endif // GGML_SYCL_F16
  7202. }
  7203. }
  7204. static void mul_mat_p021_f16_f32(
  7205. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  7206. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  7207. const sycl::nd_item<3> &item_ct1) {
  7208. const sycl::half *x = (const sycl::half *)vx;
  7209. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7210. item_ct1.get_local_id(1);
  7211. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7212. item_ct1.get_local_id(0);
  7213. const int channel_x = channel / (nchannels_y / nchannels_x);
  7214. const int nrows_y = ncols_x;
  7215. const int nrows_dst = nrows_x;
  7216. const int row_dst = row_x;
  7217. float tmp = 0.0f;
  7218. for (int col_x0 = 0; col_x0 < ncols_x;
  7219. col_x0 += item_ct1.get_local_range(2)) {
  7220. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7221. if (col_x >= ncols_x) {
  7222. break;
  7223. }
  7224. // x is transposed and permuted
  7225. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  7226. const float xi =
  7227. sycl::vec<sycl::half, 1>(x[ix])
  7228. .convert<float, sycl::rounding_mode::automatic>()[0];
  7229. const int row_y = col_x;
  7230. // y is not transposed but permuted
  7231. const int iy = channel*nrows_y + row_y;
  7232. tmp += xi * y[iy];
  7233. }
  7234. // dst is not transposed and not permuted
  7235. const int idst = channel*nrows_dst + row_dst;
  7236. // sum up partial sums and write back result
  7237. #pragma unroll
  7238. for (int mask = 16; mask > 0; mask >>= 1) {
  7239. tmp +=
  7240. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7241. }
  7242. if (item_ct1.get_local_id(2) == 0) {
  7243. dst[idst] = tmp;
  7244. }
  7245. }
  7246. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  7247. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  7248. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  7249. const sycl::nd_item<3> &item_ct1) {
  7250. const sycl::half *x = (const sycl::half *)vx;
  7251. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7252. item_ct1.get_local_id(1);
  7253. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7254. item_ct1.get_local_id(0);
  7255. const int channel_x = channel / channel_x_divisor;
  7256. const int nrows_y = ncols_x;
  7257. const int nrows_dst = nrows_x;
  7258. const int row_dst = row_x;
  7259. const int idst = channel*nrows_dst + row_dst;
  7260. float tmp = 0.0f;
  7261. for (int col_x0 = 0; col_x0 < ncols_x;
  7262. col_x0 += item_ct1.get_local_range(2)) {
  7263. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7264. if (col_x >= ncols_x) {
  7265. break;
  7266. }
  7267. const int row_y = col_x;
  7268. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  7269. const int iy = channel*nrows_y + row_y;
  7270. const float xi =
  7271. sycl::vec<sycl::half, 1>(x[ix])
  7272. .convert<float, sycl::rounding_mode::automatic>()[0];
  7273. tmp += xi * y[iy];
  7274. }
  7275. // sum up partial sums and write back result
  7276. #pragma unroll
  7277. for (int mask = 16; mask > 0; mask >>= 1) {
  7278. tmp +=
  7279. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7280. }
  7281. if (item_ct1.get_local_id(2) == 0) {
  7282. dst[idst] = tmp;
  7283. }
  7284. }
  7285. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  7286. const float * xi = (const float *) cxi;
  7287. float * dsti = (float *) cdsti;
  7288. *dsti = *xi;
  7289. }
  7290. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  7291. const float * xi = (const float *) cxi;
  7292. sycl::half *dsti = (sycl::half *)cdsti;
  7293. *dsti = sycl::vec<float, 1>(*xi)
  7294. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7295. }
  7296. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  7297. const sycl::half *xi = (const sycl::half *)cxi;
  7298. sycl::half *dsti = (sycl::half *)cdsti;
  7299. *dsti = *xi;
  7300. }
  7301. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  7302. const sycl::half *xi = (const sycl::half *)cxi;
  7303. float * dsti = (float *) cdsti;
  7304. *dsti = *xi;
  7305. }
  7306. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  7307. const int16_t *xi = (const int16_t *)cxi;
  7308. int16_t *dsti = (int16_t *)cdsti;
  7309. *dsti = *xi;
  7310. }
  7311. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  7312. const int32_t *xi = (const int32_t *)cxi;
  7313. int32_t *dsti = (int32_t *)cdsti;
  7314. *dsti = *xi;
  7315. }
  7316. template <cpy_kernel_t cpy_1>
  7317. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  7318. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7319. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7320. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7321. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7322. item_ct1.get_local_id(2);
  7323. if (i >= ne) {
  7324. return;
  7325. }
  7326. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  7327. // then combine those indices with the corresponding byte offsets to get the total offsets
  7328. const int i03 = i/(ne00 * ne01 * ne02);
  7329. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7330. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7331. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7332. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7333. const int i13 = i/(ne10 * ne11 * ne12);
  7334. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7335. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7336. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7337. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  7338. cpy_1(cx + x_offset, cdst + dst_offset);
  7339. }
  7340. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  7341. const float * xi = (const float *) cxi;
  7342. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  7343. float amax = 0.0f; // absolute max
  7344. for (int j = 0; j < QK8_0; j++) {
  7345. const float v = xi[j];
  7346. amax = sycl::fmax(amax, sycl::fabs((float)v));
  7347. }
  7348. const float d = amax / ((1 << 7) - 1);
  7349. const float id = d ? 1.0f/d : 0.0f;
  7350. dsti->d = d;
  7351. for (int j = 0; j < QK8_0; ++j) {
  7352. const float x0 = xi[j]*id;
  7353. dsti->qs[j] = sycl::round((float)x0);
  7354. }
  7355. }
  7356. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  7357. const float * xi = (const float *) cxi;
  7358. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  7359. float amax = 0.0f;
  7360. float vmax = 0.0f;
  7361. for (int j = 0; j < QK4_0; ++j) {
  7362. const float v = xi[j];
  7363. if (amax < sycl::fabs((float)v)) {
  7364. amax = sycl::fabs((float)v);
  7365. vmax = v;
  7366. }
  7367. }
  7368. const float d = vmax / -8;
  7369. const float id = d ? 1.0f/d : 0.0f;
  7370. dsti->d = d;
  7371. for (int j = 0; j < QK4_0/2; ++j) {
  7372. const float x0 = xi[0 + j]*id;
  7373. const float x1 = xi[QK4_0/2 + j]*id;
  7374. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  7375. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  7376. dsti->qs[j] = xi0;
  7377. dsti->qs[j] |= xi1 << 4;
  7378. }
  7379. }
  7380. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  7381. const float * xi = (const float *) cxi;
  7382. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  7383. float vmin = FLT_MAX;
  7384. float vmax = -FLT_MAX;
  7385. for (int j = 0; j < QK4_1; ++j) {
  7386. const float v = xi[j];
  7387. if (v < vmin) vmin = v;
  7388. if (v > vmax) vmax = v;
  7389. }
  7390. const float d = (vmax - vmin) / ((1 << 4) - 1);
  7391. const float id = d ? 1.0f/d : 0.0f;
  7392. dsti->dm.x() = d;
  7393. dsti->dm.y() = vmin;
  7394. for (int j = 0; j < QK4_1/2; ++j) {
  7395. const float x0 = (xi[0 + j] - vmin)*id;
  7396. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  7397. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  7398. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  7399. dsti->qs[j] = xi0;
  7400. dsti->qs[j] |= xi1 << 4;
  7401. }
  7402. }
  7403. template <cpy_kernel_t cpy_blck, int qk>
  7404. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  7405. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7406. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7407. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7408. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7409. item_ct1.get_local_id(2)) *
  7410. qk;
  7411. if (i >= ne) {
  7412. return;
  7413. }
  7414. const int i03 = i/(ne00 * ne01 * ne02);
  7415. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7416. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7417. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7418. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7419. const int i13 = i/(ne10 * ne11 * ne12);
  7420. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7421. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7422. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7423. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  7424. cpy_blck(cx + x_offset, cdst + dst_offset);
  7425. }
  7426. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7427. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  7428. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  7429. }
  7430. struct rope_corr_dims {
  7431. float v[4];
  7432. };
  7433. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7434. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7435. static void rope_yarn(
  7436. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  7437. float * cos_theta, float * sin_theta
  7438. ) {
  7439. // Get n-d rotational scaling corrected for extrapolation
  7440. float theta_interp = freq_scale * theta_extrap;
  7441. float theta = theta_interp;
  7442. if (ext_factor != 0.0f) {
  7443. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  7444. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7445. // Get n-d magnitude scaling corrected for interpolation
  7446. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  7447. }
  7448. *cos_theta = sycl::cos(theta) * mscale;
  7449. *sin_theta = sycl::sin(theta) * mscale;
  7450. }
  7451. // rope == RoPE == rotary positional embedding
  7452. template<typename T, bool has_pos>
  7453. static void rope(
  7454. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7455. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  7456. ,
  7457. const sycl::nd_item<3> &item_ct1) {
  7458. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7459. item_ct1.get_local_id(1));
  7460. if (col >= ncols) {
  7461. return;
  7462. }
  7463. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7464. item_ct1.get_local_id(2);
  7465. const int i = row*ncols + col;
  7466. const int i2 = row/p_delta_rows;
  7467. const int p = has_pos ? pos[i2] : 0;
  7468. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  7469. float cos_theta, sin_theta;
  7470. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7471. const float x0 = x[i + 0];
  7472. const float x1 = x[i + 1];
  7473. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7474. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  7475. }
  7476. template<typename T, bool has_pos>
  7477. static void rope_neox(
  7478. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  7479. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  7480. ,
  7481. const sycl::nd_item<3> &item_ct1) {
  7482. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7483. item_ct1.get_local_id(1));
  7484. if (col >= ncols) {
  7485. return;
  7486. }
  7487. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7488. item_ct1.get_local_id(2);
  7489. const int ib = col / n_dims;
  7490. const int ic = col % n_dims;
  7491. if (ib > 0) {
  7492. const int i = row*ncols + ib*n_dims + ic;
  7493. dst[i + 0] = x[i + 0];
  7494. dst[i + 1] = x[i + 1];
  7495. return;
  7496. }
  7497. const int i = row*ncols + ib*n_dims + ic/2;
  7498. const int i2 = row/p_delta_rows;
  7499. float cur_rot = inv_ndims * ic - ib;
  7500. const int p = has_pos ? pos[i2] : 0;
  7501. const float theta_base =
  7502. p * freq_scale * dpct::pow(theta_scale, col / 2.0f);
  7503. float cos_theta, sin_theta;
  7504. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7505. const float x0 = x[i + 0];
  7506. const float x1 = x[i + n_dims/2];
  7507. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7508. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  7509. }
  7510. static void rope_glm_f32(
  7511. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7512. int n_ctx
  7513. , const sycl::nd_item<3> &item_ct1) {
  7514. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7515. item_ct1.get_local_id(2);
  7516. const int half_n_dims = ncols/4;
  7517. if (col >= half_n_dims) {
  7518. return;
  7519. }
  7520. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7521. item_ct1.get_local_id(1);
  7522. const int i = row*ncols + col;
  7523. const int i2 = row/p_delta_rows;
  7524. const float col_theta_scale = dpct::pow(freq_base, -2.0f * col / ncols);
  7525. // FIXME: this is likely wrong
  7526. const int p = pos != nullptr ? pos[i2] : 0;
  7527. const float theta = sycl::min(p, n_ctx - 2) * freq_scale * col_theta_scale;
  7528. const float sin_theta = sycl::sin((float)theta);
  7529. const float cos_theta = sycl::cos((float)theta);
  7530. const float x0 = x[i + 0];
  7531. const float x1 = x[i + half_n_dims];
  7532. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7533. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  7534. const float block_theta =
  7535. ((float)sycl::max(p - n_ctx - 2, 0)) * col_theta_scale;
  7536. const float sin_block_theta = sycl::sin((float)block_theta);
  7537. const float cos_block_theta = sycl::cos((float)block_theta);
  7538. const float x2 = x[i + half_n_dims * 2];
  7539. const float x3 = x[i + half_n_dims * 3];
  7540. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  7541. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  7542. }
  7543. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  7544. const sycl::nd_item<3> &item_ct1) {
  7545. const int row = item_ct1.get_group(1);
  7546. const int col = item_ct1.get_local_id(2);
  7547. float sum = 0.0f;
  7548. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  7549. sum += x[row * ncols + i];
  7550. }
  7551. sum = warp_reduce_sum(sum, item_ct1);
  7552. if (col == 0) {
  7553. dst[row] = sum;
  7554. }
  7555. }
  7556. template<typename T>
  7557. static inline void ggml_sycl_swap(T & a, T & b) {
  7558. T tmp = a;
  7559. a = b;
  7560. b = tmp;
  7561. }
  7562. template <ggml_sort_order order>
  7563. __dpct_inline__ static void
  7564. k_argsort_f32_i32(const float *x, int *dst, const int ncols, int ncols_pad,
  7565. const sycl::nd_item<3> &item_ct1, uint8_t *dpct_local) {
  7566. // bitonic sort
  7567. int col = item_ct1.get_local_id(2);
  7568. int row = item_ct1.get_group(1);
  7569. if (col >= ncols_pad) {
  7570. return;
  7571. }
  7572. const float * x_row = x + row * ncols;
  7573. auto dst_row = (int *)dpct_local;
  7574. // initialize indices
  7575. dst_row[col] = col;
  7576. item_ct1.barrier(sycl::access::fence_space::local_space);
  7577. for (int k = 2; k <= ncols_pad; k *= 2) {
  7578. for (int j = k / 2; j > 0; j /= 2) {
  7579. int ixj = col ^ j;
  7580. if (ixj > col) {
  7581. if ((col & k) == 0) {
  7582. if (dst_row[col] >= ncols ||
  7583. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  7584. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  7585. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  7586. ) {
  7587. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  7588. }
  7589. } else {
  7590. if (dst_row[ixj] >= ncols ||
  7591. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  7592. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  7593. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  7594. ) {
  7595. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  7596. }
  7597. }
  7598. }
  7599. /*
  7600. DPCT1118:1: SYCL group functions and algorithms must be encountered
  7601. in converged control flow. You may need to adjust the code.
  7602. */
  7603. item_ct1.barrier(sycl::access::fence_space::local_space);
  7604. }
  7605. }
  7606. // copy the result to dst without the padding
  7607. if (col < ncols) {
  7608. dst[row * ncols + col] = dst_row[col];
  7609. }
  7610. }
  7611. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  7612. const sycl::nd_item<3> &item_ct1) {
  7613. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7614. item_ct1.get_local_id(1);
  7615. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7616. item_ct1.get_local_id(2);
  7617. if (col >= ncols) {
  7618. return;
  7619. }
  7620. const int i = row*ncols + col;
  7621. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  7622. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  7623. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  7624. }
  7625. template <bool vals_smem, int ncols_template, int block_size_template>
  7626. static void soft_max_f32(const float * x, const float * mask, float * dst, const int ncols_par,
  7627. const int nrows_y, const float scale, const float max_bias, const float m0,
  7628. const float m1, uint32_t n_head_log2, const sycl::nd_item<3> &item_ct1, float *buf) {
  7629. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  7630. const int tid = item_ct1.get_local_id(2);
  7631. const int rowx = item_ct1.get_group(2);
  7632. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  7633. const int block_size = block_size_template == 0 ? item_ct1.get_local_range(2) : block_size_template;
  7634. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  7635. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  7636. float slope = 1.0f;
  7637. // ALiBi
  7638. if (max_bias > 0.0f) {
  7639. const uint32_t h = rowx/nrows_y; // head index
  7640. const float base = h < n_head_log2 ? m0 : m1;
  7641. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  7642. slope = sycl::pow(base, float(exp));
  7643. }
  7644. float * vals = vals_smem ? buf + WARP_SIZE : dst + rowx*ncols;
  7645. float max_val = -INFINITY;
  7646. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7647. const int col = col0 + tid;
  7648. if (ncols_template == 0 && col >= ncols) {
  7649. break;
  7650. }
  7651. const int ix = rowx*ncols + col;
  7652. const int iy = rowy*ncols + col;
  7653. const float val = x[ix]*scale + (mask ? slope*mask[iy] : 0.0f);
  7654. vals[col] = val;
  7655. max_val = sycl::max(max_val, val);
  7656. }
  7657. // find the max value in the block
  7658. max_val = warp_reduce_max(max_val, item_ct1);
  7659. if (block_size > WARP_SIZE) {
  7660. if (warp_id == 0) {
  7661. buf[lane_id] = -INFINITY;
  7662. }
  7663. item_ct1.barrier(sycl::access::fence_space::local_space);
  7664. if (lane_id == 0) {
  7665. buf[warp_id] = max_val;
  7666. }
  7667. item_ct1.barrier(sycl::access::fence_space::local_space);
  7668. max_val = buf[lane_id];
  7669. max_val = warp_reduce_max(max_val, item_ct1);
  7670. }
  7671. float tmp = 0.f;
  7672. #pragma unroll
  7673. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7674. const int col = col0 + tid;
  7675. if (ncols_template == 0 && col >= ncols) {
  7676. break;
  7677. }
  7678. const float val = sycl::native::exp(vals[col] - max_val);
  7679. tmp += val;
  7680. vals[col] = val;
  7681. }
  7682. // find the sum of exps in the block
  7683. tmp = warp_reduce_sum(tmp, item_ct1);
  7684. if (block_size > WARP_SIZE) {
  7685. if (warp_id == 0) {
  7686. buf[lane_id] = 0.f;
  7687. }
  7688. item_ct1.barrier(sycl::access::fence_space::local_space);
  7689. if (lane_id == 0) {
  7690. buf[warp_id] = tmp;
  7691. }
  7692. item_ct1.barrier(sycl::access::fence_space::local_space);
  7693. tmp = buf[lane_id];
  7694. tmp = warp_reduce_sum(tmp, item_ct1);
  7695. }
  7696. const float inv_sum = 1.f / tmp;
  7697. #pragma unroll
  7698. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7699. const int col = col0 + tid;
  7700. if (ncols_template == 0 && col >= ncols) {
  7701. return;
  7702. }
  7703. const int idst = rowx*ncols + col;
  7704. dst[idst] = vals[col] * inv_sum;
  7705. }
  7706. }
  7707. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  7708. const sycl::nd_item<3> &item_ct1) {
  7709. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7710. item_ct1.get_local_id(2);
  7711. if (i >= k) {
  7712. return;
  7713. }
  7714. dst[i] = scale * x[i];
  7715. }
  7716. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  7717. const sycl::nd_item<3> &item_ct1) {
  7718. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7719. item_ct1.get_local_id(2);
  7720. if (i >= k) {
  7721. return;
  7722. }
  7723. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  7724. }
  7725. template <typename T>
  7726. static void im2col_kernel(const float *x, T *dst, int offset_delta,
  7727. int IW, int IH, int OW, int KW, int KH,
  7728. int pelements, int CHW, int s0, int s1, int p0,
  7729. int p1, int d0, int d1,
  7730. const sycl::nd_item<3> &item_ct1) {
  7731. const int i = item_ct1.get_local_id(2) +
  7732. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  7733. if (i >= pelements) {
  7734. return;
  7735. }
  7736. const int ksize = OW * (KH > 1 ? KW : 1);
  7737. const int kx = i / ksize;
  7738. const int kd = kx * ksize;
  7739. const int ky = (i - kd) / OW;
  7740. const int ix = i % OW;
  7741. const int64_t iiw = ix * s0 + kx * d0 - p0;
  7742. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  7743. const int64_t offset_dst =
  7744. (item_ct1.get_group(1) * OW + ix) * CHW +
  7745. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  7746. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  7747. dst[offset_dst] =
  7748. sycl::vec<float, 1>(0.0f)
  7749. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7750. } else {
  7751. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  7752. dst[offset_dst] =
  7753. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  7754. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7755. }
  7756. }
  7757. template <typename Ti, typename To>
  7758. static void pool2d_nchw_kernel(
  7759. const int ih, const int iw, const int oh, const int ow,
  7760. const int kh, const int kw, const int sh, const int sw,
  7761. const int ph, const int pw, const int parallel_elements,
  7762. const Ti* src, To* dst, const enum ggml_op_pool op,
  7763. const sycl::nd_item<3> &item_ct1) {
  7764. int idx = item_ct1.get_local_id(2) +
  7765. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  7766. if (idx >= parallel_elements) {
  7767. return;
  7768. }
  7769. const int I_HW = ih * iw;
  7770. const int O_HW = oh * ow;
  7771. const int nc = idx / O_HW;
  7772. const int cur_oh = idx % O_HW / ow;
  7773. const int cur_ow = idx % O_HW % ow;
  7774. const Ti* i_ptr = src + nc * I_HW;
  7775. To* o_ptr = dst + nc * O_HW;
  7776. const int start_h = cur_oh * sh - ph;
  7777. const int bh = sycl::max(0, start_h);
  7778. const int eh = sycl::min(ih, start_h + kh);
  7779. const int start_w = cur_ow * sw - pw;
  7780. const int bw = sycl::max(0, start_w);
  7781. const int ew = sycl::min(iw, start_w + kw);
  7782. To res = 0;
  7783. switch (op) {
  7784. case GGML_OP_POOL_AVG: res = 0; break;
  7785. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  7786. }
  7787. for (int i = bh; i < eh; i += 1) {
  7788. for (int j = bw; j < ew; j += 1) {
  7789. #if DPCT_COMPATIBILITY_TEMP >= 350
  7790. /*
  7791. DPCT1098:106: The '*' expression is used instead of the __ldg
  7792. call. These two expressions do not provide the exact same
  7793. functionality. Check the generated code for potential precision
  7794. and/or performance issues.
  7795. */
  7796. Ti cur = *(i_ptr + i * iw + j);
  7797. #else
  7798. Ti cur = i_ptr[i * iw + j];
  7799. #endif
  7800. switch (op) {
  7801. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  7802. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  7803. }
  7804. }
  7805. }
  7806. o_ptr[cur_oh * ow + cur_ow] = res;
  7807. }
  7808. template <int qk, int qr, dequantize_kernel_t dq>
  7809. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  7810. ggml_tensor *dst, const void *src0_dd,
  7811. const int32_t *src1_dd, float *dst_dd,
  7812. dpct::queue_ptr stream) {
  7813. GGML_TENSOR_BINARY_OP_LOCALS
  7814. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7815. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  7816. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7817. // strides in elements
  7818. //const size_t s0 = nb0 / ggml_element_size(dst);
  7819. const size_t s1 = nb1 / ggml_element_size(dst);
  7820. const size_t s2 = nb2 / ggml_element_size(dst);
  7821. const size_t s3 = nb3 / ggml_element_size(dst);
  7822. const size_t s10 = nb10 / ggml_element_size(src1);
  7823. const size_t s11 = nb11 / ggml_element_size(src1);
  7824. const size_t s12 = nb12 / ggml_element_size(src1);
  7825. //const size_t s13 = nb13 / ggml_element_size(src1);
  7826. GGML_ASSERT(ne00 % 2 == 0);
  7827. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7828. [=](sycl::nd_item<3> item_ct1) {
  7829. k_get_rows<qk, qr, dq>(
  7830. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7831. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7832. });
  7833. (void) dst;
  7834. }
  7835. template <typename src0_t>
  7836. static void get_rows_sycl_float(const ggml_tensor *src0,
  7837. const ggml_tensor *src1, ggml_tensor *dst,
  7838. const src0_t *src0_dd, const int32_t *src1_dd,
  7839. float *dst_dd, dpct::queue_ptr stream) {
  7840. GGML_TENSOR_BINARY_OP_LOCALS
  7841. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7842. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  7843. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7844. // strides in elements
  7845. //const size_t s0 = nb0 / ggml_element_size(dst);
  7846. const size_t s1 = nb1 / ggml_element_size(dst);
  7847. const size_t s2 = nb2 / ggml_element_size(dst);
  7848. const size_t s3 = nb3 / ggml_element_size(dst);
  7849. const size_t s10 = nb10 / ggml_element_size(src1);
  7850. const size_t s11 = nb11 / ggml_element_size(src1);
  7851. const size_t s12 = nb12 / ggml_element_size(src1);
  7852. //const size_t s13 = nb13 / ggml_element_size(src1);
  7853. {
  7854. dpct::has_capability_or_fail(stream->get_device(),
  7855. {sycl::aspect::fp16});
  7856. stream->parallel_for(
  7857. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7858. [=](sycl::nd_item<3> item_ct1) {
  7859. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7860. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7861. });
  7862. }
  7863. (void) dst;
  7864. }
  7865. template<float (*bin_op)(const float, const float)>
  7866. struct bin_bcast_sycl {
  7867. template <typename src0_t, typename src1_t, typename dst_t>
  7868. void operator()(const struct ggml_tensor *src0,
  7869. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  7870. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  7871. dpct::queue_ptr stream) {
  7872. GGML_TENSOR_BINARY_OP_LOCALS
  7873. int nr0 = ne10/ne0;
  7874. int nr1 = ne11/ne1;
  7875. int nr2 = ne12/ne2;
  7876. int nr3 = ne13/ne3;
  7877. int nr[4] = { nr0, nr1, nr2, nr3 };
  7878. // collapse dimensions until first broadcast dimension
  7879. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  7880. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  7881. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  7882. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  7883. auto collapse = [](int64_t cne[]) {
  7884. cne[0] *= cne[1];
  7885. cne[1] = cne[2];
  7886. cne[2] = cne[3];
  7887. cne[3] = 1;
  7888. };
  7889. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  7890. cnb[1] *= cne[1];
  7891. cnb[2] *= cne[2];
  7892. cnb[3] *= cne[3];
  7893. };
  7894. for (int i = 0; i < 4; i++) {
  7895. if (nr[i] != 1) {
  7896. break;
  7897. }
  7898. if (i > 0) {
  7899. collapse_nb(cnb0, cne0);
  7900. collapse_nb(cnb1, cne1);
  7901. collapse(cne0);
  7902. collapse(cne1);
  7903. }
  7904. }
  7905. {
  7906. int64_t ne0 = cne0[0];
  7907. int64_t ne1 = cne0[1];
  7908. int64_t ne2 = cne0[2];
  7909. int64_t ne3 = cne0[3];
  7910. int64_t ne10 = cne1[0];
  7911. int64_t ne11 = cne1[1];
  7912. int64_t ne12 = cne1[2];
  7913. int64_t ne13 = cne1[3];
  7914. size_t nb0 = cnb0[0];
  7915. size_t nb1 = cnb0[1];
  7916. size_t nb2 = cnb0[2];
  7917. size_t nb3 = cnb0[3];
  7918. size_t nb10 = cnb1[0];
  7919. size_t nb11 = cnb1[1];
  7920. size_t nb12 = cnb1[2];
  7921. size_t nb13 = cnb1[3];
  7922. size_t s0 = nb0 / sizeof(dst_t);
  7923. size_t s1 = nb1 / sizeof(dst_t);
  7924. size_t s2 = nb2 / sizeof(dst_t);
  7925. size_t s3 = nb3 / sizeof(dst_t);
  7926. size_t s10 = nb10 / sizeof(src1_t);
  7927. size_t s11 = nb11 / sizeof(src1_t);
  7928. size_t s12 = nb12 / sizeof(src1_t);
  7929. size_t s13 = nb13 / sizeof(src1_t);
  7930. GGML_ASSERT(s0 == 1);
  7931. GGML_ASSERT(s10 == 1);
  7932. const int block_size = 128;
  7933. int64_t hne0 = std::max(ne0/2LL, 1LL);
  7934. sycl::range<3> block_dims(1, 1, 1);
  7935. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  7936. block_dims[1] = std::min<unsigned int>(
  7937. ne1, block_size / (unsigned int)block_dims[2]);
  7938. block_dims[0] = std::min(
  7939. std::min<unsigned int>(
  7940. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  7941. (unsigned int)block_dims[1]),
  7942. 64U);
  7943. sycl::range<3> block_nums(
  7944. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  7945. (ne1 + block_dims[1] - 1) / block_dims[1],
  7946. (hne0 + block_dims[2] - 1) / block_dims[2]);
  7947. if (block_nums[0] > 65535) {
  7948. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  7949. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  7950. {
  7951. dpct::has_capability_or_fail(stream->get_device(),
  7952. {sycl::aspect::fp16});
  7953. stream->parallel_for(
  7954. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  7955. sycl::range<3>(1, 1, block_size),
  7956. sycl::range<3>(1, 1, block_size)),
  7957. [=](sycl::nd_item<3> item_ct1) {
  7958. k_bin_bcast_unravel<bin_op>(
  7959. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  7960. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  7961. s13, item_ct1);
  7962. });
  7963. }
  7964. } else {
  7965. /*
  7966. DPCT1049:16: The work-group size passed to the SYCL kernel may
  7967. exceed the limit. To get the device limit, query
  7968. info::device::max_work_group_size. Adjust the work-group size if
  7969. needed.
  7970. */
  7971. dpct::has_capability_or_fail(stream->get_device(),
  7972. {sycl::aspect::fp16});
  7973. stream->parallel_for(
  7974. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7975. [=](sycl::nd_item<3> item_ct1) {
  7976. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  7977. ne2, ne3, ne10, ne11, ne12, ne13,
  7978. s1, s2, s3, s11, s12, s13,
  7979. item_ct1);
  7980. });
  7981. }
  7982. }
  7983. }
  7984. };
  7985. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  7986. const int n_elements, const int ne10, const int ne11,
  7987. const int ne12, const int nb1, const int nb2,
  7988. const int offset, dpct::queue_ptr stream) {
  7989. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  7990. stream->parallel_for(
  7991. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7992. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  7993. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  7994. [=](sycl::nd_item<3> item_ct1) {
  7995. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  7996. item_ct1);
  7997. });
  7998. }
  7999. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  8000. dpct::queue_ptr stream) {
  8001. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8002. stream->parallel_for(
  8003. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8004. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8005. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8006. [=](sycl::nd_item<3> item_ct1) {
  8007. gelu_f32(x, dst, k, item_ct1);
  8008. });
  8009. }
  8010. static void silu_f32_sycl(const float *x, float *dst, const int k,
  8011. dpct::queue_ptr stream) {
  8012. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  8013. stream->parallel_for(
  8014. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8015. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  8016. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  8017. [=](sycl::nd_item<3> item_ct1) {
  8018. silu_f32(x, dst, k, item_ct1);
  8019. });
  8020. }
  8021. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  8022. dpct::queue_ptr stream) {
  8023. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8024. stream->parallel_for(
  8025. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8026. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8027. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8028. [=](sycl::nd_item<3> item_ct1) {
  8029. gelu_quick_f32(x, dst, k, item_ct1);
  8030. });
  8031. }
  8032. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  8033. dpct::queue_ptr stream) {
  8034. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  8035. stream->parallel_for(
  8036. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8037. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  8038. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  8039. [=](sycl::nd_item<3> item_ct1) {
  8040. tanh_f32(x, dst, k, item_ct1);
  8041. });
  8042. }
  8043. static void relu_f32_sycl(const float *x, float *dst, const int k,
  8044. dpct::queue_ptr stream) {
  8045. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8046. stream->parallel_for(
  8047. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8048. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8049. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8050. [=](sycl::nd_item<3> item_ct1) {
  8051. relu_f32(x, dst, k, item_ct1);
  8052. });
  8053. }
  8054. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  8055. dpct::queue_ptr stream) {
  8056. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  8057. stream->parallel_for(
  8058. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8059. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  8060. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  8061. [=](sycl::nd_item<3> item_ct1) {
  8062. hardsigmoid_f32(x, dst, k, item_ct1);
  8063. });
  8064. }
  8065. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  8066. dpct::queue_ptr stream) {
  8067. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  8068. stream->parallel_for(
  8069. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8070. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  8071. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  8072. [=](sycl::nd_item<3> item_ct1) {
  8073. hardswish_f32(x, dst, k, item_ct1);
  8074. });
  8075. }
  8076. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  8077. const float negative_slope,
  8078. dpct::queue_ptr stream) {
  8079. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8080. stream->parallel_for(
  8081. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8082. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8083. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8084. [=](sycl::nd_item<3> item_ct1) {
  8085. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  8086. });
  8087. }
  8088. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  8089. dpct::queue_ptr stream) {
  8090. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  8091. stream->parallel_for(
  8092. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8093. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  8094. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  8095. [=](sycl::nd_item<3> item_ct1) {
  8096. sqr_f32(x, dst, k, item_ct1);
  8097. });
  8098. }
  8099. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  8100. const int nrows, const float eps,
  8101. dpct::queue_ptr stream) {
  8102. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8103. if (ncols < 1024) {
  8104. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8105. stream->submit([&](sycl::handler &cgh) {
  8106. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8107. sycl::range<1>(32), cgh);
  8108. cgh.parallel_for(
  8109. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8110. block_dims),
  8111. [=](sycl::nd_item<3> item_ct1)
  8112. [[intel::reqd_sub_group_size(32)]] {
  8113. norm_f32(x, dst, ncols, eps, item_ct1,
  8114. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8115. });
  8116. });
  8117. } else {
  8118. const int work_group_size = g_work_group_size;
  8119. const sycl::range<3> block_dims(1, 1, work_group_size);
  8120. /*
  8121. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  8122. the limit. To get the device limit, query
  8123. info::device::max_work_group_size. Adjust the work-group size if needed.
  8124. */
  8125. stream->submit([&](sycl::handler &cgh) {
  8126. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8127. sycl::range<1>(32), cgh);
  8128. cgh.parallel_for(
  8129. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8130. block_dims),
  8131. [=](sycl::nd_item<3> item_ct1)
  8132. [[intel::reqd_sub_group_size(32)]] {
  8133. norm_f32(x, dst, ncols, eps, item_ct1,
  8134. s_sum_acc_ct1.get_pointer(), work_group_size);
  8135. });
  8136. });
  8137. }
  8138. }
  8139. static void group_norm_f32_sycl(const float *x, float *dst,
  8140. const int num_groups, const int group_size,
  8141. const int ne_elements, dpct::queue_ptr stream) {
  8142. static const float eps = 1e-6f;
  8143. if (group_size < 1024) {
  8144. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8145. stream->submit([&](sycl::handler &cgh) {
  8146. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8147. cgh);
  8148. const float eps_ct4 = eps;
  8149. cgh.parallel_for(
  8150. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8151. block_dims),
  8152. [=](sycl::nd_item<3> item_ct1)
  8153. [[intel::reqd_sub_group_size(32)]] {
  8154. group_norm_f32(
  8155. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  8156. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8157. });
  8158. });
  8159. } else {
  8160. const int work_group_size = g_work_group_size;
  8161. const sycl::range<3> block_dims(1, 1, work_group_size);
  8162. /*
  8163. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  8164. the limit. To get the device limit, query
  8165. info::device::max_work_group_size. Adjust the work-group size if needed.
  8166. */
  8167. stream->submit([&](sycl::handler &cgh) {
  8168. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8169. cgh);
  8170. const float eps_ct4 = eps;
  8171. cgh.parallel_for(
  8172. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8173. block_dims),
  8174. [=](sycl::nd_item<3> item_ct1)
  8175. [[intel::reqd_sub_group_size(32)]] {
  8176. group_norm_f32(x, dst, group_size, ne_elements,
  8177. eps_ct4, item_ct1,
  8178. s_sum_acc_ct1.get_pointer(), work_group_size);
  8179. });
  8180. });
  8181. }
  8182. }
  8183. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  8184. const int ne0, int ne1, int ne2, int ne02,
  8185. dpct::queue_ptr stream) {
  8186. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  8187. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8188. stream->parallel_for(
  8189. sycl::nd_range<3>(gridDim *
  8190. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  8191. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  8192. [=](sycl::nd_item<3> item_ct1) {
  8193. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  8194. });
  8195. }
  8196. static void upscale_f32_sycl(const float *x, float *dst, const int nb00, const int nb01,
  8197. const int nb02, const int nb03, const int ne10, const int ne11,
  8198. const int ne12, const int ne13, const float sf0, const float sf1,
  8199. const float sf2, const float sf3, dpct::queue_ptr stream) {
  8200. int dst_size = ne10 * ne11 * ne12 * ne13;
  8201. int num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  8202. sycl::range<1> gridDim(num_blocks * SYCL_UPSCALE_BLOCK_SIZE);
  8203. stream->parallel_for(
  8204. sycl::nd_range<1>(gridDim, sycl::range<1>(SYCL_UPSCALE_BLOCK_SIZE)),
  8205. [=](sycl::nd_item<1> item_ct1) {
  8206. upscale_f32(x, dst, nb00, nb01, nb02, nb03, ne10, ne11, ne12, ne13, sf0, sf1, sf2, sf3, item_ct1);
  8207. });
  8208. }
  8209. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  8210. const int ne01, const int ne02, const int ne0,
  8211. const int ne1, const int ne2, dpct::queue_ptr stream) {
  8212. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  8213. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8214. stream->parallel_for(
  8215. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  8216. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  8217. [=](sycl::nd_item<3> item_ct1) {
  8218. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  8219. });
  8220. }
  8221. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  8222. const int nrows, const float eps,
  8223. dpct::queue_ptr stream) {
  8224. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8225. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  8226. if (ncols < 1024) {
  8227. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8228. stream->submit([&](sycl::handler &cgh) {
  8229. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8230. cgh);
  8231. cgh.parallel_for(
  8232. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8233. block_dims),
  8234. [=](sycl::nd_item<3> item_ct1)
  8235. [[intel::reqd_sub_group_size(32)]] {
  8236. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8237. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8238. });
  8239. });
  8240. } else {
  8241. const int work_group_size = g_work_group_size;
  8242. const sycl::range<3> block_dims(1, 1, work_group_size);
  8243. /*
  8244. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  8245. the limit. To get the device limit, query
  8246. info::device::max_work_group_size. Adjust the work-group size if needed.
  8247. */
  8248. stream->submit([&](sycl::handler &cgh) {
  8249. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8250. cgh);
  8251. cgh.parallel_for(
  8252. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8253. block_dims),
  8254. [=](sycl::nd_item<3> item_ct1)
  8255. [[intel::reqd_sub_group_size(32)]] {
  8256. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8257. s_sum_acc_ct1.get_pointer(), work_group_size);
  8258. });
  8259. });
  8260. }
  8261. }
  8262. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  8263. const int ky, const int kx_padded,
  8264. dpct::queue_ptr stream) {
  8265. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  8266. const sycl::range<3> num_blocks(1, ky, block_num_x);
  8267. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  8268. {
  8269. dpct::has_capability_or_fail(stream->get_device(),
  8270. {sycl::aspect::fp16});
  8271. stream->parallel_for(
  8272. sycl::nd_range<3>(num_blocks * block_size, block_size),
  8273. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8274. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  8275. });
  8276. }
  8277. }
  8278. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  8279. static void dequantize_block_sycl(const void *__restrict__ vx,
  8280. dst_t *__restrict__ y, const int k,
  8281. dpct::queue_ptr stream) {
  8282. const int num_blocks = (k + 2*SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2*SYCL_DEQUANTIZE_BLOCK_SIZE);
  8283. {
  8284. dpct::has_capability_or_fail(stream->get_device(),
  8285. {sycl::aspect::fp16});
  8286. stream->parallel_for(
  8287. sycl::nd_range<3>(
  8288. sycl::range<3>(1, 1, num_blocks) *
  8289. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8290. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8291. [=](sycl::nd_item<3> item_ct1) {
  8292. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  8293. });
  8294. }
  8295. }
  8296. template <typename dst_t>
  8297. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  8298. dpct::queue_ptr stream) {
  8299. const int nb = k / QK_K;
  8300. {
  8301. dpct::has_capability_or_fail(stream->get_device(),
  8302. {sycl::aspect::fp16});
  8303. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8304. sycl::range<3>(1, 1, 64),
  8305. sycl::range<3>(1, 1, 64)),
  8306. [=](sycl::nd_item<3> item_ct1) {
  8307. dequantize_block_q2_K(vx, y, item_ct1);
  8308. });
  8309. }
  8310. }
  8311. template <typename dst_t>
  8312. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  8313. dpct::queue_ptr stream) {
  8314. const int nb = k / QK_K;
  8315. {
  8316. dpct::has_capability_or_fail(stream->get_device(),
  8317. {sycl::aspect::fp16});
  8318. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8319. sycl::range<3>(1, 1, 64),
  8320. sycl::range<3>(1, 1, 64)),
  8321. [=](sycl::nd_item<3> item_ct1) {
  8322. dequantize_block_q3_K(vx, y, item_ct1);
  8323. });
  8324. }
  8325. }
  8326. template <typename dst_t>
  8327. static void dequantize_row_q4_0_sycl(const void *vx, dst_t *y, const int k,
  8328. dpct::queue_ptr stream) {
  8329. const int nb32 = k / 32;
  8330. const int nb = (k + 255) / 256;
  8331. {
  8332. dpct::has_capability_or_fail(stream->get_device(),
  8333. {sycl::aspect::fp16});
  8334. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8335. sycl::range<3>(1, 1, 32),
  8336. sycl::range<3>(1, 1, 32)),
  8337. [=](sycl::nd_item<3> item_ct1) {
  8338. dequantize_block_q4_0(vx, y, nb32, item_ct1);
  8339. });
  8340. }
  8341. }
  8342. template <typename dst_t>
  8343. static void dequantize_row_q4_1_sycl(const void *vx, dst_t *y, const int k,
  8344. dpct::queue_ptr stream) {
  8345. const int nb32 = k / 32;
  8346. const int nb = (k + 255) / 256;
  8347. {
  8348. dpct::has_capability_or_fail(stream->get_device(),
  8349. {sycl::aspect::fp16});
  8350. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8351. sycl::range<3>(1, 1, 32),
  8352. sycl::range<3>(1, 1, 32)),
  8353. [=](sycl::nd_item<3> item_ct1) {
  8354. dequantize_block_q4_1(vx, y, nb32, item_ct1);
  8355. });
  8356. }
  8357. }
  8358. template <typename dst_t>
  8359. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  8360. dpct::queue_ptr stream) {
  8361. const int nb = k / QK_K;
  8362. {
  8363. dpct::has_capability_or_fail(stream->get_device(),
  8364. {sycl::aspect::fp16});
  8365. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8366. sycl::range<3>(1, 1, 32),
  8367. sycl::range<3>(1, 1, 32)),
  8368. [=](sycl::nd_item<3> item_ct1) {
  8369. dequantize_block_q4_K(vx, y, item_ct1);
  8370. });
  8371. }
  8372. }
  8373. template <typename dst_t>
  8374. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  8375. dpct::queue_ptr stream) {
  8376. const int nb = k / QK_K;
  8377. {
  8378. dpct::has_capability_or_fail(stream->get_device(),
  8379. {sycl::aspect::fp16});
  8380. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8381. sycl::range<3>(1, 1, 64),
  8382. sycl::range<3>(1, 1, 64)),
  8383. [=](sycl::nd_item<3> item_ct1) {
  8384. dequantize_block_q5_K(vx, y, item_ct1);
  8385. });
  8386. }
  8387. }
  8388. template <typename dst_t>
  8389. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  8390. dpct::queue_ptr stream) {
  8391. const int nb = k / QK_K;
  8392. {
  8393. dpct::has_capability_or_fail(stream->get_device(),
  8394. {sycl::aspect::fp16});
  8395. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8396. sycl::range<3>(1, 1, 64),
  8397. sycl::range<3>(1, 1, 64)),
  8398. [=](sycl::nd_item<3> item_ct1) {
  8399. dequantize_block_q6_K(vx, y, item_ct1);
  8400. });
  8401. }
  8402. }
  8403. template <typename dst_t>
  8404. static void dequantize_row_iq1_s_sycl(const void *vx, dst_t *y, const int k,
  8405. dpct::queue_ptr stream) {
  8406. const int nb = k / QK_K;
  8407. {
  8408. dpct::has_capability_or_fail(stream->get_device(),
  8409. {sycl::aspect::fp16});
  8410. stream->submit([&](sycl::handler &cgh) {
  8411. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8412. sycl::range<3>(1, 1, 32),
  8413. sycl::range<3>(1, 1, 32)),
  8414. [=](sycl::nd_item<3> item_ct1) {
  8415. dequantize_block_iq1_s(
  8416. vx, y, item_ct1, iq1s_grid_gpu
  8417. );
  8418. });
  8419. });
  8420. }
  8421. }
  8422. template <typename dst_t>
  8423. static void dequantize_row_iq1_m_sycl(const void *vx, dst_t *y, const int k,
  8424. dpct::queue_ptr stream) {
  8425. const int nb = k / QK_K;
  8426. {
  8427. dpct::has_capability_or_fail(stream->get_device(),
  8428. {sycl::aspect::fp16});
  8429. stream->submit([&](sycl::handler &cgh) {
  8430. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8431. sycl::range<3>(1, 1, 32),
  8432. sycl::range<3>(1, 1, 32)),
  8433. [=](sycl::nd_item<3> item_ct1) {
  8434. dequantize_block_iq1_m(
  8435. vx, y, item_ct1, iq1s_grid_gpu
  8436. );
  8437. });
  8438. });
  8439. }
  8440. }
  8441. template <typename dst_t>
  8442. static void dequantize_row_iq2_xxs_sycl(const void *vx, dst_t *y, const int k,
  8443. dpct::queue_ptr stream) {
  8444. const int nb = k / QK_K;
  8445. {
  8446. dpct::has_capability_or_fail(stream->get_device(),
  8447. {sycl::aspect::fp16});
  8448. stream->submit([&](sycl::handler &cgh) {
  8449. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8450. sycl::range<3>(1, 1, 32),
  8451. sycl::range<3>(1, 1, 32)),
  8452. [=](sycl::nd_item<3> item_ct1) {
  8453. dequantize_block_iq2_xxs(
  8454. vx, y, item_ct1, iq2xxs_grid,
  8455. ksigns_iq2xs, kmask_iq2xs);
  8456. });
  8457. });
  8458. }
  8459. }
  8460. template <typename dst_t>
  8461. static void dequantize_row_iq2_xs_sycl(const void *vx, dst_t *y, const int k,
  8462. dpct::queue_ptr stream) {
  8463. const int nb = k / QK_K;
  8464. {
  8465. dpct::has_capability_or_fail(stream->get_device(),
  8466. {sycl::aspect::fp16});
  8467. stream->submit([&](sycl::handler &cgh) {
  8468. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8469. sycl::range<3>(1, 1, 32),
  8470. sycl::range<3>(1, 1, 32)),
  8471. [=](sycl::nd_item<3> item_ct1) {
  8472. dequantize_block_iq2_xs(
  8473. vx, y, item_ct1, iq2xs_grid,
  8474. ksigns_iq2xs, kmask_iq2xs);
  8475. });
  8476. });
  8477. }
  8478. }
  8479. template <typename dst_t>
  8480. static void dequantize_row_iq2_s_sycl(const void *vx, dst_t *y, const int k,
  8481. dpct::queue_ptr stream) {
  8482. const int nb = k / QK_K;
  8483. {
  8484. dpct::has_capability_or_fail(stream->get_device(),
  8485. {sycl::aspect::fp16});
  8486. stream->submit([&](sycl::handler &cgh) {
  8487. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8488. sycl::range<3>(1, 1, 32),
  8489. sycl::range<3>(1, 1, 32)),
  8490. [=](sycl::nd_item<3> item_ct1) {
  8491. dequantize_block_iq2_s(vx, y, item_ct1);
  8492. });
  8493. });
  8494. }
  8495. }
  8496. template <typename dst_t>
  8497. static void dequantize_row_iq3_xxs_sycl(const void *vx, dst_t *y, const int k,
  8498. dpct::queue_ptr stream) {
  8499. const int nb = k / QK_K;
  8500. {
  8501. dpct::has_capability_or_fail(stream->get_device(),
  8502. {sycl::aspect::fp16});
  8503. stream->submit([&](sycl::handler &cgh) {
  8504. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8505. sycl::range<3>(1, 1, 32),
  8506. sycl::range<3>(1, 1, 32)),
  8507. [=](sycl::nd_item<3> item_ct1) {
  8508. dequantize_block_iq3_xxs(
  8509. vx, y, item_ct1, iq3xxs_grid,
  8510. ksigns_iq2xs, kmask_iq2xs);
  8511. });
  8512. });
  8513. }
  8514. }
  8515. template <typename dst_t>
  8516. static void dequantize_row_iq3_s_sycl(const void *vx, dst_t *y, const int k,
  8517. dpct::queue_ptr stream) {
  8518. const int nb = k / QK_K;
  8519. {
  8520. dpct::has_capability_or_fail(stream->get_device(),
  8521. {sycl::aspect::fp16});
  8522. stream->submit([&](sycl::handler &cgh) {
  8523. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8524. sycl::range<3>(1, 1, 32),
  8525. sycl::range<3>(1, 1, 32)),
  8526. [=](sycl::nd_item<3> item_ct1) {
  8527. dequantize_block_iq3_s(
  8528. vx, y, item_ct1, kmask_iq2xs, iq3s_grid);
  8529. });
  8530. });
  8531. }
  8532. }
  8533. template <typename dst_t>
  8534. static void dequantize_row_iq4_xs_sycl(const void *vx, dst_t *y, const int k,
  8535. dpct::queue_ptr stream) {
  8536. const int nb = (k + QK_K - 1) / QK_K;
  8537. {
  8538. dpct::has_capability_or_fail(stream->get_device(),
  8539. {sycl::aspect::fp16});
  8540. stream->submit([&](sycl::handler &cgh) {
  8541. cgh.parallel_for(
  8542. sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8543. sycl::range<3>(1, 1, 32),
  8544. sycl::range<3>(1, 1, 32)),
  8545. [=](sycl::nd_item<3> item_ct1) {
  8546. dequantize_block_iq4_xs(vx, y, item_ct1);
  8547. });
  8548. });
  8549. }
  8550. }
  8551. template <typename dst_t>
  8552. static void dequantize_row_iq4_nl_sycl(const void *vx, dst_t *y, const int k,
  8553. dpct::queue_ptr stream) {
  8554. const int nb = (k + QK_K - 1) / QK_K;
  8555. {
  8556. dpct::has_capability_or_fail(stream->get_device(),
  8557. {sycl::aspect::fp16});
  8558. stream->submit([&](sycl::handler &cgh) {
  8559. cgh.parallel_for(
  8560. sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8561. sycl::range<3>(1, 1, 32),
  8562. sycl::range<3>(1, 1, 32)),
  8563. [=](sycl::nd_item<3> item_ct1) {
  8564. dequantize_block_iq4_nl(vx, y, item_ct1);
  8565. });
  8566. });
  8567. }
  8568. }
  8569. template <typename src_t, typename dst_t>
  8570. static void convert_unary_sycl(const void *__restrict__ vx,
  8571. dst_t *__restrict__ y, const int k,
  8572. dpct::queue_ptr stream) {
  8573. const int num_blocks = (k + SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / SYCL_DEQUANTIZE_BLOCK_SIZE;
  8574. {
  8575. dpct::has_capability_or_fail(stream->get_device(),
  8576. {sycl::aspect::fp16});
  8577. stream->parallel_for(
  8578. sycl::nd_range<3>(
  8579. sycl::range<3>(1, 1, num_blocks) *
  8580. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8581. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8582. [=](sycl::nd_item<3> item_ct1) {
  8583. convert_unary<src_t>(vx, y, k, item_ct1);
  8584. });
  8585. }
  8586. }
  8587. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) try {
  8588. int id;
  8589. switch (type) {
  8590. case GGML_TYPE_Q4_0:
  8591. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  8592. case GGML_TYPE_Q4_1:
  8593. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  8594. case GGML_TYPE_Q5_0:
  8595. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  8596. case GGML_TYPE_Q5_1:
  8597. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  8598. case GGML_TYPE_Q8_0:
  8599. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  8600. case GGML_TYPE_Q2_K:
  8601. return dequantize_row_q2_K_sycl;
  8602. case GGML_TYPE_Q3_K:
  8603. return dequantize_row_q3_K_sycl;
  8604. case GGML_TYPE_Q4_K:
  8605. return dequantize_row_q4_K_sycl;
  8606. case GGML_TYPE_Q5_K:
  8607. return dequantize_row_q5_K_sycl;
  8608. case GGML_TYPE_Q6_K:
  8609. return dequantize_row_q6_K_sycl;
  8610. case GGML_TYPE_IQ1_S:
  8611. return dequantize_row_iq1_s_sycl;
  8612. case GGML_TYPE_IQ1_M:
  8613. return dequantize_row_iq1_m_sycl;
  8614. case GGML_TYPE_IQ2_XXS:
  8615. return dequantize_row_iq2_xxs_sycl;
  8616. case GGML_TYPE_IQ2_XS:
  8617. return dequantize_row_iq2_xs_sycl;
  8618. case GGML_TYPE_IQ2_S:
  8619. return dequantize_row_iq2_s_sycl;
  8620. case GGML_TYPE_IQ3_XXS:
  8621. return dequantize_row_iq3_xxs_sycl;
  8622. case GGML_TYPE_IQ3_S:
  8623. return dequantize_row_iq3_s_sycl;
  8624. case GGML_TYPE_IQ4_XS:
  8625. return dequantize_row_iq4_xs_sycl;
  8626. case GGML_TYPE_IQ4_NL:
  8627. return dequantize_row_iq4_nl_sycl;
  8628. case GGML_TYPE_F32:
  8629. return convert_unary_sycl<float>;
  8630. default:
  8631. return nullptr;
  8632. }
  8633. }
  8634. catch (sycl::exception const &exc) {
  8635. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8636. << ", line:" << __LINE__ << std::endl;
  8637. std::exit(1);
  8638. }
  8639. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  8640. switch (type) {
  8641. case GGML_TYPE_Q4_0:
  8642. return dequantize_row_q4_0_sycl;
  8643. case GGML_TYPE_Q4_1:
  8644. return dequantize_row_q4_1_sycl;
  8645. case GGML_TYPE_Q5_0:
  8646. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  8647. case GGML_TYPE_Q5_1:
  8648. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  8649. case GGML_TYPE_Q8_0:
  8650. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  8651. case GGML_TYPE_Q2_K:
  8652. return dequantize_row_q2_K_sycl;
  8653. case GGML_TYPE_Q3_K:
  8654. return dequantize_row_q3_K_sycl;
  8655. case GGML_TYPE_Q4_K:
  8656. return dequantize_row_q4_K_sycl;
  8657. case GGML_TYPE_Q5_K:
  8658. return dequantize_row_q5_K_sycl;
  8659. case GGML_TYPE_Q6_K:
  8660. return dequantize_row_q6_K_sycl;
  8661. case GGML_TYPE_IQ1_S:
  8662. return dequantize_row_iq1_s_sycl;
  8663. case GGML_TYPE_IQ1_M:
  8664. return dequantize_row_iq1_m_sycl;
  8665. case GGML_TYPE_IQ2_XXS:
  8666. return dequantize_row_iq2_xxs_sycl;
  8667. case GGML_TYPE_IQ2_XS:
  8668. return dequantize_row_iq2_xs_sycl;
  8669. case GGML_TYPE_IQ2_S:
  8670. return dequantize_row_iq2_s_sycl;
  8671. case GGML_TYPE_IQ3_XXS:
  8672. return dequantize_row_iq3_xxs_sycl;
  8673. case GGML_TYPE_IQ3_S:
  8674. return dequantize_row_iq3_s_sycl;
  8675. case GGML_TYPE_IQ4_XS:
  8676. return dequantize_row_iq4_xs_sycl;
  8677. case GGML_TYPE_IQ4_NL:
  8678. return dequantize_row_iq4_nl_sycl;
  8679. case GGML_TYPE_F16:
  8680. return convert_unary_sycl<sycl::half>;
  8681. default:
  8682. return nullptr;
  8683. }
  8684. }
  8685. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  8686. float *dst, const int ncols,
  8687. const int nrows,
  8688. dpct::queue_ptr stream) {
  8689. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8690. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8691. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  8692. const sycl::range<3> block_nums(1, 1, block_num_y);
  8693. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8694. {
  8695. dpct::has_capability_or_fail(stream->get_device(),
  8696. {sycl::aspect::fp16});
  8697. stream->parallel_for(
  8698. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8699. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8700. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  8701. vx, y, dst, ncols, nrows, item_ct1);
  8702. });
  8703. }
  8704. }
  8705. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  8706. float *dst, const int ncols,
  8707. const int nrows,
  8708. dpct::queue_ptr stream) {
  8709. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8710. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8711. const sycl::range<3> block_nums(1, 1, block_num_y);
  8712. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8713. {
  8714. dpct::has_capability_or_fail(stream->get_device(),
  8715. {sycl::aspect::fp16});
  8716. stream->parallel_for(
  8717. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8718. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8719. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  8720. vx, y, dst, ncols, nrows, item_ct1);
  8721. });
  8722. }
  8723. }
  8724. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  8725. float *dst, const int ncols,
  8726. const int nrows,
  8727. dpct::queue_ptr stream) {
  8728. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8729. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8730. const sycl::range<3> block_nums(1, 1, block_num_y);
  8731. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8732. {
  8733. dpct::has_capability_or_fail(stream->get_device(),
  8734. {sycl::aspect::fp16});
  8735. stream->parallel_for(
  8736. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8737. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8738. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  8739. vx, y, dst, ncols, nrows, item_ct1);
  8740. });
  8741. }
  8742. }
  8743. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  8744. float *dst, const int ncols,
  8745. const int nrows,
  8746. dpct::queue_ptr stream) {
  8747. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8748. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8749. const sycl::range<3> block_nums(1, 1, block_num_y);
  8750. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8751. {
  8752. dpct::has_capability_or_fail(stream->get_device(),
  8753. {sycl::aspect::fp16});
  8754. stream->parallel_for(
  8755. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8756. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8757. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  8758. vx, y, dst, ncols, nrows, item_ct1);
  8759. });
  8760. }
  8761. }
  8762. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  8763. float *dst, const int ncols,
  8764. const int nrows,
  8765. dpct::queue_ptr stream) {
  8766. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8767. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8768. const sycl::range<3> block_nums(1, 1, block_num_y);
  8769. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8770. {
  8771. dpct::has_capability_or_fail(stream->get_device(),
  8772. {sycl::aspect::fp16});
  8773. stream->parallel_for(
  8774. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8775. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8776. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  8777. vx, y, dst, ncols, nrows, item_ct1);
  8778. });
  8779. }
  8780. }
  8781. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  8782. float *dst, const int ncols,
  8783. const int nrows,
  8784. dpct::queue_ptr stream) {
  8785. GGML_ASSERT(ncols % QK_K == 0);
  8786. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  8787. const int block_num_y = (nrows + ny - 1) / ny;
  8788. const sycl::range<3> block_nums(1, 1, block_num_y);
  8789. const sycl::range<3> block_dims(1, ny, 32);
  8790. stream->parallel_for(
  8791. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8792. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8793. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  8794. });
  8795. }
  8796. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  8797. float *dst, const int ncols,
  8798. const int nrows,
  8799. dpct::queue_ptr stream) {
  8800. GGML_ASSERT(ncols % QK_K == 0);
  8801. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8802. const int block_num_y = (nrows + ny - 1) / ny;
  8803. const sycl::range<3> block_nums(1, 1, block_num_y);
  8804. const sycl::range<3> block_dims(1, ny, 32);
  8805. stream->parallel_for(
  8806. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8807. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8808. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  8809. });
  8810. }
  8811. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  8812. float *dst, const int ncols,
  8813. const int nrows,
  8814. dpct::queue_ptr stream) {
  8815. GGML_ASSERT(ncols % QK_K == 0);
  8816. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8817. const int block_num_y = (nrows + ny - 1) / ny;
  8818. const sycl::range<3> block_nums(1, 1, block_num_y);
  8819. const sycl::range<3> block_dims(1, ny, 32);
  8820. stream->parallel_for(
  8821. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8822. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8823. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  8824. });
  8825. }
  8826. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  8827. float *dst, const int ncols,
  8828. const int nrows,
  8829. dpct::queue_ptr stream) {
  8830. GGML_ASSERT(ncols % QK_K == 0);
  8831. const sycl::range<3> block_dims(1, 1, 32);
  8832. stream->parallel_for(
  8833. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  8834. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8835. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  8836. });
  8837. }
  8838. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  8839. float *dst, const int ncols,
  8840. const int nrows,
  8841. dpct::queue_ptr stream) {
  8842. GGML_ASSERT(ncols % QK_K == 0);
  8843. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8844. const int block_num_y = (nrows + ny - 1) / ny;
  8845. const sycl::range<3> block_nums(1, 1, block_num_y);
  8846. const sycl::range<3> block_dims(1, ny, 32);
  8847. stream->parallel_for(
  8848. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8849. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8850. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  8851. });
  8852. }
  8853. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  8854. float *dst, const int ncols,
  8855. const int nrows,
  8856. dpct::queue_ptr stream) {
  8857. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8858. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8859. const sycl::range<3> block_nums(1, 1, block_num_y);
  8860. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8861. {
  8862. dpct::has_capability_or_fail(stream->get_device(),
  8863. {sycl::aspect::fp16});
  8864. stream->parallel_for(
  8865. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8866. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8867. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  8868. nrows, item_ct1);
  8869. });
  8870. }
  8871. }
  8872. static void mul_mat_vec_q4_0_q8_1_sycl(const void *vx, const void *vy,
  8873. float *dst, const int ncols,
  8874. const int nrows,
  8875. dpct::queue_ptr stream) {
  8876. GGML_ASSERT(ncols % QK4_0 == 0);
  8877. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8878. const sycl::range<3> block_nums(1, 1, block_num_y);
  8879. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8880. {
  8881. stream->submit([&](sycl::handler &cgh) {
  8882. cgh.parallel_for(
  8883. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8884. [=](sycl::nd_item<3> item_ct1)
  8885. [[intel::reqd_sub_group_size(32)]] {
  8886. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0,
  8887. VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>(
  8888. vx, vy, dst, ncols, nrows, item_ct1);
  8889. });
  8890. });
  8891. }
  8892. }
  8893. static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
  8894. float *dst, const int ncols,
  8895. const int nrows,
  8896. dpct::queue_ptr stream) {
  8897. GGML_ASSERT(ncols % QK4_1 == 0);
  8898. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8899. const sycl::range<3> block_nums(1, 1, block_num_y);
  8900. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8901. {
  8902. stream->submit([&](sycl::handler &cgh) {
  8903. cgh.parallel_for(
  8904. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8905. [=](sycl::nd_item<3> item_ct1)
  8906. [[intel::reqd_sub_group_size(32)]] {
  8907. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1,
  8908. VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>(
  8909. vx, vy, dst, ncols, nrows, item_ct1);
  8910. });
  8911. });
  8912. }
  8913. }
  8914. static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
  8915. float *dst, const int ncols,
  8916. const int nrows,
  8917. dpct::queue_ptr stream) {
  8918. GGML_ASSERT(ncols % QK5_0 == 0);
  8919. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8920. const sycl::range<3> block_nums(1, 1, block_num_y);
  8921. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8922. {
  8923. stream->submit([&](sycl::handler &cgh) {
  8924. cgh.parallel_for(
  8925. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8926. [=](sycl::nd_item<3> item_ct1)
  8927. [[intel::reqd_sub_group_size(32)]] {
  8928. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0,
  8929. VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>(
  8930. vx, vy, dst, ncols, nrows, item_ct1);
  8931. });
  8932. });
  8933. }
  8934. }
  8935. static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
  8936. float *dst, const int ncols,
  8937. const int nrows,
  8938. dpct::queue_ptr stream) {
  8939. GGML_ASSERT(ncols % QK5_1 == 0);
  8940. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8941. const sycl::range<3> block_nums(1, 1, block_num_y);
  8942. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8943. {
  8944. stream->submit([&](sycl::handler &cgh) {
  8945. cgh.parallel_for(
  8946. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8947. [=](sycl::nd_item<3> item_ct1)
  8948. [[intel::reqd_sub_group_size(32)]] {
  8949. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1,
  8950. VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>(
  8951. vx, vy, dst, ncols, nrows, item_ct1);
  8952. });
  8953. });
  8954. }
  8955. }
  8956. static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
  8957. float *dst, const int ncols,
  8958. const int nrows,
  8959. dpct::queue_ptr stream) {
  8960. GGML_ASSERT(ncols % QK8_0 == 0);
  8961. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8962. const sycl::range<3> block_nums(1, 1, block_num_y);
  8963. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8964. {
  8965. stream->submit([&](sycl::handler &cgh) {
  8966. cgh.parallel_for(
  8967. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8968. [=](sycl::nd_item<3> item_ct1)
  8969. [[intel::reqd_sub_group_size(32)]] {
  8970. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0,
  8971. VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>(
  8972. vx, vy, dst, ncols, nrows, item_ct1);
  8973. });
  8974. });
  8975. }
  8976. }
  8977. static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
  8978. float *dst, const int ncols,
  8979. const int nrows,
  8980. dpct::queue_ptr stream) {
  8981. GGML_ASSERT(ncols % QK_K == 0);
  8982. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8983. const sycl::range<3> block_nums(1, 1, block_num_y);
  8984. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8985. {
  8986. stream->submit([&](sycl::handler &cgh) {
  8987. cgh.parallel_for(
  8988. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8989. [=](sycl::nd_item<3> item_ct1)
  8990. [[intel::reqd_sub_group_size(32)]] {
  8991. mul_mat_vec_q<QK_K, QI2_K, block_q2_K,
  8992. VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>(
  8993. vx, vy, dst, ncols, nrows, item_ct1);
  8994. });
  8995. });
  8996. }
  8997. }
  8998. static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
  8999. float *dst, const int ncols,
  9000. const int nrows,
  9001. dpct::queue_ptr stream) {
  9002. GGML_ASSERT(ncols % QK_K == 0);
  9003. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9004. const sycl::range<3> block_nums(1, 1, block_num_y);
  9005. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9006. {
  9007. stream->submit([&](sycl::handler &cgh) {
  9008. cgh.parallel_for(
  9009. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9010. [=](sycl::nd_item<3> item_ct1)
  9011. [[intel::reqd_sub_group_size(32)]] {
  9012. mul_mat_vec_q<QK_K, QI3_K, block_q3_K,
  9013. VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>(
  9014. vx, vy, dst, ncols, nrows, item_ct1);
  9015. });
  9016. });
  9017. }
  9018. }
  9019. static void mul_mat_vec_q4_K_q8_1_sycl(const void *vx, const void *vy,
  9020. float *dst, const int ncols,
  9021. const int nrows,
  9022. dpct::queue_ptr stream) {
  9023. GGML_ASSERT(ncols % QK_K == 0);
  9024. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9025. const sycl::range<3> block_nums(1, 1, block_num_y);
  9026. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9027. {
  9028. stream->submit([&](sycl::handler &cgh) {
  9029. cgh.parallel_for(
  9030. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9031. [=](sycl::nd_item<3> item_ct1)
  9032. [[intel::reqd_sub_group_size(32)]] {
  9033. mul_mat_vec_q<QK_K, QI4_K, block_q4_K,
  9034. VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>(
  9035. vx, vy, dst, ncols, nrows, item_ct1);
  9036. });
  9037. });
  9038. }
  9039. }
  9040. static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
  9041. float *dst, const int ncols,
  9042. const int nrows,
  9043. dpct::queue_ptr stream) {
  9044. GGML_ASSERT(ncols % QK_K == 0);
  9045. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9046. const sycl::range<3> block_nums(1, 1, block_num_y);
  9047. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9048. {
  9049. stream->submit([&](sycl::handler &cgh) {
  9050. cgh.parallel_for(
  9051. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9052. [=](sycl::nd_item<3> item_ct1)
  9053. [[intel::reqd_sub_group_size(32)]] {
  9054. mul_mat_vec_q<QK_K, QI5_K, block_q5_K,
  9055. VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
  9056. vx, vy, dst, ncols, nrows, item_ct1);
  9057. });
  9058. });
  9059. }
  9060. }
  9061. static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
  9062. float *dst, const int ncols,
  9063. const int nrows,
  9064. dpct::queue_ptr stream) {
  9065. GGML_ASSERT(ncols % QK_K == 0);
  9066. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9067. const sycl::range<3> block_nums(1, 1, block_num_y);
  9068. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9069. {
  9070. stream->submit([&](sycl::handler &cgh) {
  9071. cgh.parallel_for(
  9072. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9073. [=](sycl::nd_item<3> item_ct1)
  9074. [[intel::reqd_sub_group_size(32)]] {
  9075. mul_mat_vec_q<QK_K, QI6_K, block_q6_K,
  9076. VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>(
  9077. vx, vy, dst, ncols, nrows, item_ct1);
  9078. });
  9079. });
  9080. }
  9081. }
  9082. static void mul_mat_vec_iq2_xxs_q8_1_sycl(const void *vx, const void *vy,
  9083. float *dst, const int ncols,
  9084. const int nrows,
  9085. dpct::queue_ptr stream) {
  9086. GGML_ASSERT(ncols % QK_K == 0);
  9087. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9088. const sycl::range<3> block_nums(1, 1, block_num_y);
  9089. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9090. {
  9091. stream->submit([&](sycl::handler &cgh) {
  9092. cgh.parallel_for(
  9093. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9094. [=](sycl::nd_item<3> item_ct1)
  9095. [[intel::reqd_sub_group_size(32)]] {
  9096. mul_mat_vec_q_iq2_xxs_q8_1<QK_K, QI2_XXS, block_iq2_xxs, 1>(
  9097. vx, vy, dst, ncols, nrows, item_ct1);
  9098. });
  9099. });
  9100. }
  9101. }
  9102. static void mul_mat_vec_iq2_xs_q8_1_sycl(const void *vx, const void *vy,
  9103. float *dst, const int ncols,
  9104. const int nrows,
  9105. dpct::queue_ptr stream) {
  9106. GGML_ASSERT(ncols % QK_K == 0);
  9107. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9108. const sycl::range<3> block_nums(1, 1, block_num_y);
  9109. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9110. {
  9111. stream->submit([&](sycl::handler &cgh) {
  9112. auto iq2xs_grid_ptr_ct1 = &iq2xs_grid[0];
  9113. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9114. cgh.parallel_for(
  9115. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9116. [=](sycl::nd_item<3> item_ct1)
  9117. [[intel::reqd_sub_group_size(32)]] {
  9118. mul_mat_vec_q_iq2_xs_q8_1<QK_K, QI2_XS, block_iq2_xs, 1>(
  9119. vx, vy, dst, ncols, nrows, item_ct1);
  9120. });
  9121. });
  9122. }
  9123. }
  9124. static void mul_mat_vec_iq2_s_q8_1_sycl(const void *vx, const void *vy,
  9125. float *dst, const int ncols,
  9126. const int nrows,
  9127. dpct::queue_ptr stream) {
  9128. GGML_ASSERT(ncols % QK_K == 0);
  9129. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9130. const sycl::range<3> block_nums(1, 1, block_num_y);
  9131. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9132. {
  9133. stream->submit([&](sycl::handler &cgh) {
  9134. auto iq2xs_grid_ptr_ct1 = &iq2xs_grid[0];
  9135. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9136. cgh.parallel_for(
  9137. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9138. [=](sycl::nd_item<3> item_ct1)
  9139. [[intel::reqd_sub_group_size(32)]] {
  9140. mul_mat_vec_q_iq2_s_q8_1<QK_K, QI2_S, block_iq2_s, 1>(
  9141. vx, vy, dst, ncols, nrows, item_ct1);
  9142. });
  9143. });
  9144. }
  9145. }
  9146. static void mul_mat_vec_iq3_xxs_q8_1_sycl(const void *vx, const void *vy,
  9147. float *dst, const int ncols,
  9148. const int nrows,
  9149. dpct::queue_ptr stream) {
  9150. GGML_ASSERT(ncols % QK_K == 0);
  9151. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9152. const sycl::range<3> block_nums(1, 1, block_num_y);
  9153. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9154. {
  9155. stream->submit([&](sycl::handler &cgh) {
  9156. auto iq3xxs_grid_ptr_ct1 = &iq3xxs_grid[0];
  9157. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9158. cgh.parallel_for(
  9159. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9160. [=](sycl::nd_item<3> item_ct1)
  9161. [[intel::reqd_sub_group_size(32)]] {
  9162. mul_mat_vec_q_iq3_xxs_q8_1<QK_K, QI3_XXS, block_iq3_xxs, 1>(
  9163. vx, vy, dst, ncols, nrows, item_ct1);
  9164. });
  9165. });
  9166. }
  9167. }
  9168. static void mul_mat_vec_iq3_s_q8_1_sycl(const void *vx, const void *vy,
  9169. float *dst, const int ncols,
  9170. const int nrows,
  9171. dpct::queue_ptr stream) {
  9172. GGML_ASSERT(ncols % QK_K == 0);
  9173. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9174. const sycl::range<3> block_nums(1, 1, block_num_y);
  9175. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9176. {
  9177. stream->submit([&](sycl::handler &cgh) {
  9178. auto iq3s_grid_ptr_ct1 = &iq3s_grid[0];
  9179. cgh.parallel_for(
  9180. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9181. [=](sycl::nd_item<3> item_ct1)
  9182. [[intel::reqd_sub_group_size(32)]] {
  9183. mul_mat_vec_q_iq3_s_q8_1<QK_K, QI3_XS, block_iq3_s, 1>(
  9184. vx, vy, dst, ncols, nrows, item_ct1);
  9185. });
  9186. });
  9187. }
  9188. }
  9189. static void mul_mat_vec_iq1_s_q8_1_sycl(const void *vx, const void *vy,
  9190. float *dst, const int ncols,
  9191. const int nrows,
  9192. dpct::queue_ptr stream) {
  9193. GGML_ASSERT(ncols % QK_K == 0);
  9194. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9195. const sycl::range<3> block_nums(1, 1, block_num_y);
  9196. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9197. {
  9198. stream->submit([&](sycl::handler &cgh) {
  9199. auto iq1s_grid_ptr_ct1 = &iq1s_grid_gpu[0];
  9200. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9201. cgh.parallel_for(
  9202. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9203. [=](sycl::nd_item<3> item_ct1)
  9204. [[intel::reqd_sub_group_size(32)]] {
  9205. mul_mat_vec_q_iq1_s_q8_1<QK_K, QI1_S, block_iq1_s, 1>(
  9206. vx, vy, dst, ncols, nrows, item_ct1);
  9207. });
  9208. });
  9209. }
  9210. }
  9211. static void mul_mat_vec_iq1_m_q8_1_sycl(const void *vx, const void *vy,
  9212. float *dst, const int ncols,
  9213. const int nrows,
  9214. dpct::queue_ptr stream) {
  9215. GGML_ASSERT(ncols % QK_K == 0);
  9216. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9217. const sycl::range<3> block_nums(1, 1, block_num_y);
  9218. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9219. {
  9220. stream->submit([&](sycl::handler &cgh) {
  9221. cgh.parallel_for(
  9222. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9223. [=](sycl::nd_item<3> item_ct1)
  9224. [[intel::reqd_sub_group_size(32)]] {
  9225. mul_mat_vec_q_iq1_m_q8_1<QK_K, QI1_S, block_iq1_m, 1>(
  9226. vx, vy, dst, ncols, nrows, item_ct1);
  9227. });
  9228. });
  9229. }
  9230. }
  9231. static void mul_mat_vec_iq4_nl_q8_1_sycl(const void *vx, const void *vy,
  9232. float *dst, const int ncols,
  9233. const int nrows,
  9234. dpct::queue_ptr stream) {
  9235. GGML_ASSERT(ncols % QK4_NL == 0);
  9236. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9237. const sycl::range<3> block_nums(1, 1, block_num_y);
  9238. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9239. {
  9240. stream->submit([&](sycl::handler &cgh) {
  9241. cgh.parallel_for(
  9242. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9243. [=](sycl::nd_item<3> item_ct1)
  9244. [[intel::reqd_sub_group_size(32)]] {
  9245. mul_mat_vec_q_iq4_nl_q8_1<QK4_NL, QI4_NL, block_iq4_nl, 1>(
  9246. vx, vy, dst, ncols, nrows, item_ct1);
  9247. });
  9248. });
  9249. }
  9250. }
  9251. static void mul_mat_vec_iq4_xs_q8_1_sycl(const void *vx, const void *vy,
  9252. float *dst, const int ncols,
  9253. const int nrows,
  9254. dpct::queue_ptr stream) {
  9255. GGML_ASSERT(ncols % QK_K == 0);
  9256. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9257. const sycl::range<3> block_nums(1, 1, block_num_y);
  9258. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9259. {
  9260. stream->submit([&](sycl::handler &cgh) {
  9261. cgh.parallel_for(
  9262. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9263. [=](sycl::nd_item<3> item_ct1)
  9264. [[intel::reqd_sub_group_size(32)]] {
  9265. mul_mat_vec_q_iq4_xs_q8_1<QK_K, QI4_XS, block_iq4_xs, 1>(
  9266. vx, vy, dst, ncols, nrows, item_ct1);
  9267. });
  9268. });
  9269. }
  9270. }
  9271. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9272. float *dst, const int ncols_x,
  9273. const int nrows_x, const int ncols_y,
  9274. const int nrows_y, const int nrows_dst,
  9275. dpct::queue_ptr stream) try {
  9276. int id;
  9277. SYCL_CHECK(
  9278. CHECK_TRY_ERROR(id = get_current_device_id()));
  9279. const int compute_capability = g_device_caps[id].cc;
  9280. int mmq_x, mmq_y, nwarps;
  9281. if (compute_capability >= VER_GEN13) {
  9282. mmq_x = MMQ_X_Q4_0_RDNA2;
  9283. mmq_y = MMQ_Y_Q4_0_RDNA2;
  9284. nwarps = NWARPS_Q4_0_RDNA2;
  9285. } else if (compute_capability >= VER_GEN12) {
  9286. mmq_x = MMQ_X_Q4_0_RDNA1;
  9287. mmq_y = MMQ_Y_Q4_0_RDNA1;
  9288. nwarps = NWARPS_Q4_0_RDNA1;
  9289. } else if (compute_capability >= VER_GEN9) {
  9290. mmq_x = MMQ_X_Q4_0_AMPERE;
  9291. mmq_y = MMQ_Y_Q4_0_AMPERE;
  9292. nwarps = NWARPS_Q4_0_AMPERE;
  9293. } else if (compute_capability >= VER_4VEC) {
  9294. mmq_x = MMQ_X_Q4_0_PASCAL;
  9295. mmq_y = MMQ_Y_Q4_0_PASCAL;
  9296. nwarps = NWARPS_Q4_0_PASCAL;
  9297. } else {
  9298. GGML_ASSERT(false);
  9299. }
  9300. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9301. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9302. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9303. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9304. if (nrows_x % mmq_y == 0) {
  9305. const bool need_check = false;
  9306. /*
  9307. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  9308. the limit. To get the device limit, query
  9309. info::device::max_work_group_size. Adjust the work-group size if needed.
  9310. */
  9311. {
  9312. dpct::has_capability_or_fail(stream->get_device(),
  9313. {sycl::aspect::fp16});
  9314. stream->submit([&](sycl::handler &cgh) {
  9315. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9316. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9317. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9318. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9319. cgh);
  9320. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9321. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9322. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9323. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9324. cgh.parallel_for(
  9325. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9326. [=](sycl::nd_item<3> item_ct1) {
  9327. mul_mat_q4_0<need_check>(
  9328. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9329. nrows_dst, item_ct1,
  9330. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9331. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9332. tile_y_qs_acc_ct1.get_pointer(),
  9333. tile_y_ds_acc_ct1.get_pointer());
  9334. });
  9335. });
  9336. }
  9337. } else {
  9338. const bool need_check = true;
  9339. /*
  9340. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  9341. the limit. To get the device limit, query
  9342. info::device::max_work_group_size. Adjust the work-group size if needed.
  9343. */
  9344. {
  9345. dpct::has_capability_or_fail(stream->get_device(),
  9346. {sycl::aspect::fp16});
  9347. stream->submit([&](sycl::handler &cgh) {
  9348. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9349. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9350. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9351. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9352. cgh);
  9353. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9354. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9355. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9356. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9357. cgh.parallel_for(
  9358. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9359. [=](sycl::nd_item<3> item_ct1) {
  9360. mul_mat_q4_0<need_check>(
  9361. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9362. nrows_dst, item_ct1,
  9363. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9364. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9365. tile_y_qs_acc_ct1.get_pointer(),
  9366. tile_y_ds_acc_ct1.get_pointer());
  9367. });
  9368. });
  9369. }
  9370. }
  9371. }
  9372. catch (sycl::exception const &exc) {
  9373. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9374. << ", line:" << __LINE__ << std::endl;
  9375. std::exit(1);
  9376. }
  9377. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9378. float *dst, const int ncols_x,
  9379. const int nrows_x, const int ncols_y,
  9380. const int nrows_y, const int nrows_dst,
  9381. dpct::queue_ptr stream) try {
  9382. int id;
  9383. SYCL_CHECK(
  9384. CHECK_TRY_ERROR(id = get_current_device_id()));
  9385. const int compute_capability = g_device_caps[id].cc;
  9386. int mmq_x, mmq_y, nwarps;
  9387. if (compute_capability >= VER_GEN13) {
  9388. mmq_x = MMQ_X_Q4_1_RDNA2;
  9389. mmq_y = MMQ_Y_Q4_1_RDNA2;
  9390. nwarps = NWARPS_Q4_1_RDNA2;
  9391. } else if (compute_capability >= VER_GEN12) {
  9392. mmq_x = MMQ_X_Q4_1_RDNA1;
  9393. mmq_y = MMQ_Y_Q4_1_RDNA1;
  9394. nwarps = NWARPS_Q4_1_RDNA1;
  9395. } else if (compute_capability >= VER_GEN9) {
  9396. mmq_x = MMQ_X_Q4_1_AMPERE;
  9397. mmq_y = MMQ_Y_Q4_1_AMPERE;
  9398. nwarps = NWARPS_Q4_1_AMPERE;
  9399. } else if (compute_capability >= VER_4VEC) {
  9400. mmq_x = MMQ_X_Q4_1_PASCAL;
  9401. mmq_y = MMQ_Y_Q4_1_PASCAL;
  9402. nwarps = NWARPS_Q4_1_PASCAL;
  9403. } else {
  9404. GGML_ASSERT(false);
  9405. }
  9406. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9407. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9408. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9409. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9410. if (nrows_x % mmq_y == 0) {
  9411. const bool need_check = false;
  9412. /*
  9413. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  9414. the limit. To get the device limit, query
  9415. info::device::max_work_group_size. Adjust the work-group size if needed.
  9416. */
  9417. {
  9418. dpct::has_capability_or_fail(stream->get_device(),
  9419. {sycl::aspect::fp16});
  9420. stream->submit([&](sycl::handler &cgh) {
  9421. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9422. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9423. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9424. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9425. cgh);
  9426. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9427. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9428. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9429. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9430. cgh.parallel_for(
  9431. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9432. [=](sycl::nd_item<3> item_ct1) {
  9433. mul_mat_q4_1<need_check>(
  9434. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9435. nrows_dst, item_ct1,
  9436. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9437. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9438. tile_y_qs_acc_ct1.get_pointer(),
  9439. tile_y_ds_acc_ct1.get_pointer());
  9440. });
  9441. });
  9442. }
  9443. } else {
  9444. const bool need_check = true;
  9445. /*
  9446. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  9447. the limit. To get the device limit, query
  9448. info::device::max_work_group_size. Adjust the work-group size if needed.
  9449. */
  9450. {
  9451. dpct::has_capability_or_fail(stream->get_device(),
  9452. {sycl::aspect::fp16});
  9453. stream->submit([&](sycl::handler &cgh) {
  9454. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9455. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9456. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9457. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9458. cgh);
  9459. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9460. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9461. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9462. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9463. cgh.parallel_for(
  9464. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9465. [=](sycl::nd_item<3> item_ct1) {
  9466. mul_mat_q4_1<need_check>(
  9467. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9468. nrows_dst, item_ct1,
  9469. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9470. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9471. tile_y_qs_acc_ct1.get_pointer(),
  9472. tile_y_ds_acc_ct1.get_pointer());
  9473. });
  9474. });
  9475. }
  9476. }
  9477. }
  9478. catch (sycl::exception const &exc) {
  9479. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9480. << ", line:" << __LINE__ << std::endl;
  9481. std::exit(1);
  9482. }
  9483. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9484. float *dst, const int ncols_x,
  9485. const int nrows_x, const int ncols_y,
  9486. const int nrows_y, const int nrows_dst,
  9487. dpct::queue_ptr stream) try {
  9488. int id;
  9489. SYCL_CHECK(
  9490. CHECK_TRY_ERROR(id = get_current_device_id()));
  9491. const int compute_capability = g_device_caps[id].cc;
  9492. int mmq_x, mmq_y, nwarps;
  9493. if (compute_capability >= VER_GEN13) {
  9494. mmq_x = MMQ_X_Q5_0_RDNA2;
  9495. mmq_y = MMQ_Y_Q5_0_RDNA2;
  9496. nwarps = NWARPS_Q5_0_RDNA2;
  9497. } else if (compute_capability >= VER_GEN12) {
  9498. mmq_x = MMQ_X_Q5_0_RDNA1;
  9499. mmq_y = MMQ_Y_Q5_0_RDNA1;
  9500. nwarps = NWARPS_Q5_0_RDNA1;
  9501. } else if (compute_capability >= VER_GEN9) {
  9502. mmq_x = MMQ_X_Q5_0_AMPERE;
  9503. mmq_y = MMQ_Y_Q5_0_AMPERE;
  9504. nwarps = NWARPS_Q5_0_AMPERE;
  9505. } else if (compute_capability >= VER_4VEC) {
  9506. mmq_x = MMQ_X_Q5_0_PASCAL;
  9507. mmq_y = MMQ_Y_Q5_0_PASCAL;
  9508. nwarps = NWARPS_Q5_0_PASCAL;
  9509. } else {
  9510. GGML_ASSERT(false);
  9511. }
  9512. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9513. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9514. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9515. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9516. if (nrows_x % mmq_y == 0) {
  9517. const bool need_check = false;
  9518. /*
  9519. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  9520. the limit. To get the device limit, query
  9521. info::device::max_work_group_size. Adjust the work-group size if needed.
  9522. */
  9523. {
  9524. dpct::has_capability_or_fail(stream->get_device(),
  9525. {sycl::aspect::fp16});
  9526. stream->submit([&](sycl::handler &cgh) {
  9527. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9528. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9529. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9530. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9531. cgh);
  9532. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9533. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9534. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9535. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9536. cgh.parallel_for(
  9537. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9538. [=](sycl::nd_item<3> item_ct1) {
  9539. mul_mat_q5_0<need_check>(
  9540. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9541. nrows_dst, item_ct1,
  9542. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9543. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9544. tile_y_qs_acc_ct1.get_pointer(),
  9545. tile_y_ds_acc_ct1.get_pointer());
  9546. });
  9547. });
  9548. }
  9549. } else {
  9550. const bool need_check = true;
  9551. /*
  9552. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  9553. the limit. To get the device limit, query
  9554. info::device::max_work_group_size. Adjust the work-group size if needed.
  9555. */
  9556. {
  9557. dpct::has_capability_or_fail(stream->get_device(),
  9558. {sycl::aspect::fp16});
  9559. stream->submit([&](sycl::handler &cgh) {
  9560. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9561. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9562. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9563. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9564. cgh);
  9565. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9566. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9567. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9568. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9569. cgh.parallel_for(
  9570. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9571. [=](sycl::nd_item<3> item_ct1) {
  9572. mul_mat_q5_0<need_check>(
  9573. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9574. nrows_dst, item_ct1,
  9575. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9576. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9577. tile_y_qs_acc_ct1.get_pointer(),
  9578. tile_y_ds_acc_ct1.get_pointer());
  9579. });
  9580. });
  9581. }
  9582. }
  9583. }
  9584. catch (sycl::exception const &exc) {
  9585. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9586. << ", line:" << __LINE__ << std::endl;
  9587. std::exit(1);
  9588. }
  9589. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9590. float *dst, const int ncols_x,
  9591. const int nrows_x, const int ncols_y,
  9592. const int nrows_y, const int nrows_dst,
  9593. dpct::queue_ptr stream) try {
  9594. int id;
  9595. SYCL_CHECK(
  9596. CHECK_TRY_ERROR(id = get_current_device_id()));
  9597. const int compute_capability = g_device_caps[id].cc;
  9598. int mmq_x, mmq_y, nwarps;
  9599. if (compute_capability >= VER_GEN13) {
  9600. mmq_x = MMQ_X_Q5_1_RDNA2;
  9601. mmq_y = MMQ_Y_Q5_1_RDNA2;
  9602. nwarps = NWARPS_Q5_1_RDNA2;
  9603. } else if (compute_capability >= VER_GEN12) {
  9604. mmq_x = MMQ_X_Q5_1_RDNA1;
  9605. mmq_y = MMQ_Y_Q5_1_RDNA1;
  9606. nwarps = NWARPS_Q5_1_RDNA1;
  9607. } else if (compute_capability >= VER_GEN9) {
  9608. mmq_x = MMQ_X_Q5_1_AMPERE;
  9609. mmq_y = MMQ_Y_Q5_1_AMPERE;
  9610. nwarps = NWARPS_Q5_1_AMPERE;
  9611. } else if (compute_capability >= VER_4VEC) {
  9612. mmq_x = MMQ_X_Q5_1_PASCAL;
  9613. mmq_y = MMQ_Y_Q5_1_PASCAL;
  9614. nwarps = NWARPS_Q5_1_PASCAL;
  9615. } else {
  9616. GGML_ASSERT(false);
  9617. }
  9618. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9619. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9620. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9621. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9622. if (nrows_x % mmq_y == 0) {
  9623. const bool need_check = false;
  9624. /*
  9625. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  9626. the limit. To get the device limit, query
  9627. info::device::max_work_group_size. Adjust the work-group size if needed.
  9628. */
  9629. {
  9630. dpct::has_capability_or_fail(stream->get_device(),
  9631. {sycl::aspect::fp16});
  9632. stream->submit([&](sycl::handler &cgh) {
  9633. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  9634. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9635. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  9636. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  9637. cgh);
  9638. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9639. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9640. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9641. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9642. cgh.parallel_for(
  9643. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9644. [=](sycl::nd_item<3> item_ct1) {
  9645. mul_mat_q5_1<need_check>(
  9646. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9647. nrows_dst, item_ct1,
  9648. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  9649. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  9650. tile_y_qs_acc_ct1.get_pointer(),
  9651. tile_y_ds_acc_ct1.get_pointer());
  9652. });
  9653. });
  9654. }
  9655. } else {
  9656. const bool need_check = true;
  9657. /*
  9658. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  9659. the limit. To get the device limit, query
  9660. info::device::max_work_group_size. Adjust the work-group size if needed.
  9661. */
  9662. {
  9663. dpct::has_capability_or_fail(stream->get_device(),
  9664. {sycl::aspect::fp16});
  9665. stream->submit([&](sycl::handler &cgh) {
  9666. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  9667. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9668. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  9669. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  9670. cgh);
  9671. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9672. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9673. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9674. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9675. cgh.parallel_for(
  9676. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9677. [=](sycl::nd_item<3> item_ct1) {
  9678. mul_mat_q5_1<need_check>(
  9679. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9680. nrows_dst, item_ct1,
  9681. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  9682. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  9683. tile_y_qs_acc_ct1.get_pointer(),
  9684. tile_y_ds_acc_ct1.get_pointer());
  9685. });
  9686. });
  9687. }
  9688. }
  9689. }
  9690. catch (sycl::exception const &exc) {
  9691. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9692. << ", line:" << __LINE__ << std::endl;
  9693. std::exit(1);
  9694. }
  9695. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  9696. float *dst, const int ncols_x,
  9697. const int nrows_x, const int ncols_y,
  9698. const int nrows_y, const int nrows_dst,
  9699. dpct::queue_ptr stream) try {
  9700. int id;
  9701. SYCL_CHECK(
  9702. CHECK_TRY_ERROR(id = get_current_device_id()));
  9703. const int compute_capability = g_device_caps[id].cc;
  9704. int mmq_x, mmq_y, nwarps;
  9705. if (compute_capability >= VER_GEN13) {
  9706. mmq_x = MMQ_X_Q8_0_RDNA2;
  9707. mmq_y = MMQ_Y_Q8_0_RDNA2;
  9708. nwarps = NWARPS_Q8_0_RDNA2;
  9709. } else if (compute_capability >= VER_GEN12) {
  9710. mmq_x = MMQ_X_Q8_0_RDNA1;
  9711. mmq_y = MMQ_Y_Q8_0_RDNA1;
  9712. nwarps = NWARPS_Q8_0_RDNA1;
  9713. } else if (compute_capability >= VER_GEN9) {
  9714. mmq_x = MMQ_X_Q8_0_AMPERE;
  9715. mmq_y = MMQ_Y_Q8_0_AMPERE;
  9716. nwarps = NWARPS_Q8_0_AMPERE;
  9717. } else if (compute_capability >= VER_4VEC) {
  9718. mmq_x = MMQ_X_Q8_0_PASCAL;
  9719. mmq_y = MMQ_Y_Q8_0_PASCAL;
  9720. nwarps = NWARPS_Q8_0_PASCAL;
  9721. } else {
  9722. GGML_ASSERT(false);
  9723. }
  9724. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9725. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9726. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9727. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9728. if (nrows_x % mmq_y == 0) {
  9729. const bool need_check = false;
  9730. /*
  9731. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  9732. the limit. To get the device limit, query
  9733. info::device::max_work_group_size. Adjust the work-group size if needed.
  9734. */
  9735. {
  9736. dpct::has_capability_or_fail(stream->get_device(),
  9737. {sycl::aspect::fp16});
  9738. stream->submit([&](sycl::handler &cgh) {
  9739. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  9740. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9741. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  9742. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  9743. cgh);
  9744. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9745. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9746. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9747. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9748. cgh.parallel_for(
  9749. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9750. [=](sycl::nd_item<3> item_ct1) {
  9751. mul_mat_q8_0<need_check>(
  9752. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9753. nrows_dst, item_ct1,
  9754. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  9755. tile_x_d_q8_0_acc_ct1.get_pointer(),
  9756. tile_y_qs_acc_ct1.get_pointer(),
  9757. tile_y_ds_acc_ct1.get_pointer());
  9758. });
  9759. });
  9760. }
  9761. } else {
  9762. const bool need_check = true;
  9763. /*
  9764. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  9765. the limit. To get the device limit, query
  9766. info::device::max_work_group_size. Adjust the work-group size if needed.
  9767. */
  9768. {
  9769. dpct::has_capability_or_fail(stream->get_device(),
  9770. {sycl::aspect::fp16});
  9771. stream->submit([&](sycl::handler &cgh) {
  9772. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  9773. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9774. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  9775. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  9776. cgh);
  9777. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9778. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9779. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9780. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9781. cgh.parallel_for(
  9782. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9783. [=](sycl::nd_item<3> item_ct1) {
  9784. mul_mat_q8_0<need_check>(
  9785. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9786. nrows_dst, item_ct1,
  9787. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  9788. tile_x_d_q8_0_acc_ct1.get_pointer(),
  9789. tile_y_qs_acc_ct1.get_pointer(),
  9790. tile_y_ds_acc_ct1.get_pointer());
  9791. });
  9792. });
  9793. }
  9794. }
  9795. }
  9796. catch (sycl::exception const &exc) {
  9797. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9798. << ", line:" << __LINE__ << std::endl;
  9799. std::exit(1);
  9800. }
  9801. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  9802. float *dst, const int ncols_x,
  9803. const int nrows_x, const int ncols_y,
  9804. const int nrows_y, const int nrows_dst,
  9805. dpct::queue_ptr stream) try {
  9806. int id;
  9807. SYCL_CHECK(
  9808. CHECK_TRY_ERROR(id = get_current_device_id()));
  9809. const int compute_capability = g_device_caps[id].cc;
  9810. int mmq_x, mmq_y, nwarps;
  9811. if (compute_capability >= VER_GEN13) {
  9812. mmq_x = MMQ_X_Q2_K_RDNA2;
  9813. mmq_y = MMQ_Y_Q2_K_RDNA2;
  9814. nwarps = NWARPS_Q2_K_RDNA2;
  9815. } else if (compute_capability >= VER_GEN12) {
  9816. mmq_x = MMQ_X_Q2_K_RDNA1;
  9817. mmq_y = MMQ_Y_Q2_K_RDNA1;
  9818. nwarps = NWARPS_Q2_K_RDNA1;
  9819. } else if (compute_capability >= VER_GEN9) {
  9820. mmq_x = MMQ_X_Q2_K_AMPERE;
  9821. mmq_y = MMQ_Y_Q2_K_AMPERE;
  9822. nwarps = NWARPS_Q2_K_AMPERE;
  9823. } else if (compute_capability >= VER_4VEC) {
  9824. mmq_x = MMQ_X_Q2_K_PASCAL;
  9825. mmq_y = MMQ_Y_Q2_K_PASCAL;
  9826. nwarps = NWARPS_Q2_K_PASCAL;
  9827. } else {
  9828. GGML_ASSERT(false);
  9829. }
  9830. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9831. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9832. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9833. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9834. if (nrows_x % mmq_y == 0) {
  9835. const bool need_check = false;
  9836. /*
  9837. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  9838. the limit. To get the device limit, query
  9839. info::device::max_work_group_size. Adjust the work-group size if needed.
  9840. */
  9841. {
  9842. dpct::has_capability_or_fail(stream->get_device(),
  9843. {sycl::aspect::fp16});
  9844. stream->submit([&](sycl::handler &cgh) {
  9845. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  9846. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9847. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  9848. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  9849. cgh);
  9850. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  9851. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9852. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9853. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9854. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9855. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9856. cgh.parallel_for(
  9857. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9858. [=](sycl::nd_item<3> item_ct1) {
  9859. mul_mat_q2_K<need_check>(
  9860. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9861. nrows_dst, item_ct1,
  9862. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  9863. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  9864. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  9865. tile_y_qs_acc_ct1.get_pointer(),
  9866. tile_y_ds_acc_ct1.get_pointer());
  9867. });
  9868. });
  9869. }
  9870. } else {
  9871. const bool need_check = true;
  9872. /*
  9873. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  9874. the limit. To get the device limit, query
  9875. info::device::max_work_group_size. Adjust the work-group size if needed.
  9876. */
  9877. {
  9878. dpct::has_capability_or_fail(stream->get_device(),
  9879. {sycl::aspect::fp16});
  9880. stream->submit([&](sycl::handler &cgh) {
  9881. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  9882. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9883. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  9884. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  9885. cgh);
  9886. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  9887. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9888. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9889. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9890. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9891. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9892. cgh.parallel_for(
  9893. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9894. [=](sycl::nd_item<3> item_ct1) {
  9895. mul_mat_q2_K<need_check>(
  9896. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9897. nrows_dst, item_ct1,
  9898. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  9899. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  9900. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  9901. tile_y_qs_acc_ct1.get_pointer(),
  9902. tile_y_ds_acc_ct1.get_pointer());
  9903. });
  9904. });
  9905. }
  9906. }
  9907. }
  9908. catch (sycl::exception const &exc) {
  9909. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9910. << ", line:" << __LINE__ << std::endl;
  9911. std::exit(1);
  9912. }
  9913. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  9914. float *dst, const int ncols_x,
  9915. const int nrows_x, const int ncols_y,
  9916. const int nrows_y, const int nrows_dst,
  9917. dpct::queue_ptr stream) try {
  9918. int id;
  9919. SYCL_CHECK(
  9920. CHECK_TRY_ERROR(id = get_current_device_id()));
  9921. const int compute_capability = g_device_caps[id].cc;
  9922. int mmq_x, mmq_y, nwarps;
  9923. if (compute_capability >= VER_GEN13) {
  9924. mmq_x = MMQ_X_Q3_K_RDNA2;
  9925. mmq_y = MMQ_Y_Q3_K_RDNA2;
  9926. nwarps = NWARPS_Q3_K_RDNA2;
  9927. } else if (compute_capability >= VER_GEN12) {
  9928. mmq_x = MMQ_X_Q3_K_RDNA1;
  9929. mmq_y = MMQ_Y_Q3_K_RDNA1;
  9930. nwarps = NWARPS_Q3_K_RDNA1;
  9931. } else if (compute_capability >= VER_GEN9) {
  9932. mmq_x = MMQ_X_Q3_K_AMPERE;
  9933. mmq_y = MMQ_Y_Q3_K_AMPERE;
  9934. nwarps = NWARPS_Q3_K_AMPERE;
  9935. } else if (compute_capability >= VER_4VEC) {
  9936. mmq_x = MMQ_X_Q3_K_PASCAL;
  9937. mmq_y = MMQ_Y_Q3_K_PASCAL;
  9938. nwarps = NWARPS_Q3_K_PASCAL;
  9939. } else {
  9940. GGML_ASSERT(false);
  9941. }
  9942. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9943. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9944. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9945. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9946. if (nrows_x % mmq_y == 0) {
  9947. const bool need_check = false;
  9948. /*
  9949. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  9950. the limit. To get the device limit, query
  9951. info::device::max_work_group_size. Adjust the work-group size if needed.
  9952. */
  9953. {
  9954. dpct::has_capability_or_fail(stream->get_device(),
  9955. {sycl::aspect::fp16});
  9956. stream->submit([&](sycl::handler &cgh) {
  9957. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  9958. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9959. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  9960. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  9961. cgh);
  9962. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  9963. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  9964. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  9965. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9966. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9967. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9968. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9969. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9970. cgh.parallel_for(
  9971. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9972. [=](sycl::nd_item<3> item_ct1) {
  9973. mul_mat_q3_K<need_check>(
  9974. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9975. nrows_dst, item_ct1,
  9976. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  9977. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  9978. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  9979. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  9980. tile_y_qs_acc_ct1.get_pointer(),
  9981. tile_y_ds_acc_ct1.get_pointer());
  9982. });
  9983. });
  9984. }
  9985. } else {
  9986. const bool need_check = true;
  9987. /*
  9988. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  9989. the limit. To get the device limit, query
  9990. info::device::max_work_group_size. Adjust the work-group size if needed.
  9991. */
  9992. {
  9993. dpct::has_capability_or_fail(stream->get_device(),
  9994. {sycl::aspect::fp16});
  9995. stream->submit([&](sycl::handler &cgh) {
  9996. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  9997. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9998. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  9999. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  10000. cgh);
  10001. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  10002. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  10003. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  10004. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10005. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10006. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10007. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10008. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10009. cgh.parallel_for(
  10010. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10011. [=](sycl::nd_item<3> item_ct1) {
  10012. mul_mat_q3_K<need_check>(
  10013. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10014. nrows_dst, item_ct1,
  10015. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  10016. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  10017. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  10018. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  10019. tile_y_qs_acc_ct1.get_pointer(),
  10020. tile_y_ds_acc_ct1.get_pointer());
  10021. });
  10022. });
  10023. }
  10024. }
  10025. }
  10026. catch (sycl::exception const &exc) {
  10027. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10028. << ", line:" << __LINE__ << std::endl;
  10029. std::exit(1);
  10030. }
  10031. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  10032. float *dst, const int ncols_x,
  10033. const int nrows_x, const int ncols_y,
  10034. const int nrows_y, const int nrows_dst,
  10035. dpct::queue_ptr stream) try {
  10036. int id;
  10037. SYCL_CHECK(
  10038. CHECK_TRY_ERROR(id = get_current_device_id()));
  10039. const int compute_capability = g_device_caps[id].cc;
  10040. int mmq_x, mmq_y, nwarps;
  10041. if (compute_capability >= VER_GEN13) {
  10042. mmq_x = MMQ_X_Q4_K_RDNA2;
  10043. mmq_y = MMQ_Y_Q4_K_RDNA2;
  10044. nwarps = NWARPS_Q4_K_RDNA2;
  10045. } else if (compute_capability >= VER_GEN12) {
  10046. mmq_x = MMQ_X_Q4_K_RDNA1;
  10047. mmq_y = MMQ_Y_Q4_K_RDNA1;
  10048. nwarps = NWARPS_Q4_K_RDNA1;
  10049. } else if (compute_capability >= VER_GEN9) {
  10050. mmq_x = MMQ_X_Q4_K_AMPERE;
  10051. mmq_y = MMQ_Y_Q4_K_AMPERE;
  10052. nwarps = NWARPS_Q4_K_AMPERE;
  10053. } else if (compute_capability >= VER_4VEC) {
  10054. mmq_x = MMQ_X_Q4_K_PASCAL;
  10055. mmq_y = MMQ_Y_Q4_K_PASCAL;
  10056. nwarps = NWARPS_Q4_K_PASCAL;
  10057. } else {
  10058. GGML_ASSERT(false);
  10059. }
  10060. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10061. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10062. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10063. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10064. if (nrows_x % mmq_y == 0) {
  10065. const bool need_check = false;
  10066. /*
  10067. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  10068. the limit. To get the device limit, query
  10069. info::device::max_work_group_size. Adjust the work-group size if needed.
  10070. */
  10071. {
  10072. dpct::has_capability_or_fail(stream->get_device(),
  10073. {sycl::aspect::fp16});
  10074. stream->submit([&](sycl::handler &cgh) {
  10075. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10076. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10077. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10078. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10079. cgh);
  10080. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10081. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10082. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10083. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10084. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10085. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10086. cgh.parallel_for(
  10087. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10088. [=](sycl::nd_item<3> item_ct1) {
  10089. mul_mat_q4_K<need_check>(
  10090. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10091. nrows_dst, item_ct1,
  10092. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10093. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10094. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10095. tile_y_qs_acc_ct1.get_pointer(),
  10096. tile_y_ds_acc_ct1.get_pointer());
  10097. });
  10098. });
  10099. }
  10100. } else {
  10101. const bool need_check = true;
  10102. /*
  10103. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  10104. the limit. To get the device limit, query
  10105. info::device::max_work_group_size. Adjust the work-group size if needed.
  10106. */
  10107. {
  10108. dpct::has_capability_or_fail(stream->get_device(),
  10109. {sycl::aspect::fp16});
  10110. stream->submit([&](sycl::handler &cgh) {
  10111. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10112. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10113. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10114. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10115. cgh);
  10116. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10117. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10118. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10119. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10120. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10121. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10122. cgh.parallel_for(
  10123. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10124. [=](sycl::nd_item<3> item_ct1) {
  10125. mul_mat_q4_K<need_check>(
  10126. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10127. nrows_dst, item_ct1,
  10128. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10129. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10130. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10131. tile_y_qs_acc_ct1.get_pointer(),
  10132. tile_y_ds_acc_ct1.get_pointer());
  10133. });
  10134. });
  10135. }
  10136. }
  10137. }
  10138. catch (sycl::exception const &exc) {
  10139. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10140. << ", line:" << __LINE__ << std::endl;
  10141. std::exit(1);
  10142. }
  10143. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  10144. float *dst, const int ncols_x,
  10145. const int nrows_x, const int ncols_y,
  10146. const int nrows_y, const int nrows_dst,
  10147. dpct::queue_ptr stream) try {
  10148. int id;
  10149. SYCL_CHECK(
  10150. CHECK_TRY_ERROR(id = get_current_device_id()));
  10151. const int compute_capability = g_device_caps[id].cc;
  10152. int mmq_x, mmq_y, nwarps;
  10153. if (compute_capability >= VER_GEN13) {
  10154. mmq_x = MMQ_X_Q5_K_RDNA2;
  10155. mmq_y = MMQ_Y_Q5_K_RDNA2;
  10156. nwarps = NWARPS_Q5_K_RDNA2;
  10157. } else if (compute_capability >= VER_GEN12) {
  10158. mmq_x = MMQ_X_Q5_K_RDNA1;
  10159. mmq_y = MMQ_Y_Q5_K_RDNA1;
  10160. nwarps = NWARPS_Q5_K_RDNA1;
  10161. } else if (compute_capability >= VER_GEN9) {
  10162. mmq_x = MMQ_X_Q5_K_AMPERE;
  10163. mmq_y = MMQ_Y_Q5_K_AMPERE;
  10164. nwarps = NWARPS_Q5_K_AMPERE;
  10165. } else if (compute_capability >= VER_4VEC) {
  10166. mmq_x = MMQ_X_Q5_K_PASCAL;
  10167. mmq_y = MMQ_Y_Q5_K_PASCAL;
  10168. nwarps = NWARPS_Q5_K_PASCAL;
  10169. } else {
  10170. GGML_ASSERT(false);
  10171. }
  10172. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10173. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10174. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10175. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10176. if (nrows_x % mmq_y == 0) {
  10177. const bool need_check = false;
  10178. /*
  10179. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  10180. the limit. To get the device limit, query
  10181. info::device::max_work_group_size. Adjust the work-group size if needed.
  10182. */
  10183. {
  10184. dpct::has_capability_or_fail(stream->get_device(),
  10185. {sycl::aspect::fp16});
  10186. stream->submit([&](sycl::handler &cgh) {
  10187. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10188. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10189. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10190. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10191. cgh);
  10192. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10193. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10194. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10195. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10196. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10197. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10198. cgh.parallel_for(
  10199. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10200. [=](sycl::nd_item<3> item_ct1) {
  10201. mul_mat_q5_K<need_check>(
  10202. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10203. nrows_dst, item_ct1,
  10204. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10205. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10206. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10207. tile_y_qs_acc_ct1.get_pointer(),
  10208. tile_y_ds_acc_ct1.get_pointer());
  10209. });
  10210. });
  10211. }
  10212. } else {
  10213. const bool need_check = true;
  10214. /*
  10215. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  10216. the limit. To get the device limit, query
  10217. info::device::max_work_group_size. Adjust the work-group size if needed.
  10218. */
  10219. {
  10220. dpct::has_capability_or_fail(stream->get_device(),
  10221. {sycl::aspect::fp16});
  10222. stream->submit([&](sycl::handler &cgh) {
  10223. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10224. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10225. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10226. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10227. cgh);
  10228. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10229. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10230. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10231. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10232. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10233. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10234. cgh.parallel_for(
  10235. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10236. [=](sycl::nd_item<3> item_ct1) {
  10237. mul_mat_q5_K<need_check>(
  10238. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10239. nrows_dst, item_ct1,
  10240. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10241. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10242. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10243. tile_y_qs_acc_ct1.get_pointer(),
  10244. tile_y_ds_acc_ct1.get_pointer());
  10245. });
  10246. });
  10247. }
  10248. }
  10249. }
  10250. catch (sycl::exception const &exc) {
  10251. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10252. << ", line:" << __LINE__ << std::endl;
  10253. std::exit(1);
  10254. }
  10255. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  10256. float *dst, const int ncols_x,
  10257. const int nrows_x, const int ncols_y,
  10258. const int nrows_y, const int nrows_dst,
  10259. dpct::queue_ptr stream) try {
  10260. int id;
  10261. SYCL_CHECK(
  10262. CHECK_TRY_ERROR(id = get_current_device_id()));
  10263. const int compute_capability = g_device_caps[id].cc;
  10264. int mmq_x, mmq_y, nwarps;
  10265. if (compute_capability >= VER_GEN13) {
  10266. mmq_x = MMQ_X_Q6_K_RDNA2;
  10267. mmq_y = MMQ_Y_Q6_K_RDNA2;
  10268. nwarps = NWARPS_Q6_K_RDNA2;
  10269. } else if (compute_capability >= VER_GEN12) {
  10270. mmq_x = MMQ_X_Q6_K_RDNA1;
  10271. mmq_y = MMQ_Y_Q6_K_RDNA1;
  10272. nwarps = NWARPS_Q6_K_RDNA1;
  10273. } else if (compute_capability >= VER_GEN9) {
  10274. mmq_x = MMQ_X_Q6_K_AMPERE;
  10275. mmq_y = MMQ_Y_Q6_K_AMPERE;
  10276. nwarps = NWARPS_Q6_K_AMPERE;
  10277. } else if (compute_capability >= VER_4VEC) {
  10278. mmq_x = MMQ_X_Q6_K_PASCAL;
  10279. mmq_y = MMQ_Y_Q6_K_PASCAL;
  10280. nwarps = NWARPS_Q6_K_PASCAL;
  10281. } else {
  10282. GGML_ASSERT(false);
  10283. }
  10284. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10285. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10286. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10287. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10288. if (nrows_x % mmq_y == 0) {
  10289. const bool need_check = false;
  10290. /*
  10291. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  10292. the limit. To get the device limit, query
  10293. info::device::max_work_group_size. Adjust the work-group size if needed.
  10294. */
  10295. {
  10296. dpct::has_capability_or_fail(stream->get_device(),
  10297. {sycl::aspect::fp16});
  10298. stream->submit([&](sycl::handler &cgh) {
  10299. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10300. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10301. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10302. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10303. cgh);
  10304. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10305. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10306. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10307. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10308. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10309. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10310. cgh.parallel_for(
  10311. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10312. [=](sycl::nd_item<3> item_ct1) {
  10313. mul_mat_q6_K<need_check>(
  10314. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10315. nrows_dst, item_ct1,
  10316. tile_x_ql_acc_ct1.get_pointer(),
  10317. tile_x_dm_acc_ct1.get_pointer(),
  10318. tile_x_sc_acc_ct1.get_pointer(),
  10319. tile_y_qs_acc_ct1.get_pointer(),
  10320. tile_y_ds_acc_ct1.get_pointer());
  10321. });
  10322. });
  10323. }
  10324. } else {
  10325. const bool need_check = true;
  10326. /*
  10327. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  10328. the limit. To get the device limit, query
  10329. info::device::max_work_group_size. Adjust the work-group size if needed.
  10330. */
  10331. {
  10332. dpct::has_capability_or_fail(stream->get_device(),
  10333. {sycl::aspect::fp16});
  10334. stream->submit([&](sycl::handler &cgh) {
  10335. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10336. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10337. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10338. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10339. cgh);
  10340. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10341. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10342. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10343. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10344. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10345. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10346. cgh.parallel_for(
  10347. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10348. [=](sycl::nd_item<3> item_ct1) {
  10349. mul_mat_q6_K<need_check>(
  10350. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10351. nrows_dst, item_ct1,
  10352. tile_x_ql_acc_ct1.get_pointer(),
  10353. tile_x_dm_acc_ct1.get_pointer(),
  10354. tile_x_sc_acc_ct1.get_pointer(),
  10355. tile_y_qs_acc_ct1.get_pointer(),
  10356. tile_y_ds_acc_ct1.get_pointer());
  10357. });
  10358. });
  10359. }
  10360. }
  10361. }
  10362. catch (sycl::exception const &exc) {
  10363. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10364. << ", line:" << __LINE__ << std::endl;
  10365. std::exit(1);
  10366. }
  10367. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  10368. float *dst, const int ncols_x,
  10369. const int nrows_x,
  10370. const int nchannels_x,
  10371. const int nchannels_y,
  10372. dpct::queue_ptr stream) {
  10373. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10374. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10375. {
  10376. dpct::has_capability_or_fail(stream->get_device(),
  10377. {sycl::aspect::fp16});
  10378. stream->parallel_for(
  10379. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10380. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10381. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  10382. nchannels_y, item_ct1);
  10383. });
  10384. }
  10385. }
  10386. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  10387. const void *vx, const float *y, float *dst, const int ncols_x,
  10388. const int nrows_x, const int row_stride_x, const int nchannels_x,
  10389. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  10390. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10391. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10392. {
  10393. dpct::has_capability_or_fail(stream->get_device(),
  10394. {sycl::aspect::fp16});
  10395. stream->parallel_for(
  10396. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10397. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10398. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  10399. row_stride_x, channel_stride_x,
  10400. nchannels_y / nchannels_x, item_ct1);
  10401. });
  10402. }
  10403. }
  10404. static void
  10405. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  10406. const int ne01, const int ne02, const int nb00,
  10407. const int nb01, const int nb02, const int nb03,
  10408. const int ne10, const int ne11, const int ne12,
  10409. const int nb10, const int nb11, const int nb12,
  10410. const int nb13, dpct::queue_ptr stream) {
  10411. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10412. {
  10413. dpct::has_capability_or_fail(stream->get_device(),
  10414. {sycl::aspect::fp16});
  10415. stream->parallel_for(
  10416. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10417. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10418. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10419. [=](sycl::nd_item<3> item_ct1) {
  10420. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  10421. nb01, nb02, nb03, ne10, ne11, ne12,
  10422. nb10, nb11, nb12, nb13, item_ct1);
  10423. });
  10424. }
  10425. }
  10426. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  10427. const int ne00, const int ne01,
  10428. const int ne02, const int nb00,
  10429. const int nb01, const int nb02,
  10430. const int nb03, const int ne10,
  10431. const int ne11, const int ne12,
  10432. const int nb10, const int nb11,
  10433. const int nb12, const int nb13,
  10434. dpct::queue_ptr stream) {
  10435. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10436. {
  10437. dpct::has_capability_or_fail(stream->get_device(),
  10438. {sycl::aspect::fp16});
  10439. stream->parallel_for(
  10440. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10441. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10442. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10443. [=](sycl::nd_item<3> item_ct1) {
  10444. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10445. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10446. item_ct1);
  10447. });
  10448. }
  10449. }
  10450. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  10451. const int ne00, const int ne01,
  10452. const int ne02, const int nb00,
  10453. const int nb01, const int nb02,
  10454. const int nb03, const int ne10,
  10455. const int ne11, const int ne12,
  10456. const int nb10, const int nb11,
  10457. const int nb12, const int nb13,
  10458. dpct::queue_ptr stream) {
  10459. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10460. {
  10461. dpct::has_capability_or_fail(stream->get_device(),
  10462. {sycl::aspect::fp16});
  10463. stream->parallel_for(
  10464. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10465. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10466. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10467. [=](sycl::nd_item<3> item_ct1) {
  10468. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10469. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10470. item_ct1);
  10471. });
  10472. }
  10473. }
  10474. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  10475. const int ne00, const int ne01,
  10476. const int ne02, const int nb00,
  10477. const int nb01, const int nb02,
  10478. const int nb03, const int ne10,
  10479. const int ne11, const int ne12,
  10480. const int nb10, const int nb11,
  10481. const int nb12, const int nb13,
  10482. dpct::queue_ptr stream) {
  10483. GGML_ASSERT(ne % QK8_0 == 0);
  10484. const int num_blocks = ne / QK8_0;
  10485. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10486. sycl::range<3>(1, 1, 1)),
  10487. [=](sycl::nd_item<3> item_ct1) {
  10488. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  10489. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10490. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10491. item_ct1);
  10492. });
  10493. }
  10494. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  10495. const int ne00, const int ne01,
  10496. const int ne02, const int nb00,
  10497. const int nb01, const int nb02,
  10498. const int nb03, const int ne10,
  10499. const int ne11, const int ne12,
  10500. const int nb10, const int nb11,
  10501. const int nb12, const int nb13,
  10502. dpct::queue_ptr stream) {
  10503. GGML_ASSERT(ne % QK4_0 == 0);
  10504. const int num_blocks = ne / QK4_0;
  10505. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10506. sycl::range<3>(1, 1, 1)),
  10507. [=](sycl::nd_item<3> item_ct1) {
  10508. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  10509. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10510. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10511. item_ct1);
  10512. });
  10513. }
  10514. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  10515. const int ne00, const int ne01,
  10516. const int ne02, const int nb00,
  10517. const int nb01, const int nb02,
  10518. const int nb03, const int ne10,
  10519. const int ne11, const int ne12,
  10520. const int nb10, const int nb11,
  10521. const int nb12, const int nb13,
  10522. dpct::queue_ptr stream) {
  10523. GGML_ASSERT(ne % QK4_1 == 0);
  10524. const int num_blocks = ne / QK4_1;
  10525. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10526. sycl::range<3>(1, 1, 1)),
  10527. [=](sycl::nd_item<3> item_ct1) {
  10528. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  10529. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10530. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10531. item_ct1);
  10532. });
  10533. }
  10534. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  10535. const int ne00, const int ne01,
  10536. const int ne02, const int nb00,
  10537. const int nb01, const int nb02,
  10538. const int nb03, const int ne10,
  10539. const int ne11, const int ne12,
  10540. const int nb10, const int nb11,
  10541. const int nb12, const int nb13,
  10542. dpct::queue_ptr stream) {
  10543. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10544. {
  10545. dpct::has_capability_or_fail(stream->get_device(),
  10546. {sycl::aspect::fp16});
  10547. stream->parallel_for(
  10548. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10549. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10550. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10551. [=](sycl::nd_item<3> item_ct1) {
  10552. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10553. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10554. item_ct1);
  10555. });
  10556. }
  10557. }
  10558. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  10559. const int ne00, const int ne01,
  10560. const int ne02, const int nb00,
  10561. const int nb01, const int nb02,
  10562. const int nb03, const int ne10,
  10563. const int ne11, const int ne12,
  10564. const int nb10, const int nb11,
  10565. const int nb12, const int nb13,
  10566. dpct::queue_ptr stream) {
  10567. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10568. {
  10569. // dpct::has_capability_or_fail(stream->get_device(),
  10570. // {sycl::aspect::fp16});
  10571. stream->parallel_for(
  10572. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10573. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10574. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10575. [=](sycl::nd_item<3> item_ct1) {
  10576. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10577. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10578. item_ct1);
  10579. });
  10580. }
  10581. }
  10582. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  10583. const int ne00, const int ne01,
  10584. const int ne02, const int nb00,
  10585. const int nb01, const int nb02,
  10586. const int nb03, const int ne10,
  10587. const int ne11, const int ne12,
  10588. const int nb10, const int nb11,
  10589. const int nb12, const int nb13,
  10590. dpct::queue_ptr stream) {
  10591. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10592. {
  10593. // dpct::has_capability_or_fail(stream->get_device(),
  10594. // {sycl::aspect::fp16});
  10595. stream->parallel_for(
  10596. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10597. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10598. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10599. [=](sycl::nd_item<3> item_ct1) {
  10600. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10601. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10602. item_ct1);
  10603. });
  10604. }
  10605. }
  10606. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  10607. const int k, dpct::queue_ptr stream) {
  10608. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  10609. stream->parallel_for(
  10610. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10611. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  10612. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  10613. [=](sycl::nd_item<3> item_ct1) {
  10614. scale_f32(x, dst, scale, k, item_ct1);
  10615. });
  10616. }
  10617. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  10618. const float max, const int k,
  10619. dpct::queue_ptr stream) {
  10620. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  10621. stream->parallel_for(
  10622. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10623. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  10624. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  10625. [=](sycl::nd_item<3> item_ct1) {
  10626. clamp_f32(x, dst, min, max, k, item_ct1);
  10627. });
  10628. }
  10629. template <typename T>
  10630. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  10631. const int32_t *pos, float freq_scale, int p_delta_rows,
  10632. float freq_base, float ext_factor, float attn_factor,
  10633. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  10634. GGML_ASSERT(ncols % 2 == 0);
  10635. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  10636. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  10637. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  10638. if (pos == nullptr) {
  10639. /*
  10640. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  10641. the limit. To get the device limit, query
  10642. info::device::max_work_group_size. Adjust the work-group size if needed.
  10643. */
  10644. dpct::has_capability_or_fail(stream->get_device(),
  10645. {sycl::aspect::fp16});
  10646. stream->parallel_for(
  10647. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10648. [=](sycl::nd_item<3> item_ct1) {
  10649. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  10650. freq_base, ext_factor, attn_factor, corr_dims,
  10651. item_ct1);
  10652. });
  10653. } else {
  10654. /*
  10655. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  10656. the limit. To get the device limit, query
  10657. info::device::max_work_group_size. Adjust the work-group size if needed.
  10658. */
  10659. dpct::has_capability_or_fail(stream->get_device(),
  10660. {sycl::aspect::fp16});
  10661. stream->parallel_for(
  10662. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10663. [=](sycl::nd_item<3> item_ct1) {
  10664. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  10665. freq_base, ext_factor, attn_factor, corr_dims,
  10666. item_ct1);
  10667. });
  10668. }
  10669. }
  10670. template <typename T>
  10671. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  10672. const int32_t *pos, float freq_scale,
  10673. int p_delta_rows, float freq_base, float ext_factor,
  10674. float attn_factor, rope_corr_dims corr_dims,
  10675. dpct::queue_ptr stream) {
  10676. GGML_ASSERT(ncols % 2 == 0);
  10677. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  10678. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  10679. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  10680. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  10681. const float inv_ndims = -1.0f / n_dims;
  10682. if (pos == nullptr) {
  10683. /*
  10684. DPCT1049:42: The work-group size passed to the SYCL kernel may exceed
  10685. the limit. To get the device limit, query
  10686. info::device::max_work_group_size. Adjust the work-group size if needed.
  10687. */
  10688. dpct::has_capability_or_fail(stream->get_device(),
  10689. {sycl::aspect::fp16});
  10690. stream->parallel_for(
  10691. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10692. [=](sycl::nd_item<3> item_ct1) {
  10693. rope_neox<T, false>(x, dst, ncols, n_dims, pos, freq_scale,
  10694. p_delta_rows, ext_factor, attn_factor,
  10695. corr_dims, theta_scale, inv_ndims,
  10696. item_ct1);
  10697. });
  10698. } else {
  10699. /*
  10700. DPCT1049:43: The work-group size passed to the SYCL kernel may exceed
  10701. the limit. To get the device limit, query
  10702. info::device::max_work_group_size. Adjust the work-group size if needed.
  10703. */
  10704. dpct::has_capability_or_fail(stream->get_device(),
  10705. {sycl::aspect::fp16});
  10706. stream->parallel_for(
  10707. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10708. [=](sycl::nd_item<3> item_ct1) {
  10709. rope_neox<T, true>(x, dst, ncols, n_dims, pos, freq_scale,
  10710. p_delta_rows, ext_factor, attn_factor,
  10711. corr_dims, theta_scale, inv_ndims, item_ct1);
  10712. });
  10713. }
  10714. }
  10715. static void rope_glm_f32_sycl(const float *x, float *dst, int ncols, int nrows,
  10716. const int32_t *pos, float freq_scale,
  10717. int p_delta_rows, float freq_base, int n_ctx,
  10718. dpct::queue_ptr stream) {
  10719. GGML_ASSERT(ncols % 4 == 0);
  10720. const sycl::range<3> block_dims(1, 1, SYCL_ROPE_BLOCK_SIZE / 4);
  10721. const int num_blocks_x = (ncols + SYCL_ROPE_BLOCK_SIZE - 1) / SYCL_ROPE_BLOCK_SIZE;
  10722. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  10723. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10724. [=](sycl::nd_item<3> item_ct1) {
  10725. rope_glm_f32(x, dst, ncols, pos, freq_scale,
  10726. p_delta_rows, freq_base, n_ctx,
  10727. item_ct1);
  10728. });
  10729. }
  10730. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  10731. const int nrows, dpct::queue_ptr stream) {
  10732. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10733. const sycl::range<3> block_nums(1, nrows, 1);
  10734. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10735. [=](sycl::nd_item<3> item_ct1)
  10736. [[intel::reqd_sub_group_size(32)]] {
  10737. k_sum_rows_f32(x, dst, ncols, item_ct1);
  10738. });
  10739. }
  10740. static int next_power_of_2(int x) {
  10741. int n = 1;
  10742. while (n < x) {
  10743. n *= 2;
  10744. }
  10745. return n;
  10746. }
  10747. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  10748. const int nrows, ggml_sort_order order,
  10749. dpct::queue_ptr stream) {
  10750. // bitonic sort requires ncols to be power of 2
  10751. const int ncols_pad = next_power_of_2(ncols);
  10752. const sycl::range<3> block_dims(1, 1, ncols_pad);
  10753. const sycl::range<3> block_nums(1, nrows, 1);
  10754. const size_t shared_mem = ncols_pad * sizeof(int);
  10755. // GGML_ASSERT(shared_mem <= ggml_cuda_info().devices[ggml_cuda_get_device()].smpb);
  10756. if (order == GGML_SORT_ORDER_ASC) {
  10757. stream->submit([&](sycl::handler &cgh) {
  10758. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  10759. sycl::range<1>(shared_mem), cgh);
  10760. cgh.parallel_for(
  10761. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10762. [=](sycl::nd_item<3> item_ct1) {
  10763. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(
  10764. x, dst, ncols, ncols_pad, item_ct1,
  10765. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  10766. .get());
  10767. });
  10768. });
  10769. } else if (order == GGML_SORT_ORDER_DESC) {
  10770. stream->submit([&](sycl::handler &cgh) {
  10771. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  10772. sycl::range<1>(shared_mem), cgh);
  10773. cgh.parallel_for(
  10774. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10775. [=](sycl::nd_item<3> item_ct1) {
  10776. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(
  10777. x, dst, ncols, ncols_pad, item_ct1,
  10778. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  10779. .get());
  10780. });
  10781. });
  10782. } else {
  10783. GGML_ASSERT(false);
  10784. }
  10785. }
  10786. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  10787. const int ncols_x, const int nrows_x,
  10788. const int rows_per_channel, const int n_past,
  10789. dpct::queue_ptr stream) {
  10790. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  10791. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  10792. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  10793. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10794. [=](sycl::nd_item<3> item_ct1) {
  10795. diag_mask_inf_f32(x, dst, ncols_x,
  10796. rows_per_channel, n_past,
  10797. item_ct1);
  10798. });
  10799. }
  10800. template <bool vals_smem, int ncols_template, int block_size_template>
  10801. static void soft_max_f32_submitter(const float * x, const float * mask, float * dst, const int ncols_par,
  10802. const int nrows_y, const float scale, const float max_bias, const float m0,
  10803. const float m1, uint32_t n_head_log2, sycl::range<3> block_nums, sycl::range<3> block_dims,
  10804. const size_t n_local_scratch, dpct::queue_ptr stream) {
  10805. stream->submit([&](sycl::handler &cgh) {
  10806. sycl::local_accessor<float, 1> local_buf_acc(n_local_scratch, cgh);
  10807. cgh.parallel_for(
  10808. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10809. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10810. soft_max_f32<vals_smem, ncols_template, block_size_template>(x, mask, dst, ncols_par,
  10811. nrows_y, scale, max_bias, m0,
  10812. m1, n_head_log2, item_ct1,
  10813. local_buf_acc.get_pointer());
  10814. });
  10815. });
  10816. }
  10817. static void soft_max_f32_sycl(const float * x, const float * mask,
  10818. float * dst, const int ncols_x, const int nrows_x,
  10819. const int nrows_y, const float scale, const float max_bias,
  10820. dpct::queue_ptr stream) {
  10821. int nth = WARP_SIZE;
  10822. int max_block_size = g_work_group_size;
  10823. while (nth < ncols_x && nth < max_block_size) nth *= 2;
  10824. if (nth>max_block_size) nth = max_block_size;
  10825. const sycl::range<3> block_dims(1, 1, nth);
  10826. const sycl::range<3> block_nums(1, 1, nrows_x);
  10827. const size_t n_local_scratch = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE);
  10828. const uint32_t n_head_kv = nrows_x/nrows_y;
  10829. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  10830. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  10831. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  10832. const size_t local_mem_size = stream->get_device().get_info<sycl::info::device::local_mem_size>();
  10833. if (n_local_scratch*sizeof(float) < local_mem_size) {
  10834. if (ncols_x > max_block_size) {
  10835. soft_max_f32_submitter<true, 0, 0>(x, mask, dst, ncols_x, nrows_y, scale,
  10836. max_bias, m0, m1, n_head_log2, block_nums,
  10837. block_dims, n_local_scratch, stream);
  10838. return;
  10839. }
  10840. switch (ncols_x) {
  10841. case 32:
  10842. soft_max_f32_submitter<true, 32, 32>(x, mask, dst, ncols_x, nrows_y, scale,
  10843. max_bias, m0, m1, n_head_log2, block_nums,
  10844. block_dims, n_local_scratch, stream);
  10845. break;
  10846. case 64:
  10847. soft_max_f32_submitter<true, 64, 64>(x, mask, dst, ncols_x, nrows_y, scale,
  10848. max_bias, m0, m1, n_head_log2, block_nums,
  10849. block_dims, n_local_scratch, stream);
  10850. break;
  10851. case 128:
  10852. soft_max_f32_submitter<true, 128, 128>(x, mask, dst, ncols_x, nrows_y, scale,
  10853. max_bias, m0, m1, n_head_log2, block_nums,
  10854. block_dims, n_local_scratch, stream);
  10855. break;
  10856. case 256:
  10857. soft_max_f32_submitter<true, 256, 256>(x, mask, dst, ncols_x, nrows_y, scale,
  10858. max_bias, m0, m1, n_head_log2, block_nums,
  10859. block_dims, n_local_scratch, stream);
  10860. break;
  10861. case 512:
  10862. soft_max_f32_submitter<true, 512, 512>(x, mask, dst, ncols_x, nrows_y, scale,
  10863. max_bias, m0, m1, n_head_log2, block_nums,
  10864. block_dims, n_local_scratch, stream);
  10865. break;
  10866. case 1024:
  10867. soft_max_f32_submitter<true, 1024, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  10868. max_bias, m0, m1, n_head_log2, block_nums,
  10869. block_dims, n_local_scratch, stream);
  10870. break;
  10871. case 2048:
  10872. soft_max_f32_submitter<true, 2048, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  10873. max_bias, m0, m1, n_head_log2, block_nums,
  10874. block_dims, n_local_scratch, stream);
  10875. break;
  10876. case 4096:
  10877. soft_max_f32_submitter<true, 4096, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  10878. max_bias, m0, m1, n_head_log2, block_nums,
  10879. block_dims, n_local_scratch, stream);
  10880. break;
  10881. default:
  10882. soft_max_f32_submitter<true, 0, 0>(x, mask, dst, ncols_x, nrows_y, scale,
  10883. max_bias, m0, m1, n_head_log2, block_nums,
  10884. block_dims, n_local_scratch, stream);
  10885. break;
  10886. }
  10887. } else {
  10888. soft_max_f32_submitter<false, 0, 0>(x, mask, dst, ncols_x, nrows_y, scale,
  10889. max_bias, m0, m1, n_head_log2, block_nums,
  10890. block_dims, WARP_SIZE, stream);
  10891. }
  10892. }
  10893. template <typename T>
  10894. static void im2col_sycl(const float *x, T *dst, int IW, int IH,
  10895. int OW, int OH, int KW, int KH, int IC,
  10896. int offset_delta, int s0, int s1, int p0,
  10897. int p1, int d0, int d1,
  10898. dpct::queue_ptr stream) {
  10899. const int parallel_elements = OW * KW * KH;
  10900. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  10901. sycl::range<3> block_nums(IC, OH, num_blocks);
  10902. {
  10903. dpct::has_capability_or_fail(stream->get_device(),
  10904. {sycl::aspect::fp16});
  10905. stream->parallel_for(
  10906. sycl::nd_range<3>(block_nums *
  10907. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  10908. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  10909. [=](sycl::nd_item<3> item_ct1) {
  10910. im2col_kernel(x, dst, offset_delta, IW, IH, OW, KW, KH,
  10911. parallel_elements, (IC * KH * KW), s0, s1, p0,
  10912. p1, d0, d1, item_ct1);
  10913. });
  10914. }
  10915. }
  10916. // buffer pool for sycl
  10917. #define MAX_SYCL_BUFFERS 256
  10918. struct scoped_spin_lock {
  10919. std::atomic_flag& lock;
  10920. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  10921. while (lock.test_and_set(std::memory_order_acquire)) {
  10922. ; // spin
  10923. }
  10924. }
  10925. ~scoped_spin_lock() {
  10926. lock.clear(std::memory_order_release);
  10927. }
  10928. scoped_spin_lock(const scoped_spin_lock&) = delete;
  10929. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  10930. };
  10931. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  10932. // #define DEBUG_SYCL_MALLOC
  10933. struct sycl_buffer {
  10934. void * ptr = nullptr;
  10935. size_t size = 0;
  10936. };
  10937. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  10938. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  10939. static void *ggml_sycl_pool_malloc_leg(int device_index, size_t size, size_t *actual_size) try {
  10940. scoped_spin_lock lock(g_sycl_pool_lock);
  10941. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg device_index %d size=%lu\n", device_index, size);
  10942. #ifdef DEBUG_SYCL_MALLOC
  10943. int nnz = 0;
  10944. size_t max_size = 0;
  10945. #endif
  10946. size_t best_diff = 1ull << 36;
  10947. int ibest = -1;
  10948. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  10949. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  10950. if (b.ptr != nullptr) {
  10951. #ifdef DEBUG_SYCL_MALLOC
  10952. ++nnz;
  10953. if (b.size > max_size) max_size = b.size;
  10954. #endif
  10955. if (b.size >= size) {
  10956. size_t diff = b.size - size;
  10957. if (diff < best_diff) {
  10958. best_diff = diff;
  10959. ibest = i;
  10960. if (!best_diff) {
  10961. void * ptr = b.ptr;
  10962. *actual_size = b.size;
  10963. b.ptr = nullptr;
  10964. b.size = 0;
  10965. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p and rm in pool\n", ptr);
  10966. return ptr;
  10967. }
  10968. }
  10969. }
  10970. }
  10971. }
  10972. if (ibest >= 0) {
  10973. sycl_buffer& b = g_sycl_buffer_pool[device_index][ibest];
  10974. void * ptr = b.ptr;
  10975. *actual_size = b.size;
  10976. b.ptr = nullptr;
  10977. b.size = 0;
  10978. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p and rm in pool\n", ptr);
  10979. return ptr;
  10980. }
  10981. void * ptr;
  10982. size_t look_ahead_size = (size_t) (1.05 * size);
  10983. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  10984. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  10985. SYCL_CHECK(
  10986. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  10987. look_ahead_size, *stream)));
  10988. *actual_size = look_ahead_size;
  10989. g_sycl_pool_size[device_index] += look_ahead_size;
  10990. #ifdef DEBUG_SYCL_MALLOC
  10991. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  10992. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  10993. #endif
  10994. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  10995. return ptr;
  10996. }
  10997. catch (sycl::exception const &exc) {
  10998. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10999. << ", line:" << __LINE__ << std::endl;
  11000. std::exit(1);
  11001. }
  11002. static void ggml_sycl_pool_free_leg(int device_index, void *ptr, size_t size) try {
  11003. scoped_spin_lock lock(g_sycl_pool_lock);
  11004. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  11005. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  11006. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  11007. if (b.ptr == nullptr) {
  11008. b.ptr = ptr;
  11009. b.size = size;
  11010. return;
  11011. }
  11012. }
  11013. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  11014. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  11015. g_sycl_pool_size[device_index] -= size;
  11016. }
  11017. catch (sycl::exception const &exc) {
  11018. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11019. << ", line:" << __LINE__ << std::endl;
  11020. std::exit(1);
  11021. }
  11022. // pool with virtual memory
  11023. /*
  11024. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  11025. */
  11026. // static std::vector<CUmemGenericAllocationHandle>
  11027. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  11028. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  11029. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  11030. static void *ggml_sycl_pool_malloc_vmm(int device_index, size_t size, size_t *actual_size) try {
  11031. GGML_UNUSED(device_index);
  11032. GGML_UNUSED(size);
  11033. GGML_UNUSED(actual_size);
  11034. return NULL;
  11035. }
  11036. catch (sycl::exception const &exc) {
  11037. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11038. << ", line:" << __LINE__ << std::endl;
  11039. std::exit(1);
  11040. }
  11041. static void ggml_sycl_pool_free_vmm(int device_index, void *ptr, size_t size) try {
  11042. scoped_spin_lock lock(g_sycl_pool_lock);
  11043. #ifdef DEBUG_SYCL_MALLOC
  11044. printf("sycl pool[%d]: freed %llu bytes at %llx\n", device_index, (unsigned long long) size, ptr);
  11045. #endif
  11046. g_sycl_pool_used[device_index] -= size;
  11047. // all deallocations must be in reverse order of the allocations
  11048. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[device_index] + g_sycl_pool_used[device_index]));
  11049. }
  11050. catch (sycl::exception const &exc) {
  11051. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11052. << ", line:" << __LINE__ << std::endl;
  11053. std::exit(1);
  11054. }
  11055. static void *ggml_sycl_pool_malloc(int device_index, size_t size, size_t *actual_size) try {
  11056. if (g_device_caps[device_index].vmm) {
  11057. return ggml_sycl_pool_malloc_vmm(device_index, size, actual_size);
  11058. } else {
  11059. return ggml_sycl_pool_malloc_leg(device_index, size, actual_size);
  11060. }
  11061. }
  11062. catch (sycl::exception const &exc) {
  11063. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11064. << ", line:" << __LINE__ << std::endl;
  11065. std::exit(1);
  11066. }
  11067. static void ggml_sycl_pool_free(int device_index, void *ptr, size_t size) try {
  11068. if (g_device_caps[device_index].vmm) {
  11069. ggml_sycl_pool_free_vmm(device_index, ptr, size);
  11070. } else {
  11071. ggml_sycl_pool_free_leg(device_index, ptr, size);
  11072. }
  11073. }
  11074. catch (sycl::exception const &exc) {
  11075. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11076. << ", line:" << __LINE__ << std::endl;
  11077. std::exit(1);
  11078. }
  11079. template<typename T>
  11080. struct sycl_pool_alloc {
  11081. int device_index = -1;
  11082. int device_id = -1;
  11083. T * ptr = nullptr;
  11084. size_t actual_size = 0;
  11085. // size is in number of elements
  11086. T * alloc(size_t size) {
  11087. GGML_ASSERT(ptr == nullptr);
  11088. device_id = get_current_device_id();
  11089. device_index = g_sycl_gpu_mgr->get_index(device_id);
  11090. ptr = (T *) ggml_sycl_pool_malloc(device_index, size * sizeof(T), &this->actual_size);
  11091. // GGML_SYCL_DEBUG("sycl_pool_alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  11092. return ptr;
  11093. }
  11094. sycl_pool_alloc(size_t size) {
  11095. alloc(size);
  11096. }
  11097. ~sycl_pool_alloc() {
  11098. if (ptr != nullptr) {
  11099. ggml_sycl_pool_free(device_index, ptr, actual_size);
  11100. }
  11101. }
  11102. T * get() {
  11103. return ptr;
  11104. }
  11105. sycl_pool_alloc() = default;
  11106. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  11107. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  11108. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  11109. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  11110. };
  11111. static bool g_sycl_loaded = false;
  11112. bool ggml_sycl_loaded(void) {
  11113. return g_sycl_loaded;
  11114. }
  11115. void print_device_detail(int id, sycl::device &device, std::string device_type) {
  11116. dpct::device_info prop;
  11117. SYCL_CHECK(CHECK_TRY_ERROR(
  11118. dpct::get_device_info(prop, device)));
  11119. std::string version;
  11120. version += std::to_string(prop.get_major_version());
  11121. version += ".";
  11122. version += std::to_string(prop.get_minor_version());
  11123. device_type = std::regex_replace(device_type, std::regex("ext_oneapi_"), "");
  11124. std::string name = std::string(prop.get_name());
  11125. name = std::regex_replace(name, std::regex("\\(R\\)"), "");
  11126. name = std::regex_replace(name, std::regex("\\(TM\\)"), "");
  11127. auto global_mem_size = prop.get_global_mem_size()/1000000;
  11128. fprintf(stderr, "|%2d|%19s|%39s|%7s|%7d|%8d|%5d|%6luM|%21s|\n", id, device_type.c_str(),
  11129. name.c_str(), version.c_str(), prop.get_max_compute_units(),
  11130. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  11131. global_mem_size, device.get_info<sycl::info::device::driver_version>().c_str());
  11132. }
  11133. void ggml_backend_sycl_print_sycl_devices() {
  11134. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_print_sycl_devices\n");
  11135. int device_count = dpct::dev_mgr::instance().device_count();
  11136. std::map<std::string, size_t> DeviceNums;
  11137. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  11138. fprintf(stderr, "| | | | |Max | |Max |Global | |\n");
  11139. fprintf(stderr, "| | | | |compute|Max work|sub |mem | |\n");
  11140. fprintf(stderr, "|ID| Device Type| Name|Version|units |group |group|size | Driver version|\n");
  11141. fprintf(stderr, "|--|-------------------|---------------------------------------|-------|-------|--------|-----|-------|---------------------|\n");
  11142. for (int id = 0; id < device_count; ++id) {
  11143. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  11144. sycl::backend backend = device.get_backend();
  11145. std::string backend_type = get_device_backend_and_type(device);
  11146. int type_id=DeviceNums[backend_type]++;
  11147. std::stringstream device_type;
  11148. device_type << "[" << backend_type << ":" << std::to_string(type_id) << "]";
  11149. print_device_detail(id, device, device_type.str());
  11150. }
  11151. }
  11152. void print_gpu_device_list() {
  11153. GGML_ASSERT(g_sycl_gpu_mgr);
  11154. char* hint=NULL;
  11155. if (g_ggml_sycl_backend_gpu_mode == SYCL_SINGLE_GPU_MODE) {
  11156. hint = "use %d SYCL GPUs: [%s] with Max compute units:%d\n";
  11157. } else {
  11158. hint = "detect %d SYCL GPUs: [%s] with top Max compute units:%d\n";
  11159. }
  11160. fprintf(stderr, hint,
  11161. g_sycl_gpu_mgr->get_gpu_count(),
  11162. g_sycl_gpu_mgr->gpus_list.c_str(),
  11163. g_sycl_gpu_mgr->max_compute_units);
  11164. }
  11165. int get_sycl_env(const char *env_name, int default_val) {
  11166. char *user_device_string = getenv(env_name);
  11167. int user_number = default_val;
  11168. unsigned n;
  11169. if (user_device_string != NULL &&
  11170. sscanf(user_device_string, " %u", &n) == 1) {
  11171. user_number = (int)n;
  11172. } else {
  11173. user_number = default_val;
  11174. }
  11175. return user_number;
  11176. }
  11177. int get_work_group_size(int user_device_id) {
  11178. dpct::device_info prop;
  11179. dpct::get_device_info(prop,
  11180. dpct::dev_mgr::instance().get_device(user_device_id));
  11181. return prop.get_max_work_group_size();
  11182. }
  11183. static void ggml_init_sycl() try {
  11184. static bool initialized = false;
  11185. if (!initialized) {
  11186. fprintf(stderr, "[SYCL] call ggml_init_sycl\n");
  11187. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  11188. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  11189. #if defined(GGML_SYCL_F16)
  11190. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  11191. #else
  11192. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  11193. #endif
  11194. /* NOT REMOVE, keep it for next optimize for XMX.
  11195. #if defined(SYCL_USE_XMX)
  11196. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  11197. #else
  11198. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  11199. #endif
  11200. */
  11201. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  11202. dpct::dev_mgr::instance().device_count()) != 0) {
  11203. initialized = true;
  11204. g_sycl_loaded = false;
  11205. return;
  11206. }
  11207. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  11208. ggml_backend_sycl_print_sycl_devices();
  11209. initialized = true;
  11210. g_sycl_loaded = true;
  11211. }
  11212. }
  11213. catch (sycl::exception const &exc) {
  11214. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11215. << ", line:" << __LINE__ << std::endl;
  11216. std::exit(1);
  11217. }
  11218. void ggml_init_by_gpus(int device_count) try {
  11219. g_device_count = device_count;
  11220. g_work_group_size = g_sycl_gpu_mgr->work_group_size;
  11221. int64_t total_vram = 0;
  11222. print_gpu_device_list();
  11223. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  11224. g_device_caps[id].vmm = 0;
  11225. g_device_caps[id].device_id = -1;
  11226. g_device_caps[id].cc = 0;
  11227. g_tensor_split[id] = 0;
  11228. g_default_tensor_split[id] = 0;
  11229. }
  11230. for (int i = 0; i < g_device_count; ++i) {
  11231. int device_id = g_sycl_gpu_mgr->gpus[i];
  11232. g_device_caps[i].vmm = 0;
  11233. dpct::device_info prop;
  11234. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  11235. prop, dpct::dev_mgr::instance().get_device(device_id))));
  11236. g_default_tensor_split[i] = total_vram;
  11237. total_vram += prop.get_global_mem_size();
  11238. g_device_caps[i].cc =
  11239. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  11240. }
  11241. for (int i = 0; i < g_device_count; ++i) {
  11242. g_default_tensor_split[i] /= total_vram;
  11243. }
  11244. for (int i = 0; i < g_device_count; ++i) {
  11245. SYCL_CHECK(ggml_sycl_set_device(i));
  11246. // create sycl streams
  11247. for (int is = 0; is < MAX_STREAMS; ++is) {
  11248. SYCL_CHECK(CHECK_TRY_ERROR(
  11249. g_syclStreams[i][is] =
  11250. dpct::get_current_device().create_queue(
  11251. g_sycl_gpu_mgr->get_co_ctx(), dpct::get_current_device())));
  11252. }
  11253. const dpct::queue_ptr stream = g_syclStreams[i][0];
  11254. // create sycl handle
  11255. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[i] = stream));
  11256. }
  11257. }
  11258. catch (sycl::exception const &exc) {
  11259. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11260. << ", line:" << __LINE__ << std::endl;
  11261. std::exit(1);
  11262. }
  11263. void *ggml_sycl_host_malloc(size_t size) try {
  11264. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  11265. return nullptr;
  11266. }
  11267. void * ptr = nullptr;
  11268. //allow to use dpct::get_in_order_queue() for host malloc
  11269. dpct::err0 err = CHECK_TRY_ERROR(
  11270. ptr = (void *)sycl::malloc_host(size, dpct::get_in_order_queue()));
  11271. if (err != 0) {
  11272. // clear the error
  11273. fprintf(
  11274. stderr,
  11275. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  11276. size / 1024.0 / 1024.0,
  11277. "syclGetErrorString is not supported");
  11278. return nullptr;
  11279. }
  11280. return ptr;
  11281. }
  11282. catch (sycl::exception const &exc) {
  11283. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11284. << ", line:" << __LINE__ << std::endl;
  11285. std::exit(1);
  11286. }
  11287. void ggml_sycl_host_free(void *ptr) try {
  11288. //allow to use dpct::get_in_order_queue() for host malloc
  11289. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  11290. }
  11291. catch (sycl::exception const &exc) {
  11292. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11293. << ", line:" << __LINE__ << std::endl;
  11294. std::exit(1);
  11295. }
  11296. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  11297. const struct ggml_tensor *src,
  11298. int64_t i3, int64_t i2,
  11299. int64_t i1_low, int64_t i1_high,
  11300. dpct::queue_ptr stream) try {
  11301. dpct::memcpy_direction kind;
  11302. char * src_ptr;
  11303. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  11304. kind = dpct::host_to_device;
  11305. src_ptr = (char *) src->data;
  11306. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  11307. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  11308. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  11309. kind = dpct::device_to_device;
  11310. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  11311. int id;
  11312. SYCL_CHECK(CHECK_TRY_ERROR(
  11313. id = get_current_device_id()));
  11314. // GGML_SYCL_DEBUG("current device index %d\n", id);
  11315. src_ptr = (char *) extra->data_device[id];
  11316. } else {
  11317. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  11318. GGML_ASSERT(false);
  11319. }
  11320. char * dst_ptr = (char *) dst;
  11321. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  11322. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  11323. const enum ggml_type type = src->type;
  11324. const int64_t ts = ggml_type_size(type);
  11325. const int64_t bs = ggml_blck_size(type);
  11326. int64_t i1_diff = i1_high - i1_low;
  11327. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  11328. if (nb0 == ts && nb1 == ts*ne0/bs) {
  11329. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  11330. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  11331. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  11332. kind, *stream));
  11333. } else if (nb0 == ts) {
  11334. return CHECK_TRY_ERROR(
  11335. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  11336. ts * ne0 / bs, i1_diff, kind, *stream));
  11337. } else {
  11338. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  11339. const void * rx = (const void *) ((const char *) x + i1*nb1);
  11340. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  11341. // pretend the row is a matrix with cols=1
  11342. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11343. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  11344. /*
  11345. DPCT1001:85: The statement could not be removed.
  11346. */
  11347. /*
  11348. DPCT1000:86: Error handling if-stmt was detected but could not be
  11349. rewritten.
  11350. */
  11351. if (r != 0) return r;
  11352. }
  11353. return 0;
  11354. }
  11355. }
  11356. catch (sycl::exception const &exc) {
  11357. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11358. << ", line:" << __LINE__ << std::endl;
  11359. std::exit(1);
  11360. }
  11361. static void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  11362. const ggml_tensor *src1, ggml_tensor *dst,
  11363. const float *src0_d, const float *src1_d,
  11364. float *dst_d, const dpct::queue_ptr &stream) {
  11365. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  11366. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11367. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  11368. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  11369. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  11370. const int32_t * src1_i32 = (const int32_t *) src1_d;
  11371. switch (src0->type) {
  11372. case GGML_TYPE_F16:
  11373. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  11374. src1_i32, dst_d, stream);
  11375. break;
  11376. case GGML_TYPE_F32:
  11377. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11378. break;
  11379. case GGML_TYPE_Q4_0:
  11380. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11381. break;
  11382. case GGML_TYPE_Q4_1:
  11383. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11384. break;
  11385. case GGML_TYPE_Q5_0:
  11386. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11387. break;
  11388. case GGML_TYPE_Q5_1:
  11389. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11390. break;
  11391. case GGML_TYPE_Q8_0:
  11392. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11393. break;
  11394. default:
  11395. // TODO: k-quants
  11396. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  11397. GGML_ASSERT(false);
  11398. break;
  11399. }
  11400. }
  11401. template <class op>
  11402. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  11403. const ggml_tensor *src1, ggml_tensor *dst,
  11404. const float *src0_dd, const float *src1_dd,
  11405. float *dst_dd,
  11406. const dpct::queue_ptr &main_stream) {
  11407. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  11408. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11409. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  11410. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  11411. (sycl::half *)dst_dd, main_stream);
  11412. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  11413. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  11414. main_stream);
  11415. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  11416. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  11417. main_stream);
  11418. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  11419. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  11420. main_stream);
  11421. } else {
  11422. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  11423. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  11424. GGML_ASSERT(false);
  11425. }
  11426. }
  11427. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  11428. const ggml_tensor *src1, ggml_tensor *dst,
  11429. const float *src0_d, const float *src1_d,
  11430. float *dst_d,
  11431. const dpct::queue_ptr &main_stream) {
  11432. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  11433. (void) src1;
  11434. (void) src1_d;
  11435. }
  11436. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  11437. ggml_tensor *dst, const float *src0_dd,
  11438. const float *src1_dd, float *dst_dd,
  11439. const dpct::queue_ptr &main_stream) {
  11440. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11441. }
  11442. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  11443. ggml_tensor *dst, const float *src0_dd,
  11444. const float *src1_dd, float *dst_dd,
  11445. const dpct::queue_ptr &main_stream) {
  11446. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11447. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11448. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11449. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  11450. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  11451. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  11452. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  11453. int offset = dst->op_params[3] / 4; // offset in bytes
  11454. 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);
  11455. (void) dst;
  11456. }
  11457. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  11458. ggml_tensor *dst, const float *src0_dd,
  11459. const float *src1_dd, float *dst_dd,
  11460. const dpct::queue_ptr &main_stream) {
  11461. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11462. }
  11463. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  11464. ggml_tensor *dst, const float *src0_dd,
  11465. const float *src1_dd, float *dst_dd,
  11466. const dpct::queue_ptr &main_stream) {
  11467. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11468. }
  11469. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  11470. ggml_tensor *dst, const float *src0_dd,
  11471. const float *src1_dd, float *dst_dd,
  11472. const dpct::queue_ptr &main_stream) {
  11473. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11474. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11475. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11476. (void) src1;
  11477. (void) dst;
  11478. (void) src1_dd;
  11479. }
  11480. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  11481. ggml_tensor *dst, const float *src0_dd,
  11482. const float *src1_dd, float *dst_dd,
  11483. const dpct::queue_ptr &main_stream) {
  11484. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11485. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11486. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11487. (void) src1;
  11488. (void) dst;
  11489. (void) src1_dd;
  11490. }
  11491. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  11492. const ggml_tensor *src1, ggml_tensor *dst,
  11493. const float *src0_dd, const float *src1_dd,
  11494. float *dst_dd,
  11495. const dpct::queue_ptr &main_stream) {
  11496. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11497. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11498. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11499. (void) src1;
  11500. (void) dst;
  11501. (void) src1_dd;
  11502. }
  11503. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  11504. ggml_tensor *dst, const float *src0_dd,
  11505. const float *src1_dd, float *dst_dd,
  11506. const dpct::queue_ptr &main_stream) {
  11507. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11508. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11509. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11510. (void) src1;
  11511. (void) dst;
  11512. (void) src1_dd;
  11513. }
  11514. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  11515. ggml_tensor *dst, const float *src0_dd,
  11516. const float *src1_dd, float *dst_dd,
  11517. const dpct::queue_ptr &main_stream) {
  11518. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11519. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11520. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11521. (void) src1;
  11522. (void) dst;
  11523. (void) src1_dd;
  11524. }
  11525. static void ggml_sycl_op_hardsigmoid(const ggml_tensor *src0,
  11526. const ggml_tensor *src1, ggml_tensor *dst,
  11527. const float *src0_dd, const float *src1_dd,
  11528. float *dst_dd,
  11529. const dpct::queue_ptr &main_stream) {
  11530. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11531. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11532. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11533. (void) src1;
  11534. (void) dst;
  11535. (void) src1_dd;
  11536. }
  11537. static void ggml_sycl_op_hardswish(const ggml_tensor *src0,
  11538. const ggml_tensor *src1, ggml_tensor *dst,
  11539. const float *src0_dd, const float *src1_dd,
  11540. float *dst_dd, const dpct::queue_ptr &main_stream) {
  11541. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11542. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11543. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11544. (void) src1;
  11545. (void) dst;
  11546. (void) src1_dd;
  11547. }
  11548. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  11549. const ggml_tensor *src1, ggml_tensor *dst,
  11550. const float *src0_dd, const float *src1_dd,
  11551. float *dst_dd,
  11552. const dpct::queue_ptr &main_stream) {
  11553. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11554. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11555. float negative_slope;
  11556. memcpy(&negative_slope, dst->op_params, sizeof(float));
  11557. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  11558. (void) src1;
  11559. (void) dst;
  11560. (void) src1_dd;
  11561. }
  11562. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  11563. ggml_tensor *dst, const float *src0_dd,
  11564. const float *src1_dd, float *dst_dd,
  11565. const dpct::queue_ptr &main_stream) {
  11566. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11567. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11568. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11569. (void) src1;
  11570. (void) dst;
  11571. (void) src1_dd;
  11572. }
  11573. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  11574. ggml_tensor *dst, const float *src0_dd,
  11575. const float *src1_dd, float *dst_dd,
  11576. const dpct::queue_ptr &main_stream) {
  11577. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11578. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11579. const int64_t ne00 = src0->ne[0];
  11580. const int64_t nrows = ggml_nrows(src0);
  11581. float eps;
  11582. memcpy(&eps, dst->op_params, sizeof(float));
  11583. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11584. (void) src1;
  11585. (void) dst;
  11586. (void) src1_dd;
  11587. }
  11588. inline void ggml_sycl_op_group_norm(const ggml_tensor *src0,
  11589. const ggml_tensor *src1, ggml_tensor *dst,
  11590. const float *src0_dd, const float *src1_dd,
  11591. float *dst_dd,
  11592. const dpct::queue_ptr &main_stream) {
  11593. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11594. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11595. int num_groups = dst->op_params[0];
  11596. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  11597. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  11598. (void) src1;
  11599. (void) dst;
  11600. (void) src1_dd;
  11601. }
  11602. inline void ggml_sycl_op_concat(const ggml_tensor *src0,
  11603. const ggml_tensor *src1, ggml_tensor *dst,
  11604. const float *src0_dd, const float *src1_dd,
  11605. float *dst_dd,
  11606. const dpct::queue_ptr &main_stream) {
  11607. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11608. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11609. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11610. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  11611. 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);
  11612. }
  11613. (void) src1;
  11614. (void) dst;
  11615. }
  11616. inline void ggml_sycl_op_upscale(const ggml_tensor *src0,
  11617. const ggml_tensor *src1, ggml_tensor *dst,
  11618. const float *src0_dd, const float *src1_dd,
  11619. float *dst_dd,
  11620. const dpct::queue_ptr &main_stream) {
  11621. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11622. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11623. const float sf0 = (float)dst->ne[0]/src0->ne[0];
  11624. const float sf1 = (float)dst->ne[1]/src0->ne[1];
  11625. const float sf2 = (float)dst->ne[2]/src0->ne[2];
  11626. const float sf3 = (float)dst->ne[3]/src0->ne[3];
  11627. upscale_f32_sycl(src0_dd, dst_dd, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
  11628. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], sf0, sf1, sf2, sf3,
  11629. main_stream);
  11630. (void) src1;
  11631. (void) dst;
  11632. (void) src1_dd;
  11633. }
  11634. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  11635. ggml_tensor *dst, const float *src0_dd,
  11636. const float *src1_dd, float *dst_dd,
  11637. const dpct::queue_ptr &main_stream) {
  11638. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11639. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11640. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  11641. pad_f32_sycl(src0_dd, dst_dd,
  11642. src0->ne[0], src0->ne[1], src0->ne[2],
  11643. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  11644. (void) src1;
  11645. (void) dst;
  11646. (void) src1_dd;
  11647. }
  11648. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  11649. const ggml_tensor *src1, ggml_tensor *dst,
  11650. const float *src0_dd, const float *src1_dd,
  11651. float *dst_dd,
  11652. const dpct::queue_ptr &main_stream) {
  11653. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11654. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11655. const int64_t ne00 = src0->ne[0];
  11656. const int64_t nrows = ggml_nrows(src0);
  11657. float eps;
  11658. memcpy(&eps, dst->op_params, sizeof(float));
  11659. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11660. (void) src1;
  11661. (void) dst;
  11662. (void) src1_dd;
  11663. }
  11664. inline void ggml_sycl_op_mul_mat_q(
  11665. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11666. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11667. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11668. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11669. const dpct::queue_ptr &stream) try {
  11670. const int64_t ne00 = src0->ne[0];
  11671. const int64_t ne10 = src1->ne[0];
  11672. GGML_ASSERT(ne10 % QK8_1 == 0);
  11673. const int64_t ne0 = dst->ne[0];
  11674. const int64_t row_diff = row_high - row_low;
  11675. int device_id;
  11676. SYCL_CHECK(
  11677. CHECK_TRY_ERROR(device_id = get_current_device_id()));
  11678. // the main device has a larger memory buffer to hold the results from all GPUs
  11679. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  11680. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && device_id == g_main_device ? ne0 : row_diff;
  11681. switch (src0->type) {
  11682. case GGML_TYPE_Q4_0:
  11683. ggml_mul_mat_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11684. break;
  11685. case GGML_TYPE_Q4_1:
  11686. ggml_mul_mat_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11687. break;
  11688. case GGML_TYPE_Q5_0:
  11689. ggml_mul_mat_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11690. break;
  11691. case GGML_TYPE_Q5_1:
  11692. ggml_mul_mat_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11693. break;
  11694. case GGML_TYPE_Q8_0:
  11695. ggml_mul_mat_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11696. break;
  11697. case GGML_TYPE_Q2_K:
  11698. ggml_mul_mat_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11699. break;
  11700. case GGML_TYPE_Q3_K:
  11701. ggml_mul_mat_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11702. break;
  11703. case GGML_TYPE_Q4_K:
  11704. ggml_mul_mat_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11705. break;
  11706. case GGML_TYPE_Q5_K:
  11707. ggml_mul_mat_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11708. break;
  11709. case GGML_TYPE_Q6_K:
  11710. ggml_mul_mat_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_ncols, src1_padded_row_size, nrows_dst, stream);
  11711. break;
  11712. default:
  11713. GGML_ASSERT(false);
  11714. break;
  11715. }
  11716. (void) src1;
  11717. (void) dst;
  11718. (void) src1_ddf_i;
  11719. }
  11720. catch (sycl::exception const &exc) {
  11721. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11722. << ", line:" << __LINE__ << std::endl;
  11723. std::exit(1);
  11724. }
  11725. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  11726. int64_t min_compute_capability = INT_MAX;
  11727. int64_t max_compute_capability = INT_MIN;
  11728. for (int i = 0; i < g_device_count; ++i) {
  11729. if (tensor_split[i] < (i + 1 < g_device_count ? tensor_split[i + 1] : 1.0f)) {
  11730. if (min_compute_capability > g_device_caps[i].cc) {
  11731. min_compute_capability = g_device_caps[i].cc;
  11732. }
  11733. if (max_compute_capability < g_device_caps[i].cc) {
  11734. max_compute_capability = g_device_caps[i].cc;
  11735. }
  11736. }
  11737. }
  11738. switch(type) {
  11739. case GGML_TYPE_Q4_0:
  11740. case GGML_TYPE_Q4_1:
  11741. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11742. case GGML_TYPE_Q5_0:
  11743. case GGML_TYPE_Q5_1:
  11744. case GGML_TYPE_Q8_0:
  11745. return 64;
  11746. case GGML_TYPE_F16:
  11747. case GGML_TYPE_F32:
  11748. return 1;
  11749. case GGML_TYPE_Q2_K:
  11750. case GGML_TYPE_Q3_K:
  11751. case GGML_TYPE_Q4_K:
  11752. case GGML_TYPE_Q5_K:
  11753. case GGML_TYPE_IQ2_XXS:
  11754. case GGML_TYPE_IQ2_XS:
  11755. case GGML_TYPE_IQ2_S:
  11756. case GGML_TYPE_IQ1_S:
  11757. case GGML_TYPE_IQ1_M:
  11758. case GGML_TYPE_IQ3_XXS:
  11759. case GGML_TYPE_IQ4_XS:
  11760. case GGML_TYPE_IQ4_NL:
  11761. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11762. case GGML_TYPE_IQ3_S:
  11763. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11764. case GGML_TYPE_Q6_K:
  11765. return 64;
  11766. default:
  11767. GGML_ASSERT(false);
  11768. }
  11769. }
  11770. inline void ggml_sycl_op_mul_mat_vec_q(
  11771. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11772. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11773. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11774. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11775. const dpct::queue_ptr &stream) {
  11776. const int64_t ne10 = src1->ne[0];
  11777. GGML_ASSERT(ne10 % QK8_1 == 0);
  11778. const int64_t ne00 = src0->ne[0];
  11779. const int64_t row_diff = row_high - row_low;
  11780. int id;
  11781. SYCL_CHECK(
  11782. CHECK_TRY_ERROR(id = get_current_device_id()));
  11783. // the main device has a larger memory buffer to hold the results from all GPUs
  11784. // nrows_dst == nrows of the matrix that the kernel writes into
  11785. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne00 : row_diff;
  11786. switch (src0->type) {
  11787. case GGML_TYPE_Q4_0:
  11788. mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11789. break;
  11790. case GGML_TYPE_Q4_1:
  11791. mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11792. break;
  11793. case GGML_TYPE_Q5_0:
  11794. mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11795. break;
  11796. case GGML_TYPE_Q5_1:
  11797. mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11798. break;
  11799. case GGML_TYPE_Q8_0:
  11800. mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11801. break;
  11802. case GGML_TYPE_Q2_K:
  11803. mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11804. break;
  11805. case GGML_TYPE_Q3_K:
  11806. mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11807. break;
  11808. case GGML_TYPE_Q4_K:
  11809. mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11810. break;
  11811. case GGML_TYPE_Q5_K:
  11812. mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11813. break;
  11814. case GGML_TYPE_Q6_K:
  11815. mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11816. break;
  11817. case GGML_TYPE_IQ1_S:
  11818. mul_mat_vec_iq1_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11819. break;
  11820. case GGML_TYPE_IQ1_M:
  11821. mul_mat_vec_iq1_m_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11822. break;
  11823. case GGML_TYPE_IQ2_XXS:
  11824. mul_mat_vec_iq2_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11825. break;
  11826. case GGML_TYPE_IQ2_XS:
  11827. mul_mat_vec_iq2_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11828. break;
  11829. case GGML_TYPE_IQ2_S:
  11830. mul_mat_vec_iq2_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11831. break;
  11832. case GGML_TYPE_IQ3_XXS:
  11833. mul_mat_vec_iq3_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11834. break;
  11835. case GGML_TYPE_IQ3_S:
  11836. mul_mat_vec_iq3_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11837. break;
  11838. case GGML_TYPE_IQ4_NL:
  11839. mul_mat_vec_iq4_nl_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11840. break;
  11841. case GGML_TYPE_IQ4_XS:
  11842. mul_mat_vec_iq4_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11843. break;
  11844. default:
  11845. GGML_ASSERT(false);
  11846. break;
  11847. }
  11848. (void) src1;
  11849. (void) dst;
  11850. (void) src1_ddf_i;
  11851. (void) src1_ncols;
  11852. (void) src1_padded_row_size;
  11853. }
  11854. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  11855. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11856. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11857. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11858. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11859. const dpct::queue_ptr &stream) {
  11860. const int64_t ne00 = src0->ne[0];
  11861. const int64_t row_diff = row_high - row_low;
  11862. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11863. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  11864. #ifdef GGML_SYCL_F16
  11865. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  11866. sycl::half *src1_dfloat = nullptr; // dfloat == half
  11867. bool src1_convert_f16 =
  11868. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  11869. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  11870. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  11871. if (src1_convert_f16) {
  11872. src1_dfloat = src1_dfloat_a.alloc(ne00);
  11873. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11874. GGML_ASSERT(to_fp16_sycl != nullptr);
  11875. to_fp16_sycl(src1_ddf_i, src1_dfloat, ne00, stream);
  11876. }
  11877. #else
  11878. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  11879. #endif // GGML_SYCL_F16
  11880. switch (src0->type) {
  11881. case GGML_TYPE_Q4_0:
  11882. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11883. break;
  11884. case GGML_TYPE_Q4_1:
  11885. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11886. break;
  11887. case GGML_TYPE_Q5_0:
  11888. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11889. break;
  11890. case GGML_TYPE_Q5_1:
  11891. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11892. break;
  11893. case GGML_TYPE_Q8_0:
  11894. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11895. break;
  11896. case GGML_TYPE_Q2_K:
  11897. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11898. break;
  11899. case GGML_TYPE_Q3_K:
  11900. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11901. break;
  11902. case GGML_TYPE_Q4_K:
  11903. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11904. break;
  11905. case GGML_TYPE_Q5_K:
  11906. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11907. break;
  11908. case GGML_TYPE_Q6_K:
  11909. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11910. break;
  11911. case GGML_TYPE_F16:
  11912. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11913. break;
  11914. default:
  11915. printf("ggml_sycl_op_dequantize_mul_mat_vec unsupported GGML_TYPE %d\n", src0->type);
  11916. GGML_ASSERT(false);
  11917. break;
  11918. }
  11919. (void) src1;
  11920. (void) dst;
  11921. (void) src1_ddq_i;
  11922. (void) src1_ncols;
  11923. (void) src1_padded_row_size;
  11924. }
  11925. inline void ggml_sycl_op_mul_mat_sycl(
  11926. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11927. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11928. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11929. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11930. const dpct::queue_ptr &stream) try {
  11931. GGML_ASSERT(src0_dd_i != nullptr);
  11932. GGML_ASSERT(src1_ddf_i != nullptr);
  11933. GGML_ASSERT(dst_dd_i != nullptr);
  11934. const int64_t ne00 = src0->ne[0];
  11935. const int64_t ne10 = src1->ne[0];
  11936. const int64_t ne0 = dst->ne[0];
  11937. const int64_t row_diff = row_high - row_low;
  11938. int id;
  11939. SYCL_CHECK(
  11940. CHECK_TRY_ERROR(id = get_current_device_id()));
  11941. // the main device has a larger memory buffer to hold the results from all GPUs
  11942. // ldc == nrows of the matrix that cuBLAS writes into
  11943. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  11944. #ifdef GGML_SYCL_F16
  11945. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  11946. #else
  11947. bool use_fp16 = false;
  11948. #endif
  11949. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  11950. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  11951. dst->op_params[0] == GGML_PREC_DEFAULT) {
  11952. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  11953. sycl_pool_alloc<sycl::half> src0_as_f16;
  11954. if (src0->type != GGML_TYPE_F16) {
  11955. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  11956. GGML_ASSERT(to_fp16_sycl != nullptr);
  11957. size_t ne = row_diff*ne00;
  11958. src0_as_f16.alloc(ne);
  11959. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  11960. }
  11961. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  11962. ? (const sycl::half *)src0_dd_i
  11963. : src0_as_f16.get();
  11964. sycl_pool_alloc<sycl::half> src1_as_f16;
  11965. if (src1->type != GGML_TYPE_F16) {
  11966. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11967. GGML_ASSERT(to_fp16_sycl != nullptr);
  11968. size_t ne = src1_ncols*ne10;
  11969. src1_as_f16.alloc(ne);
  11970. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  11971. }
  11972. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  11973. ? (const sycl::half *)src1->data + src1_padded_row_size
  11974. : src1_as_f16.get();
  11975. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  11976. const sycl::half alpha_f16 = 1.0f;
  11977. const sycl::half beta_f16 = 0.0f;
  11978. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  11979. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  11980. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  11981. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  11982. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  11983. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  11984. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  11985. dpct::library_data_t::real_half)));
  11986. g_sycl_handles[id]->wait();
  11987. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  11988. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  11989. }
  11990. else {
  11991. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  11992. sycl_pool_alloc<float> src0_ddq_as_f32;
  11993. sycl_pool_alloc<float> src1_ddq_as_f32;
  11994. if (src0->type != GGML_TYPE_F32) {
  11995. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  11996. GGML_ASSERT(to_fp32_sycl != nullptr);
  11997. src0_ddq_as_f32.alloc(row_diff*ne00);
  11998. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  11999. }
  12000. if (src1->type != GGML_TYPE_F32) {
  12001. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  12002. GGML_ASSERT(to_fp32_sycl != nullptr);
  12003. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  12004. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  12005. }
  12006. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  12007. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  12008. const float alpha = 1.0f;
  12009. const float beta = 0.0f;
  12010. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  12011. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  12012. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  12013. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12014. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  12015. src1_ddf1_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  12016. dst_dd_i, ldc)));
  12017. g_sycl_handles[id]->wait();
  12018. }
  12019. (void) dst;
  12020. (void) src1_ddq_i;
  12021. (void) src1_padded_row_size;
  12022. }
  12023. catch (sycl::exception const &exc) {
  12024. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12025. << ", line:" << __LINE__ << std::endl;
  12026. std::exit(1);
  12027. }
  12028. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  12029. ggml_tensor *dst, const float *src0_dd,
  12030. const float *src1_dd, float *dst_dd,
  12031. const dpct::queue_ptr &main_stream) {
  12032. #pragma message("TODO: implement phi3 frequency factors support")
  12033. #pragma message(" https://github.com/ggerganov/llama.cpp/pull/7225")
  12034. GGML_ASSERT(dst->src[2] == nullptr && "phi3 frequency factors not implemented yet");
  12035. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  12036. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  12037. GGML_ASSERT(src0->type == dst->type);
  12038. const int64_t ne00 = src0->ne[0];
  12039. const int64_t ne01 = src0->ne[1];
  12040. const int64_t ne2 = dst->ne[2];
  12041. const int64_t nrows = ggml_nrows(src0);
  12042. //const int n_past = ((int32_t *) dst->op_params)[0];
  12043. const int n_dims = ((int32_t *) dst->op_params)[1];
  12044. const int mode = ((int32_t *) dst->op_params)[2];
  12045. const int n_ctx = ((int32_t *) dst->op_params)[3];
  12046. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  12047. // RoPE alteration for extended context
  12048. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  12049. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  12050. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  12051. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  12052. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  12053. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  12054. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  12055. const int32_t * pos = nullptr;
  12056. if ((mode & 1) == 0) {
  12057. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  12058. GGML_ASSERT(src1->ne[0] == ne2);
  12059. pos = (const int32_t *) src1_dd;
  12060. }
  12061. const bool is_neox = mode & 2;
  12062. const bool is_glm = mode & 4;
  12063. rope_corr_dims corr_dims;
  12064. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  12065. // compute
  12066. if (is_glm) {
  12067. GGML_ASSERT(false);
  12068. rope_glm_f32_sycl(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  12069. } else if (is_neox) {
  12070. if (src0->type == GGML_TYPE_F32) {
  12071. rope_neox_sycl(
  12072. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12073. attn_factor, corr_dims, main_stream
  12074. );
  12075. } else if (src0->type == GGML_TYPE_F16) {
  12076. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  12077. ne00, n_dims, nrows, pos, freq_scale, ne01,
  12078. freq_base, ext_factor, attn_factor, corr_dims,
  12079. main_stream);
  12080. } else {
  12081. GGML_ASSERT(false);
  12082. }
  12083. } else {
  12084. if (src0->type == GGML_TYPE_F32) {
  12085. rope_sycl(
  12086. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12087. attn_factor, corr_dims, main_stream
  12088. );
  12089. } else if (src0->type == GGML_TYPE_F16) {
  12090. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  12091. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12092. attn_factor, corr_dims, main_stream);
  12093. } else {
  12094. GGML_ASSERT(false);
  12095. }
  12096. }
  12097. (void) src1;
  12098. (void) dst;
  12099. (void) src1_dd;
  12100. }
  12101. static void ggml_sycl_op_pool2d(const ggml_tensor *src0,
  12102. const ggml_tensor *src1, ggml_tensor *dst,
  12103. const float *src0_dd, const float *src1_dd,
  12104. float *dst_dd, const dpct::queue_ptr &main_stream) {
  12105. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12106. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12107. const int32_t * opts = (const int32_t *)dst->op_params;
  12108. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  12109. const int k0 = opts[1];
  12110. const int k1 = opts[2];
  12111. const int s0 = opts[3];
  12112. const int s1 = opts[4];
  12113. const int p0 = opts[5];
  12114. const int p1 = opts[6];
  12115. const int64_t IH = src0->ne[1];
  12116. const int64_t IW = src0->ne[0];
  12117. const int64_t N = dst->ne[3];
  12118. const int64_t OC = dst->ne[2];
  12119. const int64_t OH = dst->ne[1];
  12120. const int64_t OW = dst->ne[0];
  12121. const int parallel_elements = N * OC * OH * OW;
  12122. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  12123. sycl::range<3> block_nums(1, 1, num_blocks);
  12124. main_stream->parallel_for(
  12125. sycl::nd_range<3>(block_nums *
  12126. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  12127. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  12128. [=](sycl::nd_item<3> item_ct1) {
  12129. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  12130. parallel_elements, src0_dd, dst_dd, op,
  12131. item_ct1);
  12132. });
  12133. (void) src1;
  12134. (void) src1_dd;
  12135. }
  12136. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  12137. const ggml_tensor *src1, ggml_tensor *dst,
  12138. const float *src0_dd, const float *src1_dd,
  12139. float *dst_dd,
  12140. const dpct::queue_ptr &main_stream) {
  12141. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12142. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12143. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  12144. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  12145. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  12146. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  12147. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  12148. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  12149. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  12150. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  12151. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  12152. const int64_t IH = is_2D ? src1->ne[1] : 1;
  12153. const int64_t IW = src1->ne[0];
  12154. const int64_t KH = is_2D ? src0->ne[1] : 1;
  12155. const int64_t KW = src0->ne[0];
  12156. const int64_t OH = is_2D ? dst->ne[2] : 1;
  12157. const int64_t OW = dst->ne[1];
  12158. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  12159. if (dst->type == GGML_TYPE_F16) {
  12160. 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);
  12161. } else {
  12162. im2col_sycl(src1_dd, (float *)dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  12163. }
  12164. (void) src0;
  12165. (void) src0_dd;
  12166. }
  12167. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  12168. const ggml_tensor *src1, ggml_tensor *dst,
  12169. const float *src0_dd, const float *src1_dd,
  12170. float *dst_dd,
  12171. const dpct::queue_ptr &main_stream) {
  12172. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12173. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12174. const int64_t ncols = src0->ne[0];
  12175. const int64_t nrows = ggml_nrows(src0);
  12176. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  12177. (void) src1;
  12178. (void) dst;
  12179. (void) src1_dd;
  12180. }
  12181. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  12182. const ggml_tensor *src1, ggml_tensor *dst,
  12183. const float *src0_dd, const float *src1_dd,
  12184. float *dst_dd,
  12185. const dpct::queue_ptr &main_stream) {
  12186. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12187. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  12188. const int64_t ncols = src0->ne[0];
  12189. const int64_t nrows = ggml_nrows(src0);
  12190. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  12191. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  12192. (void) src1;
  12193. (void) dst;
  12194. (void) src1_dd;
  12195. }
  12196. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  12197. const ggml_tensor *src1,
  12198. ggml_tensor *dst, const float *src0_dd,
  12199. const float *src1_dd, float *dst_dd,
  12200. const dpct::queue_ptr &main_stream) {
  12201. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12202. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12203. const int64_t ne00 = src0->ne[0];
  12204. const int64_t ne01 = src0->ne[1];
  12205. const int nrows0 = ggml_nrows(src0);
  12206. const int n_past = ((int32_t *) dst->op_params)[0];
  12207. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  12208. (void) src1;
  12209. (void) dst;
  12210. (void) src1_dd;
  12211. }
  12212. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  12213. const ggml_tensor *src1, ggml_tensor *dst,
  12214. const float *src0_dd, const float *src1_dd,
  12215. float *dst_dd,
  12216. const dpct::queue_ptr &main_stream) {
  12217. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12218. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12219. #pragma message("TODO: add ggml_sycl_op_soft_max() F16 src1 support")
  12220. #pragma message("ref: https://github.com/ggerganov/llama.cpp/pull/5021")
  12221. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  12222. const int64_t ne00 = src0->ne[0];
  12223. const int64_t nrows_x = ggml_nrows(src0);
  12224. const int64_t nrows_y = src0->ne[1];
  12225. float scale = 1.0f;
  12226. float max_bias = 0.0f;
  12227. memcpy(&scale, dst->op_params + 0, sizeof(float));
  12228. memcpy(&max_bias, dst->op_params + 1, sizeof(float));
  12229. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00,
  12230. nrows_x, nrows_y, scale, max_bias, main_stream);
  12231. }
  12232. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  12233. ggml_tensor *dst, const float *src0_dd,
  12234. const float *src1_dd, float *dst_dd,
  12235. const dpct::queue_ptr &main_stream) {
  12236. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12237. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12238. float scale;
  12239. memcpy(&scale, dst->op_params, sizeof(float));
  12240. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  12241. /*
  12242. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  12243. error codes. The call was replaced with 0. You need to rewrite this code.
  12244. */
  12245. SYCL_CHECK(0);
  12246. (void) src1;
  12247. (void) dst;
  12248. (void) src1_dd;
  12249. }
  12250. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  12251. ggml_tensor *dst, const float *src0_dd,
  12252. const float *src1_dd, float *dst_dd,
  12253. const dpct::queue_ptr &main_stream) {
  12254. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12255. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12256. float min;
  12257. float max;
  12258. memcpy(&min, dst->op_params, sizeof(float));
  12259. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  12260. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  12261. /*
  12262. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  12263. error codes. The call was replaced with 0. You need to rewrite this code.
  12264. */
  12265. SYCL_CHECK(0);
  12266. (void) src1;
  12267. (void) dst;
  12268. (void) src1_dd;
  12269. }
  12270. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  12271. const ggml_tensor *src1, ggml_tensor *dst,
  12272. const ggml_sycl_op_flatten_t op) try {
  12273. const int64_t nrows0 = ggml_nrows(src0);
  12274. const bool use_src1 = src1 != nullptr;
  12275. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  12276. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12277. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12278. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12279. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  12280. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12281. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12282. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  12283. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  12284. // dd = data device
  12285. float * src0_ddf = nullptr;
  12286. float * src1_ddf = nullptr;
  12287. float * dst_ddf = nullptr;
  12288. sycl_pool_alloc<float> src0_f;
  12289. sycl_pool_alloc<float> src1_f;
  12290. sycl_pool_alloc<float> dst_f;
  12291. ggml_sycl_set_device(g_main_device);
  12292. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12293. // GGML_SYCL_DEBUG("g_main_device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  12294. // g_main_device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  12295. if (src0_on_device) {
  12296. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  12297. } else {
  12298. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  12299. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  12300. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  12301. }
  12302. if (use_src1) {
  12303. if (src1_on_device) {
  12304. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12305. } else {
  12306. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  12307. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  12308. }
  12309. }
  12310. if (dst_on_device) {
  12311. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12312. } else {
  12313. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  12314. }
  12315. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  12316. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12317. // do the computation
  12318. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12319. /*
  12320. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  12321. error codes. The call was replaced with 0. You need to rewrite this code.
  12322. */
  12323. SYCL_CHECK(0);
  12324. // copy dst to host if necessary
  12325. if (!dst_on_device) {
  12326. SYCL_CHECK(CHECK_TRY_ERROR(
  12327. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst)).wait()));
  12328. }
  12329. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12330. SYCL_CHECK(CHECK_TRY_ERROR(
  12331. dpct::get_current_device().queues_wait_and_throw()));
  12332. }
  12333. // print_ggml_tensor("tensor", dst);
  12334. }
  12335. catch (sycl::exception const &exc) {
  12336. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12337. << ", line:" << __LINE__ << std::endl;
  12338. std::exit(1);
  12339. }
  12340. static void ggml_sycl_set_peer_access(const int n_tokens) {
  12341. static bool peer_access_enabled = false;
  12342. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  12343. if (peer_access_enabled == enable_peer_access) {
  12344. return;
  12345. }
  12346. #ifdef NDEBUG
  12347. for (int i = 0; i < g_device_count; ++i) {
  12348. SYCL_CHECK(ggml_sycl_set_device(i));
  12349. // SYCL_CHECK(syclDeviceSynchronize());
  12350. }
  12351. for (int i = 0; i < g_device_count; ++i) {
  12352. SYCL_CHECK(ggml_sycl_set_device(i));
  12353. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  12354. if (i == id_other) {
  12355. continue;
  12356. }
  12357. if (i != g_main_device && id_other != g_main_device) {
  12358. continue;
  12359. }
  12360. // int can_access_peer;
  12361. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  12362. // if (can_access_peer) {
  12363. // if (enable_peer_access) {
  12364. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  12365. // } else {
  12366. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  12367. // }
  12368. // }
  12369. }
  12370. }
  12371. #endif // NDEBUG
  12372. peer_access_enabled = enable_peer_access;
  12373. }
  12374. struct ggml_backend_sycl_split_buffer_type_context {
  12375. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12376. };
  12377. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  12378. const ggml_tensor *src1, ggml_tensor *dst,
  12379. ggml_sycl_op_mul_mat_t op,
  12380. const bool convert_src1_to_q8_1) try {
  12381. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  12382. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  12383. const int64_t nrows1 = ggml_nrows(src1);
  12384. GGML_ASSERT(ne03 == ne13);
  12385. const int64_t ne0 = dst->ne[0];
  12386. const int64_t ne1 = dst->ne[1];
  12387. const int nb2 = dst->nb[2];
  12388. const int nb3 = dst->nb[3];
  12389. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12390. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12391. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  12392. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  12393. const int64_t i02_divisor = ne12 / ne02;
  12394. const size_t src0_ts = ggml_type_size(src0->type);
  12395. const size_t src0_bs = ggml_blck_size(src0->type);
  12396. const size_t q8_1_ts = sizeof(block_q8_1);
  12397. const size_t q8_1_bs = QK8_1;
  12398. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12399. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12400. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12401. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12402. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  12403. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  12404. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  12405. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12406. GGML_ASSERT(!(split && ne02 > 1));
  12407. GGML_ASSERT(!(split && ne03 > 1));
  12408. GGML_ASSERT(!(split && ne02 < ne12));
  12409. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12410. if (split) {
  12411. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  12412. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  12413. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  12414. tensor_split = buft_ctx->tensor_split;
  12415. }
  12416. struct dev_data {
  12417. sycl_pool_alloc<char> src0_dd_alloc;
  12418. sycl_pool_alloc<float> src1_ddf_alloc;
  12419. sycl_pool_alloc<char> src1_ddq_alloc;
  12420. sycl_pool_alloc<float> dst_dd_alloc;
  12421. char *src0_dd = nullptr;
  12422. float *src1_ddf = nullptr; // float
  12423. char *src1_ddq = nullptr; // q8_1
  12424. float *dst_dd = nullptr;
  12425. int64_t row_low;
  12426. int64_t row_high;
  12427. };
  12428. dev_data dev[GGML_SYCL_MAX_DEVICES];
  12429. int used_devices = 0;
  12430. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12431. for (int i = 0; i < g_device_count; ++i) {
  12432. // by default, use all rows
  12433. dev[i].row_low = 0;
  12434. dev[i].row_high = ne01;
  12435. // for multi GPU, get the row boundaries from tensor split
  12436. // and round to mul_mat_q tile sizes
  12437. if (split) {
  12438. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  12439. if (i != 0) {
  12440. dev[i].row_low = ne01*tensor_split[i];
  12441. if (dev[i].row_low < ne01) {
  12442. dev[i].row_low -= dev[i].row_low % rounding;
  12443. }
  12444. }
  12445. if (i != g_device_count - 1) {
  12446. dev[i].row_high = ne01*tensor_split[i + 1];
  12447. if (dev[i].row_high < ne01) {
  12448. dev[i].row_high -= dev[i].row_high % rounding;
  12449. }
  12450. }
  12451. }
  12452. }
  12453. for (int i = 0; i < g_device_count; ++i) {
  12454. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12455. continue;
  12456. }
  12457. used_devices++;
  12458. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12459. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12460. ggml_sycl_set_device(i);
  12461. dpct::queue_ptr stream = g_syclStreams[i][0];
  12462. if (src0_on_device && src0_is_contiguous) {
  12463. dev[i].src0_dd = (char *) src0_extra->data_device[i];
  12464. } else {
  12465. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ggml_nbytes(src0));
  12466. }
  12467. if (src1_on_device && src1_is_contiguous) {
  12468. dev[i].src1_ddf = (float *) src1_extra->data_device[i];
  12469. } else {
  12470. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ggml_nelements(src1));
  12471. }
  12472. if (convert_src1_to_q8_1) {
  12473. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  12474. if (src1_on_device && src1_is_contiguous) {
  12475. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  12476. /*
  12477. DPCT1010:90: SYCL uses exceptions to report errors and does not
  12478. use the error codes. The call was replaced with 0. You need to
  12479. rewrite this code.
  12480. */
  12481. SYCL_CHECK(0);
  12482. }
  12483. }
  12484. if (dst_on_device) {
  12485. dev[i].dst_dd = (float *) dst_extra->data_device[i];
  12486. } else {
  12487. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  12488. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(size_dst_ddf);
  12489. }
  12490. }
  12491. // if multiple devices are used they need to wait for the main device
  12492. // here an event is recorded that signals that the main device has finished calculating the input data
  12493. if (split && used_devices > 1) {
  12494. ggml_sycl_set_device(g_main_device);
  12495. /*
  12496. DPCT1024:91: The original code returned the error code that was further
  12497. consumed by the program logic. This original code was replaced with 0.
  12498. You may need to rewrite the program logic consuming the error code.
  12499. */
  12500. SYCL_CHECK(CHECK_TRY_ERROR(
  12501. *src0_extra->events[g_main_device][0] =
  12502. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier()));
  12503. }
  12504. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  12505. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  12506. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  12507. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  12508. for (int i = 0; i < g_device_count; ++i) {
  12509. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12510. continue;
  12511. }
  12512. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12513. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12514. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  12515. ggml_sycl_set_device(i);
  12516. dpct::queue_ptr stream = g_syclStreams[i][is];
  12517. // wait for main GPU data if necessary
  12518. if (split && (i != g_main_device || is != 0)) {
  12519. /*
  12520. DPCT1009:163: SYCL uses exceptions to report errors and does not
  12521. use the error codes. The original code was commented out and a
  12522. warning string was inserted. You need to rewrite this code.
  12523. */
  12524. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  12525. {*src0_extra->events[g_main_device][0]})));
  12526. }
  12527. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  12528. const int64_t i03 = i0 / ne12;
  12529. const int64_t i02 = i0 % ne12;
  12530. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  12531. // for split tensors the data begins at i0 == i0_offset_low
  12532. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  12533. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  12534. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  12535. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  12536. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  12537. // in that case an offset on dst_ddf_i is needed
  12538. if (dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device) {
  12539. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  12540. }
  12541. // copy src0, src1 to device if necessary
  12542. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  12543. if (i != g_main_device) {
  12544. if (convert_src1_to_q8_1) {
  12545. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  12546. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  12547. src1_ddq_i, src1_ddq_i_source,
  12548. src1_ncols * src1_padded_col_size * q8_1_ts /
  12549. q8_1_bs).wait()));
  12550. } else {
  12551. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  12552. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  12553. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  12554. src1_ddf_i, src1_ddf_i_source,
  12555. src1_ncols * ne10 * sizeof(float))));
  12556. }
  12557. }
  12558. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  12559. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  12560. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  12561. } else {
  12562. GGML_ASSERT(false);
  12563. }
  12564. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  12565. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  12566. /*
  12567. DPCT1010:92: SYCL uses exceptions to report errors and does
  12568. not use the error codes. The call was replaced with 0. You
  12569. need to rewrite this code.
  12570. */
  12571. SYCL_CHECK(0);
  12572. }
  12573. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  12574. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  12575. }
  12576. if (src1->type == GGML_TYPE_F16) {
  12577. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  12578. }
  12579. // do the computation
  12580. SYCL_CHECK(CHECK_TRY_ERROR(op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  12581. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream)));
  12582. /*
  12583. DPCT1010:93: SYCL uses exceptions to report errors and does not
  12584. use the error codes. The call was replaced with 0. You need to
  12585. rewrite this code.
  12586. */
  12587. SYCL_CHECK(0);
  12588. // copy dst to host or other device if necessary
  12589. if (!dst_on_device) {
  12590. void * dst_off_device;
  12591. dpct::memcpy_direction kind;
  12592. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12593. dst_off_device = dst->data;
  12594. kind = dpct::device_to_host;
  12595. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  12596. dst_off_device = dst_extra->data_device[g_main_device];
  12597. kind = dpct::device_to_device;
  12598. } else {
  12599. GGML_ASSERT(false);
  12600. }
  12601. if (split) {
  12602. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  12603. // dst is NOT transposed.
  12604. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  12605. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  12606. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  12607. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12608. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12609. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  12610. //todo, dirty solution. Need be updated when device2device memcpy() is supported.
  12611. if (kind == dpct::device_to_device) {
  12612. size_t dst_size = ggml_nbytes_pad(dst);
  12613. float *host_buf = (float *)malloc(dst_size);
  12614. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12615. host_buf, ne0 * sizeof(float), dst_dd_i,
  12616. row_diff * sizeof(float), row_diff * sizeof(float),
  12617. src1_ncols, dpct::device_to_host, *stream)));
  12618. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[i]).queues_wait_and_throw();
  12619. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12620. dhf_dst_i, ne0 * sizeof(float), host_buf,
  12621. row_diff * sizeof(float), row_diff * sizeof(float),
  12622. src1_ncols, dpct::host_to_device, *main_stream)));
  12623. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[g_main_device]).queues_wait_and_throw();
  12624. free(host_buf);
  12625. } else {
  12626. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12627. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  12628. row_diff * sizeof(float), row_diff * sizeof(float),
  12629. src1_ncols, kind, *stream)));
  12630. }
  12631. } else {
  12632. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12633. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12634. dhf_dst_i += src1_col_0*ne0;
  12635. SYCL_CHECK(CHECK_TRY_ERROR(
  12636. stream->memcpy(dhf_dst_i, dst_dd_i,
  12637. src1_ncols * ne0 * sizeof(float)).wait()));
  12638. }
  12639. }
  12640. // add event for the main device to wait on until other device is done
  12641. if (split && (i != g_main_device || is != 0)) {
  12642. /*
  12643. DPCT1024:94: The original code returned the error code that
  12644. was further consumed by the program logic. This original
  12645. code was replaced with 0. You may need to rewrite the
  12646. program logic consuming the error code.
  12647. */
  12648. SYCL_CHECK(CHECK_TRY_ERROR(
  12649. *src0_extra->events[i][is] =
  12650. stream->ext_oneapi_submit_barrier()));
  12651. }
  12652. }
  12653. }
  12654. }
  12655. // main device waits for all other devices to be finished
  12656. if (split && g_device_count > 1) {
  12657. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  12658. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  12659. ggml_sycl_set_device(g_main_device);
  12660. for (int i = 0; i < g_device_count; ++i) {
  12661. if (dev[i].row_low == dev[i].row_high) {
  12662. continue;
  12663. }
  12664. for (int64_t is = 0; is < is_max; ++is) {
  12665. SYCL_CHECK(CHECK_TRY_ERROR(
  12666. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier(
  12667. {*src0_extra->events[i][is]})));
  12668. }
  12669. }
  12670. }
  12671. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12672. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12673. SYCL_CHECK(CHECK_TRY_ERROR(
  12674. dpct::get_current_device().queues_wait_and_throw()));
  12675. }
  12676. }
  12677. catch (sycl::exception const &exc) {
  12678. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12679. << ", line:" << __LINE__ << std::endl;
  12680. std::exit(1);
  12681. }
  12682. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12683. GGML_SYCL_DEBUG("call %s\n", __func__);
  12684. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  12685. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12686. }
  12687. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12688. GGML_SYCL_DEBUG("call %s\n", __func__);
  12689. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  12690. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12691. }
  12692. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12693. GGML_SYCL_DEBUG("call %s\n", __func__);
  12694. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  12695. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12696. }
  12697. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12698. GGML_SYCL_DEBUG("call %s\n", __func__);
  12699. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  12700. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12701. }
  12702. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12703. GGML_SYCL_DEBUG("call %s\n", __func__);
  12704. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  12705. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12706. }
  12707. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12708. GGML_SYCL_DEBUG("call %s\n", __func__);
  12709. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  12710. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12711. }
  12712. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12713. GGML_SYCL_DEBUG("call %s\n", __func__);
  12714. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  12715. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12716. }
  12717. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12718. GGML_SYCL_DEBUG("call %s\n", __func__);
  12719. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  12720. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12721. }
  12722. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12723. GGML_SYCL_DEBUG("call %s\n", __func__);
  12724. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  12725. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12726. }
  12727. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12728. GGML_SYCL_DEBUG("call %s\n", __func__);
  12729. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  12730. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12731. }
  12732. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12733. GGML_SYCL_DEBUG("call %s\n", __func__);
  12734. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  12735. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12736. }
  12737. static void ggml_sycl_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12738. GGML_SYCL_DEBUG("call %s\n", __func__);
  12739. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardsigmoid);
  12740. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12741. }
  12742. static void ggml_sycl_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12743. GGML_SYCL_DEBUG("call %s\n", __func__);
  12744. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardswish);
  12745. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12746. }
  12747. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12748. GGML_SYCL_DEBUG("call %s\n", __func__);
  12749. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  12750. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12751. }
  12752. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12753. GGML_SYCL_DEBUG("call %s\n", __func__);
  12754. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  12755. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12756. }
  12757. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12758. GGML_SYCL_DEBUG("call %s\n", __func__);
  12759. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  12760. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12761. }
  12762. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12763. GGML_SYCL_DEBUG("call %s\n", __func__);
  12764. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  12765. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12766. }
  12767. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12768. GGML_SYCL_DEBUG("call %s\n", __func__);
  12769. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  12770. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12771. }
  12772. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12773. GGML_SYCL_DEBUG("call %s\n", __func__);
  12774. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  12775. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12776. }
  12777. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12778. GGML_SYCL_DEBUG("call %s\n", __func__);
  12779. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  12780. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12781. }
  12782. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12783. GGML_SYCL_DEBUG("call %s\n", __func__);
  12784. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  12785. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12786. }
  12787. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  12788. if (!g_sycl_loaded) return false;
  12789. const int64_t ne10 = src1->ne[0];
  12790. const int64_t ne0 = dst->ne[0];
  12791. const int64_t ne1 = dst->ne[1];
  12792. // TODO: find the optimal values for these
  12793. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  12794. src1->type == GGML_TYPE_F32 &&
  12795. dst->type == GGML_TYPE_F32 &&
  12796. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  12797. }
  12798. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  12799. const ggml_tensor *src1,
  12800. ggml_tensor *dst) try {
  12801. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  12802. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12803. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  12804. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  12805. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12806. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12807. const int64_t ne00 = src0->ne[0];
  12808. const int64_t ne01 = src0->ne[1];
  12809. const int64_t ne02 = src0->ne[2];
  12810. const int64_t ne12 = src1->ne[2];
  12811. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12812. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12813. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12814. void * src0_ddq = src0_extra->data_device[g_main_device];
  12815. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12816. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12817. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12818. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12819. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  12820. }
  12821. catch (sycl::exception const &exc) {
  12822. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12823. << ", line:" << __LINE__ << std::endl;
  12824. std::exit(1);
  12825. }
  12826. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  12827. const ggml_tensor *src1,
  12828. ggml_tensor *dst) try {
  12829. GGML_ASSERT(!ggml_is_transposed(src0));
  12830. GGML_ASSERT(!ggml_is_transposed(src1));
  12831. GGML_ASSERT(!ggml_is_permuted(src0));
  12832. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12833. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12834. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12835. const int64_t ne00 = src0->ne[0];
  12836. const int64_t ne01 = src0->ne[1];
  12837. const int64_t ne02 = src0->ne[2];
  12838. const int64_t nb01 = src0->nb[1];
  12839. const int64_t nb02 = src0->nb[2];
  12840. const int64_t ne12 = src1->ne[2];
  12841. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12842. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12843. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12844. void * src0_ddq = src0_extra->data_device[g_main_device];
  12845. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12846. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12847. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12848. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12849. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  12850. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  12851. 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);
  12852. }
  12853. catch (sycl::exception const &exc) {
  12854. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12855. << ", line:" << __LINE__ << std::endl;
  12856. std::exit(1);
  12857. }
  12858. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  12859. const sycl::half *src1_as_f16, char *dst,
  12860. const void **ptrs_src, void **ptrs_dst,
  12861. int64_t ne12, int64_t ne13, int64_t ne23,
  12862. size_t nb02, size_t nb03, size_t nb12,
  12863. size_t nb13, size_t nbd2, size_t nbd3,
  12864. int64_t r2, int64_t r3,
  12865. const sycl::nd_item<3> &item_ct1) {
  12866. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  12867. item_ct1.get_local_id(2);
  12868. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  12869. item_ct1.get_local_id(1);
  12870. if (i13 >= ne13 || i12 >= ne12) {
  12871. return;
  12872. }
  12873. int64_t i03 = i13 / r3;
  12874. int64_t i02 = i12 / r2;
  12875. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  12876. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  12877. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  12878. }
  12879. static void ggml_sycl_mul_mat_batched_sycl(const ggml_tensor *src0,
  12880. const ggml_tensor *src1,
  12881. ggml_tensor *dst) try {
  12882. GGML_ASSERT(!ggml_is_transposed(src0));
  12883. GGML_ASSERT(!ggml_is_transposed(src1));
  12884. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12885. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12886. GGML_TENSOR_BINARY_OP_LOCALS
  12887. const int64_t ne_dst = ggml_nelements(dst);
  12888. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12889. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12890. bool no_mixed_dtypes = main_stream->get_backend() == sycl::backend::ext_oneapi_cuda ||
  12891. main_stream->get_backend() == sycl::backend::ext_oneapi_hip;
  12892. SYCL_CHECK(
  12893. CHECK_TRY_ERROR(g_sycl_handles[g_main_device] = main_stream));
  12894. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12895. void * src0_ddq = src0_extra->data_device[g_main_device];
  12896. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  12897. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12898. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12899. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12900. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12901. // convert src1 to fp16
  12902. sycl_pool_alloc<sycl::half> src1_f16_alloc;
  12903. if (src1->type != GGML_TYPE_F16) {
  12904. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12905. const int64_t ne_src1 = ggml_nelements(src1);
  12906. src1_f16_alloc.alloc(ne_src1);
  12907. GGML_ASSERT(to_fp16_sycl != nullptr);
  12908. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  12909. }
  12910. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  12911. : src1_f16_alloc.get();
  12912. sycl_pool_alloc<sycl::half> dst_f16;
  12913. char * dst_t;
  12914. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_float;
  12915. dpct::library_data_t cu_data_type = dpct::library_data_t::real_float;
  12916. if (no_mixed_dtypes) {
  12917. cu_compute_type = dpct::library_data_t::real_half;
  12918. cu_data_type = dpct::library_data_t::real_half;
  12919. }
  12920. // dst strides
  12921. size_t nbd2 = dst->nb[2];
  12922. size_t nbd3 = dst->nb[3];
  12923. const float alpha_f32 = 1.0f;
  12924. const float beta_f32 = 0.0f;
  12925. const sycl::half alpha_f16 = 1.0f;
  12926. const sycl::half beta_f16 = 0.0f;
  12927. const void * alpha = &alpha_f32;
  12928. const void * beta = &beta_f32;
  12929. if (no_mixed_dtypes) {
  12930. alpha = &alpha_f16;
  12931. beta = &beta_f16;
  12932. }
  12933. // TODO: Renable (dst->op_params[0] =! GGML_PREC_DEFAULT) pathway
  12934. // when oneMKL open source supports half, half, float, float: datatypes
  12935. dst_t = (char *) dst_ddf;
  12936. if (no_mixed_dtypes) {
  12937. dst_t = (char *) dst_f16.alloc(ne_dst);
  12938. nbd2 /= sizeof(float) / sizeof(sycl::half);
  12939. nbd3 /= sizeof(float) / sizeof(sycl::half);
  12940. }
  12941. GGML_ASSERT(ne12 % ne02 == 0);
  12942. GGML_ASSERT(ne13 % ne03 == 0);
  12943. // broadcast factors
  12944. const int64_t r2 = ne12/ne02;
  12945. const int64_t r3 = ne13/ne03;
  12946. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  12947. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  12948. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  12949. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  12950. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  12951. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  12952. nb01 / nb00, nb02 / nb00,
  12953. (const char *)src1_f16, dpct::library_data_t::real_half,
  12954. nb11 / nb10, nb12 / nb10, beta,
  12955. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  12956. ne12 * ne13, cu_compute_type)));
  12957. } else {
  12958. const int ne23 = ne12*ne13;
  12959. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  12960. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  12961. sycl::range<3> block_dims(1, ne12, ne13);
  12962. /*
  12963. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  12964. the limit. To get the device limit, query
  12965. info::device::max_work_group_size. Adjust the work-group size if needed.
  12966. */
  12967. {
  12968. dpct::has_capability_or_fail(main_stream->get_device(),
  12969. {sycl::aspect::fp16});
  12970. main_stream->submit([&](sycl::handler &cgh) {
  12971. const void **ptrs_src_get = ptrs_src.get();
  12972. void **ptrs_dst_get = ptrs_dst.get();
  12973. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  12974. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  12975. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  12976. [=](sycl::nd_item<3> item_ct1) {
  12977. k_compute_batched_ptrs(
  12978. src0_as_f16, src1_f16,
  12979. dst_t, ptrs_src_get,
  12980. ptrs_dst_get, ne12, ne13, ne23,
  12981. nb02, nb03, nb12_scaled, nb13_scaled,
  12982. nbd2, nbd3, r2, r3, item_ct1);
  12983. });
  12984. });
  12985. }
  12986. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  12987. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  12988. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  12989. (const void **)(ptrs_src.get() + 0 * ne23),
  12990. dpct::library_data_t::real_half, nb01 / nb00,
  12991. (const void **)(ptrs_src.get() + 1 * ne23),
  12992. dpct::library_data_t::real_half, nb11 / nb10, beta,
  12993. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  12994. cu_compute_type)));
  12995. }
  12996. if (no_mixed_dtypes) {
  12997. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  12998. to_fp32_sycl(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  12999. }
  13000. }
  13001. catch (sycl::exception const &exc) {
  13002. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13003. << ", line:" << __LINE__ << std::endl;
  13004. std::exit(1);
  13005. }
  13006. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13007. const bool all_on_device =
  13008. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  13009. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  13010. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  13011. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  13012. int64_t min_compute_capability = INT_MAX;
  13013. for (int i = 0; i < g_device_count; ++i) {
  13014. if (min_compute_capability > g_device_caps[i].cc && g_tensor_split[i] < (i + 1 < g_device_count ? g_tensor_split[i + 1] : 1.0f)) {
  13015. min_compute_capability = g_device_caps[i].cc;
  13016. }
  13017. }
  13018. #ifdef SYCL_USE_XMX
  13019. const bool use_xmx = true;
  13020. #else
  13021. const bool use_xmx = false;
  13022. #endif
  13023. // debug helpers
  13024. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  13025. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  13026. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  13027. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  13028. //printf("src0 is contiguous %d, transposed %d, type = %s, name = %s\n", ggml_is_contiguous(src0), ggml_is_transposed(src0), ggml_type_name(src0->type), src0->name);
  13029. //printf("src1 is contiguous %d, transposed %d, type = %s, name = %s\n", ggml_is_contiguous(src1), ggml_is_transposed(src1), ggml_type_name(src1->type), src1->name);
  13030. if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  13031. // KQ single-batch
  13032. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_p021\n");
  13033. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  13034. } else if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  13035. // KQV single-batch
  13036. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_nc\n");
  13037. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  13038. } else if (!split && all_on_device && use_xmx && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  13039. // KQ + KQV multi-batch
  13040. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_batched_sycl\n");
  13041. ggml_sycl_mul_mat_batched_sycl(src0, src1, dst);
  13042. } else if (src0->type == GGML_TYPE_F32) {
  13043. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat\n");
  13044. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13045. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  13046. // GGML_SYCL_DEBUG("ggml_is_quantized or GGML_TYPE_F16\n");
  13047. if (src1->ne[1] == 1 && src0->ne[0] % GGML_SYCL_DMMV_X == 0) {
  13048. #ifdef GGML_SYCL_FORCE_DMMV
  13049. const bool use_mul_mat_vec_q = false;
  13050. #else
  13051. bool use_mul_mat_vec_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  13052. use_mul_mat_vec_q = use_mul_mat_vec_q ||
  13053. (src0->type == GGML_TYPE_IQ2_XXS) || (src0->type == GGML_TYPE_IQ2_XS) || (src0->type == GGML_TYPE_IQ2_S) ||
  13054. (src0->type == GGML_TYPE_IQ3_XXS) || (src0->type == GGML_TYPE_IQ3_S) ||
  13055. (src0->type == GGML_TYPE_IQ4_NL) || (src0->type == GGML_TYPE_IQ4_XS) ||
  13056. (src0->type == GGML_TYPE_IQ1_S) || (src0->type == GGML_TYPE_IQ1_M);
  13057. #endif // GGML_SYCL_FORCE_DMMV
  13058. if (use_mul_mat_vec_q) {
  13059. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_vec_q path\n");
  13060. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  13061. } else {
  13062. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_dequantize_mul_mat_vec path\n");
  13063. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  13064. }
  13065. } else {
  13066. bool use_mul_mat_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  13067. if (use_xmx && min_compute_capability >= VER_GEN9 && src1->ne[1] > XMX_MAX_BATCH_SIZE) {
  13068. use_mul_mat_q = false;
  13069. }
  13070. if (use_mul_mat_q) {
  13071. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_q path\n");
  13072. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  13073. } else {
  13074. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_sycl path\n");
  13075. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13076. }
  13077. }
  13078. } else {
  13079. GGML_ASSERT(false);
  13080. }
  13081. }
  13082. #if 0
  13083. template<typename ... Srcs>
  13084. static __global__ void k_compute_batched_ptrs_id(
  13085. const void ** ptrs_src, void ** ptrs_dst,
  13086. int ne12, int ne13,
  13087. int ne23,
  13088. int nb02, int nb03,
  13089. int nb12, int nb13,
  13090. int nb2, int nb3,
  13091. int r2, int r3,
  13092. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  13093. const half * src1_f16, half * dst_f16,
  13094. const int32_t * ids, const int id,
  13095. Srcs... src0s) {
  13096. int i = ids[id];
  13097. half * src0_f16;
  13098. const void * srcs_ar[] = { (const half *) src0s... };
  13099. if (src0_type == GGML_TYPE_F16) {
  13100. src0_f16 = (half *) srcs_ar[i];
  13101. } else {
  13102. src0_f16 = src0_as_f16;
  13103. if (item_ct1.get_local_id(2) == 0 && threadIdx.y == 0) {
  13104. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  13105. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  13106. }
  13107. }
  13108. int i13 = blockIdx.x * blockDim.x + item_ct1.get_local_id(2);
  13109. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  13110. if (i13 >= ne13 || i12 >= ne12) {
  13111. return;
  13112. }
  13113. int i03 = i13 / r3;
  13114. int i02 = i12 / r2;
  13115. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  13116. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  13117. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  13118. }
  13119. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  13120. const struct ggml_tensor * ids = dst->src[0];
  13121. const struct ggml_tensor * src1 = dst->src[1];
  13122. const struct ggml_tensor * src00 = dst->src[2];
  13123. const int id = dst->op_params[0];
  13124. GGML_ASSERT(!ggml_is_transposed(src00));
  13125. GGML_ASSERT(!ggml_is_transposed(src1));
  13126. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13127. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13128. GGML_TENSOR_LOCALS(int64_t, ne0, src00, ne);
  13129. //const int64_t nb01 = src00->nb[1];
  13130. GGML_TENSOR_LOCALS(int64_t, nb0, src00, nb);
  13131. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  13132. GGML_TENSOR_LOCALS(int64_t, nb1, src1, nb);
  13133. //const int64_t nb11 = src1->nb[1];
  13134. const int64_t ne1 = ggml_nelements(src1);
  13135. const int64_t ne = ggml_nelements(dst);
  13136. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13137. syclStream_t main_stream = g_syclStreams[g_main_device][0];
  13138. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device], main_stream));
  13139. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13140. //void * src0_ddq = src0_extra->data_device[g_main_device];
  13141. //half * src0_as_f16 = (half *) src0_ddq;
  13142. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13143. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13144. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13145. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13146. // convert src1 to fp16
  13147. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13148. GGML_ASSERT(to_fp16_sycl != nullptr);
  13149. size_t src1_as = 0;
  13150. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne1 * sizeof(half), &src1_as);
  13151. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  13152. size_t dst_as = 0;
  13153. half * dst_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne * sizeof(half), &dst_as);
  13154. GGML_ASSERT(ne12 % ne02 == 0);
  13155. GGML_ASSERT(ne13 % ne03 == 0);
  13156. // broadcast factors
  13157. const int64_t r2 = ne12/ne02;
  13158. const int64_t r3 = ne13/ne03;
  13159. const half alpha_f16 = 1.0f;
  13160. const half beta_f16 = 0.0f;
  13161. // use syclGemmBatchedEx
  13162. const int ne23 = ne12*ne13;
  13163. const void ** ptrs_src = nullptr;
  13164. void ** ptrs_dst = nullptr;
  13165. size_t ptrs_src_s = 0;
  13166. size_t ptrs_dst_s = 0;
  13167. ptrs_src = (const void **) ggml_sycl_pool_malloc(g_main_device, 2*ne23*sizeof(void *), &ptrs_src_s);
  13168. ptrs_dst = ( void **) ggml_sycl_pool_malloc(g_main_device, 1*ne23*sizeof(void *), &ptrs_dst_s);
  13169. int64_t src0_ne = ggml_nelements(src00);
  13170. half * src0_as_f16 = nullptr;
  13171. size_t src0_as = 0;
  13172. if (src00->type != GGML_TYPE_F16) {
  13173. src0_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, src0_ne * sizeof(half), &src0_as);
  13174. }
  13175. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  13176. dim3 block_dims(ne13, ne12);
  13177. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  13178. ptrs_src, ptrs_dst,
  13179. ne12, ne13,
  13180. ne23,
  13181. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  13182. nb12, nb13,
  13183. dst->nb[2], dst->nb[3],
  13184. r2, r3,
  13185. src00->type, src0_as_f16, src0_ne,
  13186. src1_as_f16, dst_f16,
  13187. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  13188. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  13189. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  13190. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  13191. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  13192. );
  13193. SYCL_CHECK(syclGetLastError());
  13194. SYCL_CHECK(
  13195. syclGemmBatchedEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  13196. ne01, ne11, ne10,
  13197. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  13198. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  13199. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  13200. ne23,
  13201. CUBLAS_COMPUTE_16F,
  13202. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  13203. if (src0_as != 0) {
  13204. ggml_sycl_pool_free(g_main_device, src0_as_f16, src0_as);
  13205. }
  13206. if (ptrs_src_s != 0) {
  13207. ggml_sycl_pool_free(g_main_device, ptrs_src, ptrs_src_s);
  13208. }
  13209. if (ptrs_dst_s != 0) {
  13210. ggml_sycl_pool_free(g_main_device, ptrs_dst, ptrs_dst_s);
  13211. }
  13212. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13213. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  13214. ggml_sycl_pool_free(g_main_device, src1_as_f16, src1_as);
  13215. ggml_sycl_pool_free(g_main_device, dst_f16, dst_as);
  13216. }
  13217. #endif
  13218. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  13219. const ggml_tensor *src1,
  13220. ggml_tensor *dst) try {
  13221. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT &&
  13222. "mul_mat_id does not support split buffers");
  13223. const ggml_tensor *ids = dst->src[2];
  13224. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13225. const size_t nb11 = src1->nb[1];
  13226. const size_t nb1 = dst->nb[1];
  13227. const int32_t id = ((int32_t *)dst->op_params)[0];
  13228. const int32_t n_as = src0->ne[2];
  13229. std::vector<char> ids_host(ggml_nbytes(ids));
  13230. const char *ids_dev = (const char *)ids->data;
  13231. SYCL_CHECK(CHECK_TRY_ERROR(
  13232. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
  13233. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13234. const ggml_tensor_extra_gpu *src0_extra =
  13235. (const ggml_tensor_extra_gpu *)src0->extra;
  13236. const ggml_tensor_extra_gpu *src1_extra =
  13237. (const ggml_tensor_extra_gpu *)src1->extra;
  13238. const ggml_tensor_extra_gpu *dst_extra =
  13239. (const ggml_tensor_extra_gpu *)dst->extra;
  13240. ggml_tensor_extra_gpu src0_row_extra;
  13241. ggml_tensor_extra_gpu src1_row_extra;
  13242. ggml_tensor_extra_gpu dst_row_extra;
  13243. ggml_tensor src0_row = *src0;
  13244. ggml_tensor src1_row = *src1;
  13245. ggml_tensor dst_row = *dst;
  13246. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  13247. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  13248. src0_row.extra = &src0_row_extra;
  13249. src1_row.extra = &src1_row_extra;
  13250. dst_row.extra = &dst_row_extra;
  13251. char *src0_original = src1->backend == GGML_BACKEND_TYPE_CPU
  13252. ? (char *)src0->data
  13253. : (char *)src0_extra->data_device[g_main_device];
  13254. char *src1_original = src1->backend == GGML_BACKEND_TYPE_CPU
  13255. ? (char *)src1->data
  13256. : (char *)src1_extra->data_device[g_main_device];
  13257. char *dst_original = dst->backend == GGML_BACKEND_TYPE_CPU
  13258. ? (char *)dst->data
  13259. : (char *)dst_extra->data_device[g_main_device];
  13260. src0_row.ne[2] = 1;
  13261. src0_row.ne[3] = 1;
  13262. src0_row.nb[3] = src0->nb[2];
  13263. if (src1->ne[1] == 1) {
  13264. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13265. const int32_t row_id =
  13266. *(const int32_t *)(ids_host.data() + i01 * ids->nb[1] +
  13267. id * ids->nb[0]);
  13268. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13269. src0_row_extra.data_device[g_main_device] =
  13270. src0_original + row_id * src0->nb[2];
  13271. src1_row_extra.data_device[g_main_device] =
  13272. src1_original + i01 * src1->nb[1];
  13273. dst_row_extra.data_device[g_main_device] =
  13274. dst_original + i01 * dst->nb[1];
  13275. ggml_sycl_mul_mat(&src0_row, &src1_row, &dst_row);
  13276. }
  13277. } else {
  13278. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  13279. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  13280. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  13281. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  13282. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  13283. int64_t num_src1_rows = 0;
  13284. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13285. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13286. if (row_id_i != row_id) {
  13287. continue;
  13288. }
  13289. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13290. SYCL_CHECK(CHECK_TRY_ERROR(
  13291. stream->memcpy(src1_contiguous.get() + num_src1_rows * nb11,
  13292. src1_original + i01 * nb11, nb11)));
  13293. num_src1_rows++;
  13294. }
  13295. if (num_src1_rows == 0) {
  13296. continue;
  13297. }
  13298. src0_row_extra.data_device[g_main_device] =
  13299. src0_original + row_id * src0->nb[2];
  13300. src1_row.ne[1] = num_src1_rows;
  13301. dst_row.ne[1] = num_src1_rows;
  13302. src1_row.nb[1] = nb11;
  13303. src1_row.nb[2] = num_src1_rows*nb11;
  13304. src1_row.nb[3] = num_src1_rows*nb11;
  13305. dst_row.nb[1] = nb1;
  13306. dst_row.nb[2] = num_src1_rows*nb1;
  13307. dst_row.nb[3] = num_src1_rows*nb1;
  13308. ggml_sycl_mul_mat(&src0_row, &src1_row, &dst_row);
  13309. num_src1_rows = 0;
  13310. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13311. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13312. if (row_id_i != row_id) {
  13313. continue;
  13314. }
  13315. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13316. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  13317. dst_original + i01 * nb1,
  13318. dst_contiguous.get() + num_src1_rows * nb1, nb1)));
  13319. num_src1_rows++;
  13320. }
  13321. }
  13322. }
  13323. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  13324. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13325. }
  13326. }
  13327. catch (sycl::exception const &exc) {
  13328. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13329. << ", line:" << __LINE__ << std::endl;
  13330. std::exit(1);
  13331. }
  13332. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13333. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  13334. }
  13335. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13336. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  13337. }
  13338. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  13339. ggml_tensor *dst) try {
  13340. const int64_t ne = ggml_nelements(src0);
  13341. GGML_ASSERT(ne == ggml_nelements(src1));
  13342. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  13343. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13344. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  13345. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  13346. GGML_TENSOR_BINARY_OP_LOCALS;
  13347. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13348. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13349. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13350. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13351. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13352. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  13353. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  13354. 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);
  13355. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  13356. 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);
  13357. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  13358. 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);
  13359. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  13360. 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);
  13361. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  13362. 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);
  13363. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  13364. 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);
  13365. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  13366. 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);
  13367. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  13368. 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);
  13369. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  13370. 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);
  13371. } else {
  13372. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  13373. ggml_type_name(src0->type), ggml_type_name(src1->type));
  13374. GGML_ASSERT(false);
  13375. }
  13376. (void) dst;
  13377. }
  13378. catch (sycl::exception const &exc) {
  13379. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13380. << ", line:" << __LINE__ << std::endl;
  13381. std::exit(1);
  13382. }
  13383. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13384. // TODO: why do we pass dst as src1 here?
  13385. ggml_sycl_cpy(src0, dst, nullptr);
  13386. (void) src1;
  13387. }
  13388. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13389. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  13390. }
  13391. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13392. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  13393. }
  13394. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13395. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  13396. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  13397. }
  13398. static void ggml_sycl_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13399. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pool2d);
  13400. }
  13401. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13402. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  13403. }
  13404. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13405. GGML_ASSERT(ggml_is_contiguous(src0));
  13406. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  13407. }
  13408. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13409. GGML_ASSERT(ggml_is_contiguous(src0));
  13410. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  13411. }
  13412. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13413. (void) src0;
  13414. (void) src1;
  13415. (void) dst;
  13416. }
  13417. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  13418. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  13419. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  13420. }
  13421. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  13422. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_TYPE_GPU && tensor->backend != GGML_BACKEND_TYPE_GPU_SPLIT) ) {
  13423. return;
  13424. }
  13425. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13426. for (int i = 0; i < g_device_count; ++i) {
  13427. const dpct::queue_ptr stream = g_syclStreams[i][0];
  13428. if (extra->data_device[i] != nullptr) {
  13429. SYCL_CHECK(ggml_sycl_set_device(i));
  13430. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[i], *stream)));
  13431. }
  13432. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  13433. if (extra->events[i][is] != nullptr) {
  13434. SYCL_CHECK(ggml_sycl_set_device(i));
  13435. SYCL_CHECK(CHECK_TRY_ERROR(
  13436. dpct::destroy_event(extra->events[i][is])));
  13437. }
  13438. }
  13439. }
  13440. delete extra;
  13441. }
  13442. catch (sycl::exception const &exc) {
  13443. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13444. << ", line:" << __LINE__ << std::endl;
  13445. std::exit(1);
  13446. }
  13447. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  13448. static size_t g_temp_tensor_extra_index = 0;
  13449. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13450. if (g_temp_tensor_extras == nullptr) {
  13451. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13452. }
  13453. size_t alloc_index = g_temp_tensor_extra_index;
  13454. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13455. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  13456. memset(extra, 0, sizeof(*extra));
  13457. return extra;
  13458. }
  13459. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  13460. bool scratch, bool force_inplace,
  13461. bool no_alloc) try {
  13462. if (scratch && g_scratch_size == 0) {
  13463. return;
  13464. }
  13465. tensor->backend = GGML_BACKEND_TYPE_GPU;
  13466. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_CPU) {
  13467. const ggml_op src0_op = tensor->src[0]->op;
  13468. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  13469. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  13470. }
  13471. }
  13472. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_TYPE_CPU) {
  13473. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  13474. }
  13475. if (scratch && no_alloc) {
  13476. return;
  13477. }
  13478. ggml_tensor_extra_gpu * extra;
  13479. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  13480. tensor->op == GGML_OP_VIEW ||
  13481. force_inplace;
  13482. const size_t size = ggml_nbytes(tensor);
  13483. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13484. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13485. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT)) {
  13486. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  13487. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13488. size_t offset = 0;
  13489. if (tensor->op == GGML_OP_VIEW) {
  13490. memcpy(&offset, tensor->op_params, sizeof(size_t));
  13491. }
  13492. extra = ggml_sycl_alloc_temp_tensor_extra();
  13493. extra->data_device[g_main_device] = src0_ddc + offset;
  13494. } else if (tensor->op == GGML_OP_CPY) {
  13495. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  13496. void * src1_ddv = src1_extra->data_device[g_main_device];
  13497. extra = ggml_sycl_alloc_temp_tensor_extra();
  13498. extra->data_device[g_main_device] = src1_ddv;
  13499. } else if (scratch) {
  13500. GGML_ASSERT(size <= g_scratch_size);
  13501. if (g_scratch_offset + size > g_scratch_size) {
  13502. g_scratch_offset = 0;
  13503. }
  13504. char * data = (char *) g_scratch_buffer;
  13505. if (data == nullptr) {
  13506. SYCL_CHECK(CHECK_TRY_ERROR(
  13507. data = (char *)sycl::malloc_device(
  13508. g_scratch_size, *stream)));
  13509. g_scratch_buffer = data;
  13510. }
  13511. extra = ggml_sycl_alloc_temp_tensor_extra();
  13512. extra->data_device[g_main_device] = data + g_scratch_offset;
  13513. g_scratch_offset += size;
  13514. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  13515. } else { // allocate new buffers outside of scratch
  13516. void * data;
  13517. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  13518. size, *stream)));
  13519. SYCL_CHECK(CHECK_TRY_ERROR(
  13520. (*stream).memset(data, 0, size).wait()));
  13521. extra = new ggml_tensor_extra_gpu;
  13522. memset(extra, 0, sizeof(*extra));
  13523. extra->data_device[g_main_device] = data;
  13524. }
  13525. tensor->extra = extra;
  13526. }
  13527. catch (sycl::exception const &exc) {
  13528. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13529. << ", line:" << __LINE__ << std::endl;
  13530. std::exit(1);
  13531. }
  13532. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  13533. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13534. GGML_ASSERT(ggml_is_contiguous(tensor));
  13535. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13536. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13537. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13538. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  13539. .memcpy(extra->data_device[g_main_device],
  13540. tensor->data, ggml_nbytes(tensor))
  13541. .wait()));
  13542. }
  13543. catch (sycl::exception const &exc) {
  13544. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13545. << ", line:" << __LINE__ << std::endl;
  13546. std::exit(1);
  13547. }
  13548. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  13549. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  13550. }
  13551. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  13552. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  13553. }
  13554. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  13555. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  13556. }
  13557. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  13558. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  13559. }
  13560. void ggml_sycl_set_main_device(const int main_device) try {
  13561. if (g_main_device == main_device) return;
  13562. check_allow_gpu_index(main_device);
  13563. g_main_device = main_device;
  13564. g_main_device_id = g_sycl_gpu_mgr->gpus[main_device];
  13565. if (g_ggml_sycl_debug) {
  13566. dpct::device_info prop;
  13567. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  13568. prop, dpct::dev_mgr::instance().get_device(g_main_device_id))));
  13569. fprintf(stderr, "Using device %d (%s) as main device\n",
  13570. g_main_device_id, prop.get_name());
  13571. }
  13572. }
  13573. catch (sycl::exception const &exc) {
  13574. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13575. << ", line:" << __LINE__ << std::endl;
  13576. std::exit(1);
  13577. }
  13578. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  13579. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  13580. // it still won't always work as expected, but it's better than nothing
  13581. if (scratch_size > g_scratch_size) {
  13582. ggml_sycl_free_scratch();
  13583. }
  13584. g_scratch_size = std::max(g_scratch_size, scratch_size);
  13585. }
  13586. void ggml_sycl_free_scratch() try {
  13587. if (g_scratch_buffer == nullptr) {
  13588. return;
  13589. }
  13590. ggml_sycl_set_device(g_main_device);
  13591. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13592. SYCL_CHECK(CHECK_TRY_ERROR(
  13593. sycl::free(g_scratch_buffer, *stream)));
  13594. g_scratch_buffer = nullptr;
  13595. }
  13596. catch (sycl::exception const &exc) {
  13597. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13598. << ", line:" << __LINE__ << std::endl;
  13599. std::exit(1);
  13600. }
  13601. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  13602. if (!g_sycl_loaded) return false;
  13603. ggml_sycl_func_t func;
  13604. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  13605. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  13606. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  13607. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  13608. return false;
  13609. }
  13610. if (tensor->op == GGML_OP_MUL_MAT) {
  13611. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  13612. #ifndef NDEBUG
  13613. fprintf(stderr, "%s: cannot compute %s: src0->ne[3] = %" PRId64 ", src1->ne[3] = %" PRId64 " - fallback to CPU\n", __func__, tensor->name, tensor->src[0]->ne[3], tensor->src[1]->ne[3]);
  13614. #endif
  13615. return false;
  13616. }
  13617. }
  13618. switch (tensor->op) {
  13619. case GGML_OP_REPEAT:
  13620. func = ggml_sycl_repeat;
  13621. break;
  13622. case GGML_OP_GET_ROWS:
  13623. func = ggml_sycl_get_rows;
  13624. break;
  13625. case GGML_OP_DUP:
  13626. func = ggml_sycl_dup;
  13627. break;
  13628. case GGML_OP_ADD:
  13629. func = ggml_sycl_add;
  13630. break;
  13631. case GGML_OP_ACC:
  13632. func = ggml_sycl_acc;
  13633. break;
  13634. case GGML_OP_MUL:
  13635. func = ggml_sycl_mul;
  13636. break;
  13637. case GGML_OP_DIV:
  13638. func = ggml_sycl_div;
  13639. break;
  13640. case GGML_OP_UNARY:
  13641. switch (ggml_get_unary_op(tensor)) {
  13642. case GGML_UNARY_OP_GELU:
  13643. func = ggml_sycl_gelu;
  13644. break;
  13645. case GGML_UNARY_OP_SILU:
  13646. func = ggml_sycl_silu;
  13647. break;
  13648. case GGML_UNARY_OP_GELU_QUICK:
  13649. func = ggml_sycl_gelu_quick;
  13650. break;
  13651. case GGML_UNARY_OP_TANH:
  13652. func = ggml_sycl_tanh;
  13653. break;
  13654. case GGML_UNARY_OP_RELU:
  13655. func = ggml_sycl_relu;
  13656. break;
  13657. case GGML_UNARY_OP_HARDSIGMOID:
  13658. func = ggml_sycl_hardsigmoid;
  13659. break;
  13660. case GGML_UNARY_OP_HARDSWISH:
  13661. func = ggml_sycl_hardswish;
  13662. break;
  13663. default:
  13664. return false;
  13665. }
  13666. break;
  13667. case GGML_OP_NORM:
  13668. func = ggml_sycl_norm;
  13669. break;
  13670. case GGML_OP_GROUP_NORM:
  13671. func = ggml_sycl_group_norm;
  13672. break;
  13673. case GGML_OP_CONCAT:
  13674. func = ggml_sycl_concat;
  13675. break;
  13676. case GGML_OP_UPSCALE:
  13677. func = ggml_sycl_upscale;
  13678. break;
  13679. case GGML_OP_PAD:
  13680. func = ggml_sycl_pad;
  13681. break;
  13682. case GGML_OP_LEAKY_RELU:
  13683. func = ggml_sycl_leaky_relu;
  13684. break;
  13685. case GGML_OP_RMS_NORM:
  13686. func = ggml_sycl_rms_norm;
  13687. break;
  13688. case GGML_OP_MUL_MAT:
  13689. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  13690. return false;
  13691. }
  13692. func = ggml_sycl_mul_mat;
  13693. break;
  13694. case GGML_OP_MUL_MAT_ID:
  13695. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  13696. return false;
  13697. }
  13698. func = ggml_sycl_mul_mat_id;
  13699. break;
  13700. case GGML_OP_SCALE:
  13701. func = ggml_sycl_scale;
  13702. break;
  13703. case GGML_OP_SQR:
  13704. func = ggml_sycl_sqr;
  13705. break;
  13706. case GGML_OP_CLAMP:
  13707. func = ggml_sycl_clamp;
  13708. break;
  13709. case GGML_OP_CPY:
  13710. func = ggml_sycl_cpy;
  13711. break;
  13712. case GGML_OP_CONT:
  13713. func = ggml_sycl_dup;
  13714. break;
  13715. case GGML_OP_NONE:
  13716. case GGML_OP_RESHAPE:
  13717. case GGML_OP_VIEW:
  13718. case GGML_OP_PERMUTE:
  13719. case GGML_OP_TRANSPOSE:
  13720. func = ggml_sycl_nop;
  13721. break;
  13722. case GGML_OP_DIAG_MASK_INF:
  13723. func = ggml_sycl_diag_mask_inf;
  13724. break;
  13725. case GGML_OP_SOFT_MAX:
  13726. func = ggml_sycl_soft_max;
  13727. break;
  13728. case GGML_OP_ROPE:
  13729. func = ggml_sycl_rope;
  13730. break;
  13731. case GGML_OP_IM2COL:
  13732. func = ggml_sycl_im2col;
  13733. break;
  13734. case GGML_OP_POOL_2D:
  13735. func = ggml_sycl_pool2d;
  13736. break;
  13737. case GGML_OP_SUM_ROWS:
  13738. func = ggml_sycl_sum_rows;
  13739. break;
  13740. case GGML_OP_ARGSORT:
  13741. func = ggml_sycl_argsort;
  13742. break;
  13743. default:
  13744. return false;
  13745. }
  13746. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  13747. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  13748. }
  13749. if (params->ith != 0) {
  13750. return true;
  13751. }
  13752. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  13753. return true;
  13754. }
  13755. func(tensor->src[0], tensor->src[1], tensor);
  13756. return true;
  13757. }
  13758. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  13759. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_gpu_list\n");
  13760. for(int i=0;i<max_len;i++) id_list[i] = -1;
  13761. if (!g_sycl_gpu_mgr) {
  13762. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  13763. }
  13764. for (int i=0;i< g_sycl_gpu_mgr->gpus.size();i++){
  13765. if (i>=max_len) break;
  13766. id_list[i] = g_sycl_gpu_mgr->gpus[i];
  13767. }
  13768. return;
  13769. }
  13770. catch (sycl::exception const &exc) {
  13771. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13772. << ", line:" << __LINE__ << std::endl;
  13773. std::exit(1);
  13774. }
  13775. int ggml_sycl_get_device_count() try {
  13776. int device_count;
  13777. if (CHECK_TRY_ERROR(device_count =
  13778. dpct::dev_mgr::instance().device_count()) != 0) {
  13779. return 0;
  13780. }
  13781. return device_count;
  13782. }
  13783. catch (sycl::exception const &exc) {
  13784. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13785. << ", line:" << __LINE__ << std::endl;
  13786. std::exit(1);
  13787. }
  13788. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  13789. size_t description_size) try {
  13790. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_device_description\n");
  13791. dpct::device_info prop;
  13792. int device_id = g_sycl_gpu_mgr->gpus[device];
  13793. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  13794. prop, dpct::dev_mgr::instance().get_device(device_id))));
  13795. snprintf(description, description_size, "%s", prop.get_name());
  13796. }
  13797. catch (sycl::exception const &exc) {
  13798. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13799. << ", line:" << __LINE__ << std::endl;
  13800. std::exit(1);
  13801. }
  13802. GGML_CALL void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  13803. size_t *total) try {
  13804. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_memory\n");
  13805. ggml_sycl_set_device(device);
  13806. /*
  13807. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  13808. error codes. The original code was commented out and a warning string was
  13809. inserted. You need to rewrite this code.
  13810. */
  13811. /*
  13812. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  13813. device information which may not be supported by all compilers or runtimes.
  13814. You may need to adjust the code.
  13815. */
  13816. int device_id = g_sycl_gpu_mgr->gpus[device];
  13817. SYCL_CHECK(CHECK_TRY_ERROR(
  13818. dpct::dev_mgr::instance().get_device(device_id).get_memory_info(*free, *total)));
  13819. }
  13820. catch (sycl::exception const &exc) {
  13821. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13822. << ", line:" << __LINE__ << std::endl;
  13823. std::exit(1);
  13824. }
  13825. ////////////////////////////////////////////////////////////////////////////////
  13826. // backend interface
  13827. #define UNUSED GGML_UNUSED
  13828. // sycl buffer
  13829. struct ggml_backend_sycl_buffer_context {
  13830. int device;
  13831. void * dev_ptr = nullptr;
  13832. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  13833. size_t temp_tensor_extra_index = 0;
  13834. std::string name;
  13835. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) :
  13836. device(device), dev_ptr(dev_ptr) {
  13837. check_allow_gpu_index(device);
  13838. int id = g_sycl_gpu_mgr->gpus[device];
  13839. name = (GGML_SYCL_NAME + std::to_string(id));
  13840. }
  13841. ~ ggml_backend_sycl_buffer_context() {
  13842. delete[] temp_tensor_extras;
  13843. }
  13844. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13845. if (temp_tensor_extras == nullptr) {
  13846. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13847. }
  13848. size_t alloc_index = temp_tensor_extra_index;
  13849. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13850. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  13851. memset(extra, 0, sizeof(*extra));
  13852. return extra;
  13853. }
  13854. };
  13855. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  13856. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  13857. return ctx->name.c_str();
  13858. }
  13859. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  13860. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  13861. }
  13862. static void
  13863. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  13864. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13865. ggml_sycl_set_device(ctx->device);
  13866. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  13867. SYCL_CHECK(
  13868. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  13869. delete ctx;
  13870. }
  13871. catch (sycl::exception const &exc) {
  13872. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13873. << ", line:" << __LINE__ << std::endl;
  13874. std::exit(1);
  13875. }
  13876. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  13877. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13878. return ctx->dev_ptr;
  13879. }
  13880. GGML_CALL static void
  13881. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  13882. ggml_tensor *tensor) try {
  13883. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  13884. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  13885. assert(tensor->view_src->buffer->buft == buffer->buft);
  13886. tensor->backend = tensor->view_src->backend;
  13887. tensor->extra = tensor->view_src->extra;
  13888. return;
  13889. }
  13890. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  13891. extra->data_device[ctx->device] = tensor->data;
  13892. tensor->backend = GGML_BACKEND_TYPE_GPU;
  13893. tensor->extra = extra;
  13894. if (ggml_is_quantized(tensor->type)) {
  13895. // initialize padding to 0 to avoid possible NaN values
  13896. size_t original_size = ggml_nbytes(tensor);
  13897. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  13898. if (padded_size > original_size && tensor->view_src == nullptr) {
  13899. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  13900. (char *)tensor->data + original_size, 0,
  13901. padded_size - original_size).wait()));
  13902. }
  13903. }
  13904. }
  13905. catch (sycl::exception const &exc) {
  13906. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13907. << ", line:" << __LINE__ << std::endl;
  13908. std::exit(1);
  13909. }
  13910. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  13911. ggml_tensor *tensor,
  13912. const void *data, size_t offset,
  13913. size_t size) try {
  13914. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13915. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13916. ggml_sycl_set_device(ctx->device);
  13917. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  13918. SYCL_CHECK(
  13919. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  13920. char* host_buf = (char*)malloc(size);
  13921. memcpy(host_buf, data, size);
  13922. SYCL_CHECK(
  13923. CHECK_TRY_ERROR((*stream)
  13924. .memcpy((char *)tensor->data + offset, host_buf, size)
  13925. .wait()));
  13926. free(host_buf);
  13927. }
  13928. catch (sycl::exception const &exc) {
  13929. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13930. << ", line:" << __LINE__ << std::endl;
  13931. std::exit(1);
  13932. }
  13933. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  13934. const ggml_tensor *tensor,
  13935. void *data, size_t offset,
  13936. size_t size) try {
  13937. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13938. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13939. ggml_sycl_set_device(ctx->device);
  13940. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  13941. SYCL_CHECK(
  13942. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  13943. SYCL_CHECK(CHECK_TRY_ERROR(
  13944. (*stream)
  13945. .memcpy(data, (const char *)tensor->data + offset, size)
  13946. .wait()));
  13947. }
  13948. catch (sycl::exception const &exc) {
  13949. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13950. << ", line:" << __LINE__ << std::endl;
  13951. std::exit(1);
  13952. }
  13953. GGML_CALL static bool
  13954. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  13955. const ggml_tensor *src,
  13956. ggml_tensor *dst) try {
  13957. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  13958. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  13959. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  13960. ggml_sycl_set_device(src_ctx->device);
  13961. /*
  13962. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  13963. error codes. The original code was commented out and a warning string
  13964. was inserted. You need to rewrite this code.
  13965. */
  13966. SYCL_CHECK(CHECK_TRY_ERROR(
  13967. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  13968. ggml_sycl_set_device(dst_ctx->device);
  13969. /*
  13970. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  13971. error codes. The original code was commented out and a warning string
  13972. was inserted. You need to rewrite this code.
  13973. */
  13974. SYCL_CHECK(CHECK_TRY_ERROR(
  13975. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  13976. /*
  13977. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  13978. error codes. The original code was commented out and a warning string
  13979. was inserted. You need to rewrite this code.
  13980. */
  13981. dpct::queue_ptr stream_dst = g_syclStreams[dst_ctx->device][0];
  13982. dpct::queue_ptr stream_src = g_syclStreams[src_ctx->device][0];
  13983. size_t size = ggml_nbytes(src);
  13984. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  13985. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  13986. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  13987. #if 0
  13988. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  13989. (char *)dst->data, (const char *)src->data, size).wait()));
  13990. /*
  13991. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  13992. error codes. The original code was commented out and a warning string
  13993. was inserted. You need to rewrite this code.
  13994. */
  13995. SYCL_CHECK(CHECK_TRY_ERROR(
  13996. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  13997. #endif
  13998. return true;
  13999. }
  14000. return false;
  14001. }
  14002. catch (sycl::exception const &exc) {
  14003. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14004. << ", line:" << __LINE__ << std::endl;
  14005. std::exit(1);
  14006. }
  14007. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  14008. uint8_t value) try {
  14009. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14010. ggml_sycl_set_device(ctx->device);
  14011. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14012. SYCL_CHECK(
  14013. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  14014. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  14015. .memset(ctx->dev_ptr, value, buffer->size)
  14016. .wait()));
  14017. }
  14018. catch (sycl::exception const &exc) {
  14019. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14020. << ", line:" << __LINE__ << std::endl;
  14021. std::exit(1);
  14022. }
  14023. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  14024. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  14025. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  14026. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  14027. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  14028. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  14029. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  14030. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  14031. /* .clear = */ ggml_backend_sycl_buffer_clear,
  14032. /* .reset = */ NULL,
  14033. };
  14034. // sycl buffer type
  14035. struct ggml_backend_sycl_buffer_type_context {
  14036. int device;
  14037. std::string name;
  14038. };
  14039. struct ggml_backend_sycl_context {
  14040. int device;
  14041. std::string name;
  14042. };
  14043. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14044. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14045. return ctx->name.c_str();
  14046. }
  14047. GGML_CALL static ggml_backend_buffer_t
  14048. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  14049. size_t size) try {
  14050. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14051. ggml_sycl_set_device(buft_ctx->device);
  14052. const dpct::queue_ptr stream = g_syclStreams[buft_ctx->device][0];
  14053. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  14054. void * dev_ptr;
  14055. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  14056. size, *stream)));
  14057. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr);
  14058. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  14059. }
  14060. catch (sycl::exception const &exc) {
  14061. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14062. << ", line:" << __LINE__ << std::endl;
  14063. std::exit(1);
  14064. }
  14065. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14066. return 128;
  14067. UNUSED(buft);
  14068. }
  14069. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  14070. return dpct::get_current_device().get_max_mem_alloc_size();
  14071. UNUSED(buft);
  14072. }
  14073. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14074. size_t size = ggml_nbytes(tensor);
  14075. int64_t ne0 = tensor->ne[0];
  14076. if (ggml_is_quantized(tensor->type)) {
  14077. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14078. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14079. }
  14080. }
  14081. return size;
  14082. UNUSED(buft);
  14083. }
  14084. GGML_CALL static bool ggml_backend_sycl_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14085. if (!ggml_backend_is_sycl(backend)) {
  14086. return false;
  14087. }
  14088. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14089. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14090. return buft_ctx->device == sycl_ctx->device;
  14091. }
  14092. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  14093. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  14094. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  14095. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  14096. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  14097. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  14098. /* .supports_backend = */ ggml_backend_sycl_buffer_type_supports_backend,
  14099. /* .is_host = */ nullptr,
  14100. };
  14101. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device_index) {
  14102. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  14103. if (device_index>=g_device_count or device_index<0) {
  14104. 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",
  14105. device_index, g_device_count-1);
  14106. GGML_ASSERT(device_index<g_device_count);
  14107. }
  14108. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  14109. if (!g_ggml_backend_sycl_buffer_type_initialized) {
  14110. for (int i = 0; i < g_device_count; i++) {
  14111. ggml_backend_sycl_buffer_types[i] = {
  14112. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  14113. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(g_sycl_gpu_mgr->gpus[i])},
  14114. };
  14115. }
  14116. g_ggml_backend_sycl_buffer_type_initialized = true;
  14117. }
  14118. return &ggml_backend_sycl_buffer_types[device_index];
  14119. }
  14120. // sycl split buffer type
  14121. 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) {
  14122. const int64_t nrows = ggml_nrows(tensor);
  14123. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  14124. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  14125. *row_low -= *row_low % rounding;
  14126. if (id == g_device_count - 1) {
  14127. *row_high = nrows;
  14128. } else {
  14129. *row_high = nrows*tensor_split[id + 1];
  14130. *row_high -= *row_high % rounding;
  14131. }
  14132. }
  14133. struct ggml_backend_sycl_split_buffer_context {
  14134. ~ggml_backend_sycl_split_buffer_context() try {
  14135. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  14136. for (int i = 0; i < g_device_count; ++i) {
  14137. // int id = g_sycl_gpu_mgr->gpus[i];
  14138. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14139. if (extra->events[i][is] != nullptr) {
  14140. /*
  14141. DPCT1009:206: SYCL uses exceptions to report errors and
  14142. does not use the error codes. The original code was
  14143. commented out and a warning string was inserted. You
  14144. need to rewrite this code.
  14145. */
  14146. SYCL_CHECK(CHECK_TRY_ERROR(
  14147. dpct::destroy_event(extra->events[i][is])));
  14148. }
  14149. }
  14150. if (extra->data_device[i] != nullptr) {
  14151. /*
  14152. DPCT1009:207: SYCL uses exceptions to report errors and does
  14153. not use the error codes. The original code was commented out
  14154. and a warning string was inserted. You need to rewrite this
  14155. code.
  14156. */
  14157. ggml_sycl_set_device(i);
  14158. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  14159. extra->data_device[i], *g_syclStreams[i][0])));
  14160. }
  14161. }
  14162. delete extra;
  14163. }
  14164. }
  14165. catch (sycl::exception const &exc) {
  14166. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14167. << ", line:" << __LINE__ << std::endl;
  14168. std::exit(1);
  14169. }
  14170. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  14171. };
  14172. GGML_CALL static const char * ggml_backend_sycl_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  14173. return GGML_SYCL_NAME "_Split";
  14174. UNUSED(buffer);
  14175. }
  14176. // unused at the moment
  14177. //static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  14178. // return buffer->iface.get_name == ggml_backend_sycl_split_buffer_get_name;
  14179. //}
  14180. GGML_CALL static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14181. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14182. delete ctx;
  14183. }
  14184. GGML_CALL static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  14185. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  14186. return (void *)0x1000;
  14187. UNUSED(buffer);
  14188. }
  14189. GGML_CALL static void
  14190. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14191. ggml_tensor *tensor) try {
  14192. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  14193. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14194. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14195. const int64_t ne0 = tensor->ne[0];
  14196. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  14197. ctx->tensor_extras.push_back(extra);
  14198. for (int i = 0; i < g_device_count; ++i) {
  14199. // int id = g_sycl_gpu_mgr->gpus[i];
  14200. int64_t row_low, row_high;
  14201. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14202. int64_t nrows_split = row_high - row_low;
  14203. if (nrows_split == 0) {
  14204. continue;
  14205. }
  14206. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14207. const size_t original_size = size;
  14208. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14209. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14210. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14211. }
  14212. // FIXME: do not crash if cudaMalloc fails
  14213. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  14214. ggml_sycl_set_device(i);
  14215. char * buf;
  14216. /*
  14217. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  14218. error codes. The original code was commented out and a warning string
  14219. was inserted. You need to rewrite this code.
  14220. */
  14221. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  14222. size, *g_syclStreams[i][0])));
  14223. // set padding to 0 to avoid possible NaN values
  14224. if (size > original_size) {
  14225. /*
  14226. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  14227. the error codes. The original code was commented out and a warning
  14228. string was inserted. You need to rewrite this code.
  14229. */
  14230. SYCL_CHECK(CHECK_TRY_ERROR(
  14231. (*g_syclStreams[i][0])
  14232. .memset(buf + original_size, 0, size - original_size)
  14233. .wait()));
  14234. }
  14235. extra->data_device[i] = buf;
  14236. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14237. /*
  14238. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  14239. the error codes. The original code was commented out and a warning
  14240. string was inserted. You need to rewrite this code.
  14241. */
  14242. SYCL_CHECK(
  14243. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  14244. }
  14245. }
  14246. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  14247. tensor->extra = extra;
  14248. }
  14249. catch (sycl::exception const &exc) {
  14250. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14251. << ", line:" << __LINE__ << std::endl;
  14252. std::exit(1);
  14253. }
  14254. GGML_CALL static void
  14255. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14256. ggml_tensor *tensor, const void *data,
  14257. size_t offset, size_t size) try {
  14258. // split tensors must always be set in their entirety at once
  14259. GGML_ASSERT(offset == 0);
  14260. GGML_ASSERT(size == ggml_nbytes(tensor));
  14261. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14262. const int64_t ne0 = tensor->ne[0];
  14263. const size_t nb1 = tensor->nb[1];
  14264. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14265. for (int i = 0; i < g_device_count; ++i) {
  14266. // int id = g_sycl_gpu_mgr->gpus[i];
  14267. int64_t row_low, row_high;
  14268. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14269. int64_t nrows_split = row_high - row_low;
  14270. if (nrows_split == 0) {
  14271. continue;
  14272. }
  14273. const size_t offset_split = row_low*nb1;
  14274. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14275. const size_t original_size = size;
  14276. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14277. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14278. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14279. }
  14280. const char * buf_host = (const char *)data + offset_split;
  14281. /*
  14282. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  14283. error codes. The original code was commented out and a warning string
  14284. was inserted. You need to rewrite this code.
  14285. */
  14286. ggml_sycl_set_device(i);
  14287. SYCL_CHECK(CHECK_TRY_ERROR(
  14288. (*g_syclStreams[i][0])
  14289. .memcpy(extra->data_device[i], buf_host, original_size)
  14290. .wait()));
  14291. }
  14292. }
  14293. catch (sycl::exception const &exc) {
  14294. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14295. << ", line:" << __LINE__ << std::endl;
  14296. std::exit(1);
  14297. }
  14298. GGML_CALL static void
  14299. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14300. const ggml_tensor *tensor, void *data,
  14301. size_t offset, size_t size) try {
  14302. // split tensors must always be set in their entirety at once
  14303. GGML_ASSERT(offset == 0);
  14304. GGML_ASSERT(size == ggml_nbytes(tensor));
  14305. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14306. const int64_t ne0 = tensor->ne[0];
  14307. const size_t nb1 = tensor->nb[1];
  14308. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14309. for (int i = 0; i < g_device_count; ++i) {
  14310. // int id = g_sycl_gpu_mgr->gpus[i];
  14311. int64_t row_low, row_high;
  14312. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14313. int64_t nrows_split = row_high - row_low;
  14314. if (nrows_split == 0) {
  14315. continue;
  14316. }
  14317. const size_t offset_split = row_low*nb1;
  14318. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14319. const size_t original_size = size;
  14320. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14321. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14322. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14323. }
  14324. char * buf_host = (char *)data + offset_split;
  14325. /*
  14326. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  14327. error codes. The original code was commented out and a warning string
  14328. was inserted. You need to rewrite this code.
  14329. */
  14330. ggml_sycl_set_device(i);
  14331. SYCL_CHECK(CHECK_TRY_ERROR(
  14332. (*g_syclStreams[i][0])
  14333. .memcpy(buf_host, extra->data_device[i], original_size)
  14334. .wait()));
  14335. }
  14336. }
  14337. catch (sycl::exception const &exc) {
  14338. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14339. << ", line:" << __LINE__ << std::endl;
  14340. std::exit(1);
  14341. }
  14342. GGML_CALL static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  14343. UNUSED(buffer);
  14344. UNUSED(value);
  14345. }
  14346. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  14347. /* .get_name = */ ggml_backend_sycl_split_buffer_get_name,
  14348. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  14349. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  14350. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  14351. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  14352. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  14353. /* .cpy_tensor = */ NULL,
  14354. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  14355. /* .reset = */ NULL,
  14356. };
  14357. GGML_CALL static const char * ggml_backend_sycl_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14358. return GGML_SYCL_NAME "_Split";
  14359. UNUSED(buft);
  14360. }
  14361. GGML_CALL static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14362. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  14363. // instead, we allocate them for each tensor separately in init_tensor
  14364. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  14365. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  14366. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  14367. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  14368. }
  14369. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14370. return 128;
  14371. UNUSED(buft);
  14372. }
  14373. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14374. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  14375. size_t total_size = 0;
  14376. const int64_t ne0 = tensor->ne[0];
  14377. for (int i = 0; i < g_device_count; ++i) {
  14378. // int id = g_sycl_gpu_mgr->gpus[i];
  14379. int64_t row_low, row_high;
  14380. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  14381. int64_t nrows_split = row_high - row_low;
  14382. if (nrows_split == 0) {
  14383. continue;
  14384. }
  14385. total_size += ggml_nbytes_split(tensor, nrows_split);
  14386. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14387. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14388. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14389. }
  14390. }
  14391. return total_size;
  14392. }
  14393. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14394. return ggml_backend_is_sycl(backend);
  14395. UNUSED(buft);
  14396. }
  14397. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  14398. return false;
  14399. UNUSED(buft);
  14400. }
  14401. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  14402. /* .get_name = */ ggml_backend_sycl_split_buffer_type_name,
  14403. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  14404. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  14405. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  14406. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  14407. /* .supports_backend = */ ggml_backend_sycl_split_buffer_type_supports_backend,
  14408. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  14409. };
  14410. GGML_CALL ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  14411. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_split_buffer_type\n");
  14412. ggml_init_sycl();
  14413. // FIXME: this is not thread safe
  14414. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  14415. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  14416. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  14417. if (all_zero) {
  14418. tensor_split_arr = g_default_tensor_split;
  14419. } else {
  14420. float split_sum = 0.0f;
  14421. for (int i = 0; i < g_device_count; ++i) {
  14422. // int id = g_sycl_gpu_mgr->gpus[i];
  14423. tensor_split_arr[i] = split_sum;
  14424. split_sum += tensor_split[i];
  14425. }
  14426. for (int i = 0; i < g_device_count; ++i) {
  14427. // int id = g_sycl_gpu_mgr->gpus[i];
  14428. tensor_split_arr[i] /= split_sum;
  14429. }
  14430. }
  14431. auto it = buft_map.find(tensor_split_arr);
  14432. if (it != buft_map.end()) {
  14433. return &it->second;
  14434. }
  14435. struct ggml_backend_buffer_type buft {
  14436. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  14437. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  14438. };
  14439. auto result = buft_map.emplace(tensor_split_arr, buft);
  14440. return &result.first->second;
  14441. }
  14442. // host buffer type
  14443. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14444. return GGML_SYCL_NAME "_Host";
  14445. UNUSED(buft);
  14446. }
  14447. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  14448. return GGML_SYCL_NAME "_Host";
  14449. UNUSED(buffer);
  14450. }
  14451. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14452. ggml_sycl_host_free(buffer->context);
  14453. }
  14454. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14455. void * ptr = ggml_sycl_host_malloc(size);
  14456. if (ptr == nullptr) {
  14457. // fallback to cpu buffer
  14458. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  14459. }
  14460. // FIXME: this is a hack to avoid having to implement a new buffer type
  14461. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  14462. buffer->buft = buft;
  14463. buffer->iface.get_name = ggml_backend_sycl_host_buffer_name;
  14464. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  14465. return buffer;
  14466. }
  14467. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  14468. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_host_buffer_type\n");
  14469. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  14470. /* .iface = */ {
  14471. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  14472. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  14473. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  14474. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  14475. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  14476. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  14477. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  14478. },
  14479. /* .context = */ nullptr,
  14480. };
  14481. return &ggml_backend_sycl_buffer_type_host;
  14482. }
  14483. // backend
  14484. GGML_CALL static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  14485. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14486. return sycl_ctx->name.c_str();
  14487. }
  14488. GGML_CALL static void ggml_backend_sycl_free(ggml_backend_t backend) {
  14489. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14490. delete sycl_ctx;
  14491. delete backend;
  14492. }
  14493. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  14494. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14495. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  14496. }
  14497. GGML_CALL static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  14498. ggml_tensor *tensor,
  14499. const void *data, size_t offset,
  14500. size_t size) try {
  14501. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14502. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14503. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14504. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14505. (char *)tensor->data + offset, data, size).wait()));
  14506. }
  14507. catch (sycl::exception const &exc) {
  14508. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14509. << ", line:" << __LINE__ << std::endl;
  14510. std::exit(1);
  14511. }
  14512. GGML_CALL static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  14513. const ggml_tensor *tensor,
  14514. void *data, size_t offset,
  14515. size_t size) try {
  14516. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14517. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14518. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14519. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14520. data, (const char *)tensor->data + offset, size).wait()));
  14521. }
  14522. catch (sycl::exception const &exc) {
  14523. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14524. << ", line:" << __LINE__ << std::endl;
  14525. std::exit(1);
  14526. }
  14527. GGML_CALL static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  14528. const ggml_tensor *src,
  14529. ggml_tensor *dst) try {
  14530. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14531. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  14532. /*
  14533. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  14534. error codes. The original code was commented out and a warning string
  14535. was inserted. You need to rewrite this code.
  14536. */
  14537. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14538. dst->data, src->data, ggml_nbytes(dst)).wait()));
  14539. return true;
  14540. }
  14541. return false;
  14542. }
  14543. catch (sycl::exception const &exc) {
  14544. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14545. << ", line:" << __LINE__ << std::endl;
  14546. std::exit(1);
  14547. }
  14548. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  14549. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14550. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  14551. UNUSED(backend);
  14552. }
  14553. catch (sycl::exception const &exc) {
  14554. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14555. << ", line:" << __LINE__ << std::endl;
  14556. std::exit(1);
  14557. }
  14558. GGML_CALL static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  14559. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14560. ggml_sycl_set_main_device(sycl_ctx->device);
  14561. ggml_compute_params params = {};
  14562. params.type = GGML_TASK_TYPE_COMPUTE;
  14563. params.ith = 0;
  14564. for (int i = 0; i < cgraph->n_nodes; i++) {
  14565. ggml_tensor * node = cgraph->nodes[i];
  14566. 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) {
  14567. continue;
  14568. }
  14569. #ifndef NDEBUG
  14570. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14571. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14572. assert(node->extra != nullptr);
  14573. for (int j = 0; j < GGML_MAX_SRC; j++) {
  14574. if (node->src[j] != nullptr) {
  14575. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14576. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14577. assert(node->src[j]->extra != nullptr);
  14578. }
  14579. }
  14580. #endif
  14581. bool ok = ggml_sycl_compute_forward(&params, node);
  14582. if (!ok) {
  14583. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  14584. }
  14585. GGML_ASSERT(ok);
  14586. }
  14587. return GGML_STATUS_SUCCESS;
  14588. }
  14589. GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  14590. switch (op->op) {
  14591. case GGML_OP_UNARY:
  14592. switch (ggml_get_unary_op(op)) {
  14593. case GGML_UNARY_OP_GELU:
  14594. case GGML_UNARY_OP_SILU:
  14595. case GGML_UNARY_OP_RELU:
  14596. case GGML_UNARY_OP_HARDSIGMOID:
  14597. case GGML_UNARY_OP_HARDSWISH:
  14598. case GGML_UNARY_OP_GELU_QUICK:
  14599. case GGML_UNARY_OP_TANH:
  14600. return true;
  14601. default:
  14602. return false;
  14603. }
  14604. break;
  14605. case GGML_OP_MUL_MAT:
  14606. case GGML_OP_MUL_MAT_ID:
  14607. {
  14608. struct ggml_tensor * a;
  14609. struct ggml_tensor * b;
  14610. if (op->op == GGML_OP_MUL_MAT) {
  14611. a = op->src[0];
  14612. b = op->src[1];
  14613. } else {
  14614. a = op->src[2];
  14615. b = op->src[1];
  14616. }
  14617. if (a->ne[3] != b->ne[3]) {
  14618. return false;
  14619. }
  14620. ggml_type a_type = a->type;
  14621. if (a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ4_XS ||
  14622. a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ3_S ||
  14623. a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ2_S ||
  14624. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ1_M
  14625. ) {
  14626. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  14627. return false;
  14628. }
  14629. }
  14630. return true;
  14631. } break;
  14632. case GGML_OP_GET_ROWS:
  14633. {
  14634. switch (op->src[0]->type) {
  14635. case GGML_TYPE_F16:
  14636. case GGML_TYPE_F32:
  14637. case GGML_TYPE_Q4_0:
  14638. case GGML_TYPE_Q4_1:
  14639. case GGML_TYPE_Q5_0:
  14640. case GGML_TYPE_Q5_1:
  14641. case GGML_TYPE_Q8_0:
  14642. return true;
  14643. default:
  14644. return false;
  14645. }
  14646. } break;
  14647. case GGML_OP_CPY:
  14648. {
  14649. ggml_type src0_type = op->src[0]->type;
  14650. ggml_type src1_type = op->src[1]->type;
  14651. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  14652. return true;
  14653. }
  14654. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  14655. return true;
  14656. }
  14657. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  14658. return true;
  14659. }
  14660. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  14661. return true;
  14662. }
  14663. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  14664. return true;
  14665. }
  14666. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  14667. return true;
  14668. }
  14669. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  14670. return true;
  14671. }
  14672. return false;
  14673. } break;
  14674. case GGML_OP_CONCAT:
  14675. {
  14676. ggml_type src0_type = op->src[0]->type;
  14677. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  14678. } break;
  14679. case GGML_OP_DUP:
  14680. case GGML_OP_NONE:
  14681. case GGML_OP_RESHAPE:
  14682. case GGML_OP_REPEAT:
  14683. case GGML_OP_VIEW:
  14684. case GGML_OP_PERMUTE:
  14685. case GGML_OP_TRANSPOSE:
  14686. case GGML_OP_NORM:
  14687. case GGML_OP_ADD:
  14688. case GGML_OP_MUL:
  14689. case GGML_OP_DIV:
  14690. case GGML_OP_RMS_NORM:
  14691. case GGML_OP_SCALE:
  14692. case GGML_OP_SQR:
  14693. case GGML_OP_CLAMP:
  14694. case GGML_OP_CONT:
  14695. case GGML_OP_DIAG_MASK_INF:
  14696. case GGML_OP_SOFT_MAX:
  14697. case GGML_OP_ROPE:
  14698. case GGML_OP_IM2COL:
  14699. case GGML_OP_POOL_2D:
  14700. case GGML_OP_SUM_ROWS:
  14701. case GGML_OP_ARGSORT:
  14702. case GGML_OP_ACC:
  14703. case GGML_OP_GROUP_NORM:
  14704. case GGML_OP_UPSCALE:
  14705. case GGML_OP_PAD:
  14706. case GGML_OP_LEAKY_RELU:
  14707. return true;
  14708. default:
  14709. return false;
  14710. }
  14711. UNUSED(backend);
  14712. }
  14713. GGML_CALL static bool ggml_backend_sycl_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  14714. const int min_batch_size = 32;
  14715. return op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS && op->op != GGML_OP_MUL_MAT_ID;
  14716. GGML_UNUSED(backend);
  14717. }
  14718. static ggml_backend_i ggml_backend_sycl_interface = {
  14719. /* .get_name = */ ggml_backend_sycl_name,
  14720. /* .free = */ ggml_backend_sycl_free,
  14721. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  14722. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  14723. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  14724. /* .cpy_tensor_async = */ NULL, //ggml_backend_sycl_cpy_tensor_async, // TODO: update for the new interface
  14725. /* .synchronize = */ ggml_backend_sycl_synchronize,
  14726. /* .graph_plan_create = */ NULL,
  14727. /* .graph_plan_free = */ NULL,
  14728. /* .graph_plan_compute = */ NULL,
  14729. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  14730. /* .supports_op = */ ggml_backend_sycl_supports_op,
  14731. /* .offload_op = */ ggml_backend_sycl_offload_op,
  14732. /* .event_new = */ NULL,
  14733. /* .event_free = */ NULL,
  14734. /* .event_record = */ NULL,
  14735. /* .event_wait = */ NULL,
  14736. /* .event_synchronize = */ NULL,
  14737. };
  14738. static ggml_guid_t ggml_backend_sycl_guid() {
  14739. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  14740. return &guid;
  14741. }
  14742. GGML_CALL ggml_backend_t ggml_backend_sycl_init(int device) {
  14743. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_init\n");
  14744. ggml_init_sycl();
  14745. check_allow_gpu_index(device);
  14746. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  14747. ggml_sycl_set_main_device(device);
  14748. int id = g_sycl_gpu_mgr->gpus[device];
  14749. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context {
  14750. /* .device = */ device,
  14751. /* .name = */ GGML_SYCL_NAME + std::to_string(id),
  14752. };
  14753. ggml_backend_t sycl_backend = new ggml_backend {
  14754. /* .guid = */ ggml_backend_sycl_guid(),
  14755. /* .interface = */ ggml_backend_sycl_interface,
  14756. /* .context = */ ctx
  14757. };
  14758. return sycl_backend;
  14759. }
  14760. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  14761. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  14762. }
  14763. GGML_CALL int ggml_backend_sycl_get_device_count() {
  14764. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_count\n");
  14765. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14766. return g_sycl_gpu_mgr->get_gpu_count();
  14767. }
  14768. GGML_CALL static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  14769. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  14770. return sycl_backend;
  14771. UNUSED(params);
  14772. }
  14773. GGML_API GGML_CALL int ggml_backend_sycl_get_device_index(int device_id) {
  14774. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_index\n");
  14775. return g_sycl_gpu_mgr->get_index(device_id);
  14776. }
  14777. GGML_API GGML_CALL int ggml_backend_sycl_get_device_id(int device_index) {
  14778. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_id\n");
  14779. return g_sycl_gpu_mgr->gpus[device_index];
  14780. }
  14781. GGML_API GGML_CALL void ggml_backend_sycl_set_single_device_mode(int main_gpu_id) {
  14782. ggml_init_sycl();
  14783. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_single_device_mode\n");
  14784. fprintf(stderr, "ggml_backend_sycl_set_single_device: use single device: [%d]\n", main_gpu_id);
  14785. GGML_ASSERT(main_gpu_id<g_all_sycl_device_count);
  14786. if (g_sycl_gpu_mgr) {
  14787. delete g_sycl_gpu_mgr;
  14788. }
  14789. g_sycl_gpu_mgr = new sycl_gpu_mgr(main_gpu_id);
  14790. g_ggml_sycl_backend_gpu_mode = SYCL_SINGLE_GPU_MODE;
  14791. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  14792. g_ggml_backend_sycl_buffer_type_initialized = false;
  14793. }
  14794. GGML_API GGML_CALL void ggml_backend_sycl_set_mul_device_mode() {
  14795. ggml_init_sycl();
  14796. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_mul_device_mode\n");
  14797. if (g_ggml_sycl_backend_gpu_mode == SYCL_MUL_GPU_MODE) {
  14798. return;
  14799. }
  14800. fprintf(stderr, "ggml_backend_sycl_set_mul_device_mode: true\n");
  14801. if (g_sycl_gpu_mgr) {
  14802. delete g_sycl_gpu_mgr;
  14803. }
  14804. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14805. g_ggml_sycl_backend_gpu_mode = SYCL_MUL_GPU_MODE;
  14806. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  14807. g_ggml_backend_sycl_buffer_type_initialized = false;
  14808. }
  14809. extern "C" int ggml_backend_sycl_reg_devices();
  14810. int ggml_backend_sycl_reg_devices() {
  14811. ggml_backend_sycl_set_mul_device_mode();
  14812. assert(g_device_count>0);
  14813. for (int i = 0; i < g_device_count; i++) {
  14814. int id = g_sycl_gpu_mgr->gpus[i];
  14815. char name[128];
  14816. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, id);
  14817. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  14818. }
  14819. return g_device_count;
  14820. }