ggml-sycl.cpp 673 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429
  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. template <typename T,
  2738. sycl::access::address_space addressSpace =
  2739. sycl::access::address_space::global_space,
  2740. sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
  2741. sycl::memory_scope memoryScope = sycl::memory_scope::device>
  2742. inline T atomic_fetch_add(T *addr, T operand) {
  2743. auto atm =
  2744. sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
  2745. return atm.fetch_add(operand);
  2746. }
  2747. template <sycl::access::address_space addressSpace =
  2748. sycl::access::address_space::global_space,
  2749. sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
  2750. sycl::memory_scope memoryScope = sycl::memory_scope::device,
  2751. typename T1, typename T2>
  2752. inline T1 atomic_fetch_add(T1 *addr, T2 operand) {
  2753. auto atm =
  2754. sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
  2755. return atm.fetch_add(operand);
  2756. }
  2757. template <typename T, sycl::access::address_space addressSpace =
  2758. sycl::access::address_space::global_space>
  2759. inline T atomic_fetch_add(T *addr, T operand,
  2760. sycl::memory_order memoryOrder) {
  2761. switch (memoryOrder) {
  2762. case sycl::memory_order::relaxed:
  2763. return atomic_fetch_add<T, addressSpace, sycl::memory_order::relaxed,
  2764. sycl::memory_scope::device>(addr, operand);
  2765. case sycl::memory_order::acq_rel:
  2766. return atomic_fetch_add<T, addressSpace, sycl::memory_order::acq_rel,
  2767. sycl::memory_scope::device>(addr, operand);
  2768. case sycl::memory_order::seq_cst:
  2769. return atomic_fetch_add<T, addressSpace, sycl::memory_order::seq_cst,
  2770. sycl::memory_scope::device>(addr, operand);
  2771. default:
  2772. assert(false && "Invalid memory_order for atomics. Valid memory_order for "
  2773. "atomics are: sycl::memory_order::relaxed, "
  2774. "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
  2775. }
  2776. }
  2777. template <sycl::access::address_space addressSpace =
  2778. sycl::access::address_space::global_space,
  2779. typename T1, typename T2>
  2780. inline T1 atomic_fetch_add(T1 *addr, T2 operand,
  2781. sycl::memory_order memoryOrder) {
  2782. atomic_fetch_add<T1, addressSpace>(addr, operand, memoryOrder);
  2783. }
  2784. } // COPY from DPCT head files
  2785. #define GGML_COMMON_DECL_SYCL
  2786. #define GGML_COMMON_IMPL_SYCL
  2787. #include "ggml-common.h"
  2788. static int g_ggml_sycl_debug=0;
  2789. #define GGML_SYCL_DEBUG(...) do{if(g_ggml_sycl_debug) fprintf(stderr, __VA_ARGS__);}while(0)
  2790. #define CHECK_TRY_ERROR(expr) \
  2791. [&]() { \
  2792. try { \
  2793. expr; \
  2794. return dpct::success; \
  2795. } catch (std::exception const &e) { \
  2796. std::cerr << e.what()<< "\nException caught at file:" << __FILE__ \
  2797. << ", line:" << __LINE__ <<", func:"<<__func__<< std::endl; \
  2798. return dpct::default_error; \
  2799. } \
  2800. }()
  2801. // #define DEBUG_SYCL_MALLOC
  2802. static int g_work_group_size = 0;
  2803. // typedef sycl::half ggml_fp16_t;
  2804. #define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
  2805. #define VER_4VEC 130 //todo for hardward optimize.
  2806. #define VER_GEN9 700 //todo for hardward optimize.
  2807. #define VER_GEN12 1000000 //todo for hardward optimize.
  2808. #define VER_GEN13 (VER_GEN12 + 1030) //todo for hardward optimize.
  2809. #define GGML_SYCL_MAX_NODES 8192 //TODO: adapt to hardwares
  2810. #if !defined(GGML_SYCL_FORCE_MMQ)
  2811. #define SYCL_USE_XMX
  2812. #endif
  2813. // max batch size to use MMQ kernels when tensor cores are available
  2814. #define MMQ_MAX_BATCH_SIZE 32
  2815. #if defined(_MSC_VER)
  2816. #pragma warning(disable: 4244 4267) // possible loss of data
  2817. #endif
  2818. // dmmv = dequantize_mul_mat_vec
  2819. #ifndef GGML_SYCL_DMMV_X
  2820. #define GGML_SYCL_DMMV_X 32
  2821. #endif
  2822. #ifndef GGML_SYCL_MMV_Y
  2823. #define GGML_SYCL_MMV_Y 1
  2824. #endif
  2825. enum ggml_sycl_backend_gpu_mode {
  2826. SYCL_UNSET_GPU_MODE = -1,
  2827. SYCL_SINGLE_GPU_MODE = 0,
  2828. SYCL_MUL_GPU_MODE
  2829. };
  2830. static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  2831. static void crash(){
  2832. int *ptr = NULL;
  2833. *ptr = 0;
  2834. }
  2835. static void ggml_sycl_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  2836. fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
  2837. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  2838. GGML_ASSERT(!"SYCL error");
  2839. }
  2840. #define SYCL_CHECK(err) do { \
  2841. auto err_ = (err); if (err_ != 0) ggml_sycl_error( \
  2842. #err, __func__, __FILE__, __LINE__, \
  2843. "Meet error in this line code!"); \
  2844. } while (0)
  2845. #if DPCT_COMPAT_RT_VERSION >= 11100
  2846. #define GGML_SYCL_ASSUME(x) __builtin_assume(x)
  2847. #else
  2848. #define GGML_SYCL_ASSUME(x)
  2849. #endif // DPCT_COMPAT_RT_VERSION >= 11100
  2850. #ifdef GGML_SYCL_F16
  2851. typedef sycl::half dfloat; // dequantize float
  2852. typedef sycl::half2 dfloat2;
  2853. #else
  2854. typedef float dfloat; // dequantize float
  2855. typedef sycl::float2 dfloat2;
  2856. #endif //GGML_SYCL_F16
  2857. #define MMVQ_MAX_BATCH_SIZE 8
  2858. static const int8_t kvalues_iq4nl[16]={-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2859. bool ggml_sycl_loaded(void);
  2860. void * ggml_sycl_host_malloc(size_t size);
  2861. void ggml_sycl_host_free(void * ptr);
  2862. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  2863. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  2864. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor);
  2865. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  2866. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  2867. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  2868. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  2869. void ggml_sycl_set_main_device(int main_device);
  2870. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  2871. void ggml_sycl_set_scratch_size(size_t scratch_size);
  2872. void ggml_sycl_free_scratch(void);
  2873. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  2874. bool ggml_backend_is_sycl(ggml_backend_t backend);
  2875. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  2876. int get_main_device();
  2877. static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer);
  2878. void print_ggml_tensor(const char*name, struct ggml_tensor *src);
  2879. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt);
  2880. void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
  2881. const void *ptr_src, size_t size) {
  2882. char *host_buf = (char *)malloc(size);
  2883. q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
  2884. q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
  2885. free(host_buf);
  2886. }
  2887. static __dpct_inline__ int get_int_from_int8(const int8_t *x8, const int &i32) {
  2888. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2889. int x32 = 0;
  2890. x32 |= x16[0] << 0;
  2891. x32 |= x16[1] << 16;
  2892. return x32;
  2893. }
  2894. static __dpct_inline__ int get_int_from_uint8(const uint8_t *x8,
  2895. const int &i32) {
  2896. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2897. int x32 = 0;
  2898. x32 |= x16[0] << 0;
  2899. x32 |= x16[1] << 16;
  2900. return x32;
  2901. }
  2902. static __dpct_inline__ int get_int_from_int8_aligned(const int8_t *x8,
  2903. const int &i32) {
  2904. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2905. }
  2906. static __dpct_inline__ int get_int_from_uint8_aligned(const uint8_t *x8,
  2907. const int &i32) {
  2908. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2909. }
  2910. template <typename T>
  2911. using to_t_sycl_t = void (*)(const void *__restrict__ x, T *__restrict__ y,
  2912. int k, dpct::queue_ptr stream);
  2913. typedef to_t_sycl_t<float> to_fp32_sycl_t;
  2914. typedef to_t_sycl_t<sycl::half> to_fp16_sycl_t;
  2915. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  2916. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  2917. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  2918. typedef void (*ggml_sycl_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  2919. typedef void (*ggml_sycl_op_mul_mat_t)(
  2920. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2921. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2922. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2923. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2924. const dpct::queue_ptr &stream);
  2925. typedef void (*ggml_sycl_op_flatten_t)(const ggml_tensor *src0,
  2926. const ggml_tensor *src1,
  2927. ggml_tensor *dst, const float *src0_dd,
  2928. const float *src1_dd, float *dst_dd,
  2929. const dpct::queue_ptr &main_stream);
  2930. typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  2931. typedef void (*allocate_tiles_sycl_t)(int **x_ql, sycl::half2 **x_dm,
  2932. int **x_qh, int **x_sc);
  2933. typedef void (*load_tiles_sycl_t)(const void *__restrict__ vx,
  2934. int *__restrict__ x_ql,
  2935. sycl::half2 *__restrict__ x_dm,
  2936. int *__restrict__ x_qh,
  2937. int *__restrict__ x_sc, const int &i_offset,
  2938. const int &i_max, const int &k,
  2939. const int &blocks_per_row);
  2940. typedef float (*vec_dot_q_mul_mat_sycl_t)(
  2941. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  2942. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  2943. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ms,
  2944. const int &i, const int &j, const int &k);
  2945. #define WARP_SIZE 32
  2946. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  2947. #define SYCL_GELU_BLOCK_SIZE 256
  2948. #define SYCL_SILU_BLOCK_SIZE 256
  2949. #define SYCL_TANH_BLOCK_SIZE 256
  2950. #define SYCL_RELU_BLOCK_SIZE 256
  2951. #define SYCL_HARDSIGMOID_BLOCK_SIZE 256
  2952. #define SYCL_HARDSWISH_BLOCK_SIZE 256
  2953. #define SYCL_SQR_BLOCK_SIZE 256
  2954. #define SYCL_CPY_BLOCK_SIZE 32
  2955. #define SYCL_SCALE_BLOCK_SIZE 256
  2956. #define SYCL_CLAMP_BLOCK_SIZE 256
  2957. #define SYCL_ROPE_BLOCK_SIZE 256
  2958. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  2959. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  2960. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  2961. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  2962. #define SYCL_UPSCALE_BLOCK_SIZE 256
  2963. #define SYCL_CONCAT_BLOCK_SIZE 256
  2964. #define SYCL_PAD_BLOCK_SIZE 256
  2965. #define SYCL_ACC_BLOCK_SIZE 256
  2966. #define SYCL_IM2COL_BLOCK_SIZE 256
  2967. #define SYCL_POOL2D_BLOCK_SIZE 256
  2968. // dmmv = dequantize_mul_mat_vec
  2969. #ifndef GGML_SYCL_DMMV_X
  2970. #define GGML_SYCL_DMMV_X 32
  2971. #endif
  2972. #ifndef GGML_SYCL_MMV_Y
  2973. #define GGML_SYCL_MMV_Y 1
  2974. #endif
  2975. #ifndef K_QUANTS_PER_ITERATION
  2976. #define K_QUANTS_PER_ITERATION 2
  2977. #else
  2978. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  2979. #endif
  2980. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  2981. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  2982. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  2983. #define MUL_MAT_SRC1_COL_STRIDE 128
  2984. #define MAX_STREAMS 8
  2985. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {{0}};
  2986. struct ggml_tensor_extra_gpu {
  2987. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  2988. dpct::event_ptr
  2989. events[GGML_SYCL_MAX_DEVICES]
  2990. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  2991. };
  2992. class sycl_gpu_mgr {
  2993. public:
  2994. std::vector<int> gpus;
  2995. std::vector<sycl::device> devices;
  2996. sycl::queue *first_queue;
  2997. sycl::context co_ctx;
  2998. int max_compute_units = 0;
  2999. int work_group_size = 0;
  3000. std::string gpus_list = "";
  3001. /*
  3002. Use all GPUs with same top max compute units
  3003. */
  3004. sycl_gpu_mgr() {
  3005. detect_sycl_gpu_list_with_max_cu();
  3006. get_allow_gpus();
  3007. create_context_with_gpus();
  3008. }
  3009. /*
  3010. Only use the assigned GPU
  3011. */
  3012. sycl_gpu_mgr(int main_gpu_id) {
  3013. sycl::device device = dpct::dev_mgr::instance().get_device(main_gpu_id);
  3014. dpct::device_info prop;
  3015. dpct::get_device_info(prop, device);
  3016. gpus.push_back(main_gpu_id);
  3017. devices.push_back(device);
  3018. work_group_size = prop.get_max_work_group_size();
  3019. max_compute_units = prop.get_max_compute_units();
  3020. get_allow_gpus();
  3021. create_context_with_gpus();
  3022. }
  3023. void create_context_with_gpus() {
  3024. sycl::context ctx = sycl::context(devices);
  3025. assert(gpus.size() > 0);
  3026. first_queue = dpct::get_current_device().create_queue(ctx, devices[0]);
  3027. co_ctx = first_queue->get_context();
  3028. }
  3029. sycl::context &get_co_ctx() { return co_ctx; }
  3030. void get_allow_gpus() {
  3031. gpus_list = "";
  3032. for (size_t i = 0; i < gpus.size(); ++i) {
  3033. gpus_list += std::to_string(gpus[i]);
  3034. gpus_list += ",";
  3035. }
  3036. if (gpus_list.length() > 1) {
  3037. gpus_list.pop_back();
  3038. }
  3039. }
  3040. bool is_allowed_gpu(int device_id) {
  3041. return std::find(gpus.begin(), gpus.end(), device_id) != gpus.end();
  3042. }
  3043. void detect_sycl_gpu_list_with_max_cu() try {
  3044. int device_count = dpct::dev_mgr::instance().device_count();
  3045. for (int id = 0; id < device_count; id++) {
  3046. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3047. if (!device.is_gpu())
  3048. continue;
  3049. dpct::device_info prop;
  3050. dpct::get_device_info(prop, device);
  3051. if (max_compute_units < prop.get_max_compute_units())
  3052. max_compute_units = prop.get_max_compute_units();
  3053. }
  3054. for (int id = 0; id < device_count; id++) {
  3055. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3056. if (!device.is_gpu())
  3057. continue;
  3058. dpct::device_info prop;
  3059. dpct::get_device_info(prop, device);
  3060. if (max_compute_units == prop.get_max_compute_units() &&
  3061. is_ext_oneapi_device(device)) {
  3062. gpus.push_back(id);
  3063. devices.push_back(device);
  3064. work_group_size = prop.get_max_work_group_size();
  3065. }
  3066. }
  3067. return;
  3068. } catch (sycl::exception const &exc) {
  3069. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3070. << ", line:" << __LINE__ << std::endl;
  3071. std::exit(1);
  3072. }
  3073. int get_gpu_count() { return (int)gpus.size(); }
  3074. int get_index(int id) {
  3075. for (int i = 0; i < (int)gpus.size(); i++) {
  3076. if (gpus[i] == id)
  3077. return i;
  3078. }
  3079. printf("miss to get device index by id=%d\n", id);
  3080. GGML_ASSERT(false);
  3081. }
  3082. int get_next_index(int id) {
  3083. int cur_index = get_index(id);
  3084. for (int i = cur_index + 1; i < (int)gpus.size(); i++) {
  3085. if (gpus[i] == id)
  3086. return i;
  3087. }
  3088. GGML_ASSERT(false);
  3089. }
  3090. bool is_ext_oneapi_device(const sycl::device &dev) {
  3091. sycl::backend dev_backend = dev.get_backend();
  3092. if (dev_backend == sycl::backend::ext_oneapi_level_zero ||
  3093. dev_backend == sycl::backend::ext_oneapi_cuda ||
  3094. dev_backend == sycl::backend::ext_oneapi_hip)
  3095. return true;
  3096. return false;
  3097. }
  3098. };
  3099. static sycl_gpu_mgr *g_sycl_gpu_mgr = NULL;
  3100. static int g_device_count = -1;
  3101. static int g_all_sycl_device_count = -1;
  3102. static int g_main_device = -1;
  3103. static int g_main_device_id = -1;
  3104. static bool g_ggml_backend_sycl_buffer_type_initialized = false;
  3105. static std::array<float, GGML_SYCL_MAX_DEVICES> g_default_tensor_split = {};
  3106. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  3107. static ggml_sycl_backend_gpu_mode g_ggml_sycl_backend_gpu_mode = SYCL_UNSET_GPU_MODE;
  3108. struct sycl_device_capabilities {
  3109. int cc; // compute capability
  3110. bool vmm; // virtual memory support
  3111. size_t vmm_granularity; // granularity of virtual memory
  3112. int device_id;
  3113. };
  3114. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3115. struct sycl_device_id2index {
  3116. int index;
  3117. };
  3118. static void * g_scratch_buffer = nullptr;
  3119. static size_t g_scratch_size = 0; // disabled by default
  3120. static size_t g_scratch_offset = 0;
  3121. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3122. int get_main_device(){
  3123. return g_main_device;
  3124. }
  3125. [[noreturn]]
  3126. static void bad_arch(const sycl::stream &stream_ct1) {
  3127. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3128. "current GPU architecture.\n";
  3129. // __trap();
  3130. std::exit(1);
  3131. (void) bad_arch; // suppress unused function warning
  3132. }
  3133. /*
  3134. device_index: device index from 0 to n (continue numbers).
  3135. It is used for device select/set in SYCL backend internal data structure.
  3136. */
  3137. void check_allow_gpu_index(const int device_index) {
  3138. if (device_index >= g_device_count) {
  3139. char error_buf[256];
  3140. snprintf(error_buf, sizeof(error_buf),
  3141. "%s error: device_index:%d is out of range: [0-%d]", __func__,
  3142. device_index, g_device_count - 1);
  3143. fprintf(stderr, "%s\n", error_buf);
  3144. assert(false);
  3145. }
  3146. }
  3147. /*
  3148. device_id: device ID is shown by ggml_backend_sycl_print_sycl_devices().
  3149. It is only used to set current working device.
  3150. */
  3151. void check_allow_gpu_id(const int device_id) {
  3152. if (!g_sycl_gpu_mgr->is_allowed_gpu(device_id)) {
  3153. char error_buf[256];
  3154. snprintf(error_buf, sizeof(error_buf),
  3155. "error: cannot set device=%d, which is not allowed. Please "
  3156. "set GPU ID in: [%s]",
  3157. device_id, g_sycl_gpu_mgr->gpus_list.c_str());
  3158. fprintf(stderr, "%s\n", error_buf);
  3159. throw std::invalid_argument(error_buf);
  3160. }
  3161. }
  3162. int get_current_device_id() {
  3163. return dpct::dev_mgr::instance().current_device_id();
  3164. }
  3165. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  3166. int device_id = g_sycl_gpu_mgr->gpus[device];
  3167. check_allow_gpu_id(device_id);
  3168. int current_device_id;
  3169. SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
  3170. // GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
  3171. // current_device_id=%d\n", device, current_device);
  3172. if (device_id == current_device_id) {
  3173. return 0;
  3174. }
  3175. return CHECK_TRY_ERROR(dpct::select_device(device_id));
  3176. } catch (sycl::exception const &exc) {
  3177. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3178. << ", line:" << __LINE__ << std::endl;
  3179. crash();
  3180. std::exit(1);
  3181. }
  3182. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3183. if(!g_ggml_sycl_debug) return;
  3184. if(!src){
  3185. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3186. return;
  3187. }
  3188. char filename[1024];
  3189. sprintf(filename, "%s.txt", name);
  3190. printf("GGML Tensor:%s save to %s\n", name, filename);
  3191. size_t total_size = total_elements*sizeof(float);
  3192. float *local_buf = NULL;
  3193. if(src_on_device) {
  3194. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3195. ggml_sycl_set_device(g_main_device);
  3196. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3197. main_stream->memcpy(local_buf, src, total_size).wait();
  3198. }
  3199. else {
  3200. local_buf = (float *)src;
  3201. }
  3202. std::ofstream logfile;
  3203. logfile.open(filename);
  3204. for(size_t i=0; i<total_elements; i++){
  3205. logfile << local_buf[i] <<" ";
  3206. if((i+1)%20 ==0) logfile <<std::endl;
  3207. }
  3208. logfile <<std::endl;
  3209. logfile.close();
  3210. if(src_on_device) ggml_sycl_host_free(local_buf);
  3211. }
  3212. void log_ggml_var_device_fp16(const char*name, sycl::half *src, size_t total_elements, bool src_on_device){
  3213. if(!g_ggml_sycl_debug) return;
  3214. if(!src){
  3215. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3216. return;
  3217. }
  3218. char filename[1024];
  3219. sprintf(filename, "%s.txt", name);
  3220. printf("GGML Tensor:%s save to %s\n", name, filename);
  3221. size_t total_size = total_elements*sizeof(sycl::half);
  3222. sycl::half *local_buf = NULL;
  3223. if(src_on_device) {
  3224. local_buf = (sycl::half *) ggml_sycl_host_malloc(total_size);
  3225. ggml_sycl_set_device(g_main_device);
  3226. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3227. main_stream->memcpy(local_buf, src, total_size).wait();
  3228. }
  3229. else {
  3230. local_buf = (sycl::half *)src;
  3231. }
  3232. std::ofstream logfile;
  3233. logfile.open(filename);
  3234. for(size_t i=0; i<total_elements; i++){
  3235. logfile << local_buf[i] <<" ";
  3236. if((i+1)%20 ==0) logfile <<std::endl;
  3237. }
  3238. logfile <<std::endl;
  3239. logfile.close();
  3240. if(src_on_device) ggml_sycl_host_free(local_buf);
  3241. }
  3242. //todo: debug for crash in some case
  3243. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3244. if(!g_ggml_sycl_debug) return;
  3245. if(!src){
  3246. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3247. return;
  3248. }
  3249. size_t total_elements = ggml_nelements(src);
  3250. const bool src_on_device = src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  3251. float *src_data =NULL;
  3252. if(src_on_device) {
  3253. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3254. src_data = (float*)src_extra->data_device[g_main_device];
  3255. }
  3256. else {
  3257. src_data = (float *)src->data;
  3258. }
  3259. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3260. }
  3261. static int log_file_name_idx=0;
  3262. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3263. stop_cnt = 4;
  3264. if(log_file_name_idx>=stop_cnt) return;
  3265. char filename[1280];
  3266. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3267. log_file_name_idx++;
  3268. print_ggml_tensor(filename, src);
  3269. }
  3270. static __dpct_inline__ float warp_reduce_sum(float x,
  3271. const sycl::nd_item<3> &item_ct1) {
  3272. #pragma unroll
  3273. for (int mask = 16; mask > 0; mask >>= 1) {
  3274. /*
  3275. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3276. kernel that calls this function may be less than "32". The function
  3277. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3278. CPU device. Modify the size of the work-group to ensure that the value
  3279. of the right-most dimension is a multiple of "32".
  3280. */
  3281. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3282. }
  3283. return x;
  3284. }
  3285. static __dpct_inline__ sycl::float2
  3286. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3287. #pragma unroll
  3288. for (int mask = 16; mask > 0; mask >>= 1) {
  3289. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3290. mask);
  3291. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3292. mask);
  3293. }
  3294. return a;
  3295. }
  3296. static __dpct_inline__ float warp_reduce_max(float x,
  3297. const sycl::nd_item<3> &item_ct1) {
  3298. #pragma unroll
  3299. for (int mask = 16; mask > 0; mask >>= 1) {
  3300. /*
  3301. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3302. kernel that calls this function may be less than "32". The function
  3303. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3304. CPU device. Modify the size of the work-group to ensure that the value
  3305. of the right-most dimension is a multiple of "32".
  3306. */
  3307. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3308. item_ct1.get_sub_group(), x, mask));
  3309. }
  3310. return x;
  3311. }
  3312. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3313. return b;
  3314. GGML_UNUSED(a);
  3315. }
  3316. static __dpct_inline__ float op_add(const float a, const float b) {
  3317. return a + b;
  3318. }
  3319. static __dpct_inline__ float op_mul(const float a, const float b) {
  3320. return a * b;
  3321. }
  3322. static __dpct_inline__ float op_div(const float a, const float b) {
  3323. return a / b;
  3324. }
  3325. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3326. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3327. int ne0, int ne1, int ne2, int ne3,
  3328. int ne10, int ne11, int ne12, int ne13,
  3329. /*int s0, */ int s1, int s2, int s3,
  3330. /*int s10,*/ int s11, int s12, int s13,
  3331. const sycl::nd_item<3> &item_ct1) {
  3332. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3333. item_ct1.get_local_id(2);
  3334. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3335. item_ct1.get_local_id(1));
  3336. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3337. item_ct1.get_local_id(0)) /
  3338. ne3;
  3339. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3340. item_ct1.get_local_id(0)) %
  3341. ne3;
  3342. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3343. return;
  3344. }
  3345. const int i11 = i1 % ne11;
  3346. const int i12 = i2 % ne12;
  3347. const int i13 = i3 % ne13;
  3348. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3349. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3350. const size_t i_dst = i_src0;
  3351. const src0_t * src0_row = src0 + i_src0;
  3352. const src1_t * src1_row = src1 + i_src1;
  3353. dst_t * dst_row = dst + i_dst;
  3354. for (int i0 = i0s; i0 < ne0;
  3355. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3356. const int i10 = i0 % ne10;
  3357. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3358. }
  3359. }
  3360. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3361. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3362. int ne0, int ne1, int ne2, int ne3,
  3363. int ne10, int ne11, int ne12, int ne13,
  3364. /*int s0, */ int s1, int s2, int s3,
  3365. /*int s10,*/ int s11, int s12, int s13,
  3366. const sycl::nd_item<3> &item_ct1) {
  3367. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3368. item_ct1.get_local_id(2);
  3369. const int i3 = i/(ne2*ne1*ne0);
  3370. const int i2 = (i/(ne1*ne0)) % ne2;
  3371. const int i1 = (i/ne0) % ne1;
  3372. const int i0 = i % ne0;
  3373. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3374. return;
  3375. }
  3376. const int i11 = i1 % ne11;
  3377. const int i12 = i2 % ne12;
  3378. const int i13 = i3 % ne13;
  3379. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3380. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3381. const size_t i_dst = i_src0;
  3382. const src0_t * src0_row = src0 + i_src0;
  3383. const src1_t * src1_row = src1 + i_src1;
  3384. dst_t * dst_row = dst + i_dst;
  3385. const int i10 = i0 % ne10;
  3386. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3387. }
  3388. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3389. const int ne10, const int ne11, const int ne12,
  3390. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3391. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3392. item_ct1.get_local_id(2);
  3393. if (i >= ne) {
  3394. return;
  3395. }
  3396. int src1_idx = i - offset;
  3397. int oz = src1_idx / nb2;
  3398. int oy = (src1_idx - (oz * nb2)) / nb1;
  3399. int ox = src1_idx % nb1;
  3400. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3401. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3402. } else {
  3403. dst[i] = x[i];
  3404. }
  3405. }
  3406. static void gelu_f32(const float * x, float * dst, const int k,
  3407. const sycl::nd_item<3> &item_ct1) {
  3408. const float GELU_COEF_A = 0.044715f;
  3409. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  3410. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3411. item_ct1.get_local_id(2);
  3412. if (i >= k) {
  3413. return;
  3414. }
  3415. float xi = x[i];
  3416. dst[i] = 0.5f * xi *
  3417. (1.0f +
  3418. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3419. }
  3420. static void silu_f32(const float * x, float * dst, const int k,
  3421. const sycl::nd_item<3> &item_ct1) {
  3422. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3423. item_ct1.get_local_id(2);
  3424. if (i >= k) {
  3425. return;
  3426. }
  3427. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3428. }
  3429. static void gelu_quick_f32(const float *x, float *dst, int k,
  3430. const sycl::nd_item<3> &item_ct1) {
  3431. const float GELU_QUICK_COEF = -1.702f;
  3432. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3433. item_ct1.get_local_id(2);
  3434. if (i >= k) {
  3435. return;
  3436. }
  3437. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3438. }
  3439. static void tanh_f32(const float *x, float *dst, int k,
  3440. const sycl::nd_item<3> &item_ct1) {
  3441. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3442. item_ct1.get_local_id(2);
  3443. if (i >= k) {
  3444. return;
  3445. }
  3446. dst[i] = sycl::tanh((float)(x[i]));
  3447. }
  3448. static void relu_f32(const float * x, float * dst, const int k,
  3449. const sycl::nd_item<3> &item_ct1) {
  3450. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3451. item_ct1.get_local_id(2);
  3452. if (i >= k) {
  3453. return;
  3454. }
  3455. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3456. }
  3457. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  3458. const sycl::nd_item<3> &item_ct1) {
  3459. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3460. item_ct1.get_local_id(2);
  3461. if (i >= k) {
  3462. return;
  3463. }
  3464. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3465. }
  3466. static void hardswish_f32(const float * x, float * dst, const int k,
  3467. const sycl::nd_item<3> &item_ct1) {
  3468. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3469. item_ct1.get_local_id(2);
  3470. if (i >= k) {
  3471. return;
  3472. }
  3473. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3474. }
  3475. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3476. const sycl::nd_item<3> &item_ct1) {
  3477. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3478. item_ct1.get_local_id(2);
  3479. if (i >= k) {
  3480. return;
  3481. }
  3482. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3483. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3484. }
  3485. static void sqr_f32(const float * x, float * dst, const int k,
  3486. const sycl::nd_item<3> &item_ct1) {
  3487. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3488. item_ct1.get_local_id(2);
  3489. if (i >= k) {
  3490. return;
  3491. }
  3492. dst[i] = x[i] * x[i];
  3493. }
  3494. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3495. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3496. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3497. item_ct1.get_local_id(1);
  3498. const int tid = item_ct1.get_local_id(2);
  3499. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3500. for (int col = tid; col < ncols; col += block_size) {
  3501. const float xi = x[row*ncols + col];
  3502. mean_var.x() += xi;
  3503. mean_var.y() += xi * xi;
  3504. }
  3505. // sum up partial sums
  3506. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3507. if (block_size > WARP_SIZE) {
  3508. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3509. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3510. if (lane_id == 0) {
  3511. s_sum[warp_id] = mean_var;
  3512. }
  3513. /*
  3514. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3515. converged control flow. You may need to adjust the code.
  3516. */
  3517. item_ct1.barrier(sycl::access::fence_space::local_space);
  3518. mean_var = s_sum[lane_id];
  3519. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3520. }
  3521. const float mean = mean_var.x() / ncols;
  3522. const float var = mean_var.y() / ncols - mean * mean;
  3523. const float inv_std = sycl::rsqrt(var + eps);
  3524. for (int col = tid; col < ncols; col += block_size) {
  3525. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3526. }
  3527. }
  3528. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3529. const sycl::nd_item<3> &item_ct1) {
  3530. int nidx = item_ct1.get_local_id(2) +
  3531. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3532. if (nidx >= ne0) {
  3533. return;
  3534. }
  3535. // operation
  3536. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3537. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3538. if (item_ct1.get_group(0) < ne02) { // src0
  3539. int offset_src =
  3540. nidx + item_ct1.get_group(1) * ne0 +
  3541. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3542. dst[offset_dst] = x[offset_src];
  3543. } else {
  3544. int offset_src =
  3545. nidx + item_ct1.get_group(1) * ne0 +
  3546. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3547. dst[offset_dst] = y[offset_src];
  3548. }
  3549. }
  3550. static void upscale_f32(const float *x, float *dst, const int nb00, const int nb01,
  3551. const int nb02, const int nb03, const int ne10, const int ne11,
  3552. const int ne12, const int ne13, const float sf0, const float sf1,
  3553. const float sf2, const float sf3, const sycl::nd_item<1> &item_ct1) {
  3554. int index = item_ct1.get_local_id(0) +
  3555. item_ct1.get_group(0) * item_ct1.get_local_range(0);
  3556. if (index >= ne10 * ne11 * ne12 * ne13) {
  3557. return;
  3558. }
  3559. // operation
  3560. int i10 = index % ne10;
  3561. int i11 = (index / ne10) % ne11;
  3562. int i12 = (index / (ne10 * ne11)) % ne12;
  3563. int i13 = (index / (ne10 * ne11 * ne12)) % ne13;
  3564. int i00 = i10 / sf0;
  3565. int i01 = i11 / sf1;
  3566. int i02 = i12 / sf2;
  3567. int i03 = i13 / sf3;
  3568. dst[index] = *(float *)((char *)x + i03 * nb03 + i02 * nb02 + i01 * nb01 + i00 * nb00);
  3569. }
  3570. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3571. const sycl::nd_item<3> &item_ct1) {
  3572. int nidx = item_ct1.get_local_id(2) +
  3573. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3574. if (nidx >= ne0) {
  3575. return;
  3576. }
  3577. // operation
  3578. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3579. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3580. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3581. item_ct1.get_group(0) < ne02) {
  3582. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3583. item_ct1.get_group(0) * ne00 * ne01;
  3584. dst[offset_dst] = x[offset_src];
  3585. } else {
  3586. dst[offset_dst] = 0.0f;
  3587. }
  3588. }
  3589. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3590. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3591. int start = item_ct1.get_group(2) * group_size;
  3592. int end = start + group_size;
  3593. start += item_ct1.get_local_id(2);
  3594. if (end >= ne_elements) {
  3595. end = ne_elements;
  3596. }
  3597. float tmp = 0.0f; // partial sum for thread in warp
  3598. for (int j = start; j < end; j += block_size) {
  3599. tmp += x[j];
  3600. }
  3601. tmp = warp_reduce_sum(tmp, item_ct1);
  3602. if (block_size > WARP_SIZE) {
  3603. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3604. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3605. if (lane_id == 0) {
  3606. s_sum[warp_id] = tmp;
  3607. }
  3608. /*
  3609. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3610. converged control flow. You may need to adjust the code.
  3611. */
  3612. /*
  3613. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3614. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3615. better performance if there is no access to global memory.
  3616. */
  3617. item_ct1.barrier();
  3618. tmp = s_sum[lane_id];
  3619. tmp = warp_reduce_sum(tmp, item_ct1);
  3620. }
  3621. float mean = tmp / group_size;
  3622. tmp = 0.0f;
  3623. for (int j = start; j < end; j += block_size) {
  3624. float xi = x[j] - mean;
  3625. dst[j] = xi;
  3626. tmp += xi * xi;
  3627. }
  3628. tmp = warp_reduce_sum(tmp, item_ct1);
  3629. if (block_size > WARP_SIZE) {
  3630. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3631. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3632. if (lane_id == 0) {
  3633. s_sum[warp_id] = tmp;
  3634. }
  3635. /*
  3636. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3637. converged control flow. You may need to adjust the code.
  3638. */
  3639. /*
  3640. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3641. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3642. better performance if there is no access to global memory.
  3643. */
  3644. item_ct1.barrier();
  3645. tmp = s_sum[lane_id];
  3646. tmp = warp_reduce_sum(tmp, item_ct1);
  3647. }
  3648. float variance = tmp / group_size;
  3649. float scale = sycl::rsqrt(variance + eps);
  3650. for (int j = start; j < end; j += block_size) {
  3651. dst[j] *= scale;
  3652. }
  3653. }
  3654. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3655. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3656. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3657. item_ct1.get_local_id(1);
  3658. const int tid = item_ct1.get_local_id(2);
  3659. float tmp = 0.0f; // partial sum for thread in warp
  3660. for (int col = tid; col < ncols; col += block_size) {
  3661. const float xi = x[row*ncols + col];
  3662. tmp += xi * xi;
  3663. }
  3664. // sum up partial sums
  3665. tmp = warp_reduce_sum(tmp, item_ct1);
  3666. if (block_size > WARP_SIZE) {
  3667. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3668. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3669. if (lane_id == 0) {
  3670. s_sum[warp_id] = tmp;
  3671. }
  3672. /*
  3673. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3674. converged control flow. You may need to adjust the code.
  3675. */
  3676. item_ct1.barrier(sycl::access::fence_space::local_space);
  3677. tmp = s_sum[lane_id];
  3678. tmp = warp_reduce_sum(tmp, item_ct1);
  3679. }
  3680. const float mean = tmp / ncols;
  3681. const float scale = sycl::rsqrt(mean + eps);
  3682. for (int col = tid; col < ncols; col += block_size) {
  3683. dst[row*ncols + col] = scale * x[row*ncols + col];
  3684. }
  3685. }
  3686. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3687. const int iqs, dfloat2 &v) {
  3688. const block_q4_0 * x = (const block_q4_0 *) vx;
  3689. const dfloat d = x[ib].d;
  3690. const int vui = x[ib].qs[iqs];
  3691. v.x() = vui & 0xF;
  3692. v.y() = vui >> 4;
  3693. #ifdef GGML_SYCL_F16
  3694. // v = v - {8.0f, 8.0f};
  3695. // v = v * {d, d};
  3696. v.s0() = (v.s0() - 8.0f) * d;
  3697. v.s1() = (v.s1() - 8.0f) * d;
  3698. #else
  3699. v.x() = (v.x() - 8.0f) * d;
  3700. v.y() = (v.y() - 8.0f) * d;
  3701. #endif // GGML_SYCL_F16
  3702. }
  3703. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3704. const int iqs, dfloat2 &v) {
  3705. const block_q4_1 * x = (const block_q4_1 *) vx;
  3706. const dfloat d = x[ib].dm[0];
  3707. const dfloat m = x[ib].dm[1];
  3708. const int vui = x[ib].qs[iqs];
  3709. v.x() = vui & 0xF;
  3710. v.y() = vui >> 4;
  3711. #ifdef GGML_SYCL_F16
  3712. // v = v * {d, d};
  3713. // v = v + {m, m};
  3714. v.s0() = (v.s0() * d) + m;
  3715. v.s1() = (v.s1() * d) + m;
  3716. #else
  3717. v.x() = (v.x() * d) + m;
  3718. v.y() = (v.y() * d) + m;
  3719. #endif // GGML_SYCL_F16
  3720. }
  3721. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3722. const int iqs, dfloat2 &v) {
  3723. const block_q5_0 * x = (const block_q5_0 *) vx;
  3724. const dfloat d = x[ib].d;
  3725. uint32_t qh;
  3726. memcpy(&qh, x[ib].qh, sizeof(qh));
  3727. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3728. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3729. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3730. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3731. #ifdef GGML_SYCL_F16
  3732. // v = v - {16.0f, 16.0f};
  3733. // v = v * {d, d};
  3734. v.s0() = (v.s0() - 16.0f) * d;
  3735. v.s1() = (v.s1() - 16.0f) * d;
  3736. #else
  3737. v.x() = (v.x() - 16.0f) * d;
  3738. v.y() = (v.y() - 16.0f) * d;
  3739. #endif // GGML_SYCL_F16
  3740. }
  3741. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  3742. const int iqs, dfloat2 &v) {
  3743. const block_q5_1 * x = (const block_q5_1 *) vx;
  3744. const dfloat d = x[ib].dm[0];
  3745. const dfloat m = x[ib].dm[1];
  3746. uint32_t qh;
  3747. memcpy(&qh, x[ib].qh, sizeof(qh));
  3748. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3749. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3750. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3751. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3752. #ifdef GGML_SYCL_F16
  3753. // v = v * {d, d};
  3754. // v = v + {m, m};
  3755. v.s0() = (v.s0() * d) + m;
  3756. v.s1() = (v.s1() * d) + m;
  3757. #else
  3758. v.x() = (v.x() * d) + m;
  3759. v.y() = (v.y() * d) + m;
  3760. #endif // GGML_SYCL_F16
  3761. }
  3762. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  3763. const int iqs, dfloat2 &v) {
  3764. const block_q8_0 * x = (const block_q8_0 *) vx;
  3765. const dfloat d = x[ib].d;
  3766. v.x() = x[ib].qs[iqs + 0];
  3767. v.y() = x[ib].qs[iqs + 1];
  3768. #ifdef GGML_SYCL_F16
  3769. // v = v * {d, d};
  3770. v.s0() *= d;
  3771. v.s1() *= d;
  3772. #else
  3773. v.x() *= d;
  3774. v.y() *= d;
  3775. #endif // GGML_SYCL_F16
  3776. }
  3777. template<typename dst_t>
  3778. static void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3779. const sycl::nd_item<3> &item_ct1) {
  3780. const int i = item_ct1.get_group(2);
  3781. // assume 32 threads
  3782. const int tid = item_ct1.get_local_id(2);
  3783. const int il = tid/8;
  3784. const int ir = tid%8;
  3785. const int ib = 8*i + ir;
  3786. if (ib >= nb32) {
  3787. return;
  3788. }
  3789. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3790. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  3791. const float d = sycl::vec<sycl::half, 1>(x->d)
  3792. .convert<float, sycl::rounding_mode::automatic>()[0];
  3793. const float dm = -8*d;
  3794. const uint8_t * q = x->qs + 4*il;
  3795. for (int l = 0; l < 4; ++l) {
  3796. y[l+ 0] = d * (q[l] & 0xF) + dm;
  3797. y[l+16] = d * (q[l] >> 4) + dm;
  3798. }
  3799. }
  3800. template<typename dst_t>
  3801. static void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3802. const sycl::nd_item<3> &item_ct1) {
  3803. const int i = item_ct1.get_group(2);
  3804. // assume 32 threads
  3805. const int tid = item_ct1.get_local_id(2);
  3806. const int il = tid/8;
  3807. const int ir = tid%8;
  3808. const int ib = 8*i + ir;
  3809. if (ib >= nb32) {
  3810. return;
  3811. }
  3812. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3813. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  3814. const sycl::float2 d =
  3815. x->dm.convert<float, sycl::rounding_mode::automatic>();
  3816. const uint8_t * q = x->qs + 4*il;
  3817. for (int l = 0; l < 4; ++l) {
  3818. y[l + 0] = d.x() * (q[l] & 0xF) + d.y();
  3819. y[l + 16] = d.x() * (q[l] >> 4) + d.y();
  3820. }
  3821. }
  3822. //================================== k-quants
  3823. template<typename dst_t>
  3824. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3825. const sycl::nd_item<3> &item_ct1) {
  3826. const int i = item_ct1.get_group(2);
  3827. const block_q2_K * x = (const block_q2_K *) vx;
  3828. const int tid = item_ct1.get_local_id(2);
  3829. const int n = tid/32;
  3830. const int l = tid - 32*n;
  3831. const int is = 8*n + l/16;
  3832. const uint8_t q = x[i].qs[32*n + l];
  3833. dst_t * y = yy + i*QK_K + 128*n;
  3834. float dall = x[i].dm[0];
  3835. float dmin = x[i].dm[1];
  3836. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3837. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3838. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  3839. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  3840. }
  3841. template<typename dst_t>
  3842. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3843. const sycl::nd_item<3> &item_ct1) {
  3844. const int i = item_ct1.get_group(2);
  3845. const block_q3_K * x = (const block_q3_K *) vx;
  3846. const int r = item_ct1.get_local_id(2) / 4;
  3847. const int tid = r/2;
  3848. const int is0 = r%2;
  3849. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  3850. const int n = tid / 4;
  3851. const int j = tid - 4*n;
  3852. uint8_t m = 1 << (4*n + j);
  3853. int is = 8*n + 2*j + is0;
  3854. int shift = 2*j;
  3855. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  3856. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  3857. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  3858. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  3859. float d_all = x[i].d;
  3860. float dl = d_all * (us - 32);
  3861. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  3862. const uint8_t * q = x[i].qs + 32*n;
  3863. const uint8_t * hm = x[i].hmask;
  3864. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  3865. }
  3866. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  3867. if (j < 4) {
  3868. d = q[j] & 63; m = q[j + 4] & 63;
  3869. } else {
  3870. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  3871. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  3872. }
  3873. }
  3874. template<typename dst_t>
  3875. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3876. const sycl::nd_item<3> &item_ct1) {
  3877. const block_q4_K * x = (const block_q4_K *) vx;
  3878. const int i = item_ct1.get_group(2);
  3879. // assume 32 threads
  3880. const int tid = item_ct1.get_local_id(2);
  3881. const int il = tid/8;
  3882. const int ir = tid%8;
  3883. const int is = 2*il;
  3884. const int n = 4;
  3885. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  3886. const float dall = x[i].dm[0];
  3887. const float dmin = x[i].dm[1];
  3888. const uint8_t * q = x[i].qs + 32*il + n*ir;
  3889. uint8_t sc, m;
  3890. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3891. const float d1 = dall * sc; const float m1 = dmin * m;
  3892. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3893. const float d2 = dall * sc; const float m2 = dmin * m;
  3894. for (int l = 0; l < n; ++l) {
  3895. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  3896. y[l +32] = d2 * (q[l] >> 4) - m2;
  3897. }
  3898. }
  3899. template<typename dst_t>
  3900. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3901. const sycl::nd_item<3> &item_ct1) {
  3902. const block_q5_K * x = (const block_q5_K *) vx;
  3903. const int i = item_ct1.get_group(2);
  3904. // assume 64 threads - this is very slightly better than the one below
  3905. const int tid = item_ct1.get_local_id(2);
  3906. const int il = tid/16; // il is in 0...3
  3907. const int ir = tid%16; // ir is in 0...15
  3908. const int is = 2*il; // is is in 0...6
  3909. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  3910. const float dall = x[i].dm[0];
  3911. const float dmin = x[i].dm[1];
  3912. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  3913. const uint8_t * qh = x[i].qh + 2*ir;
  3914. uint8_t sc, m;
  3915. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3916. const float d1 = dall * sc; const float m1 = dmin * m;
  3917. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3918. const float d2 = dall * sc; const float m2 = dmin * m;
  3919. uint8_t hm = 1 << (2*il);
  3920. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  3921. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  3922. hm <<= 1;
  3923. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  3924. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  3925. }
  3926. template<typename dst_t>
  3927. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3928. const sycl::nd_item<3> &item_ct1) {
  3929. const block_q6_K * x = (const block_q6_K *) vx;
  3930. const int i = item_ct1.get_group(2);
  3931. // assume 64 threads - this is very slightly better than the one below
  3932. const int tid = item_ct1.get_local_id(2);
  3933. const int ip = tid/32; // ip is 0 or 1
  3934. const int il = tid - 32*ip; // 0...32
  3935. const int is = 8*ip + il/16;
  3936. dst_t * y = yy + i*QK_K + 128*ip + il;
  3937. const float d = x[i].d;
  3938. const uint8_t * ql = x[i].ql + 64*ip + il;
  3939. const uint8_t qh = x[i].qh[32*ip + il];
  3940. const int8_t * sc = x[i].scales + is;
  3941. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3942. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  3943. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3944. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  3945. }
  3946. template<typename dst_t>
  3947. static void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3948. const sycl::nd_item<3> &item_ct1,
  3949. const uint64_t *iq2xxs_grid_ptr,
  3950. const uint8_t *ksigns_iq2xs_ptr,
  3951. const uint8_t *kmask_iq2xs_ptr) {
  3952. const int i = item_ct1.get_group(2);
  3953. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  3954. const int tid = item_ct1.get_local_id(2);
  3955. const int il = tid/8; // 0...3
  3956. const int ib = tid%8; // 0...7
  3957. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3958. const uint16_t * q2 = x[i].qs + 4*ib;
  3959. const uint8_t * aux8 = (const uint8_t *)q2;
  3960. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid_ptr + aux8[il]);
  3961. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3962. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  3963. const uint8_t signs = ksigns_iq2xs_ptr[(aux32 >> 7*il) & 127];
  3964. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs_ptr[j] ? -1.f : 1.f);
  3965. }
  3966. template<typename dst_t>
  3967. static void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3968. const sycl::nd_item<3> &item_ct1,
  3969. const uint64_t *iq2xs_grid,
  3970. const uint8_t *ksigns_iq2xs,
  3971. const uint8_t *kmask_iq2xs) {
  3972. const int i = item_ct1.get_group(2);
  3973. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  3974. const int tid = item_ct1.get_local_id(2);
  3975. const int il = tid/8; // 0...3
  3976. const int ib = tid%8; // 0...7
  3977. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3978. const uint16_t * q2 = x[i].qs + 4*ib;
  3979. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  3980. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  3981. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  3982. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3983. }
  3984. template <typename dst_t>
  3985. __dpct_inline__ static void
  3986. dequantize_block_iq2_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  3987. const sycl::nd_item<3> &item_ct1) {
  3988. const int i = item_ct1.get_group(2);
  3989. const block_iq2_s * x = (const block_iq2_s *) vx;
  3990. const int tid = item_ct1.get_local_id(2);
  3991. const int il = tid/8; // 0...3
  3992. const int ib = tid%8; // 0...7
  3993. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3994. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  3995. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  3996. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  3997. #pragma unroll
  3998. for (int j = 0; j < 8; ++j) {
  3999. y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4000. }
  4001. }
  4002. template<typename dst_t>
  4003. static void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4004. const sycl::nd_item<3> &item_ct1,
  4005. const uint32_t *iq3xxs_grid,
  4006. const uint8_t *ksigns_iq2xs,
  4007. const uint8_t *kmask_iq2xs) {
  4008. const int i = item_ct1.get_group(2);
  4009. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  4010. const int tid = item_ct1.get_local_id(2);
  4011. const int il = tid/8; // 0...3
  4012. const int ib = tid%8; // 0...7
  4013. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4014. const uint8_t * q3 = x[i].qs + 8*ib;
  4015. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  4016. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  4017. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  4018. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4019. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  4020. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  4021. for (int j = 0; j < 4; ++j) {
  4022. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4023. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4024. }
  4025. }
  4026. template <typename dst_t>
  4027. __dpct_inline__ static void
  4028. dequantize_block_iq3_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4029. const sycl::nd_item<3> &item_ct1,
  4030. const uint8_t *kmask_iq2xs, const uint32_t *iq3s_grid) {
  4031. const int i = item_ct1.get_group(2);
  4032. const block_iq3_s * x = (const block_iq3_s *) vx;
  4033. const int tid = item_ct1.get_local_id(2);
  4034. const int il = tid/8; // 0...3
  4035. const int ib = tid%8; // 0...7
  4036. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4037. const uint8_t * qs = x[i].qs + 8*ib;
  4038. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  4039. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  4040. const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
  4041. const uint8_t signs = x[i].signs[4*ib + il];
  4042. #pragma unroll
  4043. for (int j = 0; j < 4; ++j) {
  4044. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4045. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4046. }
  4047. }
  4048. template <typename dst_t>
  4049. __dpct_inline__ static void
  4050. dequantize_block_iq1_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4051. const sycl::nd_item<3> &item_ct1,
  4052. const uint32_t *iq1s_grid_gpu) {
  4053. const int i = item_ct1.get_group(2);
  4054. const block_iq1_s * x = (const block_iq1_s *) vx;
  4055. const int tid = item_ct1.get_local_id(2);
  4056. const int il = tid/8; // 0...3
  4057. const int ib = tid%8; // 0...7
  4058. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4059. const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
  4060. const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
  4061. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  4062. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
  4063. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  4064. grid32[0] &= 0x0f0f0f0f;
  4065. #pragma unroll
  4066. for (int j = 0; j < 8; ++j) {
  4067. y[j] = d * (q[j] + delta);
  4068. }
  4069. }
  4070. template <typename dst_t>
  4071. __dpct_inline__ static void
  4072. dequantize_block_iq1_m(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4073. const sycl::nd_item<3> &item_ct1,
  4074. const uint32_t *iq1s_grid_gpu) {
  4075. const int i = item_ct1.get_group(2);
  4076. const block_iq1_m * x = (const block_iq1_m *) vx;
  4077. const int tid = item_ct1.get_local_id(2);
  4078. const int il = tid/8; // 0...3
  4079. const int ib = tid%8; // 0...7
  4080. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4081. const uint16_t * sc = (const uint16_t *)x[i].scales;
  4082. iq1m_scale_t scale;
  4083. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  4084. const int ib16 = 2*ib + il/2; // sc[ib16/4] >> 3*(ib16%4) -> sc[ib/2] >> 3*((2*ib+il/2)%4);
  4085. const float d = (float)scale.f16 * (2*((sc[ib16/4] >> 3*(ib16%4)) & 0x7) + 1);
  4086. const float delta = x[i].qh[2*ib+il/2] & (0x08 << 4*(il%2)) ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA;
  4087. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  4088. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[2*ib+il/2] >> 4*(il%2)) & 7) << 8)];
  4089. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  4090. grid32[0] &= 0x0f0f0f0f;
  4091. #pragma unroll
  4092. for (int j = 0; j < 8; ++j) {
  4093. y[j] = d * (q[j] + delta);
  4094. }
  4095. }
  4096. template <typename dst_t>
  4097. __dpct_inline__ static void
  4098. dequantize_block_iq4_nl(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4099. const sycl::nd_item<3> &item_ct1) {
  4100. const int i = item_ct1.get_group(2);
  4101. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  4102. const int tid = item_ct1.get_local_id(2);
  4103. const int il = tid/8; // 0...3
  4104. const int ib = tid%8; // 0...7
  4105. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  4106. const uint8_t * q4 = x[ib].qs + 4*il;
  4107. const float d = (float)x[ib].d;
  4108. #pragma unroll
  4109. for (int j = 0; j < 4; ++j) {
  4110. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  4111. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  4112. }
  4113. }
  4114. template <typename dst_t>
  4115. __dpct_inline__ static void
  4116. dequantize_block_iq4_xs(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4117. const sycl::nd_item<3> &item_ct1) {
  4118. const int i = item_ct1.get_group(2);
  4119. const block_iq4_xs * x = (const block_iq4_xs *)vx;
  4120. const int tid = item_ct1.get_local_id(2);
  4121. const int il = tid/8; // 0...3
  4122. const int ib = tid%8; // 0...7
  4123. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  4124. const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
  4125. 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);
  4126. #pragma unroll
  4127. for (int j = 0; j < 4; ++j) {
  4128. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  4129. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  4130. }
  4131. }
  4132. /*
  4133. DPCT1110:4: The total declared local variable size in device function
  4134. dequantize_mul_mat_vec_q2_k exceeds 128 bytes and may cause high register
  4135. pressure. Consult with your hardware vendor to find the total register size
  4136. available and adjust the code, or use smaller sub-group size to avoid high
  4137. register pressure.
  4138. */
  4139. static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
  4140. const float *__restrict__ yy,
  4141. float *__restrict__ dst,
  4142. const int ncols, int nrows,
  4143. const sycl::nd_item<3> &item_ct1) {
  4144. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4145. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4146. item_ct1.get_local_id(1);
  4147. if (row > nrows) return;
  4148. const int num_blocks_per_row = ncols / QK_K;
  4149. const int ib0 = row*num_blocks_per_row;
  4150. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  4151. float tmp = 0; // partial sum for thread in warp
  4152. const int tid =
  4153. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  4154. const int ix =
  4155. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4156. const int step = 16/K_QUANTS_PER_ITERATION;
  4157. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4158. const int in = tid - step*im; // 0...15 or 0...7
  4159. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  4160. const int q_offset = 32*im + l0;
  4161. const int s_offset = 8*im;
  4162. const int y_offset = 128*im + l0;
  4163. uint32_t aux[4];
  4164. const uint8_t * d = (const uint8_t *)aux;
  4165. const uint8_t * m = (const uint8_t *)(aux + 2);
  4166. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4167. const float * y = yy + i * QK_K + y_offset;
  4168. const uint8_t * q = x[i].qs + q_offset;
  4169. const float dall = x[i].dm[0];
  4170. const float dmin = x[i].dm[1];
  4171. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  4172. aux[0] = a[0] & 0x0f0f0f0f;
  4173. aux[1] = a[1] & 0x0f0f0f0f;
  4174. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  4175. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  4176. float sum1 = 0, sum2 = 0;
  4177. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4178. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  4179. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  4180. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  4181. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  4182. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  4183. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  4184. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  4185. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  4186. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  4187. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  4188. }
  4189. tmp += dall * sum1 - dmin * sum2;
  4190. }
  4191. // sum up partial sums and write back result
  4192. #pragma unroll
  4193. for (int mask = 16; mask > 0; mask >>= 1) {
  4194. tmp +=
  4195. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4196. }
  4197. if (item_ct1.get_local_id(2) == 0) {
  4198. dst[row] = tmp;
  4199. }
  4200. }
  4201. /*
  4202. DPCT1110:5: The total declared local variable size in device function
  4203. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  4204. pressure. Consult with your hardware vendor to find the total register size
  4205. available and adjust the code, or use smaller sub-group size to avoid high
  4206. register pressure.
  4207. */
  4208. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  4209. const float *__restrict__ yy,
  4210. float *__restrict__ dst,
  4211. const int ncols, int nrows,
  4212. const sycl::nd_item<3> &item_ct1) {
  4213. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4214. item_ct1.get_local_id(1);
  4215. if (row > nrows) return;
  4216. const int num_blocks_per_row = ncols / QK_K;
  4217. const int ib0 = row*num_blocks_per_row;
  4218. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  4219. float tmp = 0; // partial sum for thread in warp
  4220. const uint16_t kmask1 = 0x0303;
  4221. const uint16_t kmask2 = 0x0f0f;
  4222. const int tid =
  4223. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4224. const int ix =
  4225. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4226. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  4227. const int step = 16/K_QUANTS_PER_ITERATION;
  4228. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4229. const int in = tid - step*im; // 0....15 or 0...7
  4230. const uint8_t m = 1 << (4*im);
  4231. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  4232. const int q_offset = 32*im + l0;
  4233. const int y_offset = 128*im + l0;
  4234. uint16_t utmp[4];
  4235. const int8_t * s = (const int8_t *)utmp;
  4236. const uint16_t s_shift = 4*im;
  4237. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4238. const float * y = yy + i * QK_K + y_offset;
  4239. const uint8_t * q = x[i].qs + q_offset;
  4240. const uint8_t * h = x[i].hmask + l0;
  4241. const uint16_t * a = (const uint16_t *)x[i].scales;
  4242. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  4243. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  4244. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  4245. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  4246. const float d = x[i].d;
  4247. float sum = 0;
  4248. for (int l = 0; l < n; ++l) {
  4249. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  4250. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  4251. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  4252. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  4253. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  4254. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  4255. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  4256. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  4257. }
  4258. tmp += d * sum;
  4259. }
  4260. // sum up partial sums and write back result
  4261. #pragma unroll
  4262. for (int mask = 16; mask > 0; mask >>= 1) {
  4263. tmp +=
  4264. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4265. }
  4266. if (item_ct1.get_local_id(2) == 0) {
  4267. dst[row] = tmp;
  4268. }
  4269. }
  4270. /*
  4271. DPCT1110:6: The total declared local variable size in device function
  4272. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  4273. pressure. Consult with your hardware vendor to find the total register size
  4274. available and adjust the code, or use smaller sub-group size to avoid high
  4275. register pressure.
  4276. */
  4277. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  4278. const float *__restrict__ yy,
  4279. float *__restrict__ dst,
  4280. const int ncols, int nrows,
  4281. const sycl::nd_item<3> &item_ct1) {
  4282. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4283. item_ct1.get_local_id(1);
  4284. if (row > nrows) return;
  4285. const int num_blocks_per_row = ncols / QK_K;
  4286. const int ib0 = row*num_blocks_per_row;
  4287. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  4288. const uint16_t kmask1 = 0x3f3f;
  4289. const uint16_t kmask2 = 0x0f0f;
  4290. const uint16_t kmask3 = 0xc0c0;
  4291. const int tid =
  4292. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4293. const int ix =
  4294. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4295. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  4296. const int il = tid/step; // 0...3
  4297. const int ir = tid - step*il; // 0...7 or 0...3
  4298. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  4299. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4300. const int in = il%2;
  4301. const int l0 = n*(2*ir + in);
  4302. const int q_offset = 32*im + l0;
  4303. const int y_offset = 64*im + l0;
  4304. uint16_t aux[4];
  4305. const uint8_t * sc = (const uint8_t *)aux;
  4306. #if K_QUANTS_PER_ITERATION == 2
  4307. uint32_t q32[4];
  4308. const uint8_t * q4 = (const uint8_t *)q32;
  4309. #else
  4310. uint16_t q16[4];
  4311. const uint8_t * q4 = (const uint8_t *)q16;
  4312. #endif
  4313. float tmp = 0; // partial sum for thread in warp
  4314. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4315. const float * y1 = yy + i*QK_K + y_offset;
  4316. const float * y2 = y1 + 128;
  4317. const float dall = x[i].dm[0];
  4318. const float dmin = x[i].dm[1];
  4319. const uint16_t * a = (const uint16_t *)x[i].scales;
  4320. aux[0] = a[im+0] & kmask1;
  4321. aux[1] = a[im+2] & kmask1;
  4322. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4323. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4324. #if K_QUANTS_PER_ITERATION == 2
  4325. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4326. const uint32_t * q2 = q1 + 16;
  4327. q32[0] = q1[0] & 0x0f0f0f0f;
  4328. q32[1] = q1[0] & 0xf0f0f0f0;
  4329. q32[2] = q2[0] & 0x0f0f0f0f;
  4330. q32[3] = q2[0] & 0xf0f0f0f0;
  4331. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4332. float smin = 0;
  4333. for (int l = 0; l < 4; ++l) {
  4334. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4335. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4336. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4337. }
  4338. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4339. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4340. dmin * smin;
  4341. #else
  4342. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4343. const uint16_t * q2 = q1 + 32;
  4344. q16[0] = q1[0] & 0x0f0f;
  4345. q16[1] = q1[0] & 0xf0f0;
  4346. q16[2] = q2[0] & 0x0f0f;
  4347. q16[3] = q2[0] & 0xf0f0;
  4348. float4 s = {0.f, 0.f, 0.f, 0.f};
  4349. float smin = 0;
  4350. for (int l = 0; l < 2; ++l) {
  4351. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  4352. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  4353. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4354. }
  4355. 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;
  4356. #endif
  4357. }
  4358. // sum up partial sums and write back result
  4359. #pragma unroll
  4360. for (int mask = 16; mask > 0; mask >>= 1) {
  4361. tmp +=
  4362. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4363. }
  4364. if (tid == 0) {
  4365. dst[row] = tmp;
  4366. }
  4367. }
  4368. /*
  4369. DPCT1110:7: The total declared local variable size in device function
  4370. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  4371. pressure. Consult with your hardware vendor to find the total register size
  4372. available and adjust the code, or use smaller sub-group size to avoid high
  4373. register pressure.
  4374. */
  4375. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  4376. const float *__restrict__ yy,
  4377. float *__restrict__ dst,
  4378. const int ncols,
  4379. const sycl::nd_item<3> &item_ct1) {
  4380. const int row = item_ct1.get_group(2);
  4381. const int num_blocks_per_row = ncols / QK_K;
  4382. const int ib0 = row*num_blocks_per_row;
  4383. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  4384. float tmp = 0; // partial sum for thread in warp
  4385. const uint16_t kmask1 = 0x3f3f;
  4386. const uint16_t kmask2 = 0x0f0f;
  4387. const uint16_t kmask3 = 0xc0c0;
  4388. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  4389. const int ix = item_ct1.get_local_id(2) % 2;
  4390. const int il = tid/4; // 0...3
  4391. const int ir = tid - 4*il;// 0...3
  4392. const int n = 2;
  4393. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4394. const int in = il%2;
  4395. const int l0 = n*(2*ir + in);
  4396. const int q_offset = 32*im + l0;
  4397. const int y_offset = 64*im + l0;
  4398. const uint8_t hm1 = 1 << (2*im);
  4399. const uint8_t hm2 = hm1 << 4;
  4400. uint16_t aux[4];
  4401. const uint8_t * sc = (const uint8_t *)aux;
  4402. uint16_t q16[8];
  4403. const uint8_t * q4 = (const uint8_t *)q16;
  4404. for (int i = ix; i < num_blocks_per_row; i += 2) {
  4405. const uint8_t * ql1 = x[i].qs + q_offset;
  4406. const uint8_t * qh = x[i].qh + l0;
  4407. const float * y1 = yy + i*QK_K + y_offset;
  4408. const float * y2 = y1 + 128;
  4409. const float dall = x[i].dm[0];
  4410. const float dmin = x[i].dm[1];
  4411. const uint16_t * a = (const uint16_t *)x[i].scales;
  4412. aux[0] = a[im+0] & kmask1;
  4413. aux[1] = a[im+2] & kmask1;
  4414. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4415. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4416. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  4417. float smin = 0;
  4418. const uint16_t * q1 = (const uint16_t *)ql1;
  4419. const uint16_t * q2 = q1 + 32;
  4420. q16[0] = q1[0] & 0x0f0f;
  4421. q16[1] = q1[8] & 0x0f0f;
  4422. q16[2] = (q1[0] >> 4) & 0x0f0f;
  4423. q16[3] = (q1[8] >> 4) & 0x0f0f;
  4424. q16[4] = q2[0] & 0x0f0f;
  4425. q16[5] = q2[8] & 0x0f0f;
  4426. q16[6] = (q2[0] >> 4) & 0x0f0f;
  4427. q16[7] = (q2[8] >> 4) & 0x0f0f;
  4428. for (int l = 0; l < n; ++l) {
  4429. sum.x() +=
  4430. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  4431. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  4432. sum.y() +=
  4433. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  4434. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  4435. sum.z() +=
  4436. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  4437. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  4438. sum.w() +=
  4439. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  4440. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  4441. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  4442. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  4443. }
  4444. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  4445. sum.w() * sc[5]) -
  4446. dmin * smin;
  4447. }
  4448. // sum up partial sums and write back result
  4449. #pragma unroll
  4450. for (int mask = 16; mask > 0; mask >>= 1) {
  4451. tmp +=
  4452. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4453. }
  4454. if (item_ct1.get_local_id(2) == 0) {
  4455. dst[row] = tmp;
  4456. }
  4457. }
  4458. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  4459. const sycl::nd_item<3> &item_ct1) {
  4460. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4461. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4462. item_ct1.get_local_id(1);
  4463. if (row > nrows) return;
  4464. const int num_blocks_per_row = ncols / QK_K;
  4465. const int ib0 = row*num_blocks_per_row;
  4466. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  4467. const int tid =
  4468. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4469. const int ix =
  4470. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  4471. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  4472. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4473. const int in = tid - step*im; // 0...15 or 0...7
  4474. #if K_QUANTS_PER_ITERATION == 1
  4475. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  4476. const int is = 0;
  4477. #else
  4478. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  4479. const int is = in / 4;
  4480. #endif
  4481. const int ql_offset = 64*im + l0;
  4482. const int qh_offset = 32*im + l0;
  4483. const int s_offset = 8*im + is;
  4484. const int y_offset = 128*im + l0;
  4485. float tmp = 0; // partial sum for thread in warp
  4486. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4487. const float * y = yy + i * QK_K + y_offset;
  4488. const uint8_t * ql = x[i].ql + ql_offset;
  4489. const uint8_t * qh = x[i].qh + qh_offset;
  4490. const int8_t * s = x[i].scales + s_offset;
  4491. const float d = x[i].d;
  4492. #if K_QUANTS_PER_ITERATION == 1
  4493. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  4494. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  4495. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  4496. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  4497. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  4498. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  4499. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  4500. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  4501. tmp += sum;
  4502. #else
  4503. float sum = 0;
  4504. for (int l = 0; l < 4; ++l) {
  4505. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  4506. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  4507. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  4508. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  4509. }
  4510. tmp += sum;
  4511. #endif
  4512. }
  4513. // sum up partial sums and write back result
  4514. #pragma unroll
  4515. for (int mask = 16; mask > 0; mask >>= 1) {
  4516. tmp +=
  4517. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4518. }
  4519. if (tid == 0) {
  4520. dst[row] = tmp;
  4521. }
  4522. }
  4523. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4524. const sycl::half *x = (const sycl::half *)vx;
  4525. // automatic half -> float type cast if dfloat == float
  4526. v.x() = x[ib + iqs + 0];
  4527. v.y() = x[ib + iqs + 1];
  4528. }
  4529. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4530. const float * x = (const float *) vx;
  4531. // automatic half -> float type cast if dfloat == float
  4532. v.x() = x[ib + iqs + 0];
  4533. v.y() = x[ib + iqs + 1];
  4534. }
  4535. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  4536. const sycl::nd_item<3> &item_ct1) {
  4537. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4538. item_ct1.get_local_id(2);
  4539. if (ix >= kx_padded) {
  4540. return;
  4541. }
  4542. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4543. item_ct1.get_local_id(1);
  4544. const int i_padded = iy*kx_padded + ix;
  4545. block_q8_1 * y = (block_q8_1 *) vy;
  4546. const int ib = i_padded / QK8_1; // block index
  4547. const int iqs = i_padded % QK8_1; // quant index
  4548. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  4549. float amax = sycl::fabs((float)xi);
  4550. float sum = xi;
  4551. #pragma unroll
  4552. for (int mask = 16; mask > 0; mask >>= 1) {
  4553. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  4554. item_ct1.get_sub_group(), amax, mask));
  4555. sum +=
  4556. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  4557. }
  4558. const float d = amax / 127;
  4559. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  4560. y[ib].qs[iqs] = q;
  4561. if (iqs > 0) {
  4562. return;
  4563. }
  4564. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  4565. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  4566. }
  4567. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4568. static void k_get_rows(
  4569. const void * src0, const int32_t * src1, dst_t * dst,
  4570. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4571. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4572. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4573. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4574. size_t s10, size_t s11, size_t s12,
  4575. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4576. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4577. item_ct1.get_local_id(2)) *
  4578. 2;
  4579. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4580. item_ct1.get_local_id(1);
  4581. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4582. item_ct1.get_local_id(0)) /
  4583. ne12;
  4584. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4585. item_ct1.get_local_id(0)) %
  4586. ne12;
  4587. if (i00 >= ne00) {
  4588. return;
  4589. }
  4590. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4591. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4592. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  4593. const int ib = i00/qk; // block index
  4594. const int iqs = (i00%qk)/qr; // quant index
  4595. const int iybs = i00 - i00%qk; // dst block start index
  4596. const int y_offset = qr == 1 ? 1 : qk/2;
  4597. // dequantize
  4598. dfloat2 v;
  4599. dequantize_kernel(src0_row, ib, iqs, v);
  4600. dst_row[iybs + iqs + 0] = v.x();
  4601. dst_row[iybs + iqs + y_offset] = v.y();
  4602. }
  4603. template<typename src0_t, typename dst_t>
  4604. static void k_get_rows_float(
  4605. const src0_t * src0, const int32_t * src1, dst_t * dst,
  4606. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4607. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4608. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4609. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4610. size_t s10, size_t s11, size_t s12,
  4611. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4612. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4613. item_ct1.get_local_id(2);
  4614. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4615. item_ct1.get_local_id(1);
  4616. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4617. item_ct1.get_local_id(0)) /
  4618. ne12;
  4619. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4620. item_ct1.get_local_id(0)) %
  4621. ne12;
  4622. if (i00 >= ne00) {
  4623. return;
  4624. }
  4625. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4626. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4627. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  4628. dst_row[i00] = src0_row[i00];
  4629. }
  4630. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4631. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4632. const sycl::nd_item<3> &item_ct1) {
  4633. const int i = 2 * (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4634. item_ct1.get_local_id(2));
  4635. if (i >= k) {
  4636. return;
  4637. }
  4638. const int ib = i/qk; // block index
  4639. const int iqs = (i%qk)/qr; // quant index
  4640. const int iybs = i - i%qk; // y block start index
  4641. const int y_offset = qr == 1 ? 1 : qk/2;
  4642. // dequantize
  4643. dfloat2 v;
  4644. dequantize_kernel(vx, ib, iqs, v);
  4645. y[iybs + iqs + 0] = v.x();
  4646. y[iybs + iqs + y_offset] = v.y();
  4647. }
  4648. template <typename src_t, typename dst_t>
  4649. static void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4650. const sycl::nd_item<3> &item_ct1) {
  4651. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4652. item_ct1.get_local_id(2);
  4653. if (i >= k) {
  4654. return;
  4655. }
  4656. const src_t * x = (src_t *) vx;
  4657. y[i] = x[i];
  4658. }
  4659. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  4660. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  4661. #define VDR_Q4_0_Q8_1_MMVQ 2
  4662. #define VDR_Q4_0_Q8_1_MMQ 4
  4663. template <int vdr>
  4664. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  4665. const float &d4,
  4666. const sycl::half2 &ds8) {
  4667. int sumi = 0;
  4668. #pragma unroll
  4669. for (int i = 0; i < vdr; ++i) {
  4670. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4671. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4672. // SIMD dot product of quantized values
  4673. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4674. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4675. }
  4676. const sycl::float2 ds8f =
  4677. ds8.convert<float, sycl::rounding_mode::automatic>();
  4678. // second part effectively subtracts 8 from each quant value
  4679. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  4680. }
  4681. #define VDR_Q4_1_Q8_1_MMVQ 2
  4682. #define VDR_Q4_1_Q8_1_MMQ 4
  4683. template <int vdr>
  4684. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  4685. const sycl::half2 &dm4,
  4686. const sycl::half2 &ds8) {
  4687. int sumi = 0;
  4688. #pragma unroll
  4689. for (int i = 0; i < vdr; ++i) {
  4690. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4691. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4692. // SIMD dot product of quantized values
  4693. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4694. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4695. }
  4696. #ifdef GGML_SYCL_F16
  4697. const sycl::float2 tmp =
  4698. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4699. const float d4d8 = tmp.x();
  4700. const float m4s8 = tmp.y();
  4701. #else
  4702. const sycl::float2 dm4f =
  4703. dm4.convert<float, sycl::rounding_mode::automatic>();
  4704. const sycl::float2 ds8f =
  4705. ds8.convert<float, sycl::rounding_mode::automatic>();
  4706. const float d4d8 = dm4f.x() * ds8f.x();
  4707. const float m4s8 = dm4f.y() * ds8f.y();
  4708. #endif // GGML_SYCL_F16
  4709. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  4710. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  4711. }
  4712. #define VDR_Q5_0_Q8_1_MMVQ 2
  4713. #define VDR_Q5_0_Q8_1_MMQ 4
  4714. template <int vdr>
  4715. static __dpct_inline__ float
  4716. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  4717. const float &d5, const sycl::half2 &ds8) {
  4718. int sumi = 0;
  4719. #pragma unroll
  4720. for (int i = 0; i < vdr; ++i) {
  4721. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4722. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4723. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4724. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4725. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4726. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4727. sumi); // SIMD dot product of quantized values
  4728. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4729. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4730. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4731. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4732. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4733. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4734. sumi); // SIMD dot product of quantized values
  4735. }
  4736. const sycl::float2 ds8f =
  4737. ds8.convert<float, sycl::rounding_mode::automatic>();
  4738. // second part effectively subtracts 16 from each quant value
  4739. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  4740. }
  4741. #define VDR_Q5_1_Q8_1_MMVQ 2
  4742. #define VDR_Q5_1_Q8_1_MMQ 4
  4743. template <int vdr>
  4744. static __dpct_inline__ float
  4745. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  4746. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  4747. int sumi = 0;
  4748. #pragma unroll
  4749. for (int i = 0; i < vdr; ++i) {
  4750. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4751. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4752. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4753. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4754. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4755. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4756. sumi); // SIMD dot product of quantized values
  4757. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4758. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4759. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4760. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4761. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4762. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4763. sumi); // SIMD dot product of quantized values
  4764. }
  4765. #ifdef GGML_SYCL_F16
  4766. const sycl::float2 tmp =
  4767. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4768. const float d5d8 = tmp.x();
  4769. const float m5s8 = tmp.y();
  4770. #else
  4771. const sycl::float2 dm5f =
  4772. dm5.convert<float, sycl::rounding_mode::automatic>();
  4773. const sycl::float2 ds8f =
  4774. ds8.convert<float, sycl::rounding_mode::automatic>();
  4775. const float d5d8 = dm5f.x() * ds8f.x();
  4776. const float m5s8 = dm5f.y() * ds8f.y();
  4777. #endif // GGML_SYCL_F16
  4778. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  4779. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  4780. }
  4781. #define VDR_Q8_0_Q8_1_MMVQ 2
  4782. #define VDR_Q8_0_Q8_1_MMQ 8
  4783. template <int vdr>
  4784. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  4785. const float &d8_0,
  4786. const float &d8_1) {
  4787. int sumi = 0;
  4788. #pragma unroll
  4789. for (int i = 0; i < vdr; ++i) {
  4790. // SIMD dot product of quantized values
  4791. sumi = dpct::dp4a(v[i], u[i], sumi);
  4792. }
  4793. return d8_0*d8_1 * sumi;
  4794. }
  4795. template <int vdr>
  4796. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  4797. const sycl::half2 &dm8,
  4798. const sycl::half2 &ds8) {
  4799. int sumi = 0;
  4800. #pragma unroll
  4801. for (int i = 0; i < vdr; ++i) {
  4802. // SIMD dot product of quantized values
  4803. sumi = dpct::dp4a(v[i], u[i], sumi);
  4804. }
  4805. #ifdef GGML_SYCL_F16
  4806. const sycl::float2 tmp =
  4807. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4808. const float d8d8 = tmp.x();
  4809. const float m8s8 = tmp.y();
  4810. #else
  4811. const sycl::float2 dm8f =
  4812. dm8.convert<float, sycl::rounding_mode::automatic>();
  4813. const sycl::float2 ds8f =
  4814. ds8.convert<float, sycl::rounding_mode::automatic>();
  4815. const float d8d8 = dm8f.x() * ds8f.x();
  4816. const float m8s8 = dm8f.y() * ds8f.y();
  4817. #endif // GGML_SYCL_F16
  4818. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  4819. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  4820. }
  4821. #define VDR_Q2_K_Q8_1_MMVQ 1
  4822. #define VDR_Q2_K_Q8_1_MMQ 2
  4823. // contiguous v/x values
  4824. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  4825. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  4826. const sycl::half2 &dm2, const float *__restrict__ d8) {
  4827. float sumf_d = 0.0f;
  4828. float sumf_m = 0.0f;
  4829. #pragma unroll
  4830. for (int i = 0; i < QR2_K; ++i) {
  4831. const int sc = scales[2*i];
  4832. const int vi = (v >> (2*i)) & 0x03030303;
  4833. sumf_d +=
  4834. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  4835. // fill int with 4x m
  4836. int m = sc >> 4;
  4837. m |= m << 8;
  4838. m |= m << 16;
  4839. sumf_m += d8[i] *
  4840. dpct::dp4a(
  4841. m, u[i],
  4842. 0); // multiply constant q2_K part with sum of q8_1 values
  4843. }
  4844. const sycl::float2 dm2f =
  4845. dm2.convert<float, sycl::rounding_mode::automatic>();
  4846. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  4847. }
  4848. // contiguous u/y values
  4849. static __dpct_inline__ float
  4850. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4851. const uint8_t *__restrict__ scales,
  4852. const sycl::half2 &dm2, const float &d8) {
  4853. int sumi_d = 0;
  4854. int sumi_m = 0;
  4855. #pragma unroll
  4856. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  4857. int sumi_d_sc = 0;
  4858. const int sc = scales[i0 / (QI8_1/2)];
  4859. // fill int with 4x m
  4860. int m = sc >> 4;
  4861. m |= m << 8;
  4862. m |= m << 16;
  4863. #pragma unroll
  4864. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4865. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  4866. sumi_m = dpct::dp4a(m, u[i],
  4867. sumi_m); // multiply sum of q8_1 values with m
  4868. }
  4869. sumi_d += sumi_d_sc * (sc & 0xF);
  4870. }
  4871. const sycl::float2 dm2f =
  4872. dm2.convert<float, sycl::rounding_mode::automatic>();
  4873. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  4874. }
  4875. #define VDR_Q3_K_Q8_1_MMVQ 1
  4876. #define VDR_Q3_K_Q8_1_MMQ 2
  4877. // contiguous v/x values
  4878. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  4879. const int &vl, const int &vh, const int *__restrict__ u,
  4880. const uint8_t *__restrict__ scales, const int &scale_offset,
  4881. const float &d3, const float *__restrict__ d8) {
  4882. float sumf = 0.0f;
  4883. #pragma unroll
  4884. for (int i = 0; i < QR3_K; ++i) {
  4885. const int isc = scale_offset + 2*i;
  4886. const int isc_low = isc % (QK_K/32);
  4887. const int sc_shift_low = 4 * (isc / (QK_K/32));
  4888. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  4889. const int isc_high = isc % (QK_K/64);
  4890. const int sc_shift_high = 2 * (isc / (QK_K/64));
  4891. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  4892. const int sc = (sc_low | sc_high) - 32;
  4893. const int vil = (vl >> (2*i)) & 0x03030303;
  4894. const int vih = ((vh >> i) << 2) & 0x04040404;
  4895. const int vi =
  4896. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  4897. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4898. }
  4899. return d3 * sumf;
  4900. }
  4901. // contiguous u/y values
  4902. static __dpct_inline__ float
  4903. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4904. const int8_t *__restrict__ scales, const float &d3,
  4905. const float &d8) {
  4906. int sumi = 0;
  4907. #pragma unroll
  4908. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  4909. int sumi_sc = 0;
  4910. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4911. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  4912. }
  4913. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  4914. }
  4915. return d3*d8 * sumi;
  4916. }
  4917. #define VDR_Q4_K_Q8_1_MMVQ 2
  4918. #define VDR_Q4_K_Q8_1_MMQ 8
  4919. // contiguous v/x values
  4920. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  4921. const int *__restrict__ v, const int *__restrict__ u,
  4922. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4923. const sycl::half2 &dm4, const float *__restrict__ d8) {
  4924. float sumf_d = 0.0f;
  4925. float sumf_m = 0.0f;
  4926. #pragma unroll
  4927. for (int i = 0; i < QR4_K; ++i) {
  4928. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  4929. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  4930. const int dot1 =
  4931. dpct::dp4a(v1i, u[2 * i + 1],
  4932. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  4933. const int dot2 =
  4934. dpct::dp4a(0x01010101, u[2 * i + 1],
  4935. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  4936. sumf_d += d8[i] * (dot1 * sc[i]);
  4937. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  4938. }
  4939. const sycl::float2 dm4f =
  4940. dm4.convert<float, sycl::rounding_mode::automatic>();
  4941. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4942. }
  4943. // contiguous u/y values
  4944. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  4945. const int *__restrict__ v, const int *__restrict__ u,
  4946. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4947. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4948. float sumf_d = 0.0f;
  4949. float sumf_m = 0.0f;
  4950. #pragma unroll
  4951. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  4952. int sumi_d = 0;
  4953. #pragma unroll
  4954. for (int j = 0; j < QI8_1; ++j) {
  4955. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  4956. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  4957. }
  4958. const sycl::float2 ds8f =
  4959. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4960. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4961. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4962. }
  4963. const sycl::float2 dm4f =
  4964. dm4.convert<float, sycl::rounding_mode::automatic>();
  4965. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4966. }
  4967. #define VDR_Q5_K_Q8_1_MMVQ 2
  4968. #define VDR_Q5_K_Q8_1_MMQ 8
  4969. // contiguous v/x values
  4970. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  4971. const int *__restrict__ vl, const int *__restrict__ vh,
  4972. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  4973. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  4974. const float *__restrict__ d8) {
  4975. float sumf_d = 0.0f;
  4976. float sumf_m = 0.0f;
  4977. #pragma unroll
  4978. for (int i = 0; i < QR5_K; ++i) {
  4979. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  4980. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  4981. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  4982. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  4983. const int v0i = vl0i | vh0i;
  4984. const int v1i = vl1i | vh1i;
  4985. const int dot1 =
  4986. dpct::dp4a(v0i, u[2 * i + 0],
  4987. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  4988. const int dot2 =
  4989. dpct::dp4a(0x01010101, u[2 * i + 0],
  4990. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  4991. sumf_d += d8[i] * (dot1 * sc[i]);
  4992. sumf_m += d8[i] * (dot2 * m[i]);
  4993. }
  4994. const sycl::float2 dm5f =
  4995. dm5.convert<float, sycl::rounding_mode::automatic>();
  4996. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  4997. }
  4998. // contiguous u/y values
  4999. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  5000. const int *__restrict__ v, const int *__restrict__ u,
  5001. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5002. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5003. float sumf_d = 0.0f;
  5004. float sumf_m = 0.0f;
  5005. #pragma unroll
  5006. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  5007. int sumi_d = 0;
  5008. #pragma unroll
  5009. for (int j = 0; j < QI8_1; ++j) {
  5010. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  5011. sumi_d); // SIMD dot product
  5012. }
  5013. const sycl::float2 ds8f =
  5014. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5015. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5016. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5017. }
  5018. const sycl::float2 dm4f =
  5019. dm4.convert<float, sycl::rounding_mode::automatic>();
  5020. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5021. }
  5022. #define VDR_Q6_K_Q8_1_MMVQ 1
  5023. #define VDR_Q6_K_Q8_1_MMQ 8
  5024. // contiguous v/x values
  5025. static __dpct_inline__ float
  5026. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  5027. const int *__restrict__ u,
  5028. const int8_t *__restrict__ scales, const float &d,
  5029. const float *__restrict__ d8) {
  5030. float sumf = 0.0f;
  5031. #pragma unroll
  5032. for (int i = 0; i < QR6_K; ++i) {
  5033. const int sc = scales[4*i];
  5034. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  5035. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  5036. const int vi = dpct::vectorized_binary<sycl::char4>(
  5037. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  5038. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  5039. }
  5040. return d*sumf;
  5041. }
  5042. // contiguous u/y values
  5043. static __dpct_inline__ float
  5044. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5045. const int8_t *__restrict__ sc, const float &d6,
  5046. const float *__restrict__ d8) {
  5047. float sumf_d = 0.0f;
  5048. #pragma unroll
  5049. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  5050. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  5051. #pragma unroll
  5052. for (int i = i0; i < i0 + 2; ++i) {
  5053. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  5054. sumi_d.x()); // SIMD dot product
  5055. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  5056. sumi_d.x()); // SIMD dot product
  5057. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  5058. sumi_d.y()); // SIMD dot product
  5059. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  5060. sumi_d.y()); // SIMD dot product
  5061. }
  5062. sumf_d += d8[i0 / 4] *
  5063. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  5064. }
  5065. return d6 * sumf_d;
  5066. }
  5067. static __dpct_inline__ float
  5068. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  5069. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5070. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  5071. int v[VDR_Q4_0_Q8_1_MMVQ];
  5072. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  5073. #pragma unroll
  5074. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  5075. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  5076. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5077. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  5078. }
  5079. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  5080. }
  5081. template <int mmq_y>
  5082. static __dpct_inline__ void
  5083. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5084. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  5085. (void)x_qh; (void)x_sc;
  5086. *x_ql = tile_x_qs_q4_0;
  5087. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  5088. }
  5089. template <int mmq_y, int nwarps, bool need_check>
  5090. static __dpct_inline__ void
  5091. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5092. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5093. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5094. const int &k, const int &blocks_per_row) {
  5095. (void)x_qh; (void)x_sc;
  5096. GGML_SYCL_ASSUME(i_offset >= 0);
  5097. GGML_SYCL_ASSUME(i_offset < nwarps);
  5098. GGML_SYCL_ASSUME(k >= 0);
  5099. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5100. const int kbx = k / QI4_0;
  5101. const int kqsx = k % QI4_0;
  5102. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  5103. float * x_dmf = (float *) x_dm;
  5104. #pragma unroll
  5105. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5106. int i = i0 + i_offset;
  5107. if (need_check) {
  5108. i = sycl::min(i, i_max);
  5109. }
  5110. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5111. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5112. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  5113. }
  5114. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  5115. const int kbxd = k % blocks_per_tile_x_row;
  5116. #pragma unroll
  5117. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  5118. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  5119. if (need_check) {
  5120. i = sycl::min(i, i_max);
  5121. }
  5122. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5123. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  5124. }
  5125. }
  5126. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  5127. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5128. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5129. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5130. const int &i, const int &j, const int &k) {
  5131. (void)x_qh; (void)x_sc;
  5132. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5133. const float * x_dmf = (const float *) x_dm;
  5134. int u[2*VDR_Q4_0_Q8_1_MMQ];
  5135. #pragma unroll
  5136. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  5137. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5138. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  5139. }
  5140. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  5141. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  5142. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5143. }
  5144. static __dpct_inline__ float
  5145. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  5146. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5147. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  5148. int v[VDR_Q4_1_Q8_1_MMVQ];
  5149. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  5150. #pragma unroll
  5151. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  5152. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  5153. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5154. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  5155. }
  5156. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  5157. }
  5158. template <int mmq_y>
  5159. static __dpct_inline__ void
  5160. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5161. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  5162. (void)x_qh; (void)x_sc;
  5163. *x_ql = tile_x_qs_q4_1;
  5164. *x_dm = tile_x_dm_q4_1;
  5165. }
  5166. template <int mmq_y, int nwarps, bool need_check>
  5167. static __dpct_inline__ void
  5168. load_tiles_q4_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5169. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5170. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5171. const int &k, const int &blocks_per_row) {
  5172. (void)x_qh; (void)x_sc;
  5173. GGML_SYCL_ASSUME(i_offset >= 0);
  5174. GGML_SYCL_ASSUME(i_offset < nwarps);
  5175. GGML_SYCL_ASSUME(k >= 0);
  5176. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5177. const int kbx = k / QI4_1;
  5178. const int kqsx = k % QI4_1;
  5179. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  5180. #pragma unroll
  5181. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5182. int i = i0 + i_offset;
  5183. if (need_check) {
  5184. i = sycl::min(i, i_max);
  5185. }
  5186. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5187. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5188. }
  5189. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  5190. const int kbxd = k % blocks_per_tile_x_row;
  5191. #pragma unroll
  5192. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  5193. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  5194. if (need_check) {
  5195. i = sycl::min(i, i_max);
  5196. }
  5197. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5198. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  5199. }
  5200. }
  5201. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  5202. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5203. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5204. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5205. const int &i, const int &j, const int &k) {
  5206. (void)x_qh; (void)x_sc;
  5207. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5208. int u[2*VDR_Q4_1_Q8_1_MMQ];
  5209. #pragma unroll
  5210. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  5211. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5212. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  5213. }
  5214. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  5215. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  5216. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5217. }
  5218. static __dpct_inline__ float
  5219. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  5220. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5221. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  5222. int vl[VDR_Q5_0_Q8_1_MMVQ];
  5223. int vh[VDR_Q5_0_Q8_1_MMVQ];
  5224. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  5225. #pragma unroll
  5226. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  5227. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  5228. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  5229. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5230. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  5231. }
  5232. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  5233. }
  5234. template <int mmq_y>
  5235. static __dpct_inline__ void
  5236. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5237. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  5238. (void)x_qh; (void)x_sc;
  5239. *x_ql = tile_x_ql_q5_0;
  5240. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  5241. }
  5242. template <int mmq_y, int nwarps, bool need_check>
  5243. static __dpct_inline__ void
  5244. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5245. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5246. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5247. const int &k, const int &blocks_per_row) {
  5248. (void)x_qh; (void)x_sc;
  5249. GGML_SYCL_ASSUME(i_offset >= 0);
  5250. GGML_SYCL_ASSUME(i_offset < nwarps);
  5251. GGML_SYCL_ASSUME(k >= 0);
  5252. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5253. const int kbx = k / QI5_0;
  5254. const int kqsx = k % QI5_0;
  5255. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5256. #pragma unroll
  5257. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5258. int i = i0 + i_offset;
  5259. if (need_check) {
  5260. i = sycl::min(i, i_max);
  5261. }
  5262. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5263. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5264. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5265. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5266. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5267. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5268. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5269. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5270. qs0 = dpct::vectorized_binary<sycl::char4>(
  5271. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5272. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5273. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5274. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5275. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5276. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5277. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5278. qs1 = dpct::vectorized_binary<sycl::char4>(
  5279. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  5280. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5281. }
  5282. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  5283. const int kbxd = k % blocks_per_tile_x_row;
  5284. float * x_dmf = (float *) x_dm;
  5285. #pragma unroll
  5286. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  5287. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  5288. if (need_check) {
  5289. i = sycl::min(i, i_max);
  5290. }
  5291. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5292. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  5293. }
  5294. }
  5295. static __dpct_inline__ float vec_dot_q5_0_q8_1_mul_mat(
  5296. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5297. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5298. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5299. const int &i, const int &j, const int &k) {
  5300. (void)x_qh; (void)x_sc;
  5301. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5302. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  5303. const float * x_dmf = (const float *) x_dm;
  5304. const float * y_df = (const float *) y_ds;
  5305. int u[2*VDR_Q5_0_Q8_1_MMQ];
  5306. #pragma unroll
  5307. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  5308. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5309. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  5310. }
  5311. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  5312. (&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)]);
  5313. }
  5314. static __dpct_inline__ float
  5315. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  5316. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5317. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  5318. int vl[VDR_Q5_1_Q8_1_MMVQ];
  5319. int vh[VDR_Q5_1_Q8_1_MMVQ];
  5320. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  5321. #pragma unroll
  5322. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  5323. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  5324. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  5325. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5326. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  5327. }
  5328. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  5329. }
  5330. template <int mmq_y>
  5331. static __dpct_inline__ void
  5332. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5333. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  5334. (void)x_qh; (void)x_sc;
  5335. *x_ql = tile_x_ql_q5_1;
  5336. *x_dm = tile_x_dm_q5_1;
  5337. }
  5338. template <int mmq_y, int nwarps, bool need_check>
  5339. static __dpct_inline__ void
  5340. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5341. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5342. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5343. const int &k, const int &blocks_per_row) {
  5344. (void)x_qh; (void)x_sc;
  5345. GGML_SYCL_ASSUME(i_offset >= 0);
  5346. GGML_SYCL_ASSUME(i_offset < nwarps);
  5347. GGML_SYCL_ASSUME(k >= 0);
  5348. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5349. const int kbx = k / QI5_1;
  5350. const int kqsx = k % QI5_1;
  5351. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  5352. #pragma unroll
  5353. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5354. int i = i0 + i_offset;
  5355. if (need_check) {
  5356. i = sycl::min(i, i_max);
  5357. }
  5358. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5359. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5360. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  5361. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5362. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5363. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5364. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5365. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5366. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5367. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5368. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5369. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5370. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5371. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5372. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5373. }
  5374. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  5375. const int kbxd = k % blocks_per_tile_x_row;
  5376. #pragma unroll
  5377. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  5378. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  5379. if (need_check) {
  5380. i = sycl::min(i, i_max);
  5381. }
  5382. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5383. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  5384. }
  5385. }
  5386. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  5387. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5388. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5389. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5390. const int &i, const int &j, const int &k) {
  5391. (void)x_qh; (void)x_sc;
  5392. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5393. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  5394. int u[2*VDR_Q5_1_Q8_1_MMQ];
  5395. #pragma unroll
  5396. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  5397. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5398. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  5399. }
  5400. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  5401. (&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)]);
  5402. }
  5403. static __dpct_inline__ float
  5404. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  5405. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5406. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  5407. int v[VDR_Q8_0_Q8_1_MMVQ];
  5408. int u[VDR_Q8_0_Q8_1_MMVQ];
  5409. #pragma unroll
  5410. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  5411. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  5412. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5413. }
  5414. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  5415. bq8_1->ds[0]);
  5416. }
  5417. template <int mmq_y>
  5418. static __dpct_inline__ void
  5419. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5420. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  5421. (void)x_qh; (void)x_sc;
  5422. *x_ql = tile_x_qs_q8_0;
  5423. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  5424. }
  5425. template <int mmq_y, int nwarps, bool need_check>
  5426. static __dpct_inline__ void
  5427. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5428. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5429. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5430. const int &k, const int &blocks_per_row) {
  5431. (void)x_qh; (void)x_sc;
  5432. GGML_SYCL_ASSUME(i_offset >= 0);
  5433. GGML_SYCL_ASSUME(i_offset < nwarps);
  5434. GGML_SYCL_ASSUME(k >= 0);
  5435. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5436. const int kbx = k / QI8_0;
  5437. const int kqsx = k % QI8_0;
  5438. float * x_dmf = (float *) x_dm;
  5439. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  5440. #pragma unroll
  5441. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5442. int i = i0 + i_offset;
  5443. if (need_check) {
  5444. i = sycl::min(i, i_max);
  5445. }
  5446. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5447. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  5448. }
  5449. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  5450. const int kbxd = k % blocks_per_tile_x_row;
  5451. #pragma unroll
  5452. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  5453. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  5454. if (need_check) {
  5455. i = sycl::min(i, i_max);
  5456. }
  5457. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5458. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  5459. }
  5460. }
  5461. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  5462. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5463. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5464. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5465. const int &i, const int &j, const int &k) {
  5466. (void)x_qh; (void)x_sc;
  5467. const float * x_dmf = (const float *) x_dm;
  5468. const float * y_df = (const float *) y_ds;
  5469. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  5470. (&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],
  5471. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  5472. }
  5473. static __dpct_inline__ float
  5474. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  5475. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5476. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  5477. const int bq8_offset = QR2_K * (iqs / QI8_1);
  5478. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5479. const uint8_t * scales = bq2_K->scales + scale_offset;
  5480. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  5481. int u[QR2_K];
  5482. float d8[QR2_K];
  5483. #pragma unroll
  5484. for (int i = 0; i < QR2_K; ++ i) {
  5485. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5486. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5487. }
  5488. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  5489. }
  5490. template <int mmq_y>
  5491. static __dpct_inline__ void
  5492. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5493. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  5494. int *tile_x_sc_q2_K) {
  5495. (void)x_qh;
  5496. *x_ql = tile_x_ql_q2_K;
  5497. *x_dm = tile_x_dm_q2_K;
  5498. *x_sc = tile_x_sc_q2_K;
  5499. }
  5500. template <int mmq_y, int nwarps, bool need_check>
  5501. static __dpct_inline__ void
  5502. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5503. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5504. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5505. const int &k, const int &blocks_per_row) {
  5506. (void)x_qh;
  5507. GGML_SYCL_ASSUME(i_offset >= 0);
  5508. GGML_SYCL_ASSUME(i_offset < nwarps);
  5509. GGML_SYCL_ASSUME(k >= 0);
  5510. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5511. const int kbx = k / QI2_K;
  5512. const int kqsx = k % QI2_K;
  5513. const block_q2_K * bx0 = (const block_q2_K *) vx;
  5514. #pragma unroll
  5515. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5516. int i = i0 + i_offset;
  5517. if (need_check) {
  5518. i = sycl::min(i, i_max);
  5519. }
  5520. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  5521. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5522. }
  5523. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  5524. const int kbxd = k % blocks_per_tile_x_row;
  5525. #pragma unroll
  5526. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  5527. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  5528. if (need_check) {
  5529. i = sycl::min(i, i_max);
  5530. }
  5531. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5532. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  5533. }
  5534. #pragma unroll
  5535. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5536. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5537. if (need_check) {
  5538. i = sycl::min(i, i_max);
  5539. }
  5540. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  5541. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  5542. }
  5543. }
  5544. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  5545. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5546. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5547. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5548. const int &i, const int &j, const int &k) {
  5549. (void)x_qh;
  5550. const int kbx = k / QI2_K;
  5551. const int ky = (k % QI2_K) * QR2_K;
  5552. const float * y_df = (const float *) y_ds;
  5553. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  5554. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  5555. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  5556. #pragma unroll
  5557. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  5558. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5559. }
  5560. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  5561. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  5562. 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]);
  5563. }
  5564. static __dpct_inline__ float
  5565. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  5566. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5567. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  5568. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  5569. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5570. const float d = bq3_K->d;
  5571. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  5572. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5573. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  5574. int u[QR3_K];
  5575. float d8[QR3_K];
  5576. #pragma unroll
  5577. for (int i = 0; i < QR3_K; ++i) {
  5578. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5579. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5580. }
  5581. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  5582. }
  5583. template <int mmq_y>
  5584. static __dpct_inline__ void
  5585. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5586. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  5587. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  5588. *x_ql = tile_x_ql_q3_K;
  5589. *x_dm = tile_x_dm_q3_K;
  5590. *x_qh = tile_x_qh_q3_K;
  5591. *x_sc = tile_x_sc_q3_K;
  5592. }
  5593. template <int mmq_y, int nwarps, bool need_check>
  5594. static __dpct_inline__ void
  5595. load_tiles_q3_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5596. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5597. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5598. const int &k, const int &blocks_per_row) {
  5599. GGML_SYCL_ASSUME(i_offset >= 0);
  5600. GGML_SYCL_ASSUME(i_offset < nwarps);
  5601. GGML_SYCL_ASSUME(k >= 0);
  5602. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5603. const int kbx = k / QI3_K;
  5604. const int kqsx = k % QI3_K;
  5605. const block_q3_K * bx0 = (const block_q3_K *) vx;
  5606. #pragma unroll
  5607. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5608. int i = i0 + i_offset;
  5609. if (need_check) {
  5610. i = sycl::min(i, i_max);
  5611. }
  5612. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  5613. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5614. }
  5615. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  5616. const int kbxd = k % blocks_per_tile_x_row;
  5617. float * x_dmf = (float *) x_dm;
  5618. #pragma unroll
  5619. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  5620. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  5621. if (need_check) {
  5622. i = sycl::min(i, i_max);
  5623. }
  5624. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5625. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  5626. }
  5627. #pragma unroll
  5628. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  5629. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  5630. if (need_check) {
  5631. i = sycl::min(i, i_max);
  5632. }
  5633. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  5634. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5635. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  5636. }
  5637. #pragma unroll
  5638. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5639. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5640. if (need_check) {
  5641. i = sycl::min(i, i_max);
  5642. }
  5643. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  5644. const int ksc = k % (QI3_K/4);
  5645. const int ksc_low = ksc % (QI3_K/8);
  5646. const int shift_low = 4 * (ksc / (QI3_K/8));
  5647. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  5648. const int ksc_high = QI3_K/8;
  5649. const int shift_high = 2 * ksc;
  5650. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  5651. const int sc = dpct::vectorized_binary<sycl::char4>(
  5652. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  5653. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  5654. }
  5655. }
  5656. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  5657. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5658. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5659. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5660. const int &i, const int &j, const int &k) {
  5661. const int kbx = k / QI3_K;
  5662. const int ky = (k % QI3_K) * QR3_K;
  5663. const float * x_dmf = (const float *) x_dm;
  5664. const float * y_df = (const float *) y_ds;
  5665. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  5666. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  5667. #pragma unroll
  5668. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  5669. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  5670. const int shift = 2 * ((ky % 32) / 8);
  5671. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5672. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  5673. const int vlh = (vh << 2) & 0x04040404;
  5674. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  5675. }
  5676. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  5677. 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]);
  5678. }
  5679. static __dpct_inline__ float
  5680. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  5681. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5682. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5683. int v[2];
  5684. int u[2*QR4_K];
  5685. float d8[QR4_K];
  5686. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  5687. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  5688. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  5689. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  5690. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  5691. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  5692. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5693. v[0] = q4[0];
  5694. v[1] = q4[4];
  5695. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  5696. uint16_t aux[2];
  5697. const int j = bq8_offset/2;
  5698. if (j < 2) {
  5699. aux[0] = scales[j+0] & 0x3f3f;
  5700. aux[1] = scales[j+2] & 0x3f3f;
  5701. } else {
  5702. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5703. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5704. }
  5705. const uint8_t * sc = (const uint8_t *)aux;
  5706. const uint8_t * m = sc + 2;
  5707. for (int i = 0; i < QR4_K; ++i) {
  5708. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5709. d8[i] = bq8i->ds[0];
  5710. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5711. u[2*i+0] = q8[0];
  5712. u[2*i+1] = q8[4];
  5713. }
  5714. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  5715. }
  5716. template <int mmq_y>
  5717. static __dpct_inline__ void
  5718. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5719. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  5720. int *tile_x_sc_q4_K) {
  5721. (void)x_qh;
  5722. *x_ql = tile_x_ql_q4_K;
  5723. *x_dm = tile_x_dm_q4_K;
  5724. *x_sc = tile_x_sc_q4_K;
  5725. }
  5726. template <int mmq_y, int nwarps, bool need_check>
  5727. static __dpct_inline__ void
  5728. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5729. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5730. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5731. const int &k, const int &blocks_per_row) {
  5732. (void)x_qh;
  5733. GGML_SYCL_ASSUME(i_offset >= 0);
  5734. GGML_SYCL_ASSUME(i_offset < nwarps);
  5735. GGML_SYCL_ASSUME(k >= 0);
  5736. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5737. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  5738. const int kqsx = k % QI4_K; // == k if QK_K == 256
  5739. const block_q4_K * bx0 = (const block_q4_K *) vx;
  5740. #pragma unroll
  5741. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5742. int i = i0 + i_offset;
  5743. if (need_check) {
  5744. i = sycl::min(i, i_max);
  5745. }
  5746. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  5747. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5748. }
  5749. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  5750. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5751. #pragma unroll
  5752. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  5753. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  5754. if (need_check) {
  5755. i = sycl::min(i, i_max);
  5756. }
  5757. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5758. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  5759. }
  5760. #pragma unroll
  5761. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5762. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5763. if (need_check) {
  5764. i = sycl::min(i, i_max);
  5765. }
  5766. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  5767. const int * scales = (const int *) bxi->scales;
  5768. const int ksc = k % (WARP_SIZE/8);
  5769. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5770. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5771. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5772. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5773. }
  5774. }
  5775. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  5776. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5777. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5778. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5779. const int &i, const int &j, const int &k) {
  5780. (void)x_qh;
  5781. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  5782. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  5783. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  5784. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  5785. }
  5786. static __dpct_inline__ float
  5787. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  5788. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5789. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5790. int vl[2];
  5791. int vh[2];
  5792. int u[2*QR5_K];
  5793. float d8[QR5_K];
  5794. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  5795. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5796. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  5797. vl[0] = ql[0];
  5798. vl[1] = ql[4];
  5799. vh[0] = qh[0] >> bq8_offset;
  5800. vh[1] = qh[4] >> bq8_offset;
  5801. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  5802. uint16_t aux[2];
  5803. const int j = bq8_offset/2;
  5804. if (j < 2) {
  5805. aux[0] = scales[j+0] & 0x3f3f;
  5806. aux[1] = scales[j+2] & 0x3f3f;
  5807. } else {
  5808. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5809. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5810. }
  5811. const uint8_t * sc = (const uint8_t *)aux;
  5812. const uint8_t * m = sc + 2;
  5813. #pragma unroll
  5814. for (int i = 0; i < QR5_K; ++i) {
  5815. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5816. d8[i] = bq8i->ds[0];
  5817. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5818. u[2*i+0] = q8[0];
  5819. u[2*i+1] = q8[4];
  5820. }
  5821. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  5822. }
  5823. template <int mmq_y>
  5824. static __dpct_inline__ void
  5825. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5826. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  5827. int *tile_x_sc_q5_K) {
  5828. (void)x_qh;
  5829. *x_ql = tile_x_ql_q5_K;
  5830. *x_dm = tile_x_dm_q5_K;
  5831. *x_sc = tile_x_sc_q5_K;
  5832. }
  5833. template <int mmq_y, int nwarps, bool need_check>
  5834. static __dpct_inline__ void
  5835. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5836. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5837. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5838. const int &k, const int &blocks_per_row) {
  5839. (void)x_qh;
  5840. GGML_SYCL_ASSUME(i_offset >= 0);
  5841. GGML_SYCL_ASSUME(i_offset < nwarps);
  5842. GGML_SYCL_ASSUME(k >= 0);
  5843. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5844. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  5845. const int kqsx = k % QI5_K; // == k if QK_K == 256
  5846. const block_q5_K * bx0 = (const block_q5_K *) vx;
  5847. #pragma unroll
  5848. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5849. int i = i0 + i_offset;
  5850. if (need_check) {
  5851. i = sycl::min(i, i_max);
  5852. }
  5853. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  5854. const int ky = QR5_K*kqsx;
  5855. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5856. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5857. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5858. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  5859. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  5860. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  5861. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  5862. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  5863. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  5864. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  5865. }
  5866. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  5867. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5868. #pragma unroll
  5869. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  5870. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  5871. if (need_check) {
  5872. i = sycl::min(i, i_max);
  5873. }
  5874. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5875. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  5876. }
  5877. #pragma unroll
  5878. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5879. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5880. if (need_check) {
  5881. i = sycl::min(i, i_max);
  5882. }
  5883. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  5884. const int * scales = (const int *) bxi->scales;
  5885. const int ksc = k % (WARP_SIZE/8);
  5886. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5887. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5888. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5889. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5890. }
  5891. }
  5892. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  5893. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5894. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5895. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5896. const int &i, const int &j, const int &k) {
  5897. (void)x_qh;
  5898. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  5899. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  5900. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  5901. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  5902. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  5903. }
  5904. static __dpct_inline__ float
  5905. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  5906. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5907. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  5908. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  5909. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  5910. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  5911. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  5912. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  5913. const int8_t * scales = bq6_K->scales + scale_offset;
  5914. int u[QR6_K];
  5915. float d8[QR6_K];
  5916. #pragma unroll
  5917. for (int i = 0; i < QR6_K; ++i) {
  5918. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  5919. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  5920. }
  5921. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  5922. }
  5923. template <int mmq_y>
  5924. static __dpct_inline__ void
  5925. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5926. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  5927. (void)x_qh;
  5928. *x_ql = tile_x_ql;
  5929. *x_dm = tile_x_dm;
  5930. *x_sc = tile_x_sc;
  5931. }
  5932. template <int mmq_y, int nwarps, bool need_check>
  5933. static __dpct_inline__ void
  5934. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5935. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5936. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5937. const int &k, const int &blocks_per_row) {
  5938. (void)x_qh;
  5939. GGML_SYCL_ASSUME(i_offset >= 0);
  5940. GGML_SYCL_ASSUME(i_offset < nwarps);
  5941. GGML_SYCL_ASSUME(k >= 0);
  5942. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5943. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  5944. const int kqsx = k % QI6_K; // == k if QK_K == 256
  5945. const block_q6_K * bx0 = (const block_q6_K *) vx;
  5946. #pragma unroll
  5947. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5948. int i = i0 + i_offset;
  5949. if (need_check) {
  5950. i = sycl::min(i, i_max);
  5951. }
  5952. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  5953. const int ky = QR6_K*kqsx;
  5954. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  5955. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5956. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5957. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  5958. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  5959. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  5960. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  5961. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  5962. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  5963. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  5964. dpct::sub_sat());
  5965. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  5966. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  5967. dpct::sub_sat());
  5968. }
  5969. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  5970. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5971. float * x_dmf = (float *) x_dm;
  5972. #pragma unroll
  5973. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  5974. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  5975. if (need_check) {
  5976. i = sycl::min(i, i_max);
  5977. }
  5978. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5979. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  5980. }
  5981. #pragma unroll
  5982. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5983. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5984. if (need_check) {
  5985. i = sycl::min(i, i_max);
  5986. }
  5987. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  5988. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  5989. }
  5990. }
  5991. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  5992. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5993. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5994. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5995. const int &i, const int &j, const int &k) {
  5996. (void)x_qh;
  5997. const float * x_dmf = (const float *) x_dm;
  5998. const float * y_df = (const float *) y_ds;
  5999. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  6000. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  6001. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  6002. 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]);
  6003. }
  6004. static __dpct_inline__ float
  6005. vec_dot_iq2_xxs_q8_1(const void *__restrict__ vbq,
  6006. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6007. const uint64_t *iq2xxs_grid, const uint8_t *ksigns_iq2xs,
  6008. const uint8_t *kmask_iq2xs) {
  6009. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  6010. #if QR2_XXS == 8
  6011. const int ib32 = iqs;
  6012. const uint16_t * q2 = bq2->qs + 4*ib32;
  6013. const uint8_t * aux8 = (const uint8_t *)q2;
  6014. const int8_t * q8 = bq8_1[ib32].qs;
  6015. uint32_t aux32 = q2[2] | (q2[3] << 16);
  6016. int sumi = 0;
  6017. for (int l = 0; l < 4; ++l) {
  6018. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6019. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  6020. for (int j = 0; j < 8; ++j) {
  6021. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6022. }
  6023. q8 += 8;
  6024. aux32 >>= 7;
  6025. }
  6026. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.25f;
  6027. return d * sumi;
  6028. #else
  6029. // iqs is 0...15
  6030. const int ib32 = iqs/2;
  6031. const int il = iqs%2;
  6032. const uint16_t * q2 = bq2->qs + 4*ib32;
  6033. const uint8_t * aux8 = (const uint8_t *)q2;
  6034. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  6035. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  6036. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  6037. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * bq8_1[ib32].ds[0] * 0.25f;
  6038. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  6039. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  6040. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  6041. int sumi1 = 0, sumi2 = 0;
  6042. for (int j = 0; j < 8; ++j) {
  6043. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  6044. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  6045. }
  6046. return d * (sumi1 + sumi2);
  6047. #endif
  6048. }
  6049. static __dpct_inline__ float
  6050. vec_dot_iq2_xs_q8_1(const void *__restrict__ vbq,
  6051. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6052. const uint64_t *iq2xs_grid, const uint64_t *ksigns64) {
  6053. #if DPCT_COMPATIBILITY_TEMP >= \
  6054. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6055. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  6056. const int ib32 = iqs;
  6057. const uint16_t * q2 = bq2->qs + 4*ib32;
  6058. const int8_t * q8 = bq8_1[ib32].qs;
  6059. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6060. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6061. int sumi1 = 0;
  6062. for (int l = 0; l < 2; ++l) {
  6063. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6064. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6065. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6066. grid[0] ^ signs[0], signs[0], std::minus<>());
  6067. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6068. grid[1] ^ signs[1], signs[1], std::minus<>());
  6069. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6070. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6071. q8 += 8;
  6072. }
  6073. int sumi2 = 0;
  6074. for (int l = 2; l < 4; ++l) {
  6075. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6076. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6077. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6078. grid[0] ^ signs[0], signs[0], std::minus<>());
  6079. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6080. grid[1] ^ signs[1], signs[1], std::minus<>());
  6081. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6082. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6083. q8 += 8;
  6084. }
  6085. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6086. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6087. #else
  6088. assert(false);
  6089. return 0.f;
  6090. #endif
  6091. }
  6092. static __dpct_inline__ float
  6093. vec_dot_iq2_s_q8_1(const void *__restrict__ vbq,
  6094. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6095. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  6096. const int ib32 = iqs;
  6097. const int8_t * q8 = bq8_1[ib32].qs;
  6098. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  6099. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6100. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6101. int sumi1 = 0;
  6102. for (int l = 0; l < 2; ++l) {
  6103. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  6104. const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6105. ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201,
  6106. std::equal_to<>());
  6107. const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6108. ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201,
  6109. std::equal_to<>());
  6110. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6111. grid[0] ^ signs0, signs0, std::minus<>());
  6112. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6113. grid[1] ^ signs1, signs1, std::minus<>());
  6114. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6115. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6116. q8 += 8;
  6117. }
  6118. int sumi2 = 0;
  6119. for (int l = 2; l < 4; ++l) {
  6120. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  6121. const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6122. ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201,
  6123. std::equal_to<>());
  6124. const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6125. ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201,
  6126. std::equal_to<>());
  6127. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6128. grid[0] ^ signs0, signs0, std::minus<>());
  6129. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6130. grid[1] ^ signs1, signs1, std::minus<>());
  6131. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6132. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6133. q8 += 8;
  6134. }
  6135. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6136. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6137. }
  6138. static __dpct_inline__ float
  6139. vec_dot_iq3_xxs_q8_1(const void *__restrict__ vbq,
  6140. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6141. const uint32_t *iq3xxs_grid, const uint64_t *ksigns64) {
  6142. #if DPCT_COMPATIBILITY_TEMP >= \
  6143. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6144. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  6145. const int ib32 = iqs;
  6146. const uint8_t * q3 = bq2->qs + 8*ib32;
  6147. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  6148. const int8_t * q8 = bq8_1[ib32].qs;
  6149. uint32_t aux32 = gas[0] | (gas[1] << 16);
  6150. int sumi = 0;
  6151. for (int l = 0; l < 4; ++l) {
  6152. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  6153. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  6154. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  6155. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6156. grid1[0] ^ signs[0], signs[0], std::minus<>());
  6157. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6158. grid2[0] ^ signs[1], signs[1], std::minus<>());
  6159. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6160. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6161. q8 += 8;
  6162. aux32 >>= 7;
  6163. }
  6164. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.5f;
  6165. return d * sumi;
  6166. #else
  6167. assert(false);
  6168. return 0.f;
  6169. #endif
  6170. }
  6171. static __dpct_inline__ float
  6172. vec_dot_iq3_s_q8_1(const void *__restrict__ vbq,
  6173. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6174. const uint32_t *iq3s_grid) {
  6175. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  6176. const int ib32 = iqs;
  6177. const uint8_t * qs = bq2->qs + 8*ib32;
  6178. const int8_t * q8 = bq8_1[ib32].qs;
  6179. int sumi = 0;
  6180. for (int l = 0; l < 4; ++l) {
  6181. const uint32_t * grid1 = iq3s_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  6182. const uint32_t * grid2 = iq3s_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  6183. uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6184. ((bq2->signs[4 * ib32 + l] & 0xf) * 0x01010101) & 0x08040201,
  6185. 0x08040201, std::equal_to<>());
  6186. uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6187. ((bq2->signs[4 * ib32 + l] >> 4) * 0x01010101) & 0x08040201,
  6188. 0x08040201, std::equal_to<>());
  6189. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6190. grid1[0] ^ signs0, signs0, std::minus<>());
  6191. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6192. grid2[0] ^ signs1, signs1, std::minus<>());
  6193. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6194. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6195. q8 += 8;
  6196. }
  6197. const float d =
  6198. (float)bq2->d *
  6199. (1 + 2 * ((bq2->scales[ib32 / 2] >> 4 * (ib32 % 2)) & 0xf)) *
  6200. bq8_1[ib32].ds[0];
  6201. return d * sumi;
  6202. }
  6203. static __dpct_inline__ float
  6204. vec_dot_iq1_s_q8_1(const void *__restrict__ vbq,
  6205. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6206. const uint32_t *iq1s_grid_gpu) {
  6207. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  6208. const int ib32 = iqs;
  6209. int sumi = 0;
  6210. const int * q8 = (const int *)bq8_1[ib32].qs;
  6211. for (int l = 0; l < 4; ++l) {
  6212. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  6213. int grid0 = grid[0] & 0x0f0f0f0f;
  6214. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  6215. sumi = dpct::dp4a(q8[2 * l + 1], grid1,
  6216. dpct::dp4a(q8[2 * l + 0], grid0, sumi));
  6217. }
  6218. const float delta = bq1->qh[ib32] & 0x8000 ? -1-IQ1S_DELTA : -1+IQ1S_DELTA;
  6219. const float d1q = (float)bq1->d * (2*((bq1->qh[ib32] >> 12) & 7) + 1);
  6220. const float d = d1q * bq8_1[ib32].ds[0];
  6221. const float m = d1q * bq8_1[ib32].ds[1];
  6222. return d * sumi + m * delta;
  6223. }
  6224. static __dpct_inline__ float
  6225. vec_dot_iq1_m_q8_1(const void *__restrict__ vbq,
  6226. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6227. const block_iq1_m * bq1 = (const block_iq1_m *) vbq;
  6228. const int ib32 = iqs;
  6229. int sumi[2] = {0, 0};
  6230. float sumf[2] = {0.f, 0.f};
  6231. const int * q8 = (const int *)bq8_1[ib32].qs;
  6232. for (int l = 0; l < 4; ++l) {
  6233. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 7) << 8)));
  6234. int grid0 = grid[0] & 0x0f0f0f0f;
  6235. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  6236. sumi[l / 2] = dpct::dp4a(q8[2 * l + 1], grid1,
  6237. dpct::dp4a(q8[2 * l + 0], grid0, sumi[l / 2]));
  6238. const float delta = (bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 0x08 ? -1-IQ1M_DELTA : -1+IQ1M_DELTA;
  6239. const int sumy = dpct::dp4a(q8[2 * l + 1], 0x01010101,
  6240. dpct::dp4a(q8[2 * l + 0], 0x01010101, 0));
  6241. sumf[l/2] += delta*sumy;
  6242. }
  6243. iq1m_scale_t scale;
  6244. const uint16_t * sc = (const uint16_t *)bq1->scales;
  6245. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  6246. const float d = (float)scale.f16 * bq8_1[ib32].ds[0];
  6247. 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));
  6248. }
  6249. static __dpct_inline__ void get_int_from_table_16(const uint32_t &q4,
  6250. const uint8_t *values,
  6251. int &val1, int &val2) {
  6252. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  6253. aux32 = q4 & 0x0f0f0f0f;
  6254. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  6255. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  6256. val1 = v1 | (v2 << 16);
  6257. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  6258. v1 = values[q8[0]] | (values[q8[1]] << 8);
  6259. v2 = values[q8[2]] | (values[q8[3]] << 8);
  6260. val2 = v1 | (v2 << 16);
  6261. }
  6262. static __dpct_inline__ float
  6263. vec_dot_iq4_nl_q8_1(const void *__restrict__ vbq,
  6264. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6265. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  6266. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  6267. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  6268. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  6269. int v1, v2;
  6270. int sumi1 = 0, sumi2 = 0;
  6271. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  6272. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  6273. get_int_from_table_16(aux, values, v1, v2);
  6274. sumi1 = dpct::dp4a(v1, q8[l + 0], sumi1);
  6275. sumi2 = dpct::dp4a(v2, q8[l + 4], sumi2);
  6276. }
  6277. const float d = (float)bq->d * bq8_1->ds[0];
  6278. return d * (sumi1 + sumi2);
  6279. }
  6280. static __dpct_inline__ float
  6281. vec_dot_iq4_xs_q8_1(const void *__restrict__ vbq,
  6282. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6283. const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq;
  6284. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  6285. // iqs is 0...7
  6286. const int ib32 = iqs;
  6287. const int32_t * q8 = (const int *)bq8_1[ib32].qs;
  6288. const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32;
  6289. const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  6290. const float d = (float)bq4->d * (ls - 32) * bq8_1[ib32].ds[0];
  6291. int v1, v2;
  6292. int sumi1 = 0, sumi2 = 0;
  6293. for (int j = 0; j < 4; ++j) {
  6294. get_int_from_table_16(q4[j], values, v1, v2);
  6295. sumi1 = dpct::dp4a(v1, q8[j + 0], sumi1);
  6296. sumi2 = dpct::dp4a(v2, q8[j + 4], sumi2);
  6297. }
  6298. return d * (sumi1 + sumi2);
  6299. }
  6300. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  6301. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  6302. vec_dot_q_mul_mat_sycl_t vec_dot>
  6303. /*
  6304. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  6305. exceeds 128 bytes and may cause high register pressure. Consult with your
  6306. hardware vendor to find the total register size available and adjust the code,
  6307. or use smaller sub-group size to avoid high register pressure.
  6308. */
  6309. static __dpct_inline__ void
  6310. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  6311. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  6312. const int ncols_y, const int nrows_y, const int nrows_dst,
  6313. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  6314. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  6315. sycl::half2 *tile_y_ds) {
  6316. const block_q_t * x = (const block_q_t *) vx;
  6317. const block_q8_1 * y = (const block_q8_1 *) vy;
  6318. const int blocks_per_row_x = ncols_x / qk;
  6319. const int blocks_per_col_y = nrows_y / QK8_1;
  6320. const int blocks_per_warp = WARP_SIZE / qi;
  6321. const int & ncols_dst = ncols_y;
  6322. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  6323. const int & row_x_0 = row_dst_0;
  6324. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  6325. const int & col_y_0 = col_dst_0;
  6326. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  6327. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  6328. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  6329. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  6330. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  6331. blocks_per_row_x);
  6332. #pragma unroll
  6333. for (int ir = 0; ir < qr; ++ir) {
  6334. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  6335. const int kbxd = kqs / QI8_1;
  6336. #pragma unroll
  6337. for (int i = 0; i < mmq_x; i += nwarps) {
  6338. const int col_y_eff = dpct::min(
  6339. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  6340. ncols_y - 1); // to prevent out-of-bounds memory accesses
  6341. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  6342. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  6343. kqs % WARP_SIZE;
  6344. tile_y_qs[index_y] = get_int_from_int8_aligned(
  6345. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  6346. }
  6347. #pragma unroll
  6348. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  6349. const int ids =
  6350. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  6351. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  6352. mmq_x;
  6353. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  6354. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  6355. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  6356. const sycl::half2 *dsi_src =
  6357. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  6358. ir * (WARP_SIZE / QI8_1) + kby]
  6359. .ds;
  6360. sycl::half2 *dsi_dst =
  6361. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  6362. if (need_sum) {
  6363. *dsi_dst = *dsi_src;
  6364. } else {
  6365. float * dfi_dst = (float *) dsi_dst;
  6366. *dfi_dst = (*dsi_src)[0];
  6367. }
  6368. }
  6369. /*
  6370. DPCT1118:9: SYCL group functions and algorithms must be encountered
  6371. in converged control flow. You may need to adjust the code.
  6372. */
  6373. /*
  6374. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  6375. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6376. better performance if there is no access to global memory.
  6377. */
  6378. item_ct1.barrier();
  6379. // #pragma unroll // unrolling this loop causes too much register pressure
  6380. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  6381. #pragma unroll
  6382. for (int j = 0; j < mmq_x; j += nwarps) {
  6383. #pragma unroll
  6384. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6385. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  6386. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  6387. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  6388. item_ct1.get_local_id(1) + j, k);
  6389. }
  6390. }
  6391. }
  6392. /*
  6393. DPCT1118:10: SYCL group functions and algorithms must be encountered
  6394. in converged control flow. You may need to adjust the code.
  6395. */
  6396. /*
  6397. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  6398. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6399. better performance if there is no access to global memory.
  6400. */
  6401. item_ct1.barrier();
  6402. }
  6403. }
  6404. #pragma unroll
  6405. for (int j = 0; j < mmq_x; j += nwarps) {
  6406. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  6407. if (col_dst >= ncols_dst) {
  6408. return;
  6409. }
  6410. #pragma unroll
  6411. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6412. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  6413. if (row_dst >= nrows_dst) {
  6414. continue;
  6415. }
  6416. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  6417. }
  6418. }
  6419. }
  6420. #define MMQ_X_Q4_0_RDNA2 64
  6421. #define MMQ_Y_Q4_0_RDNA2 128
  6422. #define NWARPS_Q4_0_RDNA2 8
  6423. #define MMQ_X_Q4_0_RDNA1 64
  6424. #define MMQ_Y_Q4_0_RDNA1 64
  6425. #define NWARPS_Q4_0_RDNA1 8
  6426. #if defined(SYCL_USE_XMX)
  6427. #define MMQ_X_Q4_0_AMPERE 4
  6428. #define MMQ_Y_Q4_0_AMPERE 32
  6429. #define NWARPS_Q4_0_AMPERE 4
  6430. #else
  6431. #define MMQ_X_Q4_0_AMPERE 64
  6432. #define MMQ_Y_Q4_0_AMPERE 128
  6433. #define NWARPS_Q4_0_AMPERE 4
  6434. #endif
  6435. #define MMQ_X_Q4_0_PASCAL 64
  6436. #define MMQ_Y_Q4_0_PASCAL 64
  6437. #define NWARPS_Q4_0_PASCAL 8
  6438. template <bool need_check> static void
  6439. mul_mat_q4_0(
  6440. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6441. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6442. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  6443. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6444. int * tile_x_ql = nullptr;
  6445. sycl::half2 *tile_x_dm = nullptr;
  6446. int * tile_x_qh = nullptr;
  6447. int * tile_x_sc = nullptr;
  6448. //sycl_todo: change according to hardware
  6449. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  6450. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  6451. const int nwarps = NWARPS_Q4_0_AMPERE;
  6452. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6453. tile_x_qs_q4_0, tile_x_d_q4_0);
  6454. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  6455. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  6456. vec_dot_q4_0_q8_1_mul_mat>(
  6457. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6458. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6459. }
  6460. #define MMQ_X_Q4_1_RDNA2 64
  6461. #define MMQ_Y_Q4_1_RDNA2 128
  6462. #define NWARPS_Q4_1_RDNA2 8
  6463. #define MMQ_X_Q4_1_RDNA1 64
  6464. #define MMQ_Y_Q4_1_RDNA1 64
  6465. #define NWARPS_Q4_1_RDNA1 8
  6466. #if defined(SYCL_USE_XMX)
  6467. #define MMQ_X_Q4_1_AMPERE 4
  6468. #define MMQ_Y_Q4_1_AMPERE 32
  6469. #define NWARPS_Q4_1_AMPERE 4
  6470. #else
  6471. #define MMQ_X_Q4_1_AMPERE 64
  6472. #define MMQ_Y_Q4_1_AMPERE 128
  6473. #define NWARPS_Q4_1_AMPERE 4
  6474. #endif
  6475. #define MMQ_X_Q4_1_PASCAL 64
  6476. #define MMQ_Y_Q4_1_PASCAL 64
  6477. #define NWARPS_Q4_1_PASCAL 8
  6478. template <bool need_check> static void
  6479. mul_mat_q4_1(
  6480. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6481. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6482. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  6483. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6484. int * tile_x_ql = nullptr;
  6485. sycl::half2 *tile_x_dm = nullptr;
  6486. int * tile_x_qh = nullptr;
  6487. int * tile_x_sc = nullptr;
  6488. //sycl_todo: change according to hardware
  6489. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  6490. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  6491. const int nwarps = NWARPS_Q4_1_AMPERE;
  6492. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6493. tile_x_qs_q4_1, tile_x_dm_q4_1);
  6494. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  6495. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  6496. vec_dot_q4_1_q8_1_mul_mat>(
  6497. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6498. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6499. }
  6500. #define MMQ_X_Q5_0_RDNA2 64
  6501. #define MMQ_Y_Q5_0_RDNA2 128
  6502. #define NWARPS_Q5_0_RDNA2 8
  6503. #define MMQ_X_Q5_0_RDNA1 64
  6504. #define MMQ_Y_Q5_0_RDNA1 64
  6505. #define NWARPS_Q5_0_RDNA1 8
  6506. #if defined(SYCL_USE_XMX)
  6507. #define MMQ_X_Q5_0_AMPERE 4
  6508. #define MMQ_Y_Q5_0_AMPERE 32
  6509. #define NWARPS_Q5_0_AMPERE 4
  6510. #else
  6511. #define MMQ_X_Q5_0_AMPERE 128
  6512. #define MMQ_Y_Q5_0_AMPERE 64
  6513. #define NWARPS_Q5_0_AMPERE 4
  6514. #endif
  6515. #define MMQ_X_Q5_0_PASCAL 64
  6516. #define MMQ_Y_Q5_0_PASCAL 64
  6517. #define NWARPS_Q5_0_PASCAL 8
  6518. template <bool need_check> static void
  6519. mul_mat_q5_0(
  6520. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6521. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6522. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  6523. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6524. int * tile_x_ql = nullptr;
  6525. sycl::half2 *tile_x_dm = nullptr;
  6526. int * tile_x_qh = nullptr;
  6527. int * tile_x_sc = nullptr;
  6528. //sycl_todo: change according to hardware
  6529. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  6530. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  6531. const int nwarps = NWARPS_Q5_0_AMPERE;
  6532. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6533. tile_x_ql_q5_0, tile_x_d_q5_0);
  6534. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  6535. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  6536. vec_dot_q5_0_q8_1_mul_mat>(
  6537. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6538. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6539. }
  6540. #define MMQ_X_Q5_1_RDNA2 64
  6541. #define MMQ_Y_Q5_1_RDNA2 128
  6542. #define NWARPS_Q5_1_RDNA2 8
  6543. #define MMQ_X_Q5_1_RDNA1 64
  6544. #define MMQ_Y_Q5_1_RDNA1 64
  6545. #define NWARPS_Q5_1_RDNA1 8
  6546. #if defined(SYCL_USE_XMX)
  6547. #define MMQ_X_Q5_1_AMPERE 4
  6548. #define MMQ_Y_Q5_1_AMPERE 32
  6549. #define NWARPS_Q5_1_AMPERE 4
  6550. #else
  6551. #define MMQ_X_Q5_1_AMPERE 128
  6552. #define MMQ_Y_Q5_1_AMPERE 64
  6553. #define NWARPS_Q5_1_AMPERE 4
  6554. #endif
  6555. #define MMQ_X_Q5_1_PASCAL 64
  6556. #define MMQ_Y_Q5_1_PASCAL 64
  6557. #define NWARPS_Q5_1_PASCAL 8
  6558. template <bool need_check> static void
  6559. mul_mat_q5_1(
  6560. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6561. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6562. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  6563. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6564. int * tile_x_ql = nullptr;
  6565. sycl::half2 *tile_x_dm = nullptr;
  6566. int * tile_x_qh = nullptr;
  6567. int * tile_x_sc = nullptr;
  6568. //sycl_todo: change according to hardware
  6569. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  6570. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  6571. const int nwarps = NWARPS_Q5_1_AMPERE;
  6572. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6573. tile_x_ql_q5_1, tile_x_dm_q5_1);
  6574. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  6575. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  6576. vec_dot_q5_1_q8_1_mul_mat>(
  6577. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6578. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6579. }
  6580. #define MMQ_X_Q8_0_RDNA2 64
  6581. #define MMQ_Y_Q8_0_RDNA2 128
  6582. #define NWARPS_Q8_0_RDNA2 8
  6583. #define MMQ_X_Q8_0_RDNA1 64
  6584. #define MMQ_Y_Q8_0_RDNA1 64
  6585. #define NWARPS_Q8_0_RDNA1 8
  6586. #if defined(SYCL_USE_XMX)
  6587. #define MMQ_X_Q8_0_AMPERE 4
  6588. #define MMQ_Y_Q8_0_AMPERE 32
  6589. #define NWARPS_Q8_0_AMPERE 4
  6590. #else
  6591. #define MMQ_X_Q8_0_AMPERE 128
  6592. #define MMQ_Y_Q8_0_AMPERE 64
  6593. #define NWARPS_Q8_0_AMPERE 4
  6594. #endif
  6595. #define MMQ_X_Q8_0_PASCAL 64
  6596. #define MMQ_Y_Q8_0_PASCAL 64
  6597. #define NWARPS_Q8_0_PASCAL 8
  6598. template <bool need_check> static void
  6599. mul_mat_q8_0(
  6600. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6601. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6602. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  6603. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6604. int * tile_x_ql = nullptr;
  6605. sycl::half2 *tile_x_dm = nullptr;
  6606. int * tile_x_qh = nullptr;
  6607. int * tile_x_sc = nullptr;
  6608. //sycl_todo: change according to hardware
  6609. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  6610. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  6611. const int nwarps = NWARPS_Q8_0_AMPERE;
  6612. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6613. tile_x_qs_q8_0, tile_x_d_q8_0);
  6614. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  6615. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  6616. vec_dot_q8_0_q8_1_mul_mat>(
  6617. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6618. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6619. }
  6620. #define MMQ_X_Q2_K_RDNA2 64
  6621. #define MMQ_Y_Q2_K_RDNA2 128
  6622. #define NWARPS_Q2_K_RDNA2 8
  6623. #define MMQ_X_Q2_K_RDNA1 128
  6624. #define MMQ_Y_Q2_K_RDNA1 32
  6625. #define NWARPS_Q2_K_RDNA1 8
  6626. #if defined(SYCL_USE_XMX)
  6627. #define MMQ_X_Q2_K_AMPERE 4
  6628. #define MMQ_Y_Q2_K_AMPERE 32
  6629. #define NWARPS_Q2_K_AMPERE 4
  6630. #else
  6631. #define MMQ_X_Q2_K_AMPERE 64
  6632. #define MMQ_Y_Q2_K_AMPERE 128
  6633. #define NWARPS_Q2_K_AMPERE 4
  6634. #endif
  6635. #define MMQ_X_Q2_K_PASCAL 64
  6636. #define MMQ_Y_Q2_K_PASCAL 64
  6637. #define NWARPS_Q2_K_PASCAL 8
  6638. template <bool need_check> static void
  6639. mul_mat_q2_K(
  6640. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6641. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6642. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  6643. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  6644. sycl::half2 *tile_y_ds) {
  6645. int * tile_x_ql = nullptr;
  6646. sycl::half2 *tile_x_dm = nullptr;
  6647. int * tile_x_qh = nullptr;
  6648. int * tile_x_sc = nullptr;
  6649. //sycl_todo: change according to hardware
  6650. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  6651. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  6652. const int nwarps = NWARPS_Q2_K_AMPERE;
  6653. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6654. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  6655. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  6656. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  6657. vec_dot_q2_K_q8_1_mul_mat>(
  6658. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6659. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6660. }
  6661. #define MMQ_X_Q3_K_RDNA2 128
  6662. #define MMQ_Y_Q3_K_RDNA2 64
  6663. #define NWARPS_Q3_K_RDNA2 8
  6664. #define MMQ_X_Q3_K_RDNA1 32
  6665. #define MMQ_Y_Q3_K_RDNA1 128
  6666. #define NWARPS_Q3_K_RDNA1 8
  6667. #if defined(SYCL_USE_XMX)
  6668. #define MMQ_X_Q3_K_AMPERE 4
  6669. #define MMQ_Y_Q3_K_AMPERE 32
  6670. #define NWARPS_Q3_K_AMPERE 4
  6671. #else
  6672. #define MMQ_X_Q3_K_AMPERE 128
  6673. #define MMQ_Y_Q3_K_AMPERE 128
  6674. #define NWARPS_Q3_K_AMPERE 4
  6675. #endif
  6676. #define MMQ_X_Q3_K_PASCAL 64
  6677. #define MMQ_Y_Q3_K_PASCAL 64
  6678. #define NWARPS_Q3_K_PASCAL 8
  6679. template <bool need_check> static void
  6680. mul_mat_q3_K(
  6681. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6682. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6683. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  6684. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  6685. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6686. int * tile_x_ql = nullptr;
  6687. sycl::half2 *tile_x_dm = nullptr;
  6688. int * tile_x_qh = nullptr;
  6689. int * tile_x_sc = nullptr;
  6690. //sycl_todo: change according to hardware
  6691. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  6692. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  6693. const int nwarps = NWARPS_Q3_K_AMPERE;
  6694. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6695. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  6696. tile_x_sc_q3_K);
  6697. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  6698. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  6699. vec_dot_q3_K_q8_1_mul_mat>(
  6700. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6701. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6702. }
  6703. #define MMQ_X_Q4_K_RDNA2 64
  6704. #define MMQ_Y_Q4_K_RDNA2 128
  6705. #define NWARPS_Q4_K_RDNA2 8
  6706. #define MMQ_X_Q4_K_RDNA1 32
  6707. #define MMQ_Y_Q4_K_RDNA1 64
  6708. #define NWARPS_Q4_K_RDNA1 8
  6709. #if defined(SYCL_USE_XMX)
  6710. #define MMQ_X_Q4_K_AMPERE 4
  6711. #define MMQ_Y_Q4_K_AMPERE 32
  6712. #define NWARPS_Q4_K_AMPERE 4
  6713. #else
  6714. #define MMQ_X_Q4_K_AMPERE 64
  6715. #define MMQ_Y_Q4_K_AMPERE 128
  6716. #define NWARPS_Q4_K_AMPERE 4
  6717. #endif
  6718. #define MMQ_X_Q4_K_PASCAL 64
  6719. #define MMQ_Y_Q4_K_PASCAL 64
  6720. #define NWARPS_Q4_K_PASCAL 8
  6721. template <bool need_check> static void
  6722. mul_mat_q4_K(
  6723. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6724. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6725. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  6726. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  6727. sycl::half2 *tile_y_ds) {
  6728. int * tile_x_ql = nullptr;
  6729. sycl::half2 *tile_x_dm = nullptr;
  6730. int * tile_x_qh = nullptr;
  6731. int * tile_x_sc = nullptr;
  6732. //sycl_todo: change according to hardware
  6733. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  6734. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  6735. const int nwarps = NWARPS_Q4_K_AMPERE;
  6736. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6737. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  6738. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  6739. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  6740. vec_dot_q4_K_q8_1_mul_mat>(
  6741. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6742. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6743. }
  6744. #define MMQ_X_Q5_K_RDNA2 64
  6745. #define MMQ_Y_Q5_K_RDNA2 128
  6746. #define NWARPS_Q5_K_RDNA2 8
  6747. #define MMQ_X_Q5_K_RDNA1 32
  6748. #define MMQ_Y_Q5_K_RDNA1 64
  6749. #define NWARPS_Q5_K_RDNA1 8
  6750. #if defined(SYCL_USE_XMX)
  6751. #define MMQ_X_Q5_K_AMPERE 4
  6752. #define MMQ_Y_Q5_K_AMPERE 32
  6753. #define NWARPS_Q5_K_AMPERE 4
  6754. #else
  6755. #define MMQ_X_Q5_K_AMPERE 64
  6756. #define MMQ_Y_Q5_K_AMPERE 128
  6757. #define NWARPS_Q5_K_AMPERE 4
  6758. #endif
  6759. #define MMQ_X_Q5_K_PASCAL 64
  6760. #define MMQ_Y_Q5_K_PASCAL 64
  6761. #define NWARPS_Q5_K_PASCAL 8
  6762. template <bool need_check> static void
  6763. mul_mat_q5_K(
  6764. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6765. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6766. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  6767. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  6768. sycl::half2 *tile_y_ds) {
  6769. int * tile_x_ql = nullptr;
  6770. sycl::half2 *tile_x_dm = nullptr;
  6771. int * tile_x_qh = nullptr;
  6772. int * tile_x_sc = nullptr;
  6773. //sycl_todo: change according to hardware
  6774. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  6775. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  6776. const int nwarps = NWARPS_Q5_K_AMPERE;
  6777. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6778. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  6779. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  6780. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  6781. vec_dot_q5_K_q8_1_mul_mat>(
  6782. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6783. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6784. }
  6785. #define MMQ_X_Q6_K_RDNA2 64
  6786. #define MMQ_Y_Q6_K_RDNA2 128
  6787. #define NWARPS_Q6_K_RDNA2 8
  6788. #define MMQ_X_Q6_K_RDNA1 32
  6789. #define MMQ_Y_Q6_K_RDNA1 64
  6790. #define NWARPS_Q6_K_RDNA1 8
  6791. #if defined(SYCL_USE_XMX)
  6792. #define MMQ_X_Q6_K_AMPERE 4
  6793. #define MMQ_Y_Q6_K_AMPERE 32
  6794. #define NWARPS_Q6_K_AMPERE 4
  6795. #else
  6796. #define MMQ_X_Q6_K_AMPERE 64
  6797. #define MMQ_Y_Q6_K_AMPERE 64
  6798. #define NWARPS_Q6_K_AMPERE 4
  6799. #endif
  6800. #define MMQ_X_Q6_K_PASCAL 64
  6801. #define MMQ_Y_Q6_K_PASCAL 64
  6802. #define NWARPS_Q6_K_PASCAL 8
  6803. template <bool need_check> static void
  6804. mul_mat_q6_K(
  6805. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6806. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6807. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  6808. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6809. // int * tile_x_ql = nullptr;
  6810. // sycl::half2 *tile_x_dm = nullptr;
  6811. int * tile_x_qh = nullptr;
  6812. // int * tile_x_sc = nullptr;
  6813. //sycl_todo: change according to hardware
  6814. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  6815. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  6816. const int nwarps = NWARPS_Q6_K_AMPERE;
  6817. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6818. tile_x_ql, tile_x_dm, tile_x_sc);
  6819. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  6820. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  6821. vec_dot_q6_K_q8_1_mul_mat>(
  6822. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6823. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6824. }
  6825. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  6826. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  6827. const sycl::nd_item<3> &item_ct1) {
  6828. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6829. item_ct1.get_local_id(1);
  6830. if (row >= nrows) {
  6831. return;
  6832. }
  6833. const int blocks_per_row = ncols / qk;
  6834. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6835. const int qi_vdr = (qi / vdr); // N_threads processing 1 qk block
  6836. // partial sum for each thread
  6837. float tmp = 0.0f;
  6838. const block_q_t * x = (const block_q_t *) vx;
  6839. const block_q8_1 * y = (const block_q8_1 *) vy;
  6840. for (int i = item_ct1.get_local_id(2) / qi_vdr; i < blocks_per_row;
  6841. i += blocks_per_warp) {
  6842. const int ibx = row * blocks_per_row + i; // x block index
  6843. const int iby = i * (qk / QK8_1); // y block index that aligns with ibx
  6844. const int iqs =
  6845. vdr *
  6846. (item_ct1.get_local_id(2) -
  6847. i * qi_vdr); // x block quant index when casting the quants to int
  6848. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  6849. }
  6850. // sum up partial sums and write back result
  6851. #pragma unroll
  6852. for (int mask = 16; mask > 0; mask >>= 1) {
  6853. tmp +=
  6854. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6855. }
  6856. if (item_ct1.get_local_id(2) == 0) {
  6857. dst[row] = tmp;
  6858. }
  6859. }
  6860. template <int qk, int qi, typename block_q_t, int vdr>
  6861. static void mul_mat_vec_q_iq2_xxs_q8_1(const void *__restrict__ vx,
  6862. const void *__restrict__ vy,
  6863. float *__restrict__ dst, const int ncols,
  6864. const int nrows,
  6865. const sycl::nd_item<3> &item_ct1) {
  6866. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6867. item_ct1.get_local_id(1);
  6868. if (row >= nrows) {
  6869. return;
  6870. }
  6871. const int blocks_per_row = ncols / qk;
  6872. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6873. // partial sum for each thread
  6874. float tmp = 0.0f;
  6875. const block_q_t * x = (const block_q_t *) vx;
  6876. const block_q8_1 * y = (const block_q8_1 *) vy;
  6877. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6878. i += blocks_per_warp) {
  6879. const int ibx = row*blocks_per_row + i; // x block index
  6880. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6881. const int iqs =
  6882. vdr *
  6883. (item_ct1.get_local_id(2) %
  6884. (qi / vdr)); // x block quant index when casting the quants to int
  6885. tmp += vec_dot_iq2_xxs_q8_1(&x[ibx], &y[iby], iqs, iq2xxs_grid, ksigns_iq2xs, kmask_iq2xs);
  6886. }
  6887. // sum up partial sums and write back result
  6888. #pragma unroll
  6889. for (int mask = 16; mask > 0; mask >>= 1) {
  6890. tmp +=
  6891. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6892. }
  6893. if (item_ct1.get_local_id(2) == 0) {
  6894. dst[row] = tmp;
  6895. }
  6896. }
  6897. template <int qk, int qi, typename block_q_t, int vdr>
  6898. static void mul_mat_vec_q_iq2_xs_q8_1(const void *__restrict__ vx,
  6899. const void *__restrict__ vy,
  6900. float *__restrict__ dst, const int ncols,
  6901. const int nrows,
  6902. const sycl::nd_item<3> &item_ct1) {
  6903. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6904. item_ct1.get_local_id(1);
  6905. if (row >= nrows) {
  6906. return;
  6907. }
  6908. const int blocks_per_row = ncols / qk;
  6909. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6910. // partial sum for each thread
  6911. float tmp = 0.0f;
  6912. const block_q_t * x = (const block_q_t *) vx;
  6913. const block_q8_1 * y = (const block_q8_1 *) vy;
  6914. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6915. i += blocks_per_warp) {
  6916. const int ibx = row*blocks_per_row + i; // x block index
  6917. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6918. const int iqs =
  6919. vdr *
  6920. (item_ct1.get_local_id(2) %
  6921. (qi / vdr)); // x block quant index when casting the quants to int
  6922. tmp += vec_dot_iq2_xs_q8_1(&x[ibx], &y[iby], iqs, iq2xs_grid, ksigns64);
  6923. }
  6924. // sum up partial sums and write back result
  6925. #pragma unroll
  6926. for (int mask = 16; mask > 0; mask >>= 1) {
  6927. tmp +=
  6928. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6929. }
  6930. if (item_ct1.get_local_id(2) == 0) {
  6931. dst[row] = tmp;
  6932. }
  6933. }
  6934. template <int qk, int qi, typename block_q_t, int vdr>
  6935. static void mul_mat_vec_q_iq2_s_q8_1(const void *__restrict__ vx,
  6936. const void *__restrict__ vy,
  6937. float *__restrict__ dst, const int ncols,
  6938. const int nrows,
  6939. const sycl::nd_item<3> &item_ct1) {
  6940. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6941. item_ct1.get_local_id(1);
  6942. if (row >= nrows) {
  6943. return;
  6944. }
  6945. const int blocks_per_row = ncols / qk;
  6946. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6947. // partial sum for each thread
  6948. float tmp = 0.0f;
  6949. const block_q_t * x = (const block_q_t *) vx;
  6950. const block_q8_1 * y = (const block_q8_1 *) vy;
  6951. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6952. i += blocks_per_warp) {
  6953. const int ibx = row*blocks_per_row + i; // x block index
  6954. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6955. const int iqs =
  6956. vdr *
  6957. (item_ct1.get_local_id(2) %
  6958. (qi / vdr)); // x block quant index when casting the quants to int
  6959. tmp += vec_dot_iq2_s_q8_1(&x[ibx], &y[iby], iqs);
  6960. }
  6961. // sum up partial sums and write back result
  6962. #pragma unroll
  6963. for (int mask = 16; mask > 0; mask >>= 1) {
  6964. tmp +=
  6965. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6966. }
  6967. if (item_ct1.get_local_id(2) == 0) {
  6968. dst[row] = tmp;
  6969. }
  6970. }
  6971. template <int qk, int qi, typename block_q_t, int vdr>
  6972. static void mul_mat_vec_q_iq3_xxs_q8_1(const void *__restrict__ vx,
  6973. const void *__restrict__ vy,
  6974. float *__restrict__ dst, const int ncols,
  6975. const int nrows,
  6976. const sycl::nd_item<3> &item_ct1) {
  6977. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6978. item_ct1.get_local_id(1);
  6979. if (row >= nrows) {
  6980. return;
  6981. }
  6982. const int blocks_per_row = ncols / qk;
  6983. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6984. // partial sum for each thread
  6985. float tmp = 0.0f;
  6986. const block_q_t * x = (const block_q_t *) vx;
  6987. const block_q8_1 * y = (const block_q8_1 *) vy;
  6988. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6989. i += blocks_per_warp) {
  6990. const int ibx = row*blocks_per_row + i; // x block index
  6991. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6992. const int iqs =
  6993. vdr *
  6994. (item_ct1.get_local_id(2) %
  6995. (qi / vdr)); // x block quant index when casting the quants to int
  6996. tmp += vec_dot_iq3_xxs_q8_1(&x[ibx], &y[iby], iqs, iq3xxs_grid, ksigns64);
  6997. }
  6998. // sum up partial sums and write back result
  6999. #pragma unroll
  7000. for (int mask = 16; mask > 0; mask >>= 1) {
  7001. tmp +=
  7002. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7003. }
  7004. if (item_ct1.get_local_id(2) == 0) {
  7005. dst[row] = tmp;
  7006. }
  7007. }
  7008. template <int qk, int qi, typename block_q_t, int vdr>
  7009. static void mul_mat_vec_q_iq3_s_q8_1(const void *__restrict__ vx,
  7010. const void *__restrict__ vy,
  7011. float *__restrict__ dst, const int ncols,
  7012. const int nrows,
  7013. const sycl::nd_item<3> &item_ct1) {
  7014. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7015. item_ct1.get_local_id(1);
  7016. if (row >= nrows) {
  7017. return;
  7018. }
  7019. const int blocks_per_row = ncols / qk;
  7020. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7021. // partial sum for each thread
  7022. float tmp = 0.0f;
  7023. const block_q_t * x = (const block_q_t *) vx;
  7024. const block_q8_1 * y = (const block_q8_1 *) vy;
  7025. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7026. i += blocks_per_warp) {
  7027. const int ibx = row*blocks_per_row + i; // x block index
  7028. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7029. const int iqs =
  7030. vdr *
  7031. (item_ct1.get_local_id(2) %
  7032. (qi / vdr)); // x block quant index when casting the quants to int
  7033. tmp += vec_dot_iq3_s_q8_1(&x[ibx], &y[iby], iqs, iq3s_grid);
  7034. }
  7035. // sum up partial sums and write back result
  7036. #pragma unroll
  7037. for (int mask = 16; mask > 0; mask >>= 1) {
  7038. tmp +=
  7039. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7040. }
  7041. if (item_ct1.get_local_id(2) == 0) {
  7042. dst[row] = tmp;
  7043. }
  7044. }
  7045. template <int qk, int qi, typename block_q_t, int vdr>
  7046. static void mul_mat_vec_q_iq1_s_q8_1(const void *__restrict__ vx,
  7047. const void *__restrict__ vy,
  7048. float *__restrict__ dst, const int ncols,
  7049. const int nrows,
  7050. const sycl::nd_item<3> &item_ct1) {
  7051. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7052. item_ct1.get_local_id(1);
  7053. if (row >= nrows) {
  7054. return;
  7055. }
  7056. const int blocks_per_row = ncols / qk;
  7057. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7058. // partial sum for each thread
  7059. float tmp = 0.0f;
  7060. const block_q_t * x = (const block_q_t *) vx;
  7061. const block_q8_1 * y = (const block_q8_1 *) vy;
  7062. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7063. i += blocks_per_warp) {
  7064. const int ibx = row*blocks_per_row + i; // x block index
  7065. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7066. const int iqs =
  7067. vdr *
  7068. (item_ct1.get_local_id(2) %
  7069. (qi / vdr)); // x block quant index when casting the quants to int
  7070. tmp += vec_dot_iq1_s_q8_1(&x[ibx], &y[iby], iqs, iq1s_grid_gpu);
  7071. }
  7072. // sum up partial sums and write back result
  7073. #pragma unroll
  7074. for (int mask = 16; mask > 0; mask >>= 1) {
  7075. tmp +=
  7076. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7077. }
  7078. if (item_ct1.get_local_id(2) == 0) {
  7079. dst[row] = tmp;
  7080. }
  7081. }
  7082. template <int qk, int qi, typename block_q_t, int vdr>
  7083. static void mul_mat_vec_q_iq1_m_q8_1(const void *__restrict__ vx,
  7084. const void *__restrict__ vy,
  7085. float *__restrict__ dst, const int ncols,
  7086. const int nrows,
  7087. const sycl::nd_item<3> &item_ct1) {
  7088. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7089. item_ct1.get_local_id(1);
  7090. if (row >= nrows) {
  7091. return;
  7092. }
  7093. const int blocks_per_row = ncols / qk;
  7094. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7095. // partial sum for each thread
  7096. float tmp = 0.0f;
  7097. const block_q_t * x = (const block_q_t *) vx;
  7098. const block_q8_1 * y = (const block_q8_1 *) vy;
  7099. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7100. i += blocks_per_warp) {
  7101. const int ibx = row*blocks_per_row + i; // x block index
  7102. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7103. const int iqs =
  7104. vdr *
  7105. (item_ct1.get_local_id(2) %
  7106. (qi / vdr)); // x block quant index when casting the quants to int
  7107. tmp += vec_dot_iq1_m_q8_1(&x[ibx], &y[iby], iqs);
  7108. }
  7109. // sum up partial sums and write back result
  7110. #pragma unroll
  7111. for (int mask = 16; mask > 0; mask >>= 1) {
  7112. tmp +=
  7113. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7114. }
  7115. if (item_ct1.get_local_id(2) == 0) {
  7116. dst[row] = tmp;
  7117. }
  7118. }
  7119. template <int qk, int qi, typename block_q_t, int vdr>
  7120. static void mul_mat_vec_q_iq4_nl_q8_1(const void *__restrict__ vx,
  7121. const void *__restrict__ vy,
  7122. float *__restrict__ dst, const int ncols,
  7123. const int nrows,
  7124. const sycl::nd_item<3> &item_ct1) {
  7125. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7126. item_ct1.get_local_id(1);
  7127. if (row >= nrows) {
  7128. return;
  7129. }
  7130. const int blocks_per_row = ncols / qk;
  7131. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7132. // partial sum for each thread
  7133. float tmp = 0.0f;
  7134. const block_q_t * x = (const block_q_t *) vx;
  7135. const block_q8_1 * y = (const block_q8_1 *) vy;
  7136. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7137. i += blocks_per_warp) {
  7138. const int ibx = row*blocks_per_row + i; // x block index
  7139. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7140. const int iqs =
  7141. vdr *
  7142. (item_ct1.get_local_id(2) %
  7143. (qi / vdr)); // x block quant index when casting the quants to int
  7144. tmp += vec_dot_iq4_nl_q8_1(&x[ibx], &y[iby], iqs);
  7145. }
  7146. // sum up partial sums and write back result
  7147. #pragma unroll
  7148. for (int mask = 16; mask > 0; mask >>= 1) {
  7149. tmp +=
  7150. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7151. }
  7152. if (item_ct1.get_local_id(2) == 0) {
  7153. dst[row] = tmp;
  7154. }
  7155. }
  7156. template <int qk, int qi, typename block_q_t, int vdr>
  7157. static void mul_mat_vec_q_iq4_xs_q8_1(const void *__restrict__ vx,
  7158. const void *__restrict__ vy,
  7159. float *__restrict__ dst, const int ncols,
  7160. const int nrows,
  7161. const sycl::nd_item<3> &item_ct1) {
  7162. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7163. item_ct1.get_local_id(1);
  7164. if (row >= nrows) {
  7165. return;
  7166. }
  7167. const int blocks_per_row = ncols / qk;
  7168. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7169. // partial sum for each thread
  7170. float tmp = 0.0f;
  7171. const block_q_t * x = (const block_q_t *) vx;
  7172. const block_q8_1 * y = (const block_q8_1 *) vy;
  7173. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7174. i += blocks_per_warp) {
  7175. const int ibx = row*blocks_per_row + i; // x block index
  7176. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7177. const int iqs =
  7178. vdr *
  7179. (item_ct1.get_local_id(2) %
  7180. (qi / vdr)); // x block quant index when casting the quants to int
  7181. tmp += vec_dot_iq4_xs_q8_1(&x[ibx], &y[iby], iqs);
  7182. }
  7183. // sum up partial sums and write back result
  7184. #pragma unroll
  7185. for (int mask = 16; mask > 0; mask >>= 1) {
  7186. tmp +=
  7187. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7188. }
  7189. if (item_ct1.get_local_id(2) == 0) {
  7190. dst[row] = tmp;
  7191. }
  7192. }
  7193. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  7194. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  7195. const sycl::nd_item<3> &item_ct1) {
  7196. // qk = quantized weights per x block
  7197. // qr = number of quantized weights per data value in x block
  7198. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7199. item_ct1.get_local_id(1);
  7200. if (row >= nrows) {
  7201. return;
  7202. }
  7203. const int tid = item_ct1.get_local_id(2);
  7204. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  7205. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  7206. const int y_offset = qr == 1 ? 1 : qk/2;
  7207. // partial sum for each thread
  7208. #ifdef GGML_SYCL_F16
  7209. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  7210. #else
  7211. float tmp = 0.0f;
  7212. #endif // GGML_SYCL_F16
  7213. for (int i = 0; i < ncols; i += iter_stride) {
  7214. const int col = i + vals_per_iter*tid;
  7215. const int ib = (row*ncols + col)/qk; // x block index
  7216. const int iqs = (col%qk)/qr; // x quant index
  7217. const int iybs = col - col%qk; // y block start index
  7218. // processing >2 values per i iter is faster for fast GPUs
  7219. #pragma unroll
  7220. for (int j = 0; j < vals_per_iter; j += 2) {
  7221. // process 2 vals per j iter
  7222. // dequantize
  7223. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  7224. dfloat2 v;
  7225. dequantize_kernel(vx, ib, iqs + j/qr, v);
  7226. // matrix multiplication
  7227. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  7228. #ifdef GGML_SYCL_F16
  7229. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  7230. y[iybs + iqs + j / qr + y_offset]};
  7231. tmp += v * t1;
  7232. #else
  7233. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  7234. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  7235. #endif // GGML_SYCL_F16
  7236. }
  7237. }
  7238. // sum up partial sums and write back result
  7239. #pragma unroll
  7240. for (int mask = 16; mask > 0; mask >>= 1) {
  7241. tmp +=
  7242. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7243. }
  7244. if (tid == 0) {
  7245. #ifdef GGML_SYCL_F16
  7246. dst[row] = tmp.x() + tmp.y();
  7247. #else
  7248. dst[row] = tmp;
  7249. #endif // GGML_SYCL_F16
  7250. }
  7251. }
  7252. static void mul_mat_p021_f16_f32(
  7253. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  7254. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  7255. const sycl::nd_item<3> &item_ct1) {
  7256. const sycl::half *x = (const sycl::half *)vx;
  7257. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7258. item_ct1.get_local_id(1);
  7259. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7260. item_ct1.get_local_id(0);
  7261. const int channel_x = channel / (nchannels_y / nchannels_x);
  7262. const int nrows_y = ncols_x;
  7263. const int nrows_dst = nrows_x;
  7264. const int row_dst = row_x;
  7265. float tmp = 0.0f;
  7266. for (int col_x0 = 0; col_x0 < ncols_x;
  7267. col_x0 += item_ct1.get_local_range(2)) {
  7268. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7269. if (col_x >= ncols_x) {
  7270. break;
  7271. }
  7272. // x is transposed and permuted
  7273. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  7274. const float xi =
  7275. sycl::vec<sycl::half, 1>(x[ix])
  7276. .convert<float, sycl::rounding_mode::automatic>()[0];
  7277. const int row_y = col_x;
  7278. // y is not transposed but permuted
  7279. const int iy = channel*nrows_y + row_y;
  7280. tmp += xi * y[iy];
  7281. }
  7282. // dst is not transposed and not permuted
  7283. const int idst = channel*nrows_dst + row_dst;
  7284. // sum up partial sums and write back result
  7285. #pragma unroll
  7286. for (int mask = 16; mask > 0; mask >>= 1) {
  7287. tmp +=
  7288. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7289. }
  7290. if (item_ct1.get_local_id(2) == 0) {
  7291. dst[idst] = tmp;
  7292. }
  7293. }
  7294. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  7295. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  7296. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  7297. const sycl::nd_item<3> &item_ct1) {
  7298. const sycl::half *x = (const sycl::half *)vx;
  7299. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7300. item_ct1.get_local_id(1);
  7301. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7302. item_ct1.get_local_id(0);
  7303. const int channel_x = channel / channel_x_divisor;
  7304. const int nrows_y = ncols_x;
  7305. const int nrows_dst = nrows_x;
  7306. const int row_dst = row_x;
  7307. const int idst = channel*nrows_dst + row_dst;
  7308. float tmp = 0.0f;
  7309. for (int col_x0 = 0; col_x0 < ncols_x;
  7310. col_x0 += item_ct1.get_local_range(2)) {
  7311. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7312. if (col_x >= ncols_x) {
  7313. break;
  7314. }
  7315. const int row_y = col_x;
  7316. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  7317. const int iy = channel*nrows_y + row_y;
  7318. const float xi =
  7319. sycl::vec<sycl::half, 1>(x[ix])
  7320. .convert<float, sycl::rounding_mode::automatic>()[0];
  7321. tmp += xi * y[iy];
  7322. }
  7323. // sum up partial sums and write back result
  7324. #pragma unroll
  7325. for (int mask = 16; mask > 0; mask >>= 1) {
  7326. tmp +=
  7327. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7328. }
  7329. if (item_ct1.get_local_id(2) == 0) {
  7330. dst[idst] = tmp;
  7331. }
  7332. }
  7333. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  7334. const float * xi = (const float *) cxi;
  7335. float * dsti = (float *) cdsti;
  7336. *dsti = *xi;
  7337. }
  7338. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  7339. const float * xi = (const float *) cxi;
  7340. sycl::half *dsti = (sycl::half *)cdsti;
  7341. *dsti = sycl::vec<float, 1>(*xi)
  7342. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7343. }
  7344. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  7345. const sycl::half *xi = (const sycl::half *)cxi;
  7346. sycl::half *dsti = (sycl::half *)cdsti;
  7347. *dsti = *xi;
  7348. }
  7349. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  7350. const sycl::half *xi = (const sycl::half *)cxi;
  7351. float * dsti = (float *) cdsti;
  7352. *dsti = *xi;
  7353. }
  7354. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  7355. const int16_t *xi = (const int16_t *)cxi;
  7356. int16_t *dsti = (int16_t *)cdsti;
  7357. *dsti = *xi;
  7358. }
  7359. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  7360. const int32_t *xi = (const int32_t *)cxi;
  7361. int32_t *dsti = (int32_t *)cdsti;
  7362. *dsti = *xi;
  7363. }
  7364. template <cpy_kernel_t cpy_1>
  7365. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  7366. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7367. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7368. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7369. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7370. item_ct1.get_local_id(2);
  7371. if (i >= ne) {
  7372. return;
  7373. }
  7374. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  7375. // then combine those indices with the corresponding byte offsets to get the total offsets
  7376. const int i03 = i/(ne00 * ne01 * ne02);
  7377. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7378. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7379. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7380. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7381. const int i13 = i/(ne10 * ne11 * ne12);
  7382. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7383. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7384. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7385. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  7386. cpy_1(cx + x_offset, cdst + dst_offset);
  7387. }
  7388. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  7389. const float * xi = (const float *) cxi;
  7390. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  7391. float amax = 0.0f; // absolute max
  7392. for (int j = 0; j < QK8_0; j++) {
  7393. const float v = xi[j];
  7394. amax = sycl::fmax(amax, sycl::fabs((float)v));
  7395. }
  7396. const float d = amax / ((1 << 7) - 1);
  7397. const float id = d ? 1.0f/d : 0.0f;
  7398. dsti->d = d;
  7399. for (int j = 0; j < QK8_0; ++j) {
  7400. const float x0 = xi[j]*id;
  7401. dsti->qs[j] = sycl::round((float)x0);
  7402. }
  7403. }
  7404. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  7405. const float * xi = (const float *) cxi;
  7406. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  7407. float amax = 0.0f;
  7408. float vmax = 0.0f;
  7409. for (int j = 0; j < QK4_0; ++j) {
  7410. const float v = xi[j];
  7411. if (amax < sycl::fabs((float)v)) {
  7412. amax = sycl::fabs((float)v);
  7413. vmax = v;
  7414. }
  7415. }
  7416. const float d = vmax / -8;
  7417. const float id = d ? 1.0f/d : 0.0f;
  7418. dsti->d = d;
  7419. for (int j = 0; j < QK4_0/2; ++j) {
  7420. const float x0 = xi[0 + j]*id;
  7421. const float x1 = xi[QK4_0/2 + j]*id;
  7422. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  7423. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  7424. dsti->qs[j] = xi0;
  7425. dsti->qs[j] |= xi1 << 4;
  7426. }
  7427. }
  7428. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  7429. const float * xi = (const float *) cxi;
  7430. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  7431. float vmin = FLT_MAX;
  7432. float vmax = -FLT_MAX;
  7433. for (int j = 0; j < QK4_1; ++j) {
  7434. const float v = xi[j];
  7435. if (v < vmin) vmin = v;
  7436. if (v > vmax) vmax = v;
  7437. }
  7438. const float d = (vmax - vmin) / ((1 << 4) - 1);
  7439. const float id = d ? 1.0f/d : 0.0f;
  7440. dsti->dm.x() = d;
  7441. dsti->dm.y() = vmin;
  7442. for (int j = 0; j < QK4_1/2; ++j) {
  7443. const float x0 = (xi[0 + j] - vmin)*id;
  7444. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  7445. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  7446. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  7447. dsti->qs[j] = xi0;
  7448. dsti->qs[j] |= xi1 << 4;
  7449. }
  7450. }
  7451. template <cpy_kernel_t cpy_blck, int qk>
  7452. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  7453. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7454. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7455. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7456. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7457. item_ct1.get_local_id(2)) *
  7458. qk;
  7459. if (i >= ne) {
  7460. return;
  7461. }
  7462. const int i03 = i/(ne00 * ne01 * ne02);
  7463. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7464. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7465. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7466. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7467. const int i13 = i/(ne10 * ne11 * ne12);
  7468. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7469. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7470. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7471. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  7472. cpy_blck(cx + x_offset, cdst + dst_offset);
  7473. }
  7474. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7475. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  7476. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  7477. }
  7478. struct rope_corr_dims {
  7479. float v[4];
  7480. };
  7481. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7482. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7483. static void rope_yarn(
  7484. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  7485. float * cos_theta, float * sin_theta
  7486. ) {
  7487. // Get n-d rotational scaling corrected for extrapolation
  7488. float theta_interp = freq_scale * theta_extrap;
  7489. float theta = theta_interp;
  7490. if (ext_factor != 0.0f) {
  7491. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  7492. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7493. // Get n-d magnitude scaling corrected for interpolation
  7494. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  7495. }
  7496. *cos_theta = sycl::cos(theta) * mscale;
  7497. *sin_theta = sycl::sin(theta) * mscale;
  7498. }
  7499. // rope == RoPE == rotary positional embedding
  7500. template<typename T, bool has_pos>
  7501. static void rope(
  7502. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7503. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  7504. ,
  7505. const sycl::nd_item<3> &item_ct1) {
  7506. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7507. item_ct1.get_local_id(1));
  7508. if (col >= ncols) {
  7509. return;
  7510. }
  7511. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7512. item_ct1.get_local_id(2);
  7513. const int i = row*ncols + col;
  7514. const int i2 = row/p_delta_rows;
  7515. const int p = has_pos ? pos[i2] : 0;
  7516. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  7517. float cos_theta, sin_theta;
  7518. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7519. const float x0 = x[i + 0];
  7520. const float x1 = x[i + 1];
  7521. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7522. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  7523. }
  7524. template<typename T, bool has_pos, bool has_freq_facs>
  7525. static void rope_neox(
  7526. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  7527. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims,
  7528. const float * freq_factors, const sycl::nd_item<3> &item_ct1) {
  7529. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7530. item_ct1.get_local_id(1));
  7531. if (col >= ncols) {
  7532. return;
  7533. }
  7534. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7535. item_ct1.get_local_id(2);
  7536. const int ib = col / n_dims;
  7537. const int ic = col % n_dims;
  7538. if (ib > 0) {
  7539. const int i = row*ncols + ib*n_dims + ic;
  7540. dst[i + 0] = x[i + 0];
  7541. dst[i + 1] = x[i + 1];
  7542. return;
  7543. }
  7544. const int i = row*ncols + ib*n_dims + ic/2;
  7545. const int i2 = row/p_delta_rows;
  7546. float cur_rot = inv_ndims * ic - ib;
  7547. const int p = has_pos ? pos[i2] : 0;
  7548. const float freq_factor = has_freq_facs ? freq_factors[ic/2] : 1.0f;
  7549. const float theta_base =
  7550. p * freq_scale * dpct::pow(theta_scale, col / 2.0f)/freq_factor;
  7551. float cos_theta, sin_theta;
  7552. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7553. const float x0 = x[i + 0];
  7554. const float x1 = x[i + n_dims/2];
  7555. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7556. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  7557. }
  7558. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  7559. const sycl::nd_item<3> &item_ct1) {
  7560. const int row = item_ct1.get_group(1);
  7561. const int col = item_ct1.get_local_id(2);
  7562. float sum = 0.0f;
  7563. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  7564. sum += x[row * ncols + i];
  7565. }
  7566. sum = warp_reduce_sum(sum, item_ct1);
  7567. if (col == 0) {
  7568. dst[row] = sum;
  7569. }
  7570. }
  7571. template<typename T>
  7572. static inline void ggml_sycl_swap(T & a, T & b) {
  7573. T tmp = a;
  7574. a = b;
  7575. b = tmp;
  7576. }
  7577. template <ggml_sort_order order>
  7578. __dpct_inline__ static void
  7579. k_argsort_f32_i32(const float *x, int *dst, const int ncols, int ncols_pad,
  7580. const sycl::nd_item<3> &item_ct1, uint8_t *dpct_local) {
  7581. // bitonic sort
  7582. int col = item_ct1.get_local_id(2);
  7583. int row = item_ct1.get_group(1);
  7584. if (col >= ncols_pad) {
  7585. return;
  7586. }
  7587. const float * x_row = x + row * ncols;
  7588. auto dst_row = (int *)dpct_local;
  7589. // initialize indices
  7590. dst_row[col] = col;
  7591. item_ct1.barrier(sycl::access::fence_space::local_space);
  7592. for (int k = 2; k <= ncols_pad; k *= 2) {
  7593. for (int j = k / 2; j > 0; j /= 2) {
  7594. int ixj = col ^ j;
  7595. if (ixj > col) {
  7596. if ((col & k) == 0) {
  7597. if (dst_row[col] >= ncols ||
  7598. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  7599. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  7600. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  7601. ) {
  7602. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  7603. }
  7604. } else {
  7605. if (dst_row[ixj] >= ncols ||
  7606. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  7607. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  7608. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  7609. ) {
  7610. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  7611. }
  7612. }
  7613. }
  7614. /*
  7615. DPCT1118:1: SYCL group functions and algorithms must be encountered
  7616. in converged control flow. You may need to adjust the code.
  7617. */
  7618. item_ct1.barrier(sycl::access::fence_space::local_space);
  7619. }
  7620. }
  7621. // copy the result to dst without the padding
  7622. if (col < ncols) {
  7623. dst[row * ncols + col] = dst_row[col];
  7624. }
  7625. }
  7626. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  7627. const sycl::nd_item<3> &item_ct1) {
  7628. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7629. item_ct1.get_local_id(1);
  7630. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7631. item_ct1.get_local_id(2);
  7632. if (col >= ncols) {
  7633. return;
  7634. }
  7635. const int i = row*ncols + col;
  7636. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  7637. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  7638. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  7639. }
  7640. template <bool vals_smem, int ncols_template, int block_size_template>
  7641. static void soft_max_f32(const float * x, const float * mask, float * dst, const int ncols_par,
  7642. const int nrows_y, const float scale, const float max_bias, const float m0,
  7643. const float m1, uint32_t n_head_log2, const sycl::nd_item<3> &item_ct1, float *buf) {
  7644. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  7645. const int tid = item_ct1.get_local_id(2);
  7646. const int rowx = item_ct1.get_group(2);
  7647. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  7648. const int block_size = block_size_template == 0 ? item_ct1.get_local_range(2) : block_size_template;
  7649. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  7650. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  7651. float slope = 1.0f;
  7652. // ALiBi
  7653. if (max_bias > 0.0f) {
  7654. const uint32_t h = rowx/nrows_y; // head index
  7655. const float base = h < n_head_log2 ? m0 : m1;
  7656. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  7657. slope = sycl::pow(base, float(exp));
  7658. }
  7659. float * vals = vals_smem ? buf + WARP_SIZE : dst + rowx*ncols;
  7660. float max_val = -INFINITY;
  7661. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7662. const int col = col0 + tid;
  7663. if (ncols_template == 0 && col >= ncols) {
  7664. break;
  7665. }
  7666. const int ix = rowx*ncols + col;
  7667. const int iy = rowy*ncols + col;
  7668. const float val = x[ix]*scale + (mask ? slope*mask[iy] : 0.0f);
  7669. vals[col] = val;
  7670. max_val = sycl::max(max_val, val);
  7671. }
  7672. // find the max value in the block
  7673. max_val = warp_reduce_max(max_val, item_ct1);
  7674. if (block_size > WARP_SIZE) {
  7675. if (warp_id == 0) {
  7676. buf[lane_id] = -INFINITY;
  7677. }
  7678. item_ct1.barrier(sycl::access::fence_space::local_space);
  7679. if (lane_id == 0) {
  7680. buf[warp_id] = max_val;
  7681. }
  7682. item_ct1.barrier(sycl::access::fence_space::local_space);
  7683. max_val = buf[lane_id];
  7684. max_val = warp_reduce_max(max_val, item_ct1);
  7685. }
  7686. float tmp = 0.f;
  7687. #pragma unroll
  7688. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7689. const int col = col0 + tid;
  7690. if (ncols_template == 0 && col >= ncols) {
  7691. break;
  7692. }
  7693. const float val = sycl::native::exp(vals[col] - max_val);
  7694. tmp += val;
  7695. vals[col] = val;
  7696. }
  7697. // find the sum of exps in the block
  7698. tmp = warp_reduce_sum(tmp, item_ct1);
  7699. if (block_size > WARP_SIZE) {
  7700. item_ct1.barrier(sycl::access::fence_space::local_space);
  7701. if (warp_id == 0) {
  7702. buf[lane_id] = 0.f;
  7703. }
  7704. item_ct1.barrier(sycl::access::fence_space::local_space);
  7705. if (lane_id == 0) {
  7706. buf[warp_id] = tmp;
  7707. }
  7708. item_ct1.barrier(sycl::access::fence_space::local_space);
  7709. tmp = buf[lane_id];
  7710. tmp = warp_reduce_sum(tmp, item_ct1);
  7711. }
  7712. const float inv_sum = 1.f / tmp;
  7713. #pragma unroll
  7714. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7715. const int col = col0 + tid;
  7716. if (ncols_template == 0 && col >= ncols) {
  7717. return;
  7718. }
  7719. const int idst = rowx*ncols + col;
  7720. dst[idst] = vals[col] * inv_sum;
  7721. }
  7722. }
  7723. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  7724. const sycl::nd_item<3> &item_ct1) {
  7725. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7726. item_ct1.get_local_id(2);
  7727. if (i >= k) {
  7728. return;
  7729. }
  7730. dst[i] = scale * x[i];
  7731. }
  7732. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  7733. const sycl::nd_item<3> &item_ct1) {
  7734. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7735. item_ct1.get_local_id(2);
  7736. if (i >= k) {
  7737. return;
  7738. }
  7739. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  7740. }
  7741. template <typename T>
  7742. static void im2col_kernel(const float *x, T *dst, int offset_delta,
  7743. int IW, int IH, int OW, int KW, int KH,
  7744. int pelements, int CHW, int s0, int s1, int p0,
  7745. int p1, int d0, int d1,
  7746. const sycl::nd_item<3> &item_ct1) {
  7747. const int i = item_ct1.get_local_id(2) +
  7748. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  7749. if (i >= pelements) {
  7750. return;
  7751. }
  7752. const int ksize = OW * (KH > 1 ? KW : 1);
  7753. const int kx = i / ksize;
  7754. const int kd = kx * ksize;
  7755. const int ky = (i - kd) / OW;
  7756. const int ix = i % OW;
  7757. const int64_t iiw = ix * s0 + kx * d0 - p0;
  7758. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  7759. const int64_t offset_dst =
  7760. (item_ct1.get_group(1) * OW + ix) * CHW +
  7761. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  7762. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  7763. dst[offset_dst] =
  7764. sycl::vec<float, 1>(0.0f)
  7765. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7766. } else {
  7767. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  7768. dst[offset_dst] =
  7769. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  7770. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7771. }
  7772. }
  7773. template <typename Ti, typename To>
  7774. static void pool2d_nchw_kernel(
  7775. const int ih, const int iw, const int oh, const int ow,
  7776. const int kh, const int kw, const int sh, const int sw,
  7777. const int ph, const int pw, const int parallel_elements,
  7778. const Ti* src, To* dst, const enum ggml_op_pool op,
  7779. const sycl::nd_item<3> &item_ct1) {
  7780. int idx = item_ct1.get_local_id(2) +
  7781. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  7782. if (idx >= parallel_elements) {
  7783. return;
  7784. }
  7785. const int I_HW = ih * iw;
  7786. const int O_HW = oh * ow;
  7787. const int nc = idx / O_HW;
  7788. const int cur_oh = idx % O_HW / ow;
  7789. const int cur_ow = idx % O_HW % ow;
  7790. const Ti* i_ptr = src + nc * I_HW;
  7791. To* o_ptr = dst + nc * O_HW;
  7792. const int start_h = cur_oh * sh - ph;
  7793. const int bh = sycl::max(0, start_h);
  7794. const int eh = sycl::min(ih, start_h + kh);
  7795. const int start_w = cur_ow * sw - pw;
  7796. const int bw = sycl::max(0, start_w);
  7797. const int ew = sycl::min(iw, start_w + kw);
  7798. To res = 0;
  7799. switch (op) {
  7800. case GGML_OP_POOL_AVG: res = 0; break;
  7801. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  7802. }
  7803. for (int i = bh; i < eh; i += 1) {
  7804. for (int j = bw; j < ew; j += 1) {
  7805. #if DPCT_COMPATIBILITY_TEMP >= 350
  7806. /*
  7807. DPCT1098:106: The '*' expression is used instead of the __ldg
  7808. call. These two expressions do not provide the exact same
  7809. functionality. Check the generated code for potential precision
  7810. and/or performance issues.
  7811. */
  7812. Ti cur = *(i_ptr + i * iw + j);
  7813. #else
  7814. Ti cur = i_ptr[i * iw + j];
  7815. #endif
  7816. switch (op) {
  7817. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  7818. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  7819. }
  7820. }
  7821. }
  7822. o_ptr[cur_oh * ow + cur_ow] = res;
  7823. }
  7824. template <int qk, int qr, dequantize_kernel_t dq>
  7825. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  7826. ggml_tensor *dst, const void *src0_dd,
  7827. const int32_t *src1_dd, float *dst_dd,
  7828. dpct::queue_ptr stream) {
  7829. GGML_TENSOR_BINARY_OP_LOCALS
  7830. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7831. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  7832. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7833. // strides in elements
  7834. //const size_t s0 = nb0 / ggml_element_size(dst);
  7835. const size_t s1 = nb1 / ggml_element_size(dst);
  7836. const size_t s2 = nb2 / ggml_element_size(dst);
  7837. const size_t s3 = nb3 / ggml_element_size(dst);
  7838. const size_t s10 = nb10 / ggml_element_size(src1);
  7839. const size_t s11 = nb11 / ggml_element_size(src1);
  7840. const size_t s12 = nb12 / ggml_element_size(src1);
  7841. //const size_t s13 = nb13 / ggml_element_size(src1);
  7842. GGML_ASSERT(ne00 % 2 == 0);
  7843. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7844. [=](sycl::nd_item<3> item_ct1) {
  7845. k_get_rows<qk, qr, dq>(
  7846. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7847. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7848. });
  7849. (void) dst;
  7850. }
  7851. template <typename src0_t>
  7852. static void get_rows_sycl_float(const ggml_tensor *src0,
  7853. const ggml_tensor *src1, ggml_tensor *dst,
  7854. const src0_t *src0_dd, const int32_t *src1_dd,
  7855. float *dst_dd, dpct::queue_ptr stream) {
  7856. GGML_TENSOR_BINARY_OP_LOCALS
  7857. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7858. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  7859. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7860. // strides in elements
  7861. //const size_t s0 = nb0 / ggml_element_size(dst);
  7862. const size_t s1 = nb1 / ggml_element_size(dst);
  7863. const size_t s2 = nb2 / ggml_element_size(dst);
  7864. const size_t s3 = nb3 / ggml_element_size(dst);
  7865. const size_t s10 = nb10 / ggml_element_size(src1);
  7866. const size_t s11 = nb11 / ggml_element_size(src1);
  7867. const size_t s12 = nb12 / ggml_element_size(src1);
  7868. //const size_t s13 = nb13 / ggml_element_size(src1);
  7869. {
  7870. dpct::has_capability_or_fail(stream->get_device(),
  7871. {sycl::aspect::fp16});
  7872. stream->parallel_for(
  7873. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7874. [=](sycl::nd_item<3> item_ct1) {
  7875. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7876. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7877. });
  7878. }
  7879. (void) dst;
  7880. }
  7881. template<float (*bin_op)(const float, const float)>
  7882. struct bin_bcast_sycl {
  7883. template <typename src0_t, typename src1_t, typename dst_t>
  7884. void operator()(const struct ggml_tensor *src0,
  7885. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  7886. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  7887. dpct::queue_ptr stream) {
  7888. GGML_TENSOR_BINARY_OP_LOCALS
  7889. int nr0 = ne10/ne0;
  7890. int nr1 = ne11/ne1;
  7891. int nr2 = ne12/ne2;
  7892. int nr3 = ne13/ne3;
  7893. int nr[4] = { nr0, nr1, nr2, nr3 };
  7894. // collapse dimensions until first broadcast dimension
  7895. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  7896. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  7897. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  7898. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  7899. auto collapse = [](int64_t cne[]) {
  7900. cne[0] *= cne[1];
  7901. cne[1] = cne[2];
  7902. cne[2] = cne[3];
  7903. cne[3] = 1;
  7904. };
  7905. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  7906. cnb[1] *= cne[1];
  7907. cnb[2] *= cne[2];
  7908. cnb[3] *= cne[3];
  7909. };
  7910. for (int i = 0; i < 4; i++) {
  7911. if (nr[i] != 1) {
  7912. break;
  7913. }
  7914. if (i > 0) {
  7915. collapse_nb(cnb0, cne0);
  7916. collapse_nb(cnb1, cne1);
  7917. collapse(cne0);
  7918. collapse(cne1);
  7919. }
  7920. }
  7921. {
  7922. int64_t ne0 = cne0[0];
  7923. int64_t ne1 = cne0[1];
  7924. int64_t ne2 = cne0[2];
  7925. int64_t ne3 = cne0[3];
  7926. int64_t ne10 = cne1[0];
  7927. int64_t ne11 = cne1[1];
  7928. int64_t ne12 = cne1[2];
  7929. int64_t ne13 = cne1[3];
  7930. size_t nb0 = cnb0[0];
  7931. size_t nb1 = cnb0[1];
  7932. size_t nb2 = cnb0[2];
  7933. size_t nb3 = cnb0[3];
  7934. size_t nb10 = cnb1[0];
  7935. size_t nb11 = cnb1[1];
  7936. size_t nb12 = cnb1[2];
  7937. size_t nb13 = cnb1[3];
  7938. size_t s0 = nb0 / sizeof(dst_t);
  7939. size_t s1 = nb1 / sizeof(dst_t);
  7940. size_t s2 = nb2 / sizeof(dst_t);
  7941. size_t s3 = nb3 / sizeof(dst_t);
  7942. size_t s10 = nb10 / sizeof(src1_t);
  7943. size_t s11 = nb11 / sizeof(src1_t);
  7944. size_t s12 = nb12 / sizeof(src1_t);
  7945. size_t s13 = nb13 / sizeof(src1_t);
  7946. GGML_ASSERT(s0 == 1);
  7947. GGML_ASSERT(s10 == 1);
  7948. const int block_size = 128;
  7949. int64_t hne0 = std::max(ne0/2LL, 1LL);
  7950. sycl::range<3> block_dims(1, 1, 1);
  7951. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  7952. block_dims[1] = std::min<unsigned int>(
  7953. ne1, block_size / (unsigned int)block_dims[2]);
  7954. block_dims[0] = std::min(
  7955. std::min<unsigned int>(
  7956. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  7957. (unsigned int)block_dims[1]),
  7958. 64U);
  7959. sycl::range<3> block_nums(
  7960. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  7961. (ne1 + block_dims[1] - 1) / block_dims[1],
  7962. (hne0 + block_dims[2] - 1) / block_dims[2]);
  7963. if (block_nums[0] > 65535) {
  7964. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  7965. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  7966. {
  7967. dpct::has_capability_or_fail(stream->get_device(),
  7968. {sycl::aspect::fp16});
  7969. stream->parallel_for(
  7970. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  7971. sycl::range<3>(1, 1, block_size),
  7972. sycl::range<3>(1, 1, block_size)),
  7973. [=](sycl::nd_item<3> item_ct1) {
  7974. k_bin_bcast_unravel<bin_op>(
  7975. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  7976. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  7977. s13, item_ct1);
  7978. });
  7979. }
  7980. } else {
  7981. /*
  7982. DPCT1049:16: The work-group size passed to the SYCL kernel may
  7983. exceed the limit. To get the device limit, query
  7984. info::device::max_work_group_size. Adjust the work-group size if
  7985. needed.
  7986. */
  7987. dpct::has_capability_or_fail(stream->get_device(),
  7988. {sycl::aspect::fp16});
  7989. stream->parallel_for(
  7990. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7991. [=](sycl::nd_item<3> item_ct1) {
  7992. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  7993. ne2, ne3, ne10, ne11, ne12, ne13,
  7994. s1, s2, s3, s11, s12, s13,
  7995. item_ct1);
  7996. });
  7997. }
  7998. }
  7999. }
  8000. };
  8001. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  8002. const int n_elements, const int ne10, const int ne11,
  8003. const int ne12, const int nb1, const int nb2,
  8004. const int offset, dpct::queue_ptr stream) {
  8005. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  8006. stream->parallel_for(
  8007. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8008. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  8009. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  8010. [=](sycl::nd_item<3> item_ct1) {
  8011. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  8012. item_ct1);
  8013. });
  8014. }
  8015. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  8016. dpct::queue_ptr stream) {
  8017. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8018. stream->parallel_for(
  8019. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8020. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8021. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8022. [=](sycl::nd_item<3> item_ct1) {
  8023. gelu_f32(x, dst, k, item_ct1);
  8024. });
  8025. }
  8026. static void silu_f32_sycl(const float *x, float *dst, const int k,
  8027. dpct::queue_ptr stream) {
  8028. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  8029. stream->parallel_for(
  8030. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8031. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  8032. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  8033. [=](sycl::nd_item<3> item_ct1) {
  8034. silu_f32(x, dst, k, item_ct1);
  8035. });
  8036. }
  8037. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  8038. dpct::queue_ptr stream) {
  8039. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8040. stream->parallel_for(
  8041. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8042. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8043. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8044. [=](sycl::nd_item<3> item_ct1) {
  8045. gelu_quick_f32(x, dst, k, item_ct1);
  8046. });
  8047. }
  8048. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  8049. dpct::queue_ptr stream) {
  8050. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  8051. stream->parallel_for(
  8052. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8053. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  8054. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  8055. [=](sycl::nd_item<3> item_ct1) {
  8056. tanh_f32(x, dst, k, item_ct1);
  8057. });
  8058. }
  8059. static void relu_f32_sycl(const float *x, float *dst, const int k,
  8060. dpct::queue_ptr stream) {
  8061. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8062. stream->parallel_for(
  8063. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8064. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8065. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8066. [=](sycl::nd_item<3> item_ct1) {
  8067. relu_f32(x, dst, k, item_ct1);
  8068. });
  8069. }
  8070. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  8071. dpct::queue_ptr stream) {
  8072. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  8073. stream->parallel_for(
  8074. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8075. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  8076. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  8077. [=](sycl::nd_item<3> item_ct1) {
  8078. hardsigmoid_f32(x, dst, k, item_ct1);
  8079. });
  8080. }
  8081. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  8082. dpct::queue_ptr stream) {
  8083. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  8084. stream->parallel_for(
  8085. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8086. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  8087. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  8088. [=](sycl::nd_item<3> item_ct1) {
  8089. hardswish_f32(x, dst, k, item_ct1);
  8090. });
  8091. }
  8092. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  8093. const float negative_slope,
  8094. dpct::queue_ptr stream) {
  8095. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8096. stream->parallel_for(
  8097. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8098. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8099. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8100. [=](sycl::nd_item<3> item_ct1) {
  8101. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  8102. });
  8103. }
  8104. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  8105. dpct::queue_ptr stream) {
  8106. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  8107. stream->parallel_for(
  8108. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8109. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  8110. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  8111. [=](sycl::nd_item<3> item_ct1) {
  8112. sqr_f32(x, dst, k, item_ct1);
  8113. });
  8114. }
  8115. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  8116. const int nrows, const float eps,
  8117. dpct::queue_ptr stream) {
  8118. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8119. if (ncols < 1024) {
  8120. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8121. stream->submit([&](sycl::handler &cgh) {
  8122. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8123. sycl::range<1>(32), cgh);
  8124. cgh.parallel_for(
  8125. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8126. block_dims),
  8127. [=](sycl::nd_item<3> item_ct1)
  8128. [[intel::reqd_sub_group_size(32)]] {
  8129. norm_f32(x, dst, ncols, eps, item_ct1,
  8130. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8131. });
  8132. });
  8133. } else {
  8134. const int work_group_size = g_work_group_size;
  8135. const sycl::range<3> block_dims(1, 1, work_group_size);
  8136. /*
  8137. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  8138. the limit. To get the device limit, query
  8139. info::device::max_work_group_size. Adjust the work-group size if needed.
  8140. */
  8141. stream->submit([&](sycl::handler &cgh) {
  8142. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8143. sycl::range<1>(32), cgh);
  8144. cgh.parallel_for(
  8145. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8146. block_dims),
  8147. [=](sycl::nd_item<3> item_ct1)
  8148. [[intel::reqd_sub_group_size(32)]] {
  8149. norm_f32(x, dst, ncols, eps, item_ct1,
  8150. s_sum_acc_ct1.get_pointer(), work_group_size);
  8151. });
  8152. });
  8153. }
  8154. }
  8155. static void group_norm_f32_sycl(const float *x, float *dst,
  8156. const int num_groups, const int group_size,
  8157. const int ne_elements, dpct::queue_ptr stream) {
  8158. static const float eps = 1e-6f;
  8159. if (group_size < 1024) {
  8160. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8161. stream->submit([&](sycl::handler &cgh) {
  8162. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8163. cgh);
  8164. const float eps_ct4 = eps;
  8165. cgh.parallel_for(
  8166. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8167. block_dims),
  8168. [=](sycl::nd_item<3> item_ct1)
  8169. [[intel::reqd_sub_group_size(32)]] {
  8170. group_norm_f32(
  8171. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  8172. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8173. });
  8174. });
  8175. } else {
  8176. const int work_group_size = g_work_group_size;
  8177. const sycl::range<3> block_dims(1, 1, work_group_size);
  8178. /*
  8179. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  8180. the limit. To get the device limit, query
  8181. info::device::max_work_group_size. Adjust the work-group size if needed.
  8182. */
  8183. stream->submit([&](sycl::handler &cgh) {
  8184. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8185. cgh);
  8186. const float eps_ct4 = eps;
  8187. cgh.parallel_for(
  8188. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8189. block_dims),
  8190. [=](sycl::nd_item<3> item_ct1)
  8191. [[intel::reqd_sub_group_size(32)]] {
  8192. group_norm_f32(x, dst, group_size, ne_elements,
  8193. eps_ct4, item_ct1,
  8194. s_sum_acc_ct1.get_pointer(), work_group_size);
  8195. });
  8196. });
  8197. }
  8198. }
  8199. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  8200. const int ne0, int ne1, int ne2, int ne02,
  8201. dpct::queue_ptr stream) {
  8202. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  8203. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8204. stream->parallel_for(
  8205. sycl::nd_range<3>(gridDim *
  8206. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  8207. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  8208. [=](sycl::nd_item<3> item_ct1) {
  8209. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  8210. });
  8211. }
  8212. static void upscale_f32_sycl(const float *x, float *dst, const int nb00, const int nb01,
  8213. const int nb02, const int nb03, const int ne10, const int ne11,
  8214. const int ne12, const int ne13, const float sf0, const float sf1,
  8215. const float sf2, const float sf3, dpct::queue_ptr stream) {
  8216. int dst_size = ne10 * ne11 * ne12 * ne13;
  8217. int num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  8218. sycl::range<1> gridDim(num_blocks * SYCL_UPSCALE_BLOCK_SIZE);
  8219. stream->parallel_for(
  8220. sycl::nd_range<1>(gridDim, sycl::range<1>(SYCL_UPSCALE_BLOCK_SIZE)),
  8221. [=](sycl::nd_item<1> item_ct1) {
  8222. upscale_f32(x, dst, nb00, nb01, nb02, nb03, ne10, ne11, ne12, ne13, sf0, sf1, sf2, sf3, item_ct1);
  8223. });
  8224. }
  8225. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  8226. const int ne01, const int ne02, const int ne0,
  8227. const int ne1, const int ne2, dpct::queue_ptr stream) {
  8228. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  8229. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8230. stream->parallel_for(
  8231. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  8232. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  8233. [=](sycl::nd_item<3> item_ct1) {
  8234. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  8235. });
  8236. }
  8237. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  8238. const int nrows, const float eps,
  8239. dpct::queue_ptr stream) {
  8240. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8241. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  8242. if (ncols < 1024) {
  8243. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8244. stream->submit([&](sycl::handler &cgh) {
  8245. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8246. cgh);
  8247. cgh.parallel_for(
  8248. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8249. block_dims),
  8250. [=](sycl::nd_item<3> item_ct1)
  8251. [[intel::reqd_sub_group_size(32)]] {
  8252. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8253. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8254. });
  8255. });
  8256. } else {
  8257. const int work_group_size = g_work_group_size;
  8258. const sycl::range<3> block_dims(1, 1, work_group_size);
  8259. /*
  8260. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  8261. the limit. To get the device limit, query
  8262. info::device::max_work_group_size. Adjust the work-group size if needed.
  8263. */
  8264. stream->submit([&](sycl::handler &cgh) {
  8265. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8266. cgh);
  8267. cgh.parallel_for(
  8268. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8269. block_dims),
  8270. [=](sycl::nd_item<3> item_ct1)
  8271. [[intel::reqd_sub_group_size(32)]] {
  8272. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8273. s_sum_acc_ct1.get_pointer(), work_group_size);
  8274. });
  8275. });
  8276. }
  8277. }
  8278. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  8279. const int ky, const int kx_padded,
  8280. dpct::queue_ptr stream) {
  8281. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  8282. const sycl::range<3> num_blocks(1, ky, block_num_x);
  8283. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  8284. {
  8285. dpct::has_capability_or_fail(stream->get_device(),
  8286. {sycl::aspect::fp16});
  8287. stream->parallel_for(
  8288. sycl::nd_range<3>(num_blocks * block_size, block_size),
  8289. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8290. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  8291. });
  8292. }
  8293. }
  8294. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  8295. static void dequantize_block_sycl(const void *__restrict__ vx,
  8296. dst_t *__restrict__ y, const int k,
  8297. dpct::queue_ptr stream) {
  8298. const int num_blocks = (k + 2*SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2*SYCL_DEQUANTIZE_BLOCK_SIZE);
  8299. {
  8300. dpct::has_capability_or_fail(stream->get_device(),
  8301. {sycl::aspect::fp16});
  8302. stream->parallel_for(
  8303. sycl::nd_range<3>(
  8304. sycl::range<3>(1, 1, num_blocks) *
  8305. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8306. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8307. [=](sycl::nd_item<3> item_ct1) {
  8308. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  8309. });
  8310. }
  8311. }
  8312. template <typename dst_t>
  8313. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  8314. dpct::queue_ptr stream) {
  8315. const int nb = k / QK_K;
  8316. {
  8317. dpct::has_capability_or_fail(stream->get_device(),
  8318. {sycl::aspect::fp16});
  8319. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8320. sycl::range<3>(1, 1, 64),
  8321. sycl::range<3>(1, 1, 64)),
  8322. [=](sycl::nd_item<3> item_ct1) {
  8323. dequantize_block_q2_K(vx, y, item_ct1);
  8324. });
  8325. }
  8326. }
  8327. template <typename dst_t>
  8328. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  8329. dpct::queue_ptr stream) {
  8330. const int nb = k / QK_K;
  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, 64),
  8336. sycl::range<3>(1, 1, 64)),
  8337. [=](sycl::nd_item<3> item_ct1) {
  8338. dequantize_block_q3_K(vx, y, item_ct1);
  8339. });
  8340. }
  8341. }
  8342. template <typename dst_t>
  8343. static void dequantize_row_q4_0_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_0(vx, y, nb32, item_ct1);
  8355. });
  8356. }
  8357. }
  8358. template <typename dst_t>
  8359. static void dequantize_row_q4_1_sycl(const void *vx, dst_t *y, const int k,
  8360. dpct::queue_ptr stream) {
  8361. const int nb32 = k / 32;
  8362. const int nb = (k + 255) / 256;
  8363. {
  8364. dpct::has_capability_or_fail(stream->get_device(),
  8365. {sycl::aspect::fp16});
  8366. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8367. sycl::range<3>(1, 1, 32),
  8368. sycl::range<3>(1, 1, 32)),
  8369. [=](sycl::nd_item<3> item_ct1) {
  8370. dequantize_block_q4_1(vx, y, nb32, item_ct1);
  8371. });
  8372. }
  8373. }
  8374. template <typename dst_t>
  8375. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  8376. dpct::queue_ptr stream) {
  8377. const int nb = k / QK_K;
  8378. {
  8379. dpct::has_capability_or_fail(stream->get_device(),
  8380. {sycl::aspect::fp16});
  8381. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8382. sycl::range<3>(1, 1, 32),
  8383. sycl::range<3>(1, 1, 32)),
  8384. [=](sycl::nd_item<3> item_ct1) {
  8385. dequantize_block_q4_K(vx, y, item_ct1);
  8386. });
  8387. }
  8388. }
  8389. template <typename dst_t>
  8390. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  8391. dpct::queue_ptr stream) {
  8392. const int nb = k / QK_K;
  8393. {
  8394. dpct::has_capability_or_fail(stream->get_device(),
  8395. {sycl::aspect::fp16});
  8396. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8397. sycl::range<3>(1, 1, 64),
  8398. sycl::range<3>(1, 1, 64)),
  8399. [=](sycl::nd_item<3> item_ct1) {
  8400. dequantize_block_q5_K(vx, y, item_ct1);
  8401. });
  8402. }
  8403. }
  8404. template <typename dst_t>
  8405. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  8406. dpct::queue_ptr stream) {
  8407. const int nb = k / QK_K;
  8408. {
  8409. dpct::has_capability_or_fail(stream->get_device(),
  8410. {sycl::aspect::fp16});
  8411. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8412. sycl::range<3>(1, 1, 64),
  8413. sycl::range<3>(1, 1, 64)),
  8414. [=](sycl::nd_item<3> item_ct1) {
  8415. dequantize_block_q6_K(vx, y, item_ct1);
  8416. });
  8417. }
  8418. }
  8419. template <typename dst_t>
  8420. static void dequantize_row_iq1_s_sycl(const void *vx, dst_t *y, const int k,
  8421. dpct::queue_ptr stream) {
  8422. const int nb = k / QK_K;
  8423. {
  8424. dpct::has_capability_or_fail(stream->get_device(),
  8425. {sycl::aspect::fp16});
  8426. stream->submit([&](sycl::handler &cgh) {
  8427. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8428. sycl::range<3>(1, 1, 32),
  8429. sycl::range<3>(1, 1, 32)),
  8430. [=](sycl::nd_item<3> item_ct1) {
  8431. dequantize_block_iq1_s(
  8432. vx, y, item_ct1, iq1s_grid_gpu
  8433. );
  8434. });
  8435. });
  8436. }
  8437. }
  8438. template <typename dst_t>
  8439. static void dequantize_row_iq1_m_sycl(const void *vx, dst_t *y, const int k,
  8440. dpct::queue_ptr stream) {
  8441. const int nb = k / QK_K;
  8442. {
  8443. dpct::has_capability_or_fail(stream->get_device(),
  8444. {sycl::aspect::fp16});
  8445. stream->submit([&](sycl::handler &cgh) {
  8446. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8447. sycl::range<3>(1, 1, 32),
  8448. sycl::range<3>(1, 1, 32)),
  8449. [=](sycl::nd_item<3> item_ct1) {
  8450. dequantize_block_iq1_m(
  8451. vx, y, item_ct1, iq1s_grid_gpu
  8452. );
  8453. });
  8454. });
  8455. }
  8456. }
  8457. template <typename dst_t>
  8458. static void dequantize_row_iq2_xxs_sycl(const void *vx, dst_t *y, const int k,
  8459. dpct::queue_ptr stream) {
  8460. const int nb = k / QK_K;
  8461. {
  8462. dpct::has_capability_or_fail(stream->get_device(),
  8463. {sycl::aspect::fp16});
  8464. stream->submit([&](sycl::handler &cgh) {
  8465. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8466. sycl::range<3>(1, 1, 32),
  8467. sycl::range<3>(1, 1, 32)),
  8468. [=](sycl::nd_item<3> item_ct1) {
  8469. dequantize_block_iq2_xxs(
  8470. vx, y, item_ct1, iq2xxs_grid,
  8471. ksigns_iq2xs, kmask_iq2xs);
  8472. });
  8473. });
  8474. }
  8475. }
  8476. template <typename dst_t>
  8477. static void dequantize_row_iq2_xs_sycl(const void *vx, dst_t *y, const int k,
  8478. dpct::queue_ptr stream) {
  8479. const int nb = k / QK_K;
  8480. {
  8481. dpct::has_capability_or_fail(stream->get_device(),
  8482. {sycl::aspect::fp16});
  8483. stream->submit([&](sycl::handler &cgh) {
  8484. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8485. sycl::range<3>(1, 1, 32),
  8486. sycl::range<3>(1, 1, 32)),
  8487. [=](sycl::nd_item<3> item_ct1) {
  8488. dequantize_block_iq2_xs(
  8489. vx, y, item_ct1, iq2xs_grid,
  8490. ksigns_iq2xs, kmask_iq2xs);
  8491. });
  8492. });
  8493. }
  8494. }
  8495. template <typename dst_t>
  8496. static void dequantize_row_iq2_s_sycl(const void *vx, dst_t *y, const int k,
  8497. dpct::queue_ptr stream) {
  8498. const int nb = k / QK_K;
  8499. {
  8500. dpct::has_capability_or_fail(stream->get_device(),
  8501. {sycl::aspect::fp16});
  8502. stream->submit([&](sycl::handler &cgh) {
  8503. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8504. sycl::range<3>(1, 1, 32),
  8505. sycl::range<3>(1, 1, 32)),
  8506. [=](sycl::nd_item<3> item_ct1) {
  8507. dequantize_block_iq2_s(vx, y, item_ct1);
  8508. });
  8509. });
  8510. }
  8511. }
  8512. template <typename dst_t>
  8513. static void dequantize_row_iq3_xxs_sycl(const void *vx, dst_t *y, const int k,
  8514. dpct::queue_ptr stream) {
  8515. const int nb = k / QK_K;
  8516. {
  8517. dpct::has_capability_or_fail(stream->get_device(),
  8518. {sycl::aspect::fp16});
  8519. stream->submit([&](sycl::handler &cgh) {
  8520. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8521. sycl::range<3>(1, 1, 32),
  8522. sycl::range<3>(1, 1, 32)),
  8523. [=](sycl::nd_item<3> item_ct1) {
  8524. dequantize_block_iq3_xxs(
  8525. vx, y, item_ct1, iq3xxs_grid,
  8526. ksigns_iq2xs, kmask_iq2xs);
  8527. });
  8528. });
  8529. }
  8530. }
  8531. template <typename dst_t>
  8532. static void dequantize_row_iq3_s_sycl(const void *vx, dst_t *y, const int k,
  8533. dpct::queue_ptr stream) {
  8534. const int nb = k / QK_K;
  8535. {
  8536. dpct::has_capability_or_fail(stream->get_device(),
  8537. {sycl::aspect::fp16});
  8538. stream->submit([&](sycl::handler &cgh) {
  8539. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8540. sycl::range<3>(1, 1, 32),
  8541. sycl::range<3>(1, 1, 32)),
  8542. [=](sycl::nd_item<3> item_ct1) {
  8543. dequantize_block_iq3_s(
  8544. vx, y, item_ct1, kmask_iq2xs, iq3s_grid);
  8545. });
  8546. });
  8547. }
  8548. }
  8549. template <typename dst_t>
  8550. static void dequantize_row_iq4_xs_sycl(const void *vx, dst_t *y, const int k,
  8551. dpct::queue_ptr stream) {
  8552. const int nb = (k + QK_K - 1) / QK_K;
  8553. {
  8554. dpct::has_capability_or_fail(stream->get_device(),
  8555. {sycl::aspect::fp16});
  8556. stream->submit([&](sycl::handler &cgh) {
  8557. cgh.parallel_for(
  8558. sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8559. sycl::range<3>(1, 1, 32),
  8560. sycl::range<3>(1, 1, 32)),
  8561. [=](sycl::nd_item<3> item_ct1) {
  8562. dequantize_block_iq4_xs(vx, y, item_ct1);
  8563. });
  8564. });
  8565. }
  8566. }
  8567. template <typename dst_t>
  8568. static void dequantize_row_iq4_nl_sycl(const void *vx, dst_t *y, const int k,
  8569. dpct::queue_ptr stream) {
  8570. const int nb = (k + QK_K - 1) / QK_K;
  8571. {
  8572. dpct::has_capability_or_fail(stream->get_device(),
  8573. {sycl::aspect::fp16});
  8574. stream->submit([&](sycl::handler &cgh) {
  8575. cgh.parallel_for(
  8576. sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8577. sycl::range<3>(1, 1, 32),
  8578. sycl::range<3>(1, 1, 32)),
  8579. [=](sycl::nd_item<3> item_ct1) {
  8580. dequantize_block_iq4_nl(vx, y, item_ct1);
  8581. });
  8582. });
  8583. }
  8584. }
  8585. template <typename src_t, typename dst_t>
  8586. static void convert_unary_sycl(const void *__restrict__ vx,
  8587. dst_t *__restrict__ y, const int k,
  8588. dpct::queue_ptr stream) {
  8589. const int num_blocks = (k + SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / SYCL_DEQUANTIZE_BLOCK_SIZE;
  8590. {
  8591. dpct::has_capability_or_fail(stream->get_device(),
  8592. {sycl::aspect::fp16});
  8593. stream->parallel_for(
  8594. sycl::nd_range<3>(
  8595. sycl::range<3>(1, 1, num_blocks) *
  8596. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8597. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8598. [=](sycl::nd_item<3> item_ct1) {
  8599. convert_unary<src_t>(vx, y, k, item_ct1);
  8600. });
  8601. }
  8602. }
  8603. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) try {
  8604. int id;
  8605. switch (type) {
  8606. case GGML_TYPE_Q4_0:
  8607. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  8608. case GGML_TYPE_Q4_1:
  8609. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  8610. case GGML_TYPE_Q5_0:
  8611. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  8612. case GGML_TYPE_Q5_1:
  8613. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  8614. case GGML_TYPE_Q8_0:
  8615. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  8616. case GGML_TYPE_Q2_K:
  8617. return dequantize_row_q2_K_sycl;
  8618. case GGML_TYPE_Q3_K:
  8619. return dequantize_row_q3_K_sycl;
  8620. case GGML_TYPE_Q4_K:
  8621. return dequantize_row_q4_K_sycl;
  8622. case GGML_TYPE_Q5_K:
  8623. return dequantize_row_q5_K_sycl;
  8624. case GGML_TYPE_Q6_K:
  8625. return dequantize_row_q6_K_sycl;
  8626. case GGML_TYPE_IQ1_S:
  8627. return dequantize_row_iq1_s_sycl;
  8628. case GGML_TYPE_IQ1_M:
  8629. return dequantize_row_iq1_m_sycl;
  8630. case GGML_TYPE_IQ2_XXS:
  8631. return dequantize_row_iq2_xxs_sycl;
  8632. case GGML_TYPE_IQ2_XS:
  8633. return dequantize_row_iq2_xs_sycl;
  8634. case GGML_TYPE_IQ2_S:
  8635. return dequantize_row_iq2_s_sycl;
  8636. case GGML_TYPE_IQ3_XXS:
  8637. return dequantize_row_iq3_xxs_sycl;
  8638. case GGML_TYPE_IQ3_S:
  8639. return dequantize_row_iq3_s_sycl;
  8640. case GGML_TYPE_IQ4_XS:
  8641. return dequantize_row_iq4_xs_sycl;
  8642. case GGML_TYPE_IQ4_NL:
  8643. return dequantize_row_iq4_nl_sycl;
  8644. case GGML_TYPE_F32:
  8645. return convert_unary_sycl<float>;
  8646. default:
  8647. return nullptr;
  8648. }
  8649. }
  8650. catch (sycl::exception const &exc) {
  8651. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8652. << ", line:" << __LINE__ << std::endl;
  8653. std::exit(1);
  8654. }
  8655. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  8656. switch (type) {
  8657. case GGML_TYPE_Q4_0:
  8658. return dequantize_row_q4_0_sycl;
  8659. case GGML_TYPE_Q4_1:
  8660. return dequantize_row_q4_1_sycl;
  8661. case GGML_TYPE_Q5_0:
  8662. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  8663. case GGML_TYPE_Q5_1:
  8664. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  8665. case GGML_TYPE_Q8_0:
  8666. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  8667. case GGML_TYPE_Q2_K:
  8668. return dequantize_row_q2_K_sycl;
  8669. case GGML_TYPE_Q3_K:
  8670. return dequantize_row_q3_K_sycl;
  8671. case GGML_TYPE_Q4_K:
  8672. return dequantize_row_q4_K_sycl;
  8673. case GGML_TYPE_Q5_K:
  8674. return dequantize_row_q5_K_sycl;
  8675. case GGML_TYPE_Q6_K:
  8676. return dequantize_row_q6_K_sycl;
  8677. case GGML_TYPE_IQ1_S:
  8678. return dequantize_row_iq1_s_sycl;
  8679. case GGML_TYPE_IQ1_M:
  8680. return dequantize_row_iq1_m_sycl;
  8681. case GGML_TYPE_IQ2_XXS:
  8682. return dequantize_row_iq2_xxs_sycl;
  8683. case GGML_TYPE_IQ2_XS:
  8684. return dequantize_row_iq2_xs_sycl;
  8685. case GGML_TYPE_IQ2_S:
  8686. return dequantize_row_iq2_s_sycl;
  8687. case GGML_TYPE_IQ3_XXS:
  8688. return dequantize_row_iq3_xxs_sycl;
  8689. case GGML_TYPE_IQ3_S:
  8690. return dequantize_row_iq3_s_sycl;
  8691. case GGML_TYPE_IQ4_XS:
  8692. return dequantize_row_iq4_xs_sycl;
  8693. case GGML_TYPE_IQ4_NL:
  8694. return dequantize_row_iq4_nl_sycl;
  8695. case GGML_TYPE_F16:
  8696. return convert_unary_sycl<sycl::half>;
  8697. default:
  8698. return nullptr;
  8699. }
  8700. }
  8701. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  8702. float *dst, const int ncols,
  8703. const int nrows,
  8704. dpct::queue_ptr stream) {
  8705. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8706. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8707. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  8708. const sycl::range<3> block_nums(1, 1, block_num_y);
  8709. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8710. {
  8711. dpct::has_capability_or_fail(stream->get_device(),
  8712. {sycl::aspect::fp16});
  8713. stream->parallel_for(
  8714. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8715. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8716. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  8717. vx, y, dst, ncols, nrows, item_ct1);
  8718. });
  8719. }
  8720. }
  8721. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  8722. float *dst, const int ncols,
  8723. const int nrows,
  8724. dpct::queue_ptr stream) {
  8725. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8726. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8727. const sycl::range<3> block_nums(1, 1, block_num_y);
  8728. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8729. {
  8730. dpct::has_capability_or_fail(stream->get_device(),
  8731. {sycl::aspect::fp16});
  8732. stream->parallel_for(
  8733. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8734. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8735. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  8736. vx, y, dst, ncols, nrows, item_ct1);
  8737. });
  8738. }
  8739. }
  8740. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  8741. float *dst, const int ncols,
  8742. const int nrows,
  8743. dpct::queue_ptr stream) {
  8744. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8745. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8746. const sycl::range<3> block_nums(1, 1, block_num_y);
  8747. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8748. {
  8749. dpct::has_capability_or_fail(stream->get_device(),
  8750. {sycl::aspect::fp16});
  8751. stream->parallel_for(
  8752. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8753. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8754. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  8755. vx, y, dst, ncols, nrows, item_ct1);
  8756. });
  8757. }
  8758. }
  8759. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  8760. float *dst, const int ncols,
  8761. const int nrows,
  8762. dpct::queue_ptr stream) {
  8763. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8764. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8765. const sycl::range<3> block_nums(1, 1, block_num_y);
  8766. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8767. {
  8768. dpct::has_capability_or_fail(stream->get_device(),
  8769. {sycl::aspect::fp16});
  8770. stream->parallel_for(
  8771. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8772. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8773. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  8774. vx, y, dst, ncols, nrows, item_ct1);
  8775. });
  8776. }
  8777. }
  8778. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  8779. float *dst, const int ncols,
  8780. const int nrows,
  8781. dpct::queue_ptr stream) {
  8782. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8783. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8784. const sycl::range<3> block_nums(1, 1, block_num_y);
  8785. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8786. {
  8787. dpct::has_capability_or_fail(stream->get_device(),
  8788. {sycl::aspect::fp16});
  8789. stream->parallel_for(
  8790. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8791. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8792. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  8793. vx, y, dst, ncols, nrows, item_ct1);
  8794. });
  8795. }
  8796. }
  8797. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  8798. float *dst, const int ncols,
  8799. const int nrows,
  8800. dpct::queue_ptr stream) {
  8801. GGML_ASSERT(ncols % QK_K == 0);
  8802. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  8803. const int block_num_y = (nrows + ny - 1) / ny;
  8804. const sycl::range<3> block_nums(1, 1, block_num_y);
  8805. const sycl::range<3> block_dims(1, ny, 32);
  8806. stream->parallel_for(
  8807. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8808. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8809. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  8810. });
  8811. }
  8812. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  8813. float *dst, const int ncols,
  8814. const int nrows,
  8815. dpct::queue_ptr stream) {
  8816. GGML_ASSERT(ncols % QK_K == 0);
  8817. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8818. const int block_num_y = (nrows + ny - 1) / ny;
  8819. const sycl::range<3> block_nums(1, 1, block_num_y);
  8820. const sycl::range<3> block_dims(1, ny, 32);
  8821. stream->parallel_for(
  8822. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8823. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8824. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  8825. });
  8826. }
  8827. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  8828. float *dst, const int ncols,
  8829. const int nrows,
  8830. dpct::queue_ptr stream) {
  8831. GGML_ASSERT(ncols % QK_K == 0);
  8832. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8833. const int block_num_y = (nrows + ny - 1) / ny;
  8834. const sycl::range<3> block_nums(1, 1, block_num_y);
  8835. const sycl::range<3> block_dims(1, ny, 32);
  8836. stream->parallel_for(
  8837. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8838. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8839. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  8840. });
  8841. }
  8842. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  8843. float *dst, const int ncols,
  8844. const int nrows,
  8845. dpct::queue_ptr stream) {
  8846. GGML_ASSERT(ncols % QK_K == 0);
  8847. const sycl::range<3> block_dims(1, 1, 32);
  8848. stream->parallel_for(
  8849. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  8850. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8851. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  8852. });
  8853. }
  8854. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  8855. float *dst, const int ncols,
  8856. const int nrows,
  8857. dpct::queue_ptr stream) {
  8858. GGML_ASSERT(ncols % QK_K == 0);
  8859. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8860. const int block_num_y = (nrows + ny - 1) / ny;
  8861. const sycl::range<3> block_nums(1, 1, block_num_y);
  8862. const sycl::range<3> block_dims(1, ny, 32);
  8863. stream->parallel_for(
  8864. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8865. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8866. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  8867. });
  8868. }
  8869. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  8870. float *dst, const int ncols,
  8871. const int nrows,
  8872. dpct::queue_ptr stream) {
  8873. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8874. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8875. const sycl::range<3> block_nums(1, 1, block_num_y);
  8876. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8877. {
  8878. dpct::has_capability_or_fail(stream->get_device(),
  8879. {sycl::aspect::fp16});
  8880. stream->parallel_for(
  8881. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8882. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8883. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  8884. nrows, item_ct1);
  8885. });
  8886. }
  8887. }
  8888. static void mul_mat_vec_q4_0_q8_1_sycl(const void *vx, const void *vy,
  8889. float *dst, const int ncols,
  8890. const int nrows,
  8891. dpct::queue_ptr stream) {
  8892. GGML_ASSERT(ncols % QK4_0 == 0);
  8893. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8894. const sycl::range<3> block_nums(1, 1, block_num_y);
  8895. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8896. {
  8897. stream->submit([&](sycl::handler &cgh) {
  8898. cgh.parallel_for(
  8899. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8900. [=](sycl::nd_item<3> item_ct1)
  8901. [[intel::reqd_sub_group_size(32)]] {
  8902. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0,
  8903. VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>(
  8904. vx, vy, dst, ncols, nrows, item_ct1);
  8905. });
  8906. });
  8907. }
  8908. }
  8909. static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
  8910. float *dst, const int ncols,
  8911. const int nrows,
  8912. dpct::queue_ptr stream) {
  8913. GGML_ASSERT(ncols % QK4_1 == 0);
  8914. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8915. const sycl::range<3> block_nums(1, 1, block_num_y);
  8916. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8917. {
  8918. stream->submit([&](sycl::handler &cgh) {
  8919. cgh.parallel_for(
  8920. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8921. [=](sycl::nd_item<3> item_ct1)
  8922. [[intel::reqd_sub_group_size(32)]] {
  8923. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1,
  8924. VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>(
  8925. vx, vy, dst, ncols, nrows, item_ct1);
  8926. });
  8927. });
  8928. }
  8929. }
  8930. static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
  8931. float *dst, const int ncols,
  8932. const int nrows,
  8933. dpct::queue_ptr stream) {
  8934. GGML_ASSERT(ncols % QK5_0 == 0);
  8935. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8936. const sycl::range<3> block_nums(1, 1, block_num_y);
  8937. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8938. {
  8939. stream->submit([&](sycl::handler &cgh) {
  8940. cgh.parallel_for(
  8941. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8942. [=](sycl::nd_item<3> item_ct1)
  8943. [[intel::reqd_sub_group_size(32)]] {
  8944. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0,
  8945. VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>(
  8946. vx, vy, dst, ncols, nrows, item_ct1);
  8947. });
  8948. });
  8949. }
  8950. }
  8951. static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
  8952. float *dst, const int ncols,
  8953. const int nrows,
  8954. dpct::queue_ptr stream) {
  8955. GGML_ASSERT(ncols % QK5_1 == 0);
  8956. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8957. const sycl::range<3> block_nums(1, 1, block_num_y);
  8958. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8959. {
  8960. stream->submit([&](sycl::handler &cgh) {
  8961. cgh.parallel_for(
  8962. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8963. [=](sycl::nd_item<3> item_ct1)
  8964. [[intel::reqd_sub_group_size(32)]] {
  8965. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1,
  8966. VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>(
  8967. vx, vy, dst, ncols, nrows, item_ct1);
  8968. });
  8969. });
  8970. }
  8971. }
  8972. static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
  8973. float *dst, const int ncols,
  8974. const int nrows,
  8975. dpct::queue_ptr stream) {
  8976. GGML_ASSERT(ncols % QK8_0 == 0);
  8977. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8978. const sycl::range<3> block_nums(1, 1, block_num_y);
  8979. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8980. {
  8981. stream->submit([&](sycl::handler &cgh) {
  8982. cgh.parallel_for(
  8983. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8984. [=](sycl::nd_item<3> item_ct1)
  8985. [[intel::reqd_sub_group_size(32)]] {
  8986. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0,
  8987. VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>(
  8988. vx, vy, dst, ncols, nrows, item_ct1);
  8989. });
  8990. });
  8991. }
  8992. }
  8993. static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
  8994. float *dst, const int ncols,
  8995. const int nrows,
  8996. dpct::queue_ptr stream) {
  8997. GGML_ASSERT(ncols % QK_K == 0);
  8998. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8999. const sycl::range<3> block_nums(1, 1, block_num_y);
  9000. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9001. {
  9002. stream->submit([&](sycl::handler &cgh) {
  9003. cgh.parallel_for(
  9004. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9005. [=](sycl::nd_item<3> item_ct1)
  9006. [[intel::reqd_sub_group_size(32)]] {
  9007. mul_mat_vec_q<QK_K, QI2_K, block_q2_K,
  9008. VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>(
  9009. vx, vy, dst, ncols, nrows, item_ct1);
  9010. });
  9011. });
  9012. }
  9013. }
  9014. static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
  9015. float *dst, const int ncols,
  9016. const int nrows,
  9017. dpct::queue_ptr stream) {
  9018. GGML_ASSERT(ncols % QK_K == 0);
  9019. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9020. const sycl::range<3> block_nums(1, 1, block_num_y);
  9021. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9022. {
  9023. stream->submit([&](sycl::handler &cgh) {
  9024. cgh.parallel_for(
  9025. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9026. [=](sycl::nd_item<3> item_ct1)
  9027. [[intel::reqd_sub_group_size(32)]] {
  9028. mul_mat_vec_q<QK_K, QI3_K, block_q3_K,
  9029. VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>(
  9030. vx, vy, dst, ncols, nrows, item_ct1);
  9031. });
  9032. });
  9033. }
  9034. }
  9035. static void mul_mat_vec_q4_K_q8_1_sycl(const void *vx, const void *vy,
  9036. float *dst, const int ncols,
  9037. const int nrows,
  9038. dpct::queue_ptr stream) {
  9039. GGML_ASSERT(ncols % QK_K == 0);
  9040. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9041. const sycl::range<3> block_nums(1, 1, block_num_y);
  9042. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9043. {
  9044. stream->submit([&](sycl::handler &cgh) {
  9045. cgh.parallel_for(
  9046. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9047. [=](sycl::nd_item<3> item_ct1)
  9048. [[intel::reqd_sub_group_size(32)]] {
  9049. mul_mat_vec_q<QK_K, QI4_K, block_q4_K,
  9050. VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>(
  9051. vx, vy, dst, ncols, nrows, item_ct1);
  9052. });
  9053. });
  9054. }
  9055. }
  9056. static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
  9057. float *dst, const int ncols,
  9058. const int nrows,
  9059. dpct::queue_ptr stream) {
  9060. GGML_ASSERT(ncols % QK_K == 0);
  9061. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9062. const sycl::range<3> block_nums(1, 1, block_num_y);
  9063. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9064. {
  9065. stream->submit([&](sycl::handler &cgh) {
  9066. cgh.parallel_for(
  9067. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9068. [=](sycl::nd_item<3> item_ct1)
  9069. [[intel::reqd_sub_group_size(32)]] {
  9070. mul_mat_vec_q<QK_K, QI5_K, block_q5_K,
  9071. VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
  9072. vx, vy, dst, ncols, nrows, item_ct1);
  9073. });
  9074. });
  9075. }
  9076. }
  9077. static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
  9078. float *dst, const int ncols,
  9079. const int nrows,
  9080. dpct::queue_ptr stream) {
  9081. GGML_ASSERT(ncols % QK_K == 0);
  9082. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9083. const sycl::range<3> block_nums(1, 1, block_num_y);
  9084. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9085. {
  9086. stream->submit([&](sycl::handler &cgh) {
  9087. cgh.parallel_for(
  9088. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9089. [=](sycl::nd_item<3> item_ct1)
  9090. [[intel::reqd_sub_group_size(32)]] {
  9091. mul_mat_vec_q<QK_K, QI6_K, block_q6_K,
  9092. VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>(
  9093. vx, vy, dst, ncols, nrows, item_ct1);
  9094. });
  9095. });
  9096. }
  9097. }
  9098. static void mul_mat_vec_iq2_xxs_q8_1_sycl(const void *vx, const void *vy,
  9099. float *dst, const int ncols,
  9100. const int nrows,
  9101. dpct::queue_ptr stream) {
  9102. GGML_ASSERT(ncols % QK_K == 0);
  9103. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9104. const sycl::range<3> block_nums(1, 1, block_num_y);
  9105. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9106. {
  9107. stream->submit([&](sycl::handler &cgh) {
  9108. cgh.parallel_for(
  9109. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9110. [=](sycl::nd_item<3> item_ct1)
  9111. [[intel::reqd_sub_group_size(32)]] {
  9112. mul_mat_vec_q_iq2_xxs_q8_1<QK_K, QI2_XXS, block_iq2_xxs, 1>(
  9113. vx, vy, dst, ncols, nrows, item_ct1);
  9114. });
  9115. });
  9116. }
  9117. }
  9118. static void mul_mat_vec_iq2_xs_q8_1_sycl(const void *vx, const void *vy,
  9119. float *dst, const int ncols,
  9120. const int nrows,
  9121. dpct::queue_ptr stream) {
  9122. GGML_ASSERT(ncols % QK_K == 0);
  9123. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9124. const sycl::range<3> block_nums(1, 1, block_num_y);
  9125. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9126. {
  9127. stream->submit([&](sycl::handler &cgh) {
  9128. auto iq2xs_grid_ptr_ct1 = &iq2xs_grid[0];
  9129. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9130. cgh.parallel_for(
  9131. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9132. [=](sycl::nd_item<3> item_ct1)
  9133. [[intel::reqd_sub_group_size(32)]] {
  9134. mul_mat_vec_q_iq2_xs_q8_1<QK_K, QI2_XS, block_iq2_xs, 1>(
  9135. vx, vy, dst, ncols, nrows, item_ct1);
  9136. });
  9137. });
  9138. }
  9139. }
  9140. static void mul_mat_vec_iq2_s_q8_1_sycl(const void *vx, const void *vy,
  9141. float *dst, const int ncols,
  9142. const int nrows,
  9143. dpct::queue_ptr stream) {
  9144. GGML_ASSERT(ncols % QK_K == 0);
  9145. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9146. const sycl::range<3> block_nums(1, 1, block_num_y);
  9147. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9148. {
  9149. stream->submit([&](sycl::handler &cgh) {
  9150. auto iq2xs_grid_ptr_ct1 = &iq2xs_grid[0];
  9151. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9152. cgh.parallel_for(
  9153. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9154. [=](sycl::nd_item<3> item_ct1)
  9155. [[intel::reqd_sub_group_size(32)]] {
  9156. mul_mat_vec_q_iq2_s_q8_1<QK_K, QI2_S, block_iq2_s, 1>(
  9157. vx, vy, dst, ncols, nrows, item_ct1);
  9158. });
  9159. });
  9160. }
  9161. }
  9162. static void mul_mat_vec_iq3_xxs_q8_1_sycl(const void *vx, const void *vy,
  9163. float *dst, const int ncols,
  9164. const int nrows,
  9165. dpct::queue_ptr stream) {
  9166. GGML_ASSERT(ncols % QK_K == 0);
  9167. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9168. const sycl::range<3> block_nums(1, 1, block_num_y);
  9169. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9170. {
  9171. stream->submit([&](sycl::handler &cgh) {
  9172. auto iq3xxs_grid_ptr_ct1 = &iq3xxs_grid[0];
  9173. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9174. cgh.parallel_for(
  9175. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9176. [=](sycl::nd_item<3> item_ct1)
  9177. [[intel::reqd_sub_group_size(32)]] {
  9178. mul_mat_vec_q_iq3_xxs_q8_1<QK_K, QI3_XXS, block_iq3_xxs, 1>(
  9179. vx, vy, dst, ncols, nrows, item_ct1);
  9180. });
  9181. });
  9182. }
  9183. }
  9184. static void mul_mat_vec_iq3_s_q8_1_sycl(const void *vx, const void *vy,
  9185. float *dst, const int ncols,
  9186. const int nrows,
  9187. dpct::queue_ptr stream) {
  9188. GGML_ASSERT(ncols % QK_K == 0);
  9189. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9190. const sycl::range<3> block_nums(1, 1, block_num_y);
  9191. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9192. {
  9193. stream->submit([&](sycl::handler &cgh) {
  9194. auto iq3s_grid_ptr_ct1 = &iq3s_grid[0];
  9195. cgh.parallel_for(
  9196. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9197. [=](sycl::nd_item<3> item_ct1)
  9198. [[intel::reqd_sub_group_size(32)]] {
  9199. mul_mat_vec_q_iq3_s_q8_1<QK_K, QI3_XS, block_iq3_s, 1>(
  9200. vx, vy, dst, ncols, nrows, item_ct1);
  9201. });
  9202. });
  9203. }
  9204. }
  9205. static void mul_mat_vec_iq1_s_q8_1_sycl(const void *vx, const void *vy,
  9206. float *dst, const int ncols,
  9207. const int nrows,
  9208. dpct::queue_ptr stream) {
  9209. GGML_ASSERT(ncols % QK_K == 0);
  9210. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9211. const sycl::range<3> block_nums(1, 1, block_num_y);
  9212. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9213. {
  9214. stream->submit([&](sycl::handler &cgh) {
  9215. auto iq1s_grid_ptr_ct1 = &iq1s_grid_gpu[0];
  9216. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9217. cgh.parallel_for(
  9218. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9219. [=](sycl::nd_item<3> item_ct1)
  9220. [[intel::reqd_sub_group_size(32)]] {
  9221. mul_mat_vec_q_iq1_s_q8_1<QK_K, QI1_S, block_iq1_s, 1>(
  9222. vx, vy, dst, ncols, nrows, item_ct1);
  9223. });
  9224. });
  9225. }
  9226. }
  9227. static void mul_mat_vec_iq1_m_q8_1_sycl(const void *vx, const void *vy,
  9228. float *dst, const int ncols,
  9229. const int nrows,
  9230. dpct::queue_ptr stream) {
  9231. GGML_ASSERT(ncols % QK_K == 0);
  9232. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9233. const sycl::range<3> block_nums(1, 1, block_num_y);
  9234. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9235. {
  9236. stream->submit([&](sycl::handler &cgh) {
  9237. cgh.parallel_for(
  9238. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9239. [=](sycl::nd_item<3> item_ct1)
  9240. [[intel::reqd_sub_group_size(32)]] {
  9241. mul_mat_vec_q_iq1_m_q8_1<QK_K, QI1_S, block_iq1_m, 1>(
  9242. vx, vy, dst, ncols, nrows, item_ct1);
  9243. });
  9244. });
  9245. }
  9246. }
  9247. static void mul_mat_vec_iq4_nl_q8_1_sycl(const void *vx, const void *vy,
  9248. float *dst, const int ncols,
  9249. const int nrows,
  9250. dpct::queue_ptr stream) {
  9251. GGML_ASSERT(ncols % QK4_NL == 0);
  9252. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9253. const sycl::range<3> block_nums(1, 1, block_num_y);
  9254. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9255. {
  9256. stream->submit([&](sycl::handler &cgh) {
  9257. cgh.parallel_for(
  9258. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9259. [=](sycl::nd_item<3> item_ct1)
  9260. [[intel::reqd_sub_group_size(32)]] {
  9261. mul_mat_vec_q_iq4_nl_q8_1<QK4_NL, QI4_NL, block_iq4_nl, 1>(
  9262. vx, vy, dst, ncols, nrows, item_ct1);
  9263. });
  9264. });
  9265. }
  9266. }
  9267. static void mul_mat_vec_iq4_xs_q8_1_sycl(const void *vx, const void *vy,
  9268. float *dst, const int ncols,
  9269. const int nrows,
  9270. dpct::queue_ptr stream) {
  9271. GGML_ASSERT(ncols % QK_K == 0);
  9272. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9273. const sycl::range<3> block_nums(1, 1, block_num_y);
  9274. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9275. {
  9276. stream->submit([&](sycl::handler &cgh) {
  9277. cgh.parallel_for(
  9278. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9279. [=](sycl::nd_item<3> item_ct1)
  9280. [[intel::reqd_sub_group_size(32)]] {
  9281. mul_mat_vec_q_iq4_xs_q8_1<QK_K, QI4_XS, block_iq4_xs, 1>(
  9282. vx, vy, dst, ncols, nrows, item_ct1);
  9283. });
  9284. });
  9285. }
  9286. }
  9287. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9288. float *dst, const int ncols_x,
  9289. const int nrows_x, const int ncols_y,
  9290. const int nrows_y, const int nrows_dst,
  9291. dpct::queue_ptr stream) try {
  9292. int id;
  9293. SYCL_CHECK(
  9294. CHECK_TRY_ERROR(id = get_current_device_id()));
  9295. const int compute_capability = g_device_caps[id].cc;
  9296. int mmq_x, mmq_y, nwarps;
  9297. if (compute_capability >= VER_GEN13) {
  9298. mmq_x = MMQ_X_Q4_0_RDNA2;
  9299. mmq_y = MMQ_Y_Q4_0_RDNA2;
  9300. nwarps = NWARPS_Q4_0_RDNA2;
  9301. } else if (compute_capability >= VER_GEN12) {
  9302. mmq_x = MMQ_X_Q4_0_RDNA1;
  9303. mmq_y = MMQ_Y_Q4_0_RDNA1;
  9304. nwarps = NWARPS_Q4_0_RDNA1;
  9305. } else if (compute_capability >= VER_GEN9) {
  9306. mmq_x = MMQ_X_Q4_0_AMPERE;
  9307. mmq_y = MMQ_Y_Q4_0_AMPERE;
  9308. nwarps = NWARPS_Q4_0_AMPERE;
  9309. } else if (compute_capability >= VER_4VEC) {
  9310. mmq_x = MMQ_X_Q4_0_PASCAL;
  9311. mmq_y = MMQ_Y_Q4_0_PASCAL;
  9312. nwarps = NWARPS_Q4_0_PASCAL;
  9313. } else {
  9314. GGML_ASSERT(false);
  9315. }
  9316. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9317. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9318. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9319. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9320. if (nrows_x % mmq_y == 0) {
  9321. const bool need_check = false;
  9322. /*
  9323. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  9324. the limit. To get the device limit, query
  9325. info::device::max_work_group_size. Adjust the work-group size if needed.
  9326. */
  9327. {
  9328. dpct::has_capability_or_fail(stream->get_device(),
  9329. {sycl::aspect::fp16});
  9330. stream->submit([&](sycl::handler &cgh) {
  9331. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9332. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9333. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9334. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9335. cgh);
  9336. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9337. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9338. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9339. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9340. cgh.parallel_for(
  9341. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9342. [=](sycl::nd_item<3> item_ct1) {
  9343. mul_mat_q4_0<need_check>(
  9344. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9345. nrows_dst, item_ct1,
  9346. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9347. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9348. tile_y_qs_acc_ct1.get_pointer(),
  9349. tile_y_ds_acc_ct1.get_pointer());
  9350. });
  9351. });
  9352. }
  9353. } else {
  9354. const bool need_check = true;
  9355. /*
  9356. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  9357. the limit. To get the device limit, query
  9358. info::device::max_work_group_size. Adjust the work-group size if needed.
  9359. */
  9360. {
  9361. dpct::has_capability_or_fail(stream->get_device(),
  9362. {sycl::aspect::fp16});
  9363. stream->submit([&](sycl::handler &cgh) {
  9364. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9365. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9366. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9367. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9368. cgh);
  9369. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9370. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9371. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9372. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9373. cgh.parallel_for(
  9374. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9375. [=](sycl::nd_item<3> item_ct1) {
  9376. mul_mat_q4_0<need_check>(
  9377. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9378. nrows_dst, item_ct1,
  9379. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9380. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9381. tile_y_qs_acc_ct1.get_pointer(),
  9382. tile_y_ds_acc_ct1.get_pointer());
  9383. });
  9384. });
  9385. }
  9386. }
  9387. }
  9388. catch (sycl::exception const &exc) {
  9389. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9390. << ", line:" << __LINE__ << std::endl;
  9391. std::exit(1);
  9392. }
  9393. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9394. float *dst, const int ncols_x,
  9395. const int nrows_x, const int ncols_y,
  9396. const int nrows_y, const int nrows_dst,
  9397. dpct::queue_ptr stream) try {
  9398. int id;
  9399. SYCL_CHECK(
  9400. CHECK_TRY_ERROR(id = get_current_device_id()));
  9401. const int compute_capability = g_device_caps[id].cc;
  9402. int mmq_x, mmq_y, nwarps;
  9403. if (compute_capability >= VER_GEN13) {
  9404. mmq_x = MMQ_X_Q4_1_RDNA2;
  9405. mmq_y = MMQ_Y_Q4_1_RDNA2;
  9406. nwarps = NWARPS_Q4_1_RDNA2;
  9407. } else if (compute_capability >= VER_GEN12) {
  9408. mmq_x = MMQ_X_Q4_1_RDNA1;
  9409. mmq_y = MMQ_Y_Q4_1_RDNA1;
  9410. nwarps = NWARPS_Q4_1_RDNA1;
  9411. } else if (compute_capability >= VER_GEN9) {
  9412. mmq_x = MMQ_X_Q4_1_AMPERE;
  9413. mmq_y = MMQ_Y_Q4_1_AMPERE;
  9414. nwarps = NWARPS_Q4_1_AMPERE;
  9415. } else if (compute_capability >= VER_4VEC) {
  9416. mmq_x = MMQ_X_Q4_1_PASCAL;
  9417. mmq_y = MMQ_Y_Q4_1_PASCAL;
  9418. nwarps = NWARPS_Q4_1_PASCAL;
  9419. } else {
  9420. GGML_ASSERT(false);
  9421. }
  9422. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9423. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9424. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9425. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9426. if (nrows_x % mmq_y == 0) {
  9427. const bool need_check = false;
  9428. /*
  9429. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  9430. the limit. To get the device limit, query
  9431. info::device::max_work_group_size. Adjust the work-group size if needed.
  9432. */
  9433. {
  9434. dpct::has_capability_or_fail(stream->get_device(),
  9435. {sycl::aspect::fp16});
  9436. stream->submit([&](sycl::handler &cgh) {
  9437. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9438. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9439. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9440. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9441. cgh);
  9442. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9443. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9444. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9445. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9446. cgh.parallel_for(
  9447. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9448. [=](sycl::nd_item<3> item_ct1) {
  9449. mul_mat_q4_1<need_check>(
  9450. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9451. nrows_dst, item_ct1,
  9452. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9453. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9454. tile_y_qs_acc_ct1.get_pointer(),
  9455. tile_y_ds_acc_ct1.get_pointer());
  9456. });
  9457. });
  9458. }
  9459. } else {
  9460. const bool need_check = true;
  9461. /*
  9462. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  9463. the limit. To get the device limit, query
  9464. info::device::max_work_group_size. Adjust the work-group size if needed.
  9465. */
  9466. {
  9467. dpct::has_capability_or_fail(stream->get_device(),
  9468. {sycl::aspect::fp16});
  9469. stream->submit([&](sycl::handler &cgh) {
  9470. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9471. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9472. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9473. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9474. cgh);
  9475. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9476. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9477. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9478. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9479. cgh.parallel_for(
  9480. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9481. [=](sycl::nd_item<3> item_ct1) {
  9482. mul_mat_q4_1<need_check>(
  9483. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9484. nrows_dst, item_ct1,
  9485. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9486. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9487. tile_y_qs_acc_ct1.get_pointer(),
  9488. tile_y_ds_acc_ct1.get_pointer());
  9489. });
  9490. });
  9491. }
  9492. }
  9493. }
  9494. catch (sycl::exception const &exc) {
  9495. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9496. << ", line:" << __LINE__ << std::endl;
  9497. std::exit(1);
  9498. }
  9499. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9500. float *dst, const int ncols_x,
  9501. const int nrows_x, const int ncols_y,
  9502. const int nrows_y, const int nrows_dst,
  9503. dpct::queue_ptr stream) try {
  9504. int id;
  9505. SYCL_CHECK(
  9506. CHECK_TRY_ERROR(id = get_current_device_id()));
  9507. const int compute_capability = g_device_caps[id].cc;
  9508. int mmq_x, mmq_y, nwarps;
  9509. if (compute_capability >= VER_GEN13) {
  9510. mmq_x = MMQ_X_Q5_0_RDNA2;
  9511. mmq_y = MMQ_Y_Q5_0_RDNA2;
  9512. nwarps = NWARPS_Q5_0_RDNA2;
  9513. } else if (compute_capability >= VER_GEN12) {
  9514. mmq_x = MMQ_X_Q5_0_RDNA1;
  9515. mmq_y = MMQ_Y_Q5_0_RDNA1;
  9516. nwarps = NWARPS_Q5_0_RDNA1;
  9517. } else if (compute_capability >= VER_GEN9) {
  9518. mmq_x = MMQ_X_Q5_0_AMPERE;
  9519. mmq_y = MMQ_Y_Q5_0_AMPERE;
  9520. nwarps = NWARPS_Q5_0_AMPERE;
  9521. } else if (compute_capability >= VER_4VEC) {
  9522. mmq_x = MMQ_X_Q5_0_PASCAL;
  9523. mmq_y = MMQ_Y_Q5_0_PASCAL;
  9524. nwarps = NWARPS_Q5_0_PASCAL;
  9525. } else {
  9526. GGML_ASSERT(false);
  9527. }
  9528. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9529. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9530. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9531. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9532. if (nrows_x % mmq_y == 0) {
  9533. const bool need_check = false;
  9534. /*
  9535. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  9536. the limit. To get the device limit, query
  9537. info::device::max_work_group_size. Adjust the work-group size if needed.
  9538. */
  9539. {
  9540. dpct::has_capability_or_fail(stream->get_device(),
  9541. {sycl::aspect::fp16});
  9542. stream->submit([&](sycl::handler &cgh) {
  9543. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9544. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9545. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9546. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9547. cgh);
  9548. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9549. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9550. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9551. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9552. cgh.parallel_for(
  9553. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9554. [=](sycl::nd_item<3> item_ct1) {
  9555. mul_mat_q5_0<need_check>(
  9556. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9557. nrows_dst, item_ct1,
  9558. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9559. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9560. tile_y_qs_acc_ct1.get_pointer(),
  9561. tile_y_ds_acc_ct1.get_pointer());
  9562. });
  9563. });
  9564. }
  9565. } else {
  9566. const bool need_check = true;
  9567. /*
  9568. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  9569. the limit. To get the device limit, query
  9570. info::device::max_work_group_size. Adjust the work-group size if needed.
  9571. */
  9572. {
  9573. dpct::has_capability_or_fail(stream->get_device(),
  9574. {sycl::aspect::fp16});
  9575. stream->submit([&](sycl::handler &cgh) {
  9576. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9577. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9578. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9579. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9580. cgh);
  9581. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9582. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9583. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9584. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9585. cgh.parallel_for(
  9586. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9587. [=](sycl::nd_item<3> item_ct1) {
  9588. mul_mat_q5_0<need_check>(
  9589. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9590. nrows_dst, item_ct1,
  9591. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9592. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9593. tile_y_qs_acc_ct1.get_pointer(),
  9594. tile_y_ds_acc_ct1.get_pointer());
  9595. });
  9596. });
  9597. }
  9598. }
  9599. }
  9600. catch (sycl::exception const &exc) {
  9601. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9602. << ", line:" << __LINE__ << std::endl;
  9603. std::exit(1);
  9604. }
  9605. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9606. float *dst, const int ncols_x,
  9607. const int nrows_x, const int ncols_y,
  9608. const int nrows_y, const int nrows_dst,
  9609. dpct::queue_ptr stream) try {
  9610. int id;
  9611. SYCL_CHECK(
  9612. CHECK_TRY_ERROR(id = get_current_device_id()));
  9613. const int compute_capability = g_device_caps[id].cc;
  9614. int mmq_x, mmq_y, nwarps;
  9615. if (compute_capability >= VER_GEN13) {
  9616. mmq_x = MMQ_X_Q5_1_RDNA2;
  9617. mmq_y = MMQ_Y_Q5_1_RDNA2;
  9618. nwarps = NWARPS_Q5_1_RDNA2;
  9619. } else if (compute_capability >= VER_GEN12) {
  9620. mmq_x = MMQ_X_Q5_1_RDNA1;
  9621. mmq_y = MMQ_Y_Q5_1_RDNA1;
  9622. nwarps = NWARPS_Q5_1_RDNA1;
  9623. } else if (compute_capability >= VER_GEN9) {
  9624. mmq_x = MMQ_X_Q5_1_AMPERE;
  9625. mmq_y = MMQ_Y_Q5_1_AMPERE;
  9626. nwarps = NWARPS_Q5_1_AMPERE;
  9627. } else if (compute_capability >= VER_4VEC) {
  9628. mmq_x = MMQ_X_Q5_1_PASCAL;
  9629. mmq_y = MMQ_Y_Q5_1_PASCAL;
  9630. nwarps = NWARPS_Q5_1_PASCAL;
  9631. } else {
  9632. GGML_ASSERT(false);
  9633. }
  9634. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9635. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9636. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9637. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9638. if (nrows_x % mmq_y == 0) {
  9639. const bool need_check = false;
  9640. /*
  9641. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  9642. the limit. To get the device limit, query
  9643. info::device::max_work_group_size. Adjust the work-group size if needed.
  9644. */
  9645. {
  9646. dpct::has_capability_or_fail(stream->get_device(),
  9647. {sycl::aspect::fp16});
  9648. stream->submit([&](sycl::handler &cgh) {
  9649. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  9650. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9651. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  9652. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  9653. cgh);
  9654. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9655. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9656. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9657. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9658. cgh.parallel_for(
  9659. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9660. [=](sycl::nd_item<3> item_ct1) {
  9661. mul_mat_q5_1<need_check>(
  9662. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9663. nrows_dst, item_ct1,
  9664. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  9665. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  9666. tile_y_qs_acc_ct1.get_pointer(),
  9667. tile_y_ds_acc_ct1.get_pointer());
  9668. });
  9669. });
  9670. }
  9671. } else {
  9672. const bool need_check = true;
  9673. /*
  9674. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  9675. the limit. To get the device limit, query
  9676. info::device::max_work_group_size. Adjust the work-group size if needed.
  9677. */
  9678. {
  9679. dpct::has_capability_or_fail(stream->get_device(),
  9680. {sycl::aspect::fp16});
  9681. stream->submit([&](sycl::handler &cgh) {
  9682. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  9683. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9684. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  9685. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  9686. cgh);
  9687. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9688. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9689. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9690. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9691. cgh.parallel_for(
  9692. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9693. [=](sycl::nd_item<3> item_ct1) {
  9694. mul_mat_q5_1<need_check>(
  9695. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9696. nrows_dst, item_ct1,
  9697. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  9698. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  9699. tile_y_qs_acc_ct1.get_pointer(),
  9700. tile_y_ds_acc_ct1.get_pointer());
  9701. });
  9702. });
  9703. }
  9704. }
  9705. }
  9706. catch (sycl::exception const &exc) {
  9707. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9708. << ", line:" << __LINE__ << std::endl;
  9709. std::exit(1);
  9710. }
  9711. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  9712. float *dst, const int ncols_x,
  9713. const int nrows_x, const int ncols_y,
  9714. const int nrows_y, const int nrows_dst,
  9715. dpct::queue_ptr stream) try {
  9716. int id;
  9717. SYCL_CHECK(
  9718. CHECK_TRY_ERROR(id = get_current_device_id()));
  9719. const int compute_capability = g_device_caps[id].cc;
  9720. int mmq_x, mmq_y, nwarps;
  9721. if (compute_capability >= VER_GEN13) {
  9722. mmq_x = MMQ_X_Q8_0_RDNA2;
  9723. mmq_y = MMQ_Y_Q8_0_RDNA2;
  9724. nwarps = NWARPS_Q8_0_RDNA2;
  9725. } else if (compute_capability >= VER_GEN12) {
  9726. mmq_x = MMQ_X_Q8_0_RDNA1;
  9727. mmq_y = MMQ_Y_Q8_0_RDNA1;
  9728. nwarps = NWARPS_Q8_0_RDNA1;
  9729. } else if (compute_capability >= VER_GEN9) {
  9730. mmq_x = MMQ_X_Q8_0_AMPERE;
  9731. mmq_y = MMQ_Y_Q8_0_AMPERE;
  9732. nwarps = NWARPS_Q8_0_AMPERE;
  9733. } else if (compute_capability >= VER_4VEC) {
  9734. mmq_x = MMQ_X_Q8_0_PASCAL;
  9735. mmq_y = MMQ_Y_Q8_0_PASCAL;
  9736. nwarps = NWARPS_Q8_0_PASCAL;
  9737. } else {
  9738. GGML_ASSERT(false);
  9739. }
  9740. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9741. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9742. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9743. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9744. if (nrows_x % mmq_y == 0) {
  9745. const bool need_check = false;
  9746. /*
  9747. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  9748. the limit. To get the device limit, query
  9749. info::device::max_work_group_size. Adjust the work-group size if needed.
  9750. */
  9751. {
  9752. dpct::has_capability_or_fail(stream->get_device(),
  9753. {sycl::aspect::fp16});
  9754. stream->submit([&](sycl::handler &cgh) {
  9755. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  9756. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9757. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  9758. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  9759. cgh);
  9760. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9761. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9762. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9763. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9764. cgh.parallel_for(
  9765. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9766. [=](sycl::nd_item<3> item_ct1) {
  9767. mul_mat_q8_0<need_check>(
  9768. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9769. nrows_dst, item_ct1,
  9770. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  9771. tile_x_d_q8_0_acc_ct1.get_pointer(),
  9772. tile_y_qs_acc_ct1.get_pointer(),
  9773. tile_y_ds_acc_ct1.get_pointer());
  9774. });
  9775. });
  9776. }
  9777. } else {
  9778. const bool need_check = true;
  9779. /*
  9780. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  9781. the limit. To get the device limit, query
  9782. info::device::max_work_group_size. Adjust the work-group size if needed.
  9783. */
  9784. {
  9785. dpct::has_capability_or_fail(stream->get_device(),
  9786. {sycl::aspect::fp16});
  9787. stream->submit([&](sycl::handler &cgh) {
  9788. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  9789. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9790. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  9791. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  9792. cgh);
  9793. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9794. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9795. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9796. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9797. cgh.parallel_for(
  9798. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9799. [=](sycl::nd_item<3> item_ct1) {
  9800. mul_mat_q8_0<need_check>(
  9801. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9802. nrows_dst, item_ct1,
  9803. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  9804. tile_x_d_q8_0_acc_ct1.get_pointer(),
  9805. tile_y_qs_acc_ct1.get_pointer(),
  9806. tile_y_ds_acc_ct1.get_pointer());
  9807. });
  9808. });
  9809. }
  9810. }
  9811. }
  9812. catch (sycl::exception const &exc) {
  9813. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9814. << ", line:" << __LINE__ << std::endl;
  9815. std::exit(1);
  9816. }
  9817. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  9818. float *dst, const int ncols_x,
  9819. const int nrows_x, const int ncols_y,
  9820. const int nrows_y, const int nrows_dst,
  9821. dpct::queue_ptr stream) try {
  9822. int id;
  9823. SYCL_CHECK(
  9824. CHECK_TRY_ERROR(id = get_current_device_id()));
  9825. const int compute_capability = g_device_caps[id].cc;
  9826. int mmq_x, mmq_y, nwarps;
  9827. if (compute_capability >= VER_GEN13) {
  9828. mmq_x = MMQ_X_Q2_K_RDNA2;
  9829. mmq_y = MMQ_Y_Q2_K_RDNA2;
  9830. nwarps = NWARPS_Q2_K_RDNA2;
  9831. } else if (compute_capability >= VER_GEN12) {
  9832. mmq_x = MMQ_X_Q2_K_RDNA1;
  9833. mmq_y = MMQ_Y_Q2_K_RDNA1;
  9834. nwarps = NWARPS_Q2_K_RDNA1;
  9835. } else if (compute_capability >= VER_GEN9) {
  9836. mmq_x = MMQ_X_Q2_K_AMPERE;
  9837. mmq_y = MMQ_Y_Q2_K_AMPERE;
  9838. nwarps = NWARPS_Q2_K_AMPERE;
  9839. } else if (compute_capability >= VER_4VEC) {
  9840. mmq_x = MMQ_X_Q2_K_PASCAL;
  9841. mmq_y = MMQ_Y_Q2_K_PASCAL;
  9842. nwarps = NWARPS_Q2_K_PASCAL;
  9843. } else {
  9844. GGML_ASSERT(false);
  9845. }
  9846. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9847. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9848. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9849. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9850. if (nrows_x % mmq_y == 0) {
  9851. const bool need_check = false;
  9852. /*
  9853. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  9854. the limit. To get the device limit, query
  9855. info::device::max_work_group_size. Adjust the work-group size if needed.
  9856. */
  9857. {
  9858. dpct::has_capability_or_fail(stream->get_device(),
  9859. {sycl::aspect::fp16});
  9860. stream->submit([&](sycl::handler &cgh) {
  9861. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  9862. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9863. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  9864. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  9865. cgh);
  9866. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  9867. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9868. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9869. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9870. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9871. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9872. cgh.parallel_for(
  9873. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9874. [=](sycl::nd_item<3> item_ct1) {
  9875. mul_mat_q2_K<need_check>(
  9876. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9877. nrows_dst, item_ct1,
  9878. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  9879. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  9880. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  9881. tile_y_qs_acc_ct1.get_pointer(),
  9882. tile_y_ds_acc_ct1.get_pointer());
  9883. });
  9884. });
  9885. }
  9886. } else {
  9887. const bool need_check = true;
  9888. /*
  9889. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  9890. the limit. To get the device limit, query
  9891. info::device::max_work_group_size. Adjust the work-group size if needed.
  9892. */
  9893. {
  9894. dpct::has_capability_or_fail(stream->get_device(),
  9895. {sycl::aspect::fp16});
  9896. stream->submit([&](sycl::handler &cgh) {
  9897. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  9898. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9899. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  9900. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  9901. cgh);
  9902. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  9903. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9904. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9905. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9906. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9907. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9908. cgh.parallel_for(
  9909. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9910. [=](sycl::nd_item<3> item_ct1) {
  9911. mul_mat_q2_K<need_check>(
  9912. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9913. nrows_dst, item_ct1,
  9914. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  9915. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  9916. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  9917. tile_y_qs_acc_ct1.get_pointer(),
  9918. tile_y_ds_acc_ct1.get_pointer());
  9919. });
  9920. });
  9921. }
  9922. }
  9923. }
  9924. catch (sycl::exception const &exc) {
  9925. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9926. << ", line:" << __LINE__ << std::endl;
  9927. std::exit(1);
  9928. }
  9929. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  9930. float *dst, const int ncols_x,
  9931. const int nrows_x, const int ncols_y,
  9932. const int nrows_y, const int nrows_dst,
  9933. dpct::queue_ptr stream) try {
  9934. int id;
  9935. SYCL_CHECK(
  9936. CHECK_TRY_ERROR(id = get_current_device_id()));
  9937. const int compute_capability = g_device_caps[id].cc;
  9938. int mmq_x, mmq_y, nwarps;
  9939. if (compute_capability >= VER_GEN13) {
  9940. mmq_x = MMQ_X_Q3_K_RDNA2;
  9941. mmq_y = MMQ_Y_Q3_K_RDNA2;
  9942. nwarps = NWARPS_Q3_K_RDNA2;
  9943. } else if (compute_capability >= VER_GEN12) {
  9944. mmq_x = MMQ_X_Q3_K_RDNA1;
  9945. mmq_y = MMQ_Y_Q3_K_RDNA1;
  9946. nwarps = NWARPS_Q3_K_RDNA1;
  9947. } else if (compute_capability >= VER_GEN9) {
  9948. mmq_x = MMQ_X_Q3_K_AMPERE;
  9949. mmq_y = MMQ_Y_Q3_K_AMPERE;
  9950. nwarps = NWARPS_Q3_K_AMPERE;
  9951. } else if (compute_capability >= VER_4VEC) {
  9952. mmq_x = MMQ_X_Q3_K_PASCAL;
  9953. mmq_y = MMQ_Y_Q3_K_PASCAL;
  9954. nwarps = NWARPS_Q3_K_PASCAL;
  9955. } else {
  9956. GGML_ASSERT(false);
  9957. }
  9958. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9959. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9960. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9961. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9962. if (nrows_x % mmq_y == 0) {
  9963. const bool need_check = false;
  9964. /*
  9965. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  9966. the limit. To get the device limit, query
  9967. info::device::max_work_group_size. Adjust the work-group size if needed.
  9968. */
  9969. {
  9970. dpct::has_capability_or_fail(stream->get_device(),
  9971. {sycl::aspect::fp16});
  9972. stream->submit([&](sycl::handler &cgh) {
  9973. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  9974. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9975. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  9976. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  9977. cgh);
  9978. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  9979. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  9980. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  9981. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9982. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9983. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9984. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9985. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9986. cgh.parallel_for(
  9987. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9988. [=](sycl::nd_item<3> item_ct1) {
  9989. mul_mat_q3_K<need_check>(
  9990. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9991. nrows_dst, item_ct1,
  9992. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  9993. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  9994. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  9995. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  9996. tile_y_qs_acc_ct1.get_pointer(),
  9997. tile_y_ds_acc_ct1.get_pointer());
  9998. });
  9999. });
  10000. }
  10001. } else {
  10002. const bool need_check = true;
  10003. /*
  10004. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  10005. the limit. To get the device limit, query
  10006. info::device::max_work_group_size. Adjust the work-group size if needed.
  10007. */
  10008. {
  10009. dpct::has_capability_or_fail(stream->get_device(),
  10010. {sycl::aspect::fp16});
  10011. stream->submit([&](sycl::handler &cgh) {
  10012. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  10013. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10014. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  10015. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  10016. cgh);
  10017. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  10018. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  10019. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  10020. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10021. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10022. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10023. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10024. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10025. cgh.parallel_for(
  10026. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10027. [=](sycl::nd_item<3> item_ct1) {
  10028. mul_mat_q3_K<need_check>(
  10029. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10030. nrows_dst, item_ct1,
  10031. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  10032. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  10033. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  10034. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  10035. tile_y_qs_acc_ct1.get_pointer(),
  10036. tile_y_ds_acc_ct1.get_pointer());
  10037. });
  10038. });
  10039. }
  10040. }
  10041. }
  10042. catch (sycl::exception const &exc) {
  10043. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10044. << ", line:" << __LINE__ << std::endl;
  10045. std::exit(1);
  10046. }
  10047. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  10048. float *dst, const int ncols_x,
  10049. const int nrows_x, const int ncols_y,
  10050. const int nrows_y, const int nrows_dst,
  10051. dpct::queue_ptr stream) try {
  10052. int id;
  10053. SYCL_CHECK(
  10054. CHECK_TRY_ERROR(id = get_current_device_id()));
  10055. const int compute_capability = g_device_caps[id].cc;
  10056. int mmq_x, mmq_y, nwarps;
  10057. if (compute_capability >= VER_GEN13) {
  10058. mmq_x = MMQ_X_Q4_K_RDNA2;
  10059. mmq_y = MMQ_Y_Q4_K_RDNA2;
  10060. nwarps = NWARPS_Q4_K_RDNA2;
  10061. } else if (compute_capability >= VER_GEN12) {
  10062. mmq_x = MMQ_X_Q4_K_RDNA1;
  10063. mmq_y = MMQ_Y_Q4_K_RDNA1;
  10064. nwarps = NWARPS_Q4_K_RDNA1;
  10065. } else if (compute_capability >= VER_GEN9) {
  10066. mmq_x = MMQ_X_Q4_K_AMPERE;
  10067. mmq_y = MMQ_Y_Q4_K_AMPERE;
  10068. nwarps = NWARPS_Q4_K_AMPERE;
  10069. } else if (compute_capability >= VER_4VEC) {
  10070. mmq_x = MMQ_X_Q4_K_PASCAL;
  10071. mmq_y = MMQ_Y_Q4_K_PASCAL;
  10072. nwarps = NWARPS_Q4_K_PASCAL;
  10073. } else {
  10074. GGML_ASSERT(false);
  10075. }
  10076. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10077. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10078. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10079. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10080. if (nrows_x % mmq_y == 0) {
  10081. const bool need_check = false;
  10082. /*
  10083. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  10084. the limit. To get the device limit, query
  10085. info::device::max_work_group_size. Adjust the work-group size if needed.
  10086. */
  10087. {
  10088. dpct::has_capability_or_fail(stream->get_device(),
  10089. {sycl::aspect::fp16});
  10090. stream->submit([&](sycl::handler &cgh) {
  10091. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10092. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10093. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10094. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10095. cgh);
  10096. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10097. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10098. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10099. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10100. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10101. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10102. cgh.parallel_for(
  10103. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10104. [=](sycl::nd_item<3> item_ct1) {
  10105. mul_mat_q4_K<need_check>(
  10106. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10107. nrows_dst, item_ct1,
  10108. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10109. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10110. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10111. tile_y_qs_acc_ct1.get_pointer(),
  10112. tile_y_ds_acc_ct1.get_pointer());
  10113. });
  10114. });
  10115. }
  10116. } else {
  10117. const bool need_check = true;
  10118. /*
  10119. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  10120. the limit. To get the device limit, query
  10121. info::device::max_work_group_size. Adjust the work-group size if needed.
  10122. */
  10123. {
  10124. dpct::has_capability_or_fail(stream->get_device(),
  10125. {sycl::aspect::fp16});
  10126. stream->submit([&](sycl::handler &cgh) {
  10127. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10128. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10129. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10130. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10131. cgh);
  10132. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10133. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10134. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10135. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10136. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10137. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10138. cgh.parallel_for(
  10139. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10140. [=](sycl::nd_item<3> item_ct1) {
  10141. mul_mat_q4_K<need_check>(
  10142. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10143. nrows_dst, item_ct1,
  10144. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10145. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10146. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10147. tile_y_qs_acc_ct1.get_pointer(),
  10148. tile_y_ds_acc_ct1.get_pointer());
  10149. });
  10150. });
  10151. }
  10152. }
  10153. }
  10154. catch (sycl::exception const &exc) {
  10155. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10156. << ", line:" << __LINE__ << std::endl;
  10157. std::exit(1);
  10158. }
  10159. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  10160. float *dst, const int ncols_x,
  10161. const int nrows_x, const int ncols_y,
  10162. const int nrows_y, const int nrows_dst,
  10163. dpct::queue_ptr stream) try {
  10164. int id;
  10165. SYCL_CHECK(
  10166. CHECK_TRY_ERROR(id = get_current_device_id()));
  10167. const int compute_capability = g_device_caps[id].cc;
  10168. int mmq_x, mmq_y, nwarps;
  10169. if (compute_capability >= VER_GEN13) {
  10170. mmq_x = MMQ_X_Q5_K_RDNA2;
  10171. mmq_y = MMQ_Y_Q5_K_RDNA2;
  10172. nwarps = NWARPS_Q5_K_RDNA2;
  10173. } else if (compute_capability >= VER_GEN12) {
  10174. mmq_x = MMQ_X_Q5_K_RDNA1;
  10175. mmq_y = MMQ_Y_Q5_K_RDNA1;
  10176. nwarps = NWARPS_Q5_K_RDNA1;
  10177. } else if (compute_capability >= VER_GEN9) {
  10178. mmq_x = MMQ_X_Q5_K_AMPERE;
  10179. mmq_y = MMQ_Y_Q5_K_AMPERE;
  10180. nwarps = NWARPS_Q5_K_AMPERE;
  10181. } else if (compute_capability >= VER_4VEC) {
  10182. mmq_x = MMQ_X_Q5_K_PASCAL;
  10183. mmq_y = MMQ_Y_Q5_K_PASCAL;
  10184. nwarps = NWARPS_Q5_K_PASCAL;
  10185. } else {
  10186. GGML_ASSERT(false);
  10187. }
  10188. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10189. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10190. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10191. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10192. if (nrows_x % mmq_y == 0) {
  10193. const bool need_check = false;
  10194. /*
  10195. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  10196. the limit. To get the device limit, query
  10197. info::device::max_work_group_size. Adjust the work-group size if needed.
  10198. */
  10199. {
  10200. dpct::has_capability_or_fail(stream->get_device(),
  10201. {sycl::aspect::fp16});
  10202. stream->submit([&](sycl::handler &cgh) {
  10203. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10204. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10205. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10206. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10207. cgh);
  10208. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10209. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10210. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10211. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10212. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10213. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10214. cgh.parallel_for(
  10215. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10216. [=](sycl::nd_item<3> item_ct1) {
  10217. mul_mat_q5_K<need_check>(
  10218. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10219. nrows_dst, item_ct1,
  10220. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10221. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10222. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10223. tile_y_qs_acc_ct1.get_pointer(),
  10224. tile_y_ds_acc_ct1.get_pointer());
  10225. });
  10226. });
  10227. }
  10228. } else {
  10229. const bool need_check = true;
  10230. /*
  10231. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  10232. the limit. To get the device limit, query
  10233. info::device::max_work_group_size. Adjust the work-group size if needed.
  10234. */
  10235. {
  10236. dpct::has_capability_or_fail(stream->get_device(),
  10237. {sycl::aspect::fp16});
  10238. stream->submit([&](sycl::handler &cgh) {
  10239. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10240. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10241. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10242. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10243. cgh);
  10244. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10245. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10246. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10247. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10248. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10249. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10250. cgh.parallel_for(
  10251. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10252. [=](sycl::nd_item<3> item_ct1) {
  10253. mul_mat_q5_K<need_check>(
  10254. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10255. nrows_dst, item_ct1,
  10256. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10257. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10258. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10259. tile_y_qs_acc_ct1.get_pointer(),
  10260. tile_y_ds_acc_ct1.get_pointer());
  10261. });
  10262. });
  10263. }
  10264. }
  10265. }
  10266. catch (sycl::exception const &exc) {
  10267. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10268. << ", line:" << __LINE__ << std::endl;
  10269. std::exit(1);
  10270. }
  10271. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  10272. float *dst, const int ncols_x,
  10273. const int nrows_x, const int ncols_y,
  10274. const int nrows_y, const int nrows_dst,
  10275. dpct::queue_ptr stream) try {
  10276. int id;
  10277. SYCL_CHECK(
  10278. CHECK_TRY_ERROR(id = get_current_device_id()));
  10279. const int compute_capability = g_device_caps[id].cc;
  10280. int mmq_x, mmq_y, nwarps;
  10281. if (compute_capability >= VER_GEN13) {
  10282. mmq_x = MMQ_X_Q6_K_RDNA2;
  10283. mmq_y = MMQ_Y_Q6_K_RDNA2;
  10284. nwarps = NWARPS_Q6_K_RDNA2;
  10285. } else if (compute_capability >= VER_GEN12) {
  10286. mmq_x = MMQ_X_Q6_K_RDNA1;
  10287. mmq_y = MMQ_Y_Q6_K_RDNA1;
  10288. nwarps = NWARPS_Q6_K_RDNA1;
  10289. } else if (compute_capability >= VER_GEN9) {
  10290. mmq_x = MMQ_X_Q6_K_AMPERE;
  10291. mmq_y = MMQ_Y_Q6_K_AMPERE;
  10292. nwarps = NWARPS_Q6_K_AMPERE;
  10293. } else if (compute_capability >= VER_4VEC) {
  10294. mmq_x = MMQ_X_Q6_K_PASCAL;
  10295. mmq_y = MMQ_Y_Q6_K_PASCAL;
  10296. nwarps = NWARPS_Q6_K_PASCAL;
  10297. } else {
  10298. GGML_ASSERT(false);
  10299. }
  10300. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10301. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10302. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10303. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10304. if (nrows_x % mmq_y == 0) {
  10305. const bool need_check = false;
  10306. /*
  10307. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  10308. the limit. To get the device limit, query
  10309. info::device::max_work_group_size. Adjust the work-group size if needed.
  10310. */
  10311. {
  10312. dpct::has_capability_or_fail(stream->get_device(),
  10313. {sycl::aspect::fp16});
  10314. stream->submit([&](sycl::handler &cgh) {
  10315. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10316. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10317. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10318. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10319. cgh);
  10320. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10321. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10322. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10323. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10324. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10325. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10326. cgh.parallel_for(
  10327. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10328. [=](sycl::nd_item<3> item_ct1) {
  10329. mul_mat_q6_K<need_check>(
  10330. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10331. nrows_dst, item_ct1,
  10332. tile_x_ql_acc_ct1.get_pointer(),
  10333. tile_x_dm_acc_ct1.get_pointer(),
  10334. tile_x_sc_acc_ct1.get_pointer(),
  10335. tile_y_qs_acc_ct1.get_pointer(),
  10336. tile_y_ds_acc_ct1.get_pointer());
  10337. });
  10338. });
  10339. }
  10340. } else {
  10341. const bool need_check = true;
  10342. /*
  10343. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  10344. the limit. To get the device limit, query
  10345. info::device::max_work_group_size. Adjust the work-group size if needed.
  10346. */
  10347. {
  10348. dpct::has_capability_or_fail(stream->get_device(),
  10349. {sycl::aspect::fp16});
  10350. stream->submit([&](sycl::handler &cgh) {
  10351. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10352. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10353. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10354. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10355. cgh);
  10356. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10357. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10358. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10359. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10360. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10361. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10362. cgh.parallel_for(
  10363. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10364. [=](sycl::nd_item<3> item_ct1) {
  10365. mul_mat_q6_K<need_check>(
  10366. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10367. nrows_dst, item_ct1,
  10368. tile_x_ql_acc_ct1.get_pointer(),
  10369. tile_x_dm_acc_ct1.get_pointer(),
  10370. tile_x_sc_acc_ct1.get_pointer(),
  10371. tile_y_qs_acc_ct1.get_pointer(),
  10372. tile_y_ds_acc_ct1.get_pointer());
  10373. });
  10374. });
  10375. }
  10376. }
  10377. }
  10378. catch (sycl::exception const &exc) {
  10379. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10380. << ", line:" << __LINE__ << std::endl;
  10381. std::exit(1);
  10382. }
  10383. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  10384. float *dst, const int ncols_x,
  10385. const int nrows_x,
  10386. const int nchannels_x,
  10387. const int nchannels_y,
  10388. dpct::queue_ptr stream) {
  10389. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10390. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10391. {
  10392. dpct::has_capability_or_fail(stream->get_device(),
  10393. {sycl::aspect::fp16});
  10394. stream->parallel_for(
  10395. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10396. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10397. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  10398. nchannels_y, item_ct1);
  10399. });
  10400. }
  10401. }
  10402. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  10403. const void *vx, const float *y, float *dst, const int ncols_x,
  10404. const int nrows_x, const int row_stride_x, const int nchannels_x,
  10405. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  10406. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10407. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10408. {
  10409. dpct::has_capability_or_fail(stream->get_device(),
  10410. {sycl::aspect::fp16});
  10411. stream->parallel_for(
  10412. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10413. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10414. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  10415. row_stride_x, channel_stride_x,
  10416. nchannels_y / nchannels_x, item_ct1);
  10417. });
  10418. }
  10419. }
  10420. static void
  10421. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  10422. const int ne01, const int ne02, const int nb00,
  10423. const int nb01, const int nb02, const int nb03,
  10424. const int ne10, const int ne11, const int ne12,
  10425. const int nb10, const int nb11, const int nb12,
  10426. const int nb13, dpct::queue_ptr stream) {
  10427. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10428. {
  10429. dpct::has_capability_or_fail(stream->get_device(),
  10430. {sycl::aspect::fp16});
  10431. stream->parallel_for(
  10432. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10433. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10434. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10435. [=](sycl::nd_item<3> item_ct1) {
  10436. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  10437. nb01, nb02, nb03, ne10, ne11, ne12,
  10438. nb10, nb11, nb12, nb13, item_ct1);
  10439. });
  10440. }
  10441. }
  10442. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  10443. const int ne00, const int ne01,
  10444. const int ne02, const int nb00,
  10445. const int nb01, const int nb02,
  10446. const int nb03, const int ne10,
  10447. const int ne11, const int ne12,
  10448. const int nb10, const int nb11,
  10449. const int nb12, const int nb13,
  10450. dpct::queue_ptr stream) {
  10451. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10452. {
  10453. dpct::has_capability_or_fail(stream->get_device(),
  10454. {sycl::aspect::fp16});
  10455. stream->parallel_for(
  10456. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10457. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10458. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10459. [=](sycl::nd_item<3> item_ct1) {
  10460. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10461. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10462. item_ct1);
  10463. });
  10464. }
  10465. }
  10466. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  10467. const int ne00, const int ne01,
  10468. const int ne02, const int nb00,
  10469. const int nb01, const int nb02,
  10470. const int nb03, const int ne10,
  10471. const int ne11, const int ne12,
  10472. const int nb10, const int nb11,
  10473. const int nb12, const int nb13,
  10474. dpct::queue_ptr stream) {
  10475. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10476. {
  10477. dpct::has_capability_or_fail(stream->get_device(),
  10478. {sycl::aspect::fp16});
  10479. stream->parallel_for(
  10480. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10481. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10482. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10483. [=](sycl::nd_item<3> item_ct1) {
  10484. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10485. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10486. item_ct1);
  10487. });
  10488. }
  10489. }
  10490. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  10491. const int ne00, const int ne01,
  10492. const int ne02, const int nb00,
  10493. const int nb01, const int nb02,
  10494. const int nb03, const int ne10,
  10495. const int ne11, const int ne12,
  10496. const int nb10, const int nb11,
  10497. const int nb12, const int nb13,
  10498. dpct::queue_ptr stream) {
  10499. GGML_ASSERT(ne % QK8_0 == 0);
  10500. const int num_blocks = ne / QK8_0;
  10501. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10502. sycl::range<3>(1, 1, 1)),
  10503. [=](sycl::nd_item<3> item_ct1) {
  10504. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  10505. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10506. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10507. item_ct1);
  10508. });
  10509. }
  10510. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  10511. const int ne00, const int ne01,
  10512. const int ne02, const int nb00,
  10513. const int nb01, const int nb02,
  10514. const int nb03, const int ne10,
  10515. const int ne11, const int ne12,
  10516. const int nb10, const int nb11,
  10517. const int nb12, const int nb13,
  10518. dpct::queue_ptr stream) {
  10519. GGML_ASSERT(ne % QK4_0 == 0);
  10520. const int num_blocks = ne / QK4_0;
  10521. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10522. sycl::range<3>(1, 1, 1)),
  10523. [=](sycl::nd_item<3> item_ct1) {
  10524. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  10525. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10526. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10527. item_ct1);
  10528. });
  10529. }
  10530. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  10531. const int ne00, const int ne01,
  10532. const int ne02, const int nb00,
  10533. const int nb01, const int nb02,
  10534. const int nb03, const int ne10,
  10535. const int ne11, const int ne12,
  10536. const int nb10, const int nb11,
  10537. const int nb12, const int nb13,
  10538. dpct::queue_ptr stream) {
  10539. GGML_ASSERT(ne % QK4_1 == 0);
  10540. const int num_blocks = ne / QK4_1;
  10541. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10542. sycl::range<3>(1, 1, 1)),
  10543. [=](sycl::nd_item<3> item_ct1) {
  10544. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  10545. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10546. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10547. item_ct1);
  10548. });
  10549. }
  10550. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  10551. const int ne00, const int ne01,
  10552. const int ne02, const int nb00,
  10553. const int nb01, const int nb02,
  10554. const int nb03, const int ne10,
  10555. const int ne11, const int ne12,
  10556. const int nb10, const int nb11,
  10557. const int nb12, const int nb13,
  10558. dpct::queue_ptr stream) {
  10559. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10560. {
  10561. dpct::has_capability_or_fail(stream->get_device(),
  10562. {sycl::aspect::fp16});
  10563. stream->parallel_for(
  10564. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10565. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10566. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10567. [=](sycl::nd_item<3> item_ct1) {
  10568. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10569. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10570. item_ct1);
  10571. });
  10572. }
  10573. }
  10574. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  10575. const int ne00, const int ne01,
  10576. const int ne02, const int nb00,
  10577. const int nb01, const int nb02,
  10578. const int nb03, const int ne10,
  10579. const int ne11, const int ne12,
  10580. const int nb10, const int nb11,
  10581. const int nb12, const int nb13,
  10582. dpct::queue_ptr stream) {
  10583. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10584. {
  10585. // dpct::has_capability_or_fail(stream->get_device(),
  10586. // {sycl::aspect::fp16});
  10587. stream->parallel_for(
  10588. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10589. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10590. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10591. [=](sycl::nd_item<3> item_ct1) {
  10592. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10593. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10594. item_ct1);
  10595. });
  10596. }
  10597. }
  10598. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  10599. const int ne00, const int ne01,
  10600. const int ne02, const int nb00,
  10601. const int nb01, const int nb02,
  10602. const int nb03, const int ne10,
  10603. const int ne11, const int ne12,
  10604. const int nb10, const int nb11,
  10605. const int nb12, const int nb13,
  10606. dpct::queue_ptr stream) {
  10607. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10608. {
  10609. // dpct::has_capability_or_fail(stream->get_device(),
  10610. // {sycl::aspect::fp16});
  10611. stream->parallel_for(
  10612. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10613. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10614. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10615. [=](sycl::nd_item<3> item_ct1) {
  10616. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10617. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10618. item_ct1);
  10619. });
  10620. }
  10621. }
  10622. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  10623. const int k, dpct::queue_ptr stream) {
  10624. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  10625. stream->parallel_for(
  10626. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10627. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  10628. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  10629. [=](sycl::nd_item<3> item_ct1) {
  10630. scale_f32(x, dst, scale, k, item_ct1);
  10631. });
  10632. }
  10633. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  10634. const float max, const int k,
  10635. dpct::queue_ptr stream) {
  10636. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  10637. stream->parallel_for(
  10638. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10639. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  10640. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  10641. [=](sycl::nd_item<3> item_ct1) {
  10642. clamp_f32(x, dst, min, max, k, item_ct1);
  10643. });
  10644. }
  10645. template <typename T>
  10646. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  10647. const int32_t *pos, float freq_scale, int p_delta_rows,
  10648. float freq_base, float ext_factor, float attn_factor,
  10649. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  10650. GGML_ASSERT(ncols % 2 == 0);
  10651. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  10652. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  10653. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  10654. if (pos == nullptr) {
  10655. /*
  10656. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  10657. the limit. To get the device limit, query
  10658. info::device::max_work_group_size. Adjust the work-group size if needed.
  10659. */
  10660. dpct::has_capability_or_fail(stream->get_device(),
  10661. {sycl::aspect::fp16});
  10662. stream->parallel_for(
  10663. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10664. [=](sycl::nd_item<3> item_ct1) {
  10665. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  10666. freq_base, ext_factor, attn_factor, corr_dims,
  10667. item_ct1);
  10668. });
  10669. } else {
  10670. /*
  10671. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  10672. the limit. To get the device limit, query
  10673. info::device::max_work_group_size. Adjust the work-group size if needed.
  10674. */
  10675. dpct::has_capability_or_fail(stream->get_device(),
  10676. {sycl::aspect::fp16});
  10677. stream->parallel_for(
  10678. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10679. [=](sycl::nd_item<3> item_ct1) {
  10680. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  10681. freq_base, ext_factor, attn_factor, corr_dims,
  10682. item_ct1);
  10683. });
  10684. }
  10685. }
  10686. template <typename T>
  10687. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  10688. const int32_t *pos, float freq_scale,
  10689. int p_delta_rows, float freq_base, float ext_factor,
  10690. float attn_factor, rope_corr_dims corr_dims,
  10691. const float * freq_factors, dpct::queue_ptr stream) {
  10692. GGML_ASSERT(ncols % 2 == 0);
  10693. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  10694. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  10695. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  10696. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  10697. const float inv_ndims = -1.0f / n_dims;
  10698. if (pos == nullptr) {
  10699. dpct::has_capability_or_fail(stream->get_device(),
  10700. {sycl::aspect::fp16});
  10701. if (freq_factors == nullptr) {
  10702. stream->parallel_for(
  10703. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10704. [=](sycl::nd_item<3> item_ct1) {
  10705. rope_neox<T, false, false>(x, dst, ncols, n_dims, pos, freq_scale,
  10706. p_delta_rows, ext_factor, attn_factor,
  10707. corr_dims, theta_scale, inv_ndims, freq_factors,
  10708. item_ct1);
  10709. });
  10710. } else {
  10711. stream->parallel_for(
  10712. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10713. [=](sycl::nd_item<3> item_ct1) {
  10714. rope_neox<T, false, true>(x, dst, ncols, n_dims, pos, freq_scale,
  10715. p_delta_rows, ext_factor, attn_factor,
  10716. corr_dims, theta_scale, inv_ndims, freq_factors,
  10717. item_ct1);
  10718. });
  10719. }
  10720. } else {
  10721. dpct::has_capability_or_fail(stream->get_device(),
  10722. {sycl::aspect::fp16});
  10723. if (freq_factors == nullptr) {
  10724. stream->parallel_for(
  10725. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10726. [=](sycl::nd_item<3> item_ct1) {
  10727. rope_neox<T, true, false>(x, dst, ncols, n_dims, pos, freq_scale,
  10728. p_delta_rows, ext_factor, attn_factor,
  10729. corr_dims, theta_scale, inv_ndims, freq_factors, item_ct1);
  10730. });
  10731. } else {
  10732. stream->parallel_for(
  10733. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10734. [=](sycl::nd_item<3> item_ct1) {
  10735. rope_neox<T, true, true>(x, dst, ncols, n_dims, pos, freq_scale,
  10736. p_delta_rows, ext_factor, attn_factor,
  10737. corr_dims, theta_scale, inv_ndims, freq_factors, item_ct1);
  10738. });
  10739. }
  10740. }
  10741. }
  10742. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  10743. const int nrows, dpct::queue_ptr stream) {
  10744. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10745. const sycl::range<3> block_nums(1, nrows, 1);
  10746. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10747. [=](sycl::nd_item<3> item_ct1)
  10748. [[intel::reqd_sub_group_size(32)]] {
  10749. k_sum_rows_f32(x, dst, ncols, item_ct1);
  10750. });
  10751. }
  10752. static int next_power_of_2(int x) {
  10753. int n = 1;
  10754. while (n < x) {
  10755. n *= 2;
  10756. }
  10757. return n;
  10758. }
  10759. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  10760. const int nrows, ggml_sort_order order,
  10761. dpct::queue_ptr stream) {
  10762. // bitonic sort requires ncols to be power of 2
  10763. const int ncols_pad = next_power_of_2(ncols);
  10764. const sycl::range<3> block_dims(1, 1, ncols_pad);
  10765. const sycl::range<3> block_nums(1, nrows, 1);
  10766. const size_t shared_mem = ncols_pad * sizeof(int);
  10767. // GGML_ASSERT(shared_mem <= ggml_cuda_info().devices[ggml_cuda_get_device()].smpb);
  10768. if (order == GGML_SORT_ORDER_ASC) {
  10769. stream->submit([&](sycl::handler &cgh) {
  10770. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  10771. sycl::range<1>(shared_mem), cgh);
  10772. cgh.parallel_for(
  10773. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10774. [=](sycl::nd_item<3> item_ct1) {
  10775. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(
  10776. x, dst, ncols, ncols_pad, item_ct1,
  10777. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  10778. .get());
  10779. });
  10780. });
  10781. } else if (order == GGML_SORT_ORDER_DESC) {
  10782. stream->submit([&](sycl::handler &cgh) {
  10783. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  10784. sycl::range<1>(shared_mem), cgh);
  10785. cgh.parallel_for(
  10786. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10787. [=](sycl::nd_item<3> item_ct1) {
  10788. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(
  10789. x, dst, ncols, ncols_pad, item_ct1,
  10790. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  10791. .get());
  10792. });
  10793. });
  10794. } else {
  10795. GGML_ASSERT(false);
  10796. }
  10797. }
  10798. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  10799. const int ncols_x, const int nrows_x,
  10800. const int rows_per_channel, const int n_past,
  10801. dpct::queue_ptr stream) {
  10802. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  10803. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  10804. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  10805. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10806. [=](sycl::nd_item<3> item_ct1) {
  10807. diag_mask_inf_f32(x, dst, ncols_x,
  10808. rows_per_channel, n_past,
  10809. item_ct1);
  10810. });
  10811. }
  10812. template <bool vals_smem, int ncols_template, int block_size_template>
  10813. static void soft_max_f32_submitter(const float * x, const float * mask, float * dst, const int ncols_par,
  10814. const int nrows_y, const float scale, const float max_bias, const float m0,
  10815. const float m1, uint32_t n_head_log2, sycl::range<3> block_nums, sycl::range<3> block_dims,
  10816. const size_t n_local_scratch, dpct::queue_ptr stream) {
  10817. stream->submit([&](sycl::handler &cgh) {
  10818. sycl::local_accessor<float, 1> local_buf_acc(n_local_scratch, cgh);
  10819. cgh.parallel_for(
  10820. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10821. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10822. soft_max_f32<vals_smem, ncols_template, block_size_template>(x, mask, dst, ncols_par,
  10823. nrows_y, scale, max_bias, m0,
  10824. m1, n_head_log2, item_ct1,
  10825. local_buf_acc.get_pointer());
  10826. });
  10827. });
  10828. }
  10829. static void soft_max_f32_sycl(const float * x, const float * mask,
  10830. float * dst, const int ncols_x, const int nrows_x,
  10831. const int nrows_y, const float scale, const float max_bias,
  10832. dpct::queue_ptr stream) {
  10833. int nth = WARP_SIZE;
  10834. int max_block_size = g_work_group_size;
  10835. while (nth < ncols_x && nth < max_block_size) nth *= 2;
  10836. if (nth>max_block_size) nth = max_block_size;
  10837. const sycl::range<3> block_dims(1, 1, nth);
  10838. const sycl::range<3> block_nums(1, 1, nrows_x);
  10839. const size_t n_local_scratch = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE);
  10840. const uint32_t n_head_kv = nrows_x/nrows_y;
  10841. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  10842. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  10843. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  10844. const size_t local_mem_size = stream->get_device().get_info<sycl::info::device::local_mem_size>();
  10845. if (n_local_scratch*sizeof(float) < local_mem_size) {
  10846. if (ncols_x > max_block_size) {
  10847. soft_max_f32_submitter<true, 0, 0>(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. return;
  10851. }
  10852. switch (ncols_x) {
  10853. case 32:
  10854. soft_max_f32_submitter<true, 32, 32>(x, mask, dst, ncols_x, nrows_y, scale,
  10855. max_bias, m0, m1, n_head_log2, block_nums,
  10856. block_dims, n_local_scratch, stream);
  10857. break;
  10858. case 64:
  10859. soft_max_f32_submitter<true, 64, 64>(x, mask, dst, ncols_x, nrows_y, scale,
  10860. max_bias, m0, m1, n_head_log2, block_nums,
  10861. block_dims, n_local_scratch, stream);
  10862. break;
  10863. case 128:
  10864. soft_max_f32_submitter<true, 128, 128>(x, mask, dst, ncols_x, nrows_y, scale,
  10865. max_bias, m0, m1, n_head_log2, block_nums,
  10866. block_dims, n_local_scratch, stream);
  10867. break;
  10868. case 256:
  10869. soft_max_f32_submitter<true, 256, 256>(x, mask, dst, ncols_x, nrows_y, scale,
  10870. max_bias, m0, m1, n_head_log2, block_nums,
  10871. block_dims, n_local_scratch, stream);
  10872. break;
  10873. case 512:
  10874. soft_max_f32_submitter<true, 512, 512>(x, mask, dst, ncols_x, nrows_y, scale,
  10875. max_bias, m0, m1, n_head_log2, block_nums,
  10876. block_dims, n_local_scratch, stream);
  10877. break;
  10878. case 1024:
  10879. soft_max_f32_submitter<true, 1024, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  10880. max_bias, m0, m1, n_head_log2, block_nums,
  10881. block_dims, n_local_scratch, stream);
  10882. break;
  10883. case 2048:
  10884. soft_max_f32_submitter<true, 2048, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  10885. max_bias, m0, m1, n_head_log2, block_nums,
  10886. block_dims, n_local_scratch, stream);
  10887. break;
  10888. case 4096:
  10889. soft_max_f32_submitter<true, 4096, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  10890. max_bias, m0, m1, n_head_log2, block_nums,
  10891. block_dims, n_local_scratch, stream);
  10892. break;
  10893. default:
  10894. soft_max_f32_submitter<true, 0, 0>(x, mask, dst, ncols_x, nrows_y, scale,
  10895. max_bias, m0, m1, n_head_log2, block_nums,
  10896. block_dims, n_local_scratch, stream);
  10897. break;
  10898. }
  10899. } else {
  10900. soft_max_f32_submitter<false, 0, 0>(x, mask, dst, ncols_x, nrows_y, scale,
  10901. max_bias, m0, m1, n_head_log2, block_nums,
  10902. block_dims, WARP_SIZE, stream);
  10903. }
  10904. }
  10905. template <typename T>
  10906. static void im2col_sycl(const float *x, T *dst, int IW, int IH,
  10907. int OW, int OH, int KW, int KH, int IC,
  10908. int offset_delta, int s0, int s1, int p0,
  10909. int p1, int d0, int d1,
  10910. dpct::queue_ptr stream) {
  10911. const int parallel_elements = OW * KW * KH;
  10912. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  10913. sycl::range<3> block_nums(IC, OH, num_blocks);
  10914. {
  10915. dpct::has_capability_or_fail(stream->get_device(),
  10916. {sycl::aspect::fp16});
  10917. stream->parallel_for(
  10918. sycl::nd_range<3>(block_nums *
  10919. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  10920. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  10921. [=](sycl::nd_item<3> item_ct1) {
  10922. im2col_kernel(x, dst, offset_delta, IW, IH, OW, KW, KH,
  10923. parallel_elements, (IC * KH * KW), s0, s1, p0,
  10924. p1, d0, d1, item_ct1);
  10925. });
  10926. }
  10927. }
  10928. // buffer pool for sycl
  10929. #define MAX_SYCL_BUFFERS 256
  10930. struct scoped_spin_lock {
  10931. std::atomic_flag& lock;
  10932. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  10933. while (lock.test_and_set(std::memory_order_acquire)) {
  10934. ; // spin
  10935. }
  10936. }
  10937. ~scoped_spin_lock() {
  10938. lock.clear(std::memory_order_release);
  10939. }
  10940. scoped_spin_lock(const scoped_spin_lock&) = delete;
  10941. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  10942. };
  10943. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  10944. // #define DEBUG_SYCL_MALLOC
  10945. struct sycl_buffer {
  10946. void * ptr = nullptr;
  10947. size_t size = 0;
  10948. };
  10949. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  10950. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  10951. static void *ggml_sycl_pool_malloc_leg(int device_index, size_t size, size_t *actual_size) try {
  10952. scoped_spin_lock lock(g_sycl_pool_lock);
  10953. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg device_index %d size=%lu\n", device_index, size);
  10954. #ifdef DEBUG_SYCL_MALLOC
  10955. int nnz = 0;
  10956. size_t max_size = 0;
  10957. #endif
  10958. size_t best_diff = 1ull << 36;
  10959. int ibest = -1;
  10960. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  10961. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  10962. if (b.ptr != nullptr) {
  10963. #ifdef DEBUG_SYCL_MALLOC
  10964. ++nnz;
  10965. if (b.size > max_size) max_size = b.size;
  10966. #endif
  10967. if (b.size >= size) {
  10968. size_t diff = b.size - size;
  10969. if (diff < best_diff) {
  10970. best_diff = diff;
  10971. ibest = i;
  10972. if (!best_diff) {
  10973. void * ptr = b.ptr;
  10974. *actual_size = b.size;
  10975. b.ptr = nullptr;
  10976. b.size = 0;
  10977. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p and rm in pool\n", ptr);
  10978. return ptr;
  10979. }
  10980. }
  10981. }
  10982. }
  10983. }
  10984. if (ibest >= 0) {
  10985. sycl_buffer& b = g_sycl_buffer_pool[device_index][ibest];
  10986. void * ptr = b.ptr;
  10987. *actual_size = b.size;
  10988. b.ptr = nullptr;
  10989. b.size = 0;
  10990. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p and rm in pool\n", ptr);
  10991. return ptr;
  10992. }
  10993. void * ptr;
  10994. size_t look_ahead_size = (size_t) (1.05 * size);
  10995. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  10996. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  10997. SYCL_CHECK(
  10998. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  10999. look_ahead_size, *stream)));
  11000. *actual_size = look_ahead_size;
  11001. g_sycl_pool_size[device_index] += look_ahead_size;
  11002. #ifdef DEBUG_SYCL_MALLOC
  11003. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  11004. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  11005. #endif
  11006. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  11007. return ptr;
  11008. }
  11009. catch (sycl::exception const &exc) {
  11010. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11011. << ", line:" << __LINE__ << std::endl;
  11012. std::exit(1);
  11013. }
  11014. static void ggml_sycl_pool_free_leg(int device_index, void *ptr, size_t size) try {
  11015. scoped_spin_lock lock(g_sycl_pool_lock);
  11016. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  11017. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  11018. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  11019. if (b.ptr == nullptr) {
  11020. b.ptr = ptr;
  11021. b.size = size;
  11022. return;
  11023. }
  11024. }
  11025. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  11026. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  11027. g_sycl_pool_size[device_index] -= size;
  11028. }
  11029. catch (sycl::exception const &exc) {
  11030. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11031. << ", line:" << __LINE__ << std::endl;
  11032. std::exit(1);
  11033. }
  11034. // pool with virtual memory
  11035. /*
  11036. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  11037. */
  11038. // static std::vector<CUmemGenericAllocationHandle>
  11039. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  11040. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  11041. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  11042. static void *ggml_sycl_pool_malloc_vmm(int device_index, size_t size, size_t *actual_size) try {
  11043. GGML_UNUSED(device_index);
  11044. GGML_UNUSED(size);
  11045. GGML_UNUSED(actual_size);
  11046. return NULL;
  11047. }
  11048. catch (sycl::exception const &exc) {
  11049. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11050. << ", line:" << __LINE__ << std::endl;
  11051. std::exit(1);
  11052. }
  11053. static void ggml_sycl_pool_free_vmm(int device_index, void *ptr, size_t size) try {
  11054. scoped_spin_lock lock(g_sycl_pool_lock);
  11055. #ifdef DEBUG_SYCL_MALLOC
  11056. printf("sycl pool[%d]: freed %llu bytes at %llx\n", device_index, (unsigned long long) size, ptr);
  11057. #endif
  11058. g_sycl_pool_used[device_index] -= size;
  11059. // all deallocations must be in reverse order of the allocations
  11060. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[device_index] + g_sycl_pool_used[device_index]));
  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_malloc(int device_index, size_t size, size_t *actual_size) try {
  11068. if (g_device_caps[device_index].vmm) {
  11069. return ggml_sycl_pool_malloc_vmm(device_index, size, actual_size);
  11070. } else {
  11071. return ggml_sycl_pool_malloc_leg(device_index, size, actual_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. static void ggml_sycl_pool_free(int device_index, void *ptr, size_t size) try {
  11080. if (g_device_caps[device_index].vmm) {
  11081. ggml_sycl_pool_free_vmm(device_index, ptr, size);
  11082. } else {
  11083. ggml_sycl_pool_free_leg(device_index, ptr, size);
  11084. }
  11085. }
  11086. catch (sycl::exception const &exc) {
  11087. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11088. << ", line:" << __LINE__ << std::endl;
  11089. std::exit(1);
  11090. }
  11091. template<typename T>
  11092. struct sycl_pool_alloc {
  11093. int device_index = -1;
  11094. int device_id = -1;
  11095. T * ptr = nullptr;
  11096. size_t actual_size = 0;
  11097. // size is in number of elements
  11098. T * alloc(size_t size) {
  11099. GGML_ASSERT(ptr == nullptr);
  11100. device_id = get_current_device_id();
  11101. device_index = g_sycl_gpu_mgr->get_index(device_id);
  11102. ptr = (T *) ggml_sycl_pool_malloc(device_index, size * sizeof(T), &this->actual_size);
  11103. // GGML_SYCL_DEBUG("sycl_pool_alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  11104. return ptr;
  11105. }
  11106. sycl_pool_alloc(size_t size) {
  11107. alloc(size);
  11108. }
  11109. ~sycl_pool_alloc() {
  11110. if (ptr != nullptr) {
  11111. ggml_sycl_pool_free(device_index, ptr, actual_size);
  11112. }
  11113. }
  11114. T * get() {
  11115. return ptr;
  11116. }
  11117. sycl_pool_alloc() = default;
  11118. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  11119. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  11120. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  11121. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  11122. };
  11123. static bool g_sycl_loaded = false;
  11124. bool ggml_sycl_loaded(void) {
  11125. return g_sycl_loaded;
  11126. }
  11127. void print_device_detail(int id, sycl::device &device, std::string device_type) {
  11128. dpct::device_info prop;
  11129. SYCL_CHECK(CHECK_TRY_ERROR(
  11130. dpct::get_device_info(prop, device)));
  11131. std::string version;
  11132. version += std::to_string(prop.get_major_version());
  11133. version += ".";
  11134. version += std::to_string(prop.get_minor_version());
  11135. device_type = std::regex_replace(device_type, std::regex("ext_oneapi_"), "");
  11136. std::string name = std::string(prop.get_name());
  11137. name = std::regex_replace(name, std::regex("\\(R\\)"), "");
  11138. name = std::regex_replace(name, std::regex("\\(TM\\)"), "");
  11139. auto global_mem_size = prop.get_global_mem_size()/1000000;
  11140. fprintf(stderr, "|%2d|%19s|%39s|%7s|%7d|%8d|%5d|%6luM|%21s|\n", id, device_type.c_str(),
  11141. name.c_str(), version.c_str(), prop.get_max_compute_units(),
  11142. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  11143. global_mem_size, device.get_info<sycl::info::device::driver_version>().c_str());
  11144. }
  11145. void ggml_backend_sycl_print_sycl_devices() {
  11146. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_print_sycl_devices\n");
  11147. int device_count = dpct::dev_mgr::instance().device_count();
  11148. std::map<std::string, size_t> DeviceNums;
  11149. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  11150. fprintf(stderr, "| | | | |Max | |Max |Global | |\n");
  11151. fprintf(stderr, "| | | | |compute|Max work|sub |mem | |\n");
  11152. fprintf(stderr, "|ID| Device Type| Name|Version|units |group |group|size | Driver version|\n");
  11153. fprintf(stderr, "|--|-------------------|---------------------------------------|-------|-------|--------|-----|-------|---------------------|\n");
  11154. for (int id = 0; id < device_count; ++id) {
  11155. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  11156. sycl::backend backend = device.get_backend();
  11157. std::string backend_type = get_device_backend_and_type(device);
  11158. int type_id=DeviceNums[backend_type]++;
  11159. std::stringstream device_type;
  11160. device_type << "[" << backend_type << ":" << std::to_string(type_id) << "]";
  11161. print_device_detail(id, device, device_type.str());
  11162. }
  11163. }
  11164. void print_gpu_device_list() {
  11165. GGML_ASSERT(g_sycl_gpu_mgr);
  11166. char* hint=NULL;
  11167. if (g_ggml_sycl_backend_gpu_mode == SYCL_SINGLE_GPU_MODE) {
  11168. hint = "use %d SYCL GPUs: [%s] with Max compute units:%d\n";
  11169. } else {
  11170. hint = "detect %d SYCL GPUs: [%s] with top Max compute units:%d\n";
  11171. }
  11172. fprintf(stderr, hint,
  11173. g_sycl_gpu_mgr->get_gpu_count(),
  11174. g_sycl_gpu_mgr->gpus_list.c_str(),
  11175. g_sycl_gpu_mgr->max_compute_units);
  11176. }
  11177. int get_sycl_env(const char *env_name, int default_val) {
  11178. char *user_device_string = getenv(env_name);
  11179. int user_number = default_val;
  11180. unsigned n;
  11181. if (user_device_string != NULL &&
  11182. sscanf(user_device_string, " %u", &n) == 1) {
  11183. user_number = (int)n;
  11184. } else {
  11185. user_number = default_val;
  11186. }
  11187. return user_number;
  11188. }
  11189. int get_work_group_size(int user_device_id) {
  11190. dpct::device_info prop;
  11191. dpct::get_device_info(prop,
  11192. dpct::dev_mgr::instance().get_device(user_device_id));
  11193. return prop.get_max_work_group_size();
  11194. }
  11195. static void ggml_init_sycl() try {
  11196. static bool initialized = false;
  11197. if (!initialized) {
  11198. fprintf(stderr, "[SYCL] call ggml_init_sycl\n");
  11199. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  11200. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  11201. #if defined(GGML_SYCL_F16)
  11202. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  11203. #else
  11204. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  11205. #endif
  11206. /* NOT REMOVE, keep it for next optimize for XMX.
  11207. #if defined(SYCL_USE_XMX)
  11208. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  11209. #else
  11210. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  11211. #endif
  11212. */
  11213. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  11214. dpct::dev_mgr::instance().device_count()) != 0) {
  11215. initialized = true;
  11216. g_sycl_loaded = false;
  11217. return;
  11218. }
  11219. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  11220. ggml_backend_sycl_print_sycl_devices();
  11221. initialized = true;
  11222. g_sycl_loaded = true;
  11223. }
  11224. }
  11225. catch (sycl::exception const &exc) {
  11226. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11227. << ", line:" << __LINE__ << std::endl;
  11228. std::exit(1);
  11229. }
  11230. void ggml_init_by_gpus(int device_count) try {
  11231. g_device_count = device_count;
  11232. g_work_group_size = g_sycl_gpu_mgr->work_group_size;
  11233. int64_t total_vram = 0;
  11234. print_gpu_device_list();
  11235. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  11236. g_device_caps[id].vmm = 0;
  11237. g_device_caps[id].device_id = -1;
  11238. g_device_caps[id].cc = 0;
  11239. g_tensor_split[id] = 0;
  11240. g_default_tensor_split[id] = 0;
  11241. }
  11242. for (int i = 0; i < g_device_count; ++i) {
  11243. int device_id = g_sycl_gpu_mgr->gpus[i];
  11244. g_device_caps[i].vmm = 0;
  11245. dpct::device_info prop;
  11246. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  11247. prop, dpct::dev_mgr::instance().get_device(device_id))));
  11248. g_default_tensor_split[i] = total_vram;
  11249. total_vram += prop.get_global_mem_size();
  11250. g_device_caps[i].cc =
  11251. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  11252. }
  11253. for (int i = 0; i < g_device_count; ++i) {
  11254. g_default_tensor_split[i] /= total_vram;
  11255. }
  11256. for (int i = 0; i < g_device_count; ++i) {
  11257. SYCL_CHECK(ggml_sycl_set_device(i));
  11258. // create sycl streams
  11259. for (int is = 0; is < MAX_STREAMS; ++is) {
  11260. SYCL_CHECK(CHECK_TRY_ERROR(
  11261. g_syclStreams[i][is] =
  11262. dpct::get_current_device().create_queue(
  11263. g_sycl_gpu_mgr->get_co_ctx(), dpct::get_current_device())));
  11264. }
  11265. const dpct::queue_ptr stream = g_syclStreams[i][0];
  11266. // create sycl handle
  11267. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[i] = stream));
  11268. }
  11269. }
  11270. catch (sycl::exception const &exc) {
  11271. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11272. << ", line:" << __LINE__ << std::endl;
  11273. std::exit(1);
  11274. }
  11275. void *ggml_sycl_host_malloc(size_t size) try {
  11276. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  11277. return nullptr;
  11278. }
  11279. ggml_sycl_set_device(g_main_device);
  11280. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  11281. void * ptr = nullptr;
  11282. dpct::err0 err = CHECK_TRY_ERROR(
  11283. ptr = (void *)sycl::malloc_host(size, *main_stream));
  11284. if (err != 0) {
  11285. // clear the error
  11286. fprintf(
  11287. stderr,
  11288. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  11289. size / 1024.0 / 1024.0,
  11290. "syclGetErrorString is not supported");
  11291. return nullptr;
  11292. }
  11293. return ptr;
  11294. }
  11295. catch (sycl::exception const &exc) {
  11296. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11297. << ", line:" << __LINE__ << std::endl;
  11298. std::exit(1);
  11299. }
  11300. void ggml_sycl_host_free(void *ptr) try {
  11301. ggml_sycl_set_device(g_main_device);
  11302. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  11303. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *main_stream)));
  11304. }
  11305. catch (sycl::exception const &exc) {
  11306. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11307. << ", line:" << __LINE__ << std::endl;
  11308. std::exit(1);
  11309. }
  11310. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  11311. const struct ggml_tensor *src,
  11312. int64_t i3, int64_t i2,
  11313. int64_t i1_low, int64_t i1_high,
  11314. dpct::queue_ptr stream) try {
  11315. dpct::memcpy_direction kind;
  11316. char * src_ptr;
  11317. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  11318. kind = dpct::host_to_device;
  11319. src_ptr = (char *) src->data;
  11320. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  11321. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  11322. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  11323. kind = dpct::device_to_device;
  11324. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  11325. int id;
  11326. SYCL_CHECK(CHECK_TRY_ERROR(
  11327. id = get_current_device_id()));
  11328. // GGML_SYCL_DEBUG("current device index %d\n", id);
  11329. src_ptr = (char *) extra->data_device[id];
  11330. } else {
  11331. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  11332. GGML_ASSERT(false);
  11333. }
  11334. char * dst_ptr = (char *) dst;
  11335. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  11336. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  11337. const enum ggml_type type = src->type;
  11338. const int64_t ts = ggml_type_size(type);
  11339. const int64_t bs = ggml_blck_size(type);
  11340. int64_t i1_diff = i1_high - i1_low;
  11341. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  11342. if (nb0 == ts && nb1 == ts*ne0/bs) {
  11343. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  11344. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  11345. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  11346. kind, *stream));
  11347. } else if (nb0 == ts) {
  11348. return CHECK_TRY_ERROR(
  11349. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  11350. ts * ne0 / bs, i1_diff, kind, *stream));
  11351. } else {
  11352. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  11353. const void * rx = (const void *) ((const char *) x + i1*nb1);
  11354. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  11355. // pretend the row is a matrix with cols=1
  11356. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11357. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  11358. /*
  11359. DPCT1001:85: The statement could not be removed.
  11360. */
  11361. /*
  11362. DPCT1000:86: Error handling if-stmt was detected but could not be
  11363. rewritten.
  11364. */
  11365. if (r != 0) return r;
  11366. }
  11367. return 0;
  11368. }
  11369. }
  11370. catch (sycl::exception const &exc) {
  11371. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11372. << ", line:" << __LINE__ << std::endl;
  11373. std::exit(1);
  11374. }
  11375. static void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  11376. const ggml_tensor *src1, ggml_tensor *dst,
  11377. const float *src0_d, const float *src1_d,
  11378. float *dst_d, const dpct::queue_ptr &stream) {
  11379. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  11380. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11381. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  11382. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  11383. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  11384. const int32_t * src1_i32 = (const int32_t *) src1_d;
  11385. switch (src0->type) {
  11386. case GGML_TYPE_F16:
  11387. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  11388. src1_i32, dst_d, stream);
  11389. break;
  11390. case GGML_TYPE_F32:
  11391. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11392. break;
  11393. case GGML_TYPE_Q4_0:
  11394. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11395. break;
  11396. case GGML_TYPE_Q4_1:
  11397. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11398. break;
  11399. case GGML_TYPE_Q5_0:
  11400. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11401. break;
  11402. case GGML_TYPE_Q5_1:
  11403. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11404. break;
  11405. case GGML_TYPE_Q8_0:
  11406. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11407. break;
  11408. default:
  11409. // TODO: k-quants
  11410. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  11411. GGML_ASSERT(false);
  11412. break;
  11413. }
  11414. }
  11415. template <class op>
  11416. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  11417. const ggml_tensor *src1, ggml_tensor *dst,
  11418. const float *src0_dd, const float *src1_dd,
  11419. float *dst_dd,
  11420. const dpct::queue_ptr &main_stream) {
  11421. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  11422. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11423. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  11424. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  11425. (sycl::half *)dst_dd, main_stream);
  11426. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  11427. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  11428. main_stream);
  11429. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  11430. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  11431. main_stream);
  11432. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  11433. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  11434. main_stream);
  11435. } else {
  11436. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  11437. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  11438. GGML_ASSERT(false);
  11439. }
  11440. }
  11441. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  11442. const ggml_tensor *src1, ggml_tensor *dst,
  11443. const float *src0_d, const float *src1_d,
  11444. float *dst_d,
  11445. const dpct::queue_ptr &main_stream) {
  11446. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  11447. (void) src1;
  11448. (void) src1_d;
  11449. }
  11450. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  11451. ggml_tensor *dst, const float *src0_dd,
  11452. const float *src1_dd, float *dst_dd,
  11453. const dpct::queue_ptr &main_stream) {
  11454. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11455. }
  11456. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  11457. ggml_tensor *dst, const float *src0_dd,
  11458. const float *src1_dd, float *dst_dd,
  11459. const dpct::queue_ptr &main_stream) {
  11460. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11461. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11462. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11463. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  11464. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  11465. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  11466. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  11467. int offset = dst->op_params[3] / 4; // offset in bytes
  11468. 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);
  11469. (void) dst;
  11470. }
  11471. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  11472. ggml_tensor *dst, const float *src0_dd,
  11473. const float *src1_dd, float *dst_dd,
  11474. const dpct::queue_ptr &main_stream) {
  11475. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11476. }
  11477. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  11478. ggml_tensor *dst, const float *src0_dd,
  11479. const float *src1_dd, float *dst_dd,
  11480. const dpct::queue_ptr &main_stream) {
  11481. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11482. }
  11483. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  11484. ggml_tensor *dst, const float *src0_dd,
  11485. const float *src1_dd, float *dst_dd,
  11486. const dpct::queue_ptr &main_stream) {
  11487. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11488. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11489. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11490. (void) src1;
  11491. (void) dst;
  11492. (void) src1_dd;
  11493. }
  11494. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  11495. ggml_tensor *dst, const float *src0_dd,
  11496. const float *src1_dd, float *dst_dd,
  11497. const dpct::queue_ptr &main_stream) {
  11498. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11499. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11500. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11501. (void) src1;
  11502. (void) dst;
  11503. (void) src1_dd;
  11504. }
  11505. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  11506. const ggml_tensor *src1, ggml_tensor *dst,
  11507. const float *src0_dd, const float *src1_dd,
  11508. float *dst_dd,
  11509. const dpct::queue_ptr &main_stream) {
  11510. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11511. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11512. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11513. (void) src1;
  11514. (void) dst;
  11515. (void) src1_dd;
  11516. }
  11517. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  11518. ggml_tensor *dst, const float *src0_dd,
  11519. const float *src1_dd, float *dst_dd,
  11520. const dpct::queue_ptr &main_stream) {
  11521. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11522. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11523. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11524. (void) src1;
  11525. (void) dst;
  11526. (void) src1_dd;
  11527. }
  11528. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  11529. ggml_tensor *dst, const float *src0_dd,
  11530. const float *src1_dd, float *dst_dd,
  11531. const dpct::queue_ptr &main_stream) {
  11532. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11533. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11534. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11535. (void) src1;
  11536. (void) dst;
  11537. (void) src1_dd;
  11538. }
  11539. static void ggml_sycl_op_hardsigmoid(const ggml_tensor *src0,
  11540. const ggml_tensor *src1, ggml_tensor *dst,
  11541. const float *src0_dd, const float *src1_dd,
  11542. float *dst_dd,
  11543. const dpct::queue_ptr &main_stream) {
  11544. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11545. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11546. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11547. (void) src1;
  11548. (void) dst;
  11549. (void) src1_dd;
  11550. }
  11551. static void ggml_sycl_op_hardswish(const ggml_tensor *src0,
  11552. const ggml_tensor *src1, ggml_tensor *dst,
  11553. const float *src0_dd, const float *src1_dd,
  11554. float *dst_dd, const dpct::queue_ptr &main_stream) {
  11555. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11556. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11557. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11558. (void) src1;
  11559. (void) dst;
  11560. (void) src1_dd;
  11561. }
  11562. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  11563. const ggml_tensor *src1, ggml_tensor *dst,
  11564. const float *src0_dd, const float *src1_dd,
  11565. float *dst_dd,
  11566. const dpct::queue_ptr &main_stream) {
  11567. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11568. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11569. float negative_slope;
  11570. memcpy(&negative_slope, dst->op_params, sizeof(float));
  11571. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  11572. (void) src1;
  11573. (void) dst;
  11574. (void) src1_dd;
  11575. }
  11576. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  11577. ggml_tensor *dst, const float *src0_dd,
  11578. const float *src1_dd, float *dst_dd,
  11579. const dpct::queue_ptr &main_stream) {
  11580. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11581. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11582. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11583. (void) src1;
  11584. (void) dst;
  11585. (void) src1_dd;
  11586. }
  11587. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  11588. ggml_tensor *dst, const float *src0_dd,
  11589. const float *src1_dd, float *dst_dd,
  11590. const dpct::queue_ptr &main_stream) {
  11591. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11592. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11593. const int64_t ne00 = src0->ne[0];
  11594. const int64_t nrows = ggml_nrows(src0);
  11595. float eps;
  11596. memcpy(&eps, dst->op_params, sizeof(float));
  11597. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11598. (void) src1;
  11599. (void) dst;
  11600. (void) src1_dd;
  11601. }
  11602. inline void ggml_sycl_op_group_norm(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( dst->type == GGML_TYPE_F32);
  11609. int num_groups = dst->op_params[0];
  11610. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  11611. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  11612. (void) src1;
  11613. (void) dst;
  11614. (void) src1_dd;
  11615. }
  11616. inline void ggml_sycl_op_concat(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. #pragma message("TODO: generalize concat kernel for dim != 2")
  11622. #pragma message(" https://github.com/ggerganov/llama.cpp/pull/7563")
  11623. int dim = dst->op_params[0];
  11624. GGML_ASSERT(dim == 2);
  11625. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11626. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11627. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11628. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  11629. 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);
  11630. }
  11631. (void) src1;
  11632. (void) dst;
  11633. }
  11634. inline void ggml_sycl_op_upscale(const ggml_tensor *src0,
  11635. const ggml_tensor *src1, ggml_tensor *dst,
  11636. const float *src0_dd, const float *src1_dd,
  11637. float *dst_dd,
  11638. const dpct::queue_ptr &main_stream) {
  11639. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11640. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11641. const float sf0 = (float)dst->ne[0]/src0->ne[0];
  11642. const float sf1 = (float)dst->ne[1]/src0->ne[1];
  11643. const float sf2 = (float)dst->ne[2]/src0->ne[2];
  11644. const float sf3 = (float)dst->ne[3]/src0->ne[3];
  11645. upscale_f32_sycl(src0_dd, dst_dd, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
  11646. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], sf0, sf1, sf2, sf3,
  11647. main_stream);
  11648. (void) src1;
  11649. (void) dst;
  11650. (void) src1_dd;
  11651. }
  11652. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  11653. ggml_tensor *dst, const float *src0_dd,
  11654. const float *src1_dd, float *dst_dd,
  11655. const dpct::queue_ptr &main_stream) {
  11656. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11657. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11658. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  11659. pad_f32_sycl(src0_dd, dst_dd,
  11660. src0->ne[0], src0->ne[1], src0->ne[2],
  11661. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  11662. (void) src1;
  11663. (void) dst;
  11664. (void) src1_dd;
  11665. }
  11666. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  11667. const ggml_tensor *src1, ggml_tensor *dst,
  11668. const float *src0_dd, const float *src1_dd,
  11669. float *dst_dd,
  11670. const dpct::queue_ptr &main_stream) {
  11671. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11672. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11673. const int64_t ne00 = src0->ne[0];
  11674. const int64_t nrows = ggml_nrows(src0);
  11675. float eps;
  11676. memcpy(&eps, dst->op_params, sizeof(float));
  11677. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11678. (void) src1;
  11679. (void) dst;
  11680. (void) src1_dd;
  11681. }
  11682. inline void ggml_sycl_op_mul_mat_q(
  11683. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11684. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11685. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11686. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11687. const dpct::queue_ptr &stream) try {
  11688. const int64_t ne00 = src0->ne[0];
  11689. const int64_t ne10 = src1->ne[0];
  11690. GGML_ASSERT(ne10 % QK8_1 == 0);
  11691. const int64_t ne0 = dst->ne[0];
  11692. const int64_t row_diff = row_high - row_low;
  11693. int device_id;
  11694. SYCL_CHECK(
  11695. CHECK_TRY_ERROR(device_id = get_current_device_id()));
  11696. // the main device has a larger memory buffer to hold the results from all GPUs
  11697. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  11698. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && device_id == g_main_device ? ne0 : row_diff;
  11699. switch (src0->type) {
  11700. case GGML_TYPE_Q4_0:
  11701. 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);
  11702. break;
  11703. case GGML_TYPE_Q4_1:
  11704. 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);
  11705. break;
  11706. case GGML_TYPE_Q5_0:
  11707. 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);
  11708. break;
  11709. case GGML_TYPE_Q5_1:
  11710. 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);
  11711. break;
  11712. case GGML_TYPE_Q8_0:
  11713. 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);
  11714. break;
  11715. case GGML_TYPE_Q2_K:
  11716. 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);
  11717. break;
  11718. case GGML_TYPE_Q3_K:
  11719. 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);
  11720. break;
  11721. case GGML_TYPE_Q4_K:
  11722. 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);
  11723. break;
  11724. case GGML_TYPE_Q5_K:
  11725. 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);
  11726. break;
  11727. case GGML_TYPE_Q6_K:
  11728. 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);
  11729. break;
  11730. default:
  11731. GGML_ASSERT(false);
  11732. break;
  11733. }
  11734. (void) src1;
  11735. (void) dst;
  11736. (void) src1_ddf_i;
  11737. }
  11738. catch (sycl::exception const &exc) {
  11739. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11740. << ", line:" << __LINE__ << std::endl;
  11741. std::exit(1);
  11742. }
  11743. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  11744. int64_t min_compute_capability = INT_MAX;
  11745. int64_t max_compute_capability = INT_MIN;
  11746. for (int i = 0; i < g_device_count; ++i) {
  11747. if (tensor_split[i] < (i + 1 < g_device_count ? tensor_split[i + 1] : 1.0f)) {
  11748. if (min_compute_capability > g_device_caps[i].cc) {
  11749. min_compute_capability = g_device_caps[i].cc;
  11750. }
  11751. if (max_compute_capability < g_device_caps[i].cc) {
  11752. max_compute_capability = g_device_caps[i].cc;
  11753. }
  11754. }
  11755. }
  11756. switch(type) {
  11757. case GGML_TYPE_Q4_0:
  11758. case GGML_TYPE_Q4_1:
  11759. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11760. case GGML_TYPE_Q5_0:
  11761. case GGML_TYPE_Q5_1:
  11762. case GGML_TYPE_Q8_0:
  11763. return 64;
  11764. case GGML_TYPE_F16:
  11765. case GGML_TYPE_F32:
  11766. return 1;
  11767. case GGML_TYPE_Q2_K:
  11768. case GGML_TYPE_Q3_K:
  11769. case GGML_TYPE_Q4_K:
  11770. case GGML_TYPE_Q5_K:
  11771. case GGML_TYPE_IQ2_XXS:
  11772. case GGML_TYPE_IQ2_XS:
  11773. case GGML_TYPE_IQ2_S:
  11774. case GGML_TYPE_IQ1_S:
  11775. case GGML_TYPE_IQ1_M:
  11776. case GGML_TYPE_IQ3_XXS:
  11777. case GGML_TYPE_IQ4_XS:
  11778. case GGML_TYPE_IQ4_NL:
  11779. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11780. case GGML_TYPE_IQ3_S:
  11781. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11782. case GGML_TYPE_Q6_K:
  11783. return 64;
  11784. default:
  11785. GGML_ASSERT(false);
  11786. }
  11787. }
  11788. inline void ggml_sycl_op_mul_mat_vec_q(
  11789. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11790. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11791. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11792. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11793. const dpct::queue_ptr &stream) {
  11794. const int64_t ne10 = src1->ne[0];
  11795. GGML_ASSERT(ne10 % QK8_1 == 0);
  11796. const int64_t ne00 = src0->ne[0];
  11797. const int64_t row_diff = row_high - row_low;
  11798. int id;
  11799. SYCL_CHECK(
  11800. CHECK_TRY_ERROR(id = get_current_device_id()));
  11801. // the main device has a larger memory buffer to hold the results from all GPUs
  11802. // nrows_dst == nrows of the matrix that the kernel writes into
  11803. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne00 : row_diff;
  11804. switch (src0->type) {
  11805. case GGML_TYPE_Q4_0:
  11806. mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11807. break;
  11808. case GGML_TYPE_Q4_1:
  11809. mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11810. break;
  11811. case GGML_TYPE_Q5_0:
  11812. mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11813. break;
  11814. case GGML_TYPE_Q5_1:
  11815. mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11816. break;
  11817. case GGML_TYPE_Q8_0:
  11818. mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11819. break;
  11820. case GGML_TYPE_Q2_K:
  11821. mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11822. break;
  11823. case GGML_TYPE_Q3_K:
  11824. mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11825. break;
  11826. case GGML_TYPE_Q4_K:
  11827. mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11828. break;
  11829. case GGML_TYPE_Q5_K:
  11830. mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11831. break;
  11832. case GGML_TYPE_Q6_K:
  11833. mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11834. break;
  11835. case GGML_TYPE_IQ1_S:
  11836. mul_mat_vec_iq1_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11837. break;
  11838. case GGML_TYPE_IQ1_M:
  11839. mul_mat_vec_iq1_m_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11840. break;
  11841. case GGML_TYPE_IQ2_XXS:
  11842. mul_mat_vec_iq2_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11843. break;
  11844. case GGML_TYPE_IQ2_XS:
  11845. mul_mat_vec_iq2_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11846. break;
  11847. case GGML_TYPE_IQ2_S:
  11848. mul_mat_vec_iq2_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11849. break;
  11850. case GGML_TYPE_IQ3_XXS:
  11851. mul_mat_vec_iq3_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11852. break;
  11853. case GGML_TYPE_IQ3_S:
  11854. mul_mat_vec_iq3_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11855. break;
  11856. case GGML_TYPE_IQ4_NL:
  11857. mul_mat_vec_iq4_nl_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11858. break;
  11859. case GGML_TYPE_IQ4_XS:
  11860. mul_mat_vec_iq4_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11861. break;
  11862. default:
  11863. GGML_ASSERT(false);
  11864. break;
  11865. }
  11866. (void) src1;
  11867. (void) dst;
  11868. (void) src1_ddf_i;
  11869. (void) src1_ncols;
  11870. (void) src1_padded_row_size;
  11871. }
  11872. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  11873. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11874. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11875. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11876. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11877. const dpct::queue_ptr &stream) {
  11878. const int64_t ne00 = src0->ne[0];
  11879. const int64_t row_diff = row_high - row_low;
  11880. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11881. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  11882. #ifdef GGML_SYCL_F16
  11883. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  11884. sycl::half *src1_dfloat = nullptr; // dfloat == half
  11885. bool src1_convert_f16 =
  11886. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  11887. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  11888. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  11889. if (src1_convert_f16) {
  11890. src1_dfloat = src1_dfloat_a.alloc(ne00);
  11891. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11892. GGML_ASSERT(to_fp16_sycl != nullptr);
  11893. to_fp16_sycl(src1_ddf_i, src1_dfloat, ne00, stream);
  11894. }
  11895. #else
  11896. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  11897. #endif // GGML_SYCL_F16
  11898. switch (src0->type) {
  11899. case GGML_TYPE_Q4_0:
  11900. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11901. break;
  11902. case GGML_TYPE_Q4_1:
  11903. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11904. break;
  11905. case GGML_TYPE_Q5_0:
  11906. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11907. break;
  11908. case GGML_TYPE_Q5_1:
  11909. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11910. break;
  11911. case GGML_TYPE_Q8_0:
  11912. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11913. break;
  11914. case GGML_TYPE_Q2_K:
  11915. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11916. break;
  11917. case GGML_TYPE_Q3_K:
  11918. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11919. break;
  11920. case GGML_TYPE_Q4_K:
  11921. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11922. break;
  11923. case GGML_TYPE_Q5_K:
  11924. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11925. break;
  11926. case GGML_TYPE_Q6_K:
  11927. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11928. break;
  11929. case GGML_TYPE_F16:
  11930. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11931. break;
  11932. default:
  11933. printf("ggml_sycl_op_dequantize_mul_mat_vec unsupported GGML_TYPE %d\n", src0->type);
  11934. GGML_ASSERT(false);
  11935. break;
  11936. }
  11937. (void) src1;
  11938. (void) dst;
  11939. (void) src1_ddq_i;
  11940. (void) src1_ncols;
  11941. (void) src1_padded_row_size;
  11942. }
  11943. inline void ggml_sycl_op_mul_mat_sycl(
  11944. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11945. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11946. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11947. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11948. const dpct::queue_ptr &stream) try {
  11949. GGML_ASSERT(src0_dd_i != nullptr);
  11950. GGML_ASSERT(src1_ddf_i != nullptr);
  11951. GGML_ASSERT(dst_dd_i != nullptr);
  11952. const int64_t ne00 = src0->ne[0];
  11953. const int64_t ne10 = src1->ne[0];
  11954. const int64_t ne0 = dst->ne[0];
  11955. const int64_t row_diff = row_high - row_low;
  11956. int id;
  11957. SYCL_CHECK(
  11958. CHECK_TRY_ERROR(id = get_current_device_id()));
  11959. // the main device has a larger memory buffer to hold the results from all GPUs
  11960. // ldc == nrows of the matrix that cuBLAS writes into
  11961. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  11962. #ifdef GGML_SYCL_F16
  11963. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  11964. #else
  11965. bool use_fp16 = false;
  11966. #endif
  11967. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  11968. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  11969. dst->op_params[0] == GGML_PREC_DEFAULT) {
  11970. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  11971. sycl_pool_alloc<sycl::half> src0_as_f16;
  11972. if (src0->type != GGML_TYPE_F16) {
  11973. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  11974. GGML_ASSERT(to_fp16_sycl != nullptr);
  11975. size_t ne = row_diff*ne00;
  11976. src0_as_f16.alloc(ne);
  11977. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  11978. }
  11979. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  11980. ? (const sycl::half *)src0_dd_i
  11981. : src0_as_f16.get();
  11982. sycl_pool_alloc<sycl::half> src1_as_f16;
  11983. if (src1->type != GGML_TYPE_F16) {
  11984. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11985. GGML_ASSERT(to_fp16_sycl != nullptr);
  11986. size_t ne = src1_ncols*ne10;
  11987. src1_as_f16.alloc(ne);
  11988. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  11989. }
  11990. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  11991. ? (const sycl::half *)src1->data + src1_padded_row_size
  11992. : src1_as_f16.get();
  11993. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  11994. const sycl::half alpha_f16 = 1.0f;
  11995. const sycl::half beta_f16 = 0.0f;
  11996. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  11997. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  11998. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  11999. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12000. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  12001. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  12002. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  12003. dpct::library_data_t::real_half)));
  12004. g_sycl_handles[id]->wait();
  12005. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  12006. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  12007. }
  12008. else {
  12009. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  12010. sycl_pool_alloc<float> src0_ddq_as_f32;
  12011. sycl_pool_alloc<float> src1_ddq_as_f32;
  12012. if (src0->type != GGML_TYPE_F32) {
  12013. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  12014. GGML_ASSERT(to_fp32_sycl != nullptr);
  12015. src0_ddq_as_f32.alloc(row_diff*ne00);
  12016. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  12017. }
  12018. if (src1->type != GGML_TYPE_F32) {
  12019. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  12020. GGML_ASSERT(to_fp32_sycl != nullptr);
  12021. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  12022. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  12023. }
  12024. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  12025. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  12026. const float alpha = 1.0f;
  12027. const float beta = 0.0f;
  12028. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  12029. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  12030. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  12031. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12032. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  12033. src1_ddf1_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  12034. dst_dd_i, ldc)));
  12035. g_sycl_handles[id]->wait();
  12036. }
  12037. (void) dst;
  12038. (void) src1_ddq_i;
  12039. (void) src1_padded_row_size;
  12040. }
  12041. catch (sycl::exception const &exc) {
  12042. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12043. << ", line:" << __LINE__ << std::endl;
  12044. std::exit(1);
  12045. }
  12046. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  12047. ggml_tensor *dst, const float *src0_dd,
  12048. const float *src1_dd, float *dst_dd,
  12049. const dpct::queue_ptr &main_stream) {
  12050. const ggml_tensor * src2 = dst->src[2];
  12051. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  12052. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  12053. GGML_ASSERT(src0->type == dst->type);
  12054. const int64_t ne00 = src0->ne[0];
  12055. const int64_t ne01 = src0->ne[1];
  12056. const int64_t ne2 = dst->ne[2];
  12057. const int64_t nrows = ggml_nrows(src0);
  12058. //const int n_past = ((int32_t *) dst->op_params)[0];
  12059. const int n_dims = ((int32_t *) dst->op_params)[1];
  12060. const int mode = ((int32_t *) dst->op_params)[2];
  12061. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  12062. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  12063. // RoPE alteration for extended context
  12064. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  12065. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  12066. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  12067. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  12068. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  12069. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  12070. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  12071. const float * freq_factors = nullptr;
  12072. const int32_t * pos = nullptr;
  12073. if ((mode & 1) == 0) {
  12074. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  12075. GGML_ASSERT(src1->ne[0] == ne2);
  12076. pos = (const int32_t *) src1_dd;
  12077. }
  12078. const bool is_neox = mode & 2;
  12079. #pragma message("TODO: update rope NORM mode to match NEOX mode")
  12080. #pragma message(" https://github.com/ggerganov/llama.cpp/pull/7634")
  12081. if (is_neox) {
  12082. pos = (const int32_t *) src1_dd;
  12083. if (src2 != nullptr) {
  12084. freq_factors = (const float *) src2->data;
  12085. }
  12086. } else {
  12087. GGML_ASSERT(src2 == nullptr && "TODO: freq_factors not implemented for !is_neox");
  12088. }
  12089. rope_corr_dims corr_dims;
  12090. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims.v);
  12091. // compute
  12092. if (is_neox) {
  12093. if (src0->type == GGML_TYPE_F32) {
  12094. rope_neox_sycl(
  12095. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12096. attn_factor, corr_dims, freq_factors, main_stream
  12097. );
  12098. } else if (src0->type == GGML_TYPE_F16) {
  12099. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  12100. ne00, n_dims, nrows, pos, freq_scale, ne01,
  12101. freq_base, ext_factor, attn_factor, corr_dims,
  12102. freq_factors, main_stream);
  12103. } else {
  12104. GGML_ASSERT(false);
  12105. }
  12106. } else {
  12107. if (src0->type == GGML_TYPE_F32) {
  12108. rope_sycl(
  12109. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12110. attn_factor, corr_dims, main_stream
  12111. );
  12112. } else if (src0->type == GGML_TYPE_F16) {
  12113. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  12114. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12115. attn_factor, corr_dims, main_stream);
  12116. } else {
  12117. GGML_ASSERT(false);
  12118. }
  12119. }
  12120. (void) src1;
  12121. (void) dst;
  12122. (void) src1_dd;
  12123. }
  12124. static void ggml_sycl_op_pool2d(const ggml_tensor *src0,
  12125. const ggml_tensor *src1, ggml_tensor *dst,
  12126. const float *src0_dd, const float *src1_dd,
  12127. float *dst_dd, const dpct::queue_ptr &main_stream) {
  12128. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12129. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12130. const int32_t * opts = (const int32_t *)dst->op_params;
  12131. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  12132. const int k0 = opts[1];
  12133. const int k1 = opts[2];
  12134. const int s0 = opts[3];
  12135. const int s1 = opts[4];
  12136. const int p0 = opts[5];
  12137. const int p1 = opts[6];
  12138. const int64_t IH = src0->ne[1];
  12139. const int64_t IW = src0->ne[0];
  12140. const int64_t N = dst->ne[3];
  12141. const int64_t OC = dst->ne[2];
  12142. const int64_t OH = dst->ne[1];
  12143. const int64_t OW = dst->ne[0];
  12144. const int parallel_elements = N * OC * OH * OW;
  12145. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  12146. sycl::range<3> block_nums(1, 1, num_blocks);
  12147. main_stream->parallel_for(
  12148. sycl::nd_range<3>(block_nums *
  12149. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  12150. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  12151. [=](sycl::nd_item<3> item_ct1) {
  12152. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  12153. parallel_elements, src0_dd, dst_dd, op,
  12154. item_ct1);
  12155. });
  12156. (void) src1;
  12157. (void) src1_dd;
  12158. }
  12159. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  12160. const ggml_tensor *src1, ggml_tensor *dst,
  12161. const float *src0_dd, const float *src1_dd,
  12162. float *dst_dd,
  12163. const dpct::queue_ptr &main_stream) {
  12164. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12165. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12166. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  12167. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  12168. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  12169. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  12170. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  12171. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  12172. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  12173. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  12174. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  12175. const int64_t IH = is_2D ? src1->ne[1] : 1;
  12176. const int64_t IW = src1->ne[0];
  12177. const int64_t KH = is_2D ? src0->ne[1] : 1;
  12178. const int64_t KW = src0->ne[0];
  12179. const int64_t OH = is_2D ? dst->ne[2] : 1;
  12180. const int64_t OW = dst->ne[1];
  12181. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  12182. if (dst->type == GGML_TYPE_F16) {
  12183. 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);
  12184. } else {
  12185. im2col_sycl(src1_dd, (float *)dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  12186. }
  12187. (void) src0;
  12188. (void) src0_dd;
  12189. }
  12190. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  12191. const ggml_tensor *src1, ggml_tensor *dst,
  12192. const float *src0_dd, const float *src1_dd,
  12193. float *dst_dd,
  12194. const dpct::queue_ptr &main_stream) {
  12195. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12196. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12197. const int64_t ncols = src0->ne[0];
  12198. const int64_t nrows = ggml_nrows(src0);
  12199. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  12200. (void) src1;
  12201. (void) dst;
  12202. (void) src1_dd;
  12203. }
  12204. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  12205. const ggml_tensor *src1, ggml_tensor *dst,
  12206. const float *src0_dd, const float *src1_dd,
  12207. float *dst_dd,
  12208. const dpct::queue_ptr &main_stream) {
  12209. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12210. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  12211. const int64_t ncols = src0->ne[0];
  12212. const int64_t nrows = ggml_nrows(src0);
  12213. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  12214. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  12215. (void) src1;
  12216. (void) dst;
  12217. (void) src1_dd;
  12218. }
  12219. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  12220. const ggml_tensor *src1,
  12221. ggml_tensor *dst, const float *src0_dd,
  12222. const float *src1_dd, float *dst_dd,
  12223. const dpct::queue_ptr &main_stream) {
  12224. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12225. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12226. const int64_t ne00 = src0->ne[0];
  12227. const int64_t ne01 = src0->ne[1];
  12228. const int nrows0 = ggml_nrows(src0);
  12229. const int n_past = ((int32_t *) dst->op_params)[0];
  12230. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  12231. (void) src1;
  12232. (void) dst;
  12233. (void) src1_dd;
  12234. }
  12235. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  12236. const ggml_tensor *src1, ggml_tensor *dst,
  12237. const float *src0_dd, const float *src1_dd,
  12238. float *dst_dd,
  12239. const dpct::queue_ptr &main_stream) {
  12240. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12241. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12242. #pragma message("TODO: add ggml_sycl_op_soft_max() F16 src1 support")
  12243. #pragma message("ref: https://github.com/ggerganov/llama.cpp/pull/5021")
  12244. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  12245. const int64_t ne00 = src0->ne[0];
  12246. const int64_t nrows_x = ggml_nrows(src0);
  12247. const int64_t nrows_y = src0->ne[1];
  12248. float scale = 1.0f;
  12249. float max_bias = 0.0f;
  12250. memcpy(&scale, dst->op_params + 0, sizeof(float));
  12251. memcpy(&max_bias, dst->op_params + 1, sizeof(float));
  12252. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00,
  12253. nrows_x, nrows_y, scale, max_bias, main_stream);
  12254. }
  12255. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  12256. ggml_tensor *dst, const float *src0_dd,
  12257. const float *src1_dd, float *dst_dd,
  12258. const dpct::queue_ptr &main_stream) {
  12259. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12260. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12261. float scale;
  12262. memcpy(&scale, dst->op_params, sizeof(float));
  12263. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  12264. /*
  12265. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  12266. error codes. The call was replaced with 0. You need to rewrite this code.
  12267. */
  12268. SYCL_CHECK(0);
  12269. (void) src1;
  12270. (void) dst;
  12271. (void) src1_dd;
  12272. }
  12273. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  12274. ggml_tensor *dst, const float *src0_dd,
  12275. const float *src1_dd, float *dst_dd,
  12276. const dpct::queue_ptr &main_stream) {
  12277. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12278. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12279. float min;
  12280. float max;
  12281. memcpy(&min, dst->op_params, sizeof(float));
  12282. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  12283. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  12284. /*
  12285. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  12286. error codes. The call was replaced with 0. You need to rewrite this code.
  12287. */
  12288. SYCL_CHECK(0);
  12289. (void) src1;
  12290. (void) dst;
  12291. (void) src1_dd;
  12292. }
  12293. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  12294. const ggml_tensor *src1, ggml_tensor *dst,
  12295. const ggml_sycl_op_flatten_t op) try {
  12296. const int64_t nrows0 = ggml_nrows(src0);
  12297. const bool use_src1 = src1 != nullptr;
  12298. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  12299. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12300. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12301. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12302. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  12303. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12304. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12305. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  12306. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  12307. // dd = data device
  12308. float * src0_ddf = nullptr;
  12309. float * src1_ddf = nullptr;
  12310. float * dst_ddf = nullptr;
  12311. sycl_pool_alloc<float> src0_f;
  12312. sycl_pool_alloc<float> src1_f;
  12313. sycl_pool_alloc<float> dst_f;
  12314. ggml_sycl_set_device(g_main_device);
  12315. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12316. // GGML_SYCL_DEBUG("g_main_device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  12317. // g_main_device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  12318. if (src0_on_device) {
  12319. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  12320. } else {
  12321. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  12322. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  12323. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  12324. }
  12325. if (use_src1) {
  12326. if (src1_on_device) {
  12327. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12328. } else {
  12329. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  12330. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  12331. }
  12332. }
  12333. if (dst_on_device) {
  12334. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12335. } else {
  12336. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  12337. }
  12338. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  12339. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12340. // do the computation
  12341. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12342. /*
  12343. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  12344. error codes. The call was replaced with 0. You need to rewrite this code.
  12345. */
  12346. SYCL_CHECK(0);
  12347. // copy dst to host if necessary
  12348. if (!dst_on_device) {
  12349. SYCL_CHECK(CHECK_TRY_ERROR(
  12350. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst)).wait()));
  12351. }
  12352. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12353. SYCL_CHECK(CHECK_TRY_ERROR(
  12354. dpct::get_current_device().queues_wait_and_throw()));
  12355. }
  12356. // print_ggml_tensor("tensor", dst);
  12357. }
  12358. catch (sycl::exception const &exc) {
  12359. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12360. << ", line:" << __LINE__ << std::endl;
  12361. std::exit(1);
  12362. }
  12363. static void ggml_sycl_set_peer_access(const int n_tokens) {
  12364. static bool peer_access_enabled = false;
  12365. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  12366. if (peer_access_enabled == enable_peer_access) {
  12367. return;
  12368. }
  12369. #ifdef NDEBUG
  12370. for (int i = 0; i < g_device_count; ++i) {
  12371. SYCL_CHECK(ggml_sycl_set_device(i));
  12372. // SYCL_CHECK(syclDeviceSynchronize());
  12373. }
  12374. for (int i = 0; i < g_device_count; ++i) {
  12375. SYCL_CHECK(ggml_sycl_set_device(i));
  12376. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  12377. if (i == id_other) {
  12378. continue;
  12379. }
  12380. if (i != g_main_device && id_other != g_main_device) {
  12381. continue;
  12382. }
  12383. // int can_access_peer;
  12384. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  12385. // if (can_access_peer) {
  12386. // if (enable_peer_access) {
  12387. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  12388. // } else {
  12389. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  12390. // }
  12391. // }
  12392. }
  12393. }
  12394. #endif // NDEBUG
  12395. peer_access_enabled = enable_peer_access;
  12396. }
  12397. struct ggml_backend_sycl_split_buffer_type_context {
  12398. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12399. };
  12400. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  12401. const ggml_tensor *src1, ggml_tensor *dst,
  12402. ggml_sycl_op_mul_mat_t op,
  12403. const bool convert_src1_to_q8_1) try {
  12404. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  12405. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  12406. const int64_t nrows1 = ggml_nrows(src1);
  12407. GGML_ASSERT(ne03 == ne13);
  12408. const int64_t ne0 = dst->ne[0];
  12409. const int64_t ne1 = dst->ne[1];
  12410. const int nb2 = dst->nb[2];
  12411. const int nb3 = dst->nb[3];
  12412. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12413. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12414. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  12415. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  12416. const int64_t i02_divisor = ne12 / ne02;
  12417. const size_t src0_ts = ggml_type_size(src0->type);
  12418. const size_t src0_bs = ggml_blck_size(src0->type);
  12419. const size_t q8_1_ts = sizeof(block_q8_1);
  12420. const size_t q8_1_bs = QK8_1;
  12421. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12422. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12423. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12424. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12425. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  12426. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  12427. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  12428. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12429. GGML_ASSERT(!(split && ne02 > 1));
  12430. GGML_ASSERT(!(split && ne03 > 1));
  12431. GGML_ASSERT(!(split && ne02 < ne12));
  12432. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12433. if (split) {
  12434. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  12435. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  12436. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  12437. tensor_split = buft_ctx->tensor_split;
  12438. }
  12439. struct dev_data {
  12440. sycl_pool_alloc<char> src0_dd_alloc;
  12441. sycl_pool_alloc<float> src1_ddf_alloc;
  12442. sycl_pool_alloc<char> src1_ddq_alloc;
  12443. sycl_pool_alloc<float> dst_dd_alloc;
  12444. char *src0_dd = nullptr;
  12445. float *src1_ddf = nullptr; // float
  12446. char *src1_ddq = nullptr; // q8_1
  12447. float *dst_dd = nullptr;
  12448. int64_t row_low;
  12449. int64_t row_high;
  12450. };
  12451. dev_data dev[GGML_SYCL_MAX_DEVICES];
  12452. int used_devices = 0;
  12453. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12454. for (int i = 0; i < g_device_count; ++i) {
  12455. // by default, use all rows
  12456. dev[i].row_low = 0;
  12457. dev[i].row_high = ne01;
  12458. // for multi GPU, get the row boundaries from tensor split
  12459. // and round to mul_mat_q tile sizes
  12460. if (split) {
  12461. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  12462. if (i != 0) {
  12463. dev[i].row_low = ne01*tensor_split[i];
  12464. if (dev[i].row_low < ne01) {
  12465. dev[i].row_low -= dev[i].row_low % rounding;
  12466. }
  12467. }
  12468. if (i != g_device_count - 1) {
  12469. dev[i].row_high = ne01*tensor_split[i + 1];
  12470. if (dev[i].row_high < ne01) {
  12471. dev[i].row_high -= dev[i].row_high % rounding;
  12472. }
  12473. }
  12474. }
  12475. }
  12476. for (int i = 0; i < g_device_count; ++i) {
  12477. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12478. continue;
  12479. }
  12480. used_devices++;
  12481. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12482. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12483. ggml_sycl_set_device(i);
  12484. dpct::queue_ptr stream = g_syclStreams[i][0];
  12485. if (src0_on_device && src0_is_contiguous) {
  12486. dev[i].src0_dd = (char *) src0_extra->data_device[i];
  12487. } else {
  12488. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ggml_nbytes(src0));
  12489. }
  12490. if (src1_on_device && src1_is_contiguous) {
  12491. dev[i].src1_ddf = (float *) src1_extra->data_device[i];
  12492. } else {
  12493. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ggml_nelements(src1));
  12494. }
  12495. if (convert_src1_to_q8_1) {
  12496. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  12497. if (src1_on_device && src1_is_contiguous) {
  12498. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  12499. /*
  12500. DPCT1010:90: SYCL uses exceptions to report errors and does not
  12501. use the error codes. The call was replaced with 0. You need to
  12502. rewrite this code.
  12503. */
  12504. SYCL_CHECK(0);
  12505. }
  12506. }
  12507. if (dst_on_device) {
  12508. dev[i].dst_dd = (float *) dst_extra->data_device[i];
  12509. } else {
  12510. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  12511. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(size_dst_ddf);
  12512. }
  12513. }
  12514. // if multiple devices are used they need to wait for the main device
  12515. // here an event is recorded that signals that the main device has finished calculating the input data
  12516. if (split && used_devices > 1) {
  12517. ggml_sycl_set_device(g_main_device);
  12518. /*
  12519. DPCT1024:91: The original code returned the error code that was further
  12520. consumed by the program logic. This original code was replaced with 0.
  12521. You may need to rewrite the program logic consuming the error code.
  12522. */
  12523. SYCL_CHECK(CHECK_TRY_ERROR(
  12524. *src0_extra->events[g_main_device][0] =
  12525. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier()));
  12526. }
  12527. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  12528. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  12529. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  12530. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  12531. for (int i = 0; i < g_device_count; ++i) {
  12532. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12533. continue;
  12534. }
  12535. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12536. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12537. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  12538. ggml_sycl_set_device(i);
  12539. dpct::queue_ptr stream = g_syclStreams[i][is];
  12540. // wait for main GPU data if necessary
  12541. if (split && (i != g_main_device || is != 0)) {
  12542. /*
  12543. DPCT1009:163: SYCL uses exceptions to report errors and does not
  12544. use the error codes. The original code was commented out and a
  12545. warning string was inserted. You need to rewrite this code.
  12546. */
  12547. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  12548. {*src0_extra->events[g_main_device][0]})));
  12549. }
  12550. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  12551. const int64_t i03 = i0 / ne12;
  12552. const int64_t i02 = i0 % ne12;
  12553. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  12554. // for split tensors the data begins at i0 == i0_offset_low
  12555. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  12556. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  12557. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  12558. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  12559. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  12560. // in that case an offset on dst_ddf_i is needed
  12561. if (dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device) {
  12562. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  12563. }
  12564. // copy src0, src1 to device if necessary
  12565. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  12566. if (i != g_main_device) {
  12567. if (convert_src1_to_q8_1) {
  12568. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  12569. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  12570. src1_ddq_i, src1_ddq_i_source,
  12571. src1_ncols * src1_padded_col_size * q8_1_ts /
  12572. q8_1_bs).wait()));
  12573. } else {
  12574. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  12575. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  12576. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  12577. src1_ddf_i, src1_ddf_i_source,
  12578. src1_ncols * ne10 * sizeof(float))));
  12579. }
  12580. }
  12581. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  12582. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  12583. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  12584. } else {
  12585. GGML_ASSERT(false);
  12586. }
  12587. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  12588. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  12589. /*
  12590. DPCT1010:92: SYCL uses exceptions to report errors and does
  12591. not use the error codes. The call was replaced with 0. You
  12592. need to rewrite this code.
  12593. */
  12594. SYCL_CHECK(0);
  12595. }
  12596. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  12597. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  12598. }
  12599. if (src1->type == GGML_TYPE_F16) {
  12600. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  12601. }
  12602. // do the computation
  12603. SYCL_CHECK(CHECK_TRY_ERROR(op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  12604. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream)));
  12605. /*
  12606. DPCT1010:93: SYCL uses exceptions to report errors and does not
  12607. use the error codes. The call was replaced with 0. You need to
  12608. rewrite this code.
  12609. */
  12610. SYCL_CHECK(0);
  12611. // copy dst to host or other device if necessary
  12612. if (!dst_on_device) {
  12613. void * dst_off_device;
  12614. dpct::memcpy_direction kind;
  12615. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12616. dst_off_device = dst->data;
  12617. kind = dpct::device_to_host;
  12618. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  12619. dst_off_device = dst_extra->data_device[g_main_device];
  12620. kind = dpct::device_to_device;
  12621. } else {
  12622. GGML_ASSERT(false);
  12623. }
  12624. if (split) {
  12625. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  12626. // dst is NOT transposed.
  12627. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  12628. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  12629. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  12630. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12631. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12632. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  12633. //todo, dirty solution. Need be updated when device2device memcpy() is supported.
  12634. if (kind == dpct::device_to_device) {
  12635. size_t dst_size = ggml_nbytes_pad(dst);
  12636. float *host_buf = (float *)malloc(dst_size);
  12637. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12638. host_buf, ne0 * sizeof(float), dst_dd_i,
  12639. row_diff * sizeof(float), row_diff * sizeof(float),
  12640. src1_ncols, dpct::device_to_host, *stream)));
  12641. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[i]).queues_wait_and_throw();
  12642. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12643. dhf_dst_i, ne0 * sizeof(float), host_buf,
  12644. row_diff * sizeof(float), row_diff * sizeof(float),
  12645. src1_ncols, dpct::host_to_device, *main_stream)));
  12646. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[g_main_device]).queues_wait_and_throw();
  12647. free(host_buf);
  12648. } else {
  12649. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12650. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  12651. row_diff * sizeof(float), row_diff * sizeof(float),
  12652. src1_ncols, kind, *stream)));
  12653. }
  12654. } else {
  12655. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12656. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12657. dhf_dst_i += src1_col_0*ne0;
  12658. SYCL_CHECK(CHECK_TRY_ERROR(
  12659. stream->memcpy(dhf_dst_i, dst_dd_i,
  12660. src1_ncols * ne0 * sizeof(float)).wait()));
  12661. }
  12662. }
  12663. // add event for the main device to wait on until other device is done
  12664. if (split && (i != g_main_device || is != 0)) {
  12665. /*
  12666. DPCT1024:94: The original code returned the error code that
  12667. was further consumed by the program logic. This original
  12668. code was replaced with 0. You may need to rewrite the
  12669. program logic consuming the error code.
  12670. */
  12671. SYCL_CHECK(CHECK_TRY_ERROR(
  12672. *src0_extra->events[i][is] =
  12673. stream->ext_oneapi_submit_barrier()));
  12674. }
  12675. }
  12676. }
  12677. }
  12678. // main device waits for all other devices to be finished
  12679. if (split && g_device_count > 1) {
  12680. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  12681. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  12682. ggml_sycl_set_device(g_main_device);
  12683. for (int i = 0; i < g_device_count; ++i) {
  12684. if (dev[i].row_low == dev[i].row_high) {
  12685. continue;
  12686. }
  12687. for (int64_t is = 0; is < is_max; ++is) {
  12688. SYCL_CHECK(CHECK_TRY_ERROR(
  12689. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier(
  12690. {*src0_extra->events[i][is]})));
  12691. }
  12692. }
  12693. }
  12694. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12695. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12696. SYCL_CHECK(CHECK_TRY_ERROR(
  12697. dpct::get_current_device().queues_wait_and_throw()));
  12698. }
  12699. }
  12700. catch (sycl::exception const &exc) {
  12701. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12702. << ", line:" << __LINE__ << std::endl;
  12703. std::exit(1);
  12704. }
  12705. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12706. GGML_SYCL_DEBUG("call %s\n", __func__);
  12707. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  12708. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12709. }
  12710. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12711. GGML_SYCL_DEBUG("call %s\n", __func__);
  12712. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  12713. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12714. }
  12715. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12716. GGML_SYCL_DEBUG("call %s\n", __func__);
  12717. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  12718. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12719. }
  12720. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12721. GGML_SYCL_DEBUG("call %s\n", __func__);
  12722. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  12723. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12724. }
  12725. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12726. GGML_SYCL_DEBUG("call %s\n", __func__);
  12727. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  12728. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12729. }
  12730. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12731. GGML_SYCL_DEBUG("call %s\n", __func__);
  12732. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  12733. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12734. }
  12735. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12736. GGML_SYCL_DEBUG("call %s\n", __func__);
  12737. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  12738. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12739. }
  12740. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12741. GGML_SYCL_DEBUG("call %s\n", __func__);
  12742. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  12743. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12744. }
  12745. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12746. GGML_SYCL_DEBUG("call %s\n", __func__);
  12747. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  12748. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12749. }
  12750. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12751. GGML_SYCL_DEBUG("call %s\n", __func__);
  12752. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  12753. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12754. }
  12755. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12756. GGML_SYCL_DEBUG("call %s\n", __func__);
  12757. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  12758. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12759. }
  12760. static void ggml_sycl_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12761. GGML_SYCL_DEBUG("call %s\n", __func__);
  12762. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardsigmoid);
  12763. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12764. }
  12765. static void ggml_sycl_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12766. GGML_SYCL_DEBUG("call %s\n", __func__);
  12767. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardswish);
  12768. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12769. }
  12770. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12771. GGML_SYCL_DEBUG("call %s\n", __func__);
  12772. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  12773. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12774. }
  12775. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12776. GGML_SYCL_DEBUG("call %s\n", __func__);
  12777. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  12778. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12779. }
  12780. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12781. GGML_SYCL_DEBUG("call %s\n", __func__);
  12782. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  12783. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12784. }
  12785. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12786. GGML_SYCL_DEBUG("call %s\n", __func__);
  12787. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  12788. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12789. }
  12790. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12791. GGML_SYCL_DEBUG("call %s\n", __func__);
  12792. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  12793. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12794. }
  12795. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12796. GGML_SYCL_DEBUG("call %s\n", __func__);
  12797. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  12798. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12799. }
  12800. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12801. GGML_SYCL_DEBUG("call %s\n", __func__);
  12802. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  12803. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12804. }
  12805. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12806. GGML_SYCL_DEBUG("call %s\n", __func__);
  12807. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  12808. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12809. }
  12810. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  12811. if (!g_sycl_loaded) return false;
  12812. const int64_t ne10 = src1->ne[0];
  12813. const int64_t ne0 = dst->ne[0];
  12814. const int64_t ne1 = dst->ne[1];
  12815. // TODO: find the optimal values for these
  12816. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  12817. src1->type == GGML_TYPE_F32 &&
  12818. dst->type == GGML_TYPE_F32 &&
  12819. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  12820. }
  12821. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  12822. const ggml_tensor *src1,
  12823. ggml_tensor *dst) try {
  12824. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  12825. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12826. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  12827. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  12828. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12829. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12830. const int64_t ne00 = src0->ne[0];
  12831. const int64_t ne01 = src0->ne[1];
  12832. const int64_t ne02 = src0->ne[2];
  12833. const int64_t ne12 = src1->ne[2];
  12834. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12835. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12836. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12837. void * src0_ddq = src0_extra->data_device[g_main_device];
  12838. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12839. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12840. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12841. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12842. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  12843. }
  12844. catch (sycl::exception const &exc) {
  12845. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12846. << ", line:" << __LINE__ << std::endl;
  12847. std::exit(1);
  12848. }
  12849. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  12850. const ggml_tensor *src1,
  12851. ggml_tensor *dst) try {
  12852. GGML_ASSERT(!ggml_is_transposed(src0));
  12853. GGML_ASSERT(!ggml_is_transposed(src1));
  12854. GGML_ASSERT(!ggml_is_permuted(src0));
  12855. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12856. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12857. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12858. const int64_t ne00 = src0->ne[0];
  12859. const int64_t ne01 = src0->ne[1];
  12860. const int64_t ne02 = src0->ne[2];
  12861. const int64_t nb01 = src0->nb[1];
  12862. const int64_t nb02 = src0->nb[2];
  12863. const int64_t ne12 = src1->ne[2];
  12864. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12865. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12866. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12867. void * src0_ddq = src0_extra->data_device[g_main_device];
  12868. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12869. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12870. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12871. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12872. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  12873. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  12874. 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);
  12875. }
  12876. catch (sycl::exception const &exc) {
  12877. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12878. << ", line:" << __LINE__ << std::endl;
  12879. std::exit(1);
  12880. }
  12881. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  12882. const sycl::half *src1_as_f16, char *dst,
  12883. const void **ptrs_src, void **ptrs_dst,
  12884. int64_t ne12, int64_t ne13, int64_t ne23,
  12885. size_t nb02, size_t nb03, size_t nb12,
  12886. size_t nb13, size_t nbd2, size_t nbd3,
  12887. int64_t r2, int64_t r3,
  12888. const sycl::nd_item<3> &item_ct1) {
  12889. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  12890. item_ct1.get_local_id(2);
  12891. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  12892. item_ct1.get_local_id(1);
  12893. if (i13 >= ne13 || i12 >= ne12) {
  12894. return;
  12895. }
  12896. int64_t i03 = i13 / r3;
  12897. int64_t i02 = i12 / r2;
  12898. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  12899. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  12900. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  12901. }
  12902. static void ggml_sycl_mul_mat_batched_sycl(const ggml_tensor *src0,
  12903. const ggml_tensor *src1,
  12904. ggml_tensor *dst) try {
  12905. GGML_ASSERT(!ggml_is_transposed(src0));
  12906. GGML_ASSERT(!ggml_is_transposed(src1));
  12907. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12908. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12909. GGML_TENSOR_BINARY_OP_LOCALS
  12910. const int64_t ne_dst = ggml_nelements(dst);
  12911. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12912. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12913. bool no_mixed_dtypes = main_stream->get_backend() == sycl::backend::ext_oneapi_cuda ||
  12914. main_stream->get_backend() == sycl::backend::ext_oneapi_hip;
  12915. SYCL_CHECK(
  12916. CHECK_TRY_ERROR(g_sycl_handles[g_main_device] = main_stream));
  12917. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12918. void * src0_ddq = src0_extra->data_device[g_main_device];
  12919. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  12920. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12921. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12922. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12923. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12924. // convert src1 to fp16
  12925. sycl_pool_alloc<sycl::half> src1_f16_alloc;
  12926. if (src1->type != GGML_TYPE_F16) {
  12927. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12928. const int64_t ne_src1 = ggml_nelements(src1);
  12929. src1_f16_alloc.alloc(ne_src1);
  12930. GGML_ASSERT(to_fp16_sycl != nullptr);
  12931. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  12932. }
  12933. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  12934. : src1_f16_alloc.get();
  12935. sycl_pool_alloc<sycl::half> dst_f16;
  12936. char * dst_t;
  12937. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_float;
  12938. dpct::library_data_t cu_data_type = dpct::library_data_t::real_float;
  12939. if (no_mixed_dtypes) {
  12940. cu_compute_type = dpct::library_data_t::real_half;
  12941. cu_data_type = dpct::library_data_t::real_half;
  12942. }
  12943. // dst strides
  12944. size_t nbd2 = dst->nb[2];
  12945. size_t nbd3 = dst->nb[3];
  12946. const float alpha_f32 = 1.0f;
  12947. const float beta_f32 = 0.0f;
  12948. const sycl::half alpha_f16 = 1.0f;
  12949. const sycl::half beta_f16 = 0.0f;
  12950. const void * alpha = &alpha_f32;
  12951. const void * beta = &beta_f32;
  12952. if (no_mixed_dtypes) {
  12953. alpha = &alpha_f16;
  12954. beta = &beta_f16;
  12955. }
  12956. // TODO: Renable (dst->op_params[0] =! GGML_PREC_DEFAULT) pathway
  12957. // when oneMKL open source supports half, half, float, float: datatypes
  12958. dst_t = (char *) dst_ddf;
  12959. if (no_mixed_dtypes) {
  12960. dst_t = (char *) dst_f16.alloc(ne_dst);
  12961. nbd2 /= sizeof(float) / sizeof(sycl::half);
  12962. nbd3 /= sizeof(float) / sizeof(sycl::half);
  12963. }
  12964. GGML_ASSERT(ne12 % ne02 == 0);
  12965. GGML_ASSERT(ne13 % ne03 == 0);
  12966. // broadcast factors
  12967. const int64_t r2 = ne12/ne02;
  12968. const int64_t r3 = ne13/ne03;
  12969. if (r2 == 1 && r3 == 1 && ggml_is_contiguous_2(src0) && ggml_is_contiguous_2(src1)) {
  12970. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  12971. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  12972. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  12973. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  12974. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  12975. nb01 / nb00, nb02 / nb00,
  12976. (const char *)src1_f16, dpct::library_data_t::real_half,
  12977. nb11 / nb10, nb12 / nb10, beta,
  12978. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  12979. ne12 * ne13, cu_compute_type)));
  12980. } else {
  12981. const int ne23 = ne12*ne13;
  12982. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  12983. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  12984. sycl::range<3> block_dims(1, ne12, ne13);
  12985. /*
  12986. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  12987. the limit. To get the device limit, query
  12988. info::device::max_work_group_size. Adjust the work-group size if needed.
  12989. */
  12990. {
  12991. dpct::has_capability_or_fail(main_stream->get_device(),
  12992. {sycl::aspect::fp16});
  12993. main_stream->submit([&](sycl::handler &cgh) {
  12994. const void **ptrs_src_get = ptrs_src.get();
  12995. void **ptrs_dst_get = ptrs_dst.get();
  12996. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  12997. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  12998. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  12999. [=](sycl::nd_item<3> item_ct1) {
  13000. k_compute_batched_ptrs(
  13001. src0_as_f16, src1_f16,
  13002. dst_t, ptrs_src_get,
  13003. ptrs_dst_get, ne12, ne13, ne23,
  13004. nb02, nb03, nb12_scaled, nb13_scaled,
  13005. nbd2, nbd3, r2, r3, item_ct1);
  13006. });
  13007. });
  13008. }
  13009. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  13010. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  13011. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  13012. (const void **)(ptrs_src.get() + 0 * ne23),
  13013. dpct::library_data_t::real_half, nb01 / nb00,
  13014. (const void **)(ptrs_src.get() + 1 * ne23),
  13015. dpct::library_data_t::real_half, nb11 / nb10, beta,
  13016. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  13017. cu_compute_type)));
  13018. }
  13019. if (no_mixed_dtypes) {
  13020. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13021. to_fp32_sycl(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  13022. }
  13023. }
  13024. catch (sycl::exception const &exc) {
  13025. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13026. << ", line:" << __LINE__ << std::endl;
  13027. std::exit(1);
  13028. }
  13029. inline bool ggml_sycl_supports_mmq(enum ggml_type type) {
  13030. // TODO: accuracy issues in MMQ
  13031. return false;
  13032. }
  13033. bool ggml_sycl_supports_dmmv(enum ggml_type type) {
  13034. switch (type) {
  13035. case GGML_TYPE_Q4_0:
  13036. case GGML_TYPE_Q4_1:
  13037. case GGML_TYPE_Q5_0:
  13038. case GGML_TYPE_Q5_1:
  13039. case GGML_TYPE_Q8_0:
  13040. case GGML_TYPE_Q2_K:
  13041. case GGML_TYPE_Q3_K:
  13042. case GGML_TYPE_Q4_K:
  13043. case GGML_TYPE_Q5_K:
  13044. case GGML_TYPE_Q6_K:
  13045. case GGML_TYPE_F16:
  13046. return true;
  13047. default:
  13048. return false;
  13049. }
  13050. }
  13051. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13052. const bool all_on_device =
  13053. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  13054. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  13055. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  13056. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  13057. int64_t min_compute_capability = INT_MAX;
  13058. for (int i = 0; i < g_device_count; ++i) {
  13059. 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)) {
  13060. min_compute_capability = g_device_caps[i].cc;
  13061. }
  13062. }
  13063. // check data types and tensor shapes for custom matrix multiplication kernels:
  13064. bool use_dequantize_mul_mat_vec = ggml_sycl_supports_dmmv(src0->type)
  13065. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  13066. && src0->ne[0] % GGML_SYCL_DMMV_X == 0 && src1->ne[1] == 1;
  13067. bool use_mul_mat_vec_q = ggml_is_quantized(src0->type)
  13068. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32
  13069. && src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
  13070. bool use_mul_mat_q = ggml_sycl_supports_mmq(src0->type)
  13071. && src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
  13072. // mmvq and mmq need the __dp4a instruction which is available for gen12+
  13073. // Workaround in https://github.com/ggerganov/llama.cpp/commit/95f84d5ce8b449a9b16009434aca800df504a02e
  13074. use_mul_mat_q = use_mul_mat_q && (src0->type != GGML_TYPE_IQ2_XXS);
  13075. #ifdef SYCL_USE_XMX
  13076. use_mul_mat_q = use_mul_mat_q && (src1->ne[1] <= MMQ_MAX_BATCH_SIZE);
  13077. #endif // SYCL_USE_XMX
  13078. if (!split && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  13079. // KQ single-batch
  13080. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  13081. } else if (!split && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  13082. // KQV single-batch
  13083. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  13084. } else if (!split && src0->type == GGML_TYPE_F16 && (src1->type == GGML_TYPE_F16) && !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && src1->ne[2]*src1->ne[3] > 1) {
  13085. // KQ + KQV multi-batch
  13086. ggml_sycl_mul_mat_batched_sycl(src0, src1, dst);
  13087. } else if (use_dequantize_mul_mat_vec) {
  13088. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  13089. } else if (use_mul_mat_vec_q) {
  13090. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  13091. } else if (use_mul_mat_q) {
  13092. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  13093. } else {
  13094. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13095. }
  13096. }
  13097. #if 0
  13098. template<typename ... Srcs>
  13099. static __global__ void k_compute_batched_ptrs_id(
  13100. const void ** ptrs_src, void ** ptrs_dst,
  13101. int ne12, int ne13,
  13102. int ne23,
  13103. int nb02, int nb03,
  13104. int nb12, int nb13,
  13105. int nb2, int nb3,
  13106. int r2, int r3,
  13107. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  13108. const half * src1_f16, half * dst_f16,
  13109. const int32_t * ids, const int id,
  13110. Srcs... src0s) {
  13111. int i = ids[id];
  13112. half * src0_f16;
  13113. const void * srcs_ar[] = { (const half *) src0s... };
  13114. if (src0_type == GGML_TYPE_F16) {
  13115. src0_f16 = (half *) srcs_ar[i];
  13116. } else {
  13117. src0_f16 = src0_as_f16;
  13118. if (item_ct1.get_local_id(2) == 0 && threadIdx.y == 0) {
  13119. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  13120. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  13121. }
  13122. }
  13123. int i13 = blockIdx.x * blockDim.x + item_ct1.get_local_id(2);
  13124. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  13125. if (i13 >= ne13 || i12 >= ne12) {
  13126. return;
  13127. }
  13128. int i03 = i13 / r3;
  13129. int i02 = i12 / r2;
  13130. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  13131. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  13132. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  13133. }
  13134. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  13135. const struct ggml_tensor * ids = dst->src[0];
  13136. const struct ggml_tensor * src1 = dst->src[1];
  13137. const struct ggml_tensor * src00 = dst->src[2];
  13138. const int id = dst->op_params[0];
  13139. GGML_ASSERT(!ggml_is_transposed(src00));
  13140. GGML_ASSERT(!ggml_is_transposed(src1));
  13141. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13142. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13143. GGML_TENSOR_LOCALS(int64_t, ne0, src00, ne);
  13144. //const int64_t nb01 = src00->nb[1];
  13145. GGML_TENSOR_LOCALS(int64_t, nb0, src00, nb);
  13146. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  13147. GGML_TENSOR_LOCALS(int64_t, nb1, src1, nb);
  13148. //const int64_t nb11 = src1->nb[1];
  13149. const int64_t ne1 = ggml_nelements(src1);
  13150. const int64_t ne = ggml_nelements(dst);
  13151. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13152. syclStream_t main_stream = g_syclStreams[g_main_device][0];
  13153. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device], main_stream));
  13154. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13155. //void * src0_ddq = src0_extra->data_device[g_main_device];
  13156. //half * src0_as_f16 = (half *) src0_ddq;
  13157. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13158. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13159. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13160. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13161. // convert src1 to fp16
  13162. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13163. GGML_ASSERT(to_fp16_sycl != nullptr);
  13164. size_t src1_as = 0;
  13165. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne1 * sizeof(half), &src1_as);
  13166. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  13167. size_t dst_as = 0;
  13168. half * dst_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne * sizeof(half), &dst_as);
  13169. GGML_ASSERT(ne12 % ne02 == 0);
  13170. GGML_ASSERT(ne13 % ne03 == 0);
  13171. // broadcast factors
  13172. const int64_t r2 = ne12/ne02;
  13173. const int64_t r3 = ne13/ne03;
  13174. const half alpha_f16 = 1.0f;
  13175. const half beta_f16 = 0.0f;
  13176. // use syclGemmBatchedEx
  13177. const int ne23 = ne12*ne13;
  13178. const void ** ptrs_src = nullptr;
  13179. void ** ptrs_dst = nullptr;
  13180. size_t ptrs_src_s = 0;
  13181. size_t ptrs_dst_s = 0;
  13182. ptrs_src = (const void **) ggml_sycl_pool_malloc(g_main_device, 2*ne23*sizeof(void *), &ptrs_src_s);
  13183. ptrs_dst = ( void **) ggml_sycl_pool_malloc(g_main_device, 1*ne23*sizeof(void *), &ptrs_dst_s);
  13184. int64_t src0_ne = ggml_nelements(src00);
  13185. half * src0_as_f16 = nullptr;
  13186. size_t src0_as = 0;
  13187. if (src00->type != GGML_TYPE_F16) {
  13188. src0_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, src0_ne * sizeof(half), &src0_as);
  13189. }
  13190. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  13191. dim3 block_dims(ne13, ne12);
  13192. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  13193. ptrs_src, ptrs_dst,
  13194. ne12, ne13,
  13195. ne23,
  13196. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  13197. nb12, nb13,
  13198. dst->nb[2], dst->nb[3],
  13199. r2, r3,
  13200. src00->type, src0_as_f16, src0_ne,
  13201. src1_as_f16, dst_f16,
  13202. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  13203. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  13204. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  13205. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  13206. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  13207. );
  13208. SYCL_CHECK(syclGetLastError());
  13209. SYCL_CHECK(
  13210. syclGemmBatchedEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  13211. ne01, ne11, ne10,
  13212. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  13213. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  13214. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  13215. ne23,
  13216. CUBLAS_COMPUTE_16F,
  13217. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  13218. if (src0_as != 0) {
  13219. ggml_sycl_pool_free(g_main_device, src0_as_f16, src0_as);
  13220. }
  13221. if (ptrs_src_s != 0) {
  13222. ggml_sycl_pool_free(g_main_device, ptrs_src, ptrs_src_s);
  13223. }
  13224. if (ptrs_dst_s != 0) {
  13225. ggml_sycl_pool_free(g_main_device, ptrs_dst, ptrs_dst_s);
  13226. }
  13227. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13228. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  13229. ggml_sycl_pool_free(g_main_device, src1_as_f16, src1_as);
  13230. ggml_sycl_pool_free(g_main_device, dst_f16, dst_as);
  13231. }
  13232. #endif
  13233. struct mmid_row_mapping {
  13234. int32_t i1;
  13235. int32_t i2;
  13236. };
  13237. __dpct_inline__ static void k_copy_src1_to_contiguous(
  13238. const char *__restrict__ src1_original, char *__restrict__ src1_contiguous,
  13239. int *__restrict__ cur_src1_row, mmid_row_mapping *__restrict__ row_mapping,
  13240. const char *__restrict ids, int64_t i02, size_t ids_nb1, size_t ids_nb0,
  13241. int64_t ne11, int64_t ne10, size_t nb11, size_t nb12,
  13242. const sycl::nd_item<3> &item_ct1, int &src1_row) {
  13243. int32_t iid1 = item_ct1.get_group(2);
  13244. int32_t id = item_ct1.get_group(1);
  13245. const int32_t row_id_i = *(const int32_t *) (ids + iid1*ids_nb1 + id*ids_nb0);
  13246. if (row_id_i != i02) {
  13247. return;
  13248. }
  13249. const int64_t i11 = id % ne11;
  13250. const int64_t i12 = iid1;
  13251. if (item_ct1.get_local_id(2) == 0) {
  13252. src1_row =
  13253. dpct::atomic_fetch_add<sycl::access::address_space::generic_space>(
  13254. cur_src1_row, 1);
  13255. row_mapping[src1_row] = {id, iid1};
  13256. }
  13257. /*
  13258. DPCT1065:194: Consider replacing sycl::nd_item::barrier() with
  13259. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
  13260. performance if there is no access to global memory.
  13261. */
  13262. item_ct1.barrier();
  13263. const float * src1_row_original = (const float *)(src1_original + i11*nb11 + i12*nb12);
  13264. float * src1_row_contiguous = (float *)(src1_contiguous + src1_row*nb11);
  13265. #pragma unroll
  13266. for (int i = item_ct1.get_local_id(2); i < ne10;
  13267. i += item_ct1.get_local_range(2)) {
  13268. src1_row_contiguous[i] = src1_row_original[i];
  13269. }
  13270. }
  13271. __dpct_inline__ static void k_copy_dst_from_contiguous(
  13272. char *__restrict__ dst_original, const char *__restrict__ dst_contiguous,
  13273. const mmid_row_mapping *__restrict__ row_mapping, int64_t ne0, size_t nb1,
  13274. size_t nb2, const sycl::nd_item<3> &item_ct1) {
  13275. int32_t i = item_ct1.get_group(2);
  13276. const int32_t i1 = row_mapping[i].i1;
  13277. const int32_t i2 = row_mapping[i].i2;
  13278. const float * dst_row_contiguous = (const float *)(dst_contiguous + i*nb1);
  13279. float * dst_row_original = (float *)(dst_original + i1*nb1 + i2*nb2);
  13280. #pragma unroll
  13281. for (int j = item_ct1.get_local_id(2); j < ne0;
  13282. j += item_ct1.get_local_range(2)) {
  13283. dst_row_original[j] = dst_row_contiguous[j];
  13284. }
  13285. }
  13286. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  13287. const ggml_tensor *src1,
  13288. ggml_tensor *dst) try {
  13289. GGML_ASSERT(!ggml_backend_buffer_is_sycl_split(src0->buffer) && "mul_mat_id does not support split buffers");
  13290. const ggml_tensor *ids = dst->src[2];
  13291. GGML_TENSOR_BINARY_OP_LOCALS
  13292. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13293. const int64_t n_as = ne02;
  13294. const int64_t n_ids = ids->ne[0];
  13295. std::vector<char> ids_host(ggml_nbytes(ids));
  13296. const char * ids_dev = (const char *) ids->data;
  13297. SYCL_CHECK(CHECK_TRY_ERROR(
  13298. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
  13299. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13300. const ggml_tensor_extra_gpu *src0_extra =
  13301. (const ggml_tensor_extra_gpu *)src0->extra;
  13302. const ggml_tensor_extra_gpu *src1_extra =
  13303. (const ggml_tensor_extra_gpu *)src1->extra;
  13304. const ggml_tensor_extra_gpu *dst_extra =
  13305. (const ggml_tensor_extra_gpu *)dst->extra;
  13306. ggml_tensor_extra_gpu src0_row_extra;
  13307. ggml_tensor_extra_gpu src1_row_extra;
  13308. ggml_tensor_extra_gpu dst_row_extra;
  13309. ggml_tensor src0_row = *src0;
  13310. ggml_tensor src1_row = *src1;
  13311. ggml_tensor dst_row = *dst;
  13312. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  13313. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  13314. src0_row.extra = &src0_row_extra;
  13315. src1_row.extra = &src1_row_extra;
  13316. dst_row.extra = &dst_row_extra;
  13317. char *src0_original = src1->backend == GGML_BACKEND_TYPE_CPU
  13318. ? (char *)src0->data
  13319. : (char *)src0_extra->data_device[g_main_device];
  13320. char *src1_original = src1->backend == GGML_BACKEND_TYPE_CPU
  13321. ? (char *)src1->data
  13322. : (char *)src1_extra->data_device[g_main_device];
  13323. char *dst_original = dst->backend == GGML_BACKEND_TYPE_CPU
  13324. ? (char *)dst->data
  13325. : (char *)dst_extra->data_device[g_main_device];
  13326. src0_row.ne[2] = 1;
  13327. src0_row.ne[3] = 1;
  13328. src0_row.nb[3] = nb02;
  13329. src1_row.ne[1] = 1;
  13330. src1_row.ne[2] = 1;
  13331. src1_row.ne[3] = 1;
  13332. src1_row.nb[2] = nb11;
  13333. src1_row.nb[3] = nb11;
  13334. dst_row.ne[1] = 1;
  13335. dst_row.ne[2] = 1;
  13336. dst_row.ne[3] = 1;
  13337. dst_row.nb[2] = nb1;
  13338. dst_row.nb[3] = nb1;
  13339. if (ne12 == 1) {
  13340. for (int64_t iid1 = 0; iid1 < ids->ne[1]; iid1++) {
  13341. for (int64_t id = 0; id < n_ids; id++) {
  13342. const int32_t i02 = *(const int32_t *) (ids_host.data() + iid1*ids->nb[1] + id*ids->nb[0]);
  13343. GGML_ASSERT(i02 >= 0 && i02 < n_as);
  13344. const int64_t i11 = id % ne11;
  13345. const int64_t i12 = iid1;
  13346. const int64_t i1 = id;
  13347. const int64_t i2 = i12;
  13348. src0_row_extra.data_device[g_main_device] =
  13349. src0_original + i02*nb02;
  13350. src1_row_extra.data_device[g_main_device] =
  13351. src1_original + + i11*nb11 + i12*nb12;
  13352. dst_row_extra.data_device[g_main_device] =
  13353. dst_original + i1*nb1 + i2*nb2;
  13354. ggml_sycl_mul_mat(&src0_row, &src1_row, &dst_row);
  13355. }
  13356. }
  13357. } else {
  13358. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  13359. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  13360. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  13361. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  13362. for (int64_t i02 = 0; i02 < n_as; i02++) {
  13363. int64_t num_src1_rows = 0;
  13364. for (int64_t iid1 = 0; iid1 < ids->ne[1]; iid1++) {
  13365. for (int64_t id = 0; id < n_ids; id++) {
  13366. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + iid1*ids->nb[1] + id*ids->nb[0]);
  13367. GGML_ASSERT(row_id_i >= 0 && row_id_i < n_as);
  13368. if (row_id_i != i02) {
  13369. continue;
  13370. }
  13371. num_src1_rows++;
  13372. }
  13373. }
  13374. if (num_src1_rows == 0) {
  13375. continue;
  13376. }
  13377. sycl_pool_alloc<int> dev_cur_src1_row(1);
  13378. sycl_pool_alloc<mmid_row_mapping> dev_row_mapping(num_src1_rows);
  13379. SYCL_CHECK(CHECK_TRY_ERROR(
  13380. stream->memset(dev_cur_src1_row.get(), 0, sizeof(int))));
  13381. {
  13382. sycl::range<3> block_dims(1, 1, std::min((unsigned int)ne10, 768u));
  13383. sycl::range<3> grid_dims(1, n_ids, ids->ne[1]);
  13384. stream->submit([&](sycl::handler &cgh) {
  13385. sycl::local_accessor<int, 0> src1_row_acc(cgh);
  13386. char *__restrict src1_contiguous_get =
  13387. src1_contiguous.get();
  13388. int *__restrict dev_cur_src1_row_get =
  13389. dev_cur_src1_row.get();
  13390. mmid_row_mapping *__restrict dev_row_mapping_get =
  13391. dev_row_mapping.get();
  13392. size_t ids_nb_ct6 = ids->nb[1];
  13393. size_t ids_nb_ct7 = ids->nb[0];
  13394. cgh.parallel_for(
  13395. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  13396. [=](sycl::nd_item<3> item_ct1) {
  13397. k_copy_src1_to_contiguous(
  13398. src1_original, src1_contiguous_get,
  13399. dev_cur_src1_row_get,
  13400. dev_row_mapping_get, ids_dev, i02,
  13401. ids_nb_ct6, ids_nb_ct7, ne11, ne10, nb11, nb12,
  13402. item_ct1, src1_row_acc);
  13403. });
  13404. });
  13405. }
  13406. src0_row_extra.data_device[g_main_device] = src0_original + i02*nb02;
  13407. GGML_ASSERT(nb11 == sizeof(float)*ne10);
  13408. GGML_ASSERT(nb1 == sizeof(float)*ne0);
  13409. src1_row.ne[1] = num_src1_rows;
  13410. src1_row.nb[1] = nb11;
  13411. src1_row.nb[2] = num_src1_rows*nb11;
  13412. src1_row.nb[3] = num_src1_rows*nb11;
  13413. dst_row.ne[1] = num_src1_rows;
  13414. dst_row.nb[1] = nb1;
  13415. dst_row.nb[2] = num_src1_rows*nb1;
  13416. dst_row.nb[3] = num_src1_rows*nb1;
  13417. ggml_sycl_mul_mat(&src0_row, &src1_row, &dst_row);
  13418. {
  13419. sycl::range<3> block_dims(1, 1, std::min((unsigned int)ne0, 768u));
  13420. sycl::range<3> grid_dims(1, 1, num_src1_rows);
  13421. stream->submit([&](sycl::handler &cgh) {
  13422. const char *__restrict dst_contiguous_get =
  13423. dst_contiguous.get();
  13424. const mmid_row_mapping *__restrict dev_row_mapping_get =
  13425. dev_row_mapping.get();
  13426. cgh.parallel_for(
  13427. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  13428. [=](sycl::nd_item<3> item_ct1) {
  13429. k_copy_dst_from_contiguous(dst_original,
  13430. dst_contiguous_get,
  13431. dev_row_mapping_get,
  13432. ne0, nb1, nb2, item_ct1);
  13433. });
  13434. });
  13435. }
  13436. }
  13437. }
  13438. }
  13439. catch (sycl::exception const &exc) {
  13440. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13441. << ", line:" << __LINE__ << std::endl;
  13442. std::exit(1);
  13443. }
  13444. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13445. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  13446. }
  13447. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13448. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  13449. }
  13450. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  13451. ggml_tensor *dst) try {
  13452. const int64_t ne = ggml_nelements(src0);
  13453. GGML_ASSERT(ne == ggml_nelements(src1));
  13454. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  13455. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13456. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  13457. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  13458. GGML_TENSOR_BINARY_OP_LOCALS;
  13459. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13460. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13461. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13462. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13463. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13464. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  13465. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  13466. 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);
  13467. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  13468. 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);
  13469. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  13470. 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);
  13471. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  13472. 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);
  13473. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  13474. 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);
  13475. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  13476. 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);
  13477. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  13478. 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);
  13479. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  13480. 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);
  13481. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  13482. 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);
  13483. } else {
  13484. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  13485. ggml_type_name(src0->type), ggml_type_name(src1->type));
  13486. GGML_ASSERT(false);
  13487. }
  13488. (void) dst;
  13489. }
  13490. catch (sycl::exception const &exc) {
  13491. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13492. << ", line:" << __LINE__ << std::endl;
  13493. std::exit(1);
  13494. }
  13495. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13496. // TODO: why do we pass dst as src1 here?
  13497. ggml_sycl_cpy(src0, dst, nullptr);
  13498. (void) src1;
  13499. }
  13500. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13501. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  13502. }
  13503. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13504. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  13505. }
  13506. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13507. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  13508. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  13509. }
  13510. static void ggml_sycl_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13511. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pool2d);
  13512. }
  13513. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13514. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  13515. }
  13516. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13517. GGML_ASSERT(ggml_is_contiguous(src0));
  13518. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  13519. }
  13520. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13521. GGML_ASSERT(ggml_is_contiguous(src0));
  13522. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  13523. }
  13524. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13525. (void) src0;
  13526. (void) src1;
  13527. (void) dst;
  13528. }
  13529. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  13530. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  13531. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  13532. }
  13533. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  13534. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_TYPE_GPU && tensor->backend != GGML_BACKEND_TYPE_GPU_SPLIT) ) {
  13535. return;
  13536. }
  13537. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13538. for (int i = 0; i < g_device_count; ++i) {
  13539. const dpct::queue_ptr stream = g_syclStreams[i][0];
  13540. if (extra->data_device[i] != nullptr) {
  13541. SYCL_CHECK(ggml_sycl_set_device(i));
  13542. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[i], *stream)));
  13543. }
  13544. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  13545. if (extra->events[i][is] != nullptr) {
  13546. SYCL_CHECK(ggml_sycl_set_device(i));
  13547. SYCL_CHECK(CHECK_TRY_ERROR(
  13548. dpct::destroy_event(extra->events[i][is])));
  13549. }
  13550. }
  13551. }
  13552. delete extra;
  13553. }
  13554. catch (sycl::exception const &exc) {
  13555. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13556. << ", line:" << __LINE__ << std::endl;
  13557. std::exit(1);
  13558. }
  13559. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  13560. static size_t g_temp_tensor_extra_index = 0;
  13561. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13562. if (g_temp_tensor_extras == nullptr) {
  13563. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13564. }
  13565. size_t alloc_index = g_temp_tensor_extra_index;
  13566. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13567. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  13568. memset(extra, 0, sizeof(*extra));
  13569. return extra;
  13570. }
  13571. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  13572. bool scratch, bool force_inplace,
  13573. bool no_alloc) try {
  13574. if (scratch && g_scratch_size == 0) {
  13575. return;
  13576. }
  13577. tensor->backend = GGML_BACKEND_TYPE_GPU;
  13578. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_CPU) {
  13579. const ggml_op src0_op = tensor->src[0]->op;
  13580. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  13581. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  13582. }
  13583. }
  13584. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_TYPE_CPU) {
  13585. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  13586. }
  13587. if (scratch && no_alloc) {
  13588. return;
  13589. }
  13590. ggml_tensor_extra_gpu * extra;
  13591. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  13592. tensor->op == GGML_OP_VIEW ||
  13593. force_inplace;
  13594. const size_t size = ggml_nbytes(tensor);
  13595. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13596. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13597. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT)) {
  13598. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  13599. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13600. size_t offset = 0;
  13601. if (tensor->op == GGML_OP_VIEW) {
  13602. memcpy(&offset, tensor->op_params, sizeof(size_t));
  13603. }
  13604. extra = ggml_sycl_alloc_temp_tensor_extra();
  13605. extra->data_device[g_main_device] = src0_ddc + offset;
  13606. } else if (tensor->op == GGML_OP_CPY) {
  13607. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  13608. void * src1_ddv = src1_extra->data_device[g_main_device];
  13609. extra = ggml_sycl_alloc_temp_tensor_extra();
  13610. extra->data_device[g_main_device] = src1_ddv;
  13611. } else if (scratch) {
  13612. GGML_ASSERT(size <= g_scratch_size);
  13613. if (g_scratch_offset + size > g_scratch_size) {
  13614. g_scratch_offset = 0;
  13615. }
  13616. char * data = (char *) g_scratch_buffer;
  13617. if (data == nullptr) {
  13618. SYCL_CHECK(CHECK_TRY_ERROR(
  13619. data = (char *)sycl::malloc_device(
  13620. g_scratch_size, *stream)));
  13621. g_scratch_buffer = data;
  13622. }
  13623. extra = ggml_sycl_alloc_temp_tensor_extra();
  13624. extra->data_device[g_main_device] = data + g_scratch_offset;
  13625. g_scratch_offset += size;
  13626. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  13627. } else { // allocate new buffers outside of scratch
  13628. void * data;
  13629. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  13630. size, *stream)));
  13631. SYCL_CHECK(CHECK_TRY_ERROR(
  13632. (*stream).memset(data, 0, size).wait()));
  13633. extra = new ggml_tensor_extra_gpu;
  13634. memset(extra, 0, sizeof(*extra));
  13635. extra->data_device[g_main_device] = data;
  13636. }
  13637. tensor->extra = extra;
  13638. }
  13639. catch (sycl::exception const &exc) {
  13640. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13641. << ", line:" << __LINE__ << std::endl;
  13642. std::exit(1);
  13643. }
  13644. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  13645. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13646. GGML_ASSERT(ggml_is_contiguous(tensor));
  13647. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13648. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13649. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13650. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  13651. .memcpy(extra->data_device[g_main_device],
  13652. tensor->data, ggml_nbytes(tensor))
  13653. .wait()));
  13654. }
  13655. catch (sycl::exception const &exc) {
  13656. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13657. << ", line:" << __LINE__ << std::endl;
  13658. std::exit(1);
  13659. }
  13660. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  13661. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  13662. }
  13663. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  13664. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  13665. }
  13666. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  13667. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  13668. }
  13669. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  13670. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  13671. }
  13672. void ggml_sycl_set_main_device(const int main_device) try {
  13673. if (g_main_device == main_device) return;
  13674. check_allow_gpu_index(main_device);
  13675. g_main_device = main_device;
  13676. g_main_device_id = g_sycl_gpu_mgr->gpus[main_device];
  13677. if (g_ggml_sycl_debug) {
  13678. dpct::device_info prop;
  13679. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  13680. prop, dpct::dev_mgr::instance().get_device(g_main_device_id))));
  13681. fprintf(stderr, "Using device %d (%s) as main device\n",
  13682. g_main_device_id, prop.get_name());
  13683. }
  13684. }
  13685. catch (sycl::exception const &exc) {
  13686. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13687. << ", line:" << __LINE__ << std::endl;
  13688. std::exit(1);
  13689. }
  13690. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  13691. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  13692. // it still won't always work as expected, but it's better than nothing
  13693. if (scratch_size > g_scratch_size) {
  13694. ggml_sycl_free_scratch();
  13695. }
  13696. g_scratch_size = std::max(g_scratch_size, scratch_size);
  13697. }
  13698. void ggml_sycl_free_scratch() try {
  13699. if (g_scratch_buffer == nullptr) {
  13700. return;
  13701. }
  13702. ggml_sycl_set_device(g_main_device);
  13703. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13704. SYCL_CHECK(CHECK_TRY_ERROR(
  13705. sycl::free(g_scratch_buffer, *stream)));
  13706. g_scratch_buffer = nullptr;
  13707. }
  13708. catch (sycl::exception const &exc) {
  13709. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13710. << ", line:" << __LINE__ << std::endl;
  13711. std::exit(1);
  13712. }
  13713. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  13714. if (!g_sycl_loaded) return false;
  13715. ggml_sycl_func_t func;
  13716. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  13717. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  13718. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  13719. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  13720. return false;
  13721. }
  13722. if (tensor->op == GGML_OP_MUL_MAT) {
  13723. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  13724. #ifndef NDEBUG
  13725. 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]);
  13726. #endif
  13727. return false;
  13728. }
  13729. }
  13730. switch (tensor->op) {
  13731. case GGML_OP_REPEAT:
  13732. func = ggml_sycl_repeat;
  13733. break;
  13734. case GGML_OP_GET_ROWS:
  13735. func = ggml_sycl_get_rows;
  13736. break;
  13737. case GGML_OP_DUP:
  13738. func = ggml_sycl_dup;
  13739. break;
  13740. case GGML_OP_ADD:
  13741. func = ggml_sycl_add;
  13742. break;
  13743. case GGML_OP_ACC:
  13744. func = ggml_sycl_acc;
  13745. break;
  13746. case GGML_OP_MUL:
  13747. func = ggml_sycl_mul;
  13748. break;
  13749. case GGML_OP_DIV:
  13750. func = ggml_sycl_div;
  13751. break;
  13752. case GGML_OP_UNARY:
  13753. switch (ggml_get_unary_op(tensor)) {
  13754. case GGML_UNARY_OP_GELU:
  13755. func = ggml_sycl_gelu;
  13756. break;
  13757. case GGML_UNARY_OP_SILU:
  13758. func = ggml_sycl_silu;
  13759. break;
  13760. case GGML_UNARY_OP_GELU_QUICK:
  13761. func = ggml_sycl_gelu_quick;
  13762. break;
  13763. case GGML_UNARY_OP_TANH:
  13764. func = ggml_sycl_tanh;
  13765. break;
  13766. case GGML_UNARY_OP_RELU:
  13767. func = ggml_sycl_relu;
  13768. break;
  13769. case GGML_UNARY_OP_HARDSIGMOID:
  13770. func = ggml_sycl_hardsigmoid;
  13771. break;
  13772. case GGML_UNARY_OP_HARDSWISH:
  13773. func = ggml_sycl_hardswish;
  13774. break;
  13775. default:
  13776. return false;
  13777. }
  13778. break;
  13779. case GGML_OP_NORM:
  13780. func = ggml_sycl_norm;
  13781. break;
  13782. case GGML_OP_GROUP_NORM:
  13783. func = ggml_sycl_group_norm;
  13784. break;
  13785. case GGML_OP_CONCAT:
  13786. func = ggml_sycl_concat;
  13787. break;
  13788. case GGML_OP_UPSCALE:
  13789. func = ggml_sycl_upscale;
  13790. break;
  13791. case GGML_OP_PAD:
  13792. func = ggml_sycl_pad;
  13793. break;
  13794. case GGML_OP_LEAKY_RELU:
  13795. func = ggml_sycl_leaky_relu;
  13796. break;
  13797. case GGML_OP_RMS_NORM:
  13798. func = ggml_sycl_rms_norm;
  13799. break;
  13800. case GGML_OP_MUL_MAT:
  13801. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  13802. return false;
  13803. }
  13804. func = ggml_sycl_mul_mat;
  13805. break;
  13806. case GGML_OP_MUL_MAT_ID:
  13807. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  13808. return false;
  13809. }
  13810. func = ggml_sycl_mul_mat_id;
  13811. break;
  13812. case GGML_OP_SCALE:
  13813. func = ggml_sycl_scale;
  13814. break;
  13815. case GGML_OP_SQR:
  13816. func = ggml_sycl_sqr;
  13817. break;
  13818. case GGML_OP_CLAMP:
  13819. func = ggml_sycl_clamp;
  13820. break;
  13821. case GGML_OP_CPY:
  13822. func = ggml_sycl_cpy;
  13823. break;
  13824. case GGML_OP_CONT:
  13825. func = ggml_sycl_dup;
  13826. break;
  13827. case GGML_OP_NONE:
  13828. case GGML_OP_RESHAPE:
  13829. case GGML_OP_VIEW:
  13830. case GGML_OP_PERMUTE:
  13831. case GGML_OP_TRANSPOSE:
  13832. func = ggml_sycl_nop;
  13833. break;
  13834. case GGML_OP_DIAG_MASK_INF:
  13835. func = ggml_sycl_diag_mask_inf;
  13836. break;
  13837. case GGML_OP_SOFT_MAX:
  13838. func = ggml_sycl_soft_max;
  13839. break;
  13840. case GGML_OP_ROPE:
  13841. func = ggml_sycl_rope;
  13842. break;
  13843. case GGML_OP_IM2COL:
  13844. func = ggml_sycl_im2col;
  13845. break;
  13846. case GGML_OP_POOL_2D:
  13847. func = ggml_sycl_pool2d;
  13848. break;
  13849. case GGML_OP_SUM_ROWS:
  13850. func = ggml_sycl_sum_rows;
  13851. break;
  13852. case GGML_OP_ARGSORT:
  13853. func = ggml_sycl_argsort;
  13854. break;
  13855. default:
  13856. return false;
  13857. }
  13858. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  13859. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  13860. }
  13861. if (params->ith != 0) {
  13862. return true;
  13863. }
  13864. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  13865. return true;
  13866. }
  13867. func(tensor->src[0], tensor->src[1], tensor);
  13868. return true;
  13869. }
  13870. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  13871. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_gpu_list\n");
  13872. for(int i=0;i<max_len;i++) id_list[i] = -1;
  13873. if (!g_sycl_gpu_mgr) {
  13874. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  13875. }
  13876. for (int i=0;i< g_sycl_gpu_mgr->gpus.size();i++){
  13877. if (i>=max_len) break;
  13878. id_list[i] = g_sycl_gpu_mgr->gpus[i];
  13879. }
  13880. return;
  13881. }
  13882. catch (sycl::exception const &exc) {
  13883. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13884. << ", line:" << __LINE__ << std::endl;
  13885. std::exit(1);
  13886. }
  13887. int ggml_sycl_get_device_count() try {
  13888. int device_count;
  13889. if (CHECK_TRY_ERROR(device_count =
  13890. dpct::dev_mgr::instance().device_count()) != 0) {
  13891. return 0;
  13892. }
  13893. return device_count;
  13894. }
  13895. catch (sycl::exception const &exc) {
  13896. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13897. << ", line:" << __LINE__ << std::endl;
  13898. std::exit(1);
  13899. }
  13900. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  13901. size_t description_size) try {
  13902. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_device_description\n");
  13903. dpct::device_info prop;
  13904. int device_id = g_sycl_gpu_mgr->gpus[device];
  13905. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  13906. prop, dpct::dev_mgr::instance().get_device(device_id))));
  13907. snprintf(description, description_size, "%s", prop.get_name());
  13908. }
  13909. catch (sycl::exception const &exc) {
  13910. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13911. << ", line:" << __LINE__ << std::endl;
  13912. std::exit(1);
  13913. }
  13914. GGML_CALL void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  13915. size_t *total) try {
  13916. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_memory\n");
  13917. ggml_sycl_set_device(device);
  13918. /*
  13919. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  13920. error codes. The original code was commented out and a warning string was
  13921. inserted. You need to rewrite this code.
  13922. */
  13923. /*
  13924. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  13925. device information which may not be supported by all compilers or runtimes.
  13926. You may need to adjust the code.
  13927. */
  13928. int device_id = g_sycl_gpu_mgr->gpus[device];
  13929. SYCL_CHECK(CHECK_TRY_ERROR(
  13930. dpct::dev_mgr::instance().get_device(device_id).get_memory_info(*free, *total)));
  13931. }
  13932. catch (sycl::exception const &exc) {
  13933. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13934. << ", line:" << __LINE__ << std::endl;
  13935. std::exit(1);
  13936. }
  13937. ////////////////////////////////////////////////////////////////////////////////
  13938. // backend interface
  13939. #define UNUSED GGML_UNUSED
  13940. // sycl buffer
  13941. struct ggml_backend_sycl_buffer_context {
  13942. int device;
  13943. void * dev_ptr = nullptr;
  13944. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  13945. size_t temp_tensor_extra_index = 0;
  13946. std::string name;
  13947. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) :
  13948. device(device), dev_ptr(dev_ptr) {
  13949. check_allow_gpu_index(device);
  13950. int id = g_sycl_gpu_mgr->gpus[device];
  13951. name = (GGML_SYCL_NAME + std::to_string(id));
  13952. }
  13953. ~ ggml_backend_sycl_buffer_context() {
  13954. delete[] temp_tensor_extras;
  13955. }
  13956. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13957. if (temp_tensor_extras == nullptr) {
  13958. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13959. }
  13960. size_t alloc_index = temp_tensor_extra_index;
  13961. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13962. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  13963. memset(extra, 0, sizeof(*extra));
  13964. return extra;
  13965. }
  13966. };
  13967. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  13968. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  13969. return ctx->name.c_str();
  13970. }
  13971. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  13972. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  13973. }
  13974. static void
  13975. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  13976. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13977. ggml_sycl_set_device(ctx->device);
  13978. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  13979. SYCL_CHECK(
  13980. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  13981. delete ctx;
  13982. }
  13983. catch (sycl::exception const &exc) {
  13984. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13985. << ", line:" << __LINE__ << std::endl;
  13986. std::exit(1);
  13987. }
  13988. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  13989. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13990. return ctx->dev_ptr;
  13991. }
  13992. GGML_CALL static void
  13993. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  13994. ggml_tensor *tensor) try {
  13995. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  13996. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  13997. assert(tensor->view_src->buffer->buft == buffer->buft);
  13998. tensor->backend = tensor->view_src->backend;
  13999. tensor->extra = tensor->view_src->extra;
  14000. return;
  14001. }
  14002. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  14003. extra->data_device[ctx->device] = tensor->data;
  14004. tensor->backend = GGML_BACKEND_TYPE_GPU;
  14005. tensor->extra = extra;
  14006. if (ggml_is_quantized(tensor->type)) {
  14007. // initialize padding to 0 to avoid possible NaN values
  14008. size_t original_size = ggml_nbytes(tensor);
  14009. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  14010. if (padded_size > original_size && tensor->view_src == nullptr) {
  14011. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  14012. (char *)tensor->data + original_size, 0,
  14013. padded_size - original_size).wait()));
  14014. }
  14015. }
  14016. }
  14017. catch (sycl::exception const &exc) {
  14018. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14019. << ", line:" << __LINE__ << std::endl;
  14020. std::exit(1);
  14021. }
  14022. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14023. ggml_tensor *tensor,
  14024. const void *data, size_t offset,
  14025. size_t size) try {
  14026. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14027. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14028. ggml_sycl_set_device(ctx->device);
  14029. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14030. SYCL_CHECK(
  14031. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  14032. char* host_buf = (char*)malloc(size);
  14033. memcpy(host_buf, data, size);
  14034. SYCL_CHECK(
  14035. CHECK_TRY_ERROR((*stream)
  14036. .memcpy((char *)tensor->data + offset, host_buf, size)
  14037. .wait()));
  14038. free(host_buf);
  14039. }
  14040. catch (sycl::exception const &exc) {
  14041. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14042. << ", line:" << __LINE__ << std::endl;
  14043. std::exit(1);
  14044. }
  14045. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14046. const ggml_tensor *tensor,
  14047. void *data, size_t offset,
  14048. size_t size) try {
  14049. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14050. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14051. ggml_sycl_set_device(ctx->device);
  14052. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14053. SYCL_CHECK(
  14054. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  14055. SYCL_CHECK(CHECK_TRY_ERROR(
  14056. (*stream)
  14057. .memcpy(data, (const char *)tensor->data + offset, size)
  14058. .wait()));
  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 bool
  14066. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  14067. const ggml_tensor *src,
  14068. ggml_tensor *dst) try {
  14069. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  14070. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  14071. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14072. ggml_sycl_set_device(src_ctx->device);
  14073. /*
  14074. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  14075. error codes. The original code was commented out and a warning string
  14076. was inserted. You need to rewrite this code.
  14077. */
  14078. SYCL_CHECK(CHECK_TRY_ERROR(
  14079. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  14080. ggml_sycl_set_device(dst_ctx->device);
  14081. /*
  14082. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  14083. error codes. The original code was commented out and a warning string
  14084. was inserted. You need to rewrite this code.
  14085. */
  14086. SYCL_CHECK(CHECK_TRY_ERROR(
  14087. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  14088. /*
  14089. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  14090. error codes. The original code was commented out and a warning string
  14091. was inserted. You need to rewrite this code.
  14092. */
  14093. dpct::queue_ptr stream_dst = g_syclStreams[dst_ctx->device][0];
  14094. dpct::queue_ptr stream_src = g_syclStreams[src_ctx->device][0];
  14095. size_t size = ggml_nbytes(src);
  14096. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  14097. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  14098. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  14099. #if 0
  14100. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  14101. (char *)dst->data, (const char *)src->data, size).wait()));
  14102. /*
  14103. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  14104. error codes. The original code was commented out and a warning string
  14105. was inserted. You need to rewrite this code.
  14106. */
  14107. SYCL_CHECK(CHECK_TRY_ERROR(
  14108. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  14109. #endif
  14110. return true;
  14111. }
  14112. return false;
  14113. }
  14114. catch (sycl::exception const &exc) {
  14115. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14116. << ", line:" << __LINE__ << std::endl;
  14117. std::exit(1);
  14118. }
  14119. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  14120. uint8_t value) try {
  14121. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14122. ggml_sycl_set_device(ctx->device);
  14123. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14124. SYCL_CHECK(
  14125. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  14126. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  14127. .memset(ctx->dev_ptr, value, buffer->size)
  14128. .wait()));
  14129. }
  14130. catch (sycl::exception const &exc) {
  14131. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14132. << ", line:" << __LINE__ << std::endl;
  14133. std::exit(1);
  14134. }
  14135. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  14136. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  14137. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  14138. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  14139. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  14140. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  14141. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  14142. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  14143. /* .clear = */ ggml_backend_sycl_buffer_clear,
  14144. /* .reset = */ NULL,
  14145. };
  14146. // sycl buffer type
  14147. struct ggml_backend_sycl_buffer_type_context {
  14148. int device;
  14149. std::string name;
  14150. };
  14151. struct ggml_backend_sycl_context {
  14152. int device;
  14153. std::string name;
  14154. };
  14155. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14156. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14157. return ctx->name.c_str();
  14158. }
  14159. GGML_CALL static ggml_backend_buffer_t
  14160. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  14161. size_t size) try {
  14162. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14163. ggml_sycl_set_device(buft_ctx->device);
  14164. const dpct::queue_ptr stream = g_syclStreams[buft_ctx->device][0];
  14165. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  14166. void * dev_ptr;
  14167. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  14168. size, *stream)));
  14169. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr);
  14170. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  14171. }
  14172. catch (sycl::exception const &exc) {
  14173. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14174. << ", line:" << __LINE__ << std::endl;
  14175. std::exit(1);
  14176. }
  14177. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14178. return 128;
  14179. UNUSED(buft);
  14180. }
  14181. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  14182. return dpct::get_current_device().get_max_mem_alloc_size();
  14183. UNUSED(buft);
  14184. }
  14185. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14186. size_t size = ggml_nbytes(tensor);
  14187. int64_t ne0 = tensor->ne[0];
  14188. if (ggml_is_quantized(tensor->type)) {
  14189. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14190. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14191. }
  14192. }
  14193. return size;
  14194. UNUSED(buft);
  14195. }
  14196. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  14197. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  14198. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  14199. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  14200. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  14201. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  14202. /* .is_host = */ nullptr,
  14203. };
  14204. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device_index) {
  14205. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  14206. if (device_index>=g_device_count or device_index<0) {
  14207. 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",
  14208. device_index, g_device_count-1);
  14209. GGML_ASSERT(device_index<g_device_count);
  14210. }
  14211. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  14212. if (!g_ggml_backend_sycl_buffer_type_initialized) {
  14213. for (int i = 0; i < g_device_count; i++) {
  14214. ggml_backend_sycl_buffer_types[i] = {
  14215. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  14216. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(g_sycl_gpu_mgr->gpus[i])},
  14217. };
  14218. }
  14219. g_ggml_backend_sycl_buffer_type_initialized = true;
  14220. }
  14221. return &ggml_backend_sycl_buffer_types[device_index];
  14222. }
  14223. // sycl split buffer type
  14224. 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) {
  14225. const int64_t nrows = ggml_nrows(tensor);
  14226. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  14227. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  14228. *row_low -= *row_low % rounding;
  14229. if (id == g_device_count - 1) {
  14230. *row_high = nrows;
  14231. } else {
  14232. *row_high = nrows*tensor_split[id + 1];
  14233. *row_high -= *row_high % rounding;
  14234. }
  14235. }
  14236. struct ggml_backend_sycl_split_buffer_context {
  14237. ~ggml_backend_sycl_split_buffer_context() try {
  14238. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  14239. for (int i = 0; i < g_device_count; ++i) {
  14240. // int id = g_sycl_gpu_mgr->gpus[i];
  14241. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14242. if (extra->events[i][is] != nullptr) {
  14243. /*
  14244. DPCT1009:206: SYCL uses exceptions to report errors and
  14245. does not use the error codes. The original code was
  14246. commented out and a warning string was inserted. You
  14247. need to rewrite this code.
  14248. */
  14249. SYCL_CHECK(CHECK_TRY_ERROR(
  14250. dpct::destroy_event(extra->events[i][is])));
  14251. }
  14252. }
  14253. if (extra->data_device[i] != nullptr) {
  14254. /*
  14255. DPCT1009:207: SYCL uses exceptions to report errors and does
  14256. not use the error codes. The original code was commented out
  14257. and a warning string was inserted. You need to rewrite this
  14258. code.
  14259. */
  14260. ggml_sycl_set_device(i);
  14261. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  14262. extra->data_device[i], *g_syclStreams[i][0])));
  14263. }
  14264. }
  14265. delete extra;
  14266. }
  14267. }
  14268. catch (sycl::exception const &exc) {
  14269. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14270. << ", line:" << __LINE__ << std::endl;
  14271. std::exit(1);
  14272. }
  14273. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  14274. };
  14275. GGML_CALL static const char * ggml_backend_sycl_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  14276. return GGML_SYCL_NAME "_Split";
  14277. UNUSED(buffer);
  14278. }
  14279. static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  14280. return buffer->iface.get_name == ggml_backend_sycl_split_buffer_get_name;
  14281. }
  14282. GGML_CALL static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14283. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14284. delete ctx;
  14285. }
  14286. GGML_CALL static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  14287. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  14288. return (void *)0x1000;
  14289. UNUSED(buffer);
  14290. }
  14291. GGML_CALL static void
  14292. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14293. ggml_tensor *tensor) try {
  14294. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  14295. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14296. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14297. const int64_t ne0 = tensor->ne[0];
  14298. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  14299. ctx->tensor_extras.push_back(extra);
  14300. for (int i = 0; i < g_device_count; ++i) {
  14301. // int id = g_sycl_gpu_mgr->gpus[i];
  14302. int64_t row_low, row_high;
  14303. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14304. int64_t nrows_split = row_high - row_low;
  14305. if (nrows_split == 0) {
  14306. continue;
  14307. }
  14308. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14309. const size_t original_size = size;
  14310. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14311. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14312. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14313. }
  14314. // FIXME: do not crash if cudaMalloc fails
  14315. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  14316. ggml_sycl_set_device(i);
  14317. char * buf;
  14318. /*
  14319. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  14320. error codes. The original code was commented out and a warning string
  14321. was inserted. You need to rewrite this code.
  14322. */
  14323. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  14324. size, *g_syclStreams[i][0])));
  14325. // set padding to 0 to avoid possible NaN values
  14326. if (size > original_size) {
  14327. /*
  14328. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  14329. the error codes. The original code was commented out and a warning
  14330. string was inserted. You need to rewrite this code.
  14331. */
  14332. SYCL_CHECK(CHECK_TRY_ERROR(
  14333. (*g_syclStreams[i][0])
  14334. .memset(buf + original_size, 0, size - original_size)
  14335. .wait()));
  14336. }
  14337. extra->data_device[i] = buf;
  14338. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14339. /*
  14340. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  14341. the error codes. The original code was commented out and a warning
  14342. string was inserted. You need to rewrite this code.
  14343. */
  14344. SYCL_CHECK(
  14345. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  14346. }
  14347. }
  14348. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  14349. tensor->extra = extra;
  14350. }
  14351. catch (sycl::exception const &exc) {
  14352. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14353. << ", line:" << __LINE__ << std::endl;
  14354. std::exit(1);
  14355. }
  14356. GGML_CALL static void
  14357. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14358. ggml_tensor *tensor, const void *data,
  14359. size_t offset, size_t size) try {
  14360. // split tensors must always be set in their entirety at once
  14361. GGML_ASSERT(offset == 0);
  14362. GGML_ASSERT(size == ggml_nbytes(tensor));
  14363. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14364. const int64_t ne0 = tensor->ne[0];
  14365. const size_t nb1 = tensor->nb[1];
  14366. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14367. for (int i = 0; i < g_device_count; ++i) {
  14368. // int id = g_sycl_gpu_mgr->gpus[i];
  14369. int64_t row_low, row_high;
  14370. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14371. int64_t nrows_split = row_high - row_low;
  14372. if (nrows_split == 0) {
  14373. continue;
  14374. }
  14375. const size_t offset_split = row_low*nb1;
  14376. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14377. const size_t original_size = size;
  14378. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14379. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14380. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14381. }
  14382. const char * buf_host = (const char *)data + offset_split;
  14383. /*
  14384. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  14385. error codes. The original code was commented out and a warning string
  14386. was inserted. You need to rewrite this code.
  14387. */
  14388. ggml_sycl_set_device(i);
  14389. SYCL_CHECK(CHECK_TRY_ERROR(
  14390. (*g_syclStreams[i][0])
  14391. .memcpy(extra->data_device[i], buf_host, original_size)
  14392. .wait()));
  14393. }
  14394. }
  14395. catch (sycl::exception const &exc) {
  14396. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14397. << ", line:" << __LINE__ << std::endl;
  14398. std::exit(1);
  14399. }
  14400. GGML_CALL static void
  14401. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14402. const ggml_tensor *tensor, void *data,
  14403. size_t offset, size_t size) try {
  14404. // split tensors must always be set in their entirety at once
  14405. GGML_ASSERT(offset == 0);
  14406. GGML_ASSERT(size == ggml_nbytes(tensor));
  14407. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14408. const int64_t ne0 = tensor->ne[0];
  14409. const size_t nb1 = tensor->nb[1];
  14410. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14411. for (int i = 0; i < g_device_count; ++i) {
  14412. // int id = g_sycl_gpu_mgr->gpus[i];
  14413. int64_t row_low, row_high;
  14414. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14415. int64_t nrows_split = row_high - row_low;
  14416. if (nrows_split == 0) {
  14417. continue;
  14418. }
  14419. const size_t offset_split = row_low*nb1;
  14420. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14421. const size_t original_size = size;
  14422. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14423. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14424. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14425. }
  14426. char * buf_host = (char *)data + offset_split;
  14427. /*
  14428. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  14429. error codes. The original code was commented out and a warning string
  14430. was inserted. You need to rewrite this code.
  14431. */
  14432. ggml_sycl_set_device(i);
  14433. SYCL_CHECK(CHECK_TRY_ERROR(
  14434. (*g_syclStreams[i][0])
  14435. .memcpy(buf_host, extra->data_device[i], original_size)
  14436. .wait()));
  14437. }
  14438. }
  14439. catch (sycl::exception const &exc) {
  14440. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14441. << ", line:" << __LINE__ << std::endl;
  14442. std::exit(1);
  14443. }
  14444. GGML_CALL static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  14445. UNUSED(buffer);
  14446. UNUSED(value);
  14447. }
  14448. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  14449. /* .get_name = */ ggml_backend_sycl_split_buffer_get_name,
  14450. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  14451. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  14452. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  14453. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  14454. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  14455. /* .cpy_tensor = */ NULL,
  14456. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  14457. /* .reset = */ NULL,
  14458. };
  14459. GGML_CALL static const char * ggml_backend_sycl_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14460. return GGML_SYCL_NAME "_Split";
  14461. UNUSED(buft);
  14462. }
  14463. GGML_CALL static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14464. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  14465. // instead, we allocate them for each tensor separately in init_tensor
  14466. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  14467. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  14468. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  14469. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  14470. }
  14471. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14472. return 128;
  14473. UNUSED(buft);
  14474. }
  14475. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14476. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  14477. size_t total_size = 0;
  14478. const int64_t ne0 = tensor->ne[0];
  14479. for (int i = 0; i < g_device_count; ++i) {
  14480. // int id = g_sycl_gpu_mgr->gpus[i];
  14481. int64_t row_low, row_high;
  14482. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  14483. int64_t nrows_split = row_high - row_low;
  14484. if (nrows_split == 0) {
  14485. continue;
  14486. }
  14487. total_size += ggml_nbytes_split(tensor, nrows_split);
  14488. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14489. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14490. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14491. }
  14492. }
  14493. return total_size;
  14494. }
  14495. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  14496. return false;
  14497. UNUSED(buft);
  14498. }
  14499. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  14500. /* .get_name = */ ggml_backend_sycl_split_buffer_type_name,
  14501. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  14502. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  14503. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  14504. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  14505. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  14506. };
  14507. GGML_CALL ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  14508. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_split_buffer_type\n");
  14509. ggml_init_sycl();
  14510. // FIXME: this is not thread safe
  14511. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  14512. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  14513. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  14514. if (all_zero) {
  14515. tensor_split_arr = g_default_tensor_split;
  14516. } else {
  14517. float split_sum = 0.0f;
  14518. for (int i = 0; i < g_device_count; ++i) {
  14519. // int id = g_sycl_gpu_mgr->gpus[i];
  14520. tensor_split_arr[i] = split_sum;
  14521. split_sum += tensor_split[i];
  14522. }
  14523. for (int i = 0; i < g_device_count; ++i) {
  14524. // int id = g_sycl_gpu_mgr->gpus[i];
  14525. tensor_split_arr[i] /= split_sum;
  14526. }
  14527. }
  14528. auto it = buft_map.find(tensor_split_arr);
  14529. if (it != buft_map.end()) {
  14530. return &it->second;
  14531. }
  14532. struct ggml_backend_buffer_type buft {
  14533. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  14534. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  14535. };
  14536. auto result = buft_map.emplace(tensor_split_arr, buft);
  14537. return &result.first->second;
  14538. }
  14539. // host buffer type
  14540. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14541. return GGML_SYCL_NAME "_Host";
  14542. UNUSED(buft);
  14543. }
  14544. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  14545. return GGML_SYCL_NAME "_Host";
  14546. UNUSED(buffer);
  14547. }
  14548. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14549. ggml_sycl_host_free(buffer->context);
  14550. }
  14551. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14552. void * ptr = ggml_sycl_host_malloc(size);
  14553. if (ptr == nullptr) {
  14554. // fallback to cpu buffer
  14555. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  14556. }
  14557. // FIXME: this is a hack to avoid having to implement a new buffer type
  14558. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  14559. buffer->buft = buft;
  14560. buffer->iface.get_name = ggml_backend_sycl_host_buffer_name;
  14561. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  14562. return buffer;
  14563. }
  14564. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  14565. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_host_buffer_type\n");
  14566. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  14567. /* .iface = */ {
  14568. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  14569. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  14570. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  14571. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  14572. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  14573. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  14574. },
  14575. /* .context = */ nullptr,
  14576. };
  14577. return &ggml_backend_sycl_buffer_type_host;
  14578. }
  14579. // backend
  14580. GGML_CALL static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  14581. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14582. return sycl_ctx->name.c_str();
  14583. }
  14584. GGML_CALL static void ggml_backend_sycl_free(ggml_backend_t backend) {
  14585. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14586. delete sycl_ctx;
  14587. delete backend;
  14588. }
  14589. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  14590. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14591. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  14592. }
  14593. GGML_CALL static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  14594. ggml_tensor *tensor,
  14595. const void *data, size_t offset,
  14596. size_t size) try {
  14597. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14598. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14599. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14600. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14601. (char *)tensor->data + offset, data, size).wait()));
  14602. }
  14603. catch (sycl::exception const &exc) {
  14604. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14605. << ", line:" << __LINE__ << std::endl;
  14606. std::exit(1);
  14607. }
  14608. GGML_CALL static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  14609. const ggml_tensor *tensor,
  14610. void *data, size_t offset,
  14611. size_t size) try {
  14612. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14613. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14614. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14615. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14616. data, (const char *)tensor->data + offset, size).wait()));
  14617. }
  14618. catch (sycl::exception const &exc) {
  14619. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14620. << ", line:" << __LINE__ << std::endl;
  14621. std::exit(1);
  14622. }
  14623. GGML_CALL static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  14624. const ggml_tensor *src,
  14625. ggml_tensor *dst) try {
  14626. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14627. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  14628. /*
  14629. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  14630. error codes. The original code was commented out and a warning string
  14631. was inserted. You need to rewrite this code.
  14632. */
  14633. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14634. dst->data, src->data, ggml_nbytes(dst)).wait()));
  14635. return true;
  14636. }
  14637. return false;
  14638. }
  14639. catch (sycl::exception const &exc) {
  14640. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14641. << ", line:" << __LINE__ << std::endl;
  14642. std::exit(1);
  14643. }
  14644. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  14645. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14646. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  14647. UNUSED(backend);
  14648. }
  14649. catch (sycl::exception const &exc) {
  14650. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14651. << ", line:" << __LINE__ << std::endl;
  14652. std::exit(1);
  14653. }
  14654. GGML_CALL static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  14655. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14656. ggml_sycl_set_main_device(sycl_ctx->device);
  14657. ggml_compute_params params = {};
  14658. params.type = GGML_TASK_TYPE_COMPUTE;
  14659. params.ith = 0;
  14660. for (int i = 0; i < cgraph->n_nodes; i++) {
  14661. ggml_tensor * node = cgraph->nodes[i];
  14662. 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) {
  14663. continue;
  14664. }
  14665. #ifndef NDEBUG
  14666. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14667. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14668. assert(node->extra != nullptr);
  14669. for (int j = 0; j < GGML_MAX_SRC; j++) {
  14670. if (node->src[j] != nullptr) {
  14671. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14672. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14673. assert(node->src[j]->extra != nullptr);
  14674. }
  14675. }
  14676. #endif
  14677. bool ok = ggml_sycl_compute_forward(&params, node);
  14678. if (!ok) {
  14679. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  14680. }
  14681. GGML_ASSERT(ok);
  14682. }
  14683. return GGML_STATUS_SUCCESS;
  14684. }
  14685. GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  14686. switch (op->op) {
  14687. case GGML_OP_UNARY:
  14688. switch (ggml_get_unary_op(op)) {
  14689. case GGML_UNARY_OP_GELU:
  14690. case GGML_UNARY_OP_SILU:
  14691. case GGML_UNARY_OP_RELU:
  14692. case GGML_UNARY_OP_HARDSIGMOID:
  14693. case GGML_UNARY_OP_HARDSWISH:
  14694. case GGML_UNARY_OP_GELU_QUICK:
  14695. case GGML_UNARY_OP_TANH:
  14696. return ggml_is_contiguous(op->src[0]);
  14697. default:
  14698. return false;
  14699. }
  14700. break;
  14701. case GGML_OP_MUL_MAT:
  14702. case GGML_OP_MUL_MAT_ID:
  14703. {
  14704. struct ggml_tensor * a;
  14705. struct ggml_tensor * b;
  14706. if (op->op == GGML_OP_MUL_MAT) {
  14707. a = op->src[0];
  14708. b = op->src[1];
  14709. } else {
  14710. a = op->src[2];
  14711. b = op->src[1];
  14712. }
  14713. if (a->ne[3] != b->ne[3]) {
  14714. return false;
  14715. }
  14716. ggml_type a_type = a->type;
  14717. if (a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ4_XS ||
  14718. a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ3_S ||
  14719. a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ2_S ||
  14720. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ1_M
  14721. ) {
  14722. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  14723. return false;
  14724. }
  14725. }
  14726. return true;
  14727. } break;
  14728. case GGML_OP_GET_ROWS:
  14729. {
  14730. switch (op->src[0]->type) {
  14731. case GGML_TYPE_F16:
  14732. case GGML_TYPE_F32:
  14733. case GGML_TYPE_Q4_0:
  14734. case GGML_TYPE_Q4_1:
  14735. case GGML_TYPE_Q5_0:
  14736. case GGML_TYPE_Q5_1:
  14737. case GGML_TYPE_Q8_0:
  14738. return true;
  14739. default:
  14740. return false;
  14741. }
  14742. } break;
  14743. case GGML_OP_CPY:
  14744. {
  14745. ggml_type src0_type = op->src[0]->type;
  14746. ggml_type src1_type = op->src[1]->type;
  14747. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  14748. return true;
  14749. }
  14750. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  14751. return true;
  14752. }
  14753. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  14754. return true;
  14755. }
  14756. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  14757. return true;
  14758. }
  14759. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  14760. return true;
  14761. }
  14762. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  14763. return true;
  14764. }
  14765. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  14766. return true;
  14767. }
  14768. return false;
  14769. } break;
  14770. case GGML_OP_CONCAT:
  14771. {
  14772. ggml_type src0_type = op->src[0]->type;
  14773. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  14774. } break;
  14775. case GGML_OP_DUP:
  14776. case GGML_OP_NONE:
  14777. case GGML_OP_RESHAPE:
  14778. case GGML_OP_REPEAT:
  14779. case GGML_OP_VIEW:
  14780. case GGML_OP_PERMUTE:
  14781. case GGML_OP_TRANSPOSE:
  14782. case GGML_OP_NORM:
  14783. case GGML_OP_ADD:
  14784. case GGML_OP_MUL:
  14785. case GGML_OP_DIV:
  14786. case GGML_OP_RMS_NORM:
  14787. case GGML_OP_SCALE:
  14788. case GGML_OP_SQR:
  14789. case GGML_OP_CLAMP:
  14790. case GGML_OP_CONT:
  14791. case GGML_OP_DIAG_MASK_INF:
  14792. case GGML_OP_SOFT_MAX:
  14793. case GGML_OP_ROPE:
  14794. case GGML_OP_IM2COL:
  14795. case GGML_OP_POOL_2D:
  14796. case GGML_OP_SUM_ROWS:
  14797. case GGML_OP_ARGSORT:
  14798. case GGML_OP_ACC:
  14799. case GGML_OP_GROUP_NORM:
  14800. case GGML_OP_UPSCALE:
  14801. case GGML_OP_PAD:
  14802. case GGML_OP_LEAKY_RELU:
  14803. return true;
  14804. default:
  14805. return false;
  14806. }
  14807. UNUSED(backend);
  14808. }
  14809. GGML_CALL static bool ggml_backend_sycl_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  14810. const int min_batch_size = 32;
  14811. return op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS && op->op != GGML_OP_MUL_MAT_ID;
  14812. GGML_UNUSED(backend);
  14813. }
  14814. GGML_CALL static bool ggml_backend_sycl_supports_buft(ggml_backend_t backend, ggml_backend_buffer_type_t buft) {
  14815. if (buft->iface.get_name != ggml_backend_sycl_buffer_type_name) {
  14816. return false;
  14817. }
  14818. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14819. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14820. return buft_ctx->device == sycl_ctx->device;
  14821. }
  14822. static ggml_backend_i ggml_backend_sycl_interface = {
  14823. /* .get_name = */ ggml_backend_sycl_name,
  14824. /* .free = */ ggml_backend_sycl_free,
  14825. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  14826. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  14827. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  14828. /* .cpy_tensor_async = */ NULL, //ggml_backend_sycl_cpy_tensor_async, // TODO: update for the new interface
  14829. /* .synchronize = */ ggml_backend_sycl_synchronize,
  14830. /* .graph_plan_create = */ NULL,
  14831. /* .graph_plan_free = */ NULL,
  14832. /* .graph_plan_update = */ NULL,
  14833. /* .graph_plan_compute = */ NULL,
  14834. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  14835. /* .supports_op = */ ggml_backend_sycl_supports_op,
  14836. /* .supports_buft = */ ggml_backend_sycl_supports_buft,
  14837. /* .offload_op = */ ggml_backend_sycl_offload_op,
  14838. /* .event_new = */ NULL,
  14839. /* .event_free = */ NULL,
  14840. /* .event_record = */ NULL,
  14841. /* .event_wait = */ NULL,
  14842. /* .event_synchronize = */ NULL,
  14843. };
  14844. static ggml_guid_t ggml_backend_sycl_guid() {
  14845. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  14846. return &guid;
  14847. }
  14848. GGML_CALL ggml_backend_t ggml_backend_sycl_init(int device) {
  14849. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_init\n");
  14850. ggml_init_sycl();
  14851. check_allow_gpu_index(device);
  14852. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  14853. ggml_sycl_set_main_device(device);
  14854. int id = g_sycl_gpu_mgr->gpus[device];
  14855. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context {
  14856. /* .device = */ device,
  14857. /* .name = */ GGML_SYCL_NAME + std::to_string(id),
  14858. };
  14859. ggml_backend_t sycl_backend = new ggml_backend {
  14860. /* .guid = */ ggml_backend_sycl_guid(),
  14861. /* .interface = */ ggml_backend_sycl_interface,
  14862. /* .context = */ ctx
  14863. };
  14864. return sycl_backend;
  14865. }
  14866. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  14867. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  14868. }
  14869. GGML_CALL int ggml_backend_sycl_get_device_count() {
  14870. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_count\n");
  14871. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14872. return g_sycl_gpu_mgr->get_gpu_count();
  14873. }
  14874. GGML_CALL static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  14875. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  14876. return sycl_backend;
  14877. UNUSED(params);
  14878. }
  14879. GGML_API GGML_CALL int ggml_backend_sycl_get_device_index(int device_id) {
  14880. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_index\n");
  14881. return g_sycl_gpu_mgr->get_index(device_id);
  14882. }
  14883. GGML_API GGML_CALL int ggml_backend_sycl_get_device_id(int device_index) {
  14884. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_id\n");
  14885. return g_sycl_gpu_mgr->gpus[device_index];
  14886. }
  14887. GGML_API GGML_CALL void ggml_backend_sycl_set_single_device_mode(int main_gpu_id) {
  14888. ggml_init_sycl();
  14889. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_single_device_mode\n");
  14890. fprintf(stderr, "ggml_backend_sycl_set_single_device: use single device: [%d]\n", main_gpu_id);
  14891. GGML_ASSERT(main_gpu_id<g_all_sycl_device_count);
  14892. if (g_sycl_gpu_mgr) {
  14893. delete g_sycl_gpu_mgr;
  14894. }
  14895. g_sycl_gpu_mgr = new sycl_gpu_mgr(main_gpu_id);
  14896. g_ggml_sycl_backend_gpu_mode = SYCL_SINGLE_GPU_MODE;
  14897. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  14898. g_ggml_backend_sycl_buffer_type_initialized = false;
  14899. }
  14900. GGML_API GGML_CALL void ggml_backend_sycl_set_mul_device_mode() {
  14901. ggml_init_sycl();
  14902. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_mul_device_mode\n");
  14903. if (g_ggml_sycl_backend_gpu_mode == SYCL_MUL_GPU_MODE) {
  14904. return;
  14905. }
  14906. fprintf(stderr, "ggml_backend_sycl_set_mul_device_mode: true\n");
  14907. if (g_sycl_gpu_mgr) {
  14908. delete g_sycl_gpu_mgr;
  14909. }
  14910. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14911. g_ggml_sycl_backend_gpu_mode = SYCL_MUL_GPU_MODE;
  14912. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  14913. g_ggml_backend_sycl_buffer_type_initialized = false;
  14914. }
  14915. extern "C" int ggml_backend_sycl_reg_devices();
  14916. int ggml_backend_sycl_reg_devices() {
  14917. ggml_backend_sycl_set_mul_device_mode();
  14918. assert(g_device_count>0);
  14919. for (int i = 0; i < g_device_count; i++) {
  14920. int id = g_sycl_gpu_mgr->gpus[i];
  14921. char name[128];
  14922. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, id);
  14923. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  14924. }
  14925. return g_device_count;
  14926. }