ggml-sycl.cpp 683 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783
  1. //
  2. // MIT license
  3. // Copyright (C) 2024 Intel Corporation
  4. // SPDX-License-Identifier: MIT
  5. //
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. #include <algorithm>
  12. #include <assert.h>
  13. #include <atomic>
  14. #include <cinttypes>
  15. #include <cstddef>
  16. #include <cstdint>
  17. #include <cstdlib>
  18. #include <float.h>
  19. #include <limits>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <vector>
  23. #include <cmath>
  24. #include <iostream>
  25. #include <fstream>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <regex>
  29. #include <sycl/sycl.hpp>
  30. #include <sycl/half_type.hpp>
  31. #include "ggml-sycl.h"
  32. #include "ggml.h"
  33. #include "ggml-backend-impl.h"
  34. /*
  35. Following definition copied from DPCT head files, which are used by ggml-sycl.cpp
  36. */
  37. // COPY from DPCT head files
  38. #include <sycl/sycl.hpp>
  39. #include <oneapi/mkl.hpp>
  40. #include <map>
  41. #if defined(__linux__)
  42. #include <sys/mman.h>
  43. #elif defined(_WIN64)
  44. #ifndef NOMINMAX
  45. #define NOMINMAX
  46. #endif
  47. #include <windows.h>
  48. #else
  49. #error "Only support Windows and Linux."
  50. #endif
  51. #if defined(__linux__)
  52. #include <unistd.h>
  53. #include <sys/syscall.h>
  54. #endif
  55. #if defined(_WIN64)
  56. #ifndef NOMINMAX
  57. #define NOMINMAX
  58. #endif
  59. #include <windows.h>
  60. #endif
  61. #define DPCT_COMPATIBILITY_TEMP (900)
  62. #if defined(_MSC_VER)
  63. #define __dpct_align__(n) __declspec(align(n))
  64. #define __dpct_inline__ __forceinline
  65. #else
  66. #define __dpct_align__(n) __attribute__((aligned(n)))
  67. #define __dpct_inline__ __inline__ __attribute__((always_inline))
  68. #endif
  69. #if defined(_MSC_VER)
  70. #define __dpct_noinline__ __declspec(noinline)
  71. #else
  72. #define __dpct_noinline__ __attribute__((noinline))
  73. #endif
  74. std::string get_device_type_name(const sycl::device &Device) {
  75. auto DeviceType = Device.get_info<sycl::info::device::device_type>();
  76. switch (DeviceType) {
  77. case sycl::info::device_type::cpu:
  78. return "cpu";
  79. case sycl::info::device_type::gpu:
  80. return "gpu";
  81. case sycl::info::device_type::host:
  82. return "host";
  83. case sycl::info::device_type::accelerator:
  84. return "acc";
  85. default:
  86. return "unknown";
  87. }
  88. }
  89. std::string get_device_backend_and_type(const sycl::device &device) {
  90. std::stringstream device_type;
  91. sycl::backend backend = device.get_backend();
  92. device_type << backend << ":" << get_device_type_name(device);
  93. return device_type.str();
  94. }
  95. namespace dpct
  96. {
  97. typedef sycl::queue *queue_ptr;
  98. typedef sycl::event *event_ptr;
  99. typedef char *device_ptr;
  100. typedef uint8_t byte_t;
  101. typedef sycl::buffer<byte_t> buffer_t;
  102. /// SYCL default exception handler
  103. inline auto exception_handler = [](sycl::exception_list exceptions)
  104. {
  105. for (std::exception_ptr const &e : exceptions)
  106. {
  107. try
  108. {
  109. std::rethrow_exception(e);
  110. }
  111. catch (sycl::exception const &e)
  112. {
  113. std::cerr << "Caught asynchronous SYCL exception:" << std::endl
  114. << e.what() << std::endl
  115. << "Exception caught at file:" << __FILE__
  116. << ", line:" << __LINE__ << std::endl;
  117. }
  118. }
  119. };
  120. enum error_code
  121. {
  122. success = 0,
  123. default_error = 999
  124. };
  125. enum memcpy_direction
  126. {
  127. host_to_host,
  128. host_to_device,
  129. device_to_host,
  130. device_to_device,
  131. automatic
  132. };
  133. enum memory_region
  134. {
  135. global = 0, // device global memory
  136. constant, // device constant memory
  137. local, // device local memory
  138. shared, // memory which can be accessed by host and device
  139. };
  140. enum class library_data_t : unsigned char
  141. {
  142. real_float = 0,
  143. complex_float,
  144. real_double,
  145. complex_double,
  146. real_half,
  147. complex_half,
  148. real_bfloat16,
  149. complex_bfloat16,
  150. real_int4,
  151. complex_int4,
  152. real_uint4,
  153. complex_uint4,
  154. real_int8,
  155. complex_int8,
  156. real_uint8,
  157. complex_uint8,
  158. real_int16,
  159. complex_int16,
  160. real_uint16,
  161. complex_uint16,
  162. real_int32,
  163. complex_int32,
  164. real_uint32,
  165. complex_uint32,
  166. real_int64,
  167. complex_int64,
  168. real_uint64,
  169. complex_uint64,
  170. real_int8_4,
  171. real_int8_32,
  172. real_uint8_4,
  173. library_data_t_size
  174. };
  175. template <typename T>
  176. struct DataType
  177. {
  178. using T2 = T;
  179. };
  180. template <typename T>
  181. struct DataType<sycl::vec<T, 2>>
  182. {
  183. using T2 = std::complex<T>;
  184. };
  185. static void destroy_event(event_ptr event)
  186. {
  187. delete event;
  188. }
  189. static inline unsigned int get_tid()
  190. {
  191. #if defined(__linux__)
  192. return syscall(SYS_gettid);
  193. #elif defined(_WIN64)
  194. return GetCurrentThreadId();
  195. #else
  196. #error "Only support Windows and Linux."
  197. #endif
  198. }
  199. namespace detail
  200. {
  201. static void get_version(const sycl::device &dev, int &major, int &minor)
  202. {
  203. // Version string has the following format:
  204. // a. OpenCL<space><major.minor><space><vendor-specific-information>
  205. // b. <major.minor>
  206. // c. <AmdGcnArchName> e.g gfx1030
  207. std::string ver;
  208. ver = dev.get_info<sycl::info::device::version>();
  209. std::string::size_type i = 0;
  210. while (i < ver.size()) {
  211. if (isdigit(ver[i]))
  212. break;
  213. i++;
  214. }
  215. major = std::stoi(&(ver[i]));
  216. while (i < ver.size()) {
  217. if (ver[i] == '.')
  218. break;
  219. i++;
  220. }
  221. if (i < ver.size()) {
  222. // a. and b.
  223. i++;
  224. minor = std::stoi(&(ver[i]));
  225. } else {
  226. // c.
  227. minor = 0;
  228. }
  229. }
  230. template <typename tag, typename T>
  231. class generic_error_type
  232. {
  233. public:
  234. generic_error_type() = default;
  235. generic_error_type(T value) : value{value} {}
  236. operator T() const { return value; }
  237. private:
  238. T value;
  239. };
  240. } // namespace detail
  241. /// Pitched 2D/3D memory data.
  242. class pitched_data
  243. {
  244. public:
  245. pitched_data() : pitched_data(nullptr, 0, 0, 0) {}
  246. pitched_data(void *data, size_t pitch, size_t x, size_t y)
  247. : _data(data), _pitch(pitch), _x(x), _y(y) {}
  248. void *get_data_ptr() { return _data; }
  249. void set_data_ptr(void *data) { _data = data; }
  250. size_t get_pitch() { return _pitch; }
  251. void set_pitch(size_t pitch) { _pitch = pitch; }
  252. size_t get_x() { return _x; }
  253. void set_x(size_t x) { _x = x; };
  254. size_t get_y() { return _y; }
  255. void set_y(size_t y) { _y = y; }
  256. private:
  257. void *_data;
  258. size_t _pitch, _x, _y;
  259. };
  260. class device_info
  261. {
  262. public:
  263. // get interface
  264. const char *get_name() const { return _name; }
  265. char *get_name() { return _name; }
  266. template <typename WorkItemSizesTy = sycl::range<3>,
  267. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  268. std::is_same_v<WorkItemSizesTy, int *>,
  269. int> = 0>
  270. auto get_max_work_item_sizes() const
  271. {
  272. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  273. return sycl::range<3>(_max_work_item_sizes_i[0],
  274. _max_work_item_sizes_i[1],
  275. _max_work_item_sizes_i[2]);
  276. else
  277. {
  278. return _max_work_item_sizes_i;
  279. }
  280. }
  281. template <typename WorkItemSizesTy = sycl::range<3>,
  282. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  283. std::is_same_v<WorkItemSizesTy, int *>,
  284. int> = 0>
  285. auto get_max_work_item_sizes()
  286. {
  287. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  288. return sycl::range<3>(_max_work_item_sizes_i[0],
  289. _max_work_item_sizes_i[1],
  290. _max_work_item_sizes_i[2]);
  291. else
  292. {
  293. return _max_work_item_sizes_i;
  294. }
  295. }
  296. bool get_host_unified_memory() const { return _host_unified_memory; }
  297. int get_major_version() const { return _major; }
  298. int get_minor_version() const { return _minor; }
  299. int get_integrated() const { return _integrated; }
  300. int get_max_clock_frequency() const { return _frequency; }
  301. int get_max_compute_units() const { return _max_compute_units; }
  302. int get_max_work_group_size() const { return _max_work_group_size; }
  303. int get_max_sub_group_size() const { return _max_sub_group_size; }
  304. int get_max_work_items_per_compute_unit() const
  305. {
  306. return _max_work_items_per_compute_unit;
  307. }
  308. int get_max_register_size_per_work_group() const
  309. {
  310. return _max_register_size_per_work_group;
  311. }
  312. template <typename NDRangeSizeTy = size_t *,
  313. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  314. std::is_same_v<NDRangeSizeTy, int *>,
  315. int> = 0>
  316. auto get_max_nd_range_size() const
  317. {
  318. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  319. return _max_nd_range_size;
  320. else
  321. return _max_nd_range_size_i;
  322. }
  323. template <typename NDRangeSizeTy = size_t *,
  324. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  325. std::is_same_v<NDRangeSizeTy, int *>,
  326. int> = 0>
  327. auto get_max_nd_range_size()
  328. {
  329. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  330. return _max_nd_range_size;
  331. else
  332. return _max_nd_range_size_i;
  333. }
  334. size_t get_global_mem_size() const { return _global_mem_size; }
  335. size_t get_local_mem_size() const { return _local_mem_size; }
  336. size_t get_max_mem_alloc_size() const { return _max_mem_alloc_size; }
  337. /// Returns the maximum clock rate of device's global memory in kHz. If
  338. /// compiler does not support this API then returns default value 3200000 kHz.
  339. unsigned int get_memory_clock_rate() const { return _memory_clock_rate; }
  340. /// Returns the maximum bus width between device and memory in bits. If
  341. /// compiler does not support this API then returns default value 64 bits.
  342. unsigned int get_memory_bus_width() const { return _memory_bus_width; }
  343. uint32_t get_device_id() const { return _device_id; }
  344. std::array<unsigned char, 16> get_uuid() const { return _uuid; }
  345. /// Returns global memory cache size in bytes.
  346. unsigned int get_global_mem_cache_size() const
  347. {
  348. return _global_mem_cache_size;
  349. }
  350. // set interface
  351. void set_name(const char *name)
  352. {
  353. size_t length = strlen(name);
  354. if (length < 256)
  355. {
  356. std::memcpy(_name, name, length + 1);
  357. }
  358. else
  359. {
  360. std::memcpy(_name, name, 255);
  361. _name[255] = '\0';
  362. }
  363. }
  364. void set_max_work_item_sizes(const sycl::range<3> max_work_item_sizes)
  365. {
  366. for (int i = 0; i < 3; ++i)
  367. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  368. }
  369. [[deprecated]] void
  370. set_max_work_item_sizes(const sycl::id<3> max_work_item_sizes)
  371. {
  372. for (int i = 0; i < 3; ++i)
  373. {
  374. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  375. }
  376. }
  377. void set_host_unified_memory(bool host_unified_memory)
  378. {
  379. _host_unified_memory = host_unified_memory;
  380. }
  381. void set_major_version(int major) { _major = major; }
  382. void set_minor_version(int minor) { _minor = minor; }
  383. void set_integrated(int integrated) { _integrated = integrated; }
  384. void set_max_clock_frequency(int frequency) { _frequency = frequency; }
  385. void set_max_compute_units(int max_compute_units)
  386. {
  387. _max_compute_units = max_compute_units;
  388. }
  389. void set_global_mem_size(size_t global_mem_size)
  390. {
  391. _global_mem_size = global_mem_size;
  392. }
  393. void set_local_mem_size(size_t local_mem_size)
  394. {
  395. _local_mem_size = local_mem_size;
  396. }
  397. void set_max_mem_alloc_size(size_t max_mem_alloc_size)
  398. {
  399. _max_mem_alloc_size = max_mem_alloc_size;
  400. }
  401. void set_max_work_group_size(int max_work_group_size)
  402. {
  403. _max_work_group_size = max_work_group_size;
  404. }
  405. void set_max_sub_group_size(int max_sub_group_size)
  406. {
  407. _max_sub_group_size = max_sub_group_size;
  408. }
  409. void
  410. set_max_work_items_per_compute_unit(int max_work_items_per_compute_unit)
  411. {
  412. _max_work_items_per_compute_unit = max_work_items_per_compute_unit;
  413. }
  414. void set_max_nd_range_size(int max_nd_range_size[])
  415. {
  416. for (int i = 0; i < 3; i++)
  417. {
  418. _max_nd_range_size[i] = max_nd_range_size[i];
  419. _max_nd_range_size_i[i] = max_nd_range_size[i];
  420. }
  421. }
  422. void set_memory_clock_rate(unsigned int memory_clock_rate)
  423. {
  424. _memory_clock_rate = memory_clock_rate;
  425. }
  426. void set_memory_bus_width(unsigned int memory_bus_width)
  427. {
  428. _memory_bus_width = memory_bus_width;
  429. }
  430. void
  431. set_max_register_size_per_work_group(int max_register_size_per_work_group)
  432. {
  433. _max_register_size_per_work_group = max_register_size_per_work_group;
  434. }
  435. void set_device_id(uint32_t device_id)
  436. {
  437. _device_id = device_id;
  438. }
  439. void set_uuid(std::array<unsigned char, 16> uuid)
  440. {
  441. _uuid = std::move(uuid);
  442. }
  443. void set_global_mem_cache_size(unsigned int global_mem_cache_size)
  444. {
  445. _global_mem_cache_size = global_mem_cache_size;
  446. }
  447. private:
  448. char _name[256];
  449. int _max_work_item_sizes_i[3];
  450. bool _host_unified_memory = false;
  451. int _major;
  452. int _minor;
  453. int _integrated = 0;
  454. int _frequency;
  455. // Set estimated value 3200000 kHz as default value.
  456. unsigned int _memory_clock_rate = 3200000;
  457. // Set estimated value 64 bits as default value.
  458. unsigned int _memory_bus_width = 64;
  459. unsigned int _global_mem_cache_size;
  460. int _max_compute_units;
  461. int _max_work_group_size;
  462. int _max_sub_group_size;
  463. int _max_work_items_per_compute_unit;
  464. int _max_register_size_per_work_group;
  465. size_t _global_mem_size;
  466. size_t _local_mem_size;
  467. size_t _max_mem_alloc_size;
  468. size_t _max_nd_range_size[3];
  469. int _max_nd_range_size_i[3];
  470. uint32_t _device_id;
  471. std::array<unsigned char, 16> _uuid;
  472. };
  473. static int get_major_version(const sycl::device &dev)
  474. {
  475. int major, minor;
  476. detail::get_version(dev, major, minor);
  477. return major;
  478. }
  479. static int get_minor_version(const sycl::device &dev)
  480. {
  481. int major, minor;
  482. detail::get_version(dev, major, minor);
  483. return minor;
  484. }
  485. static void get_device_info(device_info &out, const sycl::device &dev)
  486. {
  487. device_info prop;
  488. prop.set_name(dev.get_info<sycl::info::device::name>().c_str());
  489. int major, minor;
  490. detail::get_version(dev, major, minor);
  491. prop.set_major_version(major);
  492. prop.set_minor_version(minor);
  493. prop.set_max_work_item_sizes(
  494. #if (__SYCL_COMPILER_VERSION && __SYCL_COMPILER_VERSION < 20220902)
  495. // oneAPI DPC++ compiler older than 2022/09/02, where max_work_item_sizes
  496. // is an enum class element
  497. dev.get_info<sycl::info::device::max_work_item_sizes>());
  498. #else
  499. // SYCL 2020-conformant code, max_work_item_sizes is a struct templated by
  500. // an int
  501. dev.get_info<sycl::info::device::max_work_item_sizes<3>>());
  502. #endif
  503. prop.set_host_unified_memory(dev.has(sycl::aspect::usm_host_allocations));
  504. prop.set_max_clock_frequency(
  505. dev.get_info<sycl::info::device::max_clock_frequency>() * 1000);
  506. prop.set_max_compute_units(
  507. dev.get_info<sycl::info::device::max_compute_units>());
  508. prop.set_max_work_group_size(
  509. dev.get_info<sycl::info::device::max_work_group_size>());
  510. prop.set_global_mem_size(dev.get_info<sycl::info::device::global_mem_size>());
  511. prop.set_local_mem_size(dev.get_info<sycl::info::device::local_mem_size>());
  512. prop.set_max_mem_alloc_size(dev.get_info<sycl::info::device::max_mem_alloc_size>());
  513. #if (defined(SYCL_EXT_INTEL_DEVICE_INFO) && SYCL_EXT_INTEL_DEVICE_INFO >= 6)
  514. if (dev.has(sycl::aspect::ext_intel_memory_clock_rate))
  515. {
  516. unsigned int tmp =
  517. dev.get_info<sycl::ext::intel::info::device::memory_clock_rate>();
  518. if (tmp != 0)
  519. prop.set_memory_clock_rate(1000 * tmp);
  520. }
  521. if (dev.has(sycl::aspect::ext_intel_memory_bus_width))
  522. {
  523. prop.set_memory_bus_width(
  524. dev.get_info<sycl::ext::intel::info::device::memory_bus_width>());
  525. }
  526. if (dev.has(sycl::aspect::ext_intel_device_id))
  527. {
  528. prop.set_device_id(
  529. dev.get_info<sycl::ext::intel::info::device::device_id>());
  530. }
  531. if (dev.has(sycl::aspect::ext_intel_device_info_uuid))
  532. {
  533. prop.set_uuid(dev.get_info<sycl::ext::intel::info::device::uuid>());
  534. }
  535. #elif defined(_MSC_VER) && !defined(__clang__)
  536. #pragma message("get_device_info: querying memory_clock_rate and \
  537. memory_bus_width are not supported by the compiler used. \
  538. Use 3200000 kHz as memory_clock_rate default value. \
  539. Use 64 bits as memory_bus_width default value.")
  540. #else
  541. #warning "get_device_info: querying memory_clock_rate and \
  542. memory_bus_width are not supported by the compiler used. \
  543. Use 3200000 kHz as memory_clock_rate default value. \
  544. Use 64 bits as memory_bus_width default value."
  545. #endif
  546. size_t max_sub_group_size = 1;
  547. std::vector<size_t> sub_group_sizes =
  548. dev.get_info<sycl::info::device::sub_group_sizes>();
  549. for (const auto &sub_group_size : sub_group_sizes)
  550. {
  551. if (max_sub_group_size < sub_group_size)
  552. max_sub_group_size = sub_group_size;
  553. }
  554. prop.set_max_sub_group_size(max_sub_group_size);
  555. prop.set_max_work_items_per_compute_unit(
  556. dev.get_info<sycl::info::device::max_work_group_size>());
  557. int max_nd_range_size[] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
  558. prop.set_max_nd_range_size(max_nd_range_size);
  559. // Estimates max register size per work group, feel free to update the value
  560. // according to device properties.
  561. prop.set_max_register_size_per_work_group(65536);
  562. prop.set_global_mem_cache_size(
  563. dev.get_info<sycl::info::device::global_mem_cache_size>());
  564. out = prop;
  565. }
  566. /// dpct device extension
  567. class device_ext : public sycl::device
  568. {
  569. typedef std::mutex mutex_type;
  570. public:
  571. device_ext() : sycl::device(), _ctx(*this) {}
  572. ~device_ext()
  573. {
  574. std::lock_guard<mutex_type> lock(m_mutex);
  575. clear_queues();
  576. }
  577. device_ext(const sycl::device &base) : sycl::device(base), _ctx(*this)
  578. {
  579. std::lock_guard<mutex_type> lock(m_mutex);
  580. init_queues();
  581. }
  582. int is_native_atomic_supported() { return 0; }
  583. int get_major_version() const
  584. {
  585. return dpct::get_major_version(*this);
  586. }
  587. int get_minor_version() const
  588. {
  589. return dpct::get_minor_version(*this);
  590. }
  591. int get_max_compute_units() const
  592. {
  593. return get_device_info().get_max_compute_units();
  594. }
  595. /// Return the maximum clock frequency of this device in KHz.
  596. int get_max_clock_frequency() const
  597. {
  598. return get_device_info().get_max_clock_frequency();
  599. }
  600. int get_integrated() const { return get_device_info().get_integrated(); }
  601. int get_max_sub_group_size() const
  602. {
  603. return get_device_info().get_max_sub_group_size();
  604. }
  605. int get_max_register_size_per_work_group() const
  606. {
  607. return get_device_info().get_max_register_size_per_work_group();
  608. }
  609. int get_max_work_group_size() const
  610. {
  611. return get_device_info().get_max_work_group_size();
  612. }
  613. int get_mem_base_addr_align() const
  614. {
  615. return get_info<sycl::info::device::mem_base_addr_align>();
  616. }
  617. size_t get_global_mem_size() const
  618. {
  619. return get_device_info().get_global_mem_size();
  620. }
  621. size_t get_max_mem_alloc_size() const
  622. {
  623. return get_device_info().get_max_mem_alloc_size();
  624. }
  625. /// Get the number of bytes of free and total memory on the SYCL device.
  626. /// \param [out] free_memory The number of bytes of free memory on the SYCL device.
  627. /// \param [out] total_memory The number of bytes of total memory on the SYCL device.
  628. void get_memory_info(size_t &free_memory, size_t &total_memory)
  629. {
  630. total_memory = get_device_info().get_global_mem_size();
  631. const char *warning_info = "get_memory_info: [warning] ext_intel_free_memory is not "
  632. "supported (export/set ZES_ENABLE_SYSMAN=1 to support), "
  633. "use total memory as free memory";
  634. #if (defined(__SYCL_COMPILER_VERSION) && __SYCL_COMPILER_VERSION >= 20221105)
  635. if (!has(sycl::aspect::ext_intel_free_memory))
  636. {
  637. std::cerr << warning_info << std::endl;
  638. free_memory = total_memory;
  639. }
  640. else
  641. {
  642. free_memory = get_info<sycl::ext::intel::info::device::free_memory>();
  643. }
  644. #else
  645. std::cerr << warning_info << std::endl;
  646. free_memory = total_memory;
  647. #if defined(_MSC_VER) && !defined(__clang__)
  648. #pragma message("Querying the number of bytes of free memory is not supported")
  649. #else
  650. #warning "Querying the number of bytes of free memory is not supported"
  651. #endif
  652. #endif
  653. }
  654. void get_device_info(device_info &out) const
  655. {
  656. dpct::get_device_info(out, *this);
  657. }
  658. device_info get_device_info() const
  659. {
  660. device_info prop;
  661. dpct::get_device_info(prop, *this);
  662. return prop;
  663. }
  664. void reset()
  665. {
  666. std::lock_guard<mutex_type> lock(m_mutex);
  667. clear_queues();
  668. init_queues();
  669. }
  670. sycl::queue &in_order_queue() { return *_q_in_order; }
  671. sycl::queue &out_of_order_queue() { return *_q_out_of_order; }
  672. sycl::queue &default_queue()
  673. {
  674. return in_order_queue();
  675. }
  676. void queues_wait_and_throw()
  677. {
  678. std::unique_lock<mutex_type> lock(m_mutex);
  679. std::vector<std::shared_ptr<sycl::queue>> current_queues(
  680. _queues);
  681. lock.unlock();
  682. for (const auto &q : current_queues)
  683. {
  684. q->wait_and_throw();
  685. }
  686. // Guard the destruct of current_queues to make sure the ref count is safe.
  687. lock.lock();
  688. }
  689. sycl::queue *create_queue(bool enable_exception_handler = false)
  690. {
  691. return create_in_order_queue(enable_exception_handler);
  692. }
  693. sycl::queue *create_queue(sycl::context context, sycl::device device,
  694. bool enable_exception_handler = false) {
  695. return create_in_order_queue(context, device, enable_exception_handler);
  696. }
  697. sycl::queue *create_in_order_queue(bool enable_exception_handler = false) {
  698. std::lock_guard<mutex_type> lock(m_mutex);
  699. return create_queue_impl(enable_exception_handler,
  700. sycl::property::queue::in_order());
  701. }
  702. sycl::queue *create_in_order_queue(sycl::context context, sycl::device device,
  703. bool enable_exception_handler = false) {
  704. std::lock_guard<mutex_type> lock(m_mutex);
  705. return create_queue_impl(context, device, enable_exception_handler,
  706. sycl::property::queue::in_order());
  707. }
  708. sycl::queue *create_out_of_order_queue(bool enable_exception_handler = false) {
  709. std::lock_guard<mutex_type> lock(m_mutex);
  710. return create_queue_impl(enable_exception_handler);
  711. }
  712. void destroy_queue(sycl::queue *&queue)
  713. {
  714. std::lock_guard<mutex_type> lock(m_mutex);
  715. _queues.erase(std::remove_if(_queues.begin(), _queues.end(),
  716. [=](const std::shared_ptr<sycl::queue> &q) -> bool
  717. {
  718. return q.get() == queue;
  719. }),
  720. _queues.end());
  721. queue = nullptr;
  722. }
  723. void set_saved_queue(sycl::queue *q)
  724. {
  725. std::lock_guard<mutex_type> lock(m_mutex);
  726. _saved_queue = q;
  727. }
  728. sycl::queue *get_saved_queue() const
  729. {
  730. std::lock_guard<mutex_type> lock(m_mutex);
  731. return _saved_queue;
  732. }
  733. sycl::context get_context() const { return _ctx; }
  734. private:
  735. void clear_queues()
  736. {
  737. _queues.clear();
  738. _q_in_order = _q_out_of_order = _saved_queue = nullptr;
  739. }
  740. void init_queues()
  741. {
  742. _q_in_order = create_queue_impl(true, sycl::property::queue::in_order());
  743. _q_out_of_order = create_queue_impl(true);
  744. _saved_queue = &default_queue();
  745. }
  746. /// Caller should acquire resource \p m_mutex before calling this function.
  747. template <class... Properties>
  748. sycl::queue *create_queue_impl(bool enable_exception_handler,
  749. Properties... properties)
  750. {
  751. sycl::async_handler eh = {};
  752. if (enable_exception_handler)
  753. {
  754. eh = exception_handler;
  755. }
  756. _queues.push_back(std::make_shared<sycl::queue>(
  757. _ctx, *this, eh,
  758. sycl::property_list(
  759. #ifdef DPCT_PROFILING_ENABLED
  760. sycl::property::queue::enable_profiling(),
  761. #endif
  762. properties...)));
  763. return _queues.back().get();
  764. }
  765. template <class... Properties>
  766. sycl::queue *create_queue_impl(sycl::context context, sycl::device device,
  767. bool enable_exception_handler,
  768. Properties... properties) {
  769. sycl::async_handler eh = {};
  770. if (enable_exception_handler) {
  771. eh = exception_handler;
  772. }
  773. _queues.push_back(std::make_shared<sycl::queue>(
  774. context, device, eh,
  775. sycl::property_list(
  776. #ifdef DPCT_PROFILING_ENABLED
  777. sycl::property::queue::enable_profiling(),
  778. #endif
  779. properties...)));
  780. return _queues.back().get();
  781. }
  782. void get_version(int &major, int &minor) const
  783. {
  784. detail::get_version(*this, major, minor);
  785. }
  786. sycl::queue *_q_in_order, *_q_out_of_order;
  787. sycl::queue *_saved_queue;
  788. sycl::context _ctx;
  789. std::vector<std::shared_ptr<sycl::queue>> _queues;
  790. mutable mutex_type m_mutex;
  791. };
  792. /// device manager
  793. class dev_mgr
  794. {
  795. public:
  796. device_ext &current_device()
  797. {
  798. unsigned int dev_id = current_device_id();
  799. check_id(dev_id);
  800. return *_devs[dev_id];
  801. }
  802. device_ext &cpu_device() const
  803. {
  804. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  805. if (_cpu_device == -1)
  806. {
  807. throw std::runtime_error("no valid cpu device");
  808. }
  809. else
  810. {
  811. return *_devs[_cpu_device];
  812. }
  813. }
  814. device_ext &get_device(unsigned int id) const
  815. {
  816. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  817. check_id(id);
  818. return *_devs[id];
  819. }
  820. unsigned int current_device_id() const
  821. {
  822. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  823. auto it = _thread2dev_map.find(get_tid());
  824. if (it != _thread2dev_map.end())
  825. return it->second;
  826. return DEFAULT_DEVICE_ID;
  827. }
  828. /// Select device with a device ID.
  829. /// \param [in] id The id of the device which can
  830. /// be obtained through get_device_id(const sycl::device).
  831. void select_device(unsigned int id)
  832. {
  833. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  834. check_id(id);
  835. _thread2dev_map[get_tid()] = id;
  836. }
  837. unsigned int device_count() { return _devs.size(); }
  838. unsigned int get_device_id(const sycl::device &dev)
  839. {
  840. unsigned int id = 0;
  841. for (auto dev_item : _devs)
  842. {
  843. if (*dev_item == dev)
  844. {
  845. break;
  846. }
  847. id++;
  848. }
  849. return id;
  850. }
  851. template <class DeviceSelector>
  852. std::enable_if_t<
  853. std::is_invocable_r_v<int, DeviceSelector, const sycl::device &>>
  854. select_device(const DeviceSelector &selector = sycl::gpu_selector_v)
  855. {
  856. sycl::device selected_device = sycl::device(selector);
  857. unsigned int selected_device_id = get_device_id(selected_device);
  858. select_device(selected_device_id);
  859. }
  860. /// Returns the instance of device manager singleton.
  861. static dev_mgr &instance()
  862. {
  863. static dev_mgr d_m;
  864. return d_m;
  865. }
  866. dev_mgr(const dev_mgr &) = delete;
  867. dev_mgr &operator=(const dev_mgr &) = delete;
  868. dev_mgr(dev_mgr &&) = delete;
  869. dev_mgr &operator=(dev_mgr &&) = delete;
  870. private:
  871. mutable std::recursive_mutex m_mutex;
  872. static bool compare_dev(sycl::device &device1, sycl::device &device2)
  873. {
  874. dpct::device_info prop1;
  875. dpct::get_device_info(prop1, device1);
  876. dpct::device_info prop2;
  877. dpct::get_device_info(prop2, device2);
  878. return prop1.get_max_compute_units() > prop2.get_max_compute_units();
  879. }
  880. static int convert_backend_index(std::string & backend) {
  881. if (backend == "ext_oneapi_level_zero:gpu") return 0;
  882. if (backend == "opencl:gpu") return 1;
  883. if (backend == "ext_oneapi_cuda:gpu") return 2;
  884. if (backend == "ext_oneapi_hip:gpu") return 3;
  885. if (backend == "opencl:cpu") return 4;
  886. if (backend == "opencl:acc") return 5;
  887. printf("convert_backend_index: can't handle backend=%s\n", backend.c_str());
  888. GGML_ASSERT(false);
  889. }
  890. static bool compare_backend(std::string &backend1, std::string &backend2) {
  891. return convert_backend_index(backend1) < convert_backend_index(backend2);
  892. }
  893. dev_mgr()
  894. {
  895. sycl::device default_device =
  896. sycl::device(sycl::default_selector_v);
  897. _devs.push_back(std::make_shared<device_ext>(default_device));
  898. std::vector<sycl::device> sycl_all_devs;
  899. // Collect other devices except for the default device.
  900. if (default_device.is_cpu())
  901. _cpu_device = 0;
  902. auto Platforms = sycl::platform::get_platforms();
  903. // Keep track of the number of devices per backend
  904. std::map<sycl::backend, size_t> DeviceNums;
  905. std::map<std::string, std::vector<sycl::device>> backend_devices;
  906. while (!Platforms.empty()) {
  907. auto Platform = Platforms.back();
  908. Platforms.pop_back();
  909. auto devices = Platform.get_devices();
  910. std::string backend_type = get_device_backend_and_type(devices[0]);
  911. for (const auto &device : devices) {
  912. backend_devices[backend_type].push_back(device);
  913. }
  914. }
  915. std::vector<std::string> keys;
  916. for(auto it = backend_devices.begin(); it != backend_devices.end(); ++it) {
  917. keys.push_back(it->first);
  918. }
  919. std::sort(keys.begin(), keys.end(), compare_backend);
  920. for (auto &key : keys) {
  921. std::vector<sycl::device> devs = backend_devices[key];
  922. std::sort(devs.begin(), devs.end(), compare_dev);
  923. for (const auto &dev : devs) {
  924. sycl_all_devs.push_back(dev);
  925. }
  926. }
  927. for (auto &dev : sycl_all_devs)
  928. {
  929. if (dev == default_device)
  930. {
  931. continue;
  932. }
  933. _devs.push_back(std::make_shared<device_ext>(dev));
  934. if (_cpu_device == -1 && dev.is_cpu())
  935. {
  936. _cpu_device = _devs.size() - 1;
  937. }
  938. }
  939. }
  940. void check_id(unsigned int id) const
  941. {
  942. if (id >= _devs.size())
  943. {
  944. throw std::runtime_error("invalid device id");
  945. }
  946. }
  947. std::vector<std::shared_ptr<device_ext>> _devs;
  948. /// DEFAULT_DEVICE_ID is used, if current_device_id() can not find current
  949. /// thread id in _thread2dev_map, which means default device should be used
  950. /// for the current thread.
  951. const unsigned int DEFAULT_DEVICE_ID = 0;
  952. /// thread-id to device-id map.
  953. std::map<unsigned int, unsigned int> _thread2dev_map;
  954. int _cpu_device = -1;
  955. };
  956. static inline sycl::queue &get_default_queue()
  957. {
  958. return dev_mgr::instance().current_device().default_queue();
  959. }
  960. namespace detail
  961. {
  962. enum class pointer_access_attribute
  963. {
  964. host_only = 0,
  965. device_only,
  966. host_device,
  967. end
  968. };
  969. static pointer_access_attribute get_pointer_attribute(sycl::queue &q,
  970. const void *ptr)
  971. {
  972. switch (sycl::get_pointer_type(ptr, q.get_context()))
  973. {
  974. case sycl::usm::alloc::unknown:
  975. return pointer_access_attribute::host_only;
  976. case sycl::usm::alloc::device:
  977. return pointer_access_attribute::device_only;
  978. case sycl::usm::alloc::shared:
  979. case sycl::usm::alloc::host:
  980. return pointer_access_attribute::host_device;
  981. }
  982. }
  983. template <typename ArgT>
  984. inline constexpr std::uint64_t get_type_combination_id(ArgT Val)
  985. {
  986. static_assert((unsigned char)library_data_t::library_data_t_size <=
  987. std::numeric_limits<unsigned char>::max() &&
  988. "library_data_t size exceeds limit.");
  989. static_assert(std::is_same_v<ArgT, library_data_t>, "Unsupported ArgT");
  990. return (std::uint64_t)Val;
  991. }
  992. template <typename FirstT, typename... RestT>
  993. inline constexpr std::uint64_t get_type_combination_id(FirstT FirstVal,
  994. RestT... RestVal)
  995. {
  996. static_assert((std::uint8_t)library_data_t::library_data_t_size <=
  997. std::numeric_limits<unsigned char>::max() &&
  998. "library_data_t size exceeds limit.");
  999. static_assert(sizeof...(RestT) <= 8 && "Too many parameters");
  1000. static_assert(std::is_same_v<FirstT, library_data_t>, "Unsupported FirstT");
  1001. return get_type_combination_id(RestVal...) << 8 | ((std::uint64_t)FirstVal);
  1002. }
  1003. class mem_mgr
  1004. {
  1005. mem_mgr()
  1006. {
  1007. // Reserved address space, no real memory allocation happens here.
  1008. #if defined(__linux__)
  1009. mapped_address_space =
  1010. (byte_t *)mmap(nullptr, mapped_region_size, PROT_NONE,
  1011. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  1012. #elif defined(_WIN64)
  1013. mapped_address_space = (byte_t *)VirtualAlloc(
  1014. NULL, // NULL specified as the base address parameter
  1015. mapped_region_size, // Size of allocation
  1016. MEM_RESERVE, // Allocate reserved pages
  1017. PAGE_NOACCESS); // Protection = no access
  1018. #else
  1019. #error "Only support Windows and Linux."
  1020. #endif
  1021. next_free = mapped_address_space;
  1022. };
  1023. public:
  1024. using buffer_id_t = int;
  1025. struct allocation
  1026. {
  1027. buffer_t buffer;
  1028. byte_t *alloc_ptr;
  1029. size_t size;
  1030. };
  1031. ~mem_mgr()
  1032. {
  1033. #if defined(__linux__)
  1034. munmap(mapped_address_space, mapped_region_size);
  1035. #elif defined(_WIN64)
  1036. VirtualFree(mapped_address_space, 0, MEM_RELEASE);
  1037. #else
  1038. #error "Only support Windows and Linux."
  1039. #endif
  1040. };
  1041. mem_mgr(const mem_mgr &) = delete;
  1042. mem_mgr &operator=(const mem_mgr &) = delete;
  1043. mem_mgr(mem_mgr &&) = delete;
  1044. mem_mgr &operator=(mem_mgr &&) = delete;
  1045. /// Allocate
  1046. void *mem_alloc(size_t size)
  1047. {
  1048. if (!size)
  1049. return nullptr;
  1050. std::lock_guard<std::mutex> lock(m_mutex);
  1051. if (next_free + size > mapped_address_space + mapped_region_size)
  1052. {
  1053. throw std::runtime_error("dpct_malloc: out of memory for virtual memory pool");
  1054. }
  1055. // Allocation
  1056. sycl::range<1> r(size);
  1057. buffer_t buf(r);
  1058. allocation A{buf, next_free, size};
  1059. // Map allocation to device pointer
  1060. void *result = next_free;
  1061. m_map.emplace(next_free + size, A);
  1062. // Update pointer to the next free space.
  1063. next_free += (size + extra_padding + alignment - 1) & ~(alignment - 1);
  1064. return result;
  1065. }
  1066. /// Deallocate
  1067. void mem_free(const void *ptr)
  1068. {
  1069. if (!ptr)
  1070. return;
  1071. std::lock_guard<std::mutex> lock(m_mutex);
  1072. auto it = get_map_iterator(ptr);
  1073. m_map.erase(it);
  1074. }
  1075. /// map: device pointer -> allocation(buffer, alloc_ptr, size)
  1076. allocation translate_ptr(const void *ptr)
  1077. {
  1078. std::lock_guard<std::mutex> lock(m_mutex);
  1079. auto it = get_map_iterator(ptr);
  1080. return it->second;
  1081. }
  1082. /// Check if the pointer represents device pointer or not.
  1083. bool is_device_ptr(const void *ptr) const
  1084. {
  1085. std::lock_guard<std::mutex> lock(m_mutex);
  1086. return (mapped_address_space <= ptr) &&
  1087. (ptr < mapped_address_space + mapped_region_size);
  1088. }
  1089. /// Returns the instance of memory manager singleton.
  1090. static mem_mgr &instance()
  1091. {
  1092. static mem_mgr m;
  1093. return m;
  1094. }
  1095. private:
  1096. std::map<byte_t *, allocation> m_map;
  1097. mutable std::mutex m_mutex;
  1098. byte_t *mapped_address_space;
  1099. byte_t *next_free;
  1100. const size_t mapped_region_size = 128ull * 1024 * 1024 * 1024;
  1101. const size_t alignment = 256;
  1102. /// This padding may be defined to some positive value to debug
  1103. /// out of bound accesses.
  1104. const size_t extra_padding = 0;
  1105. std::map<byte_t *, allocation>::iterator get_map_iterator(const void *ptr)
  1106. {
  1107. auto it = m_map.upper_bound((byte_t *)ptr);
  1108. if (it == m_map.end())
  1109. {
  1110. // Not a virtual pointer.
  1111. throw std::runtime_error("can not get buffer from non-virtual pointer");
  1112. }
  1113. const allocation &alloc = it->second;
  1114. if (ptr < alloc.alloc_ptr)
  1115. {
  1116. // Out of bound.
  1117. // This may happen if there's a gap between allocations due to alignment
  1118. // or extra padding and pointer points to this gap.
  1119. throw std::runtime_error("invalid virtual pointer");
  1120. }
  1121. return it;
  1122. }
  1123. };
  1124. template <class T, memory_region Memory, size_t Dimension>
  1125. class accessor;
  1126. template <memory_region Memory, class T = byte_t>
  1127. class memory_traits
  1128. {
  1129. public:
  1130. static constexpr sycl::access::target target =
  1131. sycl::access::target::device;
  1132. static constexpr sycl::access_mode mode =
  1133. (Memory == constant) ? sycl::access_mode::read
  1134. : sycl::access_mode::read_write;
  1135. static constexpr size_t type_size = sizeof(T);
  1136. using element_t =
  1137. typename std::conditional<Memory == constant, const T, T>::type;
  1138. using value_t = typename std::remove_cv<T>::type;
  1139. template <size_t Dimension = 1>
  1140. using accessor_t = typename std::conditional<
  1141. Memory == local, sycl::local_accessor<value_t, Dimension>,
  1142. sycl::accessor<T, Dimension, mode, target>>::type;
  1143. using pointer_t = T *;
  1144. };
  1145. static inline void *dpct_malloc(size_t size, sycl::queue &q)
  1146. {
  1147. return sycl::malloc_device(size, q.get_device(), q.get_context());
  1148. }
  1149. #define PITCH_DEFAULT_ALIGN(x) (((x) + 31) & ~(0x1F))
  1150. static inline void *dpct_malloc(size_t &pitch, size_t x, size_t y, size_t z,
  1151. sycl::queue &q)
  1152. {
  1153. pitch = PITCH_DEFAULT_ALIGN(x);
  1154. return dpct_malloc(pitch * y * z, q);
  1155. }
  1156. /**
  1157. * @brief Sets \p value to the first \p size elements starting from \p dev_ptr in \p q.
  1158. * @tparam valueT The type of the element to be set.
  1159. * @param [in] q The queue in which the operation is done.
  1160. * @param [in] dev_ptr Pointer to the virtual device memory address.
  1161. * @param [in] value The value to be set.
  1162. * @param [in] size Number of elements to be set to the value.
  1163. * @return An event representing the memset operation.
  1164. */
  1165. template <typename valueT>
  1166. static inline sycl::event dpct_memset(sycl::queue &q, void *dev_ptr,
  1167. valueT value, size_t size)
  1168. {
  1169. return q.fill(dev_ptr, value, size);
  1170. }
  1171. /**
  1172. * @brief Sets \p value to the 3D memory region pointed by \p data in \p q.
  1173. * @tparam valueT The type of the element to be set.
  1174. * @param [in] q The queue in which the operation is done.
  1175. * @param [in] data Pointer to the pitched device memory region.
  1176. * @param [in] value The value to be set.
  1177. * @param [in] size 3D memory region by number of elements.
  1178. * @return An event list representing the memset operations.
  1179. */
  1180. template <typename valueT>
  1181. static inline std::vector<sycl::event>
  1182. dpct_memset(sycl::queue &q, pitched_data data, valueT value,
  1183. sycl::range<3> size)
  1184. {
  1185. std::vector<sycl::event> event_list;
  1186. size_t slice = data.get_pitch() * data.get_y();
  1187. unsigned char *data_surface = (unsigned char *)data.get_data_ptr();
  1188. for (size_t z = 0; z < size.get(2); ++z)
  1189. {
  1190. unsigned char *data_ptr = data_surface;
  1191. for (size_t y = 0; y < size.get(1); ++y)
  1192. {
  1193. event_list.push_back(dpct_memset(q, data_ptr, value, size.get(0)));
  1194. data_ptr += data.get_pitch();
  1195. }
  1196. data_surface += slice;
  1197. }
  1198. return event_list;
  1199. }
  1200. /**
  1201. * @brief Sets \p val to the pitched 2D memory region pointed by \p ptr in \p q.
  1202. * @tparam valueT The type of the element to be set.
  1203. * @param [in] q The queue in which the operation is done.
  1204. * @param [in] ptr Pointer to the virtual device memory.
  1205. * @param [in] pitch The pitch size by number of elements, including padding.
  1206. * @param [in] val The value to be set.
  1207. * @param [in] x The width of memory region by number of elements.
  1208. * @param [in] y The height of memory region by number of elements.
  1209. * @return An event list representing the memset operations.
  1210. */
  1211. template <typename valueT>
  1212. static inline std::vector<sycl::event>
  1213. dpct_memset(sycl::queue &q, void *ptr, size_t pitch, valueT val, size_t x,
  1214. size_t y)
  1215. {
  1216. return dpct_memset(q, pitched_data(ptr, pitch, x, 1), val,
  1217. sycl::range<3>(x, y, 1));
  1218. }
  1219. static memcpy_direction deduce_memcpy_direction(sycl::queue &q, void *to_ptr,
  1220. const void *from_ptr,
  1221. memcpy_direction dir)
  1222. {
  1223. switch (dir)
  1224. {
  1225. case memcpy_direction::host_to_host:
  1226. case memcpy_direction::host_to_device:
  1227. case memcpy_direction::device_to_host:
  1228. case memcpy_direction::device_to_device:
  1229. return dir;
  1230. case memcpy_direction::automatic:
  1231. {
  1232. // table[to_attribute][from_attribute]
  1233. static const memcpy_direction
  1234. direction_table[static_cast<unsigned>(pointer_access_attribute::end)]
  1235. [static_cast<unsigned>(pointer_access_attribute::end)] =
  1236. {{memcpy_direction::host_to_host,
  1237. memcpy_direction::device_to_host,
  1238. memcpy_direction::host_to_host},
  1239. {memcpy_direction::host_to_device,
  1240. memcpy_direction::device_to_device,
  1241. memcpy_direction::device_to_device},
  1242. {memcpy_direction::host_to_host,
  1243. memcpy_direction::device_to_device,
  1244. memcpy_direction::device_to_device}};
  1245. return direction_table[static_cast<unsigned>(get_pointer_attribute(
  1246. q, to_ptr))][static_cast<unsigned>(get_pointer_attribute(q, from_ptr))];
  1247. }
  1248. default:
  1249. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1250. }
  1251. }
  1252. static sycl::event
  1253. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1254. memcpy_direction direction,
  1255. const std::vector<sycl::event> &dep_events = {})
  1256. {
  1257. if (!size)
  1258. return sycl::event{};
  1259. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1260. GGML_UNUSED(direction);
  1261. }
  1262. // Get actual copy range and make sure it will not exceed range.
  1263. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1264. size_t pitch)
  1265. {
  1266. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1267. }
  1268. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1269. size_t pitch)
  1270. {
  1271. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1272. }
  1273. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1274. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1275. static inline std::vector<sycl::event>
  1276. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1277. sycl::range<3> to_range, sycl::range<3> from_range,
  1278. sycl::id<3> to_id, sycl::id<3> from_id,
  1279. sycl::range<3> size, memcpy_direction direction,
  1280. const std::vector<sycl::event> &dep_events = {})
  1281. {
  1282. // RAII for host pointer
  1283. class host_buffer
  1284. {
  1285. void *_buf;
  1286. size_t _size;
  1287. sycl::queue &_q;
  1288. const std::vector<sycl::event> &_deps; // free operation depends
  1289. public:
  1290. host_buffer(size_t size, sycl::queue &q,
  1291. const std::vector<sycl::event> &deps)
  1292. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1293. void *get_ptr() const { return _buf; }
  1294. size_t get_size() const { return _size; }
  1295. ~host_buffer()
  1296. {
  1297. if (_buf)
  1298. {
  1299. _q.submit([&](sycl::handler &cgh)
  1300. {
  1301. cgh.depends_on(_deps);
  1302. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1303. }
  1304. }
  1305. };
  1306. std::vector<sycl::event> event_list;
  1307. size_t to_slice = to_range.get(1) * to_range.get(0),
  1308. from_slice = from_range.get(1) * from_range.get(0);
  1309. unsigned char *to_surface =
  1310. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1311. const unsigned char *from_surface =
  1312. (const unsigned char *)from_ptr +
  1313. get_offset(from_id, from_slice, from_range.get(0));
  1314. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1315. {
  1316. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1317. direction, dep_events)};
  1318. }
  1319. direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1320. size_t size_slice = size.get(1) * size.get(0);
  1321. switch (direction)
  1322. {
  1323. case host_to_host:
  1324. for (size_t z = 0; z < size.get(2); ++z)
  1325. {
  1326. unsigned char *to_ptr = to_surface;
  1327. const unsigned char *from_ptr = from_surface;
  1328. if (to_range.get(0) == from_range.get(0) &&
  1329. to_range.get(0) == size.get(0))
  1330. {
  1331. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1332. direction, dep_events));
  1333. }
  1334. else
  1335. {
  1336. for (size_t y = 0; y < size.get(1); ++y)
  1337. {
  1338. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1339. direction, dep_events));
  1340. to_ptr += to_range.get(0);
  1341. from_ptr += from_range.get(0);
  1342. }
  1343. }
  1344. to_surface += to_slice;
  1345. from_surface += from_slice;
  1346. }
  1347. break;
  1348. case host_to_device:
  1349. {
  1350. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1351. event_list);
  1352. std::vector<sycl::event> host_events;
  1353. if (to_slice == size_slice)
  1354. {
  1355. // Copy host data to a temp host buffer with the shape of target.
  1356. host_events =
  1357. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  1358. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  1359. host_to_host, dep_events);
  1360. }
  1361. else
  1362. {
  1363. // Copy host data to a temp host buffer with the shape of target.
  1364. host_events = dpct_memcpy(
  1365. q, buf.get_ptr(), from_surface, to_range, from_range,
  1366. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  1367. // If has padding data, not sure whether it is useless. So fill temp
  1368. // buffer with it.
  1369. std::vector<sycl::event>{
  1370. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  1371. device_to_host, dep_events)});
  1372. }
  1373. // Copy from temp host buffer to device with only one submit.
  1374. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  1375. buf.get_size(), host_to_device,
  1376. host_events));
  1377. break;
  1378. }
  1379. case device_to_host:
  1380. {
  1381. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  1382. event_list);
  1383. // Copy from host temp buffer to host target with reshaping.
  1384. event_list = dpct_memcpy(
  1385. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  1386. sycl::id<3>(0, 0, 0), size, host_to_host,
  1387. // Copy from device to temp host buffer with only one submit.
  1388. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  1389. buf.get_size(),
  1390. device_to_host, dep_events)});
  1391. break;
  1392. }
  1393. case device_to_device:
  1394. event_list.push_back(q.submit([&](sycl::handler &cgh){
  1395. cgh.depends_on(dep_events);
  1396. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  1397. size,
  1398. [=](sycl::id<3> id) {
  1399. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  1400. from_surface[get_offset(id, from_slice, from_range.get(0))];
  1401. }); }));
  1402. break;
  1403. default:
  1404. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1405. }
  1406. return event_list;
  1407. }
  1408. /// memcpy 2D/3D matrix specified by pitched_data.
  1409. static inline std::vector<sycl::event>
  1410. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  1411. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  1412. memcpy_direction direction = automatic)
  1413. {
  1414. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  1415. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  1416. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  1417. size, direction);
  1418. }
  1419. /// memcpy 2D matrix with pitch.
  1420. static inline std::vector<sycl::event>
  1421. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1422. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  1423. memcpy_direction direction = automatic)
  1424. {
  1425. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  1426. sycl::range<3>(from_pitch, y, 1),
  1427. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  1428. sycl::range<3>(x, y, 1), direction);
  1429. }
  1430. namespace deprecated
  1431. {
  1432. template <typename T, sycl::usm::alloc AllocKind>
  1433. class usm_allocator
  1434. {
  1435. private:
  1436. using Alloc = sycl::usm_allocator<T, AllocKind>;
  1437. Alloc _impl;
  1438. public:
  1439. using value_type = typename std::allocator_traits<Alloc>::value_type;
  1440. using pointer = typename std::allocator_traits<Alloc>::pointer;
  1441. using const_pointer = typename std::allocator_traits<Alloc>::const_pointer;
  1442. using void_pointer = typename std::allocator_traits<Alloc>::void_pointer;
  1443. using const_void_pointer =
  1444. typename std::allocator_traits<Alloc>::const_void_pointer;
  1445. using reference = typename std::allocator_traits<Alloc>::value_type &;
  1446. using const_reference =
  1447. const typename std::allocator_traits<Alloc>::value_type &;
  1448. using difference_type =
  1449. typename std::allocator_traits<Alloc>::difference_type;
  1450. using size_type = typename std::allocator_traits<Alloc>::size_type;
  1451. using propagate_on_container_copy_assignment = typename std::allocator_traits<
  1452. Alloc>::propagate_on_container_copy_assignment;
  1453. using propagate_on_container_move_assignment = typename std::allocator_traits<
  1454. Alloc>::propagate_on_container_move_assignment;
  1455. using propagate_on_container_swap =
  1456. typename std::allocator_traits<Alloc>::propagate_on_container_swap;
  1457. using is_always_equal =
  1458. typename std::allocator_traits<Alloc>::is_always_equal;
  1459. template <typename U>
  1460. struct rebind
  1461. {
  1462. typedef usm_allocator<U, AllocKind> other;
  1463. };
  1464. usm_allocator() : _impl(dpct::get_default_queue()) {}
  1465. ~usm_allocator() {}
  1466. usm_allocator(const usm_allocator &other) : _impl(other._impl) {}
  1467. usm_allocator(usm_allocator &&other) : _impl(std::move(other._impl)) {}
  1468. pointer address(reference r) { return &r; }
  1469. const_pointer address(const_reference r) { return &r; }
  1470. pointer allocate(size_type cnt, const_void_pointer hint = nullptr)
  1471. {
  1472. return std::allocator_traits<Alloc>::allocate(_impl, cnt, hint);
  1473. }
  1474. void deallocate(pointer p, size_type cnt)
  1475. {
  1476. std::allocator_traits<Alloc>::deallocate(_impl, p, cnt);
  1477. }
  1478. size_type max_size() const
  1479. {
  1480. return std::allocator_traits<Alloc>::max_size(_impl);
  1481. }
  1482. bool operator==(const usm_allocator &other) const { return _impl == other._impl; }
  1483. bool operator!=(const usm_allocator &other) const { return _impl != other._impl; }
  1484. };
  1485. } // namespace deprecated
  1486. inline void dpct_free(void *ptr,
  1487. const sycl::queue &q)
  1488. {
  1489. if (ptr)
  1490. {
  1491. sycl::free(ptr, q.get_context());
  1492. }
  1493. }
  1494. template <typename T>
  1495. inline auto get_memory(const void *x)
  1496. {
  1497. T *new_x = reinterpret_cast<T *>(const_cast<void *>(x));
  1498. return new_x;
  1499. }
  1500. template <typename T>
  1501. inline typename DataType<T>::T2 get_value(const T *s, sycl::queue &q)
  1502. {
  1503. using Ty = typename DataType<T>::T2;
  1504. Ty s_h;
  1505. if (get_pointer_attribute(q, s) == pointer_access_attribute::device_only)
  1506. detail::dpct_memcpy(q, (void *)&s_h, (const void *)s, sizeof(T), device_to_host)
  1507. .wait();
  1508. else
  1509. s_h = *reinterpret_cast<const Ty *>(s);
  1510. return s_h;
  1511. }
  1512. } // namespace detail
  1513. template <typename T>
  1514. inline auto get_value(const T *s, sycl::queue &q)
  1515. {
  1516. return detail::get_value(s, q);
  1517. }
  1518. namespace detail
  1519. {
  1520. template <class Ta, class Tb, class Tc, class Ts>
  1521. inline void gemm_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1522. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1523. const void *alpha, const void *a, int lda, const void *b,
  1524. int ldb, const void *beta, void *c, int ldc)
  1525. {
  1526. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1527. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1528. auto data_a = get_memory<const Ta>(a);
  1529. auto data_b = get_memory<const Tb>(b);
  1530. auto data_c = get_memory<Tc>(c);
  1531. oneapi::mkl::blas::column_major::gemm(
  1532. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1533. data_b, ldb, beta_value, data_c, ldc);
  1534. }
  1535. template <typename VecT, class BinaryOperation, class = void>
  1536. class vectorized_binary
  1537. {
  1538. public:
  1539. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1540. {
  1541. VecT v4;
  1542. for (size_t i = 0; i < v4.size(); ++i)
  1543. {
  1544. v4[i] = binary_op(a[i], b[i]);
  1545. }
  1546. return v4;
  1547. }
  1548. };
  1549. template <typename VecT, class BinaryOperation>
  1550. class vectorized_binary<
  1551. VecT, BinaryOperation,
  1552. std::void_t<std::invoke_result_t<BinaryOperation, VecT, VecT>>>
  1553. {
  1554. public:
  1555. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1556. {
  1557. return binary_op(a, b).template as<VecT>();
  1558. }
  1559. };
  1560. template <class Ta, class Tb, class Tc, class Ts>
  1561. inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1562. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1563. const void *alpha, const void **a, int lda,
  1564. const void **b, int ldb, const void *beta, void **c,
  1565. int ldc, int batch_size)
  1566. {
  1567. struct matrix_info_t
  1568. {
  1569. oneapi::mkl::transpose transpose_info[2];
  1570. Ts value_info[2];
  1571. std::int64_t size_info[3];
  1572. std::int64_t ld_info[3];
  1573. std::int64_t groupsize_info;
  1574. };
  1575. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1576. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1577. matrix_info_t *matrix_info =
  1578. (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
  1579. matrix_info->transpose_info[0] = a_trans;
  1580. matrix_info->transpose_info[1] = b_trans;
  1581. matrix_info->value_info[0] = alpha_value;
  1582. matrix_info->value_info[1] = beta_value;
  1583. matrix_info->size_info[0] = m;
  1584. matrix_info->size_info[1] = n;
  1585. matrix_info->size_info[2] = k;
  1586. matrix_info->ld_info[0] = lda;
  1587. matrix_info->ld_info[1] = ldb;
  1588. matrix_info->ld_info[2] = ldc;
  1589. matrix_info->groupsize_info = batch_size;
  1590. sycl::event e = oneapi::mkl::blas::column_major::gemm_batch(
  1591. q, matrix_info->transpose_info, matrix_info->transpose_info + 1,
  1592. matrix_info->size_info, matrix_info->size_info + 1,
  1593. matrix_info->size_info + 2, matrix_info->value_info,
  1594. reinterpret_cast<const Ta **>(a), matrix_info->ld_info,
  1595. reinterpret_cast<const Tb **>(b), matrix_info->ld_info + 1,
  1596. matrix_info->value_info + 1, reinterpret_cast<Tc **>(c),
  1597. matrix_info->ld_info + 2, 1, &(matrix_info->groupsize_info));
  1598. q.submit([&](sycl::handler &cgh)
  1599. {
  1600. cgh.depends_on(e);
  1601. cgh.host_task([=] { std::free(matrix_info); }); });
  1602. }
  1603. template <class Ta, class Tb, class Tc, class Ts>
  1604. inline void
  1605. gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1606. oneapi::mkl::transpose b_trans, int m, int n,
  1607. int k, const void *alpha, const void *a, int lda,
  1608. long long int stride_a, const void *b, int ldb,
  1609. long long int stride_b, const void *beta, void *c,
  1610. int ldc, long long int stride_c, int batch_size)
  1611. {
  1612. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1613. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1614. auto data_a = get_memory<const Ta>(a);
  1615. auto data_b = get_memory<const Tb>(b);
  1616. auto data_c = get_memory<Tc>(c);
  1617. oneapi::mkl::blas::column_major::gemm_batch(
  1618. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1619. stride_a, data_b, ldb, stride_b, beta_value,
  1620. data_c, ldc, stride_c, batch_size);
  1621. }
  1622. } // namespace detail
  1623. template <typename VecT, class BinaryOperation>
  1624. inline unsigned vectorized_binary(unsigned a, unsigned b,
  1625. const BinaryOperation binary_op)
  1626. {
  1627. sycl::vec<unsigned, 1> v0{a}, v1{b};
  1628. auto v2 = v0.as<VecT>();
  1629. auto v3 = v1.as<VecT>();
  1630. auto v4 =
  1631. detail::vectorized_binary<VecT, BinaryOperation>()(v2, v3, binary_op);
  1632. v0 = v4.template as<sycl::vec<unsigned, 1>>();
  1633. return v0;
  1634. }
  1635. static void async_dpct_memcpy(void *to_ptr, const void *from_ptr, size_t size,
  1636. memcpy_direction direction = automatic,
  1637. sycl::queue &q = dpct::get_default_queue())
  1638. {
  1639. detail::dpct_memcpy(q, to_ptr, from_ptr, size, direction);
  1640. }
  1641. static inline unsigned int select_device(unsigned int id)
  1642. {
  1643. dev_mgr::instance().select_device(id);
  1644. return id;
  1645. }
  1646. template <typename T>
  1647. T permute_sub_group_by_xor(sycl::sub_group g, T x, unsigned int mask,
  1648. unsigned int logical_sub_group_size = 32)
  1649. {
  1650. unsigned int id = g.get_local_linear_id();
  1651. unsigned int start_index =
  1652. id / logical_sub_group_size * logical_sub_group_size;
  1653. unsigned int target_offset = (id % logical_sub_group_size) ^ mask;
  1654. return sycl::select_from_group(g, x,
  1655. target_offset < logical_sub_group_size
  1656. ? start_index + target_offset
  1657. : id);
  1658. }
  1659. template <typename T>
  1660. sycl::vec<T, 4> extract_and_sign_or_zero_extend4(T val)
  1661. {
  1662. return sycl::vec<T, 1>(val)
  1663. .template as<sycl::vec<
  1664. std::conditional_t<std::is_signed_v<T>, int8_t, uint8_t>, 4>>()
  1665. .template convert<T>();
  1666. }
  1667. template <typename T1, typename T2>
  1668. using dot_product_acc_t =
  1669. std::conditional_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>,
  1670. uint32_t, int32_t>;
  1671. template <typename T1, typename T2, typename T3>
  1672. inline auto dp4a(T1 a, T2 b, T3 c)
  1673. {
  1674. dot_product_acc_t<T1, T2> res = c;
  1675. auto va = extract_and_sign_or_zero_extend4(a);
  1676. auto vb = extract_and_sign_or_zero_extend4(b);
  1677. res += va[0] * vb[0];
  1678. res += va[1] * vb[1];
  1679. res += va[2] * vb[2];
  1680. res += va[3] * vb[3];
  1681. return res;
  1682. }
  1683. struct sub_sat
  1684. {
  1685. template <typename T>
  1686. auto operator()(const T x, const T y) const
  1687. {
  1688. return sycl::sub_sat(x, y);
  1689. }
  1690. };
  1691. template <typename S, typename T>
  1692. inline T vectorized_min(T a, T b)
  1693. {
  1694. sycl::vec<T, 1> v0{a}, v1{b};
  1695. auto v2 = v0.template as<S>();
  1696. auto v3 = v1.template as<S>();
  1697. auto v4 = sycl::min(v2, v3);
  1698. v0 = v4.template as<sycl::vec<T, 1>>();
  1699. return v0;
  1700. }
  1701. inline float pow(const float a, const int b) { return sycl::pown(a, b); }
  1702. inline double pow(const double a, const int b) { return sycl::pown(a, b); }
  1703. inline float pow(const float a, const float b) { return sycl::pow(a, b); }
  1704. inline double pow(const double a, const double b) { return sycl::pow(a, b); }
  1705. template <typename T, typename U>
  1706. inline typename std::enable_if_t<std::is_floating_point_v<T>, T>
  1707. pow(const T a, const U b)
  1708. {
  1709. return sycl::pow(a, static_cast<T>(b));
  1710. }
  1711. template <typename T, typename U>
  1712. inline typename std::enable_if_t<!std::is_floating_point_v<T>, double>
  1713. pow(const T a, const U b)
  1714. {
  1715. return sycl::pow(static_cast<double>(a), static_cast<double>(b));
  1716. }
  1717. inline double min(const double a, const float b)
  1718. {
  1719. return sycl::fmin(a, static_cast<double>(b));
  1720. }
  1721. inline double min(const float a, const double b)
  1722. {
  1723. return sycl::fmin(static_cast<double>(a), b);
  1724. }
  1725. inline float min(const float a, const float b) { return sycl::fmin(a, b); }
  1726. inline double min(const double a, const double b) { return sycl::fmin(a, b); }
  1727. inline std::uint32_t min(const std::uint32_t a, const std::int32_t b)
  1728. {
  1729. return sycl::min(a, static_cast<std::uint32_t>(b));
  1730. }
  1731. inline std::uint32_t min(const std::int32_t a, const std::uint32_t b)
  1732. {
  1733. return sycl::min(static_cast<std::uint32_t>(a), b);
  1734. }
  1735. inline std::int32_t min(const std::int32_t a, const std::int32_t b)
  1736. {
  1737. return sycl::min(a, b);
  1738. }
  1739. inline std::uint32_t min(const std::uint32_t a, const std::uint32_t b)
  1740. {
  1741. return sycl::min(a, b);
  1742. }
  1743. inline std::uint64_t min(const std::uint64_t a, const std::int64_t b)
  1744. {
  1745. return sycl::min(a, static_cast<std::uint64_t>(b));
  1746. }
  1747. inline std::uint64_t min(const std::int64_t a, const std::uint64_t b)
  1748. {
  1749. return sycl::min(static_cast<std::uint64_t>(a), b);
  1750. }
  1751. inline std::int64_t min(const std::int64_t a, const std::int64_t b)
  1752. {
  1753. return sycl::min(a, b);
  1754. }
  1755. inline std::uint64_t min(const std::uint64_t a, const std::uint64_t b)
  1756. {
  1757. return sycl::min(a, b);
  1758. }
  1759. inline std::uint64_t min(const std::uint64_t a, const std::int32_t b)
  1760. {
  1761. return sycl::min(a, static_cast<std::uint64_t>(b));
  1762. }
  1763. inline std::uint64_t min(const std::int32_t a, const std::uint64_t b)
  1764. {
  1765. return sycl::min(static_cast<std::uint64_t>(a), b);
  1766. }
  1767. inline std::uint64_t min(const std::uint64_t a, const std::uint32_t b)
  1768. {
  1769. return sycl::min(a, static_cast<std::uint64_t>(b));
  1770. }
  1771. inline std::uint64_t min(const std::uint32_t a, const std::uint64_t b)
  1772. {
  1773. return sycl::min(static_cast<std::uint64_t>(a), b);
  1774. }
  1775. // max function overloads.
  1776. // For floating-point types, `float` or `double` arguments are acceptable.
  1777. // For integer types, `std::uint32_t`, `std::int32_t`, `std::uint64_t` or
  1778. // `std::int64_t` type arguments are acceptable.
  1779. inline double max(const double a, const float b)
  1780. {
  1781. return sycl::fmax(a, static_cast<double>(b));
  1782. }
  1783. inline double max(const float a, const double b)
  1784. {
  1785. return sycl::fmax(static_cast<double>(a), b);
  1786. }
  1787. inline float max(const float a, const float b) { return sycl::fmax(a, b); }
  1788. inline double max(const double a, const double b) { return sycl::fmax(a, b); }
  1789. inline std::uint32_t max(const std::uint32_t a, const std::int32_t b)
  1790. {
  1791. return sycl::max(a, static_cast<std::uint32_t>(b));
  1792. }
  1793. inline std::uint32_t max(const std::int32_t a, const std::uint32_t b)
  1794. {
  1795. return sycl::max(static_cast<std::uint32_t>(a), b);
  1796. }
  1797. inline std::int32_t max(const std::int32_t a, const std::int32_t b)
  1798. {
  1799. return sycl::max(a, b);
  1800. }
  1801. inline std::uint32_t max(const std::uint32_t a, const std::uint32_t b)
  1802. {
  1803. return sycl::max(a, b);
  1804. }
  1805. inline std::uint64_t max(const std::uint64_t a, const std::int64_t b)
  1806. {
  1807. return sycl::max(a, static_cast<std::uint64_t>(b));
  1808. }
  1809. inline std::uint64_t max(const std::int64_t a, const std::uint64_t b)
  1810. {
  1811. return sycl::max(static_cast<std::uint64_t>(a), b);
  1812. }
  1813. inline std::int64_t max(const std::int64_t a, const std::int64_t b)
  1814. {
  1815. return sycl::max(a, b);
  1816. }
  1817. inline std::uint64_t max(const std::uint64_t a, const std::uint64_t b)
  1818. {
  1819. return sycl::max(a, b);
  1820. }
  1821. inline std::uint64_t max(const std::uint64_t a, const std::int32_t b)
  1822. {
  1823. return sycl::max(a, static_cast<std::uint64_t>(b));
  1824. }
  1825. inline std::uint64_t max(const std::int32_t a, const std::uint64_t b)
  1826. {
  1827. return sycl::max(static_cast<std::uint64_t>(a), b);
  1828. }
  1829. inline std::uint64_t max(const std::uint64_t a, const std::uint32_t b)
  1830. {
  1831. return sycl::max(a, static_cast<std::uint64_t>(b));
  1832. }
  1833. inline std::uint64_t max(const std::uint32_t a, const std::uint64_t b)
  1834. {
  1835. return sycl::max(static_cast<std::uint64_t>(a), b);
  1836. }
  1837. inline void
  1838. has_capability_or_fail(const sycl::device &dev,
  1839. const std::initializer_list<sycl::aspect> &props)
  1840. {
  1841. for (const auto &it : props)
  1842. {
  1843. if (dev.has(it))
  1844. continue;
  1845. switch (it)
  1846. {
  1847. case sycl::aspect::fp64:
  1848. throw std::runtime_error("'double' is not supported in '" +
  1849. dev.get_info<sycl::info::device::name>() +
  1850. "' device");
  1851. break;
  1852. case sycl::aspect::fp16:
  1853. throw std::runtime_error("'half' is not supported in '" +
  1854. dev.get_info<sycl::info::device::name>() +
  1855. "' device");
  1856. break;
  1857. default:
  1858. #define __SYCL_ASPECT(ASPECT, ID) \
  1859. case sycl::aspect::ASPECT: \
  1860. return #ASPECT;
  1861. #define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
  1862. #define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
  1863. auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string
  1864. {
  1865. switch (AspectNum)
  1866. {
  1867. #include <sycl/info/aspects.def>
  1868. #include <sycl/info/aspects_deprecated.def>
  1869. default:
  1870. return "unknown aspect";
  1871. }
  1872. };
  1873. #undef __SYCL_ASPECT_DEPRECATED_ALIAS
  1874. #undef __SYCL_ASPECT_DEPRECATED
  1875. #undef __SYCL_ASPECT
  1876. throw std::runtime_error(
  1877. "'" + getAspectNameStr(it) + "' is not supported in '" +
  1878. dev.get_info<sycl::info::device::name>() + "' device");
  1879. }
  1880. break;
  1881. }
  1882. }
  1883. static inline unsigned int get_current_device_id()
  1884. {
  1885. return dev_mgr::instance().current_device_id();
  1886. }
  1887. static inline device_ext &get_current_device()
  1888. {
  1889. return dev_mgr::instance().current_device();
  1890. }
  1891. static inline sycl::queue &get_in_order_queue()
  1892. {
  1893. return dev_mgr::instance().current_device().in_order_queue();
  1894. }
  1895. static sycl::event
  1896. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1897. memcpy_direction direction,
  1898. const std::vector<sycl::event> &dep_events = {})
  1899. {
  1900. if (!size)
  1901. return sycl::event{};
  1902. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1903. GGML_UNUSED(direction);
  1904. }
  1905. // Get actual copy range and make sure it will not exceed range.
  1906. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1907. size_t pitch)
  1908. {
  1909. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1910. }
  1911. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1912. size_t pitch)
  1913. {
  1914. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1915. }
  1916. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1917. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1918. static inline std::vector<sycl::event>
  1919. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1920. sycl::range<3> to_range, sycl::range<3> from_range,
  1921. sycl::id<3> to_id, sycl::id<3> from_id,
  1922. sycl::range<3> size, memcpy_direction direction,
  1923. const std::vector<sycl::event> &dep_events = {})
  1924. {
  1925. // RAII for host pointer
  1926. class host_buffer
  1927. {
  1928. void *_buf;
  1929. size_t _size;
  1930. sycl::queue &_q;
  1931. const std::vector<sycl::event> &_deps; // free operation depends
  1932. public:
  1933. host_buffer(size_t size, sycl::queue &q,
  1934. const std::vector<sycl::event> &deps)
  1935. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1936. void *get_ptr() const { return _buf; }
  1937. size_t get_size() const { return _size; }
  1938. ~host_buffer()
  1939. {
  1940. if (_buf)
  1941. {
  1942. _q.submit([&](sycl::handler &cgh)
  1943. {
  1944. cgh.depends_on(_deps);
  1945. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1946. }
  1947. }
  1948. };
  1949. std::vector<sycl::event> event_list;
  1950. size_t to_slice = to_range.get(1) * to_range.get(0),
  1951. from_slice = from_range.get(1) * from_range.get(0);
  1952. unsigned char *to_surface =
  1953. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1954. const unsigned char *from_surface =
  1955. (const unsigned char *)from_ptr +
  1956. get_offset(from_id, from_slice, from_range.get(0));
  1957. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1958. {
  1959. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1960. direction, dep_events)};
  1961. }
  1962. direction = detail::deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1963. size_t size_slice = size.get(1) * size.get(0);
  1964. switch (direction)
  1965. {
  1966. case host_to_host:
  1967. for (size_t z = 0; z < size.get(2); ++z)
  1968. {
  1969. unsigned char *to_ptr = to_surface;
  1970. const unsigned char *from_ptr = from_surface;
  1971. if (to_range.get(0) == from_range.get(0) &&
  1972. to_range.get(0) == size.get(0))
  1973. {
  1974. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1975. direction, dep_events));
  1976. }
  1977. else
  1978. {
  1979. for (size_t y = 0; y < size.get(1); ++y)
  1980. {
  1981. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1982. direction, dep_events));
  1983. to_ptr += to_range.get(0);
  1984. from_ptr += from_range.get(0);
  1985. }
  1986. }
  1987. to_surface += to_slice;
  1988. from_surface += from_slice;
  1989. }
  1990. break;
  1991. case host_to_device:
  1992. {
  1993. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1994. event_list);
  1995. std::vector<sycl::event> host_events;
  1996. if (to_slice == size_slice)
  1997. {
  1998. // Copy host data to a temp host buffer with the shape of target.
  1999. host_events =
  2000. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  2001. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  2002. host_to_host, dep_events);
  2003. }
  2004. else
  2005. {
  2006. // Copy host data to a temp host buffer with the shape of target.
  2007. host_events = dpct_memcpy(
  2008. q, buf.get_ptr(), from_surface, to_range, from_range,
  2009. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  2010. // If has padding data, not sure whether it is useless. So fill temp
  2011. // buffer with it.
  2012. std::vector<sycl::event>{
  2013. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  2014. device_to_host, dep_events)});
  2015. }
  2016. // Copy from temp host buffer to device with only one submit.
  2017. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  2018. buf.get_size(), host_to_device,
  2019. host_events));
  2020. break;
  2021. }
  2022. case device_to_host:
  2023. {
  2024. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  2025. event_list);
  2026. // Copy from host temp buffer to host target with reshaping.
  2027. event_list = dpct_memcpy(
  2028. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  2029. sycl::id<3>(0, 0, 0), size, host_to_host,
  2030. // Copy from device to temp host buffer with only one submit.
  2031. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  2032. buf.get_size(),
  2033. device_to_host, dep_events)});
  2034. break;
  2035. }
  2036. case device_to_device:
  2037. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2038. {
  2039. cgh.depends_on(dep_events);
  2040. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  2041. size,
  2042. [=](sycl::id<3> id) {
  2043. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  2044. from_surface[get_offset(id, from_slice, from_range.get(0))];
  2045. }); }));
  2046. break;
  2047. default:
  2048. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2049. }
  2050. return event_list;
  2051. }
  2052. /// memcpy 2D/3D matrix specified by pitched_data.
  2053. static inline std::vector<sycl::event>
  2054. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  2055. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  2056. memcpy_direction direction = automatic)
  2057. {
  2058. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  2059. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  2060. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  2061. size, direction);
  2062. }
  2063. /// memcpy 2D matrix with pitch.
  2064. static inline std::vector<sycl::event>
  2065. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2066. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  2067. memcpy_direction direction = automatic)
  2068. {
  2069. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  2070. sycl::range<3>(from_pitch, y, 1),
  2071. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  2072. sycl::range<3>(x, y, 1), direction);
  2073. }
  2074. inline void gemm(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2075. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2076. const void *alpha, const void *a, library_data_t a_type,
  2077. int lda, const void *b, library_data_t b_type, int ldb,
  2078. const void *beta, void *c, library_data_t c_type, int ldc,
  2079. library_data_t scaling_type)
  2080. {
  2081. if (scaling_type == library_data_t::real_float &&
  2082. c_type == library_data_t::complex_float)
  2083. {
  2084. scaling_type = library_data_t::complex_float;
  2085. }
  2086. else if (scaling_type == library_data_t::real_double &&
  2087. c_type == library_data_t::complex_double)
  2088. {
  2089. scaling_type = library_data_t::complex_double;
  2090. }
  2091. std::uint64_t key =
  2092. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2093. switch (key)
  2094. {
  2095. case detail::get_type_combination_id(
  2096. library_data_t::real_float, library_data_t::real_float,
  2097. library_data_t::real_float, library_data_t::real_float):
  2098. {
  2099. detail::gemm_impl<float, float, float, float>(
  2100. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2101. break;
  2102. }
  2103. case detail::get_type_combination_id(
  2104. library_data_t::real_double, library_data_t::real_double,
  2105. library_data_t::real_double, library_data_t::real_double):
  2106. {
  2107. detail::gemm_impl<double, double, double, double>(
  2108. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2109. break;
  2110. }
  2111. case detail::get_type_combination_id(
  2112. library_data_t::complex_float, library_data_t::complex_float,
  2113. library_data_t::complex_float, library_data_t::complex_float):
  2114. {
  2115. detail::gemm_impl<std::complex<float>, std::complex<float>,
  2116. std::complex<float>, std::complex<float>>(
  2117. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2118. break;
  2119. }
  2120. case detail::get_type_combination_id(
  2121. library_data_t::complex_double, library_data_t::complex_double,
  2122. library_data_t::complex_double, library_data_t::complex_double):
  2123. {
  2124. detail::gemm_impl<std::complex<double>, std::complex<double>,
  2125. std::complex<double>, std::complex<double>>(
  2126. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2127. break;
  2128. }
  2129. case detail::get_type_combination_id(
  2130. library_data_t::real_half, library_data_t::real_half,
  2131. library_data_t::real_half, library_data_t::real_half):
  2132. {
  2133. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2134. sycl::half>(q, a_trans, b_trans, m, n, k, alpha, a,
  2135. lda, b, ldb, beta, c, ldc);
  2136. break;
  2137. }
  2138. #ifdef __INTEL_MKL__
  2139. case detail::get_type_combination_id(
  2140. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2141. library_data_t::real_float, library_data_t::real_float):
  2142. {
  2143. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2144. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda, b,
  2145. ldb, beta, c, ldc);
  2146. break;
  2147. }
  2148. case detail::get_type_combination_id(
  2149. library_data_t::real_half, library_data_t::real_half,
  2150. library_data_t::real_float, library_data_t::real_float):
  2151. {
  2152. detail::gemm_impl<sycl::half, sycl::half, float, float>(
  2153. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2154. break;
  2155. }
  2156. case detail::get_type_combination_id(
  2157. library_data_t::real_half, library_data_t::real_half,
  2158. library_data_t::real_half, library_data_t::real_float):
  2159. {
  2160. float alpha_value =
  2161. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2162. float beta_value =
  2163. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2164. sycl::half alpha_half(alpha_value);
  2165. sycl::half beta_half(beta_value);
  2166. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2167. sycl::half>(q, a_trans, b_trans, m, n, k, &alpha_half,
  2168. a, lda, b, ldb, &beta_half, c, ldc);
  2169. break;
  2170. }
  2171. case detail::get_type_combination_id(
  2172. library_data_t::real_int8, library_data_t::real_int8,
  2173. library_data_t::real_float, library_data_t::real_float):
  2174. {
  2175. detail::gemm_impl<std::int8_t, std::int8_t, float, float>(
  2176. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2177. break;
  2178. }
  2179. case detail::get_type_combination_id(
  2180. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2181. library_data_t::real_bfloat16, library_data_t::real_float):
  2182. {
  2183. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2184. oneapi::mkl::bfloat16, float>(
  2185. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2186. break;
  2187. }
  2188. case detail::get_type_combination_id(
  2189. library_data_t::real_int8, library_data_t::real_int8,
  2190. library_data_t::real_int32, library_data_t::real_int32):
  2191. {
  2192. float alpha_float =
  2193. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2194. float beta_float =
  2195. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2196. detail::gemm_impl<std::int8_t, std::int8_t, std::int32_t, float>(
  2197. q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float, c, ldc);
  2198. break;
  2199. }
  2200. #endif // __INTEL_MKL__
  2201. default:
  2202. throw std::runtime_error("the combination of data type is unsupported");
  2203. }
  2204. } // gemm()
  2205. /// Computes a batch of matrix-matrix product with general matrices.
  2206. /// \param [in] q The queue where the routine should be executed.
  2207. /// \param [in] a_trans Specifies the operation applied to A.
  2208. /// \param [in] b_trans Specifies the operation applied to B.
  2209. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2210. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2211. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2212. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2213. /// \param [in] a Input matrix A.
  2214. /// \param [in] a_type Data type of the matrix A.
  2215. /// \param [in] lda Leading dimension of A.
  2216. /// \param [in] b Input matrix B.
  2217. /// \param [in] b_type Data type of the matrix B.
  2218. /// \param [in] ldb Leading dimension of B.
  2219. /// \param [in] beta Scaling factor for matrix C.
  2220. /// \param [in, out] c Input/Output matrix C.
  2221. /// \param [in] c_type Data type of the matrix C.
  2222. /// \param [in] ldc Leading dimension of C.
  2223. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2224. /// \param [in] scaling_type Data type of the scaling factors.
  2225. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2226. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2227. const void *alpha, const void *a[],
  2228. library_data_t a_type, int lda, const void *b[],
  2229. library_data_t b_type, int ldb, const void *beta,
  2230. void *c[], library_data_t c_type, int ldc,
  2231. int batch_size, library_data_t scaling_type)
  2232. {
  2233. if (scaling_type == library_data_t::real_float &&
  2234. c_type == library_data_t::complex_float)
  2235. {
  2236. scaling_type = library_data_t::complex_float;
  2237. }
  2238. else if (scaling_type == library_data_t::real_double &&
  2239. c_type == library_data_t::complex_double)
  2240. {
  2241. scaling_type = library_data_t::complex_double;
  2242. }
  2243. std::uint64_t key =
  2244. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2245. switch (key)
  2246. {
  2247. case detail::get_type_combination_id(
  2248. library_data_t::real_float, library_data_t::real_float,
  2249. library_data_t::real_float, library_data_t::real_float):
  2250. {
  2251. detail::gemm_batch_impl<float, float, float, float>(
  2252. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2253. batch_size);
  2254. break;
  2255. }
  2256. case detail::get_type_combination_id(
  2257. library_data_t::real_double, library_data_t::real_double,
  2258. library_data_t::real_double, library_data_t::real_double):
  2259. {
  2260. detail::gemm_batch_impl<double, double, double, double>(
  2261. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2262. batch_size);
  2263. break;
  2264. }
  2265. case detail::get_type_combination_id(
  2266. library_data_t::complex_float, library_data_t::complex_float,
  2267. library_data_t::complex_float, library_data_t::complex_float):
  2268. {
  2269. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2270. std::complex<float>, std::complex<float>>(
  2271. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2272. batch_size);
  2273. break;
  2274. }
  2275. case detail::get_type_combination_id(
  2276. library_data_t::complex_double, library_data_t::complex_double,
  2277. library_data_t::complex_double, library_data_t::complex_double):
  2278. {
  2279. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2280. std::complex<double>, std::complex<double>>(
  2281. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2282. batch_size);
  2283. break;
  2284. }
  2285. case detail::get_type_combination_id(
  2286. library_data_t::real_half, library_data_t::real_half,
  2287. library_data_t::real_half, library_data_t::real_half):
  2288. {
  2289. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2290. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2291. a, lda, b, ldb, beta, c, ldc,
  2292. batch_size);
  2293. break;
  2294. }
  2295. #ifdef __INTEL_MKL__
  2296. case detail::get_type_combination_id(
  2297. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2298. library_data_t::real_bfloat16, library_data_t::real_float):
  2299. {
  2300. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2301. oneapi::mkl::bfloat16, float>(
  2302. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2303. batch_size);
  2304. break;
  2305. }
  2306. case detail::get_type_combination_id(
  2307. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2308. library_data_t::real_float, library_data_t::real_float):
  2309. {
  2310. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2311. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2312. b, ldb, beta, c, ldc, batch_size);
  2313. break;
  2314. }
  2315. case detail::get_type_combination_id(
  2316. library_data_t::real_int8, library_data_t::real_int8,
  2317. library_data_t::real_int32, library_data_t::real_int32):
  2318. {
  2319. float alpha_float =
  2320. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2321. float beta_float =
  2322. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2323. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2324. float>(q, a_trans, b_trans, m, n, k, &alpha_float,
  2325. a, lda, b, ldb, &beta_float, c, ldc,
  2326. batch_size);
  2327. break;
  2328. }
  2329. case detail::get_type_combination_id(
  2330. library_data_t::real_int8, library_data_t::real_int8,
  2331. library_data_t::real_float, library_data_t::real_float):
  2332. {
  2333. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2334. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2335. batch_size);
  2336. break;
  2337. }
  2338. case detail::get_type_combination_id(
  2339. library_data_t::real_half, library_data_t::real_half,
  2340. library_data_t::real_float, library_data_t::real_float):
  2341. {
  2342. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2343. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2344. batch_size);
  2345. break;
  2346. }
  2347. #endif
  2348. case detail::get_type_combination_id(
  2349. library_data_t::real_half, library_data_t::real_half,
  2350. library_data_t::real_half, library_data_t::real_float):
  2351. {
  2352. float alpha_value =
  2353. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2354. float beta_value =
  2355. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2356. sycl::half alpha_half(alpha_value);
  2357. sycl::half beta_half(beta_value);
  2358. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2359. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half, c, ldc,
  2360. batch_size);
  2361. break;
  2362. }
  2363. default:
  2364. throw std::runtime_error("the combination of data type is unsupported");
  2365. }
  2366. }
  2367. /// Computes a batch of matrix-matrix product with general matrices.
  2368. /// \param [in] q The queue where the routine should be executed.
  2369. /// \param [in] a_trans Specifies the operation applied to A.
  2370. /// \param [in] b_trans Specifies the operation applied to B.
  2371. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2372. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2373. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2374. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2375. /// \param [in] a Input matrix A.
  2376. /// \param [in] a_type Data type of the matrix A.
  2377. /// \param [in] lda Leading dimension of A.
  2378. /// \param [in] stride_a Stride between the different A matrices.
  2379. /// \param [in] b Input matrix B.
  2380. /// \param [in] b_type Data type of the matrix B.
  2381. /// \param [in] ldb Leading dimension of B.
  2382. /// \param [in] stride_b Stride between the different B matrices.
  2383. /// \param [in] beta Scaling factor for matrix C.
  2384. /// \param [in, out] c Input/Output matrix C.
  2385. /// \param [in] c_type Data type of the matrix C.
  2386. /// \param [in] ldc Leading dimension of C.
  2387. /// \param [in] stride_c Stride between the different C matrices.
  2388. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2389. /// \param [in] scaling_type Data type of the scaling factors.
  2390. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2391. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2392. const void *alpha, const void *a, library_data_t a_type,
  2393. int lda, long long int stride_a, const void *b,
  2394. library_data_t b_type, int ldb, long long int stride_b,
  2395. const void *beta, void *c, library_data_t c_type,
  2396. int ldc, long long int stride_c, int batch_size,
  2397. library_data_t scaling_type)
  2398. {
  2399. if (scaling_type == library_data_t::real_float &&
  2400. c_type == library_data_t::complex_float)
  2401. {
  2402. scaling_type = library_data_t::complex_float;
  2403. }
  2404. else if (scaling_type == library_data_t::real_double &&
  2405. c_type == library_data_t::complex_double)
  2406. {
  2407. scaling_type = library_data_t::complex_double;
  2408. }
  2409. std::uint64_t key =
  2410. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2411. switch (key)
  2412. {
  2413. case detail::get_type_combination_id(
  2414. library_data_t::real_float, library_data_t::real_float,
  2415. library_data_t::real_float, library_data_t::real_float):
  2416. {
  2417. detail::gemm_batch_impl<float, float, float, float>(
  2418. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2419. beta, c, ldc, stride_c, batch_size);
  2420. break;
  2421. }
  2422. case detail::get_type_combination_id(
  2423. library_data_t::real_double, library_data_t::real_double,
  2424. library_data_t::real_double, library_data_t::real_double):
  2425. {
  2426. detail::gemm_batch_impl<double, double, double, double>(
  2427. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2428. beta, c, ldc, stride_c, batch_size);
  2429. break;
  2430. }
  2431. case detail::get_type_combination_id(
  2432. library_data_t::complex_float, library_data_t::complex_float,
  2433. library_data_t::complex_float, library_data_t::complex_float):
  2434. {
  2435. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2436. std::complex<float>, std::complex<float>>(
  2437. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2438. beta, c, ldc, stride_c, batch_size);
  2439. break;
  2440. }
  2441. case detail::get_type_combination_id(
  2442. library_data_t::complex_double, library_data_t::complex_double,
  2443. library_data_t::complex_double, library_data_t::complex_double):
  2444. {
  2445. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2446. std::complex<double>, std::complex<double>>(
  2447. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2448. beta, c, ldc, stride_c, batch_size);
  2449. break;
  2450. }
  2451. case detail::get_type_combination_id(
  2452. library_data_t::real_half, library_data_t::real_half,
  2453. library_data_t::real_half, library_data_t::real_half):
  2454. {
  2455. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2456. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2457. a, lda, stride_a, b, ldb, stride_b,
  2458. beta, c, ldc, stride_c, batch_size);
  2459. break;
  2460. }
  2461. #ifdef __INTEL_MKL__
  2462. case detail::get_type_combination_id(
  2463. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2464. library_data_t::real_bfloat16, library_data_t::real_float):
  2465. {
  2466. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2467. oneapi::mkl::bfloat16, float>(
  2468. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2469. beta, c, ldc, stride_c, batch_size);
  2470. break;
  2471. }
  2472. case detail::get_type_combination_id(
  2473. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2474. library_data_t::real_float, library_data_t::real_float):
  2475. {
  2476. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2477. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2478. stride_a, b, ldb, stride_b, beta, c, ldc,
  2479. stride_c, batch_size);
  2480. break;
  2481. }
  2482. case detail::get_type_combination_id(
  2483. library_data_t::real_int8, library_data_t::real_int8,
  2484. library_data_t::real_int32, library_data_t::real_int32):
  2485. {
  2486. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2487. std::int32_t>(q, a_trans, b_trans, m, n, k, alpha,
  2488. a, lda, stride_a, b, ldb, stride_b,
  2489. beta, c, ldc, stride_c, batch_size);
  2490. break;
  2491. }
  2492. case detail::get_type_combination_id(
  2493. library_data_t::real_int8, library_data_t::real_int8,
  2494. library_data_t::real_float, library_data_t::real_float):
  2495. {
  2496. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2497. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2498. beta, c, ldc, stride_c, batch_size);
  2499. break;
  2500. }
  2501. case detail::get_type_combination_id(
  2502. library_data_t::real_half, library_data_t::real_half,
  2503. library_data_t::real_float, library_data_t::real_float):
  2504. {
  2505. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2506. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2507. beta, c, ldc, stride_c, batch_size);
  2508. break;
  2509. }
  2510. #endif
  2511. case detail::get_type_combination_id(
  2512. library_data_t::real_half, library_data_t::real_half,
  2513. library_data_t::real_half, library_data_t::real_float):
  2514. {
  2515. float alpha_value =
  2516. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2517. float beta_value =
  2518. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2519. sycl::half alpha_half(alpha_value);
  2520. sycl::half beta_half(beta_value);
  2521. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2522. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, stride_a, b, ldb, stride_b,
  2523. &beta_half, c, ldc, stride_c, batch_size);
  2524. break;
  2525. }
  2526. default:
  2527. throw std::runtime_error("the combination of data type is unsupported");
  2528. }
  2529. }
  2530. static inline void
  2531. async_dpct_memcpy(void *to_ptr, size_t to_pitch, const void *from_ptr,
  2532. size_t from_pitch, size_t x, size_t y,
  2533. memcpy_direction direction = automatic,
  2534. sycl::queue &q = get_default_queue())
  2535. {
  2536. detail::dpct_memcpy(q, to_ptr, from_ptr, to_pitch, from_pitch, x, y,
  2537. direction);
  2538. }
  2539. using err0 = detail::generic_error_type<struct err0_tag, int>;
  2540. using err1 = detail::generic_error_type<struct err1_tag, int>;
  2541. static inline void dpct_free(void *ptr, sycl::queue &q = get_default_queue()) {
  2542. detail::dpct_free(ptr, q);
  2543. }
  2544. /// dpct accessor used as device function parameter.
  2545. template <class T, memory_region Memory, size_t Dimension> class accessor;
  2546. template <class T, memory_region Memory> class accessor<T, Memory, 3> {
  2547. public:
  2548. using memory_t = detail::memory_traits<Memory, T>;
  2549. using element_t = typename memory_t::element_t;
  2550. using pointer_t = typename memory_t::pointer_t;
  2551. using accessor_t = typename memory_t::template accessor_t<3>;
  2552. accessor(pointer_t data, const sycl::range<3> &in_range)
  2553. : _data(data), _range(in_range) {}
  2554. template <memory_region M = Memory>
  2555. accessor(typename std::enable_if<M != local, const accessor_t>::type &acc)
  2556. : accessor(acc, acc.get_range()) {}
  2557. accessor(const accessor_t &acc, const sycl::range<3> &in_range)
  2558. : accessor(acc.get_pointer(), in_range) {}
  2559. accessor<T, Memory, 2> operator[](size_t index) const {
  2560. sycl::range<2> sub(_range.get(1), _range.get(2));
  2561. return accessor<T, Memory, 2>(_data + index * sub.size(), sub);
  2562. }
  2563. pointer_t get_ptr() const { return _data; }
  2564. private:
  2565. pointer_t _data;
  2566. sycl::range<3> _range;
  2567. };
  2568. template <class T, memory_region Memory> class accessor<T, Memory, 2> {
  2569. public:
  2570. using memory_t = detail::memory_traits<Memory, T>;
  2571. using element_t = typename memory_t::element_t;
  2572. using pointer_t = typename memory_t::pointer_t;
  2573. using accessor_t = typename memory_t::template accessor_t<2>;
  2574. accessor(pointer_t data, const sycl::range<2> &in_range)
  2575. : _data(data), _range(in_range) {}
  2576. template <memory_region M = Memory>
  2577. accessor(typename std::enable_if<M != local, const accessor_t>::type &acc)
  2578. : accessor(acc, acc.get_range()) {}
  2579. accessor(const accessor_t &acc, const sycl::range<2> &in_range)
  2580. : accessor(acc.get_pointer(), in_range) {}
  2581. pointer_t operator[](size_t index) const {
  2582. return _data + _range.get(1) * index;
  2583. }
  2584. pointer_t get_ptr() const { return _data; }
  2585. private:
  2586. pointer_t _data;
  2587. sycl::range<2> _range;
  2588. };
  2589. namespace detail {
  2590. /// Device variable with address space of shared, global or constant.
  2591. template <class T, memory_region Memory, size_t Dimension> class device_memory {
  2592. public:
  2593. using accessor_t =
  2594. typename detail::memory_traits<Memory,
  2595. T>::template accessor_t<Dimension>;
  2596. using value_t = typename detail::memory_traits<Memory, T>::value_t;
  2597. using dpct_accessor_t = dpct::accessor<T, Memory, Dimension>;
  2598. device_memory() : device_memory(sycl::range<Dimension>(1)) {}
  2599. /// Constructor of 1-D array with initializer list
  2600. device_memory(const sycl::range<Dimension> &in_range,
  2601. std::initializer_list<value_t> &&init_list)
  2602. : device_memory(in_range) {
  2603. assert(init_list.size() <= in_range.size());
  2604. _host_ptr = (value_t *)std::malloc(_size);
  2605. std::memset(_host_ptr, 0, _size);
  2606. std::memcpy(_host_ptr, init_list.begin(), init_list.size() * sizeof(T));
  2607. }
  2608. /// Constructor of 2-D array with initializer list
  2609. template <size_t D = Dimension>
  2610. device_memory(
  2611. const typename std::enable_if<D == 2, sycl::range<2>>::type &in_range,
  2612. std::initializer_list<std::initializer_list<value_t>> &&init_list)
  2613. : device_memory(in_range) {
  2614. assert(init_list.size() <= in_range[0]);
  2615. _host_ptr = (value_t *)std::malloc(_size);
  2616. std::memset(_host_ptr, 0, _size);
  2617. auto tmp_data = _host_ptr;
  2618. for (auto sub_list : init_list) {
  2619. assert(sub_list.size() <= in_range[1]);
  2620. std::memcpy(tmp_data, sub_list.begin(),
  2621. sub_list.size() * sizeof(T));
  2622. tmp_data += in_range[1];
  2623. }
  2624. }
  2625. /// Constructor with range
  2626. device_memory(const sycl::range<Dimension> &range_in)
  2627. : _size(range_in.size() * sizeof(T)), _range(range_in),
  2628. _reference(false), _host_ptr(nullptr), _device_ptr(nullptr) {
  2629. static_assert(
  2630. (Memory == global) || (Memory == constant) || (Memory == shared),
  2631. "device memory region should be global, constant or shared");
  2632. // Make sure that singleton class mem_mgr and dev_mgr will destruct
  2633. // later than this.
  2634. detail::mem_mgr::instance();
  2635. dev_mgr::instance();
  2636. }
  2637. /// Constructor with range
  2638. template <class... Args>
  2639. device_memory(Args... Arguments)
  2640. : device_memory(sycl::range<Dimension>(Arguments...)) {}
  2641. ~device_memory() {
  2642. if (_device_ptr && !_reference)
  2643. dpct::dpct_free(_device_ptr);
  2644. if (_host_ptr)
  2645. std::free(_host_ptr);
  2646. }
  2647. /// Allocate memory with default queue, and init memory if has initial
  2648. /// value.
  2649. void init() { init(dpct::get_default_queue()); }
  2650. /// Allocate memory with specified queue, and init memory if has initial
  2651. /// value.
  2652. void init(sycl::queue &q) {
  2653. if (_device_ptr)
  2654. return;
  2655. if (!_size)
  2656. return;
  2657. allocate_device(q);
  2658. if (_host_ptr)
  2659. detail::dpct_memcpy(q, _device_ptr, _host_ptr, _size,
  2660. host_to_device);
  2661. }
  2662. /// The variable is assigned to a device pointer.
  2663. void assign(value_t *src, size_t size) {
  2664. this->~device_memory();
  2665. new (this) device_memory(src, size);
  2666. }
  2667. /// Get memory pointer of the memory object, which is virtual pointer when
  2668. /// usm is not used, and device pointer when usm is used.
  2669. value_t *get_ptr() { return get_ptr(get_default_queue()); }
  2670. /// Get memory pointer of the memory object, which is virtual pointer when
  2671. /// usm is not used, and device pointer when usm is used.
  2672. value_t *get_ptr(sycl::queue &q) {
  2673. init(q);
  2674. return _device_ptr;
  2675. }
  2676. /// Get the device memory object size in bytes.
  2677. size_t get_size() { return _size; }
  2678. template <size_t D = Dimension>
  2679. typename std::enable_if<D == 1, T>::type &operator[](size_t index) {
  2680. init();
  2681. return _device_ptr[index];
  2682. }
  2683. /// Get dpct::accessor with dimension info for the device memory object
  2684. /// when usm is used and dimension is greater than 1.
  2685. template <size_t D = Dimension>
  2686. typename std::enable_if<D != 1, dpct_accessor_t>::type
  2687. get_access(sycl::handler &cgh) {
  2688. return dpct_accessor_t((T *)_device_ptr, _range);
  2689. }
  2690. private:
  2691. device_memory(value_t *memory_ptr, size_t size)
  2692. : _size(size), _range(size / sizeof(T)), _reference(true),
  2693. _device_ptr(memory_ptr) {}
  2694. void allocate_device(sycl::queue &q) {
  2695. #ifndef DPCT_USM_LEVEL_NONE
  2696. if (Memory == shared) {
  2697. _device_ptr = (value_t *)sycl::malloc_shared(_size, q.get_device(),
  2698. q.get_context());
  2699. return;
  2700. }
  2701. #ifdef SYCL_EXT_ONEAPI_USM_DEVICE_READ_ONLY
  2702. if (Memory == constant) {
  2703. _device_ptr = (value_t *)sycl::malloc_device(
  2704. _size, q.get_device(), q.get_context(),
  2705. sycl::ext::oneapi::property::usm::device_read_only());
  2706. return;
  2707. }
  2708. #endif
  2709. #endif
  2710. _device_ptr = (value_t *)detail::dpct_malloc(_size, q);
  2711. }
  2712. size_t _size;
  2713. sycl::range<Dimension> _range;
  2714. bool _reference;
  2715. value_t *_host_ptr;
  2716. value_t *_device_ptr;
  2717. };
  2718. template <class T, memory_region Memory>
  2719. class device_memory<T, Memory, 0> : public device_memory<T, Memory, 1> {
  2720. public:
  2721. using base = device_memory<T, Memory, 1>;
  2722. using value_t = typename base::value_t;
  2723. using accessor_t =
  2724. typename detail::memory_traits<Memory, T>::template accessor_t<0>;
  2725. /// Constructor with initial value.
  2726. device_memory(const value_t &val) : base(sycl::range<1>(1), {val}) {}
  2727. /// Default constructor
  2728. device_memory() : base(1) {}
  2729. };
  2730. } // namespace detail
  2731. template <class T, size_t Dimension>
  2732. using global_memory = detail::device_memory<T, global, Dimension>;
  2733. template <class T, size_t Dimension>
  2734. using constant_memory = detail::device_memory<T, constant, Dimension>;
  2735. template <class T, size_t Dimension>
  2736. using shared_memory = detail::device_memory<T, shared, Dimension>;
  2737. } // COPY from DPCT head files
  2738. #define GGML_COMMON_DECL_SYCL
  2739. #define GGML_COMMON_IMPL_SYCL
  2740. #include "ggml-common.h"
  2741. static int g_ggml_sycl_debug=0;
  2742. #define GGML_SYCL_DEBUG(...) do{if(g_ggml_sycl_debug) fprintf(stderr, __VA_ARGS__);}while(0)
  2743. #define CHECK_TRY_ERROR(expr) \
  2744. [&]() { \
  2745. try { \
  2746. expr; \
  2747. return dpct::success; \
  2748. } catch (std::exception const &e) { \
  2749. std::cerr << e.what()<< "\nException caught at file:" << __FILE__ \
  2750. << ", line:" << __LINE__ <<", func:"<<__func__<< std::endl; \
  2751. return dpct::default_error; \
  2752. } \
  2753. }()
  2754. // #define DEBUG_SYCL_MALLOC
  2755. static int g_work_group_size = 0;
  2756. // typedef sycl::half ggml_fp16_t;
  2757. #define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
  2758. #define VER_4VEC 610 //todo for hardward optimize.
  2759. #define VER_GEN9 700 //todo for hardward optimize.
  2760. #define VER_GEN12 1000000 //todo for hardward optimize.
  2761. #define VER_GEN13 (VER_GEN12 + 1030) //todo for hardward optimize.
  2762. #define GGML_SYCL_MAX_NODES 8192 //TODO: adapt to hardwares
  2763. //define for XMX in Intel GPU
  2764. //TODO: currently, it's not used for XMX really.
  2765. #define SYCL_USE_XMX
  2766. // max batch size to use MMQ kernels when tensor cores are available
  2767. #define XMX_MAX_BATCH_SIZE 32
  2768. #if defined(_MSC_VER)
  2769. #pragma warning(disable: 4244 4267) // possible loss of data
  2770. #endif
  2771. // dmmv = dequantize_mul_mat_vec
  2772. #ifndef GGML_SYCL_DMMV_X
  2773. #define GGML_SYCL_DMMV_X 32
  2774. #endif
  2775. #ifndef GGML_SYCL_MMV_Y
  2776. #define GGML_SYCL_MMV_Y 1
  2777. #endif
  2778. enum ggml_sycl_backend_gpu_mode {
  2779. SYCL_UNSET_GPU_MODE = -1,
  2780. SYCL_SINGLE_GPU_MODE = 0,
  2781. SYCL_MUL_GPU_MODE
  2782. };
  2783. static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  2784. static void crash(){
  2785. int *ptr = NULL;
  2786. *ptr = 0;
  2787. }
  2788. static void ggml_sycl_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  2789. fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
  2790. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  2791. GGML_ASSERT(!"SYCL error");
  2792. }
  2793. #define SYCL_CHECK(err) do { \
  2794. auto err_ = (err); if (err_ != 0) ggml_sycl_error( \
  2795. #err, __func__, __FILE__, __LINE__, \
  2796. "Meet error in this line code!"); \
  2797. } while (0)
  2798. #if DPCT_COMPAT_RT_VERSION >= 11100
  2799. #define GGML_SYCL_ASSUME(x) __builtin_assume(x)
  2800. #else
  2801. #define GGML_SYCL_ASSUME(x)
  2802. #endif // DPCT_COMPAT_RT_VERSION >= 11100
  2803. #ifdef GGML_SYCL_F16
  2804. typedef sycl::half dfloat; // dequantize float
  2805. typedef sycl::half2 dfloat2;
  2806. #else
  2807. typedef float dfloat; // dequantize float
  2808. typedef sycl::float2 dfloat2;
  2809. #endif //GGML_SYCL_F16
  2810. #define MMVQ_MAX_BATCH_SIZE 8
  2811. static const int8_t kvalues_iq4nl[16]={-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2812. bool ggml_sycl_loaded(void);
  2813. void * ggml_sycl_host_malloc(size_t size);
  2814. void ggml_sycl_host_free(void * ptr);
  2815. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  2816. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  2817. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor);
  2818. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  2819. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  2820. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  2821. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  2822. void ggml_sycl_set_main_device(int main_device);
  2823. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  2824. void ggml_sycl_set_scratch_size(size_t scratch_size);
  2825. void ggml_sycl_free_scratch(void);
  2826. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  2827. bool ggml_backend_is_sycl(ggml_backend_t backend);
  2828. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  2829. int get_main_device();
  2830. void print_ggml_tensor(const char*name, struct ggml_tensor *src);
  2831. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt);
  2832. void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
  2833. const void *ptr_src, size_t size) {
  2834. char *host_buf = (char *)malloc(size);
  2835. q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
  2836. q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
  2837. free(host_buf);
  2838. }
  2839. static __dpct_inline__ int get_int_from_int8(const int8_t *x8, const int &i32) {
  2840. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2841. int x32 = 0;
  2842. x32 |= x16[0] << 0;
  2843. x32 |= x16[1] << 16;
  2844. return x32;
  2845. }
  2846. static __dpct_inline__ int get_int_from_uint8(const uint8_t *x8,
  2847. const int &i32) {
  2848. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2849. int x32 = 0;
  2850. x32 |= x16[0] << 0;
  2851. x32 |= x16[1] << 16;
  2852. return x32;
  2853. }
  2854. static __dpct_inline__ int get_int_from_int8_aligned(const int8_t *x8,
  2855. const int &i32) {
  2856. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2857. }
  2858. static __dpct_inline__ int get_int_from_uint8_aligned(const uint8_t *x8,
  2859. const int &i32) {
  2860. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2861. }
  2862. template <typename T>
  2863. using to_t_sycl_t = void (*)(const void *__restrict__ x, T *__restrict__ y,
  2864. int k, dpct::queue_ptr stream);
  2865. typedef to_t_sycl_t<float> to_fp32_sycl_t;
  2866. typedef to_t_sycl_t<sycl::half> to_fp16_sycl_t;
  2867. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  2868. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  2869. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  2870. typedef void (*ggml_sycl_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  2871. typedef void (*ggml_sycl_op_mul_mat_t)(
  2872. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2873. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2874. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2875. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2876. const dpct::queue_ptr &stream);
  2877. typedef void (*ggml_sycl_op_flatten_t)(const ggml_tensor *src0,
  2878. const ggml_tensor *src1,
  2879. ggml_tensor *dst, const float *src0_dd,
  2880. const float *src1_dd, float *dst_dd,
  2881. const dpct::queue_ptr &main_stream);
  2882. typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  2883. typedef void (*allocate_tiles_sycl_t)(int **x_ql, sycl::half2 **x_dm,
  2884. int **x_qh, int **x_sc);
  2885. typedef void (*load_tiles_sycl_t)(const void *__restrict__ vx,
  2886. int *__restrict__ x_ql,
  2887. sycl::half2 *__restrict__ x_dm,
  2888. int *__restrict__ x_qh,
  2889. int *__restrict__ x_sc, const int &i_offset,
  2890. const int &i_max, const int &k,
  2891. const int &blocks_per_row);
  2892. typedef float (*vec_dot_q_mul_mat_sycl_t)(
  2893. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  2894. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  2895. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ms,
  2896. const int &i, const int &j, const int &k);
  2897. #define WARP_SIZE 32
  2898. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  2899. #define SYCL_GELU_BLOCK_SIZE 256
  2900. #define SYCL_SILU_BLOCK_SIZE 256
  2901. #define SYCL_TANH_BLOCK_SIZE 256
  2902. #define SYCL_RELU_BLOCK_SIZE 256
  2903. #define SYCL_HARDSIGMOID_BLOCK_SIZE 256
  2904. #define SYCL_HARDSWISH_BLOCK_SIZE 256
  2905. #define SYCL_SQR_BLOCK_SIZE 256
  2906. #define SYCL_CPY_BLOCK_SIZE 32
  2907. #define SYCL_SCALE_BLOCK_SIZE 256
  2908. #define SYCL_CLAMP_BLOCK_SIZE 256
  2909. #define SYCL_ROPE_BLOCK_SIZE 256
  2910. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  2911. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  2912. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  2913. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  2914. #define SYCL_UPSCALE_BLOCK_SIZE 256
  2915. #define SYCL_CONCAT_BLOCK_SIZE 256
  2916. #define SYCL_PAD_BLOCK_SIZE 256
  2917. #define SYCL_ACC_BLOCK_SIZE 256
  2918. #define SYCL_IM2COL_BLOCK_SIZE 256
  2919. #define SYCL_POOL2D_BLOCK_SIZE 256
  2920. // dmmv = dequantize_mul_mat_vec
  2921. #ifndef GGML_SYCL_DMMV_X
  2922. #define GGML_SYCL_DMMV_X 32
  2923. #endif
  2924. #ifndef GGML_SYCL_MMV_Y
  2925. #define GGML_SYCL_MMV_Y 1
  2926. #endif
  2927. #ifndef K_QUANTS_PER_ITERATION
  2928. #define K_QUANTS_PER_ITERATION 2
  2929. #else
  2930. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  2931. #endif
  2932. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  2933. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  2934. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  2935. #define MUL_MAT_SRC1_COL_STRIDE 128
  2936. #define MAX_STREAMS 8
  2937. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {{0}};
  2938. struct ggml_tensor_extra_gpu {
  2939. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  2940. dpct::event_ptr
  2941. events[GGML_SYCL_MAX_DEVICES]
  2942. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  2943. };
  2944. class sycl_gpu_mgr {
  2945. public:
  2946. std::vector<int> gpus;
  2947. std::vector<sycl::device> devices;
  2948. sycl::queue *first_queue;
  2949. sycl::context co_ctx;
  2950. int max_compute_units = 0;
  2951. int work_group_size = 0;
  2952. std::string gpus_list = "";
  2953. /*
  2954. Use all GPUs with same top max compute units
  2955. */
  2956. sycl_gpu_mgr() {
  2957. detect_sycl_gpu_list_with_max_cu();
  2958. get_allow_gpus();
  2959. create_context_with_gpus();
  2960. }
  2961. /*
  2962. Only use the assigned GPU
  2963. */
  2964. sycl_gpu_mgr(int main_gpu_id) {
  2965. sycl::device device = dpct::dev_mgr::instance().get_device(main_gpu_id);
  2966. dpct::device_info prop;
  2967. dpct::get_device_info(prop, device);
  2968. gpus.push_back(main_gpu_id);
  2969. devices.push_back(device);
  2970. work_group_size = prop.get_max_work_group_size();
  2971. max_compute_units = prop.get_max_compute_units();
  2972. get_allow_gpus();
  2973. create_context_with_gpus();
  2974. }
  2975. void create_context_with_gpus() {
  2976. sycl::context ctx = sycl::context(devices);
  2977. assert(gpus.size() > 0);
  2978. first_queue = dpct::get_current_device().create_queue(ctx, devices[0]);
  2979. co_ctx = first_queue->get_context();
  2980. }
  2981. sycl::context &get_co_ctx() { return co_ctx; }
  2982. void get_allow_gpus() {
  2983. gpus_list = "";
  2984. for (size_t i = 0; i < gpus.size(); ++i) {
  2985. gpus_list += std::to_string(gpus[i]);
  2986. gpus_list += ",";
  2987. }
  2988. if (gpus_list.length() > 1) {
  2989. gpus_list.pop_back();
  2990. }
  2991. }
  2992. bool is_allowed_gpu(int device_id) {
  2993. return std::find(gpus.begin(), gpus.end(), device_id) != gpus.end();
  2994. }
  2995. void detect_sycl_gpu_list_with_max_cu() try {
  2996. int device_count = dpct::dev_mgr::instance().device_count();
  2997. for (int id = 0; id < device_count; id++) {
  2998. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  2999. if (!device.is_gpu())
  3000. continue;
  3001. dpct::device_info prop;
  3002. dpct::get_device_info(prop, device);
  3003. if (max_compute_units < prop.get_max_compute_units())
  3004. max_compute_units = prop.get_max_compute_units();
  3005. }
  3006. for (int id = 0; id < device_count; id++) {
  3007. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3008. if (!device.is_gpu())
  3009. continue;
  3010. dpct::device_info prop;
  3011. dpct::get_device_info(prop, device);
  3012. if (max_compute_units == prop.get_max_compute_units() &&
  3013. is_ext_oneapi_device(device)) {
  3014. gpus.push_back(id);
  3015. devices.push_back(device);
  3016. work_group_size = prop.get_max_work_group_size();
  3017. }
  3018. }
  3019. return;
  3020. } catch (sycl::exception const &exc) {
  3021. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3022. << ", line:" << __LINE__ << std::endl;
  3023. std::exit(1);
  3024. }
  3025. int get_gpu_count() { return (int)gpus.size(); }
  3026. int get_index(int id) {
  3027. for (int i = 0; i < (int)gpus.size(); i++) {
  3028. if (gpus[i] == id)
  3029. return i;
  3030. }
  3031. printf("miss to get device index by id=%d\n", id);
  3032. GGML_ASSERT(false);
  3033. }
  3034. int get_next_index(int id) {
  3035. int cur_index = get_index(id);
  3036. for (int i = cur_index + 1; i < (int)gpus.size(); i++) {
  3037. if (gpus[i] == id)
  3038. return i;
  3039. }
  3040. GGML_ASSERT(false);
  3041. }
  3042. bool is_ext_oneapi_device(const sycl::device &dev) {
  3043. sycl::backend dev_backend = dev.get_backend();
  3044. if (dev_backend == sycl::backend::ext_oneapi_level_zero ||
  3045. dev_backend == sycl::backend::ext_oneapi_cuda ||
  3046. dev_backend == sycl::backend::ext_oneapi_hip)
  3047. return true;
  3048. return false;
  3049. }
  3050. };
  3051. static sycl_gpu_mgr *g_sycl_gpu_mgr = NULL;
  3052. static int g_device_count = -1;
  3053. static int g_all_sycl_device_count = -1;
  3054. static int g_main_device = -1;
  3055. static int g_main_device_id = -1;
  3056. static bool g_ggml_backend_sycl_buffer_type_initialized = false;
  3057. static std::array<float, GGML_SYCL_MAX_DEVICES> g_default_tensor_split = {};
  3058. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  3059. static ggml_sycl_backend_gpu_mode g_ggml_sycl_backend_gpu_mode = SYCL_UNSET_GPU_MODE;
  3060. struct sycl_device_capabilities {
  3061. int cc; // compute capability
  3062. bool vmm; // virtual memory support
  3063. size_t vmm_granularity; // granularity of virtual memory
  3064. int device_id;
  3065. };
  3066. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3067. struct sycl_device_id2index {
  3068. int index;
  3069. };
  3070. static void * g_scratch_buffer = nullptr;
  3071. static size_t g_scratch_size = 0; // disabled by default
  3072. static size_t g_scratch_offset = 0;
  3073. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3074. int get_main_device(){
  3075. return g_main_device;
  3076. }
  3077. [[noreturn]]
  3078. static void bad_arch(const sycl::stream &stream_ct1) {
  3079. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3080. "current GPU architecture.\n";
  3081. // __trap();
  3082. std::exit(1);
  3083. (void) bad_arch; // suppress unused function warning
  3084. }
  3085. /*
  3086. device_index: device index from 0 to n (continue numbers).
  3087. It is used for device select/set in SYCL backend internal data structure.
  3088. */
  3089. void check_allow_gpu_index(const int device_index) {
  3090. if (device_index >= g_device_count) {
  3091. char error_buf[256];
  3092. snprintf(error_buf, sizeof(error_buf),
  3093. "%s error: device_index:%d is out of range: [0-%d]", __func__,
  3094. device_index, g_device_count - 1);
  3095. fprintf(stderr, "%s\n", error_buf);
  3096. assert(false);
  3097. }
  3098. }
  3099. /*
  3100. device_id: device ID is shown by ggml_backend_sycl_print_sycl_devices().
  3101. It is only used to set current working device.
  3102. */
  3103. void check_allow_gpu_id(const int device_id) {
  3104. if (!g_sycl_gpu_mgr->is_allowed_gpu(device_id)) {
  3105. char error_buf[256];
  3106. snprintf(error_buf, sizeof(error_buf),
  3107. "error: cannot set device=%d, which is not allowed. Please "
  3108. "set GPU ID in: [%s]",
  3109. device_id, g_sycl_gpu_mgr->gpus_list.c_str());
  3110. fprintf(stderr, "%s\n", error_buf);
  3111. throw std::invalid_argument(error_buf);
  3112. }
  3113. }
  3114. int get_current_device_id() {
  3115. return dpct::dev_mgr::instance().current_device_id();
  3116. }
  3117. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  3118. int device_id = g_sycl_gpu_mgr->gpus[device];
  3119. check_allow_gpu_id(device_id);
  3120. int current_device_id;
  3121. SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
  3122. // GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
  3123. // current_device_id=%d\n", device, current_device);
  3124. if (device_id == current_device_id) {
  3125. return 0;
  3126. }
  3127. return CHECK_TRY_ERROR(dpct::select_device(device_id));
  3128. } catch (sycl::exception const &exc) {
  3129. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3130. << ", line:" << __LINE__ << std::endl;
  3131. crash();
  3132. std::exit(1);
  3133. }
  3134. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3135. if(!g_ggml_sycl_debug) return;
  3136. if(!src){
  3137. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3138. return;
  3139. }
  3140. char filename[1024];
  3141. sprintf(filename, "%s.txt", name);
  3142. printf("GGML Tensor:%s save to %s\n", name, filename);
  3143. size_t total_size = total_elements*sizeof(float);
  3144. float *local_buf = NULL;
  3145. if(src_on_device) {
  3146. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3147. ggml_sycl_set_device(g_main_device);
  3148. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3149. main_stream->memcpy(local_buf, src, total_size).wait();
  3150. }
  3151. else {
  3152. local_buf = (float *)src;
  3153. }
  3154. std::ofstream logfile;
  3155. logfile.open(filename);
  3156. for(size_t i=0; i<total_elements; i++){
  3157. logfile << local_buf[i] <<" ";
  3158. if((i+1)%20 ==0) logfile <<std::endl;
  3159. }
  3160. logfile <<std::endl;
  3161. logfile.close();
  3162. if(src_on_device) ggml_sycl_host_free(local_buf);
  3163. }
  3164. void log_ggml_var_device_fp16(const char*name, sycl::half *src, size_t total_elements, bool src_on_device){
  3165. if(!g_ggml_sycl_debug) return;
  3166. if(!src){
  3167. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3168. return;
  3169. }
  3170. char filename[1024];
  3171. sprintf(filename, "%s.txt", name);
  3172. printf("GGML Tensor:%s save to %s\n", name, filename);
  3173. size_t total_size = total_elements*sizeof(sycl::half);
  3174. sycl::half *local_buf = NULL;
  3175. if(src_on_device) {
  3176. local_buf = (sycl::half *) ggml_sycl_host_malloc(total_size);
  3177. ggml_sycl_set_device(g_main_device);
  3178. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3179. main_stream->memcpy(local_buf, src, total_size).wait();
  3180. }
  3181. else {
  3182. local_buf = (sycl::half *)src;
  3183. }
  3184. std::ofstream logfile;
  3185. logfile.open(filename);
  3186. for(size_t i=0; i<total_elements; i++){
  3187. logfile << local_buf[i] <<" ";
  3188. if((i+1)%20 ==0) logfile <<std::endl;
  3189. }
  3190. logfile <<std::endl;
  3191. logfile.close();
  3192. if(src_on_device) ggml_sycl_host_free(local_buf);
  3193. }
  3194. //todo: debug for crash in some case
  3195. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3196. if(!g_ggml_sycl_debug) return;
  3197. if(!src){
  3198. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3199. return;
  3200. }
  3201. size_t total_elements = ggml_nelements(src);
  3202. const bool src_on_device = src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  3203. float *src_data =NULL;
  3204. if(src_on_device) {
  3205. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3206. src_data = (float*)src_extra->data_device[g_main_device];
  3207. }
  3208. else {
  3209. src_data = (float *)src->data;
  3210. }
  3211. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3212. }
  3213. static int log_file_name_idx=0;
  3214. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3215. stop_cnt = 4;
  3216. if(log_file_name_idx>=stop_cnt) return;
  3217. char filename[1280];
  3218. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3219. log_file_name_idx++;
  3220. print_ggml_tensor(filename, src);
  3221. }
  3222. static __dpct_inline__ float warp_reduce_sum(float x,
  3223. const sycl::nd_item<3> &item_ct1) {
  3224. #pragma unroll
  3225. for (int mask = 16; mask > 0; mask >>= 1) {
  3226. /*
  3227. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3228. kernel that calls this function may be less than "32". The function
  3229. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3230. CPU device. Modify the size of the work-group to ensure that the value
  3231. of the right-most dimension is a multiple of "32".
  3232. */
  3233. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3234. }
  3235. return x;
  3236. }
  3237. static __dpct_inline__ sycl::float2
  3238. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3239. #pragma unroll
  3240. for (int mask = 16; mask > 0; mask >>= 1) {
  3241. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3242. mask);
  3243. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3244. mask);
  3245. }
  3246. return a;
  3247. }
  3248. static __dpct_inline__ float warp_reduce_max(float x,
  3249. const sycl::nd_item<3> &item_ct1) {
  3250. #pragma unroll
  3251. for (int mask = 16; mask > 0; mask >>= 1) {
  3252. /*
  3253. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3254. kernel that calls this function may be less than "32". The function
  3255. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3256. CPU device. Modify the size of the work-group to ensure that the value
  3257. of the right-most dimension is a multiple of "32".
  3258. */
  3259. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3260. item_ct1.get_sub_group(), x, mask));
  3261. }
  3262. return x;
  3263. }
  3264. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3265. return b;
  3266. GGML_UNUSED(a);
  3267. }
  3268. static __dpct_inline__ float op_add(const float a, const float b) {
  3269. return a + b;
  3270. }
  3271. static __dpct_inline__ float op_mul(const float a, const float b) {
  3272. return a * b;
  3273. }
  3274. static __dpct_inline__ float op_div(const float a, const float b) {
  3275. return a / b;
  3276. }
  3277. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3278. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3279. int ne0, int ne1, int ne2, int ne3,
  3280. int ne10, int ne11, int ne12, int ne13,
  3281. /*int s0, */ int s1, int s2, int s3,
  3282. /*int s10,*/ int s11, int s12, int s13,
  3283. const sycl::nd_item<3> &item_ct1) {
  3284. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3285. item_ct1.get_local_id(2);
  3286. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3287. item_ct1.get_local_id(1));
  3288. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3289. item_ct1.get_local_id(0)) /
  3290. ne3;
  3291. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3292. item_ct1.get_local_id(0)) %
  3293. ne3;
  3294. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3295. return;
  3296. }
  3297. const int i11 = i1 % ne11;
  3298. const int i12 = i2 % ne12;
  3299. const int i13 = i3 % ne13;
  3300. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3301. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3302. const size_t i_dst = i_src0;
  3303. const src0_t * src0_row = src0 + i_src0;
  3304. const src1_t * src1_row = src1 + i_src1;
  3305. dst_t * dst_row = dst + i_dst;
  3306. for (int i0 = i0s; i0 < ne0;
  3307. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3308. const int i10 = i0 % ne10;
  3309. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3310. }
  3311. }
  3312. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3313. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3314. int ne0, int ne1, int ne2, int ne3,
  3315. int ne10, int ne11, int ne12, int ne13,
  3316. /*int s0, */ int s1, int s2, int s3,
  3317. /*int s10,*/ int s11, int s12, int s13,
  3318. const sycl::nd_item<3> &item_ct1) {
  3319. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3320. item_ct1.get_local_id(2);
  3321. const int i3 = i/(ne2*ne1*ne0);
  3322. const int i2 = (i/(ne1*ne0)) % ne2;
  3323. const int i1 = (i/ne0) % ne1;
  3324. const int i0 = i % ne0;
  3325. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3326. return;
  3327. }
  3328. const int i11 = i1 % ne11;
  3329. const int i12 = i2 % ne12;
  3330. const int i13 = i3 % ne13;
  3331. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3332. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3333. const size_t i_dst = i_src0;
  3334. const src0_t * src0_row = src0 + i_src0;
  3335. const src1_t * src1_row = src1 + i_src1;
  3336. dst_t * dst_row = dst + i_dst;
  3337. const int i10 = i0 % ne10;
  3338. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3339. }
  3340. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3341. const int ne10, const int ne11, const int ne12,
  3342. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3343. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3344. item_ct1.get_local_id(2);
  3345. if (i >= ne) {
  3346. return;
  3347. }
  3348. int src1_idx = i - offset;
  3349. int oz = src1_idx / nb2;
  3350. int oy = (src1_idx - (oz * nb2)) / nb1;
  3351. int ox = src1_idx % nb1;
  3352. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3353. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3354. } else {
  3355. dst[i] = x[i];
  3356. }
  3357. }
  3358. static void gelu_f32(const float * x, float * dst, const int k,
  3359. const sycl::nd_item<3> &item_ct1) {
  3360. const float GELU_COEF_A = 0.044715f;
  3361. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  3362. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3363. item_ct1.get_local_id(2);
  3364. if (i >= k) {
  3365. return;
  3366. }
  3367. float xi = x[i];
  3368. dst[i] = 0.5f * xi *
  3369. (1.0f +
  3370. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3371. }
  3372. static void silu_f32(const float * x, float * dst, const int k,
  3373. const sycl::nd_item<3> &item_ct1) {
  3374. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3375. item_ct1.get_local_id(2);
  3376. if (i >= k) {
  3377. return;
  3378. }
  3379. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3380. }
  3381. static void gelu_quick_f32(const float *x, float *dst, int k,
  3382. const sycl::nd_item<3> &item_ct1) {
  3383. const float GELU_QUICK_COEF = -1.702f;
  3384. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3385. item_ct1.get_local_id(2);
  3386. if (i >= k) {
  3387. return;
  3388. }
  3389. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3390. }
  3391. static void tanh_f32(const float *x, float *dst, int k,
  3392. const sycl::nd_item<3> &item_ct1) {
  3393. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3394. item_ct1.get_local_id(2);
  3395. if (i >= k) {
  3396. return;
  3397. }
  3398. dst[i] = sycl::tanh((float)(x[i]));
  3399. }
  3400. static void relu_f32(const float * x, float * dst, const int k,
  3401. const sycl::nd_item<3> &item_ct1) {
  3402. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3403. item_ct1.get_local_id(2);
  3404. if (i >= k) {
  3405. return;
  3406. }
  3407. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3408. }
  3409. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  3410. const sycl::nd_item<3> &item_ct1) {
  3411. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3412. item_ct1.get_local_id(2);
  3413. if (i >= k) {
  3414. return;
  3415. }
  3416. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3417. }
  3418. static void hardswish_f32(const float * x, float * dst, const int k,
  3419. const sycl::nd_item<3> &item_ct1) {
  3420. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3421. item_ct1.get_local_id(2);
  3422. if (i >= k) {
  3423. return;
  3424. }
  3425. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3426. }
  3427. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3428. const sycl::nd_item<3> &item_ct1) {
  3429. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3430. item_ct1.get_local_id(2);
  3431. if (i >= k) {
  3432. return;
  3433. }
  3434. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3435. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3436. }
  3437. static void sqr_f32(const float * x, float * dst, const int k,
  3438. const sycl::nd_item<3> &item_ct1) {
  3439. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3440. item_ct1.get_local_id(2);
  3441. if (i >= k) {
  3442. return;
  3443. }
  3444. dst[i] = x[i] * x[i];
  3445. }
  3446. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3447. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3448. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3449. item_ct1.get_local_id(1);
  3450. const int tid = item_ct1.get_local_id(2);
  3451. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3452. for (int col = tid; col < ncols; col += block_size) {
  3453. const float xi = x[row*ncols + col];
  3454. mean_var.x() += xi;
  3455. mean_var.y() += xi * xi;
  3456. }
  3457. // sum up partial sums
  3458. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3459. if (block_size > WARP_SIZE) {
  3460. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3461. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3462. if (lane_id == 0) {
  3463. s_sum[warp_id] = mean_var;
  3464. }
  3465. /*
  3466. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3467. converged control flow. You may need to adjust the code.
  3468. */
  3469. item_ct1.barrier(sycl::access::fence_space::local_space);
  3470. mean_var = s_sum[lane_id];
  3471. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3472. }
  3473. const float mean = mean_var.x() / ncols;
  3474. const float var = mean_var.y() / ncols - mean * mean;
  3475. const float inv_std = sycl::rsqrt(var + eps);
  3476. for (int col = tid; col < ncols; col += block_size) {
  3477. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3478. }
  3479. }
  3480. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3481. const sycl::nd_item<3> &item_ct1) {
  3482. int nidx = item_ct1.get_local_id(2) +
  3483. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3484. if (nidx >= ne0) {
  3485. return;
  3486. }
  3487. // operation
  3488. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3489. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3490. if (item_ct1.get_group(0) < ne02) { // src0
  3491. int offset_src =
  3492. nidx + item_ct1.get_group(1) * ne0 +
  3493. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3494. dst[offset_dst] = x[offset_src];
  3495. } else {
  3496. int offset_src =
  3497. nidx + item_ct1.get_group(1) * ne0 +
  3498. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3499. dst[offset_dst] = y[offset_src];
  3500. }
  3501. }
  3502. static void upscale_f32(const float *x, float *dst, const int nb00, const int nb01,
  3503. const int nb02, const int nb03, const int ne10, const int ne11,
  3504. const int ne12, const int ne13, const float sf0, const float sf1,
  3505. const float sf2, const float sf3, const sycl::nd_item<1> &item_ct1) {
  3506. int index = item_ct1.get_local_id(0) +
  3507. item_ct1.get_group(0) * item_ct1.get_local_range(0);
  3508. if (index >= ne10 * ne11 * ne12 * ne13) {
  3509. return;
  3510. }
  3511. // operation
  3512. int i10 = index % ne10;
  3513. int i11 = (index / ne10) % ne11;
  3514. int i12 = (index / (ne10 * ne11)) % ne12;
  3515. int i13 = (index / (ne10 * ne11 * ne12)) % ne13;
  3516. int i00 = i10 / sf0;
  3517. int i01 = i11 / sf1;
  3518. int i02 = i12 / sf2;
  3519. int i03 = i13 / sf3;
  3520. dst[index] = *(float *)((char *)x + i03 * nb03 + i02 * nb02 + i01 * nb01 + i00 * nb00);
  3521. }
  3522. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3523. const sycl::nd_item<3> &item_ct1) {
  3524. int nidx = item_ct1.get_local_id(2) +
  3525. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3526. if (nidx >= ne0) {
  3527. return;
  3528. }
  3529. // operation
  3530. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3531. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3532. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3533. item_ct1.get_group(0) < ne02) {
  3534. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3535. item_ct1.get_group(0) * ne00 * ne01;
  3536. dst[offset_dst] = x[offset_src];
  3537. } else {
  3538. dst[offset_dst] = 0.0f;
  3539. }
  3540. }
  3541. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3542. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3543. int start = item_ct1.get_group(2) * group_size;
  3544. int end = start + group_size;
  3545. start += item_ct1.get_local_id(2);
  3546. if (end >= ne_elements) {
  3547. end = ne_elements;
  3548. }
  3549. float tmp = 0.0f; // partial sum for thread in warp
  3550. for (int j = start; j < end; j += block_size) {
  3551. tmp += x[j];
  3552. }
  3553. tmp = warp_reduce_sum(tmp, item_ct1);
  3554. if (block_size > WARP_SIZE) {
  3555. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3556. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3557. if (lane_id == 0) {
  3558. s_sum[warp_id] = tmp;
  3559. }
  3560. /*
  3561. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3562. converged control flow. You may need to adjust the code.
  3563. */
  3564. /*
  3565. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3566. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3567. better performance if there is no access to global memory.
  3568. */
  3569. item_ct1.barrier();
  3570. tmp = s_sum[lane_id];
  3571. tmp = warp_reduce_sum(tmp, item_ct1);
  3572. }
  3573. float mean = tmp / group_size;
  3574. tmp = 0.0f;
  3575. for (int j = start; j < end; j += block_size) {
  3576. float xi = x[j] - mean;
  3577. dst[j] = xi;
  3578. tmp += xi * xi;
  3579. }
  3580. tmp = warp_reduce_sum(tmp, item_ct1);
  3581. if (block_size > WARP_SIZE) {
  3582. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3583. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3584. if (lane_id == 0) {
  3585. s_sum[warp_id] = tmp;
  3586. }
  3587. /*
  3588. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3589. converged control flow. You may need to adjust the code.
  3590. */
  3591. /*
  3592. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3593. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3594. better performance if there is no access to global memory.
  3595. */
  3596. item_ct1.barrier();
  3597. tmp = s_sum[lane_id];
  3598. tmp = warp_reduce_sum(tmp, item_ct1);
  3599. }
  3600. float variance = tmp / group_size;
  3601. float scale = sycl::rsqrt(variance + eps);
  3602. for (int j = start; j < end; j += block_size) {
  3603. dst[j] *= scale;
  3604. }
  3605. }
  3606. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3607. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3608. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3609. item_ct1.get_local_id(1);
  3610. const int tid = item_ct1.get_local_id(2);
  3611. float tmp = 0.0f; // partial sum for thread in warp
  3612. for (int col = tid; col < ncols; col += block_size) {
  3613. const float xi = x[row*ncols + col];
  3614. tmp += xi * xi;
  3615. }
  3616. // sum up partial sums
  3617. tmp = warp_reduce_sum(tmp, item_ct1);
  3618. if (block_size > WARP_SIZE) {
  3619. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3620. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3621. if (lane_id == 0) {
  3622. s_sum[warp_id] = tmp;
  3623. }
  3624. /*
  3625. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3626. converged control flow. You may need to adjust the code.
  3627. */
  3628. item_ct1.barrier(sycl::access::fence_space::local_space);
  3629. tmp = s_sum[lane_id];
  3630. tmp = warp_reduce_sum(tmp, item_ct1);
  3631. }
  3632. const float mean = tmp / ncols;
  3633. const float scale = sycl::rsqrt(mean + eps);
  3634. for (int col = tid; col < ncols; col += block_size) {
  3635. dst[row*ncols + col] = scale * x[row*ncols + col];
  3636. }
  3637. }
  3638. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3639. const int iqs, dfloat2 &v) {
  3640. const block_q4_0 * x = (const block_q4_0 *) vx;
  3641. const dfloat d = x[ib].d;
  3642. const int vui = x[ib].qs[iqs];
  3643. v.x() = vui & 0xF;
  3644. v.y() = vui >> 4;
  3645. #ifdef GGML_SYCL_F16
  3646. // v = v - {8.0f, 8.0f};
  3647. // v = v * {d, d};
  3648. v.s0() = (v.s0() - 8.0f) * d;
  3649. v.s1() = (v.s1() - 8.0f) * d;
  3650. #else
  3651. v.x() = (v.x() - 8.0f) * d;
  3652. v.y() = (v.y() - 8.0f) * d;
  3653. #endif // GGML_SYCL_F16
  3654. }
  3655. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3656. const int iqs, dfloat2 &v) {
  3657. const block_q4_1 * x = (const block_q4_1 *) vx;
  3658. const dfloat d = x[ib].dm[0];
  3659. const dfloat m = x[ib].dm[1];
  3660. const int vui = x[ib].qs[iqs];
  3661. v.x() = vui & 0xF;
  3662. v.y() = vui >> 4;
  3663. #ifdef GGML_SYCL_F16
  3664. // v = v * {d, d};
  3665. // v = v + {m, m};
  3666. v.s0() = (v.s0() * d) + m;
  3667. v.s1() = (v.s1() * d) + m;
  3668. #else
  3669. v.x() = (v.x() * d) + m;
  3670. v.y() = (v.y() * d) + m;
  3671. #endif // GGML_SYCL_F16
  3672. }
  3673. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3674. const int iqs, dfloat2 &v) {
  3675. const block_q5_0 * x = (const block_q5_0 *) vx;
  3676. const dfloat d = x[ib].d;
  3677. uint32_t qh;
  3678. memcpy(&qh, x[ib].qh, sizeof(qh));
  3679. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3680. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3681. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3682. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3683. #ifdef GGML_SYCL_F16
  3684. // v = v - {16.0f, 16.0f};
  3685. // v = v * {d, d};
  3686. v.s0() = (v.s0() - 16.0f) * d;
  3687. v.s1() = (v.s1() - 16.0f) * d;
  3688. #else
  3689. v.x() = (v.x() - 16.0f) * d;
  3690. v.y() = (v.y() - 16.0f) * d;
  3691. #endif // GGML_SYCL_F16
  3692. }
  3693. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  3694. const int iqs, dfloat2 &v) {
  3695. const block_q5_1 * x = (const block_q5_1 *) vx;
  3696. const dfloat d = x[ib].dm[0];
  3697. const dfloat m = x[ib].dm[1];
  3698. uint32_t qh;
  3699. memcpy(&qh, x[ib].qh, sizeof(qh));
  3700. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3701. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3702. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3703. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3704. #ifdef GGML_SYCL_F16
  3705. // v = v * {d, d};
  3706. // v = v + {m, m};
  3707. v.s0() = (v.s0() * d) + m;
  3708. v.s1() = (v.s1() * d) + m;
  3709. #else
  3710. v.x() = (v.x() * d) + m;
  3711. v.y() = (v.y() * d) + m;
  3712. #endif // GGML_SYCL_F16
  3713. }
  3714. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  3715. const int iqs, dfloat2 &v) {
  3716. const block_q8_0 * x = (const block_q8_0 *) vx;
  3717. const dfloat d = x[ib].d;
  3718. v.x() = x[ib].qs[iqs + 0];
  3719. v.y() = x[ib].qs[iqs + 1];
  3720. #ifdef GGML_SYCL_F16
  3721. // v = v * {d, d};
  3722. v.s0() *= d;
  3723. v.s1() *= d;
  3724. #else
  3725. v.x() *= d;
  3726. v.y() *= d;
  3727. #endif // GGML_SYCL_F16
  3728. }
  3729. template<typename dst_t>
  3730. static void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3731. const sycl::nd_item<3> &item_ct1) {
  3732. const int i = item_ct1.get_group(2);
  3733. // assume 32 threads
  3734. const int tid = item_ct1.get_local_id(2);
  3735. const int il = tid/8;
  3736. const int ir = tid%8;
  3737. const int ib = 8*i + ir;
  3738. if (ib >= nb32) {
  3739. return;
  3740. }
  3741. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3742. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  3743. const float d = sycl::vec<sycl::half, 1>(x->d)
  3744. .convert<float, sycl::rounding_mode::automatic>()[0];
  3745. const float dm = -8*d;
  3746. const uint8_t * q = x->qs + 4*il;
  3747. for (int l = 0; l < 4; ++l) {
  3748. y[l+ 0] = d * (q[l] & 0xF) + dm;
  3749. y[l+16] = d * (q[l] >> 4) + dm;
  3750. }
  3751. }
  3752. template<typename dst_t>
  3753. static void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3754. const sycl::nd_item<3> &item_ct1) {
  3755. const int i = item_ct1.get_group(2);
  3756. // assume 32 threads
  3757. const int tid = item_ct1.get_local_id(2);
  3758. const int il = tid/8;
  3759. const int ir = tid%8;
  3760. const int ib = 8*i + ir;
  3761. if (ib >= nb32) {
  3762. return;
  3763. }
  3764. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3765. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  3766. const sycl::float2 d =
  3767. x->dm.convert<float, sycl::rounding_mode::automatic>();
  3768. const uint8_t * q = x->qs + 4*il;
  3769. for (int l = 0; l < 4; ++l) {
  3770. y[l + 0] = d.x() * (q[l] & 0xF) + d.y();
  3771. y[l + 16] = d.x() * (q[l] >> 4) + d.y();
  3772. }
  3773. }
  3774. //================================== k-quants
  3775. template<typename dst_t>
  3776. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3777. const sycl::nd_item<3> &item_ct1) {
  3778. const int i = item_ct1.get_group(2);
  3779. const block_q2_K * x = (const block_q2_K *) vx;
  3780. const int tid = item_ct1.get_local_id(2);
  3781. #if QK_K == 256
  3782. const int n = tid/32;
  3783. const int l = tid - 32*n;
  3784. const int is = 8*n + l/16;
  3785. const uint8_t q = x[i].qs[32*n + l];
  3786. dst_t * y = yy + i*QK_K + 128*n;
  3787. float dall = x[i].dm[0];
  3788. float dmin = x[i].dm[1];
  3789. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3790. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3791. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  3792. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  3793. #else
  3794. const int is = tid/16; // 0 or 1
  3795. const int il = tid%16; // 0...15
  3796. const uint8_t q = x[i].qs[il] >> (2*is);
  3797. dst_t * y = yy + i*QK_K + 16*is + il;
  3798. float dall = x[i].dm[0];
  3799. float dmin = x[i].dm[1];
  3800. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3801. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3802. #endif
  3803. }
  3804. template<typename dst_t>
  3805. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3806. const sycl::nd_item<3> &item_ct1) {
  3807. const int i = item_ct1.get_group(2);
  3808. const block_q3_K * x = (const block_q3_K *) vx;
  3809. #if QK_K == 256
  3810. const int r = item_ct1.get_local_id(2) / 4;
  3811. const int tid = r/2;
  3812. const int is0 = r%2;
  3813. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  3814. const int n = tid / 4;
  3815. const int j = tid - 4*n;
  3816. uint8_t m = 1 << (4*n + j);
  3817. int is = 8*n + 2*j + is0;
  3818. int shift = 2*j;
  3819. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  3820. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  3821. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  3822. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  3823. float d_all = x[i].d;
  3824. float dl = d_all * (us - 32);
  3825. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  3826. const uint8_t * q = x[i].qs + 32*n;
  3827. const uint8_t * hm = x[i].hmask;
  3828. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  3829. #else
  3830. const int tid = item_ct1.get_local_id(2);
  3831. const int is = tid/16; // 0 or 1
  3832. const int il = tid%16; // 0...15
  3833. const int im = il/8; // 0...1
  3834. const int in = il%8; // 0...7
  3835. dst_t * y = yy + i*QK_K + 16*is + il;
  3836. const uint8_t q = x[i].qs[il] >> (2*is);
  3837. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  3838. const float d = (float)x[i].d;
  3839. if (is == 0) {
  3840. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3841. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3842. } else {
  3843. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3844. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3845. }
  3846. #endif
  3847. }
  3848. #if QK_K == 256
  3849. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  3850. if (j < 4) {
  3851. d = q[j] & 63; m = q[j + 4] & 63;
  3852. } else {
  3853. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  3854. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  3855. }
  3856. }
  3857. #endif
  3858. template<typename dst_t>
  3859. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3860. const sycl::nd_item<3> &item_ct1) {
  3861. const block_q4_K * x = (const block_q4_K *) vx;
  3862. const int i = item_ct1.get_group(2);
  3863. #if QK_K == 256
  3864. // assume 32 threads
  3865. const int tid = item_ct1.get_local_id(2);
  3866. const int il = tid/8;
  3867. const int ir = tid%8;
  3868. const int is = 2*il;
  3869. const int n = 4;
  3870. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  3871. const float dall = x[i].dm[0];
  3872. const float dmin = x[i].dm[1];
  3873. const uint8_t * q = x[i].qs + 32*il + n*ir;
  3874. uint8_t sc, m;
  3875. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3876. const float d1 = dall * sc; const float m1 = dmin * m;
  3877. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3878. const float d2 = dall * sc; const float m2 = dmin * m;
  3879. for (int l = 0; l < n; ++l) {
  3880. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  3881. y[l +32] = d2 * (q[l] >> 4) - m2;
  3882. }
  3883. #else
  3884. const int tid = item_ct1.get_local_id(2);
  3885. const uint8_t * q = x[i].qs;
  3886. dst_t * y = yy + i*QK_K;
  3887. const float d = (float)x[i].dm[0];
  3888. const float m = (float)x[i].dm[1];
  3889. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  3890. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  3891. #endif
  3892. }
  3893. template<typename dst_t>
  3894. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3895. const sycl::nd_item<3> &item_ct1) {
  3896. const block_q5_K * x = (const block_q5_K *) vx;
  3897. const int i = item_ct1.get_group(2);
  3898. #if QK_K == 256
  3899. // assume 64 threads - this is very slightly better than the one below
  3900. const int tid = item_ct1.get_local_id(2);
  3901. const int il = tid/16; // il is in 0...3
  3902. const int ir = tid%16; // ir is in 0...15
  3903. const int is = 2*il; // is is in 0...6
  3904. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  3905. const float dall = x[i].dm[0];
  3906. const float dmin = x[i].dm[1];
  3907. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  3908. const uint8_t * qh = x[i].qh + 2*ir;
  3909. uint8_t sc, m;
  3910. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3911. const float d1 = dall * sc; const float m1 = dmin * m;
  3912. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3913. const float d2 = dall * sc; const float m2 = dmin * m;
  3914. uint8_t hm = 1 << (2*il);
  3915. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  3916. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  3917. hm <<= 1;
  3918. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  3919. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  3920. #else
  3921. const int tid = item_ct1.get_local_id(2);
  3922. const uint8_t q = x[i].qs[tid];
  3923. const int im = tid/8; // 0...3
  3924. const int in = tid%8; // 0...7
  3925. const int is = tid/16; // 0 or 1
  3926. const uint8_t h = x[i].qh[in] >> im;
  3927. const float d = x[i].d;
  3928. dst_t * y = yy + i*QK_K + tid;
  3929. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  3930. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  3931. #endif
  3932. }
  3933. template<typename dst_t>
  3934. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3935. const sycl::nd_item<3> &item_ct1) {
  3936. const block_q6_K * x = (const block_q6_K *) vx;
  3937. const int i = item_ct1.get_group(2);
  3938. #if QK_K == 256
  3939. // assume 64 threads - this is very slightly better than the one below
  3940. const int tid = item_ct1.get_local_id(2);
  3941. const int ip = tid/32; // ip is 0 or 1
  3942. const int il = tid - 32*ip; // 0...32
  3943. const int is = 8*ip + il/16;
  3944. dst_t * y = yy + i*QK_K + 128*ip + il;
  3945. const float d = x[i].d;
  3946. const uint8_t * ql = x[i].ql + 64*ip + il;
  3947. const uint8_t qh = x[i].qh[32*ip + il];
  3948. const int8_t * sc = x[i].scales + is;
  3949. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3950. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  3951. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3952. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  3953. #else
  3954. // assume 32 threads
  3955. const int tid = item_ct1.get_local_id(2);
  3956. const int ip = tid/16; // 0 or 1
  3957. const int il = tid - 16*ip; // 0...15
  3958. dst_t * y = yy + i*QK_K + 16*ip + il;
  3959. const float d = x[i].d;
  3960. const uint8_t ql = x[i].ql[16*ip + il];
  3961. const uint8_t qh = x[i].qh[il] >> (2*ip);
  3962. const int8_t * sc = x[i].scales;
  3963. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3964. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3965. #endif
  3966. }
  3967. template<typename dst_t>
  3968. static void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3969. const sycl::nd_item<3> &item_ct1,
  3970. const uint64_t *iq2xxs_grid_ptr,
  3971. const uint8_t *ksigns_iq2xs_ptr,
  3972. const uint8_t *kmask_iq2xs_ptr) {
  3973. const int i = item_ct1.get_group(2);
  3974. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  3975. const int tid = item_ct1.get_local_id(2);
  3976. #if QK_K == 256
  3977. const int il = tid/8; // 0...3
  3978. const int ib = tid%8; // 0...7
  3979. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3980. const uint16_t * q2 = x[i].qs + 4*ib;
  3981. const uint8_t * aux8 = (const uint8_t *)q2;
  3982. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid_ptr + aux8[il]);
  3983. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3984. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  3985. const uint8_t signs = ksigns_iq2xs_ptr[(aux32 >> 7*il) & 127];
  3986. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs_ptr[j] ? -1.f : 1.f);
  3987. #else
  3988. assert(false);
  3989. #endif
  3990. }
  3991. template<typename dst_t>
  3992. static void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3993. const sycl::nd_item<3> &item_ct1,
  3994. const uint64_t *iq2xs_grid,
  3995. const uint8_t *ksigns_iq2xs,
  3996. const uint8_t *kmask_iq2xs) {
  3997. const int i = item_ct1.get_group(2);
  3998. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  3999. const int tid = item_ct1.get_local_id(2);
  4000. #if QK_K == 256
  4001. const int il = tid/8; // 0...3
  4002. const int ib = tid%8; // 0...7
  4003. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4004. const uint16_t * q2 = x[i].qs + 4*ib;
  4005. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  4006. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  4007. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  4008. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4009. #else
  4010. assert(false);
  4011. #endif
  4012. }
  4013. template <typename dst_t>
  4014. __dpct_inline__ static void
  4015. dequantize_block_iq2_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4016. const sycl::nd_item<3> &item_ct1) {
  4017. const int i = item_ct1.get_group(2);
  4018. const block_iq2_s * x = (const block_iq2_s *) vx;
  4019. const int tid = item_ct1.get_local_id(2);
  4020. #if QK_K == 256
  4021. const int il = tid/8; // 0...3
  4022. const int ib = tid%8; // 0...7
  4023. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4024. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  4025. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  4026. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  4027. #pragma unroll
  4028. for (int j = 0; j < 8; ++j)
  4029. y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4030. #else
  4031. assert(false);
  4032. #endif
  4033. }
  4034. template<typename dst_t>
  4035. static void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4036. const sycl::nd_item<3> &item_ct1,
  4037. const uint32_t *iq3xxs_grid,
  4038. const uint8_t *ksigns_iq2xs,
  4039. const uint8_t *kmask_iq2xs) {
  4040. const int i = item_ct1.get_group(2);
  4041. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  4042. const int tid = item_ct1.get_local_id(2);
  4043. #if QK_K == 256
  4044. const int il = tid/8; // 0...3
  4045. const int ib = tid%8; // 0...7
  4046. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4047. const uint8_t * q3 = x[i].qs + 8*ib;
  4048. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  4049. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  4050. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  4051. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4052. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  4053. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  4054. for (int j = 0; j < 4; ++j) {
  4055. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4056. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4057. }
  4058. #else
  4059. assert(false);
  4060. #endif
  4061. }
  4062. template <typename dst_t>
  4063. __dpct_inline__ static void
  4064. dequantize_block_iq3_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4065. const sycl::nd_item<3> &item_ct1,
  4066. const uint8_t *kmask_iq2xs, const uint32_t *iq3s_grid) {
  4067. const int i = item_ct1.get_group(2);
  4068. const block_iq3_s * x = (const block_iq3_s *) vx;
  4069. const int tid = item_ct1.get_local_id(2);
  4070. #if QK_K == 256
  4071. const int il = tid/8; // 0...3
  4072. const int ib = tid%8; // 0...7
  4073. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4074. const uint8_t * qs = x[i].qs + 8*ib;
  4075. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  4076. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  4077. const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
  4078. const uint8_t signs = x[i].signs[4*ib + il];
  4079. #pragma unroll
  4080. for (int j = 0; j < 4; ++j) {
  4081. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4082. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4083. }
  4084. #else
  4085. assert(false);
  4086. #endif
  4087. }
  4088. template <typename dst_t>
  4089. __dpct_inline__ static void
  4090. dequantize_block_iq1_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4091. const sycl::nd_item<3> &item_ct1,
  4092. const uint32_t *iq1s_grid_gpu) {
  4093. const int i = item_ct1.get_group(2);
  4094. const block_iq1_s * x = (const block_iq1_s *) vx;
  4095. const int tid = item_ct1.get_local_id(2);
  4096. #if QK_K == 256
  4097. const int il = tid/8; // 0...3
  4098. const int ib = tid%8; // 0...7
  4099. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4100. const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
  4101. const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
  4102. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  4103. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
  4104. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  4105. grid32[0] &= 0x0f0f0f0f;
  4106. #pragma unroll
  4107. for (int j = 0; j < 8; ++j) {
  4108. y[j] = d * (q[j] + delta);
  4109. }
  4110. #else
  4111. assert(false);
  4112. #endif
  4113. }
  4114. template <typename dst_t>
  4115. __dpct_inline__ static void
  4116. dequantize_block_iq1_m(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4117. const sycl::nd_item<3> &item_ct1,
  4118. const uint32_t *iq1s_grid_gpu) {
  4119. const int i = item_ct1.get_group(2);
  4120. const block_iq1_m * x = (const block_iq1_m *) vx;
  4121. const int tid = item_ct1.get_local_id(2);
  4122. #if QK_K == 256
  4123. const int il = tid/8; // 0...3
  4124. const int ib = tid%8; // 0...7
  4125. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4126. const uint16_t * sc = (const uint16_t *)x[i].scales;
  4127. iq1m_scale_t scale;
  4128. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  4129. const int ib16 = 2*ib + il/2; // sc[ib16/4] >> 3*(ib16%4) -> sc[ib/2] >> 3*((2*ib+il/2)%4);
  4130. const float d = (float)scale.f16 * (2*((sc[ib16/4] >> 3*(ib16%4)) & 0x7) + 1);
  4131. const float delta = x[i].qh[2*ib+il/2] & (0x08 << 4*(il%2)) ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA;
  4132. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  4133. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[2*ib+il/2] >> 4*(il%2)) & 7) << 8)];
  4134. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  4135. grid32[0] &= 0x0f0f0f0f;
  4136. #pragma unroll
  4137. for (int j = 0; j < 8; ++j) {
  4138. y[j] = d * (q[j] + delta);
  4139. }
  4140. #else
  4141. assert(false);
  4142. #endif
  4143. }
  4144. template <typename dst_t>
  4145. __dpct_inline__ static void
  4146. dequantize_block_iq4_nl(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4147. const sycl::nd_item<3> &item_ct1) {
  4148. const int i = item_ct1.get_group(2);
  4149. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  4150. const int tid = item_ct1.get_local_id(2);
  4151. const int il = tid/8; // 0...3
  4152. const int ib = tid%8; // 0...7
  4153. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  4154. const uint8_t * q4 = x[ib].qs + 4*il;
  4155. const float d = (float)x[ib].d;
  4156. #pragma unroll
  4157. for (int j = 0; j < 4; ++j) {
  4158. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  4159. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  4160. }
  4161. }
  4162. template <typename dst_t>
  4163. __dpct_inline__ static void
  4164. dequantize_block_iq4_xs(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4165. const sycl::nd_item<3> &item_ct1) {
  4166. const int i = item_ct1.get_group(2);
  4167. const block_iq4_xs * x = (const block_iq4_xs *)vx;
  4168. const int tid = item_ct1.get_local_id(2);
  4169. const int il = tid/8; // 0...3
  4170. const int ib = tid%8; // 0...7
  4171. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  4172. const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
  4173. 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);
  4174. #pragma unroll
  4175. for (int j = 0; j < 4; ++j) {
  4176. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  4177. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  4178. }
  4179. }
  4180. /*
  4181. DPCT1110:4: The total declared local variable size in device function
  4182. dequantize_mul_mat_vec_q2_k exceeds 128 bytes and may cause high register
  4183. pressure. Consult with your hardware vendor to find the total register size
  4184. available and adjust the code, or use smaller sub-group size to avoid high
  4185. register pressure.
  4186. */
  4187. static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
  4188. const float *__restrict__ yy,
  4189. float *__restrict__ dst,
  4190. const int ncols, int nrows,
  4191. const sycl::nd_item<3> &item_ct1) {
  4192. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4193. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4194. item_ct1.get_local_id(1);
  4195. if (row > nrows) return;
  4196. const int num_blocks_per_row = ncols / QK_K;
  4197. const int ib0 = row*num_blocks_per_row;
  4198. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  4199. float tmp = 0; // partial sum for thread in warp
  4200. #if QK_K == 256
  4201. const int tid =
  4202. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  4203. const int ix =
  4204. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4205. const int step = 16/K_QUANTS_PER_ITERATION;
  4206. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4207. const int in = tid - step*im; // 0...15 or 0...7
  4208. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  4209. const int q_offset = 32*im + l0;
  4210. const int s_offset = 8*im;
  4211. const int y_offset = 128*im + l0;
  4212. uint32_t aux[4];
  4213. const uint8_t * d = (const uint8_t *)aux;
  4214. const uint8_t * m = (const uint8_t *)(aux + 2);
  4215. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4216. const float * y = yy + i * QK_K + y_offset;
  4217. const uint8_t * q = x[i].qs + q_offset;
  4218. const float dall = x[i].dm[0];
  4219. const float dmin = x[i].dm[1];
  4220. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  4221. aux[0] = a[0] & 0x0f0f0f0f;
  4222. aux[1] = a[1] & 0x0f0f0f0f;
  4223. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  4224. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  4225. float sum1 = 0, sum2 = 0;
  4226. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4227. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  4228. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  4229. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  4230. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  4231. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  4232. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  4233. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  4234. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  4235. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  4236. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  4237. }
  4238. tmp += dall * sum1 - dmin * sum2;
  4239. }
  4240. #else
  4241. const int tid = item_ct1.get_local_id(2) /
  4242. (2 * K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4243. const int ix = item_ct1.get_local_id(2) %
  4244. (2 * K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4245. const int offset = tid * K_QUANTS_PER_ITERATION;
  4246. uint32_t uaux[2];
  4247. const uint8_t * d = (const uint8_t *)uaux;
  4248. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4249. const float * y = yy + i * QK_K + offset;
  4250. const uint8_t * q = x[i].qs + offset;
  4251. const uint32_t * s = (const uint32_t *)x[i].scales;
  4252. uaux[0] = s[0] & 0x0f0f0f0f;
  4253. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  4254. const sycl::float2 dall =
  4255. x[i].dm.convert<float, sycl::rounding_mode::automatic>();
  4256. float sum1 = 0, sum2 = 0;
  4257. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4258. const uint8_t ql = q[l];
  4259. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  4260. + y[l+16] * d[1] * ((ql >> 2) & 3)
  4261. + y[l+32] * d[2] * ((ql >> 4) & 3)
  4262. + y[l+48] * d[3] * ((ql >> 6) & 3);
  4263. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  4264. }
  4265. tmp += dall.x() * sum1 - dall.y() * sum2;
  4266. }
  4267. #endif
  4268. // sum up partial sums and write back result
  4269. #pragma unroll
  4270. for (int mask = 16; mask > 0; mask >>= 1) {
  4271. tmp +=
  4272. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4273. }
  4274. if (item_ct1.get_local_id(2) == 0) {
  4275. dst[row] = tmp;
  4276. }
  4277. }
  4278. /*
  4279. DPCT1110:5: The total declared local variable size in device function
  4280. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  4281. pressure. Consult with your hardware vendor to find the total register size
  4282. available and adjust the code, or use smaller sub-group size to avoid high
  4283. register pressure.
  4284. */
  4285. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  4286. const float *__restrict__ yy,
  4287. float *__restrict__ dst,
  4288. const int ncols, int nrows,
  4289. const sycl::nd_item<3> &item_ct1) {
  4290. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4291. item_ct1.get_local_id(1);
  4292. if (row > nrows) return;
  4293. const int num_blocks_per_row = ncols / QK_K;
  4294. const int ib0 = row*num_blocks_per_row;
  4295. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  4296. float tmp = 0; // partial sum for thread in warp
  4297. #if QK_K == 256
  4298. const uint16_t kmask1 = 0x0303;
  4299. const uint16_t kmask2 = 0x0f0f;
  4300. const int tid =
  4301. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4302. const int ix =
  4303. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4304. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  4305. const int step = 16/K_QUANTS_PER_ITERATION;
  4306. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4307. const int in = tid - step*im; // 0....15 or 0...7
  4308. const uint8_t m = 1 << (4*im);
  4309. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  4310. const int q_offset = 32*im + l0;
  4311. const int y_offset = 128*im + l0;
  4312. uint16_t utmp[4];
  4313. const int8_t * s = (const int8_t *)utmp;
  4314. const uint16_t s_shift = 4*im;
  4315. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4316. const float * y = yy + i * QK_K + y_offset;
  4317. const uint8_t * q = x[i].qs + q_offset;
  4318. const uint8_t * h = x[i].hmask + l0;
  4319. const uint16_t * a = (const uint16_t *)x[i].scales;
  4320. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  4321. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  4322. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  4323. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  4324. const float d = x[i].d;
  4325. float sum = 0;
  4326. for (int l = 0; l < n; ++l) {
  4327. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  4328. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  4329. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  4330. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  4331. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  4332. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  4333. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  4334. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  4335. }
  4336. tmp += d * sum;
  4337. }
  4338. #else
  4339. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4340. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4341. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  4342. const int in = offset/8; // 0 or 1
  4343. const int im = offset%8; // 0...7
  4344. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4345. const float * y = yy + i * QK_K + offset;
  4346. const uint8_t * q = x[i].qs + offset;
  4347. const uint8_t * s = x[i].scales;
  4348. const float dall = (float)x[i].d;
  4349. float sum = 0;
  4350. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4351. const uint8_t hl = x[i].hmask[im+l] >> in;
  4352. const uint8_t ql = q[l];
  4353. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  4354. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  4355. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  4356. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  4357. }
  4358. tmp += sum;
  4359. }
  4360. #endif
  4361. // sum up partial sums and write back result
  4362. #pragma unroll
  4363. for (int mask = 16; mask > 0; mask >>= 1) {
  4364. tmp +=
  4365. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4366. }
  4367. if (item_ct1.get_local_id(2) == 0) {
  4368. dst[row] = tmp;
  4369. }
  4370. }
  4371. /*
  4372. DPCT1110:6: The total declared local variable size in device function
  4373. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  4374. pressure. Consult with your hardware vendor to find the total register size
  4375. available and adjust the code, or use smaller sub-group size to avoid high
  4376. register pressure.
  4377. */
  4378. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  4379. const float *__restrict__ yy,
  4380. float *__restrict__ dst,
  4381. const int ncols, int nrows,
  4382. const sycl::nd_item<3> &item_ct1) {
  4383. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4384. item_ct1.get_local_id(1);
  4385. if (row > nrows) return;
  4386. const int num_blocks_per_row = ncols / QK_K;
  4387. const int ib0 = row*num_blocks_per_row;
  4388. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  4389. #if QK_K == 256
  4390. const uint16_t kmask1 = 0x3f3f;
  4391. const uint16_t kmask2 = 0x0f0f;
  4392. const uint16_t kmask3 = 0xc0c0;
  4393. const int tid =
  4394. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4395. const int ix =
  4396. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4397. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  4398. const int il = tid/step; // 0...3
  4399. const int ir = tid - step*il; // 0...7 or 0...3
  4400. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  4401. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4402. const int in = il%2;
  4403. const int l0 = n*(2*ir + in);
  4404. const int q_offset = 32*im + l0;
  4405. const int y_offset = 64*im + l0;
  4406. uint16_t aux[4];
  4407. const uint8_t * sc = (const uint8_t *)aux;
  4408. #if K_QUANTS_PER_ITERATION == 2
  4409. uint32_t q32[4];
  4410. const uint8_t * q4 = (const uint8_t *)q32;
  4411. #else
  4412. uint16_t q16[4];
  4413. const uint8_t * q4 = (const uint8_t *)q16;
  4414. #endif
  4415. float tmp = 0; // partial sum for thread in warp
  4416. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4417. const float * y1 = yy + i*QK_K + y_offset;
  4418. const float * y2 = y1 + 128;
  4419. const float dall = x[i].dm[0];
  4420. const float dmin = x[i].dm[1];
  4421. const uint16_t * a = (const uint16_t *)x[i].scales;
  4422. aux[0] = a[im+0] & kmask1;
  4423. aux[1] = a[im+2] & kmask1;
  4424. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4425. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4426. #if K_QUANTS_PER_ITERATION == 2
  4427. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4428. const uint32_t * q2 = q1 + 16;
  4429. q32[0] = q1[0] & 0x0f0f0f0f;
  4430. q32[1] = q1[0] & 0xf0f0f0f0;
  4431. q32[2] = q2[0] & 0x0f0f0f0f;
  4432. q32[3] = q2[0] & 0xf0f0f0f0;
  4433. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4434. float smin = 0;
  4435. for (int l = 0; l < 4; ++l) {
  4436. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4437. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4438. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4439. }
  4440. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4441. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4442. dmin * smin;
  4443. #else
  4444. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4445. const uint16_t * q2 = q1 + 32;
  4446. q16[0] = q1[0] & 0x0f0f;
  4447. q16[1] = q1[0] & 0xf0f0;
  4448. q16[2] = q2[0] & 0x0f0f;
  4449. q16[3] = q2[0] & 0xf0f0;
  4450. float4 s = {0.f, 0.f, 0.f, 0.f};
  4451. float smin = 0;
  4452. for (int l = 0; l < 2; ++l) {
  4453. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  4454. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  4455. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4456. }
  4457. 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;
  4458. #endif
  4459. }
  4460. #else
  4461. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
  4462. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
  4463. const int step = tid * K_QUANTS_PER_ITERATION;
  4464. uint16_t aux16[2];
  4465. const uint8_t * s = (const uint8_t *)aux16;
  4466. float tmp = 0;
  4467. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4468. const uint8_t * q = x[i].qs + step;
  4469. const float * y = yy + i*QK_K + step;
  4470. const uint16_t * a = (const uint16_t *)x[i].scales;
  4471. aux16[0] = a[0] & 0x0f0f;
  4472. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4473. const float d = (float)x[i].dm[0];
  4474. const float m = (float)x[i].dm[1];
  4475. float sum = 0.f;
  4476. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4477. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  4478. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  4479. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  4480. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  4481. }
  4482. tmp += sum;
  4483. }
  4484. #endif
  4485. // sum up partial sums and write back result
  4486. #pragma unroll
  4487. for (int mask = 16; mask > 0; mask >>= 1) {
  4488. tmp +=
  4489. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4490. }
  4491. if (tid == 0) {
  4492. dst[row] = tmp;
  4493. }
  4494. }
  4495. /*
  4496. DPCT1110:7: The total declared local variable size in device function
  4497. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  4498. pressure. Consult with your hardware vendor to find the total register size
  4499. available and adjust the code, or use smaller sub-group size to avoid high
  4500. register pressure.
  4501. */
  4502. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  4503. const float *__restrict__ yy,
  4504. float *__restrict__ dst,
  4505. const int ncols,
  4506. const sycl::nd_item<3> &item_ct1) {
  4507. const int row = item_ct1.get_group(2);
  4508. const int num_blocks_per_row = ncols / QK_K;
  4509. const int ib0 = row*num_blocks_per_row;
  4510. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  4511. float tmp = 0; // partial sum for thread in warp
  4512. #if QK_K == 256
  4513. const uint16_t kmask1 = 0x3f3f;
  4514. const uint16_t kmask2 = 0x0f0f;
  4515. const uint16_t kmask3 = 0xc0c0;
  4516. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  4517. const int ix = item_ct1.get_local_id(2) % 2;
  4518. const int il = tid/4; // 0...3
  4519. const int ir = tid - 4*il;// 0...3
  4520. const int n = 2;
  4521. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4522. const int in = il%2;
  4523. const int l0 = n*(2*ir + in);
  4524. const int q_offset = 32*im + l0;
  4525. const int y_offset = 64*im + l0;
  4526. const uint8_t hm1 = 1 << (2*im);
  4527. const uint8_t hm2 = hm1 << 4;
  4528. uint16_t aux[4];
  4529. const uint8_t * sc = (const uint8_t *)aux;
  4530. uint16_t q16[8];
  4531. const uint8_t * q4 = (const uint8_t *)q16;
  4532. for (int i = ix; i < num_blocks_per_row; i += 2) {
  4533. const uint8_t * ql1 = x[i].qs + q_offset;
  4534. const uint8_t * qh = x[i].qh + l0;
  4535. const float * y1 = yy + i*QK_K + y_offset;
  4536. const float * y2 = y1 + 128;
  4537. const float dall = x[i].dm[0];
  4538. const float dmin = x[i].dm[1];
  4539. const uint16_t * a = (const uint16_t *)x[i].scales;
  4540. aux[0] = a[im+0] & kmask1;
  4541. aux[1] = a[im+2] & kmask1;
  4542. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4543. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4544. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  4545. float smin = 0;
  4546. const uint16_t * q1 = (const uint16_t *)ql1;
  4547. const uint16_t * q2 = q1 + 32;
  4548. q16[0] = q1[0] & 0x0f0f;
  4549. q16[1] = q1[8] & 0x0f0f;
  4550. q16[2] = (q1[0] >> 4) & 0x0f0f;
  4551. q16[3] = (q1[8] >> 4) & 0x0f0f;
  4552. q16[4] = q2[0] & 0x0f0f;
  4553. q16[5] = q2[8] & 0x0f0f;
  4554. q16[6] = (q2[0] >> 4) & 0x0f0f;
  4555. q16[7] = (q2[8] >> 4) & 0x0f0f;
  4556. for (int l = 0; l < n; ++l) {
  4557. sum.x() +=
  4558. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  4559. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  4560. sum.y() +=
  4561. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  4562. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  4563. sum.z() +=
  4564. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  4565. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  4566. sum.w() +=
  4567. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  4568. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  4569. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  4570. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  4571. }
  4572. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  4573. sum.w() * sc[5]) -
  4574. dmin * smin;
  4575. }
  4576. #else
  4577. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
  4578. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
  4579. const int step = tid * K_QUANTS_PER_ITERATION;
  4580. const int im = step/8;
  4581. const int in = step%8;
  4582. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4583. const uint8_t * q = x[i].qs + step;
  4584. const int8_t * s = x[i].scales;
  4585. const float * y = yy + i*QK_K + step;
  4586. const float d = x[i].d;
  4587. float sum = 0.f;
  4588. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4589. const uint8_t h = x[i].qh[in+j] >> im;
  4590. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  4591. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  4592. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  4593. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  4594. }
  4595. tmp += sum;
  4596. }
  4597. #endif
  4598. // sum up partial sums and write back result
  4599. #pragma unroll
  4600. for (int mask = 16; mask > 0; mask >>= 1) {
  4601. tmp +=
  4602. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4603. }
  4604. if (item_ct1.get_local_id(2) == 0) {
  4605. dst[row] = tmp;
  4606. }
  4607. }
  4608. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  4609. const sycl::nd_item<3> &item_ct1) {
  4610. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4611. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4612. item_ct1.get_local_id(1);
  4613. if (row > nrows) return;
  4614. const int num_blocks_per_row = ncols / QK_K;
  4615. const int ib0 = row*num_blocks_per_row;
  4616. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  4617. #if QK_K == 256
  4618. const int tid =
  4619. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4620. const int ix =
  4621. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  4622. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  4623. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4624. const int in = tid - step*im; // 0...15 or 0...7
  4625. #if K_QUANTS_PER_ITERATION == 1
  4626. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  4627. const int is = 0;
  4628. #else
  4629. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  4630. const int is = in / 4;
  4631. #endif
  4632. const int ql_offset = 64*im + l0;
  4633. const int qh_offset = 32*im + l0;
  4634. const int s_offset = 8*im + is;
  4635. const int y_offset = 128*im + l0;
  4636. float tmp = 0; // partial sum for thread in warp
  4637. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4638. const float * y = yy + i * QK_K + y_offset;
  4639. const uint8_t * ql = x[i].ql + ql_offset;
  4640. const uint8_t * qh = x[i].qh + qh_offset;
  4641. const int8_t * s = x[i].scales + s_offset;
  4642. const float d = x[i].d;
  4643. #if K_QUANTS_PER_ITERATION == 1
  4644. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  4645. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  4646. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  4647. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  4648. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  4649. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  4650. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  4651. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  4652. tmp += sum;
  4653. #else
  4654. float sum = 0;
  4655. for (int l = 0; l < 4; ++l) {
  4656. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  4657. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  4658. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  4659. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  4660. }
  4661. tmp += sum;
  4662. #endif
  4663. }
  4664. #else
  4665. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...7
  4666. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0...3
  4667. const int step = tid * K_QUANTS_PER_ITERATION;
  4668. float tmp = 0; // partial sum for thread in warp
  4669. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4670. const float * y = yy + i * QK_K + step;
  4671. const uint8_t * ql = x[i].ql + step;
  4672. const uint8_t * qh = x[i].qh + step;
  4673. const int8_t * s = x[i].scales;
  4674. const float d = x[i+0].d;
  4675. float sum = 0;
  4676. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4677. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  4678. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  4679. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  4680. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  4681. }
  4682. tmp += sum;
  4683. }
  4684. #endif
  4685. // sum up partial sums and write back result
  4686. #pragma unroll
  4687. for (int mask = 16; mask > 0; mask >>= 1) {
  4688. tmp +=
  4689. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4690. }
  4691. if (tid == 0) {
  4692. dst[row] = tmp;
  4693. }
  4694. }
  4695. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4696. const sycl::half *x = (const sycl::half *)vx;
  4697. // automatic half -> float type cast if dfloat == float
  4698. v.x() = x[ib + iqs + 0];
  4699. v.y() = x[ib + iqs + 1];
  4700. }
  4701. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4702. const float * x = (const float *) vx;
  4703. // automatic half -> float type cast if dfloat == float
  4704. v.x() = x[ib + iqs + 0];
  4705. v.y() = x[ib + iqs + 1];
  4706. }
  4707. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  4708. const sycl::nd_item<3> &item_ct1) {
  4709. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4710. item_ct1.get_local_id(2);
  4711. if (ix >= kx_padded) {
  4712. return;
  4713. }
  4714. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4715. item_ct1.get_local_id(1);
  4716. const int i_padded = iy*kx_padded + ix;
  4717. block_q8_1 * y = (block_q8_1 *) vy;
  4718. const int ib = i_padded / QK8_1; // block index
  4719. const int iqs = i_padded % QK8_1; // quant index
  4720. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  4721. float amax = sycl::fabs((float)xi);
  4722. float sum = xi;
  4723. #pragma unroll
  4724. for (int mask = 16; mask > 0; mask >>= 1) {
  4725. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  4726. item_ct1.get_sub_group(), amax, mask));
  4727. sum +=
  4728. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  4729. }
  4730. const float d = amax / 127;
  4731. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  4732. y[ib].qs[iqs] = q;
  4733. if (iqs > 0) {
  4734. return;
  4735. }
  4736. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  4737. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  4738. }
  4739. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4740. static void k_get_rows(
  4741. const void * src0, const int32_t * src1, dst_t * dst,
  4742. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4743. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4744. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4745. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4746. size_t s10, size_t s11, size_t s12,
  4747. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4748. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4749. item_ct1.get_local_id(2)) *
  4750. 2;
  4751. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4752. item_ct1.get_local_id(1);
  4753. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4754. item_ct1.get_local_id(0)) /
  4755. ne12;
  4756. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4757. item_ct1.get_local_id(0)) %
  4758. ne12;
  4759. if (i00 >= ne00) {
  4760. return;
  4761. }
  4762. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4763. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4764. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  4765. const int ib = i00/qk; // block index
  4766. const int iqs = (i00%qk)/qr; // quant index
  4767. const int iybs = i00 - i00%qk; // dst block start index
  4768. const int y_offset = qr == 1 ? 1 : qk/2;
  4769. // dequantize
  4770. dfloat2 v;
  4771. dequantize_kernel(src0_row, ib, iqs, v);
  4772. dst_row[iybs + iqs + 0] = v.x();
  4773. dst_row[iybs + iqs + y_offset] = v.y();
  4774. }
  4775. template<typename src0_t, typename dst_t>
  4776. static void k_get_rows_float(
  4777. const src0_t * src0, const int32_t * src1, dst_t * dst,
  4778. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4779. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4780. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4781. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4782. size_t s10, size_t s11, size_t s12,
  4783. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4784. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4785. item_ct1.get_local_id(2);
  4786. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4787. item_ct1.get_local_id(1);
  4788. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4789. item_ct1.get_local_id(0)) /
  4790. ne12;
  4791. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4792. item_ct1.get_local_id(0)) %
  4793. ne12;
  4794. if (i00 >= ne00) {
  4795. return;
  4796. }
  4797. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4798. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4799. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  4800. dst_row[i00] = src0_row[i00];
  4801. }
  4802. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4803. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4804. const sycl::nd_item<3> &item_ct1) {
  4805. const int i = 2 * (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4806. item_ct1.get_local_id(2));
  4807. if (i >= k) {
  4808. return;
  4809. }
  4810. const int ib = i/qk; // block index
  4811. const int iqs = (i%qk)/qr; // quant index
  4812. const int iybs = i - i%qk; // y block start index
  4813. const int y_offset = qr == 1 ? 1 : qk/2;
  4814. // dequantize
  4815. dfloat2 v;
  4816. dequantize_kernel(vx, ib, iqs, v);
  4817. y[iybs + iqs + 0] = v.x();
  4818. y[iybs + iqs + y_offset] = v.y();
  4819. }
  4820. template <typename src_t, typename dst_t>
  4821. static void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4822. const sycl::nd_item<3> &item_ct1) {
  4823. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4824. item_ct1.get_local_id(2);
  4825. if (i >= k) {
  4826. return;
  4827. }
  4828. const src_t * x = (src_t *) vx;
  4829. y[i] = x[i];
  4830. }
  4831. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  4832. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  4833. #define VDR_Q4_0_Q8_1_MMVQ 2
  4834. #define VDR_Q4_0_Q8_1_MMQ 4
  4835. template <int vdr>
  4836. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  4837. const float &d4,
  4838. const sycl::half2 &ds8) {
  4839. int sumi = 0;
  4840. #pragma unroll
  4841. for (int i = 0; i < vdr; ++i) {
  4842. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4843. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4844. // SIMD dot product of quantized values
  4845. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4846. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4847. }
  4848. const sycl::float2 ds8f =
  4849. ds8.convert<float, sycl::rounding_mode::automatic>();
  4850. // second part effectively subtracts 8 from each quant value
  4851. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  4852. }
  4853. #define VDR_Q4_1_Q8_1_MMVQ 2
  4854. #define VDR_Q4_1_Q8_1_MMQ 4
  4855. template <int vdr>
  4856. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  4857. const sycl::half2 &dm4,
  4858. const sycl::half2 &ds8) {
  4859. int sumi = 0;
  4860. #pragma unroll
  4861. for (int i = 0; i < vdr; ++i) {
  4862. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4863. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4864. // SIMD dot product of quantized values
  4865. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4866. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4867. }
  4868. #ifdef GGML_SYCL_F16
  4869. const sycl::float2 tmp =
  4870. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4871. const float d4d8 = tmp.x();
  4872. const float m4s8 = tmp.y();
  4873. #else
  4874. const sycl::float2 dm4f =
  4875. dm4.convert<float, sycl::rounding_mode::automatic>();
  4876. const sycl::float2 ds8f =
  4877. ds8.convert<float, sycl::rounding_mode::automatic>();
  4878. const float d4d8 = dm4f.x() * ds8f.x();
  4879. const float m4s8 = dm4f.y() * ds8f.y();
  4880. #endif // GGML_SYCL_F16
  4881. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  4882. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  4883. }
  4884. #define VDR_Q5_0_Q8_1_MMVQ 2
  4885. #define VDR_Q5_0_Q8_1_MMQ 4
  4886. template <int vdr>
  4887. static __dpct_inline__ float
  4888. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  4889. const float &d5, const sycl::half2 &ds8) {
  4890. int sumi = 0;
  4891. #pragma unroll
  4892. for (int i = 0; i < vdr; ++i) {
  4893. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4894. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4895. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4896. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4897. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4898. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4899. sumi); // SIMD dot product of quantized values
  4900. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4901. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4902. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4903. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4904. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4905. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4906. sumi); // SIMD dot product of quantized values
  4907. }
  4908. const sycl::float2 ds8f =
  4909. ds8.convert<float, sycl::rounding_mode::automatic>();
  4910. // second part effectively subtracts 16 from each quant value
  4911. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  4912. }
  4913. #define VDR_Q5_1_Q8_1_MMVQ 2
  4914. #define VDR_Q5_1_Q8_1_MMQ 4
  4915. template <int vdr>
  4916. static __dpct_inline__ float
  4917. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  4918. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  4919. int sumi = 0;
  4920. #pragma unroll
  4921. for (int i = 0; i < vdr; ++i) {
  4922. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4923. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4924. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4925. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4926. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4927. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4928. sumi); // SIMD dot product of quantized values
  4929. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4930. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4931. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4932. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4933. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4934. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4935. sumi); // SIMD dot product of quantized values
  4936. }
  4937. #ifdef GGML_SYCL_F16
  4938. const sycl::float2 tmp =
  4939. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4940. const float d5d8 = tmp.x();
  4941. const float m5s8 = tmp.y();
  4942. #else
  4943. const sycl::float2 dm5f =
  4944. dm5.convert<float, sycl::rounding_mode::automatic>();
  4945. const sycl::float2 ds8f =
  4946. ds8.convert<float, sycl::rounding_mode::automatic>();
  4947. const float d5d8 = dm5f.x() * ds8f.x();
  4948. const float m5s8 = dm5f.y() * ds8f.y();
  4949. #endif // GGML_SYCL_F16
  4950. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  4951. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  4952. }
  4953. #define VDR_Q8_0_Q8_1_MMVQ 2
  4954. #define VDR_Q8_0_Q8_1_MMQ 8
  4955. template <int vdr>
  4956. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  4957. const float &d8_0,
  4958. const float &d8_1) {
  4959. int sumi = 0;
  4960. #pragma unroll
  4961. for (int i = 0; i < vdr; ++i) {
  4962. // SIMD dot product of quantized values
  4963. sumi = dpct::dp4a(v[i], u[i], sumi);
  4964. }
  4965. return d8_0*d8_1 * sumi;
  4966. }
  4967. template <int vdr>
  4968. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  4969. const sycl::half2 &dm8,
  4970. const sycl::half2 &ds8) {
  4971. int sumi = 0;
  4972. #pragma unroll
  4973. for (int i = 0; i < vdr; ++i) {
  4974. // SIMD dot product of quantized values
  4975. sumi = dpct::dp4a(v[i], u[i], sumi);
  4976. }
  4977. #ifdef GGML_SYCL_F16
  4978. const sycl::float2 tmp =
  4979. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4980. const float d8d8 = tmp.x();
  4981. const float m8s8 = tmp.y();
  4982. #else
  4983. const sycl::float2 dm8f =
  4984. dm8.convert<float, sycl::rounding_mode::automatic>();
  4985. const sycl::float2 ds8f =
  4986. ds8.convert<float, sycl::rounding_mode::automatic>();
  4987. const float d8d8 = dm8f.x() * ds8f.x();
  4988. const float m8s8 = dm8f.y() * ds8f.y();
  4989. #endif // GGML_SYCL_F16
  4990. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  4991. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  4992. }
  4993. #define VDR_Q2_K_Q8_1_MMVQ 1
  4994. #define VDR_Q2_K_Q8_1_MMQ 2
  4995. // contiguous v/x values
  4996. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  4997. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  4998. const sycl::half2 &dm2, const float *__restrict__ d8) {
  4999. float sumf_d = 0.0f;
  5000. float sumf_m = 0.0f;
  5001. #pragma unroll
  5002. for (int i = 0; i < QR2_K; ++i) {
  5003. const int sc = scales[2*i];
  5004. const int vi = (v >> (2*i)) & 0x03030303;
  5005. sumf_d +=
  5006. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  5007. // fill int with 4x m
  5008. int m = sc >> 4;
  5009. m |= m << 8;
  5010. m |= m << 16;
  5011. sumf_m += d8[i] *
  5012. dpct::dp4a(
  5013. m, u[i],
  5014. 0); // multiply constant q2_K part with sum of q8_1 values
  5015. }
  5016. const sycl::float2 dm2f =
  5017. dm2.convert<float, sycl::rounding_mode::automatic>();
  5018. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  5019. }
  5020. // contiguous u/y values
  5021. static __dpct_inline__ float
  5022. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5023. const uint8_t *__restrict__ scales,
  5024. const sycl::half2 &dm2, const float &d8) {
  5025. int sumi_d = 0;
  5026. int sumi_m = 0;
  5027. #pragma unroll
  5028. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  5029. int sumi_d_sc = 0;
  5030. const int sc = scales[i0 / (QI8_1/2)];
  5031. // fill int with 4x m
  5032. int m = sc >> 4;
  5033. m |= m << 8;
  5034. m |= m << 16;
  5035. #pragma unroll
  5036. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  5037. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  5038. sumi_m = dpct::dp4a(m, u[i],
  5039. sumi_m); // multiply sum of q8_1 values with m
  5040. }
  5041. sumi_d += sumi_d_sc * (sc & 0xF);
  5042. }
  5043. const sycl::float2 dm2f =
  5044. dm2.convert<float, sycl::rounding_mode::automatic>();
  5045. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  5046. }
  5047. #define VDR_Q3_K_Q8_1_MMVQ 1
  5048. #define VDR_Q3_K_Q8_1_MMQ 2
  5049. // contiguous v/x values
  5050. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  5051. const int &vl, const int &vh, const int *__restrict__ u,
  5052. const uint8_t *__restrict__ scales, const int &scale_offset,
  5053. const float &d3, const float *__restrict__ d8) {
  5054. float sumf = 0.0f;
  5055. #pragma unroll
  5056. for (int i = 0; i < QR3_K; ++i) {
  5057. const int isc = scale_offset + 2*i;
  5058. const int isc_low = isc % (QK_K/32);
  5059. const int sc_shift_low = 4 * (isc / (QK_K/32));
  5060. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  5061. const int isc_high = isc % (QK_K/64);
  5062. const int sc_shift_high = 2 * (isc / (QK_K/64));
  5063. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  5064. const int sc = (sc_low | sc_high) - 32;
  5065. const int vil = (vl >> (2*i)) & 0x03030303;
  5066. const int vih = ((vh >> i) << 2) & 0x04040404;
  5067. const int vi =
  5068. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  5069. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  5070. }
  5071. return d3 * sumf;
  5072. }
  5073. // contiguous u/y values
  5074. static __dpct_inline__ float
  5075. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5076. const int8_t *__restrict__ scales, const float &d3,
  5077. const float &d8) {
  5078. int sumi = 0;
  5079. #pragma unroll
  5080. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  5081. int sumi_sc = 0;
  5082. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  5083. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  5084. }
  5085. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  5086. }
  5087. return d3*d8 * sumi;
  5088. }
  5089. #define VDR_Q4_K_Q8_1_MMVQ 2
  5090. #define VDR_Q4_K_Q8_1_MMQ 8
  5091. // contiguous v/x values
  5092. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  5093. const int *__restrict__ v, const int *__restrict__ u,
  5094. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5095. const sycl::half2 &dm4, const float *__restrict__ d8) {
  5096. float sumf_d = 0.0f;
  5097. float sumf_m = 0.0f;
  5098. #pragma unroll
  5099. for (int i = 0; i < QR4_K; ++i) {
  5100. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  5101. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  5102. const int dot1 =
  5103. dpct::dp4a(v1i, u[2 * i + 1],
  5104. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  5105. const int dot2 =
  5106. dpct::dp4a(0x01010101, u[2 * i + 1],
  5107. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  5108. sumf_d += d8[i] * (dot1 * sc[i]);
  5109. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  5110. }
  5111. const sycl::float2 dm4f =
  5112. dm4.convert<float, sycl::rounding_mode::automatic>();
  5113. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5114. }
  5115. // contiguous u/y values
  5116. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  5117. const int *__restrict__ v, const int *__restrict__ u,
  5118. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5119. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5120. float sumf_d = 0.0f;
  5121. float sumf_m = 0.0f;
  5122. #pragma unroll
  5123. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  5124. int sumi_d = 0;
  5125. #pragma unroll
  5126. for (int j = 0; j < QI8_1; ++j) {
  5127. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  5128. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  5129. }
  5130. const sycl::float2 ds8f =
  5131. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5132. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5133. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5134. }
  5135. const sycl::float2 dm4f =
  5136. dm4.convert<float, sycl::rounding_mode::automatic>();
  5137. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5138. }
  5139. #define VDR_Q5_K_Q8_1_MMVQ 2
  5140. #define VDR_Q5_K_Q8_1_MMQ 8
  5141. // contiguous v/x values
  5142. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  5143. const int *__restrict__ vl, const int *__restrict__ vh,
  5144. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  5145. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  5146. const float *__restrict__ d8) {
  5147. float sumf_d = 0.0f;
  5148. float sumf_m = 0.0f;
  5149. #pragma unroll
  5150. for (int i = 0; i < QR5_K; ++i) {
  5151. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  5152. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  5153. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  5154. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  5155. const int v0i = vl0i | vh0i;
  5156. const int v1i = vl1i | vh1i;
  5157. const int dot1 =
  5158. dpct::dp4a(v0i, u[2 * i + 0],
  5159. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  5160. const int dot2 =
  5161. dpct::dp4a(0x01010101, u[2 * i + 0],
  5162. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  5163. sumf_d += d8[i] * (dot1 * sc[i]);
  5164. sumf_m += d8[i] * (dot2 * m[i]);
  5165. }
  5166. const sycl::float2 dm5f =
  5167. dm5.convert<float, sycl::rounding_mode::automatic>();
  5168. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  5169. }
  5170. // contiguous u/y values
  5171. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  5172. const int *__restrict__ v, const int *__restrict__ u,
  5173. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5174. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5175. float sumf_d = 0.0f;
  5176. float sumf_m = 0.0f;
  5177. #pragma unroll
  5178. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  5179. int sumi_d = 0;
  5180. #pragma unroll
  5181. for (int j = 0; j < QI8_1; ++j) {
  5182. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  5183. sumi_d); // SIMD dot product
  5184. }
  5185. const sycl::float2 ds8f =
  5186. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5187. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5188. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5189. }
  5190. const sycl::float2 dm4f =
  5191. dm4.convert<float, sycl::rounding_mode::automatic>();
  5192. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5193. }
  5194. #define VDR_Q6_K_Q8_1_MMVQ 1
  5195. #define VDR_Q6_K_Q8_1_MMQ 8
  5196. // contiguous v/x values
  5197. static __dpct_inline__ float
  5198. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  5199. const int *__restrict__ u,
  5200. const int8_t *__restrict__ scales, const float &d,
  5201. const float *__restrict__ d8) {
  5202. float sumf = 0.0f;
  5203. #pragma unroll
  5204. for (int i = 0; i < QR6_K; ++i) {
  5205. const int sc = scales[4*i];
  5206. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  5207. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  5208. const int vi = dpct::vectorized_binary<sycl::char4>(
  5209. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  5210. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  5211. }
  5212. return d*sumf;
  5213. }
  5214. // contiguous u/y values
  5215. static __dpct_inline__ float
  5216. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5217. const int8_t *__restrict__ sc, const float &d6,
  5218. const float *__restrict__ d8) {
  5219. float sumf_d = 0.0f;
  5220. #pragma unroll
  5221. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  5222. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  5223. #pragma unroll
  5224. for (int i = i0; i < i0 + 2; ++i) {
  5225. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  5226. sumi_d.x()); // SIMD dot product
  5227. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  5228. sumi_d.x()); // SIMD dot product
  5229. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  5230. sumi_d.y()); // SIMD dot product
  5231. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  5232. sumi_d.y()); // SIMD dot product
  5233. }
  5234. sumf_d += d8[i0 / 4] *
  5235. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  5236. }
  5237. return d6 * sumf_d;
  5238. }
  5239. static __dpct_inline__ float
  5240. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  5241. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5242. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  5243. int v[VDR_Q4_0_Q8_1_MMVQ];
  5244. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  5245. #pragma unroll
  5246. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  5247. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  5248. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5249. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  5250. }
  5251. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  5252. }
  5253. template <int mmq_y>
  5254. static __dpct_inline__ void
  5255. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5256. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  5257. (void)x_qh; (void)x_sc;
  5258. *x_ql = tile_x_qs_q4_0;
  5259. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  5260. }
  5261. template <int mmq_y, int nwarps, bool need_check>
  5262. static __dpct_inline__ void
  5263. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5264. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5265. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5266. const int &k, const int &blocks_per_row) {
  5267. (void)x_qh; (void)x_sc;
  5268. GGML_SYCL_ASSUME(i_offset >= 0);
  5269. GGML_SYCL_ASSUME(i_offset < nwarps);
  5270. GGML_SYCL_ASSUME(k >= 0);
  5271. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5272. const int kbx = k / QI4_0;
  5273. const int kqsx = k % QI4_0;
  5274. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  5275. float * x_dmf = (float *) x_dm;
  5276. #pragma unroll
  5277. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5278. int i = i0 + i_offset;
  5279. if (need_check) {
  5280. i = sycl::min(i, i_max);
  5281. }
  5282. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5283. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5284. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  5285. }
  5286. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  5287. const int kbxd = k % blocks_per_tile_x_row;
  5288. #pragma unroll
  5289. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  5290. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  5291. if (need_check) {
  5292. i = sycl::min(i, i_max);
  5293. }
  5294. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5295. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  5296. }
  5297. }
  5298. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  5299. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5300. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5301. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5302. const int &i, const int &j, const int &k) {
  5303. (void)x_qh; (void)x_sc;
  5304. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5305. const float * x_dmf = (const float *) x_dm;
  5306. int u[2*VDR_Q4_0_Q8_1_MMQ];
  5307. #pragma unroll
  5308. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  5309. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5310. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  5311. }
  5312. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  5313. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  5314. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5315. }
  5316. static __dpct_inline__ float
  5317. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  5318. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5319. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  5320. int v[VDR_Q4_1_Q8_1_MMVQ];
  5321. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  5322. #pragma unroll
  5323. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  5324. v[i] = get_int_from_uint8_aligned(bq4_1->qs, 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 + QI4_1);
  5327. }
  5328. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  5329. }
  5330. template <int mmq_y>
  5331. static __dpct_inline__ void
  5332. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5333. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  5334. (void)x_qh; (void)x_sc;
  5335. *x_ql = tile_x_qs_q4_1;
  5336. *x_dm = tile_x_dm_q4_1;
  5337. }
  5338. template <int mmq_y, int nwarps, bool need_check>
  5339. static __dpct_inline__ void
  5340. load_tiles_q4_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 / QI4_1;
  5350. const int kqsx = k % QI4_1;
  5351. const block_q4_1 * bx0 = (const block_q4_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_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5359. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5360. }
  5361. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  5362. const int kbxd = k % blocks_per_tile_x_row;
  5363. #pragma unroll
  5364. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  5365. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  5366. if (need_check) {
  5367. i = sycl::min(i, i_max);
  5368. }
  5369. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5370. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  5371. }
  5372. }
  5373. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  5374. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5375. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5376. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5377. const int &i, const int &j, const int &k) {
  5378. (void)x_qh; (void)x_sc;
  5379. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5380. int u[2*VDR_Q4_1_Q8_1_MMQ];
  5381. #pragma unroll
  5382. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  5383. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5384. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  5385. }
  5386. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  5387. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  5388. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5389. }
  5390. static __dpct_inline__ float
  5391. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  5392. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5393. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  5394. int vl[VDR_Q5_0_Q8_1_MMVQ];
  5395. int vh[VDR_Q5_0_Q8_1_MMVQ];
  5396. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  5397. #pragma unroll
  5398. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  5399. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  5400. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  5401. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5402. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  5403. }
  5404. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  5405. }
  5406. template <int mmq_y>
  5407. static __dpct_inline__ void
  5408. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5409. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  5410. (void)x_qh; (void)x_sc;
  5411. *x_ql = tile_x_ql_q5_0;
  5412. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  5413. }
  5414. template <int mmq_y, int nwarps, bool need_check>
  5415. static __dpct_inline__ void
  5416. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5417. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5418. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5419. const int &k, const int &blocks_per_row) {
  5420. (void)x_qh; (void)x_sc;
  5421. GGML_SYCL_ASSUME(i_offset >= 0);
  5422. GGML_SYCL_ASSUME(i_offset < nwarps);
  5423. GGML_SYCL_ASSUME(k >= 0);
  5424. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5425. const int kbx = k / QI5_0;
  5426. const int kqsx = k % QI5_0;
  5427. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5428. #pragma unroll
  5429. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5430. int i = i0 + i_offset;
  5431. if (need_check) {
  5432. i = sycl::min(i, i_max);
  5433. }
  5434. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5435. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5436. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5437. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5438. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5439. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5440. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5441. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5442. qs0 = dpct::vectorized_binary<sycl::char4>(
  5443. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5444. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5445. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5446. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5447. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5448. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5449. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5450. qs1 = dpct::vectorized_binary<sycl::char4>(
  5451. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  5452. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5453. }
  5454. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  5455. const int kbxd = k % blocks_per_tile_x_row;
  5456. float * x_dmf = (float *) x_dm;
  5457. #pragma unroll
  5458. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  5459. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  5460. if (need_check) {
  5461. i = sycl::min(i, i_max);
  5462. }
  5463. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5464. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  5465. }
  5466. }
  5467. static __dpct_inline__ float vec_dot_q5_0_q8_1_mul_mat(
  5468. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5469. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5470. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5471. const int &i, const int &j, const int &k) {
  5472. (void)x_qh; (void)x_sc;
  5473. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5474. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  5475. const float * x_dmf = (const float *) x_dm;
  5476. const float * y_df = (const float *) y_ds;
  5477. int u[2*VDR_Q5_0_Q8_1_MMQ];
  5478. #pragma unroll
  5479. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  5480. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5481. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  5482. }
  5483. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  5484. (&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)]);
  5485. }
  5486. static __dpct_inline__ float
  5487. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  5488. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5489. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  5490. int vl[VDR_Q5_1_Q8_1_MMVQ];
  5491. int vh[VDR_Q5_1_Q8_1_MMVQ];
  5492. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  5493. #pragma unroll
  5494. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  5495. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  5496. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  5497. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5498. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  5499. }
  5500. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  5501. }
  5502. template <int mmq_y>
  5503. static __dpct_inline__ void
  5504. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5505. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  5506. (void)x_qh; (void)x_sc;
  5507. *x_ql = tile_x_ql_q5_1;
  5508. *x_dm = tile_x_dm_q5_1;
  5509. }
  5510. template <int mmq_y, int nwarps, bool need_check>
  5511. static __dpct_inline__ void
  5512. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5513. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5514. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5515. const int &k, const int &blocks_per_row) {
  5516. (void)x_qh; (void)x_sc;
  5517. GGML_SYCL_ASSUME(i_offset >= 0);
  5518. GGML_SYCL_ASSUME(i_offset < nwarps);
  5519. GGML_SYCL_ASSUME(k >= 0);
  5520. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5521. const int kbx = k / QI5_1;
  5522. const int kqsx = k % QI5_1;
  5523. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  5524. #pragma unroll
  5525. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5526. int i = i0 + i_offset;
  5527. if (need_check) {
  5528. i = sycl::min(i, i_max);
  5529. }
  5530. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5531. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5532. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  5533. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5534. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5535. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5536. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5537. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5538. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5539. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5540. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5541. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5542. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5543. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5544. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5545. }
  5546. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  5547. const int kbxd = k % blocks_per_tile_x_row;
  5548. #pragma unroll
  5549. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  5550. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  5551. if (need_check) {
  5552. i = sycl::min(i, i_max);
  5553. }
  5554. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5555. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  5556. }
  5557. }
  5558. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  5559. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5560. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5561. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5562. const int &i, const int &j, const int &k) {
  5563. (void)x_qh; (void)x_sc;
  5564. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5565. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  5566. int u[2*VDR_Q5_1_Q8_1_MMQ];
  5567. #pragma unroll
  5568. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  5569. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5570. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  5571. }
  5572. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  5573. (&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)]);
  5574. }
  5575. static __dpct_inline__ float
  5576. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  5577. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5578. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  5579. int v[VDR_Q8_0_Q8_1_MMVQ];
  5580. int u[VDR_Q8_0_Q8_1_MMVQ];
  5581. #pragma unroll
  5582. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  5583. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  5584. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5585. }
  5586. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  5587. bq8_1->ds[0]);
  5588. }
  5589. template <int mmq_y>
  5590. static __dpct_inline__ void
  5591. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5592. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  5593. (void)x_qh; (void)x_sc;
  5594. *x_ql = tile_x_qs_q8_0;
  5595. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  5596. }
  5597. template <int mmq_y, int nwarps, bool need_check>
  5598. static __dpct_inline__ void
  5599. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5600. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5601. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5602. const int &k, const int &blocks_per_row) {
  5603. (void)x_qh; (void)x_sc;
  5604. GGML_SYCL_ASSUME(i_offset >= 0);
  5605. GGML_SYCL_ASSUME(i_offset < nwarps);
  5606. GGML_SYCL_ASSUME(k >= 0);
  5607. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5608. const int kbx = k / QI8_0;
  5609. const int kqsx = k % QI8_0;
  5610. float * x_dmf = (float *) x_dm;
  5611. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  5612. #pragma unroll
  5613. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5614. int i = i0 + i_offset;
  5615. if (need_check) {
  5616. i = sycl::min(i, i_max);
  5617. }
  5618. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5619. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  5620. }
  5621. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  5622. const int kbxd = k % blocks_per_tile_x_row;
  5623. #pragma unroll
  5624. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  5625. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  5626. if (need_check) {
  5627. i = sycl::min(i, i_max);
  5628. }
  5629. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5630. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  5631. }
  5632. }
  5633. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  5634. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5635. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5636. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5637. const int &i, const int &j, const int &k) {
  5638. (void)x_qh; (void)x_sc;
  5639. const float * x_dmf = (const float *) x_dm;
  5640. const float * y_df = (const float *) y_ds;
  5641. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  5642. (&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],
  5643. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  5644. }
  5645. static __dpct_inline__ float
  5646. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  5647. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5648. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  5649. const int bq8_offset = QR2_K * (iqs / QI8_1);
  5650. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5651. const uint8_t * scales = bq2_K->scales + scale_offset;
  5652. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  5653. int u[QR2_K];
  5654. float d8[QR2_K];
  5655. #pragma unroll
  5656. for (int i = 0; i < QR2_K; ++ i) {
  5657. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5658. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5659. }
  5660. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  5661. }
  5662. template <int mmq_y>
  5663. static __dpct_inline__ void
  5664. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5665. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  5666. int *tile_x_sc_q2_K) {
  5667. (void)x_qh;
  5668. *x_ql = tile_x_ql_q2_K;
  5669. *x_dm = tile_x_dm_q2_K;
  5670. *x_sc = tile_x_sc_q2_K;
  5671. }
  5672. template <int mmq_y, int nwarps, bool need_check>
  5673. static __dpct_inline__ void
  5674. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5675. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5676. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5677. const int &k, const int &blocks_per_row) {
  5678. (void)x_qh;
  5679. GGML_SYCL_ASSUME(i_offset >= 0);
  5680. GGML_SYCL_ASSUME(i_offset < nwarps);
  5681. GGML_SYCL_ASSUME(k >= 0);
  5682. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5683. const int kbx = k / QI2_K;
  5684. const int kqsx = k % QI2_K;
  5685. const block_q2_K * bx0 = (const block_q2_K *) vx;
  5686. #pragma unroll
  5687. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5688. int i = i0 + i_offset;
  5689. if (need_check) {
  5690. i = sycl::min(i, i_max);
  5691. }
  5692. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  5693. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5694. }
  5695. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  5696. const int kbxd = k % blocks_per_tile_x_row;
  5697. #pragma unroll
  5698. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  5699. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  5700. if (need_check) {
  5701. i = sycl::min(i, i_max);
  5702. }
  5703. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5704. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  5705. }
  5706. #pragma unroll
  5707. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5708. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5709. if (need_check) {
  5710. i = sycl::min(i, i_max);
  5711. }
  5712. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  5713. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  5714. }
  5715. }
  5716. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  5717. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5718. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5719. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5720. const int &i, const int &j, const int &k) {
  5721. (void)x_qh;
  5722. const int kbx = k / QI2_K;
  5723. const int ky = (k % QI2_K) * QR2_K;
  5724. const float * y_df = (const float *) y_ds;
  5725. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  5726. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  5727. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  5728. #pragma unroll
  5729. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  5730. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5731. }
  5732. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  5733. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  5734. 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]);
  5735. }
  5736. static __dpct_inline__ float
  5737. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  5738. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5739. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  5740. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  5741. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5742. const float d = bq3_K->d;
  5743. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  5744. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5745. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  5746. int u[QR3_K];
  5747. float d8[QR3_K];
  5748. #pragma unroll
  5749. for (int i = 0; i < QR3_K; ++i) {
  5750. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5751. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5752. }
  5753. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  5754. }
  5755. template <int mmq_y>
  5756. static __dpct_inline__ void
  5757. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5758. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  5759. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  5760. *x_ql = tile_x_ql_q3_K;
  5761. *x_dm = tile_x_dm_q3_K;
  5762. *x_qh = tile_x_qh_q3_K;
  5763. *x_sc = tile_x_sc_q3_K;
  5764. }
  5765. template <int mmq_y, int nwarps, bool need_check>
  5766. static __dpct_inline__ void
  5767. load_tiles_q3_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5768. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5769. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5770. const int &k, const int &blocks_per_row) {
  5771. GGML_SYCL_ASSUME(i_offset >= 0);
  5772. GGML_SYCL_ASSUME(i_offset < nwarps);
  5773. GGML_SYCL_ASSUME(k >= 0);
  5774. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5775. const int kbx = k / QI3_K;
  5776. const int kqsx = k % QI3_K;
  5777. const block_q3_K * bx0 = (const block_q3_K *) vx;
  5778. #pragma unroll
  5779. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5780. int i = i0 + i_offset;
  5781. if (need_check) {
  5782. i = sycl::min(i, i_max);
  5783. }
  5784. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  5785. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5786. }
  5787. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  5788. const int kbxd = k % blocks_per_tile_x_row;
  5789. float * x_dmf = (float *) x_dm;
  5790. #pragma unroll
  5791. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  5792. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  5793. if (need_check) {
  5794. i = sycl::min(i, i_max);
  5795. }
  5796. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5797. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  5798. }
  5799. #pragma unroll
  5800. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  5801. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  5802. if (need_check) {
  5803. i = sycl::min(i, i_max);
  5804. }
  5805. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  5806. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5807. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  5808. }
  5809. #pragma unroll
  5810. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5811. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5812. if (need_check) {
  5813. i = sycl::min(i, i_max);
  5814. }
  5815. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  5816. const int ksc = k % (QI3_K/4);
  5817. const int ksc_low = ksc % (QI3_K/8);
  5818. const int shift_low = 4 * (ksc / (QI3_K/8));
  5819. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  5820. const int ksc_high = QI3_K/8;
  5821. const int shift_high = 2 * ksc;
  5822. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  5823. const int sc = dpct::vectorized_binary<sycl::char4>(
  5824. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  5825. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  5826. }
  5827. }
  5828. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  5829. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5830. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5831. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5832. const int &i, const int &j, const int &k) {
  5833. const int kbx = k / QI3_K;
  5834. const int ky = (k % QI3_K) * QR3_K;
  5835. const float * x_dmf = (const float *) x_dm;
  5836. const float * y_df = (const float *) y_ds;
  5837. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  5838. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  5839. #pragma unroll
  5840. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  5841. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  5842. const int shift = 2 * ((ky % 32) / 8);
  5843. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5844. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  5845. const int vlh = (vh << 2) & 0x04040404;
  5846. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  5847. }
  5848. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  5849. 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]);
  5850. }
  5851. static __dpct_inline__ float
  5852. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  5853. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5854. #ifndef GGML_QKK_64
  5855. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5856. int v[2];
  5857. int u[2*QR4_K];
  5858. float d8[QR4_K];
  5859. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  5860. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  5861. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  5862. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  5863. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  5864. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  5865. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5866. v[0] = q4[0];
  5867. v[1] = q4[4];
  5868. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  5869. uint16_t aux[2];
  5870. const int j = bq8_offset/2;
  5871. if (j < 2) {
  5872. aux[0] = scales[j+0] & 0x3f3f;
  5873. aux[1] = scales[j+2] & 0x3f3f;
  5874. } else {
  5875. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5876. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5877. }
  5878. const uint8_t * sc = (const uint8_t *)aux;
  5879. const uint8_t * m = sc + 2;
  5880. for (int i = 0; i < QR4_K; ++i) {
  5881. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5882. d8[i] = bq8i->ds[0];
  5883. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5884. u[2*i+0] = q8[0];
  5885. u[2*i+1] = q8[4];
  5886. }
  5887. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  5888. #else
  5889. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5890. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5891. float sumf_d = 0.0f;
  5892. float sumf_m = 0.0f;
  5893. uint16_t aux16[2];
  5894. const uint8_t * s = (const uint8_t *)aux16;
  5895. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  5896. aux16[0] = a[0] & 0x0f0f;
  5897. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5898. const float dall = bq4_K->dm[0];
  5899. const float dmin = bq4_K->dm[1];
  5900. const float d8_1 = bq8_1[0].ds[0];
  5901. const float d8_2 = bq8_1[1].ds[1];
  5902. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5903. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5904. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5905. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5906. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  5907. const int v1 = q4[0];
  5908. const int v2 = q4[4];
  5909. const int dot1 = dpct::dp4a(ui2, v2 & 0x0f0f0f0f, dpct::dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  5910. const int dot2 = dpct::dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, dpct::dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  5911. const int dot3 = dpct::dp4a(0x01010101, ui2, dpct::dp4a(0x01010101, ui1, 0));
  5912. const int dot4 = dpct::dp4a(0x01010101, ui4, dpct::dp4a(0x01010101, ui3, 0));
  5913. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  5914. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  5915. return dall * sumf_d - dmin * sumf_m;
  5916. #else
  5917. bad_arch();
  5918. #endif // __SYCL_ARCH__ >= VER_4VEC
  5919. #endif
  5920. }
  5921. template <int mmq_y>
  5922. static __dpct_inline__ void
  5923. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5924. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  5925. int *tile_x_sc_q4_K) {
  5926. (void)x_qh;
  5927. *x_ql = tile_x_ql_q4_K;
  5928. *x_dm = tile_x_dm_q4_K;
  5929. *x_sc = tile_x_sc_q4_K;
  5930. }
  5931. template <int mmq_y, int nwarps, bool need_check>
  5932. static __dpct_inline__ void
  5933. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5934. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5935. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5936. const int &k, const int &blocks_per_row) {
  5937. (void)x_qh;
  5938. GGML_SYCL_ASSUME(i_offset >= 0);
  5939. GGML_SYCL_ASSUME(i_offset < nwarps);
  5940. GGML_SYCL_ASSUME(k >= 0);
  5941. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5942. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  5943. const int kqsx = k % QI4_K; // == k if QK_K == 256
  5944. const block_q4_K * bx0 = (const block_q4_K *) vx;
  5945. #pragma unroll
  5946. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5947. int i = i0 + i_offset;
  5948. if (need_check) {
  5949. i = sycl::min(i, i_max);
  5950. }
  5951. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  5952. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5953. }
  5954. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  5955. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5956. #pragma unroll
  5957. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  5958. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  5959. if (need_check) {
  5960. i = sycl::min(i, i_max);
  5961. }
  5962. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5963. #if QK_K == 256
  5964. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  5965. #else
  5966. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  5967. #endif
  5968. }
  5969. #pragma unroll
  5970. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5971. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5972. if (need_check) {
  5973. i = sycl::min(i, i_max);
  5974. }
  5975. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  5976. const int * scales = (const int *) bxi->scales;
  5977. const int ksc = k % (WARP_SIZE/8);
  5978. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5979. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5980. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5981. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5982. }
  5983. }
  5984. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  5985. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5986. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5987. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5988. const int &i, const int &j, const int &k) {
  5989. (void)x_qh;
  5990. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  5991. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  5992. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  5993. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  5994. }
  5995. static __dpct_inline__ float
  5996. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  5997. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5998. #ifndef GGML_QKK_64
  5999. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  6000. int vl[2];
  6001. int vh[2];
  6002. int u[2*QR5_K];
  6003. float d8[QR5_K];
  6004. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  6005. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  6006. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  6007. vl[0] = ql[0];
  6008. vl[1] = ql[4];
  6009. vh[0] = qh[0] >> bq8_offset;
  6010. vh[1] = qh[4] >> bq8_offset;
  6011. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  6012. uint16_t aux[2];
  6013. const int j = bq8_offset/2;
  6014. if (j < 2) {
  6015. aux[0] = scales[j+0] & 0x3f3f;
  6016. aux[1] = scales[j+2] & 0x3f3f;
  6017. } else {
  6018. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  6019. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  6020. }
  6021. const uint8_t * sc = (const uint8_t *)aux;
  6022. const uint8_t * m = sc + 2;
  6023. #pragma unroll
  6024. for (int i = 0; i < QR5_K; ++i) {
  6025. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  6026. d8[i] = bq8i->ds[0];
  6027. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  6028. u[2*i+0] = q8[0];
  6029. u[2*i+1] = q8[4];
  6030. }
  6031. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  6032. #else
  6033. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  6034. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  6035. const int8_t * s = bq5_K->scales;
  6036. const float d = bq5_K->d;
  6037. const float d8_1 = bq8_1[0].ds[0];
  6038. const float d8_2 = bq8_1[1].ds[1];
  6039. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  6040. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  6041. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  6042. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  6043. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  6044. const int vl1 = ql[0];
  6045. const int vl2 = ql[4];
  6046. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  6047. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  6048. const int in = step%8; // 0, 4, 0, 4
  6049. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  6050. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  6051. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  6052. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  6053. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  6054. const float sumf_d = d8_1 * (dpct::dp4a(ui1, v1, 0) * s[0] + dpct::dp4a(ui2, v2, 0) * s[1])
  6055. + d8_2 * (dpct::dp4a(ui3, v3, 0) * s[2] + dpct::dp4a(ui4, v4, 0) * s[3]);
  6056. return d * sumf_d;
  6057. #else
  6058. bad_arch();
  6059. #endif // __SYCL_ARCH__ >= VER_4VEC
  6060. #endif
  6061. }
  6062. template <int mmq_y>
  6063. static __dpct_inline__ void
  6064. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6065. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  6066. int *tile_x_sc_q5_K) {
  6067. (void)x_qh;
  6068. *x_ql = tile_x_ql_q5_K;
  6069. *x_dm = tile_x_dm_q5_K;
  6070. *x_sc = tile_x_sc_q5_K;
  6071. }
  6072. template <int mmq_y, int nwarps, bool need_check>
  6073. static __dpct_inline__ void
  6074. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6075. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6076. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6077. const int &k, const int &blocks_per_row) {
  6078. (void)x_qh;
  6079. GGML_SYCL_ASSUME(i_offset >= 0);
  6080. GGML_SYCL_ASSUME(i_offset < nwarps);
  6081. GGML_SYCL_ASSUME(k >= 0);
  6082. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6083. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  6084. const int kqsx = k % QI5_K; // == k if QK_K == 256
  6085. const block_q5_K * bx0 = (const block_q5_K *) vx;
  6086. #pragma unroll
  6087. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6088. int i = i0 + i_offset;
  6089. if (need_check) {
  6090. i = sycl::min(i, i_max);
  6091. }
  6092. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  6093. const int ky = QR5_K*kqsx;
  6094. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  6095. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  6096. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  6097. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  6098. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  6099. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  6100. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  6101. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  6102. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  6103. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  6104. }
  6105. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  6106. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6107. #pragma unroll
  6108. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  6109. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  6110. if (need_check) {
  6111. i = sycl::min(i, i_max);
  6112. }
  6113. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6114. #if QK_K == 256
  6115. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  6116. #endif
  6117. }
  6118. #pragma unroll
  6119. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6120. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6121. if (need_check) {
  6122. i = sycl::min(i, i_max);
  6123. }
  6124. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  6125. const int * scales = (const int *) bxi->scales;
  6126. const int ksc = k % (WARP_SIZE/8);
  6127. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  6128. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  6129. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  6130. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  6131. }
  6132. }
  6133. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  6134. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6135. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6136. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6137. const int &i, const int &j, const int &k) {
  6138. (void)x_qh;
  6139. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  6140. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  6141. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  6142. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  6143. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  6144. }
  6145. static __dpct_inline__ float
  6146. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  6147. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6148. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  6149. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  6150. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  6151. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  6152. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  6153. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  6154. const int8_t * scales = bq6_K->scales + scale_offset;
  6155. int u[QR6_K];
  6156. float d8[QR6_K];
  6157. #pragma unroll
  6158. for (int i = 0; i < QR6_K; ++i) {
  6159. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  6160. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  6161. }
  6162. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  6163. }
  6164. template <int mmq_y>
  6165. static __dpct_inline__ void
  6166. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6167. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  6168. (void)x_qh;
  6169. *x_ql = tile_x_ql;
  6170. *x_dm = tile_x_dm;
  6171. *x_sc = tile_x_sc;
  6172. }
  6173. template <int mmq_y, int nwarps, bool need_check>
  6174. static __dpct_inline__ void
  6175. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6176. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6177. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6178. const int &k, const int &blocks_per_row) {
  6179. (void)x_qh;
  6180. GGML_SYCL_ASSUME(i_offset >= 0);
  6181. GGML_SYCL_ASSUME(i_offset < nwarps);
  6182. GGML_SYCL_ASSUME(k >= 0);
  6183. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6184. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  6185. const int kqsx = k % QI6_K; // == k if QK_K == 256
  6186. const block_q6_K * bx0 = (const block_q6_K *) vx;
  6187. #pragma unroll
  6188. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6189. int i = i0 + i_offset;
  6190. if (need_check) {
  6191. i = sycl::min(i, i_max);
  6192. }
  6193. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  6194. const int ky = QR6_K*kqsx;
  6195. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  6196. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  6197. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  6198. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  6199. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  6200. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  6201. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  6202. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  6203. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  6204. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  6205. dpct::sub_sat());
  6206. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  6207. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  6208. dpct::sub_sat());
  6209. }
  6210. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  6211. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6212. float * x_dmf = (float *) x_dm;
  6213. #pragma unroll
  6214. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  6215. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  6216. if (need_check) {
  6217. i = sycl::min(i, i_max);
  6218. }
  6219. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6220. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  6221. }
  6222. #pragma unroll
  6223. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6224. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6225. if (need_check) {
  6226. i = sycl::min(i, i_max);
  6227. }
  6228. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  6229. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  6230. }
  6231. }
  6232. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  6233. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6234. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6235. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6236. const int &i, const int &j, const int &k) {
  6237. (void)x_qh;
  6238. const float * x_dmf = (const float *) x_dm;
  6239. const float * y_df = (const float *) y_ds;
  6240. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  6241. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  6242. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  6243. 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]);
  6244. }
  6245. static __dpct_inline__ float
  6246. vec_dot_iq2_xxs_q8_1(const void *__restrict__ vbq,
  6247. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6248. const uint64_t *iq2xxs_grid, const uint8_t *ksigns_iq2xs,
  6249. const uint8_t *kmask_iq2xs) {
  6250. #if QK_K == 256
  6251. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  6252. #if QR2_XXS == 8
  6253. const int ib32 = iqs;
  6254. const uint16_t * q2 = bq2->qs + 4*ib32;
  6255. const uint8_t * aux8 = (const uint8_t *)q2;
  6256. const int8_t * q8 = bq8_1[ib32].qs;
  6257. uint32_t aux32 = q2[2] | (q2[3] << 16);
  6258. int sumi = 0;
  6259. for (int l = 0; l < 4; ++l) {
  6260. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6261. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  6262. for (int j = 0; j < 8; ++j) {
  6263. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6264. }
  6265. q8 += 8;
  6266. aux32 >>= 7;
  6267. }
  6268. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.25f;
  6269. return d * sumi;
  6270. #else
  6271. // iqs is 0...15
  6272. const int ib32 = iqs/2;
  6273. const int il = iqs%2;
  6274. const uint16_t * q2 = bq2->qs + 4*ib32;
  6275. const uint8_t * aux8 = (const uint8_t *)q2;
  6276. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  6277. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  6278. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  6279. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * bq8_1[ib32].ds[0] * 0.25f;
  6280. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  6281. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  6282. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  6283. int sumi1 = 0, sumi2 = 0;
  6284. for (int j = 0; j < 8; ++j) {
  6285. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  6286. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  6287. }
  6288. return d * (sumi1 + sumi2);
  6289. #endif
  6290. #else
  6291. assert(false);
  6292. return 0.f;
  6293. #endif
  6294. }
  6295. static __dpct_inline__ float
  6296. vec_dot_iq2_xs_q8_1(const void *__restrict__ vbq,
  6297. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6298. const uint64_t *iq2xs_grid, const uint64_t *ksigns64) {
  6299. #if DPCT_COMPATIBILITY_TEMP >= \
  6300. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6301. #if QK_K == 256
  6302. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  6303. const int ib32 = iqs;
  6304. const uint16_t * q2 = bq2->qs + 4*ib32;
  6305. const int8_t * q8 = bq8_1[ib32].qs;
  6306. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6307. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6308. int sumi1 = 0;
  6309. for (int l = 0; l < 2; ++l) {
  6310. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6311. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6312. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6313. grid[0] ^ signs[0], signs[0], std::minus<>());
  6314. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6315. grid[1] ^ signs[1], signs[1], std::minus<>());
  6316. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6317. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6318. q8 += 8;
  6319. }
  6320. int sumi2 = 0;
  6321. for (int l = 2; l < 4; ++l) {
  6322. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6323. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6324. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6325. grid[0] ^ signs[0], signs[0], std::minus<>());
  6326. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6327. grid[1] ^ signs[1], signs[1], std::minus<>());
  6328. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6329. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6330. q8 += 8;
  6331. }
  6332. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6333. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6334. #else
  6335. assert(false);
  6336. return 0.f;
  6337. #endif
  6338. #else
  6339. assert(false);
  6340. return 0.f;
  6341. #endif
  6342. }
  6343. static __dpct_inline__ float
  6344. vec_dot_iq2_s_q8_1(const void *__restrict__ vbq,
  6345. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6346. #if QK_K == 256
  6347. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  6348. const int ib32 = iqs;
  6349. const int8_t * q8 = bq8_1[ib32].qs;
  6350. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  6351. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6352. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6353. int sumi1 = 0;
  6354. for (int l = 0; l < 2; ++l) {
  6355. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  6356. const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6357. ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201,
  6358. std::equal_to<>());
  6359. const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6360. ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201,
  6361. std::equal_to<>());
  6362. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6363. grid[0] ^ signs0, signs0, std::minus<>());
  6364. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6365. grid[1] ^ signs1, signs1, std::minus<>());
  6366. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6367. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6368. q8 += 8;
  6369. }
  6370. int sumi2 = 0;
  6371. for (int l = 2; l < 4; ++l) {
  6372. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  6373. const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6374. ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201,
  6375. std::equal_to<>());
  6376. const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6377. ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201,
  6378. std::equal_to<>());
  6379. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6380. grid[0] ^ signs0, signs0, std::minus<>());
  6381. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6382. grid[1] ^ signs1, signs1, std::minus<>());
  6383. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6384. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6385. q8 += 8;
  6386. }
  6387. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6388. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6389. #else
  6390. assert(false);
  6391. #endif
  6392. }
  6393. static __dpct_inline__ float
  6394. vec_dot_iq3_xxs_q8_1(const void *__restrict__ vbq,
  6395. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6396. const uint32_t *iq3xxs_grid, const uint64_t *ksigns64) {
  6397. #if DPCT_COMPATIBILITY_TEMP >= \
  6398. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6399. #if QK_K == 256
  6400. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  6401. const int ib32 = iqs;
  6402. const uint8_t * q3 = bq2->qs + 8*ib32;
  6403. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  6404. const int8_t * q8 = bq8_1[ib32].qs;
  6405. uint32_t aux32 = gas[0] | (gas[1] << 16);
  6406. int sumi = 0;
  6407. for (int l = 0; l < 4; ++l) {
  6408. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  6409. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  6410. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  6411. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6412. grid1[0] ^ signs[0], signs[0], std::minus<>());
  6413. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6414. grid2[0] ^ signs[1], signs[1], std::minus<>());
  6415. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6416. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6417. q8 += 8;
  6418. aux32 >>= 7;
  6419. }
  6420. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.5f;
  6421. return d * sumi;
  6422. #else
  6423. assert(false);
  6424. return 0.f;
  6425. #endif
  6426. #else
  6427. assert(false);
  6428. return 0.f;
  6429. #endif
  6430. }
  6431. static __dpct_inline__ float
  6432. vec_dot_iq3_s_q8_1(const void *__restrict__ vbq,
  6433. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6434. const uint32_t *iq3s_grid) {
  6435. #if QK_K == 256
  6436. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  6437. const int ib32 = iqs;
  6438. const uint8_t * qs = bq2->qs + 8*ib32;
  6439. const int8_t * q8 = bq8_1[ib32].qs;
  6440. int sumi = 0;
  6441. for (int l = 0; l < 4; ++l) {
  6442. const uint32_t * grid1 = iq3s_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  6443. const uint32_t * grid2 = iq3s_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  6444. uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6445. ((bq2->signs[4 * ib32 + l] & 0xf) * 0x01010101) & 0x08040201,
  6446. 0x08040201, std::equal_to<>());
  6447. uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6448. ((bq2->signs[4 * ib32 + l] >> 4) * 0x01010101) & 0x08040201,
  6449. 0x08040201, std::equal_to<>());
  6450. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6451. grid1[0] ^ signs0, signs0, std::minus<>());
  6452. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6453. grid2[0] ^ signs1, signs1, std::minus<>());
  6454. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6455. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6456. q8 += 8;
  6457. }
  6458. const float d =
  6459. (float)bq2->d *
  6460. (1 + 2 * ((bq2->scales[ib32 / 2] >> 4 * (ib32 % 2)) & 0xf)) *
  6461. bq8_1[ib32].ds[0];
  6462. return d * sumi;
  6463. #else
  6464. assert(false);
  6465. #endif
  6466. }
  6467. static __dpct_inline__ float
  6468. vec_dot_iq1_s_q8_1(const void *__restrict__ vbq,
  6469. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6470. const uint32_t *iq1s_grid_gpu) {
  6471. #if QK_K == 256
  6472. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  6473. const int ib32 = iqs;
  6474. int sumi = 0;
  6475. const int * q8 = (const int *)bq8_1[ib32].qs;
  6476. for (int l = 0; l < 4; ++l) {
  6477. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  6478. int grid0 = grid[0] & 0x0f0f0f0f;
  6479. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  6480. sumi = dpct::dp4a(q8[2 * l + 1], grid1,
  6481. dpct::dp4a(q8[2 * l + 0], grid0, sumi));
  6482. }
  6483. const float delta = bq1->qh[ib32] & 0x8000 ? -1-IQ1S_DELTA : -1+IQ1S_DELTA;
  6484. const float d1q = (float)bq1->d * (2*((bq1->qh[ib32] >> 12) & 7) + 1);
  6485. const float d = d1q * bq8_1[ib32].ds[0];
  6486. const float m = d1q * bq8_1[ib32].ds[1];
  6487. return d * sumi + m * delta;
  6488. #else
  6489. assert(false);
  6490. #endif
  6491. }
  6492. static __dpct_inline__ float
  6493. vec_dot_iq1_m_q8_1(const void *__restrict__ vbq,
  6494. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6495. #if QK_K == 256
  6496. const block_iq1_m * bq1 = (const block_iq1_m *) vbq;
  6497. const int ib32 = iqs;
  6498. int sumi[2] = {0, 0};
  6499. float sumf[2] = {0.f, 0.f};
  6500. const int * q8 = (const int *)bq8_1[ib32].qs;
  6501. for (int l = 0; l < 4; ++l) {
  6502. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 7) << 8)));
  6503. int grid0 = grid[0] & 0x0f0f0f0f;
  6504. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  6505. sumi[l / 2] = dpct::dp4a(q8[2 * l + 1], grid1,
  6506. dpct::dp4a(q8[2 * l + 0], grid0, sumi[l / 2]));
  6507. const float delta = (bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 0x08 ? -1-IQ1M_DELTA : -1+IQ1M_DELTA;
  6508. const int sumy = dpct::dp4a(q8[2 * l + 1], 0x01010101,
  6509. dpct::dp4a(q8[2 * l + 0], 0x01010101, 0));
  6510. sumf[l/2] += delta*sumy;
  6511. }
  6512. iq1m_scale_t scale;
  6513. const uint16_t * sc = (const uint16_t *)bq1->scales;
  6514. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  6515. const float d = (float)scale.f16 * bq8_1[ib32].ds[0];
  6516. 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));
  6517. #else
  6518. assert(false);
  6519. #endif
  6520. }
  6521. static __dpct_inline__ void get_int_from_table_16(const uint32_t &q4,
  6522. const uint8_t *values,
  6523. int &val1, int &val2) {
  6524. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  6525. aux32 = q4 & 0x0f0f0f0f;
  6526. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  6527. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  6528. val1 = v1 | (v2 << 16);
  6529. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  6530. v1 = values[q8[0]] | (values[q8[1]] << 8);
  6531. v2 = values[q8[2]] | (values[q8[3]] << 8);
  6532. val2 = v1 | (v2 << 16);
  6533. }
  6534. static __dpct_inline__ float
  6535. vec_dot_iq4_nl_q8_1(const void *__restrict__ vbq,
  6536. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6537. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  6538. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  6539. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  6540. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  6541. int v1, v2;
  6542. int sumi1 = 0, sumi2 = 0;
  6543. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  6544. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  6545. get_int_from_table_16(aux, values, v1, v2);
  6546. sumi1 = dpct::dp4a(v1, q8[l + 0], sumi1);
  6547. sumi2 = dpct::dp4a(v2, q8[l + 4], sumi2);
  6548. }
  6549. const float d = (float)bq->d * bq8_1->ds[0];
  6550. return d * (sumi1 + sumi2);
  6551. }
  6552. static __dpct_inline__ float
  6553. vec_dot_iq4_xs_q8_1(const void *__restrict__ vbq,
  6554. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6555. #if QK_K == 256
  6556. const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq;
  6557. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  6558. // iqs is 0...7
  6559. const int ib32 = iqs;
  6560. const int32_t * q8 = (const int *)bq8_1[ib32].qs;
  6561. const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32;
  6562. const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  6563. const float d = (float)bq4->d * (ls - 32) * bq8_1[ib32].ds[0];
  6564. int v1, v2;
  6565. int sumi1 = 0, sumi2 = 0;
  6566. for (int j = 0; j < 4; ++j) {
  6567. get_int_from_table_16(q4[j], values, v1, v2);
  6568. sumi1 = dpct::dp4a(v1, q8[j + 0], sumi1);
  6569. sumi2 = dpct::dp4a(v2, q8[j + 4], sumi2);
  6570. }
  6571. return d * (sumi1 + sumi2);
  6572. #else
  6573. assert(false);
  6574. #endif
  6575. }
  6576. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  6577. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  6578. vec_dot_q_mul_mat_sycl_t vec_dot>
  6579. /*
  6580. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  6581. exceeds 128 bytes and may cause high register pressure. Consult with your
  6582. hardware vendor to find the total register size available and adjust the code,
  6583. or use smaller sub-group size to avoid high register pressure.
  6584. */
  6585. static __dpct_inline__ void
  6586. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  6587. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  6588. const int ncols_y, const int nrows_y, const int nrows_dst,
  6589. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  6590. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  6591. sycl::half2 *tile_y_ds) {
  6592. const block_q_t * x = (const block_q_t *) vx;
  6593. const block_q8_1 * y = (const block_q8_1 *) vy;
  6594. const int blocks_per_row_x = ncols_x / qk;
  6595. const int blocks_per_col_y = nrows_y / QK8_1;
  6596. const int blocks_per_warp = WARP_SIZE / qi;
  6597. const int & ncols_dst = ncols_y;
  6598. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  6599. const int & row_x_0 = row_dst_0;
  6600. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  6601. const int & col_y_0 = col_dst_0;
  6602. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  6603. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  6604. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  6605. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  6606. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  6607. blocks_per_row_x);
  6608. #pragma unroll
  6609. for (int ir = 0; ir < qr; ++ir) {
  6610. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  6611. const int kbxd = kqs / QI8_1;
  6612. #pragma unroll
  6613. for (int i = 0; i < mmq_x; i += nwarps) {
  6614. const int col_y_eff = dpct::min(
  6615. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  6616. ncols_y - 1); // to prevent out-of-bounds memory accesses
  6617. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  6618. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  6619. kqs % WARP_SIZE;
  6620. tile_y_qs[index_y] = get_int_from_int8_aligned(
  6621. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  6622. }
  6623. #pragma unroll
  6624. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  6625. const int ids =
  6626. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  6627. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  6628. mmq_x;
  6629. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  6630. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  6631. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  6632. const sycl::half2 *dsi_src =
  6633. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  6634. ir * (WARP_SIZE / QI8_1) + kby]
  6635. .ds;
  6636. sycl::half2 *dsi_dst =
  6637. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  6638. if (need_sum) {
  6639. *dsi_dst = *dsi_src;
  6640. } else {
  6641. float * dfi_dst = (float *) dsi_dst;
  6642. *dfi_dst = (*dsi_src)[0];
  6643. }
  6644. }
  6645. /*
  6646. DPCT1118:9: SYCL group functions and algorithms must be encountered
  6647. in converged control flow. You may need to adjust the code.
  6648. */
  6649. /*
  6650. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  6651. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6652. better performance if there is no access to global memory.
  6653. */
  6654. item_ct1.barrier();
  6655. // #pragma unroll // unrolling this loop causes too much register pressure
  6656. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  6657. #pragma unroll
  6658. for (int j = 0; j < mmq_x; j += nwarps) {
  6659. #pragma unroll
  6660. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6661. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  6662. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  6663. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  6664. item_ct1.get_local_id(1) + j, k);
  6665. }
  6666. }
  6667. }
  6668. /*
  6669. DPCT1118:10: SYCL group functions and algorithms must be encountered
  6670. in converged control flow. You may need to adjust the code.
  6671. */
  6672. /*
  6673. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  6674. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6675. better performance if there is no access to global memory.
  6676. */
  6677. item_ct1.barrier();
  6678. }
  6679. }
  6680. #pragma unroll
  6681. for (int j = 0; j < mmq_x; j += nwarps) {
  6682. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  6683. if (col_dst >= ncols_dst) {
  6684. return;
  6685. }
  6686. #pragma unroll
  6687. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6688. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  6689. if (row_dst >= nrows_dst) {
  6690. continue;
  6691. }
  6692. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  6693. }
  6694. }
  6695. }
  6696. #define MMQ_X_Q4_0_RDNA2 64
  6697. #define MMQ_Y_Q4_0_RDNA2 128
  6698. #define NWARPS_Q4_0_RDNA2 8
  6699. #define MMQ_X_Q4_0_RDNA1 64
  6700. #define MMQ_Y_Q4_0_RDNA1 64
  6701. #define NWARPS_Q4_0_RDNA1 8
  6702. #if defined(SYCL_USE_XMX)
  6703. #define MMQ_X_Q4_0_AMPERE 4
  6704. #define MMQ_Y_Q4_0_AMPERE 32
  6705. #define NWARPS_Q4_0_AMPERE 4
  6706. #else
  6707. #define MMQ_X_Q4_0_AMPERE 64
  6708. #define MMQ_Y_Q4_0_AMPERE 128
  6709. #define NWARPS_Q4_0_AMPERE 4
  6710. #endif
  6711. #define MMQ_X_Q4_0_PASCAL 64
  6712. #define MMQ_Y_Q4_0_PASCAL 64
  6713. #define NWARPS_Q4_0_PASCAL 8
  6714. template <bool need_check> static void
  6715. mul_mat_q4_0(
  6716. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6717. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6718. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  6719. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6720. int * tile_x_ql = nullptr;
  6721. sycl::half2 *tile_x_dm = nullptr;
  6722. int * tile_x_qh = nullptr;
  6723. int * tile_x_sc = nullptr;
  6724. //sycl_todo: change according to hardware
  6725. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  6726. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  6727. const int nwarps = NWARPS_Q4_0_AMPERE;
  6728. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6729. tile_x_qs_q4_0, tile_x_d_q4_0);
  6730. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  6731. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  6732. vec_dot_q4_0_q8_1_mul_mat>(
  6733. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6734. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6735. }
  6736. #define MMQ_X_Q4_1_RDNA2 64
  6737. #define MMQ_Y_Q4_1_RDNA2 128
  6738. #define NWARPS_Q4_1_RDNA2 8
  6739. #define MMQ_X_Q4_1_RDNA1 64
  6740. #define MMQ_Y_Q4_1_RDNA1 64
  6741. #define NWARPS_Q4_1_RDNA1 8
  6742. #if defined(SYCL_USE_XMX)
  6743. #define MMQ_X_Q4_1_AMPERE 4
  6744. #define MMQ_Y_Q4_1_AMPERE 32
  6745. #define NWARPS_Q4_1_AMPERE 4
  6746. #else
  6747. #define MMQ_X_Q4_1_AMPERE 64
  6748. #define MMQ_Y_Q4_1_AMPERE 128
  6749. #define NWARPS_Q4_1_AMPERE 4
  6750. #endif
  6751. #define MMQ_X_Q4_1_PASCAL 64
  6752. #define MMQ_Y_Q4_1_PASCAL 64
  6753. #define NWARPS_Q4_1_PASCAL 8
  6754. template <bool need_check> static void
  6755. mul_mat_q4_1(
  6756. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6757. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6758. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  6759. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6760. int * tile_x_ql = nullptr;
  6761. sycl::half2 *tile_x_dm = nullptr;
  6762. int * tile_x_qh = nullptr;
  6763. int * tile_x_sc = nullptr;
  6764. //sycl_todo: change according to hardware
  6765. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  6766. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  6767. const int nwarps = NWARPS_Q4_1_AMPERE;
  6768. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6769. tile_x_qs_q4_1, tile_x_dm_q4_1);
  6770. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  6771. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  6772. vec_dot_q4_1_q8_1_mul_mat>(
  6773. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6774. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6775. }
  6776. #define MMQ_X_Q5_0_RDNA2 64
  6777. #define MMQ_Y_Q5_0_RDNA2 128
  6778. #define NWARPS_Q5_0_RDNA2 8
  6779. #define MMQ_X_Q5_0_RDNA1 64
  6780. #define MMQ_Y_Q5_0_RDNA1 64
  6781. #define NWARPS_Q5_0_RDNA1 8
  6782. #if defined(SYCL_USE_XMX)
  6783. #define MMQ_X_Q5_0_AMPERE 4
  6784. #define MMQ_Y_Q5_0_AMPERE 32
  6785. #define NWARPS_Q5_0_AMPERE 4
  6786. #else
  6787. #define MMQ_X_Q5_0_AMPERE 128
  6788. #define MMQ_Y_Q5_0_AMPERE 64
  6789. #define NWARPS_Q5_0_AMPERE 4
  6790. #endif
  6791. #define MMQ_X_Q5_0_PASCAL 64
  6792. #define MMQ_Y_Q5_0_PASCAL 64
  6793. #define NWARPS_Q5_0_PASCAL 8
  6794. template <bool need_check> static void
  6795. mul_mat_q5_0(
  6796. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6797. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6798. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  6799. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6800. int * tile_x_ql = nullptr;
  6801. sycl::half2 *tile_x_dm = nullptr;
  6802. int * tile_x_qh = nullptr;
  6803. int * tile_x_sc = nullptr;
  6804. //sycl_todo: change according to hardware
  6805. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  6806. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  6807. const int nwarps = NWARPS_Q5_0_AMPERE;
  6808. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6809. tile_x_ql_q5_0, tile_x_d_q5_0);
  6810. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  6811. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  6812. vec_dot_q5_0_q8_1_mul_mat>(
  6813. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6814. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6815. }
  6816. #define MMQ_X_Q5_1_RDNA2 64
  6817. #define MMQ_Y_Q5_1_RDNA2 128
  6818. #define NWARPS_Q5_1_RDNA2 8
  6819. #define MMQ_X_Q5_1_RDNA1 64
  6820. #define MMQ_Y_Q5_1_RDNA1 64
  6821. #define NWARPS_Q5_1_RDNA1 8
  6822. #if defined(SYCL_USE_XMX)
  6823. #define MMQ_X_Q5_1_AMPERE 4
  6824. #define MMQ_Y_Q5_1_AMPERE 32
  6825. #define NWARPS_Q5_1_AMPERE 4
  6826. #else
  6827. #define MMQ_X_Q5_1_AMPERE 128
  6828. #define MMQ_Y_Q5_1_AMPERE 64
  6829. #define NWARPS_Q5_1_AMPERE 4
  6830. #endif
  6831. #define MMQ_X_Q5_1_PASCAL 64
  6832. #define MMQ_Y_Q5_1_PASCAL 64
  6833. #define NWARPS_Q5_1_PASCAL 8
  6834. template <bool need_check> static void
  6835. mul_mat_q5_1(
  6836. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6837. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6838. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  6839. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6840. int * tile_x_ql = nullptr;
  6841. sycl::half2 *tile_x_dm = nullptr;
  6842. int * tile_x_qh = nullptr;
  6843. int * tile_x_sc = nullptr;
  6844. //sycl_todo: change according to hardware
  6845. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  6846. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  6847. const int nwarps = NWARPS_Q5_1_AMPERE;
  6848. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6849. tile_x_ql_q5_1, tile_x_dm_q5_1);
  6850. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  6851. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  6852. vec_dot_q5_1_q8_1_mul_mat>(
  6853. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6854. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6855. }
  6856. #define MMQ_X_Q8_0_RDNA2 64
  6857. #define MMQ_Y_Q8_0_RDNA2 128
  6858. #define NWARPS_Q8_0_RDNA2 8
  6859. #define MMQ_X_Q8_0_RDNA1 64
  6860. #define MMQ_Y_Q8_0_RDNA1 64
  6861. #define NWARPS_Q8_0_RDNA1 8
  6862. #if defined(SYCL_USE_XMX)
  6863. #define MMQ_X_Q8_0_AMPERE 4
  6864. #define MMQ_Y_Q8_0_AMPERE 32
  6865. #define NWARPS_Q8_0_AMPERE 4
  6866. #else
  6867. #define MMQ_X_Q8_0_AMPERE 128
  6868. #define MMQ_Y_Q8_0_AMPERE 64
  6869. #define NWARPS_Q8_0_AMPERE 4
  6870. #endif
  6871. #define MMQ_X_Q8_0_PASCAL 64
  6872. #define MMQ_Y_Q8_0_PASCAL 64
  6873. #define NWARPS_Q8_0_PASCAL 8
  6874. template <bool need_check> static void
  6875. mul_mat_q8_0(
  6876. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6877. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6878. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  6879. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6880. int * tile_x_ql = nullptr;
  6881. sycl::half2 *tile_x_dm = nullptr;
  6882. int * tile_x_qh = nullptr;
  6883. int * tile_x_sc = nullptr;
  6884. //sycl_todo: change according to hardware
  6885. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  6886. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  6887. const int nwarps = NWARPS_Q8_0_AMPERE;
  6888. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6889. tile_x_qs_q8_0, tile_x_d_q8_0);
  6890. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  6891. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  6892. vec_dot_q8_0_q8_1_mul_mat>(
  6893. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6894. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6895. }
  6896. #define MMQ_X_Q2_K_RDNA2 64
  6897. #define MMQ_Y_Q2_K_RDNA2 128
  6898. #define NWARPS_Q2_K_RDNA2 8
  6899. #define MMQ_X_Q2_K_RDNA1 128
  6900. #define MMQ_Y_Q2_K_RDNA1 32
  6901. #define NWARPS_Q2_K_RDNA1 8
  6902. #if defined(SYCL_USE_XMX)
  6903. #define MMQ_X_Q2_K_AMPERE 4
  6904. #define MMQ_Y_Q2_K_AMPERE 32
  6905. #define NWARPS_Q2_K_AMPERE 4
  6906. #else
  6907. #define MMQ_X_Q2_K_AMPERE 64
  6908. #define MMQ_Y_Q2_K_AMPERE 128
  6909. #define NWARPS_Q2_K_AMPERE 4
  6910. #endif
  6911. #define MMQ_X_Q2_K_PASCAL 64
  6912. #define MMQ_Y_Q2_K_PASCAL 64
  6913. #define NWARPS_Q2_K_PASCAL 8
  6914. template <bool need_check> static void
  6915. mul_mat_q2_K(
  6916. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6917. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6918. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  6919. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  6920. sycl::half2 *tile_y_ds) {
  6921. int * tile_x_ql = nullptr;
  6922. sycl::half2 *tile_x_dm = nullptr;
  6923. int * tile_x_qh = nullptr;
  6924. int * tile_x_sc = nullptr;
  6925. //sycl_todo: change according to hardware
  6926. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  6927. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  6928. const int nwarps = NWARPS_Q2_K_AMPERE;
  6929. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6930. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  6931. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  6932. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  6933. vec_dot_q2_K_q8_1_mul_mat>(
  6934. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6935. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6936. }
  6937. #define MMQ_X_Q3_K_RDNA2 128
  6938. #define MMQ_Y_Q3_K_RDNA2 64
  6939. #define NWARPS_Q3_K_RDNA2 8
  6940. #define MMQ_X_Q3_K_RDNA1 32
  6941. #define MMQ_Y_Q3_K_RDNA1 128
  6942. #define NWARPS_Q3_K_RDNA1 8
  6943. #if defined(SYCL_USE_XMX)
  6944. #define MMQ_X_Q3_K_AMPERE 4
  6945. #define MMQ_Y_Q3_K_AMPERE 32
  6946. #define NWARPS_Q3_K_AMPERE 4
  6947. #else
  6948. #define MMQ_X_Q3_K_AMPERE 128
  6949. #define MMQ_Y_Q3_K_AMPERE 128
  6950. #define NWARPS_Q3_K_AMPERE 4
  6951. #endif
  6952. #define MMQ_X_Q3_K_PASCAL 64
  6953. #define MMQ_Y_Q3_K_PASCAL 64
  6954. #define NWARPS_Q3_K_PASCAL 8
  6955. template <bool need_check> static void
  6956. mul_mat_q3_K(
  6957. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6958. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6959. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  6960. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  6961. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6962. int * tile_x_ql = nullptr;
  6963. sycl::half2 *tile_x_dm = nullptr;
  6964. int * tile_x_qh = nullptr;
  6965. int * tile_x_sc = nullptr;
  6966. //sycl_todo: change according to hardware
  6967. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  6968. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  6969. const int nwarps = NWARPS_Q3_K_AMPERE;
  6970. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6971. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  6972. tile_x_sc_q3_K);
  6973. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  6974. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  6975. vec_dot_q3_K_q8_1_mul_mat>(
  6976. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6977. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6978. }
  6979. #define MMQ_X_Q4_K_RDNA2 64
  6980. #define MMQ_Y_Q4_K_RDNA2 128
  6981. #define NWARPS_Q4_K_RDNA2 8
  6982. #define MMQ_X_Q4_K_RDNA1 32
  6983. #define MMQ_Y_Q4_K_RDNA1 64
  6984. #define NWARPS_Q4_K_RDNA1 8
  6985. #if defined(SYCL_USE_XMX)
  6986. #define MMQ_X_Q4_K_AMPERE 4
  6987. #define MMQ_Y_Q4_K_AMPERE 32
  6988. #define NWARPS_Q4_K_AMPERE 4
  6989. #else
  6990. #define MMQ_X_Q4_K_AMPERE 64
  6991. #define MMQ_Y_Q4_K_AMPERE 128
  6992. #define NWARPS_Q4_K_AMPERE 4
  6993. #endif
  6994. #define MMQ_X_Q4_K_PASCAL 64
  6995. #define MMQ_Y_Q4_K_PASCAL 64
  6996. #define NWARPS_Q4_K_PASCAL 8
  6997. template <bool need_check> static void
  6998. mul_mat_q4_K(
  6999. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7000. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7001. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  7002. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  7003. sycl::half2 *tile_y_ds) {
  7004. int * tile_x_ql = nullptr;
  7005. sycl::half2 *tile_x_dm = nullptr;
  7006. int * tile_x_qh = nullptr;
  7007. int * tile_x_sc = nullptr;
  7008. //sycl_todo: change according to hardware
  7009. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  7010. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  7011. const int nwarps = NWARPS_Q4_K_AMPERE;
  7012. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7013. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  7014. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  7015. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  7016. vec_dot_q4_K_q8_1_mul_mat>(
  7017. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7018. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7019. }
  7020. #define MMQ_X_Q5_K_RDNA2 64
  7021. #define MMQ_Y_Q5_K_RDNA2 128
  7022. #define NWARPS_Q5_K_RDNA2 8
  7023. #define MMQ_X_Q5_K_RDNA1 32
  7024. #define MMQ_Y_Q5_K_RDNA1 64
  7025. #define NWARPS_Q5_K_RDNA1 8
  7026. #if defined(SYCL_USE_XMX)
  7027. #define MMQ_X_Q5_K_AMPERE 4
  7028. #define MMQ_Y_Q5_K_AMPERE 32
  7029. #define NWARPS_Q5_K_AMPERE 4
  7030. #else
  7031. #define MMQ_X_Q5_K_AMPERE 64
  7032. #define MMQ_Y_Q5_K_AMPERE 128
  7033. #define NWARPS_Q5_K_AMPERE 4
  7034. #endif
  7035. #define MMQ_X_Q5_K_PASCAL 64
  7036. #define MMQ_Y_Q5_K_PASCAL 64
  7037. #define NWARPS_Q5_K_PASCAL 8
  7038. template <bool need_check> static void
  7039. mul_mat_q5_K(
  7040. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7041. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7042. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  7043. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  7044. sycl::half2 *tile_y_ds) {
  7045. int * tile_x_ql = nullptr;
  7046. sycl::half2 *tile_x_dm = nullptr;
  7047. int * tile_x_qh = nullptr;
  7048. int * tile_x_sc = nullptr;
  7049. //sycl_todo: change according to hardware
  7050. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  7051. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  7052. const int nwarps = NWARPS_Q5_K_AMPERE;
  7053. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7054. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  7055. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  7056. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  7057. vec_dot_q5_K_q8_1_mul_mat>(
  7058. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7059. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7060. }
  7061. #define MMQ_X_Q6_K_RDNA2 64
  7062. #define MMQ_Y_Q6_K_RDNA2 128
  7063. #define NWARPS_Q6_K_RDNA2 8
  7064. #define MMQ_X_Q6_K_RDNA1 32
  7065. #define MMQ_Y_Q6_K_RDNA1 64
  7066. #define NWARPS_Q6_K_RDNA1 8
  7067. #if defined(SYCL_USE_XMX)
  7068. #define MMQ_X_Q6_K_AMPERE 4
  7069. #define MMQ_Y_Q6_K_AMPERE 32
  7070. #define NWARPS_Q6_K_AMPERE 4
  7071. #else
  7072. #define MMQ_X_Q6_K_AMPERE 64
  7073. #define MMQ_Y_Q6_K_AMPERE 64
  7074. #define NWARPS_Q6_K_AMPERE 4
  7075. #endif
  7076. #define MMQ_X_Q6_K_PASCAL 64
  7077. #define MMQ_Y_Q6_K_PASCAL 64
  7078. #define NWARPS_Q6_K_PASCAL 8
  7079. template <bool need_check> static void
  7080. mul_mat_q6_K(
  7081. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7082. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7083. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  7084. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7085. // int * tile_x_ql = nullptr;
  7086. // sycl::half2 *tile_x_dm = nullptr;
  7087. int * tile_x_qh = nullptr;
  7088. // int * tile_x_sc = nullptr;
  7089. //sycl_todo: change according to hardware
  7090. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  7091. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  7092. const int nwarps = NWARPS_Q6_K_AMPERE;
  7093. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7094. tile_x_ql, tile_x_dm, tile_x_sc);
  7095. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  7096. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  7097. vec_dot_q6_K_q8_1_mul_mat>(
  7098. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7099. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7100. }
  7101. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  7102. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7103. const sycl::nd_item<3> &item_ct1) {
  7104. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7105. item_ct1.get_local_id(1);
  7106. if (row >= nrows) {
  7107. return;
  7108. }
  7109. const int blocks_per_row = ncols / qk;
  7110. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7111. const int qi_vdr = (qi / vdr); // N_threads processing 1 qk block
  7112. // partial sum for each thread
  7113. float tmp = 0.0f;
  7114. const block_q_t * x = (const block_q_t *) vx;
  7115. const block_q8_1 * y = (const block_q8_1 *) vy;
  7116. for (int i = item_ct1.get_local_id(2) / qi_vdr; i < blocks_per_row;
  7117. i += blocks_per_warp) {
  7118. const int ibx = row * blocks_per_row + i; // x block index
  7119. const int iby = i * (qk / QK8_1); // y block index that aligns with ibx
  7120. const int iqs =
  7121. vdr *
  7122. (item_ct1.get_local_id(2) -
  7123. i * qi_vdr); // x block quant index when casting the quants to int
  7124. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  7125. }
  7126. // sum up partial sums and write back result
  7127. #pragma unroll
  7128. for (int mask = 16; mask > 0; mask >>= 1) {
  7129. tmp +=
  7130. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7131. }
  7132. if (item_ct1.get_local_id(2) == 0) {
  7133. dst[row] = tmp;
  7134. }
  7135. }
  7136. template <int qk, int qi, typename block_q_t, int vdr>
  7137. static void mul_mat_vec_q_iq2_xxs_q8_1(const void *__restrict__ vx,
  7138. const void *__restrict__ vy,
  7139. float *__restrict__ dst, const int ncols,
  7140. const int nrows,
  7141. const sycl::nd_item<3> &item_ct1) {
  7142. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7143. item_ct1.get_local_id(1);
  7144. if (row >= nrows) {
  7145. return;
  7146. }
  7147. const int blocks_per_row = ncols / qk;
  7148. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7149. // partial sum for each thread
  7150. float tmp = 0.0f;
  7151. const block_q_t * x = (const block_q_t *) vx;
  7152. const block_q8_1 * y = (const block_q8_1 *) vy;
  7153. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7154. i += blocks_per_warp) {
  7155. const int ibx = row*blocks_per_row + i; // x block index
  7156. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7157. const int iqs =
  7158. vdr *
  7159. (item_ct1.get_local_id(2) %
  7160. (qi / vdr)); // x block quant index when casting the quants to int
  7161. tmp += vec_dot_iq2_xxs_q8_1(&x[ibx], &y[iby], iqs, iq2xxs_grid, ksigns_iq2xs, kmask_iq2xs);
  7162. }
  7163. // sum up partial sums and write back result
  7164. #pragma unroll
  7165. for (int mask = 16; mask > 0; mask >>= 1) {
  7166. tmp +=
  7167. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7168. }
  7169. if (item_ct1.get_local_id(2) == 0) {
  7170. dst[row] = tmp;
  7171. }
  7172. }
  7173. template <int qk, int qi, typename block_q_t, int vdr>
  7174. static void mul_mat_vec_q_iq2_xs_q8_1(const void *__restrict__ vx,
  7175. const void *__restrict__ vy,
  7176. float *__restrict__ dst, const int ncols,
  7177. const int nrows,
  7178. const sycl::nd_item<3> &item_ct1) {
  7179. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7180. item_ct1.get_local_id(1);
  7181. if (row >= nrows) {
  7182. return;
  7183. }
  7184. const int blocks_per_row = ncols / qk;
  7185. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7186. // partial sum for each thread
  7187. float tmp = 0.0f;
  7188. const block_q_t * x = (const block_q_t *) vx;
  7189. const block_q8_1 * y = (const block_q8_1 *) vy;
  7190. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7191. i += blocks_per_warp) {
  7192. const int ibx = row*blocks_per_row + i; // x block index
  7193. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7194. const int iqs =
  7195. vdr *
  7196. (item_ct1.get_local_id(2) %
  7197. (qi / vdr)); // x block quant index when casting the quants to int
  7198. tmp += vec_dot_iq2_xs_q8_1(&x[ibx], &y[iby], iqs, iq2xs_grid, ksigns64);
  7199. }
  7200. // sum up partial sums and write back result
  7201. #pragma unroll
  7202. for (int mask = 16; mask > 0; mask >>= 1) {
  7203. tmp +=
  7204. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7205. }
  7206. if (item_ct1.get_local_id(2) == 0) {
  7207. dst[row] = tmp;
  7208. }
  7209. }
  7210. template <int qk, int qi, typename block_q_t, int vdr>
  7211. static void mul_mat_vec_q_iq2_s_q8_1(const void *__restrict__ vx,
  7212. const void *__restrict__ vy,
  7213. float *__restrict__ dst, const int ncols,
  7214. const int nrows,
  7215. const sycl::nd_item<3> &item_ct1) {
  7216. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7217. item_ct1.get_local_id(1);
  7218. if (row >= nrows) {
  7219. return;
  7220. }
  7221. const int blocks_per_row = ncols / qk;
  7222. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7223. // partial sum for each thread
  7224. float tmp = 0.0f;
  7225. const block_q_t * x = (const block_q_t *) vx;
  7226. const block_q8_1 * y = (const block_q8_1 *) vy;
  7227. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7228. i += blocks_per_warp) {
  7229. const int ibx = row*blocks_per_row + i; // x block index
  7230. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7231. const int iqs =
  7232. vdr *
  7233. (item_ct1.get_local_id(2) %
  7234. (qi / vdr)); // x block quant index when casting the quants to int
  7235. tmp += vec_dot_iq2_s_q8_1(&x[ibx], &y[iby], iqs);
  7236. }
  7237. // sum up partial sums and write back result
  7238. #pragma unroll
  7239. for (int mask = 16; mask > 0; mask >>= 1) {
  7240. tmp +=
  7241. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7242. }
  7243. if (item_ct1.get_local_id(2) == 0) {
  7244. dst[row] = tmp;
  7245. }
  7246. }
  7247. template <int qk, int qi, typename block_q_t, int vdr>
  7248. static void mul_mat_vec_q_iq3_xxs_q8_1(const void *__restrict__ vx,
  7249. const void *__restrict__ vy,
  7250. float *__restrict__ dst, const int ncols,
  7251. const int nrows,
  7252. const sycl::nd_item<3> &item_ct1) {
  7253. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7254. item_ct1.get_local_id(1);
  7255. if (row >= nrows) {
  7256. return;
  7257. }
  7258. const int blocks_per_row = ncols / qk;
  7259. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7260. // partial sum for each thread
  7261. float tmp = 0.0f;
  7262. const block_q_t * x = (const block_q_t *) vx;
  7263. const block_q8_1 * y = (const block_q8_1 *) vy;
  7264. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7265. i += blocks_per_warp) {
  7266. const int ibx = row*blocks_per_row + i; // x block index
  7267. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7268. const int iqs =
  7269. vdr *
  7270. (item_ct1.get_local_id(2) %
  7271. (qi / vdr)); // x block quant index when casting the quants to int
  7272. tmp += vec_dot_iq3_xxs_q8_1(&x[ibx], &y[iby], iqs, iq3xxs_grid, ksigns64);
  7273. }
  7274. // sum up partial sums and write back result
  7275. #pragma unroll
  7276. for (int mask = 16; mask > 0; mask >>= 1) {
  7277. tmp +=
  7278. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7279. }
  7280. if (item_ct1.get_local_id(2) == 0) {
  7281. dst[row] = tmp;
  7282. }
  7283. }
  7284. template <int qk, int qi, typename block_q_t, int vdr>
  7285. static void mul_mat_vec_q_iq3_s_q8_1(const void *__restrict__ vx,
  7286. const void *__restrict__ vy,
  7287. float *__restrict__ dst, const int ncols,
  7288. const int nrows,
  7289. const sycl::nd_item<3> &item_ct1) {
  7290. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7291. item_ct1.get_local_id(1);
  7292. if (row >= nrows) {
  7293. return;
  7294. }
  7295. const int blocks_per_row = ncols / qk;
  7296. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7297. // partial sum for each thread
  7298. float tmp = 0.0f;
  7299. const block_q_t * x = (const block_q_t *) vx;
  7300. const block_q8_1 * y = (const block_q8_1 *) vy;
  7301. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7302. i += blocks_per_warp) {
  7303. const int ibx = row*blocks_per_row + i; // x block index
  7304. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7305. const int iqs =
  7306. vdr *
  7307. (item_ct1.get_local_id(2) %
  7308. (qi / vdr)); // x block quant index when casting the quants to int
  7309. tmp += vec_dot_iq3_s_q8_1(&x[ibx], &y[iby], iqs, iq3s_grid);
  7310. }
  7311. // sum up partial sums and write back result
  7312. #pragma unroll
  7313. for (int mask = 16; mask > 0; mask >>= 1) {
  7314. tmp +=
  7315. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7316. }
  7317. if (item_ct1.get_local_id(2) == 0) {
  7318. dst[row] = tmp;
  7319. }
  7320. }
  7321. template <int qk, int qi, typename block_q_t, int vdr>
  7322. static void mul_mat_vec_q_iq1_s_q8_1(const void *__restrict__ vx,
  7323. const void *__restrict__ vy,
  7324. float *__restrict__ dst, const int ncols,
  7325. const int nrows,
  7326. const sycl::nd_item<3> &item_ct1) {
  7327. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7328. item_ct1.get_local_id(1);
  7329. if (row >= nrows) {
  7330. return;
  7331. }
  7332. const int blocks_per_row = ncols / qk;
  7333. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7334. // partial sum for each thread
  7335. float tmp = 0.0f;
  7336. const block_q_t * x = (const block_q_t *) vx;
  7337. const block_q8_1 * y = (const block_q8_1 *) vy;
  7338. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7339. i += blocks_per_warp) {
  7340. const int ibx = row*blocks_per_row + i; // x block index
  7341. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7342. const int iqs =
  7343. vdr *
  7344. (item_ct1.get_local_id(2) %
  7345. (qi / vdr)); // x block quant index when casting the quants to int
  7346. tmp += vec_dot_iq1_s_q8_1(&x[ibx], &y[iby], iqs, iq1s_grid_gpu);
  7347. }
  7348. // sum up partial sums and write back result
  7349. #pragma unroll
  7350. for (int mask = 16; mask > 0; mask >>= 1) {
  7351. tmp +=
  7352. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7353. }
  7354. if (item_ct1.get_local_id(2) == 0) {
  7355. dst[row] = tmp;
  7356. }
  7357. }
  7358. template <int qk, int qi, typename block_q_t, int vdr>
  7359. static void mul_mat_vec_q_iq1_m_q8_1(const void *__restrict__ vx,
  7360. const void *__restrict__ vy,
  7361. float *__restrict__ dst, const int ncols,
  7362. const int nrows,
  7363. const sycl::nd_item<3> &item_ct1) {
  7364. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7365. item_ct1.get_local_id(1);
  7366. if (row >= nrows) {
  7367. return;
  7368. }
  7369. const int blocks_per_row = ncols / qk;
  7370. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7371. // partial sum for each thread
  7372. float tmp = 0.0f;
  7373. const block_q_t * x = (const block_q_t *) vx;
  7374. const block_q8_1 * y = (const block_q8_1 *) vy;
  7375. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7376. i += blocks_per_warp) {
  7377. const int ibx = row*blocks_per_row + i; // x block index
  7378. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7379. const int iqs =
  7380. vdr *
  7381. (item_ct1.get_local_id(2) %
  7382. (qi / vdr)); // x block quant index when casting the quants to int
  7383. tmp += vec_dot_iq1_m_q8_1(&x[ibx], &y[iby], iqs);
  7384. }
  7385. // sum up partial sums and write back result
  7386. #pragma unroll
  7387. for (int mask = 16; mask > 0; mask >>= 1) {
  7388. tmp +=
  7389. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7390. }
  7391. if (item_ct1.get_local_id(2) == 0) {
  7392. dst[row] = tmp;
  7393. }
  7394. }
  7395. template <int qk, int qi, typename block_q_t, int vdr>
  7396. static void mul_mat_vec_q_iq4_nl_q8_1(const void *__restrict__ vx,
  7397. const void *__restrict__ vy,
  7398. float *__restrict__ dst, const int ncols,
  7399. const int nrows,
  7400. const sycl::nd_item<3> &item_ct1) {
  7401. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7402. item_ct1.get_local_id(1);
  7403. if (row >= nrows) {
  7404. return;
  7405. }
  7406. const int blocks_per_row = ncols / qk;
  7407. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7408. // partial sum for each thread
  7409. float tmp = 0.0f;
  7410. const block_q_t * x = (const block_q_t *) vx;
  7411. const block_q8_1 * y = (const block_q8_1 *) vy;
  7412. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7413. i += blocks_per_warp) {
  7414. const int ibx = row*blocks_per_row + i; // x block index
  7415. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7416. const int iqs =
  7417. vdr *
  7418. (item_ct1.get_local_id(2) %
  7419. (qi / vdr)); // x block quant index when casting the quants to int
  7420. tmp += vec_dot_iq4_nl_q8_1(&x[ibx], &y[iby], iqs);
  7421. }
  7422. // sum up partial sums and write back result
  7423. #pragma unroll
  7424. for (int mask = 16; mask > 0; mask >>= 1) {
  7425. tmp +=
  7426. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7427. }
  7428. if (item_ct1.get_local_id(2) == 0) {
  7429. dst[row] = tmp;
  7430. }
  7431. }
  7432. template <int qk, int qi, typename block_q_t, int vdr>
  7433. static void mul_mat_vec_q_iq4_xs_q8_1(const void *__restrict__ vx,
  7434. const void *__restrict__ vy,
  7435. float *__restrict__ dst, const int ncols,
  7436. const int nrows,
  7437. const sycl::nd_item<3> &item_ct1) {
  7438. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7439. item_ct1.get_local_id(1);
  7440. if (row >= nrows) {
  7441. return;
  7442. }
  7443. const int blocks_per_row = ncols / qk;
  7444. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7445. // partial sum for each thread
  7446. float tmp = 0.0f;
  7447. const block_q_t * x = (const block_q_t *) vx;
  7448. const block_q8_1 * y = (const block_q8_1 *) vy;
  7449. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7450. i += blocks_per_warp) {
  7451. const int ibx = row*blocks_per_row + i; // x block index
  7452. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7453. const int iqs =
  7454. vdr *
  7455. (item_ct1.get_local_id(2) %
  7456. (qi / vdr)); // x block quant index when casting the quants to int
  7457. tmp += vec_dot_iq4_xs_q8_1(&x[ibx], &y[iby], iqs);
  7458. }
  7459. // sum up partial sums and write back result
  7460. #pragma unroll
  7461. for (int mask = 16; mask > 0; mask >>= 1) {
  7462. tmp +=
  7463. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7464. }
  7465. if (item_ct1.get_local_id(2) == 0) {
  7466. dst[row] = tmp;
  7467. }
  7468. }
  7469. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  7470. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  7471. const sycl::nd_item<3> &item_ct1) {
  7472. // qk = quantized weights per x block
  7473. // qr = number of quantized weights per data value in x block
  7474. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7475. item_ct1.get_local_id(1);
  7476. if (row >= nrows) {
  7477. return;
  7478. }
  7479. const int tid = item_ct1.get_local_id(2);
  7480. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  7481. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  7482. const int y_offset = qr == 1 ? 1 : qk/2;
  7483. // partial sum for each thread
  7484. #ifdef GGML_SYCL_F16
  7485. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  7486. #else
  7487. float tmp = 0.0f;
  7488. #endif // GGML_SYCL_F16
  7489. for (int i = 0; i < ncols; i += iter_stride) {
  7490. const int col = i + vals_per_iter*tid;
  7491. const int ib = (row*ncols + col)/qk; // x block index
  7492. const int iqs = (col%qk)/qr; // x quant index
  7493. const int iybs = col - col%qk; // y block start index
  7494. // processing >2 values per i iter is faster for fast GPUs
  7495. #pragma unroll
  7496. for (int j = 0; j < vals_per_iter; j += 2) {
  7497. // process 2 vals per j iter
  7498. // dequantize
  7499. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  7500. dfloat2 v;
  7501. dequantize_kernel(vx, ib, iqs + j/qr, v);
  7502. // matrix multiplication
  7503. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  7504. #ifdef GGML_SYCL_F16
  7505. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  7506. y[iybs + iqs + j / qr + y_offset]};
  7507. tmp += v * t1;
  7508. #else
  7509. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  7510. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  7511. #endif // GGML_SYCL_F16
  7512. }
  7513. }
  7514. // sum up partial sums and write back result
  7515. #pragma unroll
  7516. for (int mask = 16; mask > 0; mask >>= 1) {
  7517. tmp +=
  7518. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7519. }
  7520. if (tid == 0) {
  7521. #ifdef GGML_SYCL_F16
  7522. dst[row] = tmp.x() + tmp.y();
  7523. #else
  7524. dst[row] = tmp;
  7525. #endif // GGML_SYCL_F16
  7526. }
  7527. }
  7528. static void mul_mat_p021_f16_f32(
  7529. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  7530. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  7531. const sycl::nd_item<3> &item_ct1) {
  7532. const sycl::half *x = (const sycl::half *)vx;
  7533. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7534. item_ct1.get_local_id(1);
  7535. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7536. item_ct1.get_local_id(0);
  7537. const int channel_x = channel / (nchannels_y / nchannels_x);
  7538. const int nrows_y = ncols_x;
  7539. const int nrows_dst = nrows_x;
  7540. const int row_dst = row_x;
  7541. float tmp = 0.0f;
  7542. for (int col_x0 = 0; col_x0 < ncols_x;
  7543. col_x0 += item_ct1.get_local_range(2)) {
  7544. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7545. if (col_x >= ncols_x) {
  7546. break;
  7547. }
  7548. // x is transposed and permuted
  7549. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  7550. const float xi =
  7551. sycl::vec<sycl::half, 1>(x[ix])
  7552. .convert<float, sycl::rounding_mode::automatic>()[0];
  7553. const int row_y = col_x;
  7554. // y is not transposed but permuted
  7555. const int iy = channel*nrows_y + row_y;
  7556. tmp += xi * y[iy];
  7557. }
  7558. // dst is not transposed and not permuted
  7559. const int idst = channel*nrows_dst + row_dst;
  7560. // sum up partial sums and write back result
  7561. #pragma unroll
  7562. for (int mask = 16; mask > 0; mask >>= 1) {
  7563. tmp +=
  7564. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7565. }
  7566. if (item_ct1.get_local_id(2) == 0) {
  7567. dst[idst] = tmp;
  7568. }
  7569. }
  7570. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  7571. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  7572. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  7573. const sycl::nd_item<3> &item_ct1) {
  7574. const sycl::half *x = (const sycl::half *)vx;
  7575. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7576. item_ct1.get_local_id(1);
  7577. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7578. item_ct1.get_local_id(0);
  7579. const int channel_x = channel / channel_x_divisor;
  7580. const int nrows_y = ncols_x;
  7581. const int nrows_dst = nrows_x;
  7582. const int row_dst = row_x;
  7583. const int idst = channel*nrows_dst + row_dst;
  7584. float tmp = 0.0f;
  7585. for (int col_x0 = 0; col_x0 < ncols_x;
  7586. col_x0 += item_ct1.get_local_range(2)) {
  7587. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7588. if (col_x >= ncols_x) {
  7589. break;
  7590. }
  7591. const int row_y = col_x;
  7592. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  7593. const int iy = channel*nrows_y + row_y;
  7594. const float xi =
  7595. sycl::vec<sycl::half, 1>(x[ix])
  7596. .convert<float, sycl::rounding_mode::automatic>()[0];
  7597. tmp += xi * y[iy];
  7598. }
  7599. // sum up partial sums and write back result
  7600. #pragma unroll
  7601. for (int mask = 16; mask > 0; mask >>= 1) {
  7602. tmp +=
  7603. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7604. }
  7605. if (item_ct1.get_local_id(2) == 0) {
  7606. dst[idst] = tmp;
  7607. }
  7608. }
  7609. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  7610. const float * xi = (const float *) cxi;
  7611. float * dsti = (float *) cdsti;
  7612. *dsti = *xi;
  7613. }
  7614. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  7615. const float * xi = (const float *) cxi;
  7616. sycl::half *dsti = (sycl::half *)cdsti;
  7617. *dsti = sycl::vec<float, 1>(*xi)
  7618. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7619. }
  7620. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  7621. const sycl::half *xi = (const sycl::half *)cxi;
  7622. sycl::half *dsti = (sycl::half *)cdsti;
  7623. *dsti = *xi;
  7624. }
  7625. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  7626. const sycl::half *xi = (const sycl::half *)cxi;
  7627. float * dsti = (float *) cdsti;
  7628. *dsti = *xi;
  7629. }
  7630. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  7631. const int16_t *xi = (const int16_t *)cxi;
  7632. int16_t *dsti = (int16_t *)cdsti;
  7633. *dsti = *xi;
  7634. }
  7635. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  7636. const int32_t *xi = (const int32_t *)cxi;
  7637. int32_t *dsti = (int32_t *)cdsti;
  7638. *dsti = *xi;
  7639. }
  7640. template <cpy_kernel_t cpy_1>
  7641. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  7642. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7643. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7644. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7645. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7646. item_ct1.get_local_id(2);
  7647. if (i >= ne) {
  7648. return;
  7649. }
  7650. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  7651. // then combine those indices with the corresponding byte offsets to get the total offsets
  7652. const int i03 = i/(ne00 * ne01 * ne02);
  7653. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7654. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7655. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7656. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7657. const int i13 = i/(ne10 * ne11 * ne12);
  7658. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7659. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7660. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7661. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  7662. cpy_1(cx + x_offset, cdst + dst_offset);
  7663. }
  7664. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  7665. const float * xi = (const float *) cxi;
  7666. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  7667. float amax = 0.0f; // absolute max
  7668. for (int j = 0; j < QK8_0; j++) {
  7669. const float v = xi[j];
  7670. amax = sycl::fmax(amax, sycl::fabs((float)v));
  7671. }
  7672. const float d = amax / ((1 << 7) - 1);
  7673. const float id = d ? 1.0f/d : 0.0f;
  7674. dsti->d = d;
  7675. for (int j = 0; j < QK8_0; ++j) {
  7676. const float x0 = xi[j]*id;
  7677. dsti->qs[j] = sycl::round((float)x0);
  7678. }
  7679. }
  7680. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  7681. const float * xi = (const float *) cxi;
  7682. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  7683. float amax = 0.0f;
  7684. float vmax = 0.0f;
  7685. for (int j = 0; j < QK4_0; ++j) {
  7686. const float v = xi[j];
  7687. if (amax < sycl::fabs((float)v)) {
  7688. amax = sycl::fabs((float)v);
  7689. vmax = v;
  7690. }
  7691. }
  7692. const float d = vmax / -8;
  7693. const float id = d ? 1.0f/d : 0.0f;
  7694. dsti->d = d;
  7695. for (int j = 0; j < QK4_0/2; ++j) {
  7696. const float x0 = xi[0 + j]*id;
  7697. const float x1 = xi[QK4_0/2 + j]*id;
  7698. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  7699. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  7700. dsti->qs[j] = xi0;
  7701. dsti->qs[j] |= xi1 << 4;
  7702. }
  7703. }
  7704. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  7705. const float * xi = (const float *) cxi;
  7706. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  7707. float vmin = FLT_MAX;
  7708. float vmax = -FLT_MAX;
  7709. for (int j = 0; j < QK4_1; ++j) {
  7710. const float v = xi[j];
  7711. if (v < vmin) vmin = v;
  7712. if (v > vmax) vmax = v;
  7713. }
  7714. const float d = (vmax - vmin) / ((1 << 4) - 1);
  7715. const float id = d ? 1.0f/d : 0.0f;
  7716. dsti->dm.x() = d;
  7717. dsti->dm.y() = vmin;
  7718. for (int j = 0; j < QK4_1/2; ++j) {
  7719. const float x0 = (xi[0 + j] - vmin)*id;
  7720. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  7721. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  7722. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  7723. dsti->qs[j] = xi0;
  7724. dsti->qs[j] |= xi1 << 4;
  7725. }
  7726. }
  7727. template <cpy_kernel_t cpy_blck, int qk>
  7728. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  7729. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7730. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7731. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7732. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7733. item_ct1.get_local_id(2)) *
  7734. qk;
  7735. if (i >= ne) {
  7736. return;
  7737. }
  7738. const int i03 = i/(ne00 * ne01 * ne02);
  7739. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7740. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7741. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7742. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7743. const int i13 = i/(ne10 * ne11 * ne12);
  7744. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7745. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7746. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7747. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  7748. cpy_blck(cx + x_offset, cdst + dst_offset);
  7749. }
  7750. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7751. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  7752. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  7753. }
  7754. struct rope_corr_dims {
  7755. float v[4];
  7756. };
  7757. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7758. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7759. static void rope_yarn(
  7760. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  7761. float * cos_theta, float * sin_theta
  7762. ) {
  7763. // Get n-d rotational scaling corrected for extrapolation
  7764. float theta_interp = freq_scale * theta_extrap;
  7765. float theta = theta_interp;
  7766. if (ext_factor != 0.0f) {
  7767. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  7768. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7769. // Get n-d magnitude scaling corrected for interpolation
  7770. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  7771. }
  7772. *cos_theta = sycl::cos(theta) * mscale;
  7773. *sin_theta = sycl::sin(theta) * mscale;
  7774. }
  7775. // rope == RoPE == rotary positional embedding
  7776. template<typename T, bool has_pos>
  7777. static void rope(
  7778. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7779. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  7780. ,
  7781. const sycl::nd_item<3> &item_ct1) {
  7782. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7783. item_ct1.get_local_id(1));
  7784. if (col >= ncols) {
  7785. return;
  7786. }
  7787. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7788. item_ct1.get_local_id(2);
  7789. const int i = row*ncols + col;
  7790. const int i2 = row/p_delta_rows;
  7791. const int p = has_pos ? pos[i2] : 0;
  7792. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  7793. float cos_theta, sin_theta;
  7794. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7795. const float x0 = x[i + 0];
  7796. const float x1 = x[i + 1];
  7797. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7798. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  7799. }
  7800. template<typename T, bool has_pos>
  7801. static void rope_neox(
  7802. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  7803. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  7804. ,
  7805. const sycl::nd_item<3> &item_ct1) {
  7806. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7807. item_ct1.get_local_id(1));
  7808. if (col >= ncols) {
  7809. return;
  7810. }
  7811. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7812. item_ct1.get_local_id(2);
  7813. const int ib = col / n_dims;
  7814. const int ic = col % n_dims;
  7815. if (ib > 0) {
  7816. const int i = row*ncols + ib*n_dims + ic;
  7817. dst[i + 0] = x[i + 0];
  7818. dst[i + 1] = x[i + 1];
  7819. return;
  7820. }
  7821. const int i = row*ncols + ib*n_dims + ic/2;
  7822. const int i2 = row/p_delta_rows;
  7823. float cur_rot = inv_ndims * ic - ib;
  7824. const int p = has_pos ? pos[i2] : 0;
  7825. const float theta_base =
  7826. p * freq_scale * dpct::pow(theta_scale, col / 2.0f);
  7827. float cos_theta, sin_theta;
  7828. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7829. const float x0 = x[i + 0];
  7830. const float x1 = x[i + n_dims/2];
  7831. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7832. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  7833. }
  7834. static void rope_glm_f32(
  7835. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7836. int n_ctx
  7837. , const sycl::nd_item<3> &item_ct1) {
  7838. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7839. item_ct1.get_local_id(2);
  7840. const int half_n_dims = ncols/4;
  7841. if (col >= half_n_dims) {
  7842. return;
  7843. }
  7844. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7845. item_ct1.get_local_id(1);
  7846. const int i = row*ncols + col;
  7847. const int i2 = row/p_delta_rows;
  7848. const float col_theta_scale = dpct::pow(freq_base, -2.0f * col / ncols);
  7849. // FIXME: this is likely wrong
  7850. const int p = pos != nullptr ? pos[i2] : 0;
  7851. const float theta = sycl::min(p, n_ctx - 2) * freq_scale * col_theta_scale;
  7852. const float sin_theta = sycl::sin((float)theta);
  7853. const float cos_theta = sycl::cos((float)theta);
  7854. const float x0 = x[i + 0];
  7855. const float x1 = x[i + half_n_dims];
  7856. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7857. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  7858. const float block_theta =
  7859. ((float)sycl::max(p - n_ctx - 2, 0)) * col_theta_scale;
  7860. const float sin_block_theta = sycl::sin((float)block_theta);
  7861. const float cos_block_theta = sycl::cos((float)block_theta);
  7862. const float x2 = x[i + half_n_dims * 2];
  7863. const float x3 = x[i + half_n_dims * 3];
  7864. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  7865. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  7866. }
  7867. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  7868. const sycl::nd_item<3> &item_ct1) {
  7869. const int row = item_ct1.get_group(1);
  7870. const int col = item_ct1.get_local_id(2);
  7871. float sum = 0.0f;
  7872. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  7873. sum += x[row * ncols + i];
  7874. }
  7875. sum = warp_reduce_sum(sum, item_ct1);
  7876. if (col == 0) {
  7877. dst[row] = sum;
  7878. }
  7879. }
  7880. template<typename T>
  7881. static inline void ggml_sycl_swap(T & a, T & b) {
  7882. T tmp = a;
  7883. a = b;
  7884. b = tmp;
  7885. }
  7886. template <ggml_sort_order order>
  7887. __dpct_inline__ static void
  7888. k_argsort_f32_i32(const float *x, int *dst, const int ncols, int ncols_pad,
  7889. const sycl::nd_item<3> &item_ct1, uint8_t *dpct_local) {
  7890. // bitonic sort
  7891. int col = item_ct1.get_local_id(2);
  7892. int row = item_ct1.get_group(1);
  7893. if (col >= ncols_pad) {
  7894. return;
  7895. }
  7896. const float * x_row = x + row * ncols;
  7897. auto dst_row = (int *)dpct_local;
  7898. // initialize indices
  7899. dst_row[col] = col;
  7900. item_ct1.barrier(sycl::access::fence_space::local_space);
  7901. for (int k = 2; k <= ncols_pad; k *= 2) {
  7902. for (int j = k / 2; j > 0; j /= 2) {
  7903. int ixj = col ^ j;
  7904. if (ixj > col) {
  7905. if ((col & k) == 0) {
  7906. if (dst_row[col] >= ncols ||
  7907. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  7908. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  7909. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  7910. ) {
  7911. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  7912. }
  7913. } else {
  7914. if (dst_row[ixj] >= ncols ||
  7915. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  7916. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  7917. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  7918. ) {
  7919. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  7920. }
  7921. }
  7922. }
  7923. /*
  7924. DPCT1118:1: SYCL group functions and algorithms must be encountered
  7925. in converged control flow. You may need to adjust the code.
  7926. */
  7927. item_ct1.barrier(sycl::access::fence_space::local_space);
  7928. }
  7929. }
  7930. // copy the result to dst without the padding
  7931. if (col < ncols) {
  7932. dst[row * ncols + col] = dst_row[col];
  7933. }
  7934. }
  7935. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  7936. const sycl::nd_item<3> &item_ct1) {
  7937. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7938. item_ct1.get_local_id(1);
  7939. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7940. item_ct1.get_local_id(2);
  7941. if (col >= ncols) {
  7942. return;
  7943. }
  7944. const int i = row*ncols + col;
  7945. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  7946. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  7947. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  7948. }
  7949. template <bool vals_smem, int ncols_template, int block_size_template>
  7950. static void soft_max_f32(const float * x, const float * mask, float * dst, const int ncols_par,
  7951. const int nrows_y, const float scale, const float max_bias, const float m0,
  7952. const float m1, uint32_t n_head_log2, const sycl::nd_item<3> &item_ct1, float *buf) {
  7953. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  7954. const int tid = item_ct1.get_local_id(2);
  7955. const int rowx = item_ct1.get_group(2);
  7956. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  7957. const int block_size = block_size_template == 0 ? item_ct1.get_local_range(2) : block_size_template;
  7958. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  7959. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  7960. float slope = 1.0f;
  7961. // ALiBi
  7962. if (max_bias > 0.0f) {
  7963. const uint32_t h = rowx/nrows_y; // head index
  7964. const float base = h < n_head_log2 ? m0 : m1;
  7965. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  7966. slope = sycl::pow(base, float(exp));
  7967. }
  7968. float * vals = vals_smem ? buf + WARP_SIZE : dst + rowx*ncols;
  7969. float max_val = -INFINITY;
  7970. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7971. const int col = col0 + tid;
  7972. if (ncols_template == 0 && col >= ncols) {
  7973. break;
  7974. }
  7975. const int ix = rowx*ncols + col;
  7976. const int iy = rowy*ncols + col;
  7977. const float val = x[ix]*scale + (mask ? slope*mask[iy] : 0.0f);
  7978. vals[col] = val;
  7979. max_val = sycl::max(max_val, val);
  7980. }
  7981. // find the max value in the block
  7982. max_val = warp_reduce_max(max_val, item_ct1);
  7983. if (block_size > WARP_SIZE) {
  7984. if (warp_id == 0) {
  7985. buf[lane_id] = -INFINITY;
  7986. }
  7987. item_ct1.barrier(sycl::access::fence_space::local_space);
  7988. if (lane_id == 0) {
  7989. buf[warp_id] = max_val;
  7990. }
  7991. item_ct1.barrier(sycl::access::fence_space::local_space);
  7992. max_val = buf[lane_id];
  7993. max_val = warp_reduce_max(max_val, item_ct1);
  7994. }
  7995. float tmp = 0.f;
  7996. #pragma unroll
  7997. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7998. const int col = col0 + tid;
  7999. if (ncols_template == 0 && col >= ncols) {
  8000. break;
  8001. }
  8002. const float val = sycl::native::exp(vals[col] - max_val);
  8003. tmp += val;
  8004. vals[col] = val;
  8005. }
  8006. // find the sum of exps in the block
  8007. tmp = warp_reduce_sum(tmp, item_ct1);
  8008. if (block_size > WARP_SIZE) {
  8009. if (warp_id == 0) {
  8010. buf[lane_id] = 0.f;
  8011. }
  8012. item_ct1.barrier(sycl::access::fence_space::local_space);
  8013. if (lane_id == 0) {
  8014. buf[warp_id] = tmp;
  8015. }
  8016. item_ct1.barrier(sycl::access::fence_space::local_space);
  8017. tmp = buf[lane_id];
  8018. tmp = warp_reduce_sum(tmp, item_ct1);
  8019. }
  8020. const float inv_sum = 1.f / tmp;
  8021. #pragma unroll
  8022. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  8023. const int col = col0 + tid;
  8024. if (ncols_template == 0 && col >= ncols) {
  8025. return;
  8026. }
  8027. const int idst = rowx*ncols + col;
  8028. dst[idst] = vals[col] * inv_sum;
  8029. }
  8030. }
  8031. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  8032. const sycl::nd_item<3> &item_ct1) {
  8033. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  8034. item_ct1.get_local_id(2);
  8035. if (i >= k) {
  8036. return;
  8037. }
  8038. dst[i] = scale * x[i];
  8039. }
  8040. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  8041. const sycl::nd_item<3> &item_ct1) {
  8042. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  8043. item_ct1.get_local_id(2);
  8044. if (i >= k) {
  8045. return;
  8046. }
  8047. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  8048. }
  8049. template <typename T>
  8050. static void im2col_kernel(const float *x, T *dst, int offset_delta,
  8051. int IW, int IH, int OW, int KW, int KH,
  8052. int pelements, int CHW, int s0, int s1, int p0,
  8053. int p1, int d0, int d1,
  8054. const sycl::nd_item<3> &item_ct1) {
  8055. const int i = item_ct1.get_local_id(2) +
  8056. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  8057. if (i >= pelements) {
  8058. return;
  8059. }
  8060. const int ksize = OW * (KH > 1 ? KW : 1);
  8061. const int kx = i / ksize;
  8062. const int kd = kx * ksize;
  8063. const int ky = (i - kd) / OW;
  8064. const int ix = i % OW;
  8065. const int64_t iiw = ix * s0 + kx * d0 - p0;
  8066. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  8067. const int64_t offset_dst =
  8068. (item_ct1.get_group(1) * OW + ix) * CHW +
  8069. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  8070. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8071. dst[offset_dst] =
  8072. sycl::vec<float, 1>(0.0f)
  8073. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  8074. } else {
  8075. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  8076. dst[offset_dst] =
  8077. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  8078. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  8079. }
  8080. }
  8081. template <typename Ti, typename To>
  8082. static void pool2d_nchw_kernel(
  8083. const int ih, const int iw, const int oh, const int ow,
  8084. const int kh, const int kw, const int sh, const int sw,
  8085. const int ph, const int pw, const int parallel_elements,
  8086. const Ti* src, To* dst, const enum ggml_op_pool op,
  8087. const sycl::nd_item<3> &item_ct1) {
  8088. int idx = item_ct1.get_local_id(2) +
  8089. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  8090. if (idx >= parallel_elements) {
  8091. return;
  8092. }
  8093. const int I_HW = ih * iw;
  8094. const int O_HW = oh * ow;
  8095. const int nc = idx / O_HW;
  8096. const int cur_oh = idx % O_HW / ow;
  8097. const int cur_ow = idx % O_HW % ow;
  8098. const Ti* i_ptr = src + nc * I_HW;
  8099. To* o_ptr = dst + nc * O_HW;
  8100. const int start_h = cur_oh * sh - ph;
  8101. const int bh = sycl::max(0, start_h);
  8102. const int eh = sycl::min(ih, start_h + kh);
  8103. const int start_w = cur_ow * sw - pw;
  8104. const int bw = sycl::max(0, start_w);
  8105. const int ew = sycl::min(iw, start_w + kw);
  8106. To res = 0;
  8107. switch (op) {
  8108. case GGML_OP_POOL_AVG: res = 0; break;
  8109. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  8110. }
  8111. for (int i = bh; i < eh; i += 1) {
  8112. for (int j = bw; j < ew; j += 1) {
  8113. #if DPCT_COMPATIBILITY_TEMP >= 350
  8114. /*
  8115. DPCT1098:106: The '*' expression is used instead of the __ldg
  8116. call. These two expressions do not provide the exact same
  8117. functionality. Check the generated code for potential precision
  8118. and/or performance issues.
  8119. */
  8120. Ti cur = *(i_ptr + i * iw + j);
  8121. #else
  8122. Ti cur = i_ptr[i * iw + j];
  8123. #endif
  8124. switch (op) {
  8125. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  8126. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  8127. }
  8128. }
  8129. }
  8130. o_ptr[cur_oh * ow + cur_ow] = res;
  8131. }
  8132. template <int qk, int qr, dequantize_kernel_t dq>
  8133. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  8134. ggml_tensor *dst, const void *src0_dd,
  8135. const int32_t *src1_dd, float *dst_dd,
  8136. dpct::queue_ptr stream) {
  8137. GGML_TENSOR_BINARY_OP_LOCALS
  8138. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  8139. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  8140. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  8141. // strides in elements
  8142. //const size_t s0 = nb0 / ggml_element_size(dst);
  8143. const size_t s1 = nb1 / ggml_element_size(dst);
  8144. const size_t s2 = nb2 / ggml_element_size(dst);
  8145. const size_t s3 = nb3 / ggml_element_size(dst);
  8146. const size_t s10 = nb10 / ggml_element_size(src1);
  8147. const size_t s11 = nb11 / ggml_element_size(src1);
  8148. const size_t s12 = nb12 / ggml_element_size(src1);
  8149. //const size_t s13 = nb13 / ggml_element_size(src1);
  8150. GGML_ASSERT(ne00 % 2 == 0);
  8151. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8152. [=](sycl::nd_item<3> item_ct1) {
  8153. k_get_rows<qk, qr, dq>(
  8154. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  8155. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  8156. });
  8157. (void) dst;
  8158. }
  8159. template <typename src0_t>
  8160. static void get_rows_sycl_float(const ggml_tensor *src0,
  8161. const ggml_tensor *src1, ggml_tensor *dst,
  8162. const src0_t *src0_dd, const int32_t *src1_dd,
  8163. float *dst_dd, dpct::queue_ptr stream) {
  8164. GGML_TENSOR_BINARY_OP_LOCALS
  8165. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  8166. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  8167. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  8168. // strides in elements
  8169. //const size_t s0 = nb0 / ggml_element_size(dst);
  8170. const size_t s1 = nb1 / ggml_element_size(dst);
  8171. const size_t s2 = nb2 / ggml_element_size(dst);
  8172. const size_t s3 = nb3 / ggml_element_size(dst);
  8173. const size_t s10 = nb10 / ggml_element_size(src1);
  8174. const size_t s11 = nb11 / ggml_element_size(src1);
  8175. const size_t s12 = nb12 / ggml_element_size(src1);
  8176. //const size_t s13 = nb13 / ggml_element_size(src1);
  8177. {
  8178. dpct::has_capability_or_fail(stream->get_device(),
  8179. {sycl::aspect::fp16});
  8180. stream->parallel_for(
  8181. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8182. [=](sycl::nd_item<3> item_ct1) {
  8183. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  8184. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  8185. });
  8186. }
  8187. (void) dst;
  8188. }
  8189. template<float (*bin_op)(const float, const float)>
  8190. struct bin_bcast_sycl {
  8191. template <typename src0_t, typename src1_t, typename dst_t>
  8192. void operator()(const struct ggml_tensor *src0,
  8193. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  8194. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  8195. dpct::queue_ptr stream) {
  8196. GGML_TENSOR_BINARY_OP_LOCALS
  8197. int nr0 = ne10/ne0;
  8198. int nr1 = ne11/ne1;
  8199. int nr2 = ne12/ne2;
  8200. int nr3 = ne13/ne3;
  8201. int nr[4] = { nr0, nr1, nr2, nr3 };
  8202. // collapse dimensions until first broadcast dimension
  8203. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  8204. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  8205. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  8206. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  8207. auto collapse = [](int64_t cne[]) {
  8208. cne[0] *= cne[1];
  8209. cne[1] = cne[2];
  8210. cne[2] = cne[3];
  8211. cne[3] = 1;
  8212. };
  8213. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  8214. cnb[1] *= cne[1];
  8215. cnb[2] *= cne[2];
  8216. cnb[3] *= cne[3];
  8217. };
  8218. for (int i = 0; i < 4; i++) {
  8219. if (nr[i] != 1) {
  8220. break;
  8221. }
  8222. if (i > 0) {
  8223. collapse_nb(cnb0, cne0);
  8224. collapse_nb(cnb1, cne1);
  8225. collapse(cne0);
  8226. collapse(cne1);
  8227. }
  8228. }
  8229. {
  8230. int64_t ne0 = cne0[0];
  8231. int64_t ne1 = cne0[1];
  8232. int64_t ne2 = cne0[2];
  8233. int64_t ne3 = cne0[3];
  8234. int64_t ne10 = cne1[0];
  8235. int64_t ne11 = cne1[1];
  8236. int64_t ne12 = cne1[2];
  8237. int64_t ne13 = cne1[3];
  8238. size_t nb0 = cnb0[0];
  8239. size_t nb1 = cnb0[1];
  8240. size_t nb2 = cnb0[2];
  8241. size_t nb3 = cnb0[3];
  8242. size_t nb10 = cnb1[0];
  8243. size_t nb11 = cnb1[1];
  8244. size_t nb12 = cnb1[2];
  8245. size_t nb13 = cnb1[3];
  8246. size_t s0 = nb0 / sizeof(dst_t);
  8247. size_t s1 = nb1 / sizeof(dst_t);
  8248. size_t s2 = nb2 / sizeof(dst_t);
  8249. size_t s3 = nb3 / sizeof(dst_t);
  8250. size_t s10 = nb10 / sizeof(src1_t);
  8251. size_t s11 = nb11 / sizeof(src1_t);
  8252. size_t s12 = nb12 / sizeof(src1_t);
  8253. size_t s13 = nb13 / sizeof(src1_t);
  8254. GGML_ASSERT(s0 == 1);
  8255. GGML_ASSERT(s10 == 1);
  8256. const int block_size = 128;
  8257. int64_t hne0 = std::max(ne0/2LL, 1LL);
  8258. sycl::range<3> block_dims(1, 1, 1);
  8259. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  8260. block_dims[1] = std::min<unsigned int>(
  8261. ne1, block_size / (unsigned int)block_dims[2]);
  8262. block_dims[0] = std::min(
  8263. std::min<unsigned int>(
  8264. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  8265. (unsigned int)block_dims[1]),
  8266. 64U);
  8267. sycl::range<3> block_nums(
  8268. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  8269. (ne1 + block_dims[1] - 1) / block_dims[1],
  8270. (hne0 + block_dims[2] - 1) / block_dims[2]);
  8271. if (block_nums[0] > 65535) {
  8272. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  8273. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  8274. {
  8275. dpct::has_capability_or_fail(stream->get_device(),
  8276. {sycl::aspect::fp16});
  8277. stream->parallel_for(
  8278. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  8279. sycl::range<3>(1, 1, block_size),
  8280. sycl::range<3>(1, 1, block_size)),
  8281. [=](sycl::nd_item<3> item_ct1) {
  8282. k_bin_bcast_unravel<bin_op>(
  8283. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  8284. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  8285. s13, item_ct1);
  8286. });
  8287. }
  8288. } else {
  8289. /*
  8290. DPCT1049:16: The work-group size passed to the SYCL kernel may
  8291. exceed the limit. To get the device limit, query
  8292. info::device::max_work_group_size. Adjust the work-group size if
  8293. needed.
  8294. */
  8295. dpct::has_capability_or_fail(stream->get_device(),
  8296. {sycl::aspect::fp16});
  8297. stream->parallel_for(
  8298. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8299. [=](sycl::nd_item<3> item_ct1) {
  8300. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  8301. ne2, ne3, ne10, ne11, ne12, ne13,
  8302. s1, s2, s3, s11, s12, s13,
  8303. item_ct1);
  8304. });
  8305. }
  8306. }
  8307. }
  8308. };
  8309. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  8310. const int n_elements, const int ne10, const int ne11,
  8311. const int ne12, const int nb1, const int nb2,
  8312. const int offset, dpct::queue_ptr stream) {
  8313. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  8314. stream->parallel_for(
  8315. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8316. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  8317. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  8318. [=](sycl::nd_item<3> item_ct1) {
  8319. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  8320. item_ct1);
  8321. });
  8322. }
  8323. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  8324. dpct::queue_ptr stream) {
  8325. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8326. stream->parallel_for(
  8327. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8328. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8329. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8330. [=](sycl::nd_item<3> item_ct1) {
  8331. gelu_f32(x, dst, k, item_ct1);
  8332. });
  8333. }
  8334. static void silu_f32_sycl(const float *x, float *dst, const int k,
  8335. dpct::queue_ptr stream) {
  8336. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  8337. stream->parallel_for(
  8338. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8339. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  8340. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  8341. [=](sycl::nd_item<3> item_ct1) {
  8342. silu_f32(x, dst, k, item_ct1);
  8343. });
  8344. }
  8345. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  8346. dpct::queue_ptr stream) {
  8347. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8348. stream->parallel_for(
  8349. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8350. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8351. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8352. [=](sycl::nd_item<3> item_ct1) {
  8353. gelu_quick_f32(x, dst, k, item_ct1);
  8354. });
  8355. }
  8356. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  8357. dpct::queue_ptr stream) {
  8358. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  8359. stream->parallel_for(
  8360. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8361. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  8362. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  8363. [=](sycl::nd_item<3> item_ct1) {
  8364. tanh_f32(x, dst, k, item_ct1);
  8365. });
  8366. }
  8367. static void relu_f32_sycl(const float *x, float *dst, const int k,
  8368. dpct::queue_ptr stream) {
  8369. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8370. stream->parallel_for(
  8371. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8372. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8373. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8374. [=](sycl::nd_item<3> item_ct1) {
  8375. relu_f32(x, dst, k, item_ct1);
  8376. });
  8377. }
  8378. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  8379. dpct::queue_ptr stream) {
  8380. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  8381. stream->parallel_for(
  8382. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8383. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  8384. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  8385. [=](sycl::nd_item<3> item_ct1) {
  8386. hardsigmoid_f32(x, dst, k, item_ct1);
  8387. });
  8388. }
  8389. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  8390. dpct::queue_ptr stream) {
  8391. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  8392. stream->parallel_for(
  8393. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8394. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  8395. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  8396. [=](sycl::nd_item<3> item_ct1) {
  8397. hardswish_f32(x, dst, k, item_ct1);
  8398. });
  8399. }
  8400. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  8401. const float negative_slope,
  8402. dpct::queue_ptr stream) {
  8403. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8404. stream->parallel_for(
  8405. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8406. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8407. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8408. [=](sycl::nd_item<3> item_ct1) {
  8409. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  8410. });
  8411. }
  8412. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  8413. dpct::queue_ptr stream) {
  8414. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  8415. stream->parallel_for(
  8416. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8417. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  8418. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  8419. [=](sycl::nd_item<3> item_ct1) {
  8420. sqr_f32(x, dst, k, item_ct1);
  8421. });
  8422. }
  8423. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  8424. const int nrows, const float eps,
  8425. dpct::queue_ptr stream) {
  8426. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8427. if (ncols < 1024) {
  8428. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8429. stream->submit([&](sycl::handler &cgh) {
  8430. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8431. sycl::range<1>(32), cgh);
  8432. cgh.parallel_for(
  8433. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8434. block_dims),
  8435. [=](sycl::nd_item<3> item_ct1)
  8436. [[intel::reqd_sub_group_size(32)]] {
  8437. norm_f32(x, dst, ncols, eps, item_ct1,
  8438. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8439. });
  8440. });
  8441. } else {
  8442. const int work_group_size = g_work_group_size;
  8443. const sycl::range<3> block_dims(1, 1, work_group_size);
  8444. /*
  8445. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  8446. the limit. To get the device limit, query
  8447. info::device::max_work_group_size. Adjust the work-group size if needed.
  8448. */
  8449. stream->submit([&](sycl::handler &cgh) {
  8450. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8451. sycl::range<1>(32), cgh);
  8452. cgh.parallel_for(
  8453. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8454. block_dims),
  8455. [=](sycl::nd_item<3> item_ct1)
  8456. [[intel::reqd_sub_group_size(32)]] {
  8457. norm_f32(x, dst, ncols, eps, item_ct1,
  8458. s_sum_acc_ct1.get_pointer(), work_group_size);
  8459. });
  8460. });
  8461. }
  8462. }
  8463. static void group_norm_f32_sycl(const float *x, float *dst,
  8464. const int num_groups, const int group_size,
  8465. const int ne_elements, dpct::queue_ptr stream) {
  8466. static const float eps = 1e-6f;
  8467. if (group_size < 1024) {
  8468. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8469. stream->submit([&](sycl::handler &cgh) {
  8470. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8471. cgh);
  8472. const float eps_ct4 = eps;
  8473. cgh.parallel_for(
  8474. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8475. block_dims),
  8476. [=](sycl::nd_item<3> item_ct1)
  8477. [[intel::reqd_sub_group_size(32)]] {
  8478. group_norm_f32(
  8479. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  8480. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8481. });
  8482. });
  8483. } else {
  8484. const int work_group_size = g_work_group_size;
  8485. const sycl::range<3> block_dims(1, 1, work_group_size);
  8486. /*
  8487. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  8488. the limit. To get the device limit, query
  8489. info::device::max_work_group_size. Adjust the work-group size if needed.
  8490. */
  8491. stream->submit([&](sycl::handler &cgh) {
  8492. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8493. cgh);
  8494. const float eps_ct4 = eps;
  8495. cgh.parallel_for(
  8496. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8497. block_dims),
  8498. [=](sycl::nd_item<3> item_ct1)
  8499. [[intel::reqd_sub_group_size(32)]] {
  8500. group_norm_f32(x, dst, group_size, ne_elements,
  8501. eps_ct4, item_ct1,
  8502. s_sum_acc_ct1.get_pointer(), work_group_size);
  8503. });
  8504. });
  8505. }
  8506. }
  8507. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  8508. const int ne0, int ne1, int ne2, int ne02,
  8509. dpct::queue_ptr stream) {
  8510. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  8511. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8512. stream->parallel_for(
  8513. sycl::nd_range<3>(gridDim *
  8514. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  8515. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  8516. [=](sycl::nd_item<3> item_ct1) {
  8517. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  8518. });
  8519. }
  8520. static void upscale_f32_sycl(const float *x, float *dst, const int nb00, const int nb01,
  8521. const int nb02, const int nb03, const int ne10, const int ne11,
  8522. const int ne12, const int ne13, const float sf0, const float sf1,
  8523. const float sf2, const float sf3, dpct::queue_ptr stream) {
  8524. int dst_size = ne10 * ne11 * ne12 * ne13;
  8525. int num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  8526. sycl::range<1> gridDim(num_blocks * SYCL_UPSCALE_BLOCK_SIZE);
  8527. stream->parallel_for(
  8528. sycl::nd_range<1>(gridDim, sycl::range<1>(SYCL_UPSCALE_BLOCK_SIZE)),
  8529. [=](sycl::nd_item<1> item_ct1) {
  8530. upscale_f32(x, dst, nb00, nb01, nb02, nb03, ne10, ne11, ne12, ne13, sf0, sf1, sf2, sf3, item_ct1);
  8531. });
  8532. }
  8533. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  8534. const int ne01, const int ne02, const int ne0,
  8535. const int ne1, const int ne2, dpct::queue_ptr stream) {
  8536. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  8537. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8538. stream->parallel_for(
  8539. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  8540. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  8541. [=](sycl::nd_item<3> item_ct1) {
  8542. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  8543. });
  8544. }
  8545. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  8546. const int nrows, const float eps,
  8547. dpct::queue_ptr stream) {
  8548. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8549. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  8550. if (ncols < 1024) {
  8551. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8552. stream->submit([&](sycl::handler &cgh) {
  8553. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8554. cgh);
  8555. cgh.parallel_for(
  8556. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8557. block_dims),
  8558. [=](sycl::nd_item<3> item_ct1)
  8559. [[intel::reqd_sub_group_size(32)]] {
  8560. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8561. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8562. });
  8563. });
  8564. } else {
  8565. const int work_group_size = g_work_group_size;
  8566. const sycl::range<3> block_dims(1, 1, work_group_size);
  8567. /*
  8568. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  8569. the limit. To get the device limit, query
  8570. info::device::max_work_group_size. Adjust the work-group size if needed.
  8571. */
  8572. stream->submit([&](sycl::handler &cgh) {
  8573. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8574. cgh);
  8575. cgh.parallel_for(
  8576. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8577. block_dims),
  8578. [=](sycl::nd_item<3> item_ct1)
  8579. [[intel::reqd_sub_group_size(32)]] {
  8580. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8581. s_sum_acc_ct1.get_pointer(), work_group_size);
  8582. });
  8583. });
  8584. }
  8585. }
  8586. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  8587. const int ky, const int kx_padded,
  8588. dpct::queue_ptr stream) {
  8589. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  8590. const sycl::range<3> num_blocks(1, ky, block_num_x);
  8591. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  8592. {
  8593. dpct::has_capability_or_fail(stream->get_device(),
  8594. {sycl::aspect::fp16});
  8595. stream->parallel_for(
  8596. sycl::nd_range<3>(num_blocks * block_size, block_size),
  8597. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8598. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  8599. });
  8600. }
  8601. }
  8602. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  8603. static void dequantize_block_sycl(const void *__restrict__ vx,
  8604. dst_t *__restrict__ y, const int k,
  8605. dpct::queue_ptr stream) {
  8606. const int num_blocks = (k + 2*SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2*SYCL_DEQUANTIZE_BLOCK_SIZE);
  8607. {
  8608. dpct::has_capability_or_fail(stream->get_device(),
  8609. {sycl::aspect::fp16});
  8610. stream->parallel_for(
  8611. sycl::nd_range<3>(
  8612. sycl::range<3>(1, 1, num_blocks) *
  8613. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8614. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8615. [=](sycl::nd_item<3> item_ct1) {
  8616. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  8617. });
  8618. }
  8619. }
  8620. template <typename dst_t>
  8621. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  8622. dpct::queue_ptr stream) {
  8623. const int nb = k / QK_K;
  8624. #if QK_K == 256
  8625. {
  8626. dpct::has_capability_or_fail(stream->get_device(),
  8627. {sycl::aspect::fp16});
  8628. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8629. sycl::range<3>(1, 1, 64),
  8630. sycl::range<3>(1, 1, 64)),
  8631. [=](sycl::nd_item<3> item_ct1) {
  8632. dequantize_block_q2_K(vx, y, item_ct1);
  8633. });
  8634. }
  8635. #else
  8636. {
  8637. dpct::has_capability_or_fail(stream->get_device(),
  8638. {sycl::aspect::fp16});
  8639. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8640. sycl::range<3>(1, 1, 32),
  8641. sycl::range<3>(1, 1, 32)),
  8642. [=](sycl::nd_item<3> item_ct1) {
  8643. dequantize_block_q2_K(vx, y, item_ct1);
  8644. });
  8645. }
  8646. #endif
  8647. }
  8648. template <typename dst_t>
  8649. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  8650. dpct::queue_ptr stream) {
  8651. const int nb = k / QK_K;
  8652. #if QK_K == 256
  8653. {
  8654. dpct::has_capability_or_fail(stream->get_device(),
  8655. {sycl::aspect::fp16});
  8656. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8657. sycl::range<3>(1, 1, 64),
  8658. sycl::range<3>(1, 1, 64)),
  8659. [=](sycl::nd_item<3> item_ct1) {
  8660. dequantize_block_q3_K(vx, y, item_ct1);
  8661. });
  8662. }
  8663. #else
  8664. {
  8665. dpct::has_capability_or_fail(stream->get_device(),
  8666. {sycl::aspect::fp16});
  8667. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8668. sycl::range<3>(1, 1, 32),
  8669. sycl::range<3>(1, 1, 32)),
  8670. [=](sycl::nd_item<3> item_ct1) {
  8671. dequantize_block_q3_K(vx, y, item_ct1);
  8672. });
  8673. }
  8674. #endif
  8675. }
  8676. template <typename dst_t>
  8677. static void dequantize_row_q4_0_sycl(const void *vx, dst_t *y, const int k,
  8678. dpct::queue_ptr stream) {
  8679. const int nb32 = k / 32;
  8680. const int nb = (k + 255) / 256;
  8681. {
  8682. dpct::has_capability_or_fail(stream->get_device(),
  8683. {sycl::aspect::fp16});
  8684. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8685. sycl::range<3>(1, 1, 32),
  8686. sycl::range<3>(1, 1, 32)),
  8687. [=](sycl::nd_item<3> item_ct1) {
  8688. dequantize_block_q4_0(vx, y, nb32, item_ct1);
  8689. });
  8690. }
  8691. }
  8692. template <typename dst_t>
  8693. static void dequantize_row_q4_1_sycl(const void *vx, dst_t *y, const int k,
  8694. dpct::queue_ptr stream) {
  8695. const int nb32 = k / 32;
  8696. const int nb = (k + 255) / 256;
  8697. {
  8698. dpct::has_capability_or_fail(stream->get_device(),
  8699. {sycl::aspect::fp16});
  8700. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8701. sycl::range<3>(1, 1, 32),
  8702. sycl::range<3>(1, 1, 32)),
  8703. [=](sycl::nd_item<3> item_ct1) {
  8704. dequantize_block_q4_1(vx, y, nb32, item_ct1);
  8705. });
  8706. }
  8707. }
  8708. template <typename dst_t>
  8709. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  8710. dpct::queue_ptr stream) {
  8711. const int nb = k / QK_K;
  8712. {
  8713. dpct::has_capability_or_fail(stream->get_device(),
  8714. {sycl::aspect::fp16});
  8715. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8716. sycl::range<3>(1, 1, 32),
  8717. sycl::range<3>(1, 1, 32)),
  8718. [=](sycl::nd_item<3> item_ct1) {
  8719. dequantize_block_q4_K(vx, y, item_ct1);
  8720. });
  8721. }
  8722. }
  8723. template <typename dst_t>
  8724. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  8725. dpct::queue_ptr stream) {
  8726. const int nb = k / QK_K;
  8727. #if QK_K == 256
  8728. {
  8729. dpct::has_capability_or_fail(stream->get_device(),
  8730. {sycl::aspect::fp16});
  8731. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8732. sycl::range<3>(1, 1, 64),
  8733. sycl::range<3>(1, 1, 64)),
  8734. [=](sycl::nd_item<3> item_ct1) {
  8735. dequantize_block_q5_K(vx, y, item_ct1);
  8736. });
  8737. }
  8738. #else
  8739. {
  8740. dpct::has_capability_or_fail(stream->get_device(),
  8741. {sycl::aspect::fp16});
  8742. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8743. sycl::range<3>(1, 1, 32),
  8744. sycl::range<3>(1, 1, 32)),
  8745. [=](sycl::nd_item<3> item_ct1) {
  8746. dequantize_block_q5_K(vx, y, item_ct1);
  8747. });
  8748. }
  8749. #endif
  8750. }
  8751. template <typename dst_t>
  8752. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  8753. dpct::queue_ptr stream) {
  8754. const int nb = k / QK_K;
  8755. #if QK_K == 256
  8756. {
  8757. dpct::has_capability_or_fail(stream->get_device(),
  8758. {sycl::aspect::fp16});
  8759. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8760. sycl::range<3>(1, 1, 64),
  8761. sycl::range<3>(1, 1, 64)),
  8762. [=](sycl::nd_item<3> item_ct1) {
  8763. dequantize_block_q6_K(vx, y, item_ct1);
  8764. });
  8765. }
  8766. #else
  8767. {
  8768. dpct::has_capability_or_fail(stream->get_device(),
  8769. {sycl::aspect::fp16});
  8770. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8771. sycl::range<3>(1, 1, 32),
  8772. sycl::range<3>(1, 1, 32)),
  8773. [=](sycl::nd_item<3> item_ct1) {
  8774. dequantize_block_q6_K(vx, y, item_ct1);
  8775. });
  8776. }
  8777. #endif
  8778. }
  8779. template <typename dst_t>
  8780. static void dequantize_row_iq1_s_sycl(const void *vx, dst_t *y, const int k,
  8781. dpct::queue_ptr stream) {
  8782. const int nb = k / QK_K;
  8783. {
  8784. dpct::has_capability_or_fail(stream->get_device(),
  8785. {sycl::aspect::fp16});
  8786. stream->submit([&](sycl::handler &cgh) {
  8787. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8788. sycl::range<3>(1, 1, 32),
  8789. sycl::range<3>(1, 1, 32)),
  8790. [=](sycl::nd_item<3> item_ct1) {
  8791. dequantize_block_iq1_s(
  8792. vx, y, item_ct1, iq1s_grid_gpu
  8793. );
  8794. });
  8795. });
  8796. }
  8797. }
  8798. template <typename dst_t>
  8799. static void dequantize_row_iq1_m_sycl(const void *vx, dst_t *y, const int k,
  8800. dpct::queue_ptr stream) {
  8801. const int nb = k / QK_K;
  8802. {
  8803. dpct::has_capability_or_fail(stream->get_device(),
  8804. {sycl::aspect::fp16});
  8805. stream->submit([&](sycl::handler &cgh) {
  8806. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8807. sycl::range<3>(1, 1, 32),
  8808. sycl::range<3>(1, 1, 32)),
  8809. [=](sycl::nd_item<3> item_ct1) {
  8810. dequantize_block_iq1_m(
  8811. vx, y, item_ct1, iq1s_grid_gpu
  8812. );
  8813. });
  8814. });
  8815. }
  8816. }
  8817. template <typename dst_t>
  8818. static void dequantize_row_iq2_xxs_sycl(const void *vx, dst_t *y, const int k,
  8819. dpct::queue_ptr stream) {
  8820. const int nb = k / QK_K;
  8821. {
  8822. dpct::has_capability_or_fail(stream->get_device(),
  8823. {sycl::aspect::fp16});
  8824. stream->submit([&](sycl::handler &cgh) {
  8825. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8826. sycl::range<3>(1, 1, 32),
  8827. sycl::range<3>(1, 1, 32)),
  8828. [=](sycl::nd_item<3> item_ct1) {
  8829. dequantize_block_iq2_xxs(
  8830. vx, y, item_ct1, iq2xxs_grid,
  8831. ksigns_iq2xs, kmask_iq2xs);
  8832. });
  8833. });
  8834. }
  8835. }
  8836. template <typename dst_t>
  8837. static void dequantize_row_iq2_xs_sycl(const void *vx, dst_t *y, const int k,
  8838. dpct::queue_ptr stream) {
  8839. const int nb = k / QK_K;
  8840. {
  8841. dpct::has_capability_or_fail(stream->get_device(),
  8842. {sycl::aspect::fp16});
  8843. stream->submit([&](sycl::handler &cgh) {
  8844. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8845. sycl::range<3>(1, 1, 32),
  8846. sycl::range<3>(1, 1, 32)),
  8847. [=](sycl::nd_item<3> item_ct1) {
  8848. dequantize_block_iq2_xs(
  8849. vx, y, item_ct1, iq2xs_grid,
  8850. ksigns_iq2xs, kmask_iq2xs);
  8851. });
  8852. });
  8853. }
  8854. }
  8855. template <typename dst_t>
  8856. static void dequantize_row_iq2_s_sycl(const void *vx, dst_t *y, const int k,
  8857. dpct::queue_ptr stream) {
  8858. const int nb = k / QK_K;
  8859. {
  8860. dpct::has_capability_or_fail(stream->get_device(),
  8861. {sycl::aspect::fp16});
  8862. stream->submit([&](sycl::handler &cgh) {
  8863. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8864. sycl::range<3>(1, 1, 32),
  8865. sycl::range<3>(1, 1, 32)),
  8866. [=](sycl::nd_item<3> item_ct1) {
  8867. dequantize_block_iq2_s(vx, y, item_ct1);
  8868. });
  8869. });
  8870. }
  8871. }
  8872. template <typename dst_t>
  8873. static void dequantize_row_iq3_xxs_sycl(const void *vx, dst_t *y, const int k,
  8874. dpct::queue_ptr stream) {
  8875. const int nb = k / QK_K;
  8876. {
  8877. dpct::has_capability_or_fail(stream->get_device(),
  8878. {sycl::aspect::fp16});
  8879. stream->submit([&](sycl::handler &cgh) {
  8880. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8881. sycl::range<3>(1, 1, 32),
  8882. sycl::range<3>(1, 1, 32)),
  8883. [=](sycl::nd_item<3> item_ct1) {
  8884. dequantize_block_iq3_xxs(
  8885. vx, y, item_ct1, iq3xxs_grid,
  8886. ksigns_iq2xs, kmask_iq2xs);
  8887. });
  8888. });
  8889. }
  8890. }
  8891. template <typename dst_t>
  8892. static void dequantize_row_iq3_s_sycl(const void *vx, dst_t *y, const int k,
  8893. dpct::queue_ptr stream) {
  8894. const int nb = k / QK_K;
  8895. {
  8896. dpct::has_capability_or_fail(stream->get_device(),
  8897. {sycl::aspect::fp16});
  8898. stream->submit([&](sycl::handler &cgh) {
  8899. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8900. sycl::range<3>(1, 1, 32),
  8901. sycl::range<3>(1, 1, 32)),
  8902. [=](sycl::nd_item<3> item_ct1) {
  8903. dequantize_block_iq3_s(
  8904. vx, y, item_ct1, kmask_iq2xs, iq3s_grid);
  8905. });
  8906. });
  8907. }
  8908. }
  8909. template <typename dst_t>
  8910. static void dequantize_row_iq4_xs_sycl(const void *vx, dst_t *y, const int k,
  8911. dpct::queue_ptr stream) {
  8912. const int nb = (k + QK_K - 1) / QK_K;
  8913. #if QK_K == 64
  8914. dequantize_row_iq4_nl_sycl(vx, y, k, stream);
  8915. #else
  8916. {
  8917. dpct::has_capability_or_fail(stream->get_device(),
  8918. {sycl::aspect::fp16});
  8919. stream->submit([&](sycl::handler &cgh) {
  8920. cgh.parallel_for(
  8921. sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8922. sycl::range<3>(1, 1, 32),
  8923. sycl::range<3>(1, 1, 32)),
  8924. [=](sycl::nd_item<3> item_ct1) {
  8925. dequantize_block_iq4_xs(vx, y, item_ct1);
  8926. });
  8927. });
  8928. }
  8929. #endif
  8930. }
  8931. template <typename dst_t>
  8932. static void dequantize_row_iq4_nl_sycl(const void *vx, dst_t *y, const int k,
  8933. dpct::queue_ptr stream) {
  8934. const int nb = (k + QK_K - 1) / QK_K;
  8935. {
  8936. dpct::has_capability_or_fail(stream->get_device(),
  8937. {sycl::aspect::fp16});
  8938. stream->submit([&](sycl::handler &cgh) {
  8939. cgh.parallel_for(
  8940. sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8941. sycl::range<3>(1, 1, 32),
  8942. sycl::range<3>(1, 1, 32)),
  8943. [=](sycl::nd_item<3> item_ct1) {
  8944. dequantize_block_iq4_nl(vx, y, item_ct1);
  8945. });
  8946. });
  8947. }
  8948. }
  8949. template <typename src_t, typename dst_t>
  8950. static void convert_unary_sycl(const void *__restrict__ vx,
  8951. dst_t *__restrict__ y, const int k,
  8952. dpct::queue_ptr stream) {
  8953. const int num_blocks = (k + SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / SYCL_DEQUANTIZE_BLOCK_SIZE;
  8954. {
  8955. dpct::has_capability_or_fail(stream->get_device(),
  8956. {sycl::aspect::fp16});
  8957. stream->parallel_for(
  8958. sycl::nd_range<3>(
  8959. sycl::range<3>(1, 1, num_blocks) *
  8960. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8961. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8962. [=](sycl::nd_item<3> item_ct1) {
  8963. convert_unary<src_t>(vx, y, k, item_ct1);
  8964. });
  8965. }
  8966. }
  8967. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) try {
  8968. int id;
  8969. switch (type) {
  8970. case GGML_TYPE_Q4_0:
  8971. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  8972. case GGML_TYPE_Q4_1:
  8973. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  8974. case GGML_TYPE_Q5_0:
  8975. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  8976. case GGML_TYPE_Q5_1:
  8977. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  8978. case GGML_TYPE_Q8_0:
  8979. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  8980. case GGML_TYPE_Q2_K:
  8981. return dequantize_row_q2_K_sycl;
  8982. case GGML_TYPE_Q3_K:
  8983. return dequantize_row_q3_K_sycl;
  8984. case GGML_TYPE_Q4_K:
  8985. return dequantize_row_q4_K_sycl;
  8986. case GGML_TYPE_Q5_K:
  8987. return dequantize_row_q5_K_sycl;
  8988. case GGML_TYPE_Q6_K:
  8989. return dequantize_row_q6_K_sycl;
  8990. case GGML_TYPE_IQ1_S:
  8991. return dequantize_row_iq1_s_sycl;
  8992. case GGML_TYPE_IQ1_M:
  8993. return dequantize_row_iq1_m_sycl;
  8994. case GGML_TYPE_IQ2_XXS:
  8995. return dequantize_row_iq2_xxs_sycl;
  8996. case GGML_TYPE_IQ2_XS:
  8997. return dequantize_row_iq2_xs_sycl;
  8998. case GGML_TYPE_IQ2_S:
  8999. return dequantize_row_iq2_s_sycl;
  9000. case GGML_TYPE_IQ3_XXS:
  9001. return dequantize_row_iq3_xxs_sycl;
  9002. case GGML_TYPE_IQ3_S:
  9003. return dequantize_row_iq3_s_sycl;
  9004. case GGML_TYPE_IQ4_XS:
  9005. return dequantize_row_iq4_xs_sycl;
  9006. case GGML_TYPE_IQ4_NL:
  9007. return dequantize_row_iq4_nl_sycl;
  9008. case GGML_TYPE_F32:
  9009. return convert_unary_sycl<float>;
  9010. default:
  9011. return nullptr;
  9012. }
  9013. }
  9014. catch (sycl::exception const &exc) {
  9015. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9016. << ", line:" << __LINE__ << std::endl;
  9017. std::exit(1);
  9018. }
  9019. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  9020. switch (type) {
  9021. case GGML_TYPE_Q4_0:
  9022. return dequantize_row_q4_0_sycl;
  9023. case GGML_TYPE_Q4_1:
  9024. return dequantize_row_q4_1_sycl;
  9025. case GGML_TYPE_Q5_0:
  9026. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  9027. case GGML_TYPE_Q5_1:
  9028. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  9029. case GGML_TYPE_Q8_0:
  9030. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  9031. case GGML_TYPE_Q2_K:
  9032. return dequantize_row_q2_K_sycl;
  9033. case GGML_TYPE_Q3_K:
  9034. return dequantize_row_q3_K_sycl;
  9035. case GGML_TYPE_Q4_K:
  9036. return dequantize_row_q4_K_sycl;
  9037. case GGML_TYPE_Q5_K:
  9038. return dequantize_row_q5_K_sycl;
  9039. case GGML_TYPE_Q6_K:
  9040. return dequantize_row_q6_K_sycl;
  9041. case GGML_TYPE_IQ1_S:
  9042. return dequantize_row_iq1_s_sycl;
  9043. case GGML_TYPE_IQ1_M:
  9044. return dequantize_row_iq1_m_sycl;
  9045. case GGML_TYPE_IQ2_XXS:
  9046. return dequantize_row_iq2_xxs_sycl;
  9047. case GGML_TYPE_IQ2_XS:
  9048. return dequantize_row_iq2_xs_sycl;
  9049. case GGML_TYPE_IQ2_S:
  9050. return dequantize_row_iq2_s_sycl;
  9051. case GGML_TYPE_IQ3_XXS:
  9052. return dequantize_row_iq3_xxs_sycl;
  9053. case GGML_TYPE_IQ3_S:
  9054. return dequantize_row_iq3_s_sycl;
  9055. case GGML_TYPE_IQ4_XS:
  9056. return dequantize_row_iq4_xs_sycl;
  9057. case GGML_TYPE_IQ4_NL:
  9058. return dequantize_row_iq4_nl_sycl;
  9059. case GGML_TYPE_F16:
  9060. return convert_unary_sycl<sycl::half>;
  9061. default:
  9062. return nullptr;
  9063. }
  9064. }
  9065. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  9066. float *dst, const int ncols,
  9067. const int nrows,
  9068. dpct::queue_ptr stream) {
  9069. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9070. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9071. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  9072. const sycl::range<3> block_nums(1, 1, block_num_y);
  9073. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9074. {
  9075. dpct::has_capability_or_fail(stream->get_device(),
  9076. {sycl::aspect::fp16});
  9077. stream->parallel_for(
  9078. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9079. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9080. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  9081. vx, y, dst, ncols, nrows, item_ct1);
  9082. });
  9083. }
  9084. }
  9085. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  9086. float *dst, const int ncols,
  9087. const int nrows,
  9088. dpct::queue_ptr stream) {
  9089. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9090. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9091. const sycl::range<3> block_nums(1, 1, block_num_y);
  9092. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9093. {
  9094. dpct::has_capability_or_fail(stream->get_device(),
  9095. {sycl::aspect::fp16});
  9096. stream->parallel_for(
  9097. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9098. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9099. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  9100. vx, y, dst, ncols, nrows, item_ct1);
  9101. });
  9102. }
  9103. }
  9104. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  9105. float *dst, const int ncols,
  9106. const int nrows,
  9107. dpct::queue_ptr stream) {
  9108. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9109. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9110. const sycl::range<3> block_nums(1, 1, block_num_y);
  9111. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9112. {
  9113. dpct::has_capability_or_fail(stream->get_device(),
  9114. {sycl::aspect::fp16});
  9115. stream->parallel_for(
  9116. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9117. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9118. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  9119. vx, y, dst, ncols, nrows, item_ct1);
  9120. });
  9121. }
  9122. }
  9123. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  9124. float *dst, const int ncols,
  9125. const int nrows,
  9126. dpct::queue_ptr stream) {
  9127. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9128. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9129. const sycl::range<3> block_nums(1, 1, block_num_y);
  9130. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9131. {
  9132. dpct::has_capability_or_fail(stream->get_device(),
  9133. {sycl::aspect::fp16});
  9134. stream->parallel_for(
  9135. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9136. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9137. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  9138. vx, y, dst, ncols, nrows, item_ct1);
  9139. });
  9140. }
  9141. }
  9142. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  9143. float *dst, const int ncols,
  9144. const int nrows,
  9145. dpct::queue_ptr stream) {
  9146. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9147. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9148. const sycl::range<3> block_nums(1, 1, block_num_y);
  9149. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9150. {
  9151. dpct::has_capability_or_fail(stream->get_device(),
  9152. {sycl::aspect::fp16});
  9153. stream->parallel_for(
  9154. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9155. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9156. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  9157. vx, y, dst, ncols, nrows, item_ct1);
  9158. });
  9159. }
  9160. }
  9161. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  9162. float *dst, const int ncols,
  9163. const int nrows,
  9164. dpct::queue_ptr stream) {
  9165. GGML_ASSERT(ncols % QK_K == 0);
  9166. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  9167. const int block_num_y = (nrows + ny - 1) / ny;
  9168. const sycl::range<3> block_nums(1, 1, block_num_y);
  9169. const sycl::range<3> block_dims(1, ny, 32);
  9170. stream->parallel_for(
  9171. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9172. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9173. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  9174. });
  9175. }
  9176. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  9177. float *dst, const int ncols,
  9178. const int nrows,
  9179. dpct::queue_ptr stream) {
  9180. GGML_ASSERT(ncols % QK_K == 0);
  9181. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9182. const int block_num_y = (nrows + ny - 1) / ny;
  9183. const sycl::range<3> block_nums(1, 1, block_num_y);
  9184. const sycl::range<3> block_dims(1, ny, 32);
  9185. stream->parallel_for(
  9186. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9187. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9188. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  9189. });
  9190. }
  9191. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  9192. float *dst, const int ncols,
  9193. const int nrows,
  9194. dpct::queue_ptr stream) {
  9195. GGML_ASSERT(ncols % QK_K == 0);
  9196. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9197. const int block_num_y = (nrows + ny - 1) / ny;
  9198. const sycl::range<3> block_nums(1, 1, block_num_y);
  9199. const sycl::range<3> block_dims(1, ny, 32);
  9200. stream->parallel_for(
  9201. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9202. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9203. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  9204. });
  9205. }
  9206. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  9207. float *dst, const int ncols,
  9208. const int nrows,
  9209. dpct::queue_ptr stream) {
  9210. GGML_ASSERT(ncols % QK_K == 0);
  9211. const sycl::range<3> block_dims(1, 1, 32);
  9212. stream->parallel_for(
  9213. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  9214. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9215. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  9216. });
  9217. }
  9218. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  9219. float *dst, const int ncols,
  9220. const int nrows,
  9221. dpct::queue_ptr stream) {
  9222. GGML_ASSERT(ncols % QK_K == 0);
  9223. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9224. const int block_num_y = (nrows + ny - 1) / ny;
  9225. const sycl::range<3> block_nums(1, 1, block_num_y);
  9226. const sycl::range<3> block_dims(1, ny, 32);
  9227. stream->parallel_for(
  9228. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9229. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9230. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  9231. });
  9232. }
  9233. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  9234. float *dst, const int ncols,
  9235. const int nrows,
  9236. dpct::queue_ptr stream) {
  9237. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9238. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9239. const sycl::range<3> block_nums(1, 1, block_num_y);
  9240. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9241. {
  9242. dpct::has_capability_or_fail(stream->get_device(),
  9243. {sycl::aspect::fp16});
  9244. stream->parallel_for(
  9245. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9246. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9247. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  9248. nrows, item_ct1);
  9249. });
  9250. }
  9251. }
  9252. static void mul_mat_vec_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9253. float *dst, const int ncols,
  9254. const int nrows,
  9255. dpct::queue_ptr stream) {
  9256. GGML_ASSERT(ncols % QK4_0 == 0);
  9257. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9258. const sycl::range<3> block_nums(1, 1, block_num_y);
  9259. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9260. {
  9261. stream->submit([&](sycl::handler &cgh) {
  9262. cgh.parallel_for(
  9263. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9264. [=](sycl::nd_item<3> item_ct1)
  9265. [[intel::reqd_sub_group_size(32)]] {
  9266. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0,
  9267. VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>(
  9268. vx, vy, dst, ncols, nrows, item_ct1);
  9269. });
  9270. });
  9271. }
  9272. }
  9273. static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9274. float *dst, const int ncols,
  9275. const int nrows,
  9276. dpct::queue_ptr stream) {
  9277. GGML_ASSERT(ncols % QK4_1 == 0);
  9278. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9279. const sycl::range<3> block_nums(1, 1, block_num_y);
  9280. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9281. {
  9282. stream->submit([&](sycl::handler &cgh) {
  9283. cgh.parallel_for(
  9284. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9285. [=](sycl::nd_item<3> item_ct1)
  9286. [[intel::reqd_sub_group_size(32)]] {
  9287. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1,
  9288. VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>(
  9289. vx, vy, dst, ncols, nrows, item_ct1);
  9290. });
  9291. });
  9292. }
  9293. }
  9294. static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9295. float *dst, const int ncols,
  9296. const int nrows,
  9297. dpct::queue_ptr stream) {
  9298. GGML_ASSERT(ncols % QK5_0 == 0);
  9299. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9300. const sycl::range<3> block_nums(1, 1, block_num_y);
  9301. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9302. {
  9303. stream->submit([&](sycl::handler &cgh) {
  9304. cgh.parallel_for(
  9305. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9306. [=](sycl::nd_item<3> item_ct1)
  9307. [[intel::reqd_sub_group_size(32)]] {
  9308. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0,
  9309. VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>(
  9310. vx, vy, dst, ncols, nrows, item_ct1);
  9311. });
  9312. });
  9313. }
  9314. }
  9315. static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9316. float *dst, const int ncols,
  9317. const int nrows,
  9318. dpct::queue_ptr stream) {
  9319. GGML_ASSERT(ncols % QK5_1 == 0);
  9320. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9321. const sycl::range<3> block_nums(1, 1, block_num_y);
  9322. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9323. {
  9324. stream->submit([&](sycl::handler &cgh) {
  9325. cgh.parallel_for(
  9326. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9327. [=](sycl::nd_item<3> item_ct1)
  9328. [[intel::reqd_sub_group_size(32)]] {
  9329. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1,
  9330. VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>(
  9331. vx, vy, dst, ncols, nrows, item_ct1);
  9332. });
  9333. });
  9334. }
  9335. }
  9336. static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
  9337. float *dst, const int ncols,
  9338. const int nrows,
  9339. dpct::queue_ptr stream) {
  9340. GGML_ASSERT(ncols % QK8_0 == 0);
  9341. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9342. const sycl::range<3> block_nums(1, 1, block_num_y);
  9343. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9344. {
  9345. stream->submit([&](sycl::handler &cgh) {
  9346. cgh.parallel_for(
  9347. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9348. [=](sycl::nd_item<3> item_ct1)
  9349. [[intel::reqd_sub_group_size(32)]] {
  9350. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0,
  9351. VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>(
  9352. vx, vy, dst, ncols, nrows, item_ct1);
  9353. });
  9354. });
  9355. }
  9356. }
  9357. static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
  9358. float *dst, const int ncols,
  9359. const int nrows,
  9360. dpct::queue_ptr stream) {
  9361. GGML_ASSERT(ncols % QK_K == 0);
  9362. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9363. const sycl::range<3> block_nums(1, 1, block_num_y);
  9364. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9365. {
  9366. stream->submit([&](sycl::handler &cgh) {
  9367. cgh.parallel_for(
  9368. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9369. [=](sycl::nd_item<3> item_ct1)
  9370. [[intel::reqd_sub_group_size(32)]] {
  9371. mul_mat_vec_q<QK_K, QI2_K, block_q2_K,
  9372. VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>(
  9373. vx, vy, dst, ncols, nrows, item_ct1);
  9374. });
  9375. });
  9376. }
  9377. }
  9378. static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
  9379. float *dst, const int ncols,
  9380. const int nrows,
  9381. dpct::queue_ptr stream) {
  9382. GGML_ASSERT(ncols % QK_K == 0);
  9383. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9384. const sycl::range<3> block_nums(1, 1, block_num_y);
  9385. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9386. {
  9387. stream->submit([&](sycl::handler &cgh) {
  9388. cgh.parallel_for(
  9389. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9390. [=](sycl::nd_item<3> item_ct1)
  9391. [[intel::reqd_sub_group_size(32)]] {
  9392. mul_mat_vec_q<QK_K, QI3_K, block_q3_K,
  9393. VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>(
  9394. vx, vy, dst, ncols, nrows, item_ct1);
  9395. });
  9396. });
  9397. }
  9398. }
  9399. static void mul_mat_vec_q4_K_q8_1_sycl(const void *vx, const void *vy,
  9400. float *dst, const int ncols,
  9401. const int nrows,
  9402. dpct::queue_ptr stream) {
  9403. GGML_ASSERT(ncols % QK_K == 0);
  9404. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9405. const sycl::range<3> block_nums(1, 1, block_num_y);
  9406. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9407. {
  9408. stream->submit([&](sycl::handler &cgh) {
  9409. cgh.parallel_for(
  9410. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9411. [=](sycl::nd_item<3> item_ct1)
  9412. [[intel::reqd_sub_group_size(32)]] {
  9413. mul_mat_vec_q<QK_K, QI4_K, block_q4_K,
  9414. VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>(
  9415. vx, vy, dst, ncols, nrows, item_ct1);
  9416. });
  9417. });
  9418. }
  9419. }
  9420. static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
  9421. float *dst, const int ncols,
  9422. const int nrows,
  9423. dpct::queue_ptr stream) {
  9424. GGML_ASSERT(ncols % QK_K == 0);
  9425. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9426. const sycl::range<3> block_nums(1, 1, block_num_y);
  9427. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9428. {
  9429. stream->submit([&](sycl::handler &cgh) {
  9430. cgh.parallel_for(
  9431. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9432. [=](sycl::nd_item<3> item_ct1)
  9433. [[intel::reqd_sub_group_size(32)]] {
  9434. mul_mat_vec_q<QK_K, QI5_K, block_q5_K,
  9435. VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
  9436. vx, vy, dst, ncols, nrows, item_ct1);
  9437. });
  9438. });
  9439. }
  9440. }
  9441. static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
  9442. float *dst, const int ncols,
  9443. const int nrows,
  9444. dpct::queue_ptr stream) {
  9445. GGML_ASSERT(ncols % QK_K == 0);
  9446. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9447. const sycl::range<3> block_nums(1, 1, block_num_y);
  9448. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9449. {
  9450. stream->submit([&](sycl::handler &cgh) {
  9451. cgh.parallel_for(
  9452. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9453. [=](sycl::nd_item<3> item_ct1)
  9454. [[intel::reqd_sub_group_size(32)]] {
  9455. mul_mat_vec_q<QK_K, QI6_K, block_q6_K,
  9456. VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>(
  9457. vx, vy, dst, ncols, nrows, item_ct1);
  9458. });
  9459. });
  9460. }
  9461. }
  9462. static void mul_mat_vec_iq2_xxs_q8_1_sycl(const void *vx, const void *vy,
  9463. float *dst, const int ncols,
  9464. const int nrows,
  9465. dpct::queue_ptr stream) {
  9466. GGML_ASSERT(ncols % QK_K == 0);
  9467. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9468. const sycl::range<3> block_nums(1, 1, block_num_y);
  9469. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9470. {
  9471. stream->submit([&](sycl::handler &cgh) {
  9472. cgh.parallel_for(
  9473. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9474. [=](sycl::nd_item<3> item_ct1)
  9475. [[intel::reqd_sub_group_size(32)]] {
  9476. mul_mat_vec_q_iq2_xxs_q8_1<QK_K, QI2_XXS, block_iq2_xxs, 1>(
  9477. vx, vy, dst, ncols, nrows, item_ct1);
  9478. });
  9479. });
  9480. }
  9481. }
  9482. static void mul_mat_vec_iq2_xs_q8_1_sycl(const void *vx, const void *vy,
  9483. float *dst, const int ncols,
  9484. const int nrows,
  9485. dpct::queue_ptr stream) {
  9486. GGML_ASSERT(ncols % QK_K == 0);
  9487. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9488. const sycl::range<3> block_nums(1, 1, block_num_y);
  9489. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9490. {
  9491. stream->submit([&](sycl::handler &cgh) {
  9492. auto iq2xs_grid_ptr_ct1 = &iq2xs_grid[0];
  9493. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9494. cgh.parallel_for(
  9495. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9496. [=](sycl::nd_item<3> item_ct1)
  9497. [[intel::reqd_sub_group_size(32)]] {
  9498. mul_mat_vec_q_iq2_xs_q8_1<QK_K, QI2_XS, block_iq2_xs, 1>(
  9499. vx, vy, dst, ncols, nrows, item_ct1);
  9500. });
  9501. });
  9502. }
  9503. }
  9504. static void mul_mat_vec_iq2_s_q8_1_sycl(const void *vx, const void *vy,
  9505. float *dst, const int ncols,
  9506. const int nrows,
  9507. dpct::queue_ptr stream) {
  9508. GGML_ASSERT(ncols % QK_K == 0);
  9509. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9510. const sycl::range<3> block_nums(1, 1, block_num_y);
  9511. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9512. {
  9513. stream->submit([&](sycl::handler &cgh) {
  9514. auto iq2xs_grid_ptr_ct1 = &iq2xs_grid[0];
  9515. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9516. cgh.parallel_for(
  9517. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9518. [=](sycl::nd_item<3> item_ct1)
  9519. [[intel::reqd_sub_group_size(32)]] {
  9520. mul_mat_vec_q_iq2_s_q8_1<QK_K, QI2_S, block_iq2_s, 1>(
  9521. vx, vy, dst, ncols, nrows, item_ct1);
  9522. });
  9523. });
  9524. }
  9525. }
  9526. static void mul_mat_vec_iq3_xxs_q8_1_sycl(const void *vx, const void *vy,
  9527. float *dst, const int ncols,
  9528. const int nrows,
  9529. dpct::queue_ptr stream) {
  9530. GGML_ASSERT(ncols % QK_K == 0);
  9531. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9532. const sycl::range<3> block_nums(1, 1, block_num_y);
  9533. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9534. {
  9535. stream->submit([&](sycl::handler &cgh) {
  9536. auto iq3xxs_grid_ptr_ct1 = &iq3xxs_grid[0];
  9537. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9538. cgh.parallel_for(
  9539. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9540. [=](sycl::nd_item<3> item_ct1)
  9541. [[intel::reqd_sub_group_size(32)]] {
  9542. mul_mat_vec_q_iq3_xxs_q8_1<QK_K, QI3_XXS, block_iq3_xxs, 1>(
  9543. vx, vy, dst, ncols, nrows, item_ct1);
  9544. });
  9545. });
  9546. }
  9547. }
  9548. static void mul_mat_vec_iq3_s_q8_1_sycl(const void *vx, const void *vy,
  9549. float *dst, const int ncols,
  9550. const int nrows,
  9551. dpct::queue_ptr stream) {
  9552. GGML_ASSERT(ncols % QK_K == 0);
  9553. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9554. const sycl::range<3> block_nums(1, 1, block_num_y);
  9555. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9556. {
  9557. stream->submit([&](sycl::handler &cgh) {
  9558. auto iq3s_grid_ptr_ct1 = &iq3s_grid[0];
  9559. cgh.parallel_for(
  9560. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9561. [=](sycl::nd_item<3> item_ct1)
  9562. [[intel::reqd_sub_group_size(32)]] {
  9563. mul_mat_vec_q_iq3_s_q8_1<QK_K, QI3_XS, block_iq3_s, 1>(
  9564. vx, vy, dst, ncols, nrows, item_ct1);
  9565. });
  9566. });
  9567. }
  9568. }
  9569. static void mul_mat_vec_iq1_s_q8_1_sycl(const void *vx, const void *vy,
  9570. float *dst, const int ncols,
  9571. const int nrows,
  9572. dpct::queue_ptr stream) {
  9573. GGML_ASSERT(ncols % QK_K == 0);
  9574. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9575. const sycl::range<3> block_nums(1, 1, block_num_y);
  9576. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9577. {
  9578. stream->submit([&](sycl::handler &cgh) {
  9579. auto iq1s_grid_ptr_ct1 = &iq1s_grid_gpu[0];
  9580. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9581. cgh.parallel_for(
  9582. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9583. [=](sycl::nd_item<3> item_ct1)
  9584. [[intel::reqd_sub_group_size(32)]] {
  9585. mul_mat_vec_q_iq1_s_q8_1<QK_K, QI1_S, block_iq1_s, 1>(
  9586. vx, vy, dst, ncols, nrows, item_ct1);
  9587. });
  9588. });
  9589. }
  9590. }
  9591. static void mul_mat_vec_iq1_m_q8_1_sycl(const void *vx, const void *vy,
  9592. float *dst, const int ncols,
  9593. const int nrows,
  9594. dpct::queue_ptr stream) {
  9595. GGML_ASSERT(ncols % QK_K == 0);
  9596. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9597. const sycl::range<3> block_nums(1, 1, block_num_y);
  9598. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9599. {
  9600. stream->submit([&](sycl::handler &cgh) {
  9601. cgh.parallel_for(
  9602. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9603. [=](sycl::nd_item<3> item_ct1)
  9604. [[intel::reqd_sub_group_size(32)]] {
  9605. mul_mat_vec_q_iq1_m_q8_1<QK_K, QI1_S, block_iq1_m, 1>(
  9606. vx, vy, dst, ncols, nrows, item_ct1);
  9607. });
  9608. });
  9609. }
  9610. }
  9611. static void mul_mat_vec_iq4_nl_q8_1_sycl(const void *vx, const void *vy,
  9612. float *dst, const int ncols,
  9613. const int nrows,
  9614. dpct::queue_ptr stream) {
  9615. GGML_ASSERT(ncols % QK4_NL == 0);
  9616. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9617. const sycl::range<3> block_nums(1, 1, block_num_y);
  9618. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9619. {
  9620. stream->submit([&](sycl::handler &cgh) {
  9621. cgh.parallel_for(
  9622. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9623. [=](sycl::nd_item<3> item_ct1)
  9624. [[intel::reqd_sub_group_size(32)]] {
  9625. mul_mat_vec_q_iq4_nl_q8_1<QK4_NL, QI4_NL, block_iq4_nl, 1>(
  9626. vx, vy, dst, ncols, nrows, item_ct1);
  9627. });
  9628. });
  9629. }
  9630. }
  9631. static void mul_mat_vec_iq4_xs_q8_1_sycl(const void *vx, const void *vy,
  9632. float *dst, const int ncols,
  9633. const int nrows,
  9634. dpct::queue_ptr stream) {
  9635. GGML_ASSERT(ncols % QK_K == 0);
  9636. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9637. const sycl::range<3> block_nums(1, 1, block_num_y);
  9638. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9639. {
  9640. stream->submit([&](sycl::handler &cgh) {
  9641. cgh.parallel_for(
  9642. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9643. [=](sycl::nd_item<3> item_ct1)
  9644. [[intel::reqd_sub_group_size(32)]] {
  9645. mul_mat_vec_q_iq4_xs_q8_1<QK_K, QI4_XS, block_iq4_xs, 1>(
  9646. vx, vy, dst, ncols, nrows, item_ct1);
  9647. });
  9648. });
  9649. }
  9650. }
  9651. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9652. float *dst, const int ncols_x,
  9653. const int nrows_x, const int ncols_y,
  9654. const int nrows_y, const int nrows_dst,
  9655. dpct::queue_ptr stream) try {
  9656. int id;
  9657. SYCL_CHECK(
  9658. CHECK_TRY_ERROR(id = get_current_device_id()));
  9659. const int compute_capability = g_device_caps[id].cc;
  9660. int mmq_x, mmq_y, nwarps;
  9661. if (compute_capability >= VER_GEN13) {
  9662. mmq_x = MMQ_X_Q4_0_RDNA2;
  9663. mmq_y = MMQ_Y_Q4_0_RDNA2;
  9664. nwarps = NWARPS_Q4_0_RDNA2;
  9665. } else if (compute_capability >= VER_GEN12) {
  9666. mmq_x = MMQ_X_Q4_0_RDNA1;
  9667. mmq_y = MMQ_Y_Q4_0_RDNA1;
  9668. nwarps = NWARPS_Q4_0_RDNA1;
  9669. } else if (compute_capability >= VER_GEN9) {
  9670. mmq_x = MMQ_X_Q4_0_AMPERE;
  9671. mmq_y = MMQ_Y_Q4_0_AMPERE;
  9672. nwarps = NWARPS_Q4_0_AMPERE;
  9673. } else if (compute_capability >= VER_4VEC) {
  9674. mmq_x = MMQ_X_Q4_0_PASCAL;
  9675. mmq_y = MMQ_Y_Q4_0_PASCAL;
  9676. nwarps = NWARPS_Q4_0_PASCAL;
  9677. } else {
  9678. GGML_ASSERT(false);
  9679. }
  9680. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9681. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9682. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9683. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9684. if (nrows_x % mmq_y == 0) {
  9685. const bool need_check = false;
  9686. /*
  9687. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  9688. the limit. To get the device limit, query
  9689. info::device::max_work_group_size. Adjust the work-group size if needed.
  9690. */
  9691. {
  9692. dpct::has_capability_or_fail(stream->get_device(),
  9693. {sycl::aspect::fp16});
  9694. stream->submit([&](sycl::handler &cgh) {
  9695. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9696. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9697. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9698. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9699. cgh);
  9700. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9701. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9702. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9703. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9704. cgh.parallel_for(
  9705. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9706. [=](sycl::nd_item<3> item_ct1) {
  9707. mul_mat_q4_0<need_check>(
  9708. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9709. nrows_dst, item_ct1,
  9710. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9711. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9712. tile_y_qs_acc_ct1.get_pointer(),
  9713. tile_y_ds_acc_ct1.get_pointer());
  9714. });
  9715. });
  9716. }
  9717. } else {
  9718. const bool need_check = true;
  9719. /*
  9720. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  9721. the limit. To get the device limit, query
  9722. info::device::max_work_group_size. Adjust the work-group size if needed.
  9723. */
  9724. {
  9725. dpct::has_capability_or_fail(stream->get_device(),
  9726. {sycl::aspect::fp16});
  9727. stream->submit([&](sycl::handler &cgh) {
  9728. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9729. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9730. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9731. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9732. cgh);
  9733. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9734. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9735. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9736. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9737. cgh.parallel_for(
  9738. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9739. [=](sycl::nd_item<3> item_ct1) {
  9740. mul_mat_q4_0<need_check>(
  9741. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9742. nrows_dst, item_ct1,
  9743. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9744. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9745. tile_y_qs_acc_ct1.get_pointer(),
  9746. tile_y_ds_acc_ct1.get_pointer());
  9747. });
  9748. });
  9749. }
  9750. }
  9751. }
  9752. catch (sycl::exception const &exc) {
  9753. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9754. << ", line:" << __LINE__ << std::endl;
  9755. std::exit(1);
  9756. }
  9757. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9758. float *dst, const int ncols_x,
  9759. const int nrows_x, const int ncols_y,
  9760. const int nrows_y, const int nrows_dst,
  9761. dpct::queue_ptr stream) try {
  9762. int id;
  9763. SYCL_CHECK(
  9764. CHECK_TRY_ERROR(id = get_current_device_id()));
  9765. const int compute_capability = g_device_caps[id].cc;
  9766. int mmq_x, mmq_y, nwarps;
  9767. if (compute_capability >= VER_GEN13) {
  9768. mmq_x = MMQ_X_Q4_1_RDNA2;
  9769. mmq_y = MMQ_Y_Q4_1_RDNA2;
  9770. nwarps = NWARPS_Q4_1_RDNA2;
  9771. } else if (compute_capability >= VER_GEN12) {
  9772. mmq_x = MMQ_X_Q4_1_RDNA1;
  9773. mmq_y = MMQ_Y_Q4_1_RDNA1;
  9774. nwarps = NWARPS_Q4_1_RDNA1;
  9775. } else if (compute_capability >= VER_GEN9) {
  9776. mmq_x = MMQ_X_Q4_1_AMPERE;
  9777. mmq_y = MMQ_Y_Q4_1_AMPERE;
  9778. nwarps = NWARPS_Q4_1_AMPERE;
  9779. } else if (compute_capability >= VER_4VEC) {
  9780. mmq_x = MMQ_X_Q4_1_PASCAL;
  9781. mmq_y = MMQ_Y_Q4_1_PASCAL;
  9782. nwarps = NWARPS_Q4_1_PASCAL;
  9783. } else {
  9784. GGML_ASSERT(false);
  9785. }
  9786. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9787. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9788. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9789. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9790. if (nrows_x % mmq_y == 0) {
  9791. const bool need_check = false;
  9792. /*
  9793. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  9794. the limit. To get the device limit, query
  9795. info::device::max_work_group_size. Adjust the work-group size if needed.
  9796. */
  9797. {
  9798. dpct::has_capability_or_fail(stream->get_device(),
  9799. {sycl::aspect::fp16});
  9800. stream->submit([&](sycl::handler &cgh) {
  9801. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9802. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9803. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9804. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9805. cgh);
  9806. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9807. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9808. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9809. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9810. cgh.parallel_for(
  9811. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9812. [=](sycl::nd_item<3> item_ct1) {
  9813. mul_mat_q4_1<need_check>(
  9814. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9815. nrows_dst, item_ct1,
  9816. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9817. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9818. tile_y_qs_acc_ct1.get_pointer(),
  9819. tile_y_ds_acc_ct1.get_pointer());
  9820. });
  9821. });
  9822. }
  9823. } else {
  9824. const bool need_check = true;
  9825. /*
  9826. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  9827. the limit. To get the device limit, query
  9828. info::device::max_work_group_size. Adjust the work-group size if needed.
  9829. */
  9830. {
  9831. dpct::has_capability_or_fail(stream->get_device(),
  9832. {sycl::aspect::fp16});
  9833. stream->submit([&](sycl::handler &cgh) {
  9834. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9835. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9836. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9837. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9838. cgh);
  9839. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9840. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9841. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9842. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9843. cgh.parallel_for(
  9844. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9845. [=](sycl::nd_item<3> item_ct1) {
  9846. mul_mat_q4_1<need_check>(
  9847. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9848. nrows_dst, item_ct1,
  9849. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9850. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9851. tile_y_qs_acc_ct1.get_pointer(),
  9852. tile_y_ds_acc_ct1.get_pointer());
  9853. });
  9854. });
  9855. }
  9856. }
  9857. }
  9858. catch (sycl::exception const &exc) {
  9859. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9860. << ", line:" << __LINE__ << std::endl;
  9861. std::exit(1);
  9862. }
  9863. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9864. float *dst, const int ncols_x,
  9865. const int nrows_x, const int ncols_y,
  9866. const int nrows_y, const int nrows_dst,
  9867. dpct::queue_ptr stream) try {
  9868. int id;
  9869. SYCL_CHECK(
  9870. CHECK_TRY_ERROR(id = get_current_device_id()));
  9871. const int compute_capability = g_device_caps[id].cc;
  9872. int mmq_x, mmq_y, nwarps;
  9873. if (compute_capability >= VER_GEN13) {
  9874. mmq_x = MMQ_X_Q5_0_RDNA2;
  9875. mmq_y = MMQ_Y_Q5_0_RDNA2;
  9876. nwarps = NWARPS_Q5_0_RDNA2;
  9877. } else if (compute_capability >= VER_GEN12) {
  9878. mmq_x = MMQ_X_Q5_0_RDNA1;
  9879. mmq_y = MMQ_Y_Q5_0_RDNA1;
  9880. nwarps = NWARPS_Q5_0_RDNA1;
  9881. } else if (compute_capability >= VER_GEN9) {
  9882. mmq_x = MMQ_X_Q5_0_AMPERE;
  9883. mmq_y = MMQ_Y_Q5_0_AMPERE;
  9884. nwarps = NWARPS_Q5_0_AMPERE;
  9885. } else if (compute_capability >= VER_4VEC) {
  9886. mmq_x = MMQ_X_Q5_0_PASCAL;
  9887. mmq_y = MMQ_Y_Q5_0_PASCAL;
  9888. nwarps = NWARPS_Q5_0_PASCAL;
  9889. } else {
  9890. GGML_ASSERT(false);
  9891. }
  9892. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9893. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9894. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9895. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9896. if (nrows_x % mmq_y == 0) {
  9897. const bool need_check = false;
  9898. /*
  9899. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  9900. the limit. To get the device limit, query
  9901. info::device::max_work_group_size. Adjust the work-group size if needed.
  9902. */
  9903. {
  9904. dpct::has_capability_or_fail(stream->get_device(),
  9905. {sycl::aspect::fp16});
  9906. stream->submit([&](sycl::handler &cgh) {
  9907. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9908. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9909. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9910. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9911. cgh);
  9912. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9913. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9914. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9915. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9916. cgh.parallel_for(
  9917. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9918. [=](sycl::nd_item<3> item_ct1) {
  9919. mul_mat_q5_0<need_check>(
  9920. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9921. nrows_dst, item_ct1,
  9922. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9923. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9924. tile_y_qs_acc_ct1.get_pointer(),
  9925. tile_y_ds_acc_ct1.get_pointer());
  9926. });
  9927. });
  9928. }
  9929. } else {
  9930. const bool need_check = true;
  9931. /*
  9932. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  9933. the limit. To get the device limit, query
  9934. info::device::max_work_group_size. Adjust the work-group size if needed.
  9935. */
  9936. {
  9937. dpct::has_capability_or_fail(stream->get_device(),
  9938. {sycl::aspect::fp16});
  9939. stream->submit([&](sycl::handler &cgh) {
  9940. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9941. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9942. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9943. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9944. cgh);
  9945. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9946. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9947. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9948. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9949. cgh.parallel_for(
  9950. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9951. [=](sycl::nd_item<3> item_ct1) {
  9952. mul_mat_q5_0<need_check>(
  9953. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9954. nrows_dst, item_ct1,
  9955. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9956. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9957. tile_y_qs_acc_ct1.get_pointer(),
  9958. tile_y_ds_acc_ct1.get_pointer());
  9959. });
  9960. });
  9961. }
  9962. }
  9963. }
  9964. catch (sycl::exception const &exc) {
  9965. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9966. << ", line:" << __LINE__ << std::endl;
  9967. std::exit(1);
  9968. }
  9969. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9970. float *dst, const int ncols_x,
  9971. const int nrows_x, const int ncols_y,
  9972. const int nrows_y, const int nrows_dst,
  9973. dpct::queue_ptr stream) try {
  9974. int id;
  9975. SYCL_CHECK(
  9976. CHECK_TRY_ERROR(id = get_current_device_id()));
  9977. const int compute_capability = g_device_caps[id].cc;
  9978. int mmq_x, mmq_y, nwarps;
  9979. if (compute_capability >= VER_GEN13) {
  9980. mmq_x = MMQ_X_Q5_1_RDNA2;
  9981. mmq_y = MMQ_Y_Q5_1_RDNA2;
  9982. nwarps = NWARPS_Q5_1_RDNA2;
  9983. } else if (compute_capability >= VER_GEN12) {
  9984. mmq_x = MMQ_X_Q5_1_RDNA1;
  9985. mmq_y = MMQ_Y_Q5_1_RDNA1;
  9986. nwarps = NWARPS_Q5_1_RDNA1;
  9987. } else if (compute_capability >= VER_GEN9) {
  9988. mmq_x = MMQ_X_Q5_1_AMPERE;
  9989. mmq_y = MMQ_Y_Q5_1_AMPERE;
  9990. nwarps = NWARPS_Q5_1_AMPERE;
  9991. } else if (compute_capability >= VER_4VEC) {
  9992. mmq_x = MMQ_X_Q5_1_PASCAL;
  9993. mmq_y = MMQ_Y_Q5_1_PASCAL;
  9994. nwarps = NWARPS_Q5_1_PASCAL;
  9995. } else {
  9996. GGML_ASSERT(false);
  9997. }
  9998. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9999. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10000. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10001. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10002. if (nrows_x % mmq_y == 0) {
  10003. const bool need_check = false;
  10004. /*
  10005. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  10006. the limit. To get the device limit, query
  10007. info::device::max_work_group_size. Adjust the work-group size if needed.
  10008. */
  10009. {
  10010. dpct::has_capability_or_fail(stream->get_device(),
  10011. {sycl::aspect::fp16});
  10012. stream->submit([&](sycl::handler &cgh) {
  10013. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  10014. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10015. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  10016. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  10017. cgh);
  10018. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10019. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10020. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10021. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10022. cgh.parallel_for(
  10023. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10024. [=](sycl::nd_item<3> item_ct1) {
  10025. mul_mat_q5_1<need_check>(
  10026. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10027. nrows_dst, item_ct1,
  10028. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  10029. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  10030. tile_y_qs_acc_ct1.get_pointer(),
  10031. tile_y_ds_acc_ct1.get_pointer());
  10032. });
  10033. });
  10034. }
  10035. } else {
  10036. const bool need_check = true;
  10037. /*
  10038. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  10039. the limit. To get the device limit, query
  10040. info::device::max_work_group_size. Adjust the work-group size if needed.
  10041. */
  10042. {
  10043. dpct::has_capability_or_fail(stream->get_device(),
  10044. {sycl::aspect::fp16});
  10045. stream->submit([&](sycl::handler &cgh) {
  10046. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  10047. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10048. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  10049. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  10050. cgh);
  10051. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10052. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10053. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10054. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10055. cgh.parallel_for(
  10056. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10057. [=](sycl::nd_item<3> item_ct1) {
  10058. mul_mat_q5_1<need_check>(
  10059. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10060. nrows_dst, item_ct1,
  10061. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  10062. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  10063. tile_y_qs_acc_ct1.get_pointer(),
  10064. tile_y_ds_acc_ct1.get_pointer());
  10065. });
  10066. });
  10067. }
  10068. }
  10069. }
  10070. catch (sycl::exception const &exc) {
  10071. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10072. << ", line:" << __LINE__ << std::endl;
  10073. std::exit(1);
  10074. }
  10075. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  10076. float *dst, const int ncols_x,
  10077. const int nrows_x, const int ncols_y,
  10078. const int nrows_y, const int nrows_dst,
  10079. dpct::queue_ptr stream) try {
  10080. int id;
  10081. SYCL_CHECK(
  10082. CHECK_TRY_ERROR(id = get_current_device_id()));
  10083. const int compute_capability = g_device_caps[id].cc;
  10084. int mmq_x, mmq_y, nwarps;
  10085. if (compute_capability >= VER_GEN13) {
  10086. mmq_x = MMQ_X_Q8_0_RDNA2;
  10087. mmq_y = MMQ_Y_Q8_0_RDNA2;
  10088. nwarps = NWARPS_Q8_0_RDNA2;
  10089. } else if (compute_capability >= VER_GEN12) {
  10090. mmq_x = MMQ_X_Q8_0_RDNA1;
  10091. mmq_y = MMQ_Y_Q8_0_RDNA1;
  10092. nwarps = NWARPS_Q8_0_RDNA1;
  10093. } else if (compute_capability >= VER_GEN9) {
  10094. mmq_x = MMQ_X_Q8_0_AMPERE;
  10095. mmq_y = MMQ_Y_Q8_0_AMPERE;
  10096. nwarps = NWARPS_Q8_0_AMPERE;
  10097. } else if (compute_capability >= VER_4VEC) {
  10098. mmq_x = MMQ_X_Q8_0_PASCAL;
  10099. mmq_y = MMQ_Y_Q8_0_PASCAL;
  10100. nwarps = NWARPS_Q8_0_PASCAL;
  10101. } else {
  10102. GGML_ASSERT(false);
  10103. }
  10104. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10105. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10106. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10107. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10108. if (nrows_x % mmq_y == 0) {
  10109. const bool need_check = false;
  10110. /*
  10111. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  10112. the limit. To get the device limit, query
  10113. info::device::max_work_group_size. Adjust the work-group size if needed.
  10114. */
  10115. {
  10116. dpct::has_capability_or_fail(stream->get_device(),
  10117. {sycl::aspect::fp16});
  10118. stream->submit([&](sycl::handler &cgh) {
  10119. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  10120. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10121. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  10122. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  10123. cgh);
  10124. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10125. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10126. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10127. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10128. cgh.parallel_for(
  10129. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10130. [=](sycl::nd_item<3> item_ct1) {
  10131. mul_mat_q8_0<need_check>(
  10132. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10133. nrows_dst, item_ct1,
  10134. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  10135. tile_x_d_q8_0_acc_ct1.get_pointer(),
  10136. tile_y_qs_acc_ct1.get_pointer(),
  10137. tile_y_ds_acc_ct1.get_pointer());
  10138. });
  10139. });
  10140. }
  10141. } else {
  10142. const bool need_check = true;
  10143. /*
  10144. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  10145. the limit. To get the device limit, query
  10146. info::device::max_work_group_size. Adjust the work-group size if needed.
  10147. */
  10148. {
  10149. dpct::has_capability_or_fail(stream->get_device(),
  10150. {sycl::aspect::fp16});
  10151. stream->submit([&](sycl::handler &cgh) {
  10152. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  10153. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10154. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  10155. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  10156. cgh);
  10157. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10158. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10159. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10160. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10161. cgh.parallel_for(
  10162. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10163. [=](sycl::nd_item<3> item_ct1) {
  10164. mul_mat_q8_0<need_check>(
  10165. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10166. nrows_dst, item_ct1,
  10167. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  10168. tile_x_d_q8_0_acc_ct1.get_pointer(),
  10169. tile_y_qs_acc_ct1.get_pointer(),
  10170. tile_y_ds_acc_ct1.get_pointer());
  10171. });
  10172. });
  10173. }
  10174. }
  10175. }
  10176. catch (sycl::exception const &exc) {
  10177. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10178. << ", line:" << __LINE__ << std::endl;
  10179. std::exit(1);
  10180. }
  10181. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  10182. float *dst, const int ncols_x,
  10183. const int nrows_x, const int ncols_y,
  10184. const int nrows_y, const int nrows_dst,
  10185. dpct::queue_ptr stream) try {
  10186. int id;
  10187. SYCL_CHECK(
  10188. CHECK_TRY_ERROR(id = get_current_device_id()));
  10189. const int compute_capability = g_device_caps[id].cc;
  10190. int mmq_x, mmq_y, nwarps;
  10191. if (compute_capability >= VER_GEN13) {
  10192. mmq_x = MMQ_X_Q2_K_RDNA2;
  10193. mmq_y = MMQ_Y_Q2_K_RDNA2;
  10194. nwarps = NWARPS_Q2_K_RDNA2;
  10195. } else if (compute_capability >= VER_GEN12) {
  10196. mmq_x = MMQ_X_Q2_K_RDNA1;
  10197. mmq_y = MMQ_Y_Q2_K_RDNA1;
  10198. nwarps = NWARPS_Q2_K_RDNA1;
  10199. } else if (compute_capability >= VER_GEN9) {
  10200. mmq_x = MMQ_X_Q2_K_AMPERE;
  10201. mmq_y = MMQ_Y_Q2_K_AMPERE;
  10202. nwarps = NWARPS_Q2_K_AMPERE;
  10203. } else if (compute_capability >= VER_4VEC) {
  10204. mmq_x = MMQ_X_Q2_K_PASCAL;
  10205. mmq_y = MMQ_Y_Q2_K_PASCAL;
  10206. nwarps = NWARPS_Q2_K_PASCAL;
  10207. } else {
  10208. GGML_ASSERT(false);
  10209. }
  10210. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10211. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10212. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10213. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10214. if (nrows_x % mmq_y == 0) {
  10215. const bool need_check = false;
  10216. /*
  10217. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  10218. the limit. To get the device limit, query
  10219. info::device::max_work_group_size. Adjust the work-group size if needed.
  10220. */
  10221. {
  10222. dpct::has_capability_or_fail(stream->get_device(),
  10223. {sycl::aspect::fp16});
  10224. stream->submit([&](sycl::handler &cgh) {
  10225. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  10226. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10227. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  10228. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  10229. cgh);
  10230. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  10231. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10232. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10233. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10234. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10235. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10236. cgh.parallel_for(
  10237. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10238. [=](sycl::nd_item<3> item_ct1) {
  10239. mul_mat_q2_K<need_check>(
  10240. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10241. nrows_dst, item_ct1,
  10242. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  10243. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  10244. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  10245. tile_y_qs_acc_ct1.get_pointer(),
  10246. tile_y_ds_acc_ct1.get_pointer());
  10247. });
  10248. });
  10249. }
  10250. } else {
  10251. const bool need_check = true;
  10252. /*
  10253. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  10254. the limit. To get the device limit, query
  10255. info::device::max_work_group_size. Adjust the work-group size if needed.
  10256. */
  10257. {
  10258. dpct::has_capability_or_fail(stream->get_device(),
  10259. {sycl::aspect::fp16});
  10260. stream->submit([&](sycl::handler &cgh) {
  10261. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  10262. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10263. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  10264. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  10265. cgh);
  10266. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  10267. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10268. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10269. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10270. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10271. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10272. cgh.parallel_for(
  10273. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10274. [=](sycl::nd_item<3> item_ct1) {
  10275. mul_mat_q2_K<need_check>(
  10276. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10277. nrows_dst, item_ct1,
  10278. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  10279. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  10280. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  10281. tile_y_qs_acc_ct1.get_pointer(),
  10282. tile_y_ds_acc_ct1.get_pointer());
  10283. });
  10284. });
  10285. }
  10286. }
  10287. }
  10288. catch (sycl::exception const &exc) {
  10289. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10290. << ", line:" << __LINE__ << std::endl;
  10291. std::exit(1);
  10292. }
  10293. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  10294. float *dst, const int ncols_x,
  10295. const int nrows_x, const int ncols_y,
  10296. const int nrows_y, const int nrows_dst,
  10297. dpct::queue_ptr stream) try {
  10298. #if QK_K == 256
  10299. int id;
  10300. SYCL_CHECK(
  10301. CHECK_TRY_ERROR(id = get_current_device_id()));
  10302. const int compute_capability = g_device_caps[id].cc;
  10303. int mmq_x, mmq_y, nwarps;
  10304. if (compute_capability >= VER_GEN13) {
  10305. mmq_x = MMQ_X_Q3_K_RDNA2;
  10306. mmq_y = MMQ_Y_Q3_K_RDNA2;
  10307. nwarps = NWARPS_Q3_K_RDNA2;
  10308. } else if (compute_capability >= VER_GEN12) {
  10309. mmq_x = MMQ_X_Q3_K_RDNA1;
  10310. mmq_y = MMQ_Y_Q3_K_RDNA1;
  10311. nwarps = NWARPS_Q3_K_RDNA1;
  10312. } else if (compute_capability >= VER_GEN9) {
  10313. mmq_x = MMQ_X_Q3_K_AMPERE;
  10314. mmq_y = MMQ_Y_Q3_K_AMPERE;
  10315. nwarps = NWARPS_Q3_K_AMPERE;
  10316. } else if (compute_capability >= VER_4VEC) {
  10317. mmq_x = MMQ_X_Q3_K_PASCAL;
  10318. mmq_y = MMQ_Y_Q3_K_PASCAL;
  10319. nwarps = NWARPS_Q3_K_PASCAL;
  10320. } else {
  10321. GGML_ASSERT(false);
  10322. }
  10323. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10324. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10325. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10326. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10327. if (nrows_x % mmq_y == 0) {
  10328. const bool need_check = false;
  10329. /*
  10330. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  10331. the limit. To get the device limit, query
  10332. info::device::max_work_group_size. Adjust the work-group size if needed.
  10333. */
  10334. {
  10335. dpct::has_capability_or_fail(stream->get_device(),
  10336. {sycl::aspect::fp16});
  10337. stream->submit([&](sycl::handler &cgh) {
  10338. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  10339. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10340. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  10341. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  10342. cgh);
  10343. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  10344. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  10345. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  10346. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10347. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10348. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10349. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10350. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10351. cgh.parallel_for(
  10352. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10353. [=](sycl::nd_item<3> item_ct1) {
  10354. mul_mat_q3_K<need_check>(
  10355. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10356. nrows_dst, item_ct1,
  10357. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  10358. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  10359. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  10360. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  10361. tile_y_qs_acc_ct1.get_pointer(),
  10362. tile_y_ds_acc_ct1.get_pointer());
  10363. });
  10364. });
  10365. }
  10366. } else {
  10367. const bool need_check = true;
  10368. /*
  10369. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  10370. the limit. To get the device limit, query
  10371. info::device::max_work_group_size. Adjust the work-group size if needed.
  10372. */
  10373. {
  10374. dpct::has_capability_or_fail(stream->get_device(),
  10375. {sycl::aspect::fp16});
  10376. stream->submit([&](sycl::handler &cgh) {
  10377. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  10378. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10379. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  10380. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  10381. cgh);
  10382. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  10383. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  10384. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  10385. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10386. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10387. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10388. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10389. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10390. cgh.parallel_for(
  10391. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10392. [=](sycl::nd_item<3> item_ct1) {
  10393. mul_mat_q3_K<need_check>(
  10394. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10395. nrows_dst, item_ct1,
  10396. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  10397. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  10398. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  10399. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  10400. tile_y_qs_acc_ct1.get_pointer(),
  10401. tile_y_ds_acc_ct1.get_pointer());
  10402. });
  10403. });
  10404. }
  10405. }
  10406. #endif
  10407. }
  10408. catch (sycl::exception const &exc) {
  10409. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10410. << ", line:" << __LINE__ << std::endl;
  10411. std::exit(1);
  10412. }
  10413. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  10414. float *dst, const int ncols_x,
  10415. const int nrows_x, const int ncols_y,
  10416. const int nrows_y, const int nrows_dst,
  10417. dpct::queue_ptr stream) try {
  10418. int id;
  10419. SYCL_CHECK(
  10420. CHECK_TRY_ERROR(id = get_current_device_id()));
  10421. const int compute_capability = g_device_caps[id].cc;
  10422. int mmq_x, mmq_y, nwarps;
  10423. if (compute_capability >= VER_GEN13) {
  10424. mmq_x = MMQ_X_Q4_K_RDNA2;
  10425. mmq_y = MMQ_Y_Q4_K_RDNA2;
  10426. nwarps = NWARPS_Q4_K_RDNA2;
  10427. } else if (compute_capability >= VER_GEN12) {
  10428. mmq_x = MMQ_X_Q4_K_RDNA1;
  10429. mmq_y = MMQ_Y_Q4_K_RDNA1;
  10430. nwarps = NWARPS_Q4_K_RDNA1;
  10431. } else if (compute_capability >= VER_GEN9) {
  10432. mmq_x = MMQ_X_Q4_K_AMPERE;
  10433. mmq_y = MMQ_Y_Q4_K_AMPERE;
  10434. nwarps = NWARPS_Q4_K_AMPERE;
  10435. } else if (compute_capability >= VER_4VEC) {
  10436. mmq_x = MMQ_X_Q4_K_PASCAL;
  10437. mmq_y = MMQ_Y_Q4_K_PASCAL;
  10438. nwarps = NWARPS_Q4_K_PASCAL;
  10439. } else {
  10440. GGML_ASSERT(false);
  10441. }
  10442. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10443. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10444. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10445. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10446. if (nrows_x % mmq_y == 0) {
  10447. const bool need_check = false;
  10448. /*
  10449. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  10450. the limit. To get the device limit, query
  10451. info::device::max_work_group_size. Adjust the work-group size if needed.
  10452. */
  10453. {
  10454. dpct::has_capability_or_fail(stream->get_device(),
  10455. {sycl::aspect::fp16});
  10456. stream->submit([&](sycl::handler &cgh) {
  10457. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10458. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10459. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10460. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10461. cgh);
  10462. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10463. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10464. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10465. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10466. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10467. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10468. cgh.parallel_for(
  10469. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10470. [=](sycl::nd_item<3> item_ct1) {
  10471. mul_mat_q4_K<need_check>(
  10472. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10473. nrows_dst, item_ct1,
  10474. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10475. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10476. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10477. tile_y_qs_acc_ct1.get_pointer(),
  10478. tile_y_ds_acc_ct1.get_pointer());
  10479. });
  10480. });
  10481. }
  10482. } else {
  10483. const bool need_check = true;
  10484. /*
  10485. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  10486. the limit. To get the device limit, query
  10487. info::device::max_work_group_size. Adjust the work-group size if needed.
  10488. */
  10489. {
  10490. dpct::has_capability_or_fail(stream->get_device(),
  10491. {sycl::aspect::fp16});
  10492. stream->submit([&](sycl::handler &cgh) {
  10493. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10494. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10495. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10496. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10497. cgh);
  10498. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10499. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10500. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10501. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10502. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10503. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10504. cgh.parallel_for(
  10505. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10506. [=](sycl::nd_item<3> item_ct1) {
  10507. mul_mat_q4_K<need_check>(
  10508. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10509. nrows_dst, item_ct1,
  10510. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10511. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10512. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10513. tile_y_qs_acc_ct1.get_pointer(),
  10514. tile_y_ds_acc_ct1.get_pointer());
  10515. });
  10516. });
  10517. }
  10518. }
  10519. }
  10520. catch (sycl::exception const &exc) {
  10521. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10522. << ", line:" << __LINE__ << std::endl;
  10523. std::exit(1);
  10524. }
  10525. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  10526. float *dst, const int ncols_x,
  10527. const int nrows_x, const int ncols_y,
  10528. const int nrows_y, const int nrows_dst,
  10529. dpct::queue_ptr stream) try {
  10530. int id;
  10531. SYCL_CHECK(
  10532. CHECK_TRY_ERROR(id = get_current_device_id()));
  10533. const int compute_capability = g_device_caps[id].cc;
  10534. int mmq_x, mmq_y, nwarps;
  10535. if (compute_capability >= VER_GEN13) {
  10536. mmq_x = MMQ_X_Q5_K_RDNA2;
  10537. mmq_y = MMQ_Y_Q5_K_RDNA2;
  10538. nwarps = NWARPS_Q5_K_RDNA2;
  10539. } else if (compute_capability >= VER_GEN12) {
  10540. mmq_x = MMQ_X_Q5_K_RDNA1;
  10541. mmq_y = MMQ_Y_Q5_K_RDNA1;
  10542. nwarps = NWARPS_Q5_K_RDNA1;
  10543. } else if (compute_capability >= VER_GEN9) {
  10544. mmq_x = MMQ_X_Q5_K_AMPERE;
  10545. mmq_y = MMQ_Y_Q5_K_AMPERE;
  10546. nwarps = NWARPS_Q5_K_AMPERE;
  10547. } else if (compute_capability >= VER_4VEC) {
  10548. mmq_x = MMQ_X_Q5_K_PASCAL;
  10549. mmq_y = MMQ_Y_Q5_K_PASCAL;
  10550. nwarps = NWARPS_Q5_K_PASCAL;
  10551. } else {
  10552. GGML_ASSERT(false);
  10553. }
  10554. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10555. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10556. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10557. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10558. if (nrows_x % mmq_y == 0) {
  10559. const bool need_check = false;
  10560. /*
  10561. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  10562. the limit. To get the device limit, query
  10563. info::device::max_work_group_size. Adjust the work-group size if needed.
  10564. */
  10565. {
  10566. dpct::has_capability_or_fail(stream->get_device(),
  10567. {sycl::aspect::fp16});
  10568. stream->submit([&](sycl::handler &cgh) {
  10569. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10570. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10571. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10572. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10573. cgh);
  10574. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10575. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10576. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10577. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10578. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10579. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10580. cgh.parallel_for(
  10581. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10582. [=](sycl::nd_item<3> item_ct1) {
  10583. mul_mat_q5_K<need_check>(
  10584. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10585. nrows_dst, item_ct1,
  10586. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10587. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10588. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10589. tile_y_qs_acc_ct1.get_pointer(),
  10590. tile_y_ds_acc_ct1.get_pointer());
  10591. });
  10592. });
  10593. }
  10594. } else {
  10595. const bool need_check = true;
  10596. /*
  10597. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  10598. the limit. To get the device limit, query
  10599. info::device::max_work_group_size. Adjust the work-group size if needed.
  10600. */
  10601. {
  10602. dpct::has_capability_or_fail(stream->get_device(),
  10603. {sycl::aspect::fp16});
  10604. stream->submit([&](sycl::handler &cgh) {
  10605. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10606. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10607. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10608. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10609. cgh);
  10610. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10611. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10612. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10613. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10614. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10615. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10616. cgh.parallel_for(
  10617. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10618. [=](sycl::nd_item<3> item_ct1) {
  10619. mul_mat_q5_K<need_check>(
  10620. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10621. nrows_dst, item_ct1,
  10622. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10623. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10624. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10625. tile_y_qs_acc_ct1.get_pointer(),
  10626. tile_y_ds_acc_ct1.get_pointer());
  10627. });
  10628. });
  10629. }
  10630. }
  10631. }
  10632. catch (sycl::exception const &exc) {
  10633. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10634. << ", line:" << __LINE__ << std::endl;
  10635. std::exit(1);
  10636. }
  10637. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  10638. float *dst, const int ncols_x,
  10639. const int nrows_x, const int ncols_y,
  10640. const int nrows_y, const int nrows_dst,
  10641. dpct::queue_ptr stream) try {
  10642. int id;
  10643. SYCL_CHECK(
  10644. CHECK_TRY_ERROR(id = get_current_device_id()));
  10645. const int compute_capability = g_device_caps[id].cc;
  10646. int mmq_x, mmq_y, nwarps;
  10647. if (compute_capability >= VER_GEN13) {
  10648. mmq_x = MMQ_X_Q6_K_RDNA2;
  10649. mmq_y = MMQ_Y_Q6_K_RDNA2;
  10650. nwarps = NWARPS_Q6_K_RDNA2;
  10651. } else if (compute_capability >= VER_GEN12) {
  10652. mmq_x = MMQ_X_Q6_K_RDNA1;
  10653. mmq_y = MMQ_Y_Q6_K_RDNA1;
  10654. nwarps = NWARPS_Q6_K_RDNA1;
  10655. } else if (compute_capability >= VER_GEN9) {
  10656. mmq_x = MMQ_X_Q6_K_AMPERE;
  10657. mmq_y = MMQ_Y_Q6_K_AMPERE;
  10658. nwarps = NWARPS_Q6_K_AMPERE;
  10659. } else if (compute_capability >= VER_4VEC) {
  10660. mmq_x = MMQ_X_Q6_K_PASCAL;
  10661. mmq_y = MMQ_Y_Q6_K_PASCAL;
  10662. nwarps = NWARPS_Q6_K_PASCAL;
  10663. } else {
  10664. GGML_ASSERT(false);
  10665. }
  10666. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10667. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10668. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10669. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10670. if (nrows_x % mmq_y == 0) {
  10671. const bool need_check = false;
  10672. /*
  10673. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  10674. the limit. To get the device limit, query
  10675. info::device::max_work_group_size. Adjust the work-group size if needed.
  10676. */
  10677. {
  10678. dpct::has_capability_or_fail(stream->get_device(),
  10679. {sycl::aspect::fp16});
  10680. stream->submit([&](sycl::handler &cgh) {
  10681. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10682. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10683. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10684. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10685. cgh);
  10686. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10687. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10688. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10689. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10690. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10691. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10692. cgh.parallel_for(
  10693. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10694. [=](sycl::nd_item<3> item_ct1) {
  10695. mul_mat_q6_K<need_check>(
  10696. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10697. nrows_dst, item_ct1,
  10698. tile_x_ql_acc_ct1.get_pointer(),
  10699. tile_x_dm_acc_ct1.get_pointer(),
  10700. tile_x_sc_acc_ct1.get_pointer(),
  10701. tile_y_qs_acc_ct1.get_pointer(),
  10702. tile_y_ds_acc_ct1.get_pointer());
  10703. });
  10704. });
  10705. }
  10706. } else {
  10707. const bool need_check = true;
  10708. /*
  10709. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  10710. the limit. To get the device limit, query
  10711. info::device::max_work_group_size. Adjust the work-group size if needed.
  10712. */
  10713. {
  10714. dpct::has_capability_or_fail(stream->get_device(),
  10715. {sycl::aspect::fp16});
  10716. stream->submit([&](sycl::handler &cgh) {
  10717. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10718. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10719. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10720. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10721. cgh);
  10722. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10723. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10724. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10725. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10726. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10727. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10728. cgh.parallel_for(
  10729. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10730. [=](sycl::nd_item<3> item_ct1) {
  10731. mul_mat_q6_K<need_check>(
  10732. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10733. nrows_dst, item_ct1,
  10734. tile_x_ql_acc_ct1.get_pointer(),
  10735. tile_x_dm_acc_ct1.get_pointer(),
  10736. tile_x_sc_acc_ct1.get_pointer(),
  10737. tile_y_qs_acc_ct1.get_pointer(),
  10738. tile_y_ds_acc_ct1.get_pointer());
  10739. });
  10740. });
  10741. }
  10742. }
  10743. }
  10744. catch (sycl::exception const &exc) {
  10745. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10746. << ", line:" << __LINE__ << std::endl;
  10747. std::exit(1);
  10748. }
  10749. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  10750. float *dst, const int ncols_x,
  10751. const int nrows_x,
  10752. const int nchannels_x,
  10753. const int nchannels_y,
  10754. dpct::queue_ptr stream) {
  10755. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10756. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10757. {
  10758. dpct::has_capability_or_fail(stream->get_device(),
  10759. {sycl::aspect::fp16});
  10760. stream->parallel_for(
  10761. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10762. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10763. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  10764. nchannels_y, item_ct1);
  10765. });
  10766. }
  10767. }
  10768. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  10769. const void *vx, const float *y, float *dst, const int ncols_x,
  10770. const int nrows_x, const int row_stride_x, const int nchannels_x,
  10771. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  10772. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10773. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10774. {
  10775. dpct::has_capability_or_fail(stream->get_device(),
  10776. {sycl::aspect::fp16});
  10777. stream->parallel_for(
  10778. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10779. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10780. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  10781. row_stride_x, channel_stride_x,
  10782. nchannels_y / nchannels_x, item_ct1);
  10783. });
  10784. }
  10785. }
  10786. static void
  10787. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  10788. const int ne01, const int ne02, const int nb00,
  10789. const int nb01, const int nb02, const int nb03,
  10790. const int ne10, const int ne11, const int ne12,
  10791. const int nb10, const int nb11, const int nb12,
  10792. const int nb13, dpct::queue_ptr stream) {
  10793. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10794. {
  10795. dpct::has_capability_or_fail(stream->get_device(),
  10796. {sycl::aspect::fp16});
  10797. stream->parallel_for(
  10798. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10799. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10800. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10801. [=](sycl::nd_item<3> item_ct1) {
  10802. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  10803. nb01, nb02, nb03, ne10, ne11, ne12,
  10804. nb10, nb11, nb12, nb13, item_ct1);
  10805. });
  10806. }
  10807. }
  10808. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  10809. const int ne00, const int ne01,
  10810. const int ne02, const int nb00,
  10811. const int nb01, const int nb02,
  10812. const int nb03, const int ne10,
  10813. const int ne11, const int ne12,
  10814. const int nb10, const int nb11,
  10815. const int nb12, const int nb13,
  10816. dpct::queue_ptr stream) {
  10817. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10818. {
  10819. dpct::has_capability_or_fail(stream->get_device(),
  10820. {sycl::aspect::fp16});
  10821. stream->parallel_for(
  10822. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10823. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10824. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10825. [=](sycl::nd_item<3> item_ct1) {
  10826. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10827. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10828. item_ct1);
  10829. });
  10830. }
  10831. }
  10832. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  10833. const int ne00, const int ne01,
  10834. const int ne02, const int nb00,
  10835. const int nb01, const int nb02,
  10836. const int nb03, const int ne10,
  10837. const int ne11, const int ne12,
  10838. const int nb10, const int nb11,
  10839. const int nb12, const int nb13,
  10840. dpct::queue_ptr stream) {
  10841. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10842. {
  10843. dpct::has_capability_or_fail(stream->get_device(),
  10844. {sycl::aspect::fp16});
  10845. stream->parallel_for(
  10846. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10847. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10848. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10849. [=](sycl::nd_item<3> item_ct1) {
  10850. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10851. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10852. item_ct1);
  10853. });
  10854. }
  10855. }
  10856. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  10857. const int ne00, const int ne01,
  10858. const int ne02, const int nb00,
  10859. const int nb01, const int nb02,
  10860. const int nb03, const int ne10,
  10861. const int ne11, const int ne12,
  10862. const int nb10, const int nb11,
  10863. const int nb12, const int nb13,
  10864. dpct::queue_ptr stream) {
  10865. GGML_ASSERT(ne % QK8_0 == 0);
  10866. const int num_blocks = ne / QK8_0;
  10867. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10868. sycl::range<3>(1, 1, 1)),
  10869. [=](sycl::nd_item<3> item_ct1) {
  10870. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  10871. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10872. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10873. item_ct1);
  10874. });
  10875. }
  10876. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  10877. const int ne00, const int ne01,
  10878. const int ne02, const int nb00,
  10879. const int nb01, const int nb02,
  10880. const int nb03, const int ne10,
  10881. const int ne11, const int ne12,
  10882. const int nb10, const int nb11,
  10883. const int nb12, const int nb13,
  10884. dpct::queue_ptr stream) {
  10885. GGML_ASSERT(ne % QK4_0 == 0);
  10886. const int num_blocks = ne / QK4_0;
  10887. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10888. sycl::range<3>(1, 1, 1)),
  10889. [=](sycl::nd_item<3> item_ct1) {
  10890. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  10891. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10892. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10893. item_ct1);
  10894. });
  10895. }
  10896. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  10897. const int ne00, const int ne01,
  10898. const int ne02, const int nb00,
  10899. const int nb01, const int nb02,
  10900. const int nb03, const int ne10,
  10901. const int ne11, const int ne12,
  10902. const int nb10, const int nb11,
  10903. const int nb12, const int nb13,
  10904. dpct::queue_ptr stream) {
  10905. GGML_ASSERT(ne % QK4_1 == 0);
  10906. const int num_blocks = ne / QK4_1;
  10907. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10908. sycl::range<3>(1, 1, 1)),
  10909. [=](sycl::nd_item<3> item_ct1) {
  10910. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  10911. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10912. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10913. item_ct1);
  10914. });
  10915. }
  10916. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  10917. const int ne00, const int ne01,
  10918. const int ne02, const int nb00,
  10919. const int nb01, const int nb02,
  10920. const int nb03, const int ne10,
  10921. const int ne11, const int ne12,
  10922. const int nb10, const int nb11,
  10923. const int nb12, const int nb13,
  10924. dpct::queue_ptr stream) {
  10925. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10926. {
  10927. dpct::has_capability_or_fail(stream->get_device(),
  10928. {sycl::aspect::fp16});
  10929. stream->parallel_for(
  10930. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10931. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10932. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10933. [=](sycl::nd_item<3> item_ct1) {
  10934. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10935. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10936. item_ct1);
  10937. });
  10938. }
  10939. }
  10940. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  10941. const int ne00, const int ne01,
  10942. const int ne02, const int nb00,
  10943. const int nb01, const int nb02,
  10944. const int nb03, const int ne10,
  10945. const int ne11, const int ne12,
  10946. const int nb10, const int nb11,
  10947. const int nb12, const int nb13,
  10948. dpct::queue_ptr stream) {
  10949. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10950. {
  10951. // dpct::has_capability_or_fail(stream->get_device(),
  10952. // {sycl::aspect::fp16});
  10953. stream->parallel_for(
  10954. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10955. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10956. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10957. [=](sycl::nd_item<3> item_ct1) {
  10958. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10959. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10960. item_ct1);
  10961. });
  10962. }
  10963. }
  10964. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  10965. const int ne00, const int ne01,
  10966. const int ne02, const int nb00,
  10967. const int nb01, const int nb02,
  10968. const int nb03, const int ne10,
  10969. const int ne11, const int ne12,
  10970. const int nb10, const int nb11,
  10971. const int nb12, const int nb13,
  10972. dpct::queue_ptr stream) {
  10973. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10974. {
  10975. // dpct::has_capability_or_fail(stream->get_device(),
  10976. // {sycl::aspect::fp16});
  10977. stream->parallel_for(
  10978. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10979. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10980. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10981. [=](sycl::nd_item<3> item_ct1) {
  10982. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10983. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10984. item_ct1);
  10985. });
  10986. }
  10987. }
  10988. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  10989. const int k, dpct::queue_ptr stream) {
  10990. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  10991. stream->parallel_for(
  10992. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10993. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  10994. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  10995. [=](sycl::nd_item<3> item_ct1) {
  10996. scale_f32(x, dst, scale, k, item_ct1);
  10997. });
  10998. }
  10999. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  11000. const float max, const int k,
  11001. dpct::queue_ptr stream) {
  11002. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  11003. stream->parallel_for(
  11004. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  11005. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  11006. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  11007. [=](sycl::nd_item<3> item_ct1) {
  11008. clamp_f32(x, dst, min, max, k, item_ct1);
  11009. });
  11010. }
  11011. template <typename T>
  11012. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  11013. const int32_t *pos, float freq_scale, int p_delta_rows,
  11014. float freq_base, float ext_factor, float attn_factor,
  11015. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  11016. GGML_ASSERT(ncols % 2 == 0);
  11017. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  11018. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  11019. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  11020. if (pos == nullptr) {
  11021. /*
  11022. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  11023. the limit. To get the device limit, query
  11024. info::device::max_work_group_size. Adjust the work-group size if needed.
  11025. */
  11026. dpct::has_capability_or_fail(stream->get_device(),
  11027. {sycl::aspect::fp16});
  11028. stream->parallel_for(
  11029. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11030. [=](sycl::nd_item<3> item_ct1) {
  11031. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  11032. freq_base, ext_factor, attn_factor, corr_dims,
  11033. item_ct1);
  11034. });
  11035. } else {
  11036. /*
  11037. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  11038. the limit. To get the device limit, query
  11039. info::device::max_work_group_size. Adjust the work-group size if needed.
  11040. */
  11041. dpct::has_capability_or_fail(stream->get_device(),
  11042. {sycl::aspect::fp16});
  11043. stream->parallel_for(
  11044. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11045. [=](sycl::nd_item<3> item_ct1) {
  11046. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  11047. freq_base, ext_factor, attn_factor, corr_dims,
  11048. item_ct1);
  11049. });
  11050. }
  11051. }
  11052. template <typename T>
  11053. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  11054. const int32_t *pos, float freq_scale,
  11055. int p_delta_rows, float freq_base, float ext_factor,
  11056. float attn_factor, rope_corr_dims corr_dims,
  11057. dpct::queue_ptr stream) {
  11058. GGML_ASSERT(ncols % 2 == 0);
  11059. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  11060. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  11061. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  11062. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  11063. const float inv_ndims = -1.0f / n_dims;
  11064. if (pos == nullptr) {
  11065. /*
  11066. DPCT1049:42: The work-group size passed to the SYCL kernel may exceed
  11067. the limit. To get the device limit, query
  11068. info::device::max_work_group_size. Adjust the work-group size if needed.
  11069. */
  11070. dpct::has_capability_or_fail(stream->get_device(),
  11071. {sycl::aspect::fp16});
  11072. stream->parallel_for(
  11073. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11074. [=](sycl::nd_item<3> item_ct1) {
  11075. rope_neox<T, false>(x, dst, ncols, n_dims, pos, freq_scale,
  11076. p_delta_rows, ext_factor, attn_factor,
  11077. corr_dims, theta_scale, inv_ndims,
  11078. item_ct1);
  11079. });
  11080. } else {
  11081. /*
  11082. DPCT1049:43: The work-group size passed to the SYCL kernel may exceed
  11083. the limit. To get the device limit, query
  11084. info::device::max_work_group_size. Adjust the work-group size if needed.
  11085. */
  11086. dpct::has_capability_or_fail(stream->get_device(),
  11087. {sycl::aspect::fp16});
  11088. stream->parallel_for(
  11089. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11090. [=](sycl::nd_item<3> item_ct1) {
  11091. rope_neox<T, true>(x, dst, ncols, n_dims, pos, freq_scale,
  11092. p_delta_rows, ext_factor, attn_factor,
  11093. corr_dims, theta_scale, inv_ndims, item_ct1);
  11094. });
  11095. }
  11096. }
  11097. static void rope_glm_f32_sycl(const float *x, float *dst, int ncols, int nrows,
  11098. const int32_t *pos, float freq_scale,
  11099. int p_delta_rows, float freq_base, int n_ctx,
  11100. dpct::queue_ptr stream) {
  11101. GGML_ASSERT(ncols % 4 == 0);
  11102. const sycl::range<3> block_dims(1, 1, SYCL_ROPE_BLOCK_SIZE / 4);
  11103. const int num_blocks_x = (ncols + SYCL_ROPE_BLOCK_SIZE - 1) / SYCL_ROPE_BLOCK_SIZE;
  11104. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  11105. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11106. [=](sycl::nd_item<3> item_ct1) {
  11107. rope_glm_f32(x, dst, ncols, pos, freq_scale,
  11108. p_delta_rows, freq_base, n_ctx,
  11109. item_ct1);
  11110. });
  11111. }
  11112. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  11113. const int nrows, dpct::queue_ptr stream) {
  11114. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  11115. const sycl::range<3> block_nums(1, nrows, 1);
  11116. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11117. [=](sycl::nd_item<3> item_ct1)
  11118. [[intel::reqd_sub_group_size(32)]] {
  11119. k_sum_rows_f32(x, dst, ncols, item_ct1);
  11120. });
  11121. }
  11122. static int next_power_of_2(int x) {
  11123. int n = 1;
  11124. while (n < x) {
  11125. n *= 2;
  11126. }
  11127. return n;
  11128. }
  11129. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  11130. const int nrows, ggml_sort_order order,
  11131. dpct::queue_ptr stream) {
  11132. // bitonic sort requires ncols to be power of 2
  11133. const int ncols_pad = next_power_of_2(ncols);
  11134. const sycl::range<3> block_dims(1, 1, ncols_pad);
  11135. const sycl::range<3> block_nums(1, nrows, 1);
  11136. const size_t shared_mem = ncols_pad * sizeof(int);
  11137. // GGML_ASSERT(shared_mem <= ggml_cuda_info().devices[ggml_cuda_get_device()].smpb);
  11138. if (order == GGML_SORT_ORDER_ASC) {
  11139. stream->submit([&](sycl::handler &cgh) {
  11140. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  11141. sycl::range<1>(shared_mem), cgh);
  11142. cgh.parallel_for(
  11143. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11144. [=](sycl::nd_item<3> item_ct1) {
  11145. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(
  11146. x, dst, ncols, ncols_pad, item_ct1,
  11147. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  11148. .get());
  11149. });
  11150. });
  11151. } else if (order == GGML_SORT_ORDER_DESC) {
  11152. stream->submit([&](sycl::handler &cgh) {
  11153. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  11154. sycl::range<1>(shared_mem), cgh);
  11155. cgh.parallel_for(
  11156. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11157. [=](sycl::nd_item<3> item_ct1) {
  11158. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(
  11159. x, dst, ncols, ncols_pad, item_ct1,
  11160. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  11161. .get());
  11162. });
  11163. });
  11164. } else {
  11165. GGML_ASSERT(false);
  11166. }
  11167. }
  11168. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  11169. const int ncols_x, const int nrows_x,
  11170. const int rows_per_channel, const int n_past,
  11171. dpct::queue_ptr stream) {
  11172. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  11173. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  11174. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  11175. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11176. [=](sycl::nd_item<3> item_ct1) {
  11177. diag_mask_inf_f32(x, dst, ncols_x,
  11178. rows_per_channel, n_past,
  11179. item_ct1);
  11180. });
  11181. }
  11182. template <bool vals_smem, int ncols_template, int block_size_template>
  11183. static void soft_max_f32_submitter(const float * x, const float * mask, float * dst, const int ncols_par,
  11184. const int nrows_y, const float scale, const float max_bias, const float m0,
  11185. const float m1, uint32_t n_head_log2, sycl::range<3> block_nums, sycl::range<3> block_dims,
  11186. const size_t n_local_scratch, dpct::queue_ptr stream) {
  11187. stream->submit([&](sycl::handler &cgh) {
  11188. sycl::local_accessor<float, 1> local_buf_acc(n_local_scratch, cgh);
  11189. cgh.parallel_for(
  11190. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11191. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  11192. soft_max_f32<vals_smem, ncols_template, block_size_template>(x, mask, dst, ncols_par,
  11193. nrows_y, scale, max_bias, m0,
  11194. m1, n_head_log2, item_ct1,
  11195. local_buf_acc.get_pointer());
  11196. });
  11197. });
  11198. }
  11199. static void soft_max_f32_sycl(const float * x, const float * mask,
  11200. float * dst, const int ncols_x, const int nrows_x,
  11201. const int nrows_y, const float scale, const float max_bias,
  11202. dpct::queue_ptr stream) {
  11203. int nth = WARP_SIZE;
  11204. int max_block_size = g_work_group_size;
  11205. while (nth < ncols_x && nth < max_block_size) nth *= 2;
  11206. if (nth>max_block_size) nth = max_block_size;
  11207. const sycl::range<3> block_dims(1, 1, nth);
  11208. const sycl::range<3> block_nums(1, 1, nrows_x);
  11209. const size_t n_local_scratch = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE);
  11210. const uint32_t n_head_kv = nrows_x/nrows_y;
  11211. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  11212. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  11213. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  11214. const size_t local_mem_size = stream->get_device().get_info<sycl::info::device::local_mem_size>();
  11215. if (n_local_scratch*sizeof(float) < local_mem_size) {
  11216. if (ncols_x > max_block_size) {
  11217. soft_max_f32_submitter<true, 0, 0>(x, mask, dst, ncols_x, nrows_y, scale,
  11218. max_bias, m0, m1, n_head_log2, block_nums,
  11219. block_dims, n_local_scratch, stream);
  11220. return;
  11221. }
  11222. switch (ncols_x) {
  11223. case 32:
  11224. soft_max_f32_submitter<true, 32, 32>(x, mask, dst, ncols_x, nrows_y, scale,
  11225. max_bias, m0, m1, n_head_log2, block_nums,
  11226. block_dims, n_local_scratch, stream);
  11227. break;
  11228. case 64:
  11229. soft_max_f32_submitter<true, 64, 64>(x, mask, dst, ncols_x, nrows_y, scale,
  11230. max_bias, m0, m1, n_head_log2, block_nums,
  11231. block_dims, n_local_scratch, stream);
  11232. break;
  11233. case 128:
  11234. soft_max_f32_submitter<true, 128, 128>(x, mask, dst, ncols_x, nrows_y, scale,
  11235. max_bias, m0, m1, n_head_log2, block_nums,
  11236. block_dims, n_local_scratch, stream);
  11237. break;
  11238. case 256:
  11239. soft_max_f32_submitter<true, 256, 256>(x, mask, dst, ncols_x, nrows_y, scale,
  11240. max_bias, m0, m1, n_head_log2, block_nums,
  11241. block_dims, n_local_scratch, stream);
  11242. break;
  11243. case 512:
  11244. soft_max_f32_submitter<true, 512, 512>(x, mask, dst, ncols_x, nrows_y, scale,
  11245. max_bias, m0, m1, n_head_log2, block_nums,
  11246. block_dims, n_local_scratch, stream);
  11247. break;
  11248. case 1024:
  11249. soft_max_f32_submitter<true, 1024, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  11250. max_bias, m0, m1, n_head_log2, block_nums,
  11251. block_dims, n_local_scratch, stream);
  11252. break;
  11253. case 2048:
  11254. soft_max_f32_submitter<true, 2048, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  11255. max_bias, m0, m1, n_head_log2, block_nums,
  11256. block_dims, n_local_scratch, stream);
  11257. break;
  11258. case 4096:
  11259. soft_max_f32_submitter<true, 4096, 1024>(x, mask, dst, ncols_x, nrows_y, scale,
  11260. max_bias, m0, m1, n_head_log2, block_nums,
  11261. block_dims, n_local_scratch, stream);
  11262. break;
  11263. default:
  11264. soft_max_f32_submitter<true, 0, 0>(x, mask, dst, ncols_x, nrows_y, scale,
  11265. max_bias, m0, m1, n_head_log2, block_nums,
  11266. block_dims, n_local_scratch, stream);
  11267. break;
  11268. }
  11269. } else {
  11270. soft_max_f32_submitter<false, 0, 0>(x, mask, dst, ncols_x, nrows_y, scale,
  11271. max_bias, m0, m1, n_head_log2, block_nums,
  11272. block_dims, WARP_SIZE, stream);
  11273. }
  11274. }
  11275. template <typename T>
  11276. static void im2col_sycl(const float *x, T *dst, int IW, int IH,
  11277. int OW, int OH, int KW, int KH, int IC,
  11278. int offset_delta, int s0, int s1, int p0,
  11279. int p1, int d0, int d1,
  11280. dpct::queue_ptr stream) {
  11281. const int parallel_elements = OW * KW * KH;
  11282. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  11283. sycl::range<3> block_nums(IC, OH, num_blocks);
  11284. {
  11285. dpct::has_capability_or_fail(stream->get_device(),
  11286. {sycl::aspect::fp16});
  11287. stream->parallel_for(
  11288. sycl::nd_range<3>(block_nums *
  11289. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  11290. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  11291. [=](sycl::nd_item<3> item_ct1) {
  11292. im2col_kernel(x, dst, offset_delta, IW, IH, OW, KW, KH,
  11293. parallel_elements, (IC * KH * KW), s0, s1, p0,
  11294. p1, d0, d1, item_ct1);
  11295. });
  11296. }
  11297. }
  11298. // buffer pool for sycl
  11299. #define MAX_SYCL_BUFFERS 256
  11300. struct scoped_spin_lock {
  11301. std::atomic_flag& lock;
  11302. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  11303. while (lock.test_and_set(std::memory_order_acquire)) {
  11304. ; // spin
  11305. }
  11306. }
  11307. ~scoped_spin_lock() {
  11308. lock.clear(std::memory_order_release);
  11309. }
  11310. scoped_spin_lock(const scoped_spin_lock&) = delete;
  11311. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  11312. };
  11313. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  11314. // #define DEBUG_SYCL_MALLOC
  11315. struct sycl_buffer {
  11316. void * ptr = nullptr;
  11317. size_t size = 0;
  11318. };
  11319. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  11320. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  11321. static void *ggml_sycl_pool_malloc_leg(int device_index, size_t size, size_t *actual_size) try {
  11322. scoped_spin_lock lock(g_sycl_pool_lock);
  11323. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg device_index %d size=%lu\n", device_index, size);
  11324. #ifdef DEBUG_SYCL_MALLOC
  11325. int nnz = 0;
  11326. size_t max_size = 0;
  11327. #endif
  11328. size_t best_diff = 1ull << 36;
  11329. int ibest = -1;
  11330. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  11331. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  11332. if (b.ptr != nullptr) {
  11333. #ifdef DEBUG_SYCL_MALLOC
  11334. ++nnz;
  11335. if (b.size > max_size) max_size = b.size;
  11336. #endif
  11337. if (b.size >= size) {
  11338. size_t diff = b.size - size;
  11339. if (diff < best_diff) {
  11340. best_diff = diff;
  11341. ibest = i;
  11342. if (!best_diff) {
  11343. void * ptr = b.ptr;
  11344. *actual_size = b.size;
  11345. b.ptr = nullptr;
  11346. b.size = 0;
  11347. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p and rm in pool\n", ptr);
  11348. return ptr;
  11349. }
  11350. }
  11351. }
  11352. }
  11353. }
  11354. if (ibest >= 0) {
  11355. sycl_buffer& b = g_sycl_buffer_pool[device_index][ibest];
  11356. void * ptr = b.ptr;
  11357. *actual_size = b.size;
  11358. b.ptr = nullptr;
  11359. b.size = 0;
  11360. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p and rm in pool\n", ptr);
  11361. return ptr;
  11362. }
  11363. void * ptr;
  11364. size_t look_ahead_size = (size_t) (1.05 * size);
  11365. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  11366. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  11367. SYCL_CHECK(
  11368. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  11369. look_ahead_size, *stream)));
  11370. *actual_size = look_ahead_size;
  11371. g_sycl_pool_size[device_index] += look_ahead_size;
  11372. #ifdef DEBUG_SYCL_MALLOC
  11373. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  11374. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  11375. #endif
  11376. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  11377. return ptr;
  11378. }
  11379. catch (sycl::exception const &exc) {
  11380. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11381. << ", line:" << __LINE__ << std::endl;
  11382. std::exit(1);
  11383. }
  11384. static void ggml_sycl_pool_free_leg(int device_index, void *ptr, size_t size) try {
  11385. scoped_spin_lock lock(g_sycl_pool_lock);
  11386. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  11387. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  11388. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  11389. if (b.ptr == nullptr) {
  11390. b.ptr = ptr;
  11391. b.size = size;
  11392. return;
  11393. }
  11394. }
  11395. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  11396. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  11397. g_sycl_pool_size[device_index] -= size;
  11398. }
  11399. catch (sycl::exception const &exc) {
  11400. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11401. << ", line:" << __LINE__ << std::endl;
  11402. std::exit(1);
  11403. }
  11404. // pool with virtual memory
  11405. /*
  11406. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  11407. */
  11408. // static std::vector<CUmemGenericAllocationHandle>
  11409. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  11410. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  11411. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  11412. static void *ggml_sycl_pool_malloc_vmm(int device_index, size_t size, size_t *actual_size) try {
  11413. GGML_UNUSED(device_index);
  11414. GGML_UNUSED(size);
  11415. GGML_UNUSED(actual_size);
  11416. return NULL;
  11417. }
  11418. catch (sycl::exception const &exc) {
  11419. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11420. << ", line:" << __LINE__ << std::endl;
  11421. std::exit(1);
  11422. }
  11423. static void ggml_sycl_pool_free_vmm(int device_index, void *ptr, size_t size) try {
  11424. scoped_spin_lock lock(g_sycl_pool_lock);
  11425. #ifdef DEBUG_SYCL_MALLOC
  11426. printf("sycl pool[%d]: freed %llu bytes at %llx\n", device_index, (unsigned long long) size, ptr);
  11427. #endif
  11428. g_sycl_pool_used[device_index] -= size;
  11429. // all deallocations must be in reverse order of the allocations
  11430. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[device_index] + g_sycl_pool_used[device_index]));
  11431. }
  11432. catch (sycl::exception const &exc) {
  11433. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11434. << ", line:" << __LINE__ << std::endl;
  11435. std::exit(1);
  11436. }
  11437. static void *ggml_sycl_pool_malloc(int device_index, size_t size, size_t *actual_size) try {
  11438. if (g_device_caps[device_index].vmm) {
  11439. return ggml_sycl_pool_malloc_vmm(device_index, size, actual_size);
  11440. } else {
  11441. return ggml_sycl_pool_malloc_leg(device_index, size, actual_size);
  11442. }
  11443. }
  11444. catch (sycl::exception const &exc) {
  11445. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11446. << ", line:" << __LINE__ << std::endl;
  11447. std::exit(1);
  11448. }
  11449. static void ggml_sycl_pool_free(int device_index, void *ptr, size_t size) try {
  11450. if (g_device_caps[device_index].vmm) {
  11451. ggml_sycl_pool_free_vmm(device_index, ptr, size);
  11452. } else {
  11453. ggml_sycl_pool_free_leg(device_index, ptr, size);
  11454. }
  11455. }
  11456. catch (sycl::exception const &exc) {
  11457. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11458. << ", line:" << __LINE__ << std::endl;
  11459. std::exit(1);
  11460. }
  11461. template<typename T>
  11462. struct sycl_pool_alloc {
  11463. int device_index = -1;
  11464. int device_id = -1;
  11465. T * ptr = nullptr;
  11466. size_t actual_size = 0;
  11467. // size is in number of elements
  11468. T * alloc(size_t size) {
  11469. GGML_ASSERT(ptr == nullptr);
  11470. device_id = get_current_device_id();
  11471. device_index = g_sycl_gpu_mgr->get_index(device_id);
  11472. ptr = (T *) ggml_sycl_pool_malloc(device_index, size * sizeof(T), &this->actual_size);
  11473. // GGML_SYCL_DEBUG("sycl_pool_alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  11474. return ptr;
  11475. }
  11476. sycl_pool_alloc(size_t size) {
  11477. alloc(size);
  11478. }
  11479. ~sycl_pool_alloc() {
  11480. if (ptr != nullptr) {
  11481. ggml_sycl_pool_free(device_index, ptr, actual_size);
  11482. }
  11483. }
  11484. T * get() {
  11485. return ptr;
  11486. }
  11487. sycl_pool_alloc() = default;
  11488. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  11489. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  11490. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  11491. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  11492. };
  11493. static bool g_sycl_loaded = false;
  11494. bool ggml_sycl_loaded(void) {
  11495. return g_sycl_loaded;
  11496. }
  11497. void print_device_detail(int id, sycl::device &device, std::string device_type) {
  11498. dpct::device_info prop;
  11499. SYCL_CHECK(CHECK_TRY_ERROR(
  11500. dpct::get_device_info(prop, device)));
  11501. std::string version;
  11502. version += std::to_string(prop.get_major_version());
  11503. version += ".";
  11504. version += std::to_string(prop.get_minor_version());
  11505. device_type = std::regex_replace(device_type, std::regex("ext_oneapi_"), "");
  11506. std::string name = std::string(prop.get_name());
  11507. name = std::regex_replace(name, std::regex("\\(R\\)"), "");
  11508. name = std::regex_replace(name, std::regex("\\(TM\\)"), "");
  11509. auto global_mem_size = prop.get_global_mem_size()/1000000;
  11510. fprintf(stderr, "|%2d|%19s|%39s|%7s|%7d|%8d|%5d|%6luM|%21s|\n", id, device_type.c_str(),
  11511. name.c_str(), version.c_str(), prop.get_max_compute_units(),
  11512. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  11513. global_mem_size, device.get_info<sycl::info::device::driver_version>().c_str());
  11514. }
  11515. void ggml_backend_sycl_print_sycl_devices() {
  11516. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_print_sycl_devices\n");
  11517. int device_count = dpct::dev_mgr::instance().device_count();
  11518. std::map<std::string, size_t> DeviceNums;
  11519. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  11520. fprintf(stderr, "| | | | |Max | |Max |Global | |\n");
  11521. fprintf(stderr, "| | | | |compute|Max work|sub |mem | |\n");
  11522. fprintf(stderr, "|ID| Device Type| Name|Version|units |group |group|size | Driver version|\n");
  11523. fprintf(stderr, "|--|-------------------|---------------------------------------|-------|-------|--------|-----|-------|---------------------|\n");
  11524. for (int id = 0; id < device_count; ++id) {
  11525. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  11526. sycl::backend backend = device.get_backend();
  11527. std::string backend_type = get_device_backend_and_type(device);
  11528. int type_id=DeviceNums[backend_type]++;
  11529. std::stringstream device_type;
  11530. device_type << "[" << backend_type << ":" << std::to_string(type_id) << "]";
  11531. print_device_detail(id, device, device_type.str());
  11532. }
  11533. }
  11534. void print_gpu_device_list() {
  11535. GGML_ASSERT(g_sycl_gpu_mgr);
  11536. char* hint=NULL;
  11537. if (g_ggml_sycl_backend_gpu_mode == SYCL_SINGLE_GPU_MODE) {
  11538. hint = "use %d SYCL GPUs: [%s] with Max compute units:%d\n";
  11539. } else {
  11540. hint = "detect %d SYCL GPUs: [%s] with top Max compute units:%d\n";
  11541. }
  11542. fprintf(stderr, hint,
  11543. g_sycl_gpu_mgr->get_gpu_count(),
  11544. g_sycl_gpu_mgr->gpus_list.c_str(),
  11545. g_sycl_gpu_mgr->max_compute_units);
  11546. }
  11547. int get_sycl_env(const char *env_name, int default_val) {
  11548. char *user_device_string = getenv(env_name);
  11549. int user_number = default_val;
  11550. unsigned n;
  11551. if (user_device_string != NULL &&
  11552. sscanf(user_device_string, " %u", &n) == 1) {
  11553. user_number = (int)n;
  11554. } else {
  11555. user_number = default_val;
  11556. }
  11557. return user_number;
  11558. }
  11559. int get_work_group_size(int user_device_id) {
  11560. dpct::device_info prop;
  11561. dpct::get_device_info(prop,
  11562. dpct::dev_mgr::instance().get_device(user_device_id));
  11563. return prop.get_max_work_group_size();
  11564. }
  11565. static void ggml_init_sycl() try {
  11566. static bool initialized = false;
  11567. if (!initialized) {
  11568. fprintf(stderr, "[SYCL] call ggml_init_sycl\n");
  11569. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  11570. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  11571. #if defined(GGML_SYCL_F16)
  11572. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  11573. #else
  11574. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  11575. #endif
  11576. /* NOT REMOVE, keep it for next optimize for XMX.
  11577. #if defined(SYCL_USE_XMX)
  11578. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  11579. #else
  11580. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  11581. #endif
  11582. */
  11583. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  11584. dpct::dev_mgr::instance().device_count()) != 0) {
  11585. initialized = true;
  11586. g_sycl_loaded = false;
  11587. return;
  11588. }
  11589. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  11590. ggml_backend_sycl_print_sycl_devices();
  11591. initialized = true;
  11592. g_sycl_loaded = true;
  11593. }
  11594. }
  11595. catch (sycl::exception const &exc) {
  11596. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11597. << ", line:" << __LINE__ << std::endl;
  11598. std::exit(1);
  11599. }
  11600. void ggml_init_by_gpus(int device_count) try {
  11601. g_device_count = device_count;
  11602. g_work_group_size = g_sycl_gpu_mgr->work_group_size;
  11603. int64_t total_vram = 0;
  11604. print_gpu_device_list();
  11605. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  11606. g_device_caps[id].vmm = 0;
  11607. g_device_caps[id].device_id = -1;
  11608. g_device_caps[id].cc = 0;
  11609. g_tensor_split[id] = 0;
  11610. g_default_tensor_split[id] = 0;
  11611. }
  11612. for (int i = 0; i < g_device_count; ++i) {
  11613. int device_id = g_sycl_gpu_mgr->gpus[i];
  11614. g_device_caps[i].vmm = 0;
  11615. dpct::device_info prop;
  11616. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  11617. prop, dpct::dev_mgr::instance().get_device(device_id))));
  11618. g_default_tensor_split[i] = total_vram;
  11619. total_vram += prop.get_global_mem_size();
  11620. g_device_caps[i].cc =
  11621. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  11622. }
  11623. for (int i = 0; i < g_device_count; ++i) {
  11624. g_default_tensor_split[i] /= total_vram;
  11625. }
  11626. for (int i = 0; i < g_device_count; ++i) {
  11627. SYCL_CHECK(ggml_sycl_set_device(i));
  11628. // create sycl streams
  11629. for (int is = 0; is < MAX_STREAMS; ++is) {
  11630. SYCL_CHECK(CHECK_TRY_ERROR(
  11631. g_syclStreams[i][is] =
  11632. dpct::get_current_device().create_queue(
  11633. g_sycl_gpu_mgr->get_co_ctx(), dpct::get_current_device())));
  11634. }
  11635. const dpct::queue_ptr stream = g_syclStreams[i][0];
  11636. // create sycl handle
  11637. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[i] = stream));
  11638. }
  11639. }
  11640. catch (sycl::exception const &exc) {
  11641. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11642. << ", line:" << __LINE__ << std::endl;
  11643. std::exit(1);
  11644. }
  11645. void *ggml_sycl_host_malloc(size_t size) try {
  11646. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  11647. return nullptr;
  11648. }
  11649. void * ptr = nullptr;
  11650. //allow to use dpct::get_in_order_queue() for host malloc
  11651. dpct::err0 err = CHECK_TRY_ERROR(
  11652. ptr = (void *)sycl::malloc_host(size, dpct::get_in_order_queue()));
  11653. if (err != 0) {
  11654. // clear the error
  11655. fprintf(
  11656. stderr,
  11657. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  11658. size / 1024.0 / 1024.0,
  11659. "syclGetErrorString is not supported");
  11660. return nullptr;
  11661. }
  11662. return ptr;
  11663. }
  11664. catch (sycl::exception const &exc) {
  11665. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11666. << ", line:" << __LINE__ << std::endl;
  11667. std::exit(1);
  11668. }
  11669. void ggml_sycl_host_free(void *ptr) try {
  11670. //allow to use dpct::get_in_order_queue() for host malloc
  11671. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  11672. }
  11673. catch (sycl::exception const &exc) {
  11674. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11675. << ", line:" << __LINE__ << std::endl;
  11676. std::exit(1);
  11677. }
  11678. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  11679. const struct ggml_tensor *src,
  11680. int64_t i3, int64_t i2,
  11681. int64_t i1_low, int64_t i1_high,
  11682. dpct::queue_ptr stream) try {
  11683. dpct::memcpy_direction kind;
  11684. char * src_ptr;
  11685. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  11686. kind = dpct::host_to_device;
  11687. src_ptr = (char *) src->data;
  11688. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  11689. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  11690. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  11691. kind = dpct::device_to_device;
  11692. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  11693. int id;
  11694. SYCL_CHECK(CHECK_TRY_ERROR(
  11695. id = get_current_device_id()));
  11696. // GGML_SYCL_DEBUG("current device index %d\n", id);
  11697. src_ptr = (char *) extra->data_device[id];
  11698. } else {
  11699. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  11700. GGML_ASSERT(false);
  11701. }
  11702. char * dst_ptr = (char *) dst;
  11703. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  11704. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  11705. const enum ggml_type type = src->type;
  11706. const int64_t ts = ggml_type_size(type);
  11707. const int64_t bs = ggml_blck_size(type);
  11708. int64_t i1_diff = i1_high - i1_low;
  11709. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  11710. if (nb0 == ts && nb1 == ts*ne0/bs) {
  11711. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  11712. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  11713. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  11714. kind, *stream));
  11715. } else if (nb0 == ts) {
  11716. return CHECK_TRY_ERROR(
  11717. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  11718. ts * ne0 / bs, i1_diff, kind, *stream));
  11719. } else {
  11720. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  11721. const void * rx = (const void *) ((const char *) x + i1*nb1);
  11722. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  11723. // pretend the row is a matrix with cols=1
  11724. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11725. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  11726. /*
  11727. DPCT1001:85: The statement could not be removed.
  11728. */
  11729. /*
  11730. DPCT1000:86: Error handling if-stmt was detected but could not be
  11731. rewritten.
  11732. */
  11733. if (r != 0) return r;
  11734. }
  11735. return 0;
  11736. }
  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 void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  11744. const ggml_tensor *src1, ggml_tensor *dst,
  11745. const float *src0_d, const float *src1_d,
  11746. float *dst_d, const dpct::queue_ptr &stream) {
  11747. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  11748. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11749. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  11750. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  11751. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  11752. const int32_t * src1_i32 = (const int32_t *) src1_d;
  11753. switch (src0->type) {
  11754. case GGML_TYPE_F16:
  11755. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  11756. src1_i32, dst_d, stream);
  11757. break;
  11758. case GGML_TYPE_F32:
  11759. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11760. break;
  11761. case GGML_TYPE_Q4_0:
  11762. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11763. break;
  11764. case GGML_TYPE_Q4_1:
  11765. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11766. break;
  11767. case GGML_TYPE_Q5_0:
  11768. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11769. break;
  11770. case GGML_TYPE_Q5_1:
  11771. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11772. break;
  11773. case GGML_TYPE_Q8_0:
  11774. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11775. break;
  11776. default:
  11777. // TODO: k-quants
  11778. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  11779. GGML_ASSERT(false);
  11780. break;
  11781. }
  11782. }
  11783. template <class op>
  11784. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  11785. const ggml_tensor *src1, ggml_tensor *dst,
  11786. const float *src0_dd, const float *src1_dd,
  11787. float *dst_dd,
  11788. const dpct::queue_ptr &main_stream) {
  11789. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  11790. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11791. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  11792. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  11793. (sycl::half *)dst_dd, main_stream);
  11794. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  11795. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  11796. main_stream);
  11797. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  11798. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  11799. main_stream);
  11800. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  11801. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  11802. main_stream);
  11803. } else {
  11804. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  11805. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  11806. GGML_ASSERT(false);
  11807. }
  11808. }
  11809. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  11810. const ggml_tensor *src1, ggml_tensor *dst,
  11811. const float *src0_d, const float *src1_d,
  11812. float *dst_d,
  11813. const dpct::queue_ptr &main_stream) {
  11814. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  11815. (void) src1;
  11816. (void) src1_d;
  11817. }
  11818. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  11819. ggml_tensor *dst, const float *src0_dd,
  11820. const float *src1_dd, float *dst_dd,
  11821. const dpct::queue_ptr &main_stream) {
  11822. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11823. }
  11824. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  11825. ggml_tensor *dst, const float *src0_dd,
  11826. const float *src1_dd, float *dst_dd,
  11827. const dpct::queue_ptr &main_stream) {
  11828. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11829. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11830. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11831. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  11832. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  11833. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  11834. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  11835. int offset = dst->op_params[3] / 4; // offset in bytes
  11836. 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);
  11837. (void) dst;
  11838. }
  11839. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  11840. ggml_tensor *dst, const float *src0_dd,
  11841. const float *src1_dd, float *dst_dd,
  11842. const dpct::queue_ptr &main_stream) {
  11843. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11844. }
  11845. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  11846. ggml_tensor *dst, const float *src0_dd,
  11847. const float *src1_dd, float *dst_dd,
  11848. const dpct::queue_ptr &main_stream) {
  11849. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11850. }
  11851. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  11852. ggml_tensor *dst, const float *src0_dd,
  11853. const float *src1_dd, float *dst_dd,
  11854. const dpct::queue_ptr &main_stream) {
  11855. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11856. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11857. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11858. (void) src1;
  11859. (void) dst;
  11860. (void) src1_dd;
  11861. }
  11862. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  11863. ggml_tensor *dst, const float *src0_dd,
  11864. const float *src1_dd, float *dst_dd,
  11865. const dpct::queue_ptr &main_stream) {
  11866. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11867. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11868. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11869. (void) src1;
  11870. (void) dst;
  11871. (void) src1_dd;
  11872. }
  11873. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  11874. const ggml_tensor *src1, ggml_tensor *dst,
  11875. const float *src0_dd, const float *src1_dd,
  11876. float *dst_dd,
  11877. const dpct::queue_ptr &main_stream) {
  11878. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11879. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11880. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11881. (void) src1;
  11882. (void) dst;
  11883. (void) src1_dd;
  11884. }
  11885. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  11886. ggml_tensor *dst, const float *src0_dd,
  11887. const float *src1_dd, float *dst_dd,
  11888. const dpct::queue_ptr &main_stream) {
  11889. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11890. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11891. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11892. (void) src1;
  11893. (void) dst;
  11894. (void) src1_dd;
  11895. }
  11896. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  11897. ggml_tensor *dst, const float *src0_dd,
  11898. const float *src1_dd, float *dst_dd,
  11899. const dpct::queue_ptr &main_stream) {
  11900. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11901. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11902. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11903. (void) src1;
  11904. (void) dst;
  11905. (void) src1_dd;
  11906. }
  11907. static void ggml_sycl_op_hardsigmoid(const ggml_tensor *src0,
  11908. const ggml_tensor *src1, ggml_tensor *dst,
  11909. const float *src0_dd, const float *src1_dd,
  11910. float *dst_dd,
  11911. const dpct::queue_ptr &main_stream) {
  11912. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11913. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11914. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11915. (void) src1;
  11916. (void) dst;
  11917. (void) src1_dd;
  11918. }
  11919. static void ggml_sycl_op_hardswish(const ggml_tensor *src0,
  11920. const ggml_tensor *src1, ggml_tensor *dst,
  11921. const float *src0_dd, const float *src1_dd,
  11922. float *dst_dd, const dpct::queue_ptr &main_stream) {
  11923. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11924. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11925. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11926. (void) src1;
  11927. (void) dst;
  11928. (void) src1_dd;
  11929. }
  11930. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  11931. const ggml_tensor *src1, ggml_tensor *dst,
  11932. const float *src0_dd, const float *src1_dd,
  11933. float *dst_dd,
  11934. const dpct::queue_ptr &main_stream) {
  11935. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11936. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11937. float negative_slope;
  11938. memcpy(&negative_slope, dst->op_params, sizeof(float));
  11939. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  11940. (void) src1;
  11941. (void) dst;
  11942. (void) src1_dd;
  11943. }
  11944. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  11945. ggml_tensor *dst, const float *src0_dd,
  11946. const float *src1_dd, float *dst_dd,
  11947. const dpct::queue_ptr &main_stream) {
  11948. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11949. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11950. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11951. (void) src1;
  11952. (void) dst;
  11953. (void) src1_dd;
  11954. }
  11955. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  11956. ggml_tensor *dst, const float *src0_dd,
  11957. const float *src1_dd, float *dst_dd,
  11958. const dpct::queue_ptr &main_stream) {
  11959. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11960. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11961. const int64_t ne00 = src0->ne[0];
  11962. const int64_t nrows = ggml_nrows(src0);
  11963. float eps;
  11964. memcpy(&eps, dst->op_params, sizeof(float));
  11965. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11966. (void) src1;
  11967. (void) dst;
  11968. (void) src1_dd;
  11969. }
  11970. inline void ggml_sycl_op_group_norm(const ggml_tensor *src0,
  11971. const ggml_tensor *src1, ggml_tensor *dst,
  11972. const float *src0_dd, const float *src1_dd,
  11973. float *dst_dd,
  11974. const dpct::queue_ptr &main_stream) {
  11975. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11976. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11977. int num_groups = dst->op_params[0];
  11978. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  11979. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  11980. (void) src1;
  11981. (void) dst;
  11982. (void) src1_dd;
  11983. }
  11984. inline void ggml_sycl_op_concat(const ggml_tensor *src0,
  11985. const ggml_tensor *src1, ggml_tensor *dst,
  11986. const float *src0_dd, const float *src1_dd,
  11987. float *dst_dd,
  11988. const dpct::queue_ptr &main_stream) {
  11989. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11990. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11991. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11992. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  11993. 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);
  11994. }
  11995. (void) src1;
  11996. (void) dst;
  11997. }
  11998. inline void ggml_sycl_op_upscale(const ggml_tensor *src0,
  11999. const ggml_tensor *src1, ggml_tensor *dst,
  12000. const float *src0_dd, const float *src1_dd,
  12001. float *dst_dd,
  12002. const dpct::queue_ptr &main_stream) {
  12003. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12004. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  12005. const float sf0 = (float)dst->ne[0]/src0->ne[0];
  12006. const float sf1 = (float)dst->ne[1]/src0->ne[1];
  12007. const float sf2 = (float)dst->ne[2]/src0->ne[2];
  12008. const float sf3 = (float)dst->ne[3]/src0->ne[3];
  12009. upscale_f32_sycl(src0_dd, dst_dd, src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
  12010. dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], sf0, sf1, sf2, sf3,
  12011. main_stream);
  12012. (void) src1;
  12013. (void) dst;
  12014. (void) src1_dd;
  12015. }
  12016. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  12017. ggml_tensor *dst, const float *src0_dd,
  12018. const float *src1_dd, float *dst_dd,
  12019. const dpct::queue_ptr &main_stream) {
  12020. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12021. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  12022. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  12023. pad_f32_sycl(src0_dd, dst_dd,
  12024. src0->ne[0], src0->ne[1], src0->ne[2],
  12025. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  12026. (void) src1;
  12027. (void) dst;
  12028. (void) src1_dd;
  12029. }
  12030. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  12031. const ggml_tensor *src1, ggml_tensor *dst,
  12032. const float *src0_dd, const float *src1_dd,
  12033. float *dst_dd,
  12034. const dpct::queue_ptr &main_stream) {
  12035. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12036. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12037. const int64_t ne00 = src0->ne[0];
  12038. const int64_t nrows = ggml_nrows(src0);
  12039. float eps;
  12040. memcpy(&eps, dst->op_params, sizeof(float));
  12041. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  12042. (void) src1;
  12043. (void) dst;
  12044. (void) src1_dd;
  12045. }
  12046. inline void ggml_sycl_op_mul_mat_q(
  12047. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12048. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12049. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12050. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12051. const dpct::queue_ptr &stream) try {
  12052. const int64_t ne00 = src0->ne[0];
  12053. const int64_t ne10 = src1->ne[0];
  12054. GGML_ASSERT(ne10 % QK8_1 == 0);
  12055. const int64_t ne0 = dst->ne[0];
  12056. const int64_t row_diff = row_high - row_low;
  12057. int device_id;
  12058. SYCL_CHECK(
  12059. CHECK_TRY_ERROR(device_id = get_current_device_id()));
  12060. // the main device has a larger memory buffer to hold the results from all GPUs
  12061. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  12062. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && device_id == g_main_device ? ne0 : row_diff;
  12063. switch (src0->type) {
  12064. case GGML_TYPE_Q4_0:
  12065. 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);
  12066. break;
  12067. case GGML_TYPE_Q4_1:
  12068. 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);
  12069. break;
  12070. case GGML_TYPE_Q5_0:
  12071. 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);
  12072. break;
  12073. case GGML_TYPE_Q5_1:
  12074. 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);
  12075. break;
  12076. case GGML_TYPE_Q8_0:
  12077. 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);
  12078. break;
  12079. case GGML_TYPE_Q2_K:
  12080. 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);
  12081. break;
  12082. case GGML_TYPE_Q3_K:
  12083. 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);
  12084. break;
  12085. case GGML_TYPE_Q4_K:
  12086. 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);
  12087. break;
  12088. case GGML_TYPE_Q5_K:
  12089. 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);
  12090. break;
  12091. case GGML_TYPE_Q6_K:
  12092. 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);
  12093. break;
  12094. default:
  12095. GGML_ASSERT(false);
  12096. break;
  12097. }
  12098. (void) src1;
  12099. (void) dst;
  12100. (void) src1_ddf_i;
  12101. }
  12102. catch (sycl::exception const &exc) {
  12103. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12104. << ", line:" << __LINE__ << std::endl;
  12105. std::exit(1);
  12106. }
  12107. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  12108. int64_t min_compute_capability = INT_MAX;
  12109. int64_t max_compute_capability = INT_MIN;
  12110. for (int i = 0; i < g_device_count; ++i) {
  12111. if (tensor_split[i] < (i + 1 < g_device_count ? tensor_split[i + 1] : 1.0f)) {
  12112. if (min_compute_capability > g_device_caps[i].cc) {
  12113. min_compute_capability = g_device_caps[i].cc;
  12114. }
  12115. if (max_compute_capability < g_device_caps[i].cc) {
  12116. max_compute_capability = g_device_caps[i].cc;
  12117. }
  12118. }
  12119. }
  12120. switch(type) {
  12121. case GGML_TYPE_Q4_0:
  12122. case GGML_TYPE_Q4_1:
  12123. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  12124. case GGML_TYPE_Q5_0:
  12125. case GGML_TYPE_Q5_1:
  12126. case GGML_TYPE_Q8_0:
  12127. return 64;
  12128. case GGML_TYPE_F16:
  12129. case GGML_TYPE_F32:
  12130. return 1;
  12131. case GGML_TYPE_Q2_K:
  12132. case GGML_TYPE_Q3_K:
  12133. case GGML_TYPE_Q4_K:
  12134. case GGML_TYPE_Q5_K:
  12135. case GGML_TYPE_IQ2_XXS:
  12136. case GGML_TYPE_IQ2_XS:
  12137. case GGML_TYPE_IQ2_S:
  12138. case GGML_TYPE_IQ1_S:
  12139. case GGML_TYPE_IQ1_M:
  12140. case GGML_TYPE_IQ3_XXS:
  12141. case GGML_TYPE_IQ4_XS:
  12142. case GGML_TYPE_IQ4_NL:
  12143. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  12144. case GGML_TYPE_IQ3_S:
  12145. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  12146. case GGML_TYPE_Q6_K:
  12147. return 64;
  12148. default:
  12149. GGML_ASSERT(false);
  12150. }
  12151. }
  12152. inline void ggml_sycl_op_mul_mat_vec_q(
  12153. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12154. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12155. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12156. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12157. const dpct::queue_ptr &stream) {
  12158. const int64_t ne10 = src1->ne[0];
  12159. GGML_ASSERT(ne10 % QK8_1 == 0);
  12160. const int64_t ne00 = src0->ne[0];
  12161. const int64_t row_diff = row_high - row_low;
  12162. int id;
  12163. SYCL_CHECK(
  12164. CHECK_TRY_ERROR(id = get_current_device_id()));
  12165. // the main device has a larger memory buffer to hold the results from all GPUs
  12166. // nrows_dst == nrows of the matrix that the kernel writes into
  12167. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne00 : row_diff;
  12168. switch (src0->type) {
  12169. case GGML_TYPE_Q4_0:
  12170. mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12171. break;
  12172. case GGML_TYPE_Q4_1:
  12173. mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12174. break;
  12175. case GGML_TYPE_Q5_0:
  12176. mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12177. break;
  12178. case GGML_TYPE_Q5_1:
  12179. mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12180. break;
  12181. case GGML_TYPE_Q8_0:
  12182. mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12183. break;
  12184. case GGML_TYPE_Q2_K:
  12185. mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12186. break;
  12187. case GGML_TYPE_Q3_K:
  12188. mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12189. break;
  12190. case GGML_TYPE_Q4_K:
  12191. mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12192. break;
  12193. case GGML_TYPE_Q5_K:
  12194. mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12195. break;
  12196. case GGML_TYPE_Q6_K:
  12197. mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12198. break;
  12199. case GGML_TYPE_IQ1_S:
  12200. mul_mat_vec_iq1_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12201. break;
  12202. case GGML_TYPE_IQ1_M:
  12203. mul_mat_vec_iq1_m_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12204. break;
  12205. case GGML_TYPE_IQ2_XXS:
  12206. mul_mat_vec_iq2_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12207. break;
  12208. case GGML_TYPE_IQ2_XS:
  12209. mul_mat_vec_iq2_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12210. break;
  12211. case GGML_TYPE_IQ2_S:
  12212. mul_mat_vec_iq2_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12213. break;
  12214. case GGML_TYPE_IQ3_XXS:
  12215. mul_mat_vec_iq3_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12216. break;
  12217. case GGML_TYPE_IQ3_S:
  12218. mul_mat_vec_iq3_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12219. break;
  12220. case GGML_TYPE_IQ4_NL:
  12221. mul_mat_vec_iq4_nl_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12222. break;
  12223. case GGML_TYPE_IQ4_XS:
  12224. mul_mat_vec_iq4_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12225. break;
  12226. default:
  12227. GGML_ASSERT(false);
  12228. break;
  12229. }
  12230. (void) src1;
  12231. (void) dst;
  12232. (void) src1_ddf_i;
  12233. (void) src1_ncols;
  12234. (void) src1_padded_row_size;
  12235. }
  12236. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  12237. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12238. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12239. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12240. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12241. const dpct::queue_ptr &stream) {
  12242. const int64_t ne00 = src0->ne[0];
  12243. const int64_t row_diff = row_high - row_low;
  12244. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12245. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  12246. #ifdef GGML_SYCL_F16
  12247. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  12248. sycl::half *src1_dfloat = nullptr; // dfloat == half
  12249. bool src1_convert_f16 =
  12250. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  12251. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  12252. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  12253. if (src1_convert_f16) {
  12254. src1_dfloat = src1_dfloat_a.alloc(ne00);
  12255. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12256. GGML_ASSERT(to_fp16_sycl != nullptr);
  12257. to_fp16_sycl(src1_ddf_i, src1_dfloat, ne00, stream);
  12258. }
  12259. #else
  12260. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  12261. #endif // GGML_SYCL_F16
  12262. switch (src0->type) {
  12263. case GGML_TYPE_Q4_0:
  12264. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12265. break;
  12266. case GGML_TYPE_Q4_1:
  12267. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12268. break;
  12269. case GGML_TYPE_Q5_0:
  12270. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12271. break;
  12272. case GGML_TYPE_Q5_1:
  12273. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12274. break;
  12275. case GGML_TYPE_Q8_0:
  12276. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12277. break;
  12278. case GGML_TYPE_Q2_K:
  12279. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12280. break;
  12281. case GGML_TYPE_Q3_K:
  12282. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12283. break;
  12284. case GGML_TYPE_Q4_K:
  12285. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12286. break;
  12287. case GGML_TYPE_Q5_K:
  12288. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12289. break;
  12290. case GGML_TYPE_Q6_K:
  12291. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12292. break;
  12293. case GGML_TYPE_F16:
  12294. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12295. break;
  12296. default:
  12297. printf("ggml_sycl_op_dequantize_mul_mat_vec unsupported GGML_TYPE %d\n", src0->type);
  12298. GGML_ASSERT(false);
  12299. break;
  12300. }
  12301. (void) src1;
  12302. (void) dst;
  12303. (void) src1_ddq_i;
  12304. (void) src1_ncols;
  12305. (void) src1_padded_row_size;
  12306. }
  12307. inline void ggml_sycl_op_mul_mat_sycl(
  12308. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12309. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12310. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12311. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12312. const dpct::queue_ptr &stream) try {
  12313. GGML_ASSERT(src0_dd_i != nullptr);
  12314. GGML_ASSERT(src1_ddf_i != nullptr);
  12315. GGML_ASSERT(dst_dd_i != nullptr);
  12316. const int64_t ne00 = src0->ne[0];
  12317. const int64_t ne10 = src1->ne[0];
  12318. const int64_t ne0 = dst->ne[0];
  12319. const int64_t row_diff = row_high - row_low;
  12320. int id;
  12321. SYCL_CHECK(
  12322. CHECK_TRY_ERROR(id = get_current_device_id()));
  12323. // the main device has a larger memory buffer to hold the results from all GPUs
  12324. // ldc == nrows of the matrix that cuBLAS writes into
  12325. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  12326. #ifdef GGML_SYCL_F16
  12327. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  12328. #else
  12329. bool use_fp16 = false;
  12330. #endif
  12331. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  12332. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  12333. dst->op_params[0] == GGML_PREC_DEFAULT) {
  12334. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  12335. sycl_pool_alloc<sycl::half> src0_as_f16;
  12336. if (src0->type != GGML_TYPE_F16) {
  12337. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  12338. GGML_ASSERT(to_fp16_sycl != nullptr);
  12339. size_t ne = row_diff*ne00;
  12340. src0_as_f16.alloc(ne);
  12341. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  12342. }
  12343. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  12344. ? (const sycl::half *)src0_dd_i
  12345. : src0_as_f16.get();
  12346. sycl_pool_alloc<sycl::half> src1_as_f16;
  12347. if (src1->type != GGML_TYPE_F16) {
  12348. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12349. GGML_ASSERT(to_fp16_sycl != nullptr);
  12350. size_t ne = src1_ncols*ne10;
  12351. src1_as_f16.alloc(ne);
  12352. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  12353. }
  12354. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  12355. ? (const sycl::half *)src1->data + src1_padded_row_size
  12356. : src1_as_f16.get();
  12357. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  12358. const sycl::half alpha_f16 = 1.0f;
  12359. const sycl::half beta_f16 = 0.0f;
  12360. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  12361. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  12362. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  12363. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12364. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  12365. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  12366. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  12367. dpct::library_data_t::real_half)));
  12368. g_sycl_handles[id]->wait();
  12369. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  12370. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  12371. }
  12372. else {
  12373. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  12374. sycl_pool_alloc<float> src0_ddq_as_f32;
  12375. sycl_pool_alloc<float> src1_ddq_as_f32;
  12376. if (src0->type != GGML_TYPE_F32) {
  12377. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  12378. GGML_ASSERT(to_fp32_sycl != nullptr);
  12379. src0_ddq_as_f32.alloc(row_diff*ne00);
  12380. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  12381. }
  12382. if (src1->type != GGML_TYPE_F32) {
  12383. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  12384. GGML_ASSERT(to_fp32_sycl != nullptr);
  12385. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  12386. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  12387. }
  12388. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  12389. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  12390. const float alpha = 1.0f;
  12391. const float beta = 0.0f;
  12392. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  12393. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  12394. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  12395. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12396. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  12397. src1_ddf1_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  12398. dst_dd_i, ldc)));
  12399. g_sycl_handles[id]->wait();
  12400. }
  12401. (void) dst;
  12402. (void) src1_ddq_i;
  12403. (void) src1_padded_row_size;
  12404. }
  12405. catch (sycl::exception const &exc) {
  12406. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12407. << ", line:" << __LINE__ << std::endl;
  12408. std::exit(1);
  12409. }
  12410. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  12411. ggml_tensor *dst, const float *src0_dd,
  12412. const float *src1_dd, float *dst_dd,
  12413. const dpct::queue_ptr &main_stream) {
  12414. #pragma message("TODO: implement phi3 frequency factors support")
  12415. #pragma message(" https://github.com/ggerganov/llama.cpp/pull/7225")
  12416. GGML_ASSERT(dst->src[2] == nullptr && "phi3 frequency factors not implemented yet");
  12417. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  12418. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  12419. GGML_ASSERT(src0->type == dst->type);
  12420. const int64_t ne00 = src0->ne[0];
  12421. const int64_t ne01 = src0->ne[1];
  12422. const int64_t ne2 = dst->ne[2];
  12423. const int64_t nrows = ggml_nrows(src0);
  12424. //const int n_past = ((int32_t *) dst->op_params)[0];
  12425. const int n_dims = ((int32_t *) dst->op_params)[1];
  12426. const int mode = ((int32_t *) dst->op_params)[2];
  12427. const int n_ctx = ((int32_t *) dst->op_params)[3];
  12428. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  12429. // RoPE alteration for extended context
  12430. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  12431. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  12432. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  12433. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  12434. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  12435. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  12436. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  12437. const int32_t * pos = nullptr;
  12438. if ((mode & 1) == 0) {
  12439. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  12440. GGML_ASSERT(src1->ne[0] == ne2);
  12441. pos = (const int32_t *) src1_dd;
  12442. }
  12443. const bool is_neox = mode & 2;
  12444. const bool is_glm = mode & 4;
  12445. rope_corr_dims corr_dims;
  12446. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  12447. // compute
  12448. if (is_glm) {
  12449. GGML_ASSERT(false);
  12450. rope_glm_f32_sycl(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  12451. } else if (is_neox) {
  12452. if (src0->type == GGML_TYPE_F32) {
  12453. rope_neox_sycl(
  12454. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12455. attn_factor, corr_dims, main_stream
  12456. );
  12457. } else if (src0->type == GGML_TYPE_F16) {
  12458. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  12459. ne00, n_dims, nrows, pos, freq_scale, ne01,
  12460. freq_base, ext_factor, attn_factor, corr_dims,
  12461. main_stream);
  12462. } else {
  12463. GGML_ASSERT(false);
  12464. }
  12465. } else {
  12466. if (src0->type == GGML_TYPE_F32) {
  12467. rope_sycl(
  12468. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12469. attn_factor, corr_dims, main_stream
  12470. );
  12471. } else if (src0->type == GGML_TYPE_F16) {
  12472. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  12473. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12474. attn_factor, corr_dims, main_stream);
  12475. } else {
  12476. GGML_ASSERT(false);
  12477. }
  12478. }
  12479. (void) src1;
  12480. (void) dst;
  12481. (void) src1_dd;
  12482. }
  12483. static void ggml_sycl_op_pool2d(const ggml_tensor *src0,
  12484. const ggml_tensor *src1, ggml_tensor *dst,
  12485. const float *src0_dd, const float *src1_dd,
  12486. float *dst_dd, const dpct::queue_ptr &main_stream) {
  12487. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12488. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12489. const int32_t * opts = (const int32_t *)dst->op_params;
  12490. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  12491. const int k0 = opts[1];
  12492. const int k1 = opts[2];
  12493. const int s0 = opts[3];
  12494. const int s1 = opts[4];
  12495. const int p0 = opts[5];
  12496. const int p1 = opts[6];
  12497. const int64_t IH = src0->ne[1];
  12498. const int64_t IW = src0->ne[0];
  12499. const int64_t N = dst->ne[3];
  12500. const int64_t OC = dst->ne[2];
  12501. const int64_t OH = dst->ne[1];
  12502. const int64_t OW = dst->ne[0];
  12503. const int parallel_elements = N * OC * OH * OW;
  12504. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  12505. sycl::range<3> block_nums(1, 1, num_blocks);
  12506. main_stream->parallel_for(
  12507. sycl::nd_range<3>(block_nums *
  12508. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  12509. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  12510. [=](sycl::nd_item<3> item_ct1) {
  12511. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  12512. parallel_elements, src0_dd, dst_dd, op,
  12513. item_ct1);
  12514. });
  12515. (void) src1;
  12516. (void) src1_dd;
  12517. }
  12518. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  12519. const ggml_tensor *src1, ggml_tensor *dst,
  12520. const float *src0_dd, const float *src1_dd,
  12521. float *dst_dd,
  12522. const dpct::queue_ptr &main_stream) {
  12523. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12524. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12525. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  12526. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  12527. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  12528. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  12529. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  12530. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  12531. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  12532. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  12533. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  12534. const int64_t IH = is_2D ? src1->ne[1] : 1;
  12535. const int64_t IW = src1->ne[0];
  12536. const int64_t KH = is_2D ? src0->ne[1] : 1;
  12537. const int64_t KW = src0->ne[0];
  12538. const int64_t OH = is_2D ? dst->ne[2] : 1;
  12539. const int64_t OW = dst->ne[1];
  12540. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  12541. if (dst->type == GGML_TYPE_F16) {
  12542. 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);
  12543. } else {
  12544. im2col_sycl(src1_dd, (float *)dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  12545. }
  12546. (void) src0;
  12547. (void) src0_dd;
  12548. }
  12549. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  12550. const ggml_tensor *src1, ggml_tensor *dst,
  12551. const float *src0_dd, const float *src1_dd,
  12552. float *dst_dd,
  12553. const dpct::queue_ptr &main_stream) {
  12554. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12555. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12556. const int64_t ncols = src0->ne[0];
  12557. const int64_t nrows = ggml_nrows(src0);
  12558. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  12559. (void) src1;
  12560. (void) dst;
  12561. (void) src1_dd;
  12562. }
  12563. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  12564. const ggml_tensor *src1, ggml_tensor *dst,
  12565. const float *src0_dd, const float *src1_dd,
  12566. float *dst_dd,
  12567. const dpct::queue_ptr &main_stream) {
  12568. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12569. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  12570. const int64_t ncols = src0->ne[0];
  12571. const int64_t nrows = ggml_nrows(src0);
  12572. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  12573. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  12574. (void) src1;
  12575. (void) dst;
  12576. (void) src1_dd;
  12577. }
  12578. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  12579. const ggml_tensor *src1,
  12580. ggml_tensor *dst, const float *src0_dd,
  12581. const float *src1_dd, float *dst_dd,
  12582. const dpct::queue_ptr &main_stream) {
  12583. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12584. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12585. const int64_t ne00 = src0->ne[0];
  12586. const int64_t ne01 = src0->ne[1];
  12587. const int nrows0 = ggml_nrows(src0);
  12588. const int n_past = ((int32_t *) dst->op_params)[0];
  12589. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  12590. (void) src1;
  12591. (void) dst;
  12592. (void) src1_dd;
  12593. }
  12594. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  12595. const ggml_tensor *src1, ggml_tensor *dst,
  12596. const float *src0_dd, const float *src1_dd,
  12597. float *dst_dd,
  12598. const dpct::queue_ptr &main_stream) {
  12599. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12600. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12601. #pragma message("TODO: add ggml_sycl_op_soft_max() F16 src1 support")
  12602. #pragma message("ref: https://github.com/ggerganov/llama.cpp/pull/5021")
  12603. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  12604. const int64_t ne00 = src0->ne[0];
  12605. const int64_t nrows_x = ggml_nrows(src0);
  12606. const int64_t nrows_y = src0->ne[1];
  12607. float scale = 1.0f;
  12608. float max_bias = 0.0f;
  12609. memcpy(&scale, dst->op_params + 0, sizeof(float));
  12610. memcpy(&max_bias, dst->op_params + 1, sizeof(float));
  12611. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00,
  12612. nrows_x, nrows_y, scale, max_bias, main_stream);
  12613. }
  12614. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  12615. ggml_tensor *dst, const float *src0_dd,
  12616. const float *src1_dd, float *dst_dd,
  12617. const dpct::queue_ptr &main_stream) {
  12618. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12619. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12620. float scale;
  12621. memcpy(&scale, dst->op_params, sizeof(float));
  12622. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  12623. /*
  12624. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  12625. error codes. The call was replaced with 0. You need to rewrite this code.
  12626. */
  12627. SYCL_CHECK(0);
  12628. (void) src1;
  12629. (void) dst;
  12630. (void) src1_dd;
  12631. }
  12632. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  12633. ggml_tensor *dst, const float *src0_dd,
  12634. const float *src1_dd, float *dst_dd,
  12635. const dpct::queue_ptr &main_stream) {
  12636. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12637. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12638. float min;
  12639. float max;
  12640. memcpy(&min, dst->op_params, sizeof(float));
  12641. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  12642. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  12643. /*
  12644. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  12645. error codes. The call was replaced with 0. You need to rewrite this code.
  12646. */
  12647. SYCL_CHECK(0);
  12648. (void) src1;
  12649. (void) dst;
  12650. (void) src1_dd;
  12651. }
  12652. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  12653. const ggml_tensor *src1, ggml_tensor *dst,
  12654. const ggml_sycl_op_flatten_t op) try {
  12655. const int64_t nrows0 = ggml_nrows(src0);
  12656. const bool use_src1 = src1 != nullptr;
  12657. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  12658. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12659. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12660. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12661. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  12662. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12663. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12664. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  12665. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  12666. // dd = data device
  12667. float * src0_ddf = nullptr;
  12668. float * src1_ddf = nullptr;
  12669. float * dst_ddf = nullptr;
  12670. sycl_pool_alloc<float> src0_f;
  12671. sycl_pool_alloc<float> src1_f;
  12672. sycl_pool_alloc<float> dst_f;
  12673. ggml_sycl_set_device(g_main_device);
  12674. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12675. // GGML_SYCL_DEBUG("g_main_device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  12676. // g_main_device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  12677. if (src0_on_device) {
  12678. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  12679. } else {
  12680. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  12681. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  12682. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  12683. }
  12684. if (use_src1) {
  12685. if (src1_on_device) {
  12686. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12687. } else {
  12688. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  12689. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  12690. }
  12691. }
  12692. if (dst_on_device) {
  12693. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12694. } else {
  12695. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  12696. }
  12697. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  12698. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12699. // do the computation
  12700. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12701. /*
  12702. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  12703. error codes. The call was replaced with 0. You need to rewrite this code.
  12704. */
  12705. SYCL_CHECK(0);
  12706. // copy dst to host if necessary
  12707. if (!dst_on_device) {
  12708. SYCL_CHECK(CHECK_TRY_ERROR(
  12709. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst)).wait()));
  12710. }
  12711. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12712. SYCL_CHECK(CHECK_TRY_ERROR(
  12713. dpct::get_current_device().queues_wait_and_throw()));
  12714. }
  12715. // print_ggml_tensor("tensor", dst);
  12716. }
  12717. catch (sycl::exception const &exc) {
  12718. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12719. << ", line:" << __LINE__ << std::endl;
  12720. std::exit(1);
  12721. }
  12722. static void ggml_sycl_set_peer_access(const int n_tokens) {
  12723. static bool peer_access_enabled = false;
  12724. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  12725. if (peer_access_enabled == enable_peer_access) {
  12726. return;
  12727. }
  12728. #ifdef NDEBUG
  12729. for (int i = 0; i < g_device_count; ++i) {
  12730. SYCL_CHECK(ggml_sycl_set_device(i));
  12731. // SYCL_CHECK(syclDeviceSynchronize());
  12732. }
  12733. for (int i = 0; i < g_device_count; ++i) {
  12734. SYCL_CHECK(ggml_sycl_set_device(i));
  12735. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  12736. if (i == id_other) {
  12737. continue;
  12738. }
  12739. if (i != g_main_device && id_other != g_main_device) {
  12740. continue;
  12741. }
  12742. // int can_access_peer;
  12743. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  12744. // if (can_access_peer) {
  12745. // if (enable_peer_access) {
  12746. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  12747. // } else {
  12748. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  12749. // }
  12750. // }
  12751. }
  12752. }
  12753. #endif // NDEBUG
  12754. peer_access_enabled = enable_peer_access;
  12755. }
  12756. struct ggml_backend_sycl_split_buffer_type_context {
  12757. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12758. };
  12759. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  12760. const ggml_tensor *src1, ggml_tensor *dst,
  12761. ggml_sycl_op_mul_mat_t op,
  12762. const bool convert_src1_to_q8_1) try {
  12763. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  12764. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  12765. const int64_t nrows1 = ggml_nrows(src1);
  12766. GGML_ASSERT(ne03 == ne13);
  12767. const int64_t ne0 = dst->ne[0];
  12768. const int64_t ne1 = dst->ne[1];
  12769. const int nb2 = dst->nb[2];
  12770. const int nb3 = dst->nb[3];
  12771. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12772. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12773. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  12774. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  12775. const int64_t i02_divisor = ne12 / ne02;
  12776. const size_t src0_ts = ggml_type_size(src0->type);
  12777. const size_t src0_bs = ggml_blck_size(src0->type);
  12778. const size_t q8_1_ts = sizeof(block_q8_1);
  12779. const size_t q8_1_bs = QK8_1;
  12780. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12781. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12782. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12783. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12784. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  12785. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  12786. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  12787. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12788. GGML_ASSERT(!(split && ne02 > 1));
  12789. GGML_ASSERT(!(split && ne03 > 1));
  12790. GGML_ASSERT(!(split && ne02 < ne12));
  12791. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12792. if (split) {
  12793. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  12794. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  12795. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  12796. tensor_split = buft_ctx->tensor_split;
  12797. }
  12798. struct dev_data {
  12799. sycl_pool_alloc<char> src0_dd_alloc;
  12800. sycl_pool_alloc<float> src1_ddf_alloc;
  12801. sycl_pool_alloc<char> src1_ddq_alloc;
  12802. sycl_pool_alloc<float> dst_dd_alloc;
  12803. char *src0_dd = nullptr;
  12804. float *src1_ddf = nullptr; // float
  12805. char *src1_ddq = nullptr; // q8_1
  12806. float *dst_dd = nullptr;
  12807. int64_t row_low;
  12808. int64_t row_high;
  12809. };
  12810. dev_data dev[GGML_SYCL_MAX_DEVICES];
  12811. int used_devices = 0;
  12812. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12813. for (int i = 0; i < g_device_count; ++i) {
  12814. // by default, use all rows
  12815. dev[i].row_low = 0;
  12816. dev[i].row_high = ne01;
  12817. // for multi GPU, get the row boundaries from tensor split
  12818. // and round to mul_mat_q tile sizes
  12819. if (split) {
  12820. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  12821. if (i != 0) {
  12822. dev[i].row_low = ne01*tensor_split[i];
  12823. if (dev[i].row_low < ne01) {
  12824. dev[i].row_low -= dev[i].row_low % rounding;
  12825. }
  12826. }
  12827. if (i != g_device_count - 1) {
  12828. dev[i].row_high = ne01*tensor_split[i + 1];
  12829. if (dev[i].row_high < ne01) {
  12830. dev[i].row_high -= dev[i].row_high % rounding;
  12831. }
  12832. }
  12833. }
  12834. }
  12835. for (int i = 0; i < g_device_count; ++i) {
  12836. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12837. continue;
  12838. }
  12839. used_devices++;
  12840. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12841. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12842. ggml_sycl_set_device(i);
  12843. dpct::queue_ptr stream = g_syclStreams[i][0];
  12844. if (src0_on_device && src0_is_contiguous) {
  12845. dev[i].src0_dd = (char *) src0_extra->data_device[i];
  12846. } else {
  12847. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ggml_nbytes(src0));
  12848. }
  12849. if (src1_on_device && src1_is_contiguous) {
  12850. dev[i].src1_ddf = (float *) src1_extra->data_device[i];
  12851. } else {
  12852. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ggml_nelements(src1));
  12853. }
  12854. if (convert_src1_to_q8_1) {
  12855. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  12856. if (src1_on_device && src1_is_contiguous) {
  12857. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  12858. /*
  12859. DPCT1010:90: SYCL uses exceptions to report errors and does not
  12860. use the error codes. The call was replaced with 0. You need to
  12861. rewrite this code.
  12862. */
  12863. SYCL_CHECK(0);
  12864. }
  12865. }
  12866. if (dst_on_device) {
  12867. dev[i].dst_dd = (float *) dst_extra->data_device[i];
  12868. } else {
  12869. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  12870. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(size_dst_ddf);
  12871. }
  12872. }
  12873. // if multiple devices are used they need to wait for the main device
  12874. // here an event is recorded that signals that the main device has finished calculating the input data
  12875. if (split && used_devices > 1) {
  12876. ggml_sycl_set_device(g_main_device);
  12877. /*
  12878. DPCT1024:91: The original code returned the error code that was further
  12879. consumed by the program logic. This original code was replaced with 0.
  12880. You may need to rewrite the program logic consuming the error code.
  12881. */
  12882. SYCL_CHECK(CHECK_TRY_ERROR(
  12883. *src0_extra->events[g_main_device][0] =
  12884. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier()));
  12885. }
  12886. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  12887. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  12888. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  12889. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  12890. for (int i = 0; i < g_device_count; ++i) {
  12891. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12892. continue;
  12893. }
  12894. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12895. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12896. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  12897. ggml_sycl_set_device(i);
  12898. dpct::queue_ptr stream = g_syclStreams[i][is];
  12899. // wait for main GPU data if necessary
  12900. if (split && (i != g_main_device || is != 0)) {
  12901. /*
  12902. DPCT1009:163: SYCL uses exceptions to report errors and does not
  12903. use the error codes. The original code was commented out and a
  12904. warning string was inserted. You need to rewrite this code.
  12905. */
  12906. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  12907. {*src0_extra->events[g_main_device][0]})));
  12908. }
  12909. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  12910. const int64_t i03 = i0 / ne12;
  12911. const int64_t i02 = i0 % ne12;
  12912. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  12913. // for split tensors the data begins at i0 == i0_offset_low
  12914. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  12915. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  12916. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  12917. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  12918. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  12919. // in that case an offset on dst_ddf_i is needed
  12920. if (dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device) {
  12921. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  12922. }
  12923. // copy src0, src1 to device if necessary
  12924. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  12925. if (i != g_main_device) {
  12926. if (convert_src1_to_q8_1) {
  12927. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  12928. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  12929. src1_ddq_i, src1_ddq_i_source,
  12930. src1_ncols * src1_padded_col_size * q8_1_ts /
  12931. q8_1_bs).wait()));
  12932. } else {
  12933. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  12934. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  12935. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  12936. src1_ddf_i, src1_ddf_i_source,
  12937. src1_ncols * ne10 * sizeof(float))));
  12938. }
  12939. }
  12940. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  12941. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  12942. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  12943. } else {
  12944. GGML_ASSERT(false);
  12945. }
  12946. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  12947. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  12948. /*
  12949. DPCT1010:92: SYCL uses exceptions to report errors and does
  12950. not use the error codes. The call was replaced with 0. You
  12951. need to rewrite this code.
  12952. */
  12953. SYCL_CHECK(0);
  12954. }
  12955. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  12956. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  12957. }
  12958. if (src1->type == GGML_TYPE_F16) {
  12959. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  12960. }
  12961. // do the computation
  12962. SYCL_CHECK(CHECK_TRY_ERROR(op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  12963. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream)));
  12964. /*
  12965. DPCT1010:93: SYCL uses exceptions to report errors and does not
  12966. use the error codes. The call was replaced with 0. You need to
  12967. rewrite this code.
  12968. */
  12969. SYCL_CHECK(0);
  12970. // copy dst to host or other device if necessary
  12971. if (!dst_on_device) {
  12972. void * dst_off_device;
  12973. dpct::memcpy_direction kind;
  12974. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12975. dst_off_device = dst->data;
  12976. kind = dpct::device_to_host;
  12977. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  12978. dst_off_device = dst_extra->data_device[g_main_device];
  12979. kind = dpct::device_to_device;
  12980. } else {
  12981. GGML_ASSERT(false);
  12982. }
  12983. if (split) {
  12984. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  12985. // dst is NOT transposed.
  12986. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  12987. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  12988. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  12989. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12990. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12991. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  12992. //todo, dirty solution. Need be updated when device2device memcpy() is supported.
  12993. if (kind == dpct::device_to_device) {
  12994. size_t dst_size = ggml_nbytes_pad(dst);
  12995. float *host_buf = (float *)malloc(dst_size);
  12996. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12997. host_buf, ne0 * sizeof(float), dst_dd_i,
  12998. row_diff * sizeof(float), row_diff * sizeof(float),
  12999. src1_ncols, dpct::device_to_host, *stream)));
  13000. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[i]).queues_wait_and_throw();
  13001. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  13002. dhf_dst_i, ne0 * sizeof(float), host_buf,
  13003. row_diff * sizeof(float), row_diff * sizeof(float),
  13004. src1_ncols, dpct::host_to_device, *main_stream)));
  13005. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[g_main_device]).queues_wait_and_throw();
  13006. free(host_buf);
  13007. } else {
  13008. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  13009. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  13010. row_diff * sizeof(float), row_diff * sizeof(float),
  13011. src1_ncols, kind, *stream)));
  13012. }
  13013. } else {
  13014. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  13015. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  13016. dhf_dst_i += src1_col_0*ne0;
  13017. SYCL_CHECK(CHECK_TRY_ERROR(
  13018. stream->memcpy(dhf_dst_i, dst_dd_i,
  13019. src1_ncols * ne0 * sizeof(float)).wait()));
  13020. }
  13021. }
  13022. // add event for the main device to wait on until other device is done
  13023. if (split && (i != g_main_device || is != 0)) {
  13024. /*
  13025. DPCT1024:94: The original code returned the error code that
  13026. was further consumed by the program logic. This original
  13027. code was replaced with 0. You may need to rewrite the
  13028. program logic consuming the error code.
  13029. */
  13030. SYCL_CHECK(CHECK_TRY_ERROR(
  13031. *src0_extra->events[i][is] =
  13032. stream->ext_oneapi_submit_barrier()));
  13033. }
  13034. }
  13035. }
  13036. }
  13037. // main device waits for all other devices to be finished
  13038. if (split && g_device_count > 1) {
  13039. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  13040. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  13041. ggml_sycl_set_device(g_main_device);
  13042. for (int i = 0; i < g_device_count; ++i) {
  13043. if (dev[i].row_low == dev[i].row_high) {
  13044. continue;
  13045. }
  13046. for (int64_t is = 0; is < is_max; ++is) {
  13047. SYCL_CHECK(CHECK_TRY_ERROR(
  13048. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier(
  13049. {*src0_extra->events[i][is]})));
  13050. }
  13051. }
  13052. }
  13053. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  13054. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13055. SYCL_CHECK(CHECK_TRY_ERROR(
  13056. dpct::get_current_device().queues_wait_and_throw()));
  13057. }
  13058. }
  13059. catch (sycl::exception const &exc) {
  13060. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13061. << ", line:" << __LINE__ << std::endl;
  13062. std::exit(1);
  13063. }
  13064. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13065. GGML_SYCL_DEBUG("call %s\n", __func__);
  13066. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  13067. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13068. }
  13069. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13070. GGML_SYCL_DEBUG("call %s\n", __func__);
  13071. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  13072. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13073. }
  13074. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13075. GGML_SYCL_DEBUG("call %s\n", __func__);
  13076. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  13077. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13078. }
  13079. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13080. GGML_SYCL_DEBUG("call %s\n", __func__);
  13081. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  13082. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13083. }
  13084. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13085. GGML_SYCL_DEBUG("call %s\n", __func__);
  13086. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  13087. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13088. }
  13089. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13090. GGML_SYCL_DEBUG("call %s\n", __func__);
  13091. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  13092. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13093. }
  13094. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13095. GGML_SYCL_DEBUG("call %s\n", __func__);
  13096. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  13097. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13098. }
  13099. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13100. GGML_SYCL_DEBUG("call %s\n", __func__);
  13101. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  13102. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13103. }
  13104. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13105. GGML_SYCL_DEBUG("call %s\n", __func__);
  13106. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  13107. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13108. }
  13109. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13110. GGML_SYCL_DEBUG("call %s\n", __func__);
  13111. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  13112. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13113. }
  13114. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13115. GGML_SYCL_DEBUG("call %s\n", __func__);
  13116. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  13117. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13118. }
  13119. static void ggml_sycl_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13120. GGML_SYCL_DEBUG("call %s\n", __func__);
  13121. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardsigmoid);
  13122. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13123. }
  13124. static void ggml_sycl_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13125. GGML_SYCL_DEBUG("call %s\n", __func__);
  13126. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardswish);
  13127. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13128. }
  13129. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13130. GGML_SYCL_DEBUG("call %s\n", __func__);
  13131. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  13132. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13133. }
  13134. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13135. GGML_SYCL_DEBUG("call %s\n", __func__);
  13136. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  13137. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13138. }
  13139. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13140. GGML_SYCL_DEBUG("call %s\n", __func__);
  13141. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  13142. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13143. }
  13144. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13145. GGML_SYCL_DEBUG("call %s\n", __func__);
  13146. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  13147. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13148. }
  13149. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13150. GGML_SYCL_DEBUG("call %s\n", __func__);
  13151. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  13152. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13153. }
  13154. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13155. GGML_SYCL_DEBUG("call %s\n", __func__);
  13156. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  13157. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13158. }
  13159. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13160. GGML_SYCL_DEBUG("call %s\n", __func__);
  13161. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  13162. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13163. }
  13164. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13165. GGML_SYCL_DEBUG("call %s\n", __func__);
  13166. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  13167. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13168. }
  13169. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  13170. if (!g_sycl_loaded) return false;
  13171. const int64_t ne10 = src1->ne[0];
  13172. const int64_t ne0 = dst->ne[0];
  13173. const int64_t ne1 = dst->ne[1];
  13174. // TODO: find the optimal values for these
  13175. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  13176. src1->type == GGML_TYPE_F32 &&
  13177. dst->type == GGML_TYPE_F32 &&
  13178. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  13179. }
  13180. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  13181. const ggml_tensor *src1,
  13182. ggml_tensor *dst) try {
  13183. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  13184. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13185. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  13186. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  13187. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13188. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13189. const int64_t ne00 = src0->ne[0];
  13190. const int64_t ne01 = src0->ne[1];
  13191. const int64_t ne02 = src0->ne[2];
  13192. const int64_t ne12 = src1->ne[2];
  13193. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13194. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13195. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13196. void * src0_ddq = src0_extra->data_device[g_main_device];
  13197. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13198. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13199. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13200. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13201. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  13202. }
  13203. catch (sycl::exception const &exc) {
  13204. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13205. << ", line:" << __LINE__ << std::endl;
  13206. std::exit(1);
  13207. }
  13208. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  13209. const ggml_tensor *src1,
  13210. ggml_tensor *dst) try {
  13211. GGML_ASSERT(!ggml_is_transposed(src0));
  13212. GGML_ASSERT(!ggml_is_transposed(src1));
  13213. GGML_ASSERT(!ggml_is_permuted(src0));
  13214. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13215. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13216. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13217. const int64_t ne00 = src0->ne[0];
  13218. const int64_t ne01 = src0->ne[1];
  13219. const int64_t ne02 = src0->ne[2];
  13220. const int64_t nb01 = src0->nb[1];
  13221. const int64_t nb02 = src0->nb[2];
  13222. const int64_t ne12 = src1->ne[2];
  13223. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13224. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13225. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13226. void * src0_ddq = src0_extra->data_device[g_main_device];
  13227. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13228. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13229. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13230. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13231. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  13232. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  13233. 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);
  13234. }
  13235. catch (sycl::exception const &exc) {
  13236. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13237. << ", line:" << __LINE__ << std::endl;
  13238. std::exit(1);
  13239. }
  13240. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  13241. const sycl::half *src1_as_f16, char *dst,
  13242. const void **ptrs_src, void **ptrs_dst,
  13243. int64_t ne12, int64_t ne13, int64_t ne23,
  13244. size_t nb02, size_t nb03, size_t nb12,
  13245. size_t nb13, size_t nbd2, size_t nbd3,
  13246. int64_t r2, int64_t r3,
  13247. const sycl::nd_item<3> &item_ct1) {
  13248. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  13249. item_ct1.get_local_id(2);
  13250. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  13251. item_ct1.get_local_id(1);
  13252. if (i13 >= ne13 || i12 >= ne12) {
  13253. return;
  13254. }
  13255. int64_t i03 = i13 / r3;
  13256. int64_t i02 = i12 / r2;
  13257. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  13258. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  13259. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  13260. }
  13261. static void ggml_sycl_mul_mat_batched_sycl(const ggml_tensor *src0,
  13262. const ggml_tensor *src1,
  13263. ggml_tensor *dst) try {
  13264. GGML_ASSERT(!ggml_is_transposed(src0));
  13265. GGML_ASSERT(!ggml_is_transposed(src1));
  13266. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13267. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13268. GGML_TENSOR_BINARY_OP_LOCALS
  13269. const int64_t ne_dst = ggml_nelements(dst);
  13270. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13271. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13272. bool no_mixed_dtypes = main_stream->get_backend() == sycl::backend::ext_oneapi_cuda ||
  13273. main_stream->get_backend() == sycl::backend::ext_oneapi_hip;
  13274. SYCL_CHECK(
  13275. CHECK_TRY_ERROR(g_sycl_handles[g_main_device] = main_stream));
  13276. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13277. void * src0_ddq = src0_extra->data_device[g_main_device];
  13278. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  13279. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13280. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13281. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13282. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13283. // convert src1 to fp16
  13284. sycl_pool_alloc<sycl::half> src1_f16_alloc;
  13285. if (src1->type != GGML_TYPE_F16) {
  13286. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13287. const int64_t ne_src1 = ggml_nelements(src1);
  13288. src1_f16_alloc.alloc(ne_src1);
  13289. GGML_ASSERT(to_fp16_sycl != nullptr);
  13290. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  13291. }
  13292. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  13293. : src1_f16_alloc.get();
  13294. sycl_pool_alloc<sycl::half> dst_f16;
  13295. char * dst_t;
  13296. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_float;
  13297. dpct::library_data_t cu_data_type = dpct::library_data_t::real_float;
  13298. if (no_mixed_dtypes) {
  13299. cu_compute_type = dpct::library_data_t::real_half;
  13300. cu_data_type = dpct::library_data_t::real_half;
  13301. }
  13302. // dst strides
  13303. size_t nbd2 = dst->nb[2];
  13304. size_t nbd3 = dst->nb[3];
  13305. const float alpha_f32 = 1.0f;
  13306. const float beta_f32 = 0.0f;
  13307. const sycl::half alpha_f16 = 1.0f;
  13308. const sycl::half beta_f16 = 0.0f;
  13309. const void * alpha = &alpha_f32;
  13310. const void * beta = &beta_f32;
  13311. if (no_mixed_dtypes) {
  13312. alpha = &alpha_f16;
  13313. beta = &beta_f16;
  13314. }
  13315. // TODO: Renable (dst->op_params[0] =! GGML_PREC_DEFAULT) pathway
  13316. // when oneMKL open source supports half, half, float, float: datatypes
  13317. dst_t = (char *) dst_ddf;
  13318. if (no_mixed_dtypes) {
  13319. dst_t = (char *) dst_f16.alloc(ne_dst);
  13320. nbd2 /= sizeof(float) / sizeof(sycl::half);
  13321. nbd3 /= sizeof(float) / sizeof(sycl::half);
  13322. }
  13323. GGML_ASSERT(ne12 % ne02 == 0);
  13324. GGML_ASSERT(ne13 % ne03 == 0);
  13325. // broadcast factors
  13326. const int64_t r2 = ne12/ne02;
  13327. const int64_t r3 = ne13/ne03;
  13328. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  13329. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  13330. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  13331. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  13332. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  13333. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  13334. nb01 / nb00, nb02 / nb00,
  13335. (const char *)src1_f16, dpct::library_data_t::real_half,
  13336. nb11 / nb10, nb12 / nb10, beta,
  13337. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  13338. ne12 * ne13, cu_compute_type)));
  13339. } else {
  13340. const int ne23 = ne12*ne13;
  13341. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  13342. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  13343. sycl::range<3> block_dims(1, ne12, ne13);
  13344. /*
  13345. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  13346. the limit. To get the device limit, query
  13347. info::device::max_work_group_size. Adjust the work-group size if needed.
  13348. */
  13349. {
  13350. dpct::has_capability_or_fail(main_stream->get_device(),
  13351. {sycl::aspect::fp16});
  13352. main_stream->submit([&](sycl::handler &cgh) {
  13353. const void **ptrs_src_get = ptrs_src.get();
  13354. void **ptrs_dst_get = ptrs_dst.get();
  13355. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  13356. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  13357. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  13358. [=](sycl::nd_item<3> item_ct1) {
  13359. k_compute_batched_ptrs(
  13360. src0_as_f16, src1_f16,
  13361. dst_t, ptrs_src_get,
  13362. ptrs_dst_get, ne12, ne13, ne23,
  13363. nb02, nb03, nb12_scaled, nb13_scaled,
  13364. nbd2, nbd3, r2, r3, item_ct1);
  13365. });
  13366. });
  13367. }
  13368. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  13369. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  13370. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  13371. (const void **)(ptrs_src.get() + 0 * ne23),
  13372. dpct::library_data_t::real_half, nb01 / nb00,
  13373. (const void **)(ptrs_src.get() + 1 * ne23),
  13374. dpct::library_data_t::real_half, nb11 / nb10, beta,
  13375. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  13376. cu_compute_type)));
  13377. }
  13378. if (no_mixed_dtypes) {
  13379. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13380. to_fp32_sycl(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  13381. }
  13382. }
  13383. catch (sycl::exception const &exc) {
  13384. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13385. << ", line:" << __LINE__ << std::endl;
  13386. std::exit(1);
  13387. }
  13388. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13389. const bool all_on_device =
  13390. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  13391. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  13392. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  13393. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  13394. int64_t min_compute_capability = INT_MAX;
  13395. for (int i = 0; i < g_device_count; ++i) {
  13396. 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)) {
  13397. min_compute_capability = g_device_caps[i].cc;
  13398. }
  13399. }
  13400. #ifdef SYCL_USE_XMX
  13401. const bool use_xmx = true;
  13402. #else
  13403. const bool use_xmx = false;
  13404. #endif
  13405. // debug helpers
  13406. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  13407. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  13408. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  13409. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  13410. //printf("src0 is contiguous %d, transposed %d, type = %s, name = %s\n", ggml_is_contiguous(src0), ggml_is_transposed(src0), ggml_type_name(src0->type), src0->name);
  13411. //printf("src1 is contiguous %d, transposed %d, type = %s, name = %s\n", ggml_is_contiguous(src1), ggml_is_transposed(src1), ggml_type_name(src1->type), src1->name);
  13412. if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  13413. // KQ single-batch
  13414. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_p021\n");
  13415. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  13416. } else if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && !ggml_is_contiguous(src0) && !ggml_is_transposed(src1) && src1->ne[1] == 1) {
  13417. // KQV single-batch
  13418. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_nc\n");
  13419. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  13420. } else if (!split && all_on_device && use_xmx && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  13421. // KQ + KQV multi-batch
  13422. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_batched_sycl\n");
  13423. ggml_sycl_mul_mat_batched_sycl(src0, src1, dst);
  13424. } else if (src0->type == GGML_TYPE_F32) {
  13425. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat\n");
  13426. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13427. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  13428. // GGML_SYCL_DEBUG("ggml_is_quantized or GGML_TYPE_F16\n");
  13429. if (src1->ne[1] == 1 && src0->ne[0] % GGML_SYCL_DMMV_X == 0) {
  13430. #ifdef GGML_SYCL_FORCE_DMMV
  13431. const bool use_mul_mat_vec_q = false;
  13432. #else
  13433. bool use_mul_mat_vec_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  13434. use_mul_mat_vec_q = use_mul_mat_vec_q ||
  13435. (src0->type == GGML_TYPE_IQ2_XXS) || (src0->type == GGML_TYPE_IQ2_XS) || (src0->type == GGML_TYPE_IQ2_S) ||
  13436. (src0->type == GGML_TYPE_IQ3_XXS) || (src0->type == GGML_TYPE_IQ3_S) ||
  13437. (src0->type == GGML_TYPE_IQ4_NL) || (src0->type == GGML_TYPE_IQ4_XS) ||
  13438. (src0->type == GGML_TYPE_IQ1_S) || (src0->type == GGML_TYPE_IQ1_M);
  13439. #endif // GGML_SYCL_FORCE_DMMV
  13440. if (use_mul_mat_vec_q) {
  13441. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_vec_q path\n");
  13442. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  13443. } else {
  13444. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_dequantize_mul_mat_vec path\n");
  13445. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  13446. }
  13447. } else {
  13448. bool use_mul_mat_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  13449. if (use_xmx && min_compute_capability >= VER_GEN9 && src1->ne[1] > XMX_MAX_BATCH_SIZE) {
  13450. use_mul_mat_q = false;
  13451. }
  13452. if (use_mul_mat_q) {
  13453. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_q path\n");
  13454. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  13455. } else {
  13456. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_sycl path\n");
  13457. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13458. }
  13459. }
  13460. } else {
  13461. GGML_ASSERT(false);
  13462. }
  13463. }
  13464. #if 0
  13465. template<typename ... Srcs>
  13466. static __global__ void k_compute_batched_ptrs_id(
  13467. const void ** ptrs_src, void ** ptrs_dst,
  13468. int ne12, int ne13,
  13469. int ne23,
  13470. int nb02, int nb03,
  13471. int nb12, int nb13,
  13472. int nb2, int nb3,
  13473. int r2, int r3,
  13474. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  13475. const half * src1_f16, half * dst_f16,
  13476. const int32_t * ids, const int id,
  13477. Srcs... src0s) {
  13478. int i = ids[id];
  13479. half * src0_f16;
  13480. const void * srcs_ar[] = { (const half *) src0s... };
  13481. if (src0_type == GGML_TYPE_F16) {
  13482. src0_f16 = (half *) srcs_ar[i];
  13483. } else {
  13484. src0_f16 = src0_as_f16;
  13485. if (item_ct1.get_local_id(2) == 0 && threadIdx.y == 0) {
  13486. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  13487. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  13488. }
  13489. }
  13490. int i13 = blockIdx.x * blockDim.x + item_ct1.get_local_id(2);
  13491. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  13492. if (i13 >= ne13 || i12 >= ne12) {
  13493. return;
  13494. }
  13495. int i03 = i13 / r3;
  13496. int i02 = i12 / r2;
  13497. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  13498. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  13499. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  13500. }
  13501. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  13502. const struct ggml_tensor * ids = dst->src[0];
  13503. const struct ggml_tensor * src1 = dst->src[1];
  13504. const struct ggml_tensor * src00 = dst->src[2];
  13505. const int id = dst->op_params[0];
  13506. GGML_ASSERT(!ggml_is_transposed(src00));
  13507. GGML_ASSERT(!ggml_is_transposed(src1));
  13508. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13509. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13510. GGML_TENSOR_LOCALS(int64_t, ne0, src00, ne);
  13511. //const int64_t nb01 = src00->nb[1];
  13512. GGML_TENSOR_LOCALS(int64_t, nb0, src00, nb);
  13513. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  13514. GGML_TENSOR_LOCALS(int64_t, nb1, src1, nb);
  13515. //const int64_t nb11 = src1->nb[1];
  13516. const int64_t ne1 = ggml_nelements(src1);
  13517. const int64_t ne = ggml_nelements(dst);
  13518. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13519. syclStream_t main_stream = g_syclStreams[g_main_device][0];
  13520. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device], main_stream));
  13521. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13522. //void * src0_ddq = src0_extra->data_device[g_main_device];
  13523. //half * src0_as_f16 = (half *) src0_ddq;
  13524. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13525. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13526. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13527. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13528. // convert src1 to fp16
  13529. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13530. GGML_ASSERT(to_fp16_sycl != nullptr);
  13531. size_t src1_as = 0;
  13532. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne1 * sizeof(half), &src1_as);
  13533. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  13534. size_t dst_as = 0;
  13535. half * dst_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne * sizeof(half), &dst_as);
  13536. GGML_ASSERT(ne12 % ne02 == 0);
  13537. GGML_ASSERT(ne13 % ne03 == 0);
  13538. // broadcast factors
  13539. const int64_t r2 = ne12/ne02;
  13540. const int64_t r3 = ne13/ne03;
  13541. const half alpha_f16 = 1.0f;
  13542. const half beta_f16 = 0.0f;
  13543. // use syclGemmBatchedEx
  13544. const int ne23 = ne12*ne13;
  13545. const void ** ptrs_src = nullptr;
  13546. void ** ptrs_dst = nullptr;
  13547. size_t ptrs_src_s = 0;
  13548. size_t ptrs_dst_s = 0;
  13549. ptrs_src = (const void **) ggml_sycl_pool_malloc(g_main_device, 2*ne23*sizeof(void *), &ptrs_src_s);
  13550. ptrs_dst = ( void **) ggml_sycl_pool_malloc(g_main_device, 1*ne23*sizeof(void *), &ptrs_dst_s);
  13551. int64_t src0_ne = ggml_nelements(src00);
  13552. half * src0_as_f16 = nullptr;
  13553. size_t src0_as = 0;
  13554. if (src00->type != GGML_TYPE_F16) {
  13555. src0_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, src0_ne * sizeof(half), &src0_as);
  13556. }
  13557. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  13558. dim3 block_dims(ne13, ne12);
  13559. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  13560. ptrs_src, ptrs_dst,
  13561. ne12, ne13,
  13562. ne23,
  13563. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  13564. nb12, nb13,
  13565. dst->nb[2], dst->nb[3],
  13566. r2, r3,
  13567. src00->type, src0_as_f16, src0_ne,
  13568. src1_as_f16, dst_f16,
  13569. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  13570. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  13571. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  13572. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  13573. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  13574. );
  13575. SYCL_CHECK(syclGetLastError());
  13576. SYCL_CHECK(
  13577. syclGemmBatchedEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  13578. ne01, ne11, ne10,
  13579. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  13580. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  13581. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  13582. ne23,
  13583. CUBLAS_COMPUTE_16F,
  13584. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  13585. if (src0_as != 0) {
  13586. ggml_sycl_pool_free(g_main_device, src0_as_f16, src0_as);
  13587. }
  13588. if (ptrs_src_s != 0) {
  13589. ggml_sycl_pool_free(g_main_device, ptrs_src, ptrs_src_s);
  13590. }
  13591. if (ptrs_dst_s != 0) {
  13592. ggml_sycl_pool_free(g_main_device, ptrs_dst, ptrs_dst_s);
  13593. }
  13594. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13595. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  13596. ggml_sycl_pool_free(g_main_device, src1_as_f16, src1_as);
  13597. ggml_sycl_pool_free(g_main_device, dst_f16, dst_as);
  13598. }
  13599. #endif
  13600. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  13601. const ggml_tensor *src1,
  13602. ggml_tensor *dst) try {
  13603. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT &&
  13604. "mul_mat_id does not support split buffers");
  13605. const ggml_tensor *ids = dst->src[2];
  13606. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13607. const size_t nb11 = src1->nb[1];
  13608. const size_t nb1 = dst->nb[1];
  13609. const int32_t id = ((int32_t *)dst->op_params)[0];
  13610. const int32_t n_as = src0->ne[2];
  13611. std::vector<char> ids_host(ggml_nbytes(ids));
  13612. const char *ids_dev = (const char *)ids->data;
  13613. SYCL_CHECK(CHECK_TRY_ERROR(
  13614. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
  13615. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13616. const ggml_tensor_extra_gpu *src0_extra =
  13617. (const ggml_tensor_extra_gpu *)src0->extra;
  13618. const ggml_tensor_extra_gpu *src1_extra =
  13619. (const ggml_tensor_extra_gpu *)src1->extra;
  13620. const ggml_tensor_extra_gpu *dst_extra =
  13621. (const ggml_tensor_extra_gpu *)dst->extra;
  13622. ggml_tensor_extra_gpu src0_row_extra;
  13623. ggml_tensor_extra_gpu src1_row_extra;
  13624. ggml_tensor_extra_gpu dst_row_extra;
  13625. ggml_tensor src0_row = *src0;
  13626. ggml_tensor src1_row = *src1;
  13627. ggml_tensor dst_row = *dst;
  13628. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  13629. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  13630. src0_row.extra = &src0_row_extra;
  13631. src1_row.extra = &src1_row_extra;
  13632. dst_row.extra = &dst_row_extra;
  13633. char *src0_original = src1->backend == GGML_BACKEND_TYPE_CPU
  13634. ? (char *)src0->data
  13635. : (char *)src0_extra->data_device[g_main_device];
  13636. char *src1_original = src1->backend == GGML_BACKEND_TYPE_CPU
  13637. ? (char *)src1->data
  13638. : (char *)src1_extra->data_device[g_main_device];
  13639. char *dst_original = dst->backend == GGML_BACKEND_TYPE_CPU
  13640. ? (char *)dst->data
  13641. : (char *)dst_extra->data_device[g_main_device];
  13642. src0_row.ne[2] = 1;
  13643. src0_row.ne[3] = 1;
  13644. src0_row.nb[3] = src0->nb[2];
  13645. if (src1->ne[1] == 1) {
  13646. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13647. const int32_t row_id =
  13648. *(const int32_t *)(ids_host.data() + i01 * ids->nb[1] +
  13649. id * ids->nb[0]);
  13650. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13651. src0_row_extra.data_device[g_main_device] =
  13652. src0_original + row_id * src0->nb[2];
  13653. src1_row_extra.data_device[g_main_device] =
  13654. src1_original + i01 * src1->nb[1];
  13655. dst_row_extra.data_device[g_main_device] =
  13656. dst_original + i01 * dst->nb[1];
  13657. ggml_sycl_mul_mat(&src0_row, &src1_row, &dst_row);
  13658. }
  13659. } else {
  13660. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  13661. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  13662. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  13663. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  13664. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  13665. int64_t num_src1_rows = 0;
  13666. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13667. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13668. if (row_id_i != row_id) {
  13669. continue;
  13670. }
  13671. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13672. SYCL_CHECK(CHECK_TRY_ERROR(
  13673. stream->memcpy(src1_contiguous.get() + num_src1_rows * nb11,
  13674. src1_original + i01 * nb11, nb11)));
  13675. num_src1_rows++;
  13676. }
  13677. if (num_src1_rows == 0) {
  13678. continue;
  13679. }
  13680. src0_row_extra.data_device[g_main_device] =
  13681. src0_original + row_id * src0->nb[2];
  13682. src1_row.ne[1] = num_src1_rows;
  13683. dst_row.ne[1] = num_src1_rows;
  13684. src1_row.nb[1] = nb11;
  13685. src1_row.nb[2] = num_src1_rows*nb11;
  13686. src1_row.nb[3] = num_src1_rows*nb11;
  13687. dst_row.nb[1] = nb1;
  13688. dst_row.nb[2] = num_src1_rows*nb1;
  13689. dst_row.nb[3] = num_src1_rows*nb1;
  13690. ggml_sycl_mul_mat(&src0_row, &src1_row, &dst_row);
  13691. num_src1_rows = 0;
  13692. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13693. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13694. if (row_id_i != row_id) {
  13695. continue;
  13696. }
  13697. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13698. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  13699. dst_original + i01 * nb1,
  13700. dst_contiguous.get() + num_src1_rows * nb1, nb1)));
  13701. num_src1_rows++;
  13702. }
  13703. }
  13704. }
  13705. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  13706. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13707. }
  13708. }
  13709. catch (sycl::exception const &exc) {
  13710. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13711. << ", line:" << __LINE__ << std::endl;
  13712. std::exit(1);
  13713. }
  13714. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13715. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  13716. }
  13717. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13718. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  13719. }
  13720. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  13721. ggml_tensor *dst) try {
  13722. const int64_t ne = ggml_nelements(src0);
  13723. GGML_ASSERT(ne == ggml_nelements(src1));
  13724. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  13725. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13726. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  13727. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  13728. GGML_TENSOR_BINARY_OP_LOCALS;
  13729. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13730. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13731. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13732. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13733. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13734. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  13735. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  13736. 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);
  13737. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  13738. 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);
  13739. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  13740. 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);
  13741. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  13742. 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);
  13743. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  13744. 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);
  13745. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  13746. 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);
  13747. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  13748. 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);
  13749. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  13750. 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);
  13751. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  13752. 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);
  13753. } else {
  13754. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  13755. ggml_type_name(src0->type), ggml_type_name(src1->type));
  13756. GGML_ASSERT(false);
  13757. }
  13758. (void) dst;
  13759. }
  13760. catch (sycl::exception const &exc) {
  13761. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13762. << ", line:" << __LINE__ << std::endl;
  13763. std::exit(1);
  13764. }
  13765. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13766. // TODO: why do we pass dst as src1 here?
  13767. ggml_sycl_cpy(src0, dst, nullptr);
  13768. (void) src1;
  13769. }
  13770. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13771. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  13772. }
  13773. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13774. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  13775. }
  13776. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13777. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  13778. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  13779. }
  13780. static void ggml_sycl_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13781. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pool2d);
  13782. }
  13783. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13784. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  13785. }
  13786. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13787. GGML_ASSERT(ggml_is_contiguous(src0));
  13788. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  13789. }
  13790. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13791. GGML_ASSERT(ggml_is_contiguous(src0));
  13792. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  13793. }
  13794. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13795. (void) src0;
  13796. (void) src1;
  13797. (void) dst;
  13798. }
  13799. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  13800. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  13801. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  13802. }
  13803. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  13804. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_TYPE_GPU && tensor->backend != GGML_BACKEND_TYPE_GPU_SPLIT) ) {
  13805. return;
  13806. }
  13807. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13808. for (int i = 0; i < g_device_count; ++i) {
  13809. const dpct::queue_ptr stream = g_syclStreams[i][0];
  13810. if (extra->data_device[i] != nullptr) {
  13811. SYCL_CHECK(ggml_sycl_set_device(i));
  13812. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[i], *stream)));
  13813. }
  13814. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  13815. if (extra->events[i][is] != nullptr) {
  13816. SYCL_CHECK(ggml_sycl_set_device(i));
  13817. SYCL_CHECK(CHECK_TRY_ERROR(
  13818. dpct::destroy_event(extra->events[i][is])));
  13819. }
  13820. }
  13821. }
  13822. delete extra;
  13823. }
  13824. catch (sycl::exception const &exc) {
  13825. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13826. << ", line:" << __LINE__ << std::endl;
  13827. std::exit(1);
  13828. }
  13829. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  13830. static size_t g_temp_tensor_extra_index = 0;
  13831. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13832. if (g_temp_tensor_extras == nullptr) {
  13833. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13834. }
  13835. size_t alloc_index = g_temp_tensor_extra_index;
  13836. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13837. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  13838. memset(extra, 0, sizeof(*extra));
  13839. return extra;
  13840. }
  13841. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  13842. bool scratch, bool force_inplace,
  13843. bool no_alloc) try {
  13844. if (scratch && g_scratch_size == 0) {
  13845. return;
  13846. }
  13847. tensor->backend = GGML_BACKEND_TYPE_GPU;
  13848. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_CPU) {
  13849. const ggml_op src0_op = tensor->src[0]->op;
  13850. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  13851. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  13852. }
  13853. }
  13854. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_TYPE_CPU) {
  13855. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  13856. }
  13857. if (scratch && no_alloc) {
  13858. return;
  13859. }
  13860. ggml_tensor_extra_gpu * extra;
  13861. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  13862. tensor->op == GGML_OP_VIEW ||
  13863. force_inplace;
  13864. const size_t size = ggml_nbytes(tensor);
  13865. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13866. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13867. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT)) {
  13868. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  13869. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13870. size_t offset = 0;
  13871. if (tensor->op == GGML_OP_VIEW) {
  13872. memcpy(&offset, tensor->op_params, sizeof(size_t));
  13873. }
  13874. extra = ggml_sycl_alloc_temp_tensor_extra();
  13875. extra->data_device[g_main_device] = src0_ddc + offset;
  13876. } else if (tensor->op == GGML_OP_CPY) {
  13877. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  13878. void * src1_ddv = src1_extra->data_device[g_main_device];
  13879. extra = ggml_sycl_alloc_temp_tensor_extra();
  13880. extra->data_device[g_main_device] = src1_ddv;
  13881. } else if (scratch) {
  13882. GGML_ASSERT(size <= g_scratch_size);
  13883. if (g_scratch_offset + size > g_scratch_size) {
  13884. g_scratch_offset = 0;
  13885. }
  13886. char * data = (char *) g_scratch_buffer;
  13887. if (data == nullptr) {
  13888. SYCL_CHECK(CHECK_TRY_ERROR(
  13889. data = (char *)sycl::malloc_device(
  13890. g_scratch_size, *stream)));
  13891. g_scratch_buffer = data;
  13892. }
  13893. extra = ggml_sycl_alloc_temp_tensor_extra();
  13894. extra->data_device[g_main_device] = data + g_scratch_offset;
  13895. g_scratch_offset += size;
  13896. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  13897. } else { // allocate new buffers outside of scratch
  13898. void * data;
  13899. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  13900. size, *stream)));
  13901. SYCL_CHECK(CHECK_TRY_ERROR(
  13902. (*stream).memset(data, 0, size).wait()));
  13903. extra = new ggml_tensor_extra_gpu;
  13904. memset(extra, 0, sizeof(*extra));
  13905. extra->data_device[g_main_device] = data;
  13906. }
  13907. tensor->extra = extra;
  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. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  13915. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13916. GGML_ASSERT(ggml_is_contiguous(tensor));
  13917. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13918. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13919. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13920. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  13921. .memcpy(extra->data_device[g_main_device],
  13922. tensor->data, ggml_nbytes(tensor))
  13923. .wait()));
  13924. }
  13925. catch (sycl::exception const &exc) {
  13926. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13927. << ", line:" << __LINE__ << std::endl;
  13928. std::exit(1);
  13929. }
  13930. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  13931. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  13932. }
  13933. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  13934. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  13935. }
  13936. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  13937. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  13938. }
  13939. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  13940. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  13941. }
  13942. void ggml_sycl_set_main_device(const int main_device) try {
  13943. if (g_main_device == main_device) return;
  13944. check_allow_gpu_index(main_device);
  13945. g_main_device = main_device;
  13946. g_main_device_id = g_sycl_gpu_mgr->gpus[main_device];
  13947. if (g_ggml_sycl_debug) {
  13948. dpct::device_info prop;
  13949. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  13950. prop, dpct::dev_mgr::instance().get_device(g_main_device_id))));
  13951. fprintf(stderr, "Using device %d (%s) as main device\n",
  13952. g_main_device_id, prop.get_name());
  13953. }
  13954. }
  13955. catch (sycl::exception const &exc) {
  13956. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13957. << ", line:" << __LINE__ << std::endl;
  13958. std::exit(1);
  13959. }
  13960. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  13961. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  13962. // it still won't always work as expected, but it's better than nothing
  13963. if (scratch_size > g_scratch_size) {
  13964. ggml_sycl_free_scratch();
  13965. }
  13966. g_scratch_size = std::max(g_scratch_size, scratch_size);
  13967. }
  13968. void ggml_sycl_free_scratch() try {
  13969. if (g_scratch_buffer == nullptr) {
  13970. return;
  13971. }
  13972. ggml_sycl_set_device(g_main_device);
  13973. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13974. SYCL_CHECK(CHECK_TRY_ERROR(
  13975. sycl::free(g_scratch_buffer, *stream)));
  13976. g_scratch_buffer = nullptr;
  13977. }
  13978. catch (sycl::exception const &exc) {
  13979. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13980. << ", line:" << __LINE__ << std::endl;
  13981. std::exit(1);
  13982. }
  13983. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  13984. if (!g_sycl_loaded) return false;
  13985. ggml_sycl_func_t func;
  13986. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  13987. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  13988. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  13989. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  13990. return false;
  13991. }
  13992. if (tensor->op == GGML_OP_MUL_MAT) {
  13993. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  13994. #ifndef NDEBUG
  13995. 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]);
  13996. #endif
  13997. return false;
  13998. }
  13999. }
  14000. switch (tensor->op) {
  14001. case GGML_OP_REPEAT:
  14002. func = ggml_sycl_repeat;
  14003. break;
  14004. case GGML_OP_GET_ROWS:
  14005. func = ggml_sycl_get_rows;
  14006. break;
  14007. case GGML_OP_DUP:
  14008. func = ggml_sycl_dup;
  14009. break;
  14010. case GGML_OP_ADD:
  14011. func = ggml_sycl_add;
  14012. break;
  14013. case GGML_OP_ACC:
  14014. func = ggml_sycl_acc;
  14015. break;
  14016. case GGML_OP_MUL:
  14017. func = ggml_sycl_mul;
  14018. break;
  14019. case GGML_OP_DIV:
  14020. func = ggml_sycl_div;
  14021. break;
  14022. case GGML_OP_UNARY:
  14023. switch (ggml_get_unary_op(tensor)) {
  14024. case GGML_UNARY_OP_GELU:
  14025. func = ggml_sycl_gelu;
  14026. break;
  14027. case GGML_UNARY_OP_SILU:
  14028. func = ggml_sycl_silu;
  14029. break;
  14030. case GGML_UNARY_OP_GELU_QUICK:
  14031. func = ggml_sycl_gelu_quick;
  14032. break;
  14033. case GGML_UNARY_OP_TANH:
  14034. func = ggml_sycl_tanh;
  14035. break;
  14036. case GGML_UNARY_OP_RELU:
  14037. func = ggml_sycl_relu;
  14038. break;
  14039. case GGML_UNARY_OP_HARDSIGMOID:
  14040. func = ggml_sycl_hardsigmoid;
  14041. break;
  14042. case GGML_UNARY_OP_HARDSWISH:
  14043. func = ggml_sycl_hardswish;
  14044. break;
  14045. default:
  14046. return false;
  14047. }
  14048. break;
  14049. case GGML_OP_NORM:
  14050. func = ggml_sycl_norm;
  14051. break;
  14052. case GGML_OP_GROUP_NORM:
  14053. func = ggml_sycl_group_norm;
  14054. break;
  14055. case GGML_OP_CONCAT:
  14056. func = ggml_sycl_concat;
  14057. break;
  14058. case GGML_OP_UPSCALE:
  14059. func = ggml_sycl_upscale;
  14060. break;
  14061. case GGML_OP_PAD:
  14062. func = ggml_sycl_pad;
  14063. break;
  14064. case GGML_OP_LEAKY_RELU:
  14065. func = ggml_sycl_leaky_relu;
  14066. break;
  14067. case GGML_OP_RMS_NORM:
  14068. func = ggml_sycl_rms_norm;
  14069. break;
  14070. case GGML_OP_MUL_MAT:
  14071. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  14072. return false;
  14073. }
  14074. func = ggml_sycl_mul_mat;
  14075. break;
  14076. case GGML_OP_MUL_MAT_ID:
  14077. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  14078. return false;
  14079. }
  14080. func = ggml_sycl_mul_mat_id;
  14081. break;
  14082. case GGML_OP_SCALE:
  14083. func = ggml_sycl_scale;
  14084. break;
  14085. case GGML_OP_SQR:
  14086. func = ggml_sycl_sqr;
  14087. break;
  14088. case GGML_OP_CLAMP:
  14089. func = ggml_sycl_clamp;
  14090. break;
  14091. case GGML_OP_CPY:
  14092. func = ggml_sycl_cpy;
  14093. break;
  14094. case GGML_OP_CONT:
  14095. func = ggml_sycl_dup;
  14096. break;
  14097. case GGML_OP_NONE:
  14098. case GGML_OP_RESHAPE:
  14099. case GGML_OP_VIEW:
  14100. case GGML_OP_PERMUTE:
  14101. case GGML_OP_TRANSPOSE:
  14102. func = ggml_sycl_nop;
  14103. break;
  14104. case GGML_OP_DIAG_MASK_INF:
  14105. func = ggml_sycl_diag_mask_inf;
  14106. break;
  14107. case GGML_OP_SOFT_MAX:
  14108. func = ggml_sycl_soft_max;
  14109. break;
  14110. case GGML_OP_ROPE:
  14111. func = ggml_sycl_rope;
  14112. break;
  14113. case GGML_OP_IM2COL:
  14114. func = ggml_sycl_im2col;
  14115. break;
  14116. case GGML_OP_POOL_2D:
  14117. func = ggml_sycl_pool2d;
  14118. break;
  14119. case GGML_OP_SUM_ROWS:
  14120. func = ggml_sycl_sum_rows;
  14121. break;
  14122. case GGML_OP_ARGSORT:
  14123. func = ggml_sycl_argsort;
  14124. break;
  14125. default:
  14126. return false;
  14127. }
  14128. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  14129. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  14130. }
  14131. if (params->ith != 0) {
  14132. return true;
  14133. }
  14134. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  14135. return true;
  14136. }
  14137. func(tensor->src[0], tensor->src[1], tensor);
  14138. return true;
  14139. }
  14140. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  14141. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_gpu_list\n");
  14142. for(int i=0;i<max_len;i++) id_list[i] = -1;
  14143. if (!g_sycl_gpu_mgr) {
  14144. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14145. }
  14146. for (int i=0;i< g_sycl_gpu_mgr->gpus.size();i++){
  14147. if (i>=max_len) break;
  14148. id_list[i] = g_sycl_gpu_mgr->gpus[i];
  14149. }
  14150. return;
  14151. }
  14152. catch (sycl::exception const &exc) {
  14153. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14154. << ", line:" << __LINE__ << std::endl;
  14155. std::exit(1);
  14156. }
  14157. int ggml_sycl_get_device_count() try {
  14158. int device_count;
  14159. if (CHECK_TRY_ERROR(device_count =
  14160. dpct::dev_mgr::instance().device_count()) != 0) {
  14161. return 0;
  14162. }
  14163. return device_count;
  14164. }
  14165. catch (sycl::exception const &exc) {
  14166. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14167. << ", line:" << __LINE__ << std::endl;
  14168. std::exit(1);
  14169. }
  14170. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  14171. size_t description_size) try {
  14172. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_device_description\n");
  14173. dpct::device_info prop;
  14174. int device_id = g_sycl_gpu_mgr->gpus[device];
  14175. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  14176. prop, dpct::dev_mgr::instance().get_device(device_id))));
  14177. snprintf(description, description_size, "%s", prop.get_name());
  14178. }
  14179. catch (sycl::exception const &exc) {
  14180. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14181. << ", line:" << __LINE__ << std::endl;
  14182. std::exit(1);
  14183. }
  14184. GGML_CALL void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  14185. size_t *total) try {
  14186. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_memory\n");
  14187. ggml_sycl_set_device(device);
  14188. /*
  14189. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  14190. error codes. The original code was commented out and a warning string was
  14191. inserted. You need to rewrite this code.
  14192. */
  14193. /*
  14194. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  14195. device information which may not be supported by all compilers or runtimes.
  14196. You may need to adjust the code.
  14197. */
  14198. int device_id = g_sycl_gpu_mgr->gpus[device];
  14199. SYCL_CHECK(CHECK_TRY_ERROR(
  14200. dpct::dev_mgr::instance().get_device(device_id).get_memory_info(*free, *total)));
  14201. }
  14202. catch (sycl::exception const &exc) {
  14203. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14204. << ", line:" << __LINE__ << std::endl;
  14205. std::exit(1);
  14206. }
  14207. ////////////////////////////////////////////////////////////////////////////////
  14208. // backend interface
  14209. #define UNUSED GGML_UNUSED
  14210. // sycl buffer
  14211. struct ggml_backend_sycl_buffer_context {
  14212. int device;
  14213. void * dev_ptr = nullptr;
  14214. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  14215. size_t temp_tensor_extra_index = 0;
  14216. std::string name;
  14217. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) :
  14218. device(device), dev_ptr(dev_ptr) {
  14219. check_allow_gpu_index(device);
  14220. int id = g_sycl_gpu_mgr->gpus[device];
  14221. name = (GGML_SYCL_NAME + std::to_string(id));
  14222. }
  14223. ~ ggml_backend_sycl_buffer_context() {
  14224. delete[] temp_tensor_extras;
  14225. }
  14226. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  14227. if (temp_tensor_extras == nullptr) {
  14228. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  14229. }
  14230. size_t alloc_index = temp_tensor_extra_index;
  14231. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  14232. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  14233. memset(extra, 0, sizeof(*extra));
  14234. return extra;
  14235. }
  14236. };
  14237. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  14238. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14239. return ctx->name.c_str();
  14240. }
  14241. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  14242. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  14243. }
  14244. static void
  14245. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  14246. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14247. ggml_sycl_set_device(ctx->device);
  14248. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14249. SYCL_CHECK(
  14250. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  14251. delete ctx;
  14252. }
  14253. catch (sycl::exception const &exc) {
  14254. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14255. << ", line:" << __LINE__ << std::endl;
  14256. std::exit(1);
  14257. }
  14258. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  14259. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14260. return ctx->dev_ptr;
  14261. }
  14262. GGML_CALL static void
  14263. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14264. ggml_tensor *tensor) try {
  14265. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14266. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  14267. assert(tensor->view_src->buffer->buft == buffer->buft);
  14268. tensor->backend = tensor->view_src->backend;
  14269. tensor->extra = tensor->view_src->extra;
  14270. return;
  14271. }
  14272. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  14273. extra->data_device[ctx->device] = tensor->data;
  14274. tensor->backend = GGML_BACKEND_TYPE_GPU;
  14275. tensor->extra = extra;
  14276. if (ggml_is_quantized(tensor->type)) {
  14277. // initialize padding to 0 to avoid possible NaN values
  14278. size_t original_size = ggml_nbytes(tensor);
  14279. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  14280. if (padded_size > original_size && tensor->view_src == nullptr) {
  14281. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  14282. (char *)tensor->data + original_size, 0,
  14283. padded_size - original_size).wait()));
  14284. }
  14285. }
  14286. }
  14287. catch (sycl::exception const &exc) {
  14288. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14289. << ", line:" << __LINE__ << std::endl;
  14290. std::exit(1);
  14291. }
  14292. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14293. ggml_tensor *tensor,
  14294. const void *data, size_t offset,
  14295. size_t size) try {
  14296. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14297. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14298. ggml_sycl_set_device(ctx->device);
  14299. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14300. SYCL_CHECK(
  14301. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  14302. char* host_buf = (char*)malloc(size);
  14303. memcpy(host_buf, data, size);
  14304. SYCL_CHECK(
  14305. CHECK_TRY_ERROR((*stream)
  14306. .memcpy((char *)tensor->data + offset, host_buf, size)
  14307. .wait()));
  14308. free(host_buf);
  14309. }
  14310. catch (sycl::exception const &exc) {
  14311. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14312. << ", line:" << __LINE__ << std::endl;
  14313. std::exit(1);
  14314. }
  14315. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14316. const ggml_tensor *tensor,
  14317. void *data, size_t offset,
  14318. size_t size) try {
  14319. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14320. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14321. ggml_sycl_set_device(ctx->device);
  14322. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14323. SYCL_CHECK(
  14324. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  14325. SYCL_CHECK(CHECK_TRY_ERROR(
  14326. (*stream)
  14327. .memcpy(data, (const char *)tensor->data + offset, size)
  14328. .wait()));
  14329. }
  14330. catch (sycl::exception const &exc) {
  14331. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14332. << ", line:" << __LINE__ << std::endl;
  14333. std::exit(1);
  14334. }
  14335. GGML_CALL static bool
  14336. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  14337. const ggml_tensor *src,
  14338. ggml_tensor *dst) try {
  14339. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  14340. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  14341. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14342. ggml_sycl_set_device(src_ctx->device);
  14343. /*
  14344. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  14345. error codes. The original code was commented out and a warning string
  14346. was inserted. You need to rewrite this code.
  14347. */
  14348. SYCL_CHECK(CHECK_TRY_ERROR(
  14349. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  14350. ggml_sycl_set_device(dst_ctx->device);
  14351. /*
  14352. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  14353. error codes. The original code was commented out and a warning string
  14354. was inserted. You need to rewrite this code.
  14355. */
  14356. SYCL_CHECK(CHECK_TRY_ERROR(
  14357. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  14358. /*
  14359. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  14360. error codes. The original code was commented out and a warning string
  14361. was inserted. You need to rewrite this code.
  14362. */
  14363. dpct::queue_ptr stream_dst = g_syclStreams[dst_ctx->device][0];
  14364. dpct::queue_ptr stream_src = g_syclStreams[src_ctx->device][0];
  14365. size_t size = ggml_nbytes(src);
  14366. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  14367. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  14368. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  14369. #if 0
  14370. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  14371. (char *)dst->data, (const char *)src->data, size).wait()));
  14372. /*
  14373. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  14374. error codes. The original code was commented out and a warning string
  14375. was inserted. You need to rewrite this code.
  14376. */
  14377. SYCL_CHECK(CHECK_TRY_ERROR(
  14378. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  14379. #endif
  14380. return true;
  14381. }
  14382. return false;
  14383. }
  14384. catch (sycl::exception const &exc) {
  14385. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14386. << ", line:" << __LINE__ << std::endl;
  14387. std::exit(1);
  14388. }
  14389. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  14390. uint8_t value) try {
  14391. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14392. ggml_sycl_set_device(ctx->device);
  14393. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14394. SYCL_CHECK(
  14395. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  14396. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  14397. .memset(ctx->dev_ptr, value, buffer->size)
  14398. .wait()));
  14399. }
  14400. catch (sycl::exception const &exc) {
  14401. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14402. << ", line:" << __LINE__ << std::endl;
  14403. std::exit(1);
  14404. }
  14405. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  14406. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  14407. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  14408. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  14409. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  14410. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  14411. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  14412. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  14413. /* .clear = */ ggml_backend_sycl_buffer_clear,
  14414. /* .reset = */ NULL,
  14415. };
  14416. // sycl buffer type
  14417. struct ggml_backend_sycl_buffer_type_context {
  14418. int device;
  14419. std::string name;
  14420. };
  14421. struct ggml_backend_sycl_context {
  14422. int device;
  14423. std::string name;
  14424. };
  14425. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14426. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14427. return ctx->name.c_str();
  14428. }
  14429. GGML_CALL static ggml_backend_buffer_t
  14430. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  14431. size_t size) try {
  14432. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14433. ggml_sycl_set_device(buft_ctx->device);
  14434. const dpct::queue_ptr stream = g_syclStreams[buft_ctx->device][0];
  14435. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  14436. void * dev_ptr;
  14437. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  14438. size, *stream)));
  14439. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr);
  14440. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  14441. }
  14442. catch (sycl::exception const &exc) {
  14443. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14444. << ", line:" << __LINE__ << std::endl;
  14445. std::exit(1);
  14446. }
  14447. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14448. return 128;
  14449. UNUSED(buft);
  14450. }
  14451. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  14452. return dpct::get_current_device().get_max_mem_alloc_size();
  14453. UNUSED(buft);
  14454. }
  14455. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14456. size_t size = ggml_nbytes(tensor);
  14457. int64_t ne0 = tensor->ne[0];
  14458. if (ggml_is_quantized(tensor->type)) {
  14459. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14460. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14461. }
  14462. }
  14463. return size;
  14464. UNUSED(buft);
  14465. }
  14466. GGML_CALL static bool ggml_backend_sycl_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14467. if (!ggml_backend_is_sycl(backend)) {
  14468. return false;
  14469. }
  14470. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14471. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14472. return buft_ctx->device == sycl_ctx->device;
  14473. }
  14474. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  14475. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  14476. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  14477. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  14478. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  14479. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  14480. /* .supports_backend = */ ggml_backend_sycl_buffer_type_supports_backend,
  14481. /* .is_host = */ nullptr,
  14482. };
  14483. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device_index) {
  14484. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  14485. if (device_index>=g_device_count or device_index<0) {
  14486. 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",
  14487. device_index, g_device_count-1);
  14488. GGML_ASSERT(device_index<g_device_count);
  14489. }
  14490. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  14491. if (!g_ggml_backend_sycl_buffer_type_initialized) {
  14492. for (int i = 0; i < g_device_count; i++) {
  14493. ggml_backend_sycl_buffer_types[i] = {
  14494. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  14495. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(g_sycl_gpu_mgr->gpus[i])},
  14496. };
  14497. }
  14498. g_ggml_backend_sycl_buffer_type_initialized = true;
  14499. }
  14500. return &ggml_backend_sycl_buffer_types[device_index];
  14501. }
  14502. // sycl split buffer type
  14503. 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) {
  14504. const int64_t nrows = ggml_nrows(tensor);
  14505. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  14506. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  14507. *row_low -= *row_low % rounding;
  14508. if (id == g_device_count - 1) {
  14509. *row_high = nrows;
  14510. } else {
  14511. *row_high = nrows*tensor_split[id + 1];
  14512. *row_high -= *row_high % rounding;
  14513. }
  14514. }
  14515. struct ggml_backend_sycl_split_buffer_context {
  14516. ~ggml_backend_sycl_split_buffer_context() try {
  14517. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  14518. for (int i = 0; i < g_device_count; ++i) {
  14519. // int id = g_sycl_gpu_mgr->gpus[i];
  14520. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14521. if (extra->events[i][is] != nullptr) {
  14522. /*
  14523. DPCT1009:206: SYCL uses exceptions to report errors and
  14524. does not use the error codes. The original code was
  14525. commented out and a warning string was inserted. You
  14526. need to rewrite this code.
  14527. */
  14528. SYCL_CHECK(CHECK_TRY_ERROR(
  14529. dpct::destroy_event(extra->events[i][is])));
  14530. }
  14531. }
  14532. if (extra->data_device[i] != nullptr) {
  14533. /*
  14534. DPCT1009:207: SYCL uses exceptions to report errors and does
  14535. not use the error codes. The original code was commented out
  14536. and a warning string was inserted. You need to rewrite this
  14537. code.
  14538. */
  14539. ggml_sycl_set_device(i);
  14540. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  14541. extra->data_device[i], *g_syclStreams[i][0])));
  14542. }
  14543. }
  14544. delete extra;
  14545. }
  14546. }
  14547. catch (sycl::exception const &exc) {
  14548. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14549. << ", line:" << __LINE__ << std::endl;
  14550. std::exit(1);
  14551. }
  14552. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  14553. };
  14554. GGML_CALL static const char * ggml_backend_sycl_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  14555. return GGML_SYCL_NAME "_Split";
  14556. UNUSED(buffer);
  14557. }
  14558. // unused at the moment
  14559. //static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  14560. // return buffer->iface.get_name == ggml_backend_sycl_split_buffer_get_name;
  14561. //}
  14562. GGML_CALL static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14563. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14564. delete ctx;
  14565. }
  14566. GGML_CALL static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  14567. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  14568. return (void *)0x1000;
  14569. UNUSED(buffer);
  14570. }
  14571. GGML_CALL static void
  14572. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14573. ggml_tensor *tensor) try {
  14574. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  14575. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14576. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14577. const int64_t ne0 = tensor->ne[0];
  14578. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  14579. ctx->tensor_extras.push_back(extra);
  14580. for (int i = 0; i < g_device_count; ++i) {
  14581. // int id = g_sycl_gpu_mgr->gpus[i];
  14582. int64_t row_low, row_high;
  14583. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14584. int64_t nrows_split = row_high - row_low;
  14585. if (nrows_split == 0) {
  14586. continue;
  14587. }
  14588. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14589. const size_t original_size = size;
  14590. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14591. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14592. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14593. }
  14594. // FIXME: do not crash if cudaMalloc fails
  14595. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  14596. ggml_sycl_set_device(i);
  14597. char * buf;
  14598. /*
  14599. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  14600. error codes. The original code was commented out and a warning string
  14601. was inserted. You need to rewrite this code.
  14602. */
  14603. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  14604. size, *g_syclStreams[i][0])));
  14605. // set padding to 0 to avoid possible NaN values
  14606. if (size > original_size) {
  14607. /*
  14608. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  14609. the error codes. The original code was commented out and a warning
  14610. string was inserted. You need to rewrite this code.
  14611. */
  14612. SYCL_CHECK(CHECK_TRY_ERROR(
  14613. (*g_syclStreams[i][0])
  14614. .memset(buf + original_size, 0, size - original_size)
  14615. .wait()));
  14616. }
  14617. extra->data_device[i] = buf;
  14618. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14619. /*
  14620. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  14621. the error codes. The original code was commented out and a warning
  14622. string was inserted. You need to rewrite this code.
  14623. */
  14624. SYCL_CHECK(
  14625. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  14626. }
  14627. }
  14628. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  14629. tensor->extra = extra;
  14630. }
  14631. catch (sycl::exception const &exc) {
  14632. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14633. << ", line:" << __LINE__ << std::endl;
  14634. std::exit(1);
  14635. }
  14636. GGML_CALL static void
  14637. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14638. ggml_tensor *tensor, const void *data,
  14639. size_t offset, size_t size) try {
  14640. // split tensors must always be set in their entirety at once
  14641. GGML_ASSERT(offset == 0);
  14642. GGML_ASSERT(size == ggml_nbytes(tensor));
  14643. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14644. const int64_t ne0 = tensor->ne[0];
  14645. const size_t nb1 = tensor->nb[1];
  14646. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14647. for (int i = 0; i < g_device_count; ++i) {
  14648. // int id = g_sycl_gpu_mgr->gpus[i];
  14649. int64_t row_low, row_high;
  14650. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14651. int64_t nrows_split = row_high - row_low;
  14652. if (nrows_split == 0) {
  14653. continue;
  14654. }
  14655. const size_t offset_split = row_low*nb1;
  14656. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14657. const size_t original_size = size;
  14658. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14659. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14660. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14661. }
  14662. const char * buf_host = (const char *)data + offset_split;
  14663. /*
  14664. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  14665. error codes. The original code was commented out and a warning string
  14666. was inserted. You need to rewrite this code.
  14667. */
  14668. ggml_sycl_set_device(i);
  14669. SYCL_CHECK(CHECK_TRY_ERROR(
  14670. (*g_syclStreams[i][0])
  14671. .memcpy(extra->data_device[i], buf_host, original_size)
  14672. .wait()));
  14673. }
  14674. }
  14675. catch (sycl::exception const &exc) {
  14676. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14677. << ", line:" << __LINE__ << std::endl;
  14678. std::exit(1);
  14679. }
  14680. GGML_CALL static void
  14681. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14682. const ggml_tensor *tensor, void *data,
  14683. size_t offset, size_t size) try {
  14684. // split tensors must always be set in their entirety at once
  14685. GGML_ASSERT(offset == 0);
  14686. GGML_ASSERT(size == ggml_nbytes(tensor));
  14687. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14688. const int64_t ne0 = tensor->ne[0];
  14689. const size_t nb1 = tensor->nb[1];
  14690. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14691. for (int i = 0; i < g_device_count; ++i) {
  14692. // int id = g_sycl_gpu_mgr->gpus[i];
  14693. int64_t row_low, row_high;
  14694. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14695. int64_t nrows_split = row_high - row_low;
  14696. if (nrows_split == 0) {
  14697. continue;
  14698. }
  14699. const size_t offset_split = row_low*nb1;
  14700. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14701. const size_t original_size = size;
  14702. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14703. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14704. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14705. }
  14706. char * buf_host = (char *)data + offset_split;
  14707. /*
  14708. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  14709. error codes. The original code was commented out and a warning string
  14710. was inserted. You need to rewrite this code.
  14711. */
  14712. ggml_sycl_set_device(i);
  14713. SYCL_CHECK(CHECK_TRY_ERROR(
  14714. (*g_syclStreams[i][0])
  14715. .memcpy(buf_host, extra->data_device[i], original_size)
  14716. .wait()));
  14717. }
  14718. }
  14719. catch (sycl::exception const &exc) {
  14720. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14721. << ", line:" << __LINE__ << std::endl;
  14722. std::exit(1);
  14723. }
  14724. GGML_CALL static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  14725. UNUSED(buffer);
  14726. UNUSED(value);
  14727. }
  14728. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  14729. /* .get_name = */ ggml_backend_sycl_split_buffer_get_name,
  14730. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  14731. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  14732. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  14733. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  14734. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  14735. /* .cpy_tensor = */ NULL,
  14736. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  14737. /* .reset = */ NULL,
  14738. };
  14739. GGML_CALL static const char * ggml_backend_sycl_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14740. return GGML_SYCL_NAME "_Split";
  14741. UNUSED(buft);
  14742. }
  14743. GGML_CALL static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14744. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  14745. // instead, we allocate them for each tensor separately in init_tensor
  14746. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  14747. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  14748. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  14749. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  14750. }
  14751. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14752. return 128;
  14753. UNUSED(buft);
  14754. }
  14755. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14756. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  14757. size_t total_size = 0;
  14758. const int64_t ne0 = tensor->ne[0];
  14759. for (int i = 0; i < g_device_count; ++i) {
  14760. // int id = g_sycl_gpu_mgr->gpus[i];
  14761. int64_t row_low, row_high;
  14762. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  14763. int64_t nrows_split = row_high - row_low;
  14764. if (nrows_split == 0) {
  14765. continue;
  14766. }
  14767. total_size += ggml_nbytes_split(tensor, nrows_split);
  14768. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14769. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14770. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14771. }
  14772. }
  14773. return total_size;
  14774. }
  14775. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14776. return ggml_backend_is_sycl(backend);
  14777. UNUSED(buft);
  14778. }
  14779. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  14780. return false;
  14781. UNUSED(buft);
  14782. }
  14783. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  14784. /* .get_name = */ ggml_backend_sycl_split_buffer_type_name,
  14785. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  14786. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  14787. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  14788. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  14789. /* .supports_backend = */ ggml_backend_sycl_split_buffer_type_supports_backend,
  14790. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  14791. };
  14792. GGML_CALL ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  14793. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_split_buffer_type\n");
  14794. ggml_init_sycl();
  14795. // FIXME: this is not thread safe
  14796. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  14797. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  14798. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  14799. if (all_zero) {
  14800. tensor_split_arr = g_default_tensor_split;
  14801. } else {
  14802. float split_sum = 0.0f;
  14803. for (int i = 0; i < g_device_count; ++i) {
  14804. // int id = g_sycl_gpu_mgr->gpus[i];
  14805. tensor_split_arr[i] = split_sum;
  14806. split_sum += tensor_split[i];
  14807. }
  14808. for (int i = 0; i < g_device_count; ++i) {
  14809. // int id = g_sycl_gpu_mgr->gpus[i];
  14810. tensor_split_arr[i] /= split_sum;
  14811. }
  14812. }
  14813. auto it = buft_map.find(tensor_split_arr);
  14814. if (it != buft_map.end()) {
  14815. return &it->second;
  14816. }
  14817. struct ggml_backend_buffer_type buft {
  14818. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  14819. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  14820. };
  14821. auto result = buft_map.emplace(tensor_split_arr, buft);
  14822. return &result.first->second;
  14823. }
  14824. // host buffer type
  14825. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14826. return GGML_SYCL_NAME "_Host";
  14827. UNUSED(buft);
  14828. }
  14829. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  14830. return GGML_SYCL_NAME "_Host";
  14831. UNUSED(buffer);
  14832. }
  14833. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14834. ggml_sycl_host_free(buffer->context);
  14835. }
  14836. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14837. void * ptr = ggml_sycl_host_malloc(size);
  14838. if (ptr == nullptr) {
  14839. // fallback to cpu buffer
  14840. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  14841. }
  14842. // FIXME: this is a hack to avoid having to implement a new buffer type
  14843. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  14844. buffer->buft = buft;
  14845. buffer->iface.get_name = ggml_backend_sycl_host_buffer_name;
  14846. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  14847. return buffer;
  14848. }
  14849. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  14850. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_host_buffer_type\n");
  14851. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  14852. /* .iface = */ {
  14853. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  14854. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  14855. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  14856. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  14857. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  14858. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  14859. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  14860. },
  14861. /* .context = */ nullptr,
  14862. };
  14863. return &ggml_backend_sycl_buffer_type_host;
  14864. }
  14865. // backend
  14866. GGML_CALL static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  14867. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14868. return sycl_ctx->name.c_str();
  14869. }
  14870. GGML_CALL static void ggml_backend_sycl_free(ggml_backend_t backend) {
  14871. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14872. delete sycl_ctx;
  14873. delete backend;
  14874. }
  14875. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  14876. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14877. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  14878. }
  14879. GGML_CALL static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  14880. ggml_tensor *tensor,
  14881. const void *data, size_t offset,
  14882. size_t size) try {
  14883. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14884. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14885. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14886. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14887. (char *)tensor->data + offset, data, size).wait()));
  14888. }
  14889. catch (sycl::exception const &exc) {
  14890. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14891. << ", line:" << __LINE__ << std::endl;
  14892. std::exit(1);
  14893. }
  14894. GGML_CALL static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  14895. const ggml_tensor *tensor,
  14896. void *data, size_t offset,
  14897. size_t size) try {
  14898. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14899. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14900. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14901. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14902. data, (const char *)tensor->data + offset, size).wait()));
  14903. }
  14904. catch (sycl::exception const &exc) {
  14905. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14906. << ", line:" << __LINE__ << std::endl;
  14907. std::exit(1);
  14908. }
  14909. GGML_CALL static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  14910. const ggml_tensor *src,
  14911. ggml_tensor *dst) try {
  14912. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14913. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  14914. /*
  14915. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  14916. error codes. The original code was commented out and a warning string
  14917. was inserted. You need to rewrite this code.
  14918. */
  14919. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14920. dst->data, src->data, ggml_nbytes(dst)).wait()));
  14921. return true;
  14922. }
  14923. return false;
  14924. }
  14925. catch (sycl::exception const &exc) {
  14926. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14927. << ", line:" << __LINE__ << std::endl;
  14928. std::exit(1);
  14929. }
  14930. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  14931. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14932. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  14933. UNUSED(backend);
  14934. }
  14935. catch (sycl::exception const &exc) {
  14936. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14937. << ", line:" << __LINE__ << std::endl;
  14938. std::exit(1);
  14939. }
  14940. GGML_CALL static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  14941. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14942. ggml_sycl_set_main_device(sycl_ctx->device);
  14943. ggml_compute_params params = {};
  14944. params.type = GGML_TASK_TYPE_COMPUTE;
  14945. params.ith = 0;
  14946. for (int i = 0; i < cgraph->n_nodes; i++) {
  14947. ggml_tensor * node = cgraph->nodes[i];
  14948. 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) {
  14949. continue;
  14950. }
  14951. #ifndef NDEBUG
  14952. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14953. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14954. assert(node->extra != nullptr);
  14955. for (int j = 0; j < GGML_MAX_SRC; j++) {
  14956. if (node->src[j] != nullptr) {
  14957. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14958. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14959. assert(node->src[j]->extra != nullptr);
  14960. }
  14961. }
  14962. #endif
  14963. bool ok = ggml_sycl_compute_forward(&params, node);
  14964. if (!ok) {
  14965. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  14966. }
  14967. GGML_ASSERT(ok);
  14968. }
  14969. return GGML_STATUS_SUCCESS;
  14970. }
  14971. GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  14972. switch (op->op) {
  14973. case GGML_OP_UNARY:
  14974. switch (ggml_get_unary_op(op)) {
  14975. case GGML_UNARY_OP_GELU:
  14976. case GGML_UNARY_OP_SILU:
  14977. case GGML_UNARY_OP_RELU:
  14978. case GGML_UNARY_OP_HARDSIGMOID:
  14979. case GGML_UNARY_OP_HARDSWISH:
  14980. case GGML_UNARY_OP_GELU_QUICK:
  14981. case GGML_UNARY_OP_TANH:
  14982. return true;
  14983. default:
  14984. return false;
  14985. }
  14986. break;
  14987. case GGML_OP_MUL_MAT:
  14988. case GGML_OP_MUL_MAT_ID:
  14989. {
  14990. struct ggml_tensor * a;
  14991. struct ggml_tensor * b;
  14992. if (op->op == GGML_OP_MUL_MAT) {
  14993. a = op->src[0];
  14994. b = op->src[1];
  14995. } else {
  14996. a = op->src[2];
  14997. b = op->src[1];
  14998. }
  14999. if (a->ne[3] != b->ne[3]) {
  15000. return false;
  15001. }
  15002. ggml_type a_type = a->type;
  15003. if (a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ4_XS ||
  15004. a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ3_S ||
  15005. a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ2_S ||
  15006. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ1_M
  15007. ) {
  15008. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  15009. return false;
  15010. }
  15011. }
  15012. return true;
  15013. } break;
  15014. case GGML_OP_GET_ROWS:
  15015. {
  15016. switch (op->src[0]->type) {
  15017. case GGML_TYPE_F16:
  15018. case GGML_TYPE_F32:
  15019. case GGML_TYPE_Q4_0:
  15020. case GGML_TYPE_Q4_1:
  15021. case GGML_TYPE_Q5_0:
  15022. case GGML_TYPE_Q5_1:
  15023. case GGML_TYPE_Q8_0:
  15024. return true;
  15025. default:
  15026. return false;
  15027. }
  15028. } break;
  15029. case GGML_OP_CPY:
  15030. {
  15031. ggml_type src0_type = op->src[0]->type;
  15032. ggml_type src1_type = op->src[1]->type;
  15033. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  15034. return true;
  15035. }
  15036. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  15037. return true;
  15038. }
  15039. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  15040. return true;
  15041. }
  15042. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  15043. return true;
  15044. }
  15045. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  15046. return true;
  15047. }
  15048. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  15049. return true;
  15050. }
  15051. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  15052. return true;
  15053. }
  15054. return false;
  15055. } break;
  15056. case GGML_OP_CONCAT:
  15057. {
  15058. ggml_type src0_type = op->src[0]->type;
  15059. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  15060. } break;
  15061. case GGML_OP_DUP:
  15062. case GGML_OP_NONE:
  15063. case GGML_OP_RESHAPE:
  15064. case GGML_OP_REPEAT:
  15065. case GGML_OP_VIEW:
  15066. case GGML_OP_PERMUTE:
  15067. case GGML_OP_TRANSPOSE:
  15068. case GGML_OP_NORM:
  15069. case GGML_OP_ADD:
  15070. case GGML_OP_MUL:
  15071. case GGML_OP_DIV:
  15072. case GGML_OP_RMS_NORM:
  15073. case GGML_OP_SCALE:
  15074. case GGML_OP_SQR:
  15075. case GGML_OP_CLAMP:
  15076. case GGML_OP_CONT:
  15077. case GGML_OP_DIAG_MASK_INF:
  15078. case GGML_OP_SOFT_MAX:
  15079. case GGML_OP_ROPE:
  15080. case GGML_OP_IM2COL:
  15081. case GGML_OP_POOL_2D:
  15082. case GGML_OP_SUM_ROWS:
  15083. case GGML_OP_ARGSORT:
  15084. case GGML_OP_ACC:
  15085. case GGML_OP_GROUP_NORM:
  15086. case GGML_OP_UPSCALE:
  15087. case GGML_OP_PAD:
  15088. case GGML_OP_LEAKY_RELU:
  15089. return true;
  15090. default:
  15091. return false;
  15092. }
  15093. UNUSED(backend);
  15094. }
  15095. GGML_CALL static bool ggml_backend_sycl_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  15096. const int min_batch_size = 32;
  15097. return op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS && op->op != GGML_OP_MUL_MAT_ID;
  15098. GGML_UNUSED(backend);
  15099. }
  15100. static ggml_backend_i ggml_backend_sycl_interface = {
  15101. /* .get_name = */ ggml_backend_sycl_name,
  15102. /* .free = */ ggml_backend_sycl_free,
  15103. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  15104. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  15105. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  15106. /* .cpy_tensor_async = */ NULL, //ggml_backend_sycl_cpy_tensor_async, // TODO: update for the new interface
  15107. /* .synchronize = */ ggml_backend_sycl_synchronize,
  15108. /* .graph_plan_create = */ NULL,
  15109. /* .graph_plan_free = */ NULL,
  15110. /* .graph_plan_compute = */ NULL,
  15111. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  15112. /* .supports_op = */ ggml_backend_sycl_supports_op,
  15113. /* .offload_op = */ ggml_backend_sycl_offload_op,
  15114. /* .event_new = */ NULL,
  15115. /* .event_free = */ NULL,
  15116. /* .event_record = */ NULL,
  15117. /* .event_wait = */ NULL,
  15118. /* .event_synchronize = */ NULL,
  15119. };
  15120. static ggml_guid_t ggml_backend_sycl_guid() {
  15121. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  15122. return &guid;
  15123. }
  15124. GGML_CALL ggml_backend_t ggml_backend_sycl_init(int device) {
  15125. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_init\n");
  15126. ggml_init_sycl();
  15127. check_allow_gpu_index(device);
  15128. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  15129. ggml_sycl_set_main_device(device);
  15130. int id = g_sycl_gpu_mgr->gpus[device];
  15131. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context {
  15132. /* .device = */ device,
  15133. /* .name = */ GGML_SYCL_NAME + std::to_string(id),
  15134. };
  15135. ggml_backend_t sycl_backend = new ggml_backend {
  15136. /* .guid = */ ggml_backend_sycl_guid(),
  15137. /* .interface = */ ggml_backend_sycl_interface,
  15138. /* .context = */ ctx
  15139. };
  15140. return sycl_backend;
  15141. }
  15142. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  15143. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  15144. }
  15145. GGML_CALL int ggml_backend_sycl_get_device_count() {
  15146. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_count\n");
  15147. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  15148. return g_sycl_gpu_mgr->get_gpu_count();
  15149. }
  15150. GGML_CALL static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  15151. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  15152. return sycl_backend;
  15153. UNUSED(params);
  15154. }
  15155. GGML_API GGML_CALL int ggml_backend_sycl_get_device_index(int device_id) {
  15156. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_index\n");
  15157. return g_sycl_gpu_mgr->get_index(device_id);
  15158. }
  15159. GGML_API GGML_CALL int ggml_backend_sycl_get_device_id(int device_index) {
  15160. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_id\n");
  15161. return g_sycl_gpu_mgr->gpus[device_index];
  15162. }
  15163. GGML_API GGML_CALL void ggml_backend_sycl_set_single_device_mode(int main_gpu_id) {
  15164. ggml_init_sycl();
  15165. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_single_device_mode\n");
  15166. fprintf(stderr, "ggml_backend_sycl_set_single_device: use single device: [%d]\n", main_gpu_id);
  15167. GGML_ASSERT(main_gpu_id<g_all_sycl_device_count);
  15168. if (g_sycl_gpu_mgr) {
  15169. delete g_sycl_gpu_mgr;
  15170. }
  15171. g_sycl_gpu_mgr = new sycl_gpu_mgr(main_gpu_id);
  15172. g_ggml_sycl_backend_gpu_mode = SYCL_SINGLE_GPU_MODE;
  15173. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  15174. g_ggml_backend_sycl_buffer_type_initialized = false;
  15175. }
  15176. GGML_API GGML_CALL void ggml_backend_sycl_set_mul_device_mode() {
  15177. ggml_init_sycl();
  15178. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_mul_device_mode\n");
  15179. if (g_ggml_sycl_backend_gpu_mode == SYCL_MUL_GPU_MODE) {
  15180. return;
  15181. }
  15182. fprintf(stderr, "ggml_backend_sycl_set_mul_device_mode: true\n");
  15183. if (g_sycl_gpu_mgr) {
  15184. delete g_sycl_gpu_mgr;
  15185. }
  15186. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  15187. g_ggml_sycl_backend_gpu_mode = SYCL_MUL_GPU_MODE;
  15188. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  15189. g_ggml_backend_sycl_buffer_type_initialized = false;
  15190. }
  15191. extern "C" int ggml_backend_sycl_reg_devices();
  15192. int ggml_backend_sycl_reg_devices() {
  15193. ggml_backend_sycl_set_mul_device_mode();
  15194. assert(g_device_count>0);
  15195. for (int i = 0; i < g_device_count; i++) {
  15196. int id = g_sycl_gpu_mgr->gpus[i];
  15197. char name[128];
  15198. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, id);
  15199. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  15200. }
  15201. return g_device_count;
  15202. }